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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInJ1bGVzL2V4dGVuc2lvbnMuanMiXSwibmFtZXMiOlsiZW51bVZhbHVlcyIsImVudW0iLCJwYXR0ZXJuUHJvcGVydGllcyIsInR5cGUiLCJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIsImRvY3MiLCJzY2hlbWEiLCJhbnlPZiIsIml0ZW1zIiwiYWRka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