the-forest/client/node_modules/eslint-plugin-import/lib/rules/extensions.js

193 lines
20 KiB
JavaScript
Raw Normal View History

2024-09-17 20:35:18 -04: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: {
category: 'Style guide',
description: 'Ensure consistent use of file extension within the import path.',
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) {
if (file === '.' || file === '..') {return false;}
var slashCount = file.split('/').length - 1;
if (slashCount === 0) {return true;}
if ((0, _importType.isScoped)(file) && slashCount <= 1) {return true;}
return false;
}
function checkFileExtension(source, node) {
// 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,
(0, _resolve2['default'])(importPath, context),
context) ||
(0, _importType.isScoped)(importPath);
if (!extension || !importPath.endsWith('.' + String(extension))) {
// ignore type-only imports and exports
if (node.importKind === 'type' || node.exportKind === 'type') {return;}
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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9leHRlbnNpb25zLmpzIl0sIm5hbWVzIjpbImVudW1WYWx1ZXMiLCJwYXR0ZXJuUHJvcGVydGllcyIsInR5cGUiLCJwcm9wZXJ0aWVzIiwicGF0dGVybiIsImlnbm9yZVBhY2thZ2VzIiwiYnVpbGRQcm9wZXJ0aWVzIiwiY29udGV4dCIsInJlc3VsdCIsImRlZmF1bHRDb25maWciLCJvcHRpb25zIiwiZm9yRWFjaCIsIm9iaiIsInVuZGVmaW5lZCIsIk9iamVjdCIsImFzc2lnbiIsIm1vZHVsZSIsImV4cG9ydHMiLCJtZXRhIiwiZG9jcyIsImNhdGVnb3J5IiwiZGVzY3JpcHRpb24iLCJ1cmwiLCJzY2hlbWEiLCJhbnlPZiIsIml0ZW1zIiwiYWRkaXRpb25hbEl0ZW1zIiwiY3JlYXRlIiwicHJvcHMiLCJnZXRNb2RpZmllciIsImV4dGVuc2lvbiIsImlzVXNlT2ZFeHRlbnNpb25SZXF1aXJlZCIsImlzUGFja2FnZSIsImlzVXNlT2ZFeHRlbnNpb25Gb3JiaWRkZW4iLCJpc1Jlc29sdmFibGVXaXRob3V0RXh0ZW5zaW9uIiwiZmlsZSIsInBhdGgiLCJleHRuYW1lIiwiZmlsZVdpdGhvdXRFeHRlbnNpb24iLCJzbGljZSIsImxlbmd0aCIsInJlc29sdmVkRmlsZVdpdGhvdXRFeHRlbnNpb24iLCJpc0V4dGVybmFsUm9vdE1vZHVsZSIsInNsYXNoQ291bnQiLCJzcGxpdCIsImNoZWNrRmlsZUV4dGVuc2lvbiIsIn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