github-pages-deploy-action/node_modules/eslint-plugin-import/lib/rules/extensions.js

189 lines
19 KiB
JavaScript
Raw Normal View History

2020-03-07 11:45:40 +08:00
'use strict';
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _resolve = require('eslint-module-utils/resolve');
var _resolve2 = _interopRequireDefault(_resolve);
var _importType = require('../core/importType');
var _docsUrl = require('../docsUrl');
var _docsUrl2 = _interopRequireDefault(_docsUrl);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const enumValues = { enum: ['always', 'ignorePackages', 'never'] };
const patternProperties = {
type: 'object',
patternProperties: { '.*': enumValues }
};
const properties = {
type: 'object',
properties: {
'pattern': patternProperties,
'ignorePackages': { type: 'boolean' }
}
};
function buildProperties(context) {
const result = {
defaultConfig: 'never',
pattern: {},
ignorePackages: false
};
context.options.forEach(obj => {
// If this is a string, set defaultConfig to its value
if (typeof obj === 'string') {
result.defaultConfig = obj;
return;
}
// If this is not the new structure, transfer all props to result.pattern
if (obj.pattern === undefined && obj.ignorePackages === undefined) {
Object.assign(result.pattern, obj);
return;
}
// If pattern is provided, transfer all props
if (obj.pattern !== undefined) {
Object.assign(result.pattern, obj.pattern);
}
// If ignorePackages is provided, transfer it to result
if (obj.ignorePackages !== undefined) {
result.ignorePackages = obj.ignorePackages;
}
});
if (result.defaultConfig === 'ignorePackages') {
result.defaultConfig = 'always';
result.ignorePackages = true;
}
return result;
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
url: (0, _docsUrl2.default)('extensions')
},
schema: {
anyOf: [{
type: 'array',
items: [enumValues],
additionalItems: false
}, {
type: 'array',
items: [enumValues, properties],
additionalItems: false
}, {
type: 'array',
items: [properties],
additionalItems: false
}, {
type: 'array',
items: [patternProperties],
additionalItems: false
}, {
type: 'array',
items: [enumValues, patternProperties],
additionalItems: false
}]
}
},
create: function (context) {
const props = buildProperties(context);
function getModifier(extension) {
return props.pattern[extension] || props.defaultConfig;
}
function isUseOfExtensionRequired(extension, isPackage) {
return getModifier(extension) === 'always' && (!props.ignorePackages || !isPackage);
}
function isUseOfExtensionForbidden(extension) {
return getModifier(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 isExternalRootModule(file) {
const slashCount = file.split('/').length - 1;
if ((0, _importType.isScopedModule)(file) && slashCount <= 1) return true;
if ((0, _importType.isExternalModule)(file, context, (0, _resolve2.default)(file, context)) && !slashCount) return true;
return false;
}
function checkFileExtension(node) {
const source = node.source;
// bail if the declaration doesn't have a source, e.g. "export { foo };"
if (!source) return;
const importPathWithQueryString = source.value;
// don't enforce anything on builtins
if ((0, _importType.isBuiltIn)(importPathWithQueryString, context.settings)) return;
const importPath = importPathWithQueryString.replace(/\?(.*)$/, '');
// don't enforce in root external packages as they may have names with `.js`.
// Like `import Decimal from decimal.js`)
if (isExternalRootModule(importPath)) 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);
// determine if this is a module
const isPackage = (0, _importType.isExternalModule)(importPath, context.settings) || (0, _importType.isScoped)(importPath);
if (!extension || !importPath.endsWith(`.${extension}`)) {
const extensionRequired = isUseOfExtensionRequired(extension, isPackage);
const extensionForbidden = isUseOfExtensionForbidden(extension);
if (extensionRequired && !extensionForbidden) {
context.report({
node: source,
message: `Missing file extension ${extension ? `"${extension}" ` : ''}for "${importPathWithQueryString}"`
});
}
} else if (extension) {
if (isUseOfExtensionForbidden(extension) && isResolvableWithoutExtension(importPath)) {
context.report({
node: source,
message: `Unexpected use of file extension "${extension}" for "${importPathWithQueryString}"`
});
}
}
}
return {
ImportDeclaration: checkFileExtension,
ExportNamedDeclaration: checkFileExtension
};
}
};
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ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