109 lines
11 KiB
JavaScript
109 lines
11 KiB
JavaScript
'use strict';
|
|
|
|
var _path = require('path');
|
|
|
|
var _path2 = _interopRequireDefault(_path);
|
|
|
|
var _has = require('has');
|
|
|
|
var _has2 = _interopRequireDefault(_has);
|
|
|
|
var _resolve = require('eslint-module-utils/resolve');
|
|
|
|
var _resolve2 = _interopRequireDefault(_resolve);
|
|
|
|
var _importType = require('../core/importType');
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
const enumValues = { enum: ['always', 'never'] };
|
|
const patternProperties = {
|
|
type: 'object',
|
|
patternProperties: { '.*': enumValues }
|
|
};
|
|
|
|
module.exports = {
|
|
meta: {
|
|
docs: {},
|
|
|
|
schema: {
|
|
anyOf: [{
|
|
type: 'array',
|
|
items: [enumValues],
|
|
additionalItems: false
|
|
}, {
|
|
type: 'array',
|
|
items: [patternProperties],
|
|
additionalItems: false
|
|
}, {
|
|
type: 'array',
|
|
items: [enumValues, patternProperties],
|
|
additionalItems: false
|
|
}]
|
|
}
|
|
},
|
|
|
|
create: function (context) {
|
|
const configuration = context.options[0] || 'never';
|
|
const defaultConfig = typeof configuration === 'string' ? configuration : null;
|
|
const modifiers = Object.assign({}, typeof configuration === 'object' ? configuration : context.options[1]);
|
|
|
|
function isUseOfExtensionRequired(extension) {
|
|
if (!(0, _has2.default)(modifiers, extension)) {
|
|
modifiers[extension] = defaultConfig;
|
|
}
|
|
return modifiers[extension] === 'always';
|
|
}
|
|
|
|
function isUseOfExtensionForbidden(extension) {
|
|
if (!(0, _has2.default)(modifiers, extension)) {
|
|
modifiers[extension] = defaultConfig;
|
|
}
|
|
return modifiers[extension] === 'never';
|
|
}
|
|
|
|
function isResolvableWithoutExtension(file) {
|
|
const extension = _path2.default.extname(file);
|
|
const fileWithoutExtension = file.slice(0, -extension.length);
|
|
const resolvedFileWithoutExtension = (0, _resolve2.default)(fileWithoutExtension, context);
|
|
|
|
return resolvedFileWithoutExtension === (0, _resolve2.default)(file, context);
|
|
}
|
|
|
|
function checkFileExtension(node) {
|
|
const source = node.source;
|
|
|
|
const importPath = source.value;
|
|
|
|
// don't enforce anything on builtins
|
|
if ((0, _importType.isBuiltIn)(importPath, context.settings)) return;
|
|
|
|
const resolvedPath = (0, _resolve2.default)(importPath, context);
|
|
|
|
// get extension from resolved path, if possible.
|
|
// for unresolved, use source value.
|
|
const extension = _path2.default.extname(resolvedPath || importPath).substring(1);
|
|
|
|
if (!extension || !importPath.endsWith(extension)) {
|
|
if (isUseOfExtensionRequired(extension) && !isUseOfExtensionForbidden(extension)) {
|
|
context.report({
|
|
node: source,
|
|
message: `Missing file extension ${extension ? `"${extension}" ` : ''}for "${importPath}"`
|
|
});
|
|
}
|
|
} else if (extension) {
|
|
if (isUseOfExtensionForbidden(extension) && isResolvableWithoutExtension(importPath)) {
|
|
context.report({
|
|
node: source,
|
|
message: `Unexpected use of file extension "${extension}" for "${importPath}"`
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
return {
|
|
ImportDeclaration: checkFileExtension
|
|
};
|
|
}
|
|
};
|
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInJ1bGVzL2V4dGVuc2lvbnMuanMiXSwibmFtZXMiOlsiZW51bVZhbHVlcyIsImVudW0iLCJwYXR0ZXJuUHJvcGVydGllcyIsInR5cGUiLCJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIsImRvY3MiLCJzY2hlbWEiLCJhbnlPZiIsIml0ZW1zIiwiYWRkaXRpb25hbEl0ZW1zIiwiY3JlYXRlIiwiY29udGV4dCIsImNvbmZpZ3VyYXRpb24iLCJvcHRpb25zIiwiZGVmYXVsdENvbmZpZyIsIm1vZGlmaWVycyIsIk9iamVjdCIsImFzc2lnbiIsImlzVXNlT2ZFeHRlbnNpb25SZXF1aXJlZCIsImV4dGVuc2lvbiIsImlzVXNlT2ZFeHRlbnNpb25Gb3JiaWRkZW4iL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