GoScrobble/web/node_modules/eslint-plugin-import/lib/rules/extensions.js

189 lines
19 KiB
JavaScript
Raw Normal View History

2022-04-25 02:47:15 +00: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 _moduleVisitor = require('eslint-module-utils/moduleVisitor');var _moduleVisitor2 = _interopRequireDefault(_moduleVisitor);
var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}
var enumValues = { 'enum': ['always', 'ignorePackages', 'never'] };
var patternProperties = {
type: 'object',
patternProperties: { '.*': enumValues } };
var properties = {
type: 'object',
properties: {
'pattern': patternProperties,
'ignorePackages': { type: 'boolean' } } };
function buildProperties(context) {
var result = {
defaultConfig: 'never',
pattern: {},
ignorePackages: false };
context.options.forEach(function (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 () {function create(context) {
var 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) {
var extension = _path2['default'].extname(file);
var fileWithoutExtension = file.slice(0, -extension.length);
var resolvedFileWithoutExtension = (0, _resolve2['default'])(fileWithoutExtension, context);
return resolvedFileWithoutExtension === (0, _resolve2['default'])(file, context);
}
function isExternalRootModule(file) {
var slashCount = file.split('/').length - 1;
if (slashCount === 0) return true;
if ((0, _importType.isScopedModule)(file) && slashCount <= 1) return true;
return false;
}
function checkFileExtension(source) {
// bail if the declaration doesn't have a source, e.g. "export { foo };", or if it's only partially typed like in an editor
if (!source || !source.value) return;
var importPathWithQueryString = source.value;
// don't enforce anything on builtins
if ((0, _importType.isBuiltIn)(importPathWithQueryString, context.settings)) return;
var 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;
var resolvedPath = (0, _resolve2['default'])(importPath, context);
// get extension from resolved path, if possible.
// for unresolved, use source value.
var extension = _path2['default'].extname(resolvedPath || importPath).substring(1);
// determine if this is a module
var isPackage = (0, _importType.isExternalModule)(
importPath,
context.settings,
(0, _resolve2['default'])(importPath, context),
context) ||
(0, _importType.isScoped)(importPath);
if (!extension || !importPath.endsWith('.' + String(extension))) {
var extensionRequired = isUseOfExtensionRequired(extension, isPackage);
var extensionForbidden = isUseOfExtensionForbidden(extension);
if (extensionRequired && !extensionForbidden) {
context.report({
node: source,
message: 'Missing file extension ' + (
extension ? '"' + String(extension) + '" ' : '') + 'for "' + String(importPathWithQueryString) + '"' });
}
} else if (extension) {
if (isUseOfExtensionForbidden(extension) && isResolvableWithoutExtension(importPath)) {
context.report({
node: source,
message: 'Unexpected use of file extension "' + String(extension) + '" for "' + String(importPathWithQueryString) + '"' });
}
}
}
return (0, _moduleVisitor2['default'])(checkFileExtension, { commonjs: true });
}return create;}() };
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9leHRlbnNpb25zLmpzIl0sIm5hbWVzIjpbImVudW1WYWx1ZXMiLCJwYXR0ZXJuUHJvcGVydGllcyIsInR5cGUiLCJwcm9wZXJ0aWVzIiwiYnVpbGRQcm9wZXJ0aWVzIiwiY29udGV4dCIsInJlc3VsdCIsImRlZmF1bHRDb25maWciLCJwYXR0ZXJuIiwiaWdub3JlUGFja2FnZXMiLCJvcHRpb25zIiwiZm9yRWFjaCIsIm9iaiIsInVuZGVmaW5lZCIsIk9iamVjdCIsImFzc2lnbiIsIm1vZHVsZSIsImV4cG9ydHMiLCJtZXRhIiwiZG9jcyIsInVybCIsInNjaGVtYSIsImFueU9mIiwiaXRlbXMiLCJhZGRpdGlvbmFsSXRlbXMiLCJjcmVhdGUiLCJwcm9wcyIsImdldE1vZGlmaWVyIiwiZXh0ZW5zaW9uIiwiaXNVc2VPZkV4dGVuc2lvblJlcXVpcmVkIiwiaXNQYWNrYWdlIiwiaXNVc2VPZkV4dGVuc2lvbkZvcmJpZGRlbiIsImlzUmVzb2x2YWJsZVdpdGhvdXRFeHRlbnNpb24iLCJmaWxlIiwicGF0aCIsImV4dG5hbWUiLCJmaWxlV2l0aG91dEV4dGVuc2lvbiIsInNsaWNlIiwibGVuZ3RoIiwicmVzb2x2ZWRGaWxlV2l0aG91dEV4dGVuc2lvbiIsImlzRXh0ZXJuYWxSb290TW9kdWxlIiwic2xhc2hDb3VudCIsInNwbGl0IiwiY2hlY2tGaWxlRXh0ZW5zaW9uIiwic291cmNlIiwidmFsdWUiLCJpbXBvcnRQYXRoV2l0aFF1ZXJ5U3RyaW5nIiwic2V0dGluZ3MiLCJpbXBvcnRQYXRoIiwicmVwbGFjZSIsInJlc29sdmVkUGF0aCIsInN1Y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