@rushstack/eslint-plugin
Version:
An ESLint plugin providing supplementary rules for use with the @rushstack/eslint-config package
102 lines • 4.9 kB
JavaScript
;
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizedImportsRule = exports.MESSAGE_ID = void 0;
const path = __importStar(require("node:path"));
const LintUtilities_1 = require("./LintUtilities");
exports.MESSAGE_ID = 'error-normalized-imports';
exports.normalizedImportsRule = {
defaultOptions: [],
meta: {
type: 'suggestion',
messages: {
[exports.MESSAGE_ID]: 'The specified import target path was not provided in a normalized form.'
},
schema: [],
docs: {
description: 'Prevents and normalizes references to relative imports using paths that make unnecessary ' +
'traversals (ex. "../blah/module" in directory "blah" -> "./module")',
url: 'https://www.npmjs.com/package/@rushstack/eslint-plugin'
},
fixable: 'code'
},
create: (context) => {
const checkImportExpression = (importExpression) => {
if (!importExpression) {
// Can't validate, return
return;
}
// Determine the target file path and find the most direct relative path from the source file
const importSpecifier = (0, LintUtilities_1.parseImportSpecifierFromExpression)(importExpression);
if (!importSpecifier || !importSpecifier.importTarget.startsWith('.')) {
// Can't validate, return
return;
}
const { importTarget } = importSpecifier;
const parentDirectory = path.dirname((0, LintUtilities_1.getFilePathFromContext)(context));
const absoluteImportPath = path.resolve(parentDirectory, importTarget);
const relativeImportPath = path.relative(parentDirectory, absoluteImportPath);
// Reconstruct the import target using posix separators and manually re-add the leading './' if needed
let normalizedImportPath = path.sep !== '/' ? relativeImportPath.replace(/\\/g, '/') : relativeImportPath;
if (!normalizedImportPath.startsWith('.')) {
normalizedImportPath = `.${normalizedImportPath ? '/' : ''}${normalizedImportPath}`;
}
// If they don't match, suggest the normalized path as a fix
if (importTarget !== normalizedImportPath) {
context.report({
node: importExpression,
messageId: exports.MESSAGE_ID,
fix: (fixer) => {
// Re-include stripped loader and query strings, if provided
const normalizedSpecifier = (0, LintUtilities_1.serializeImportSpecifier)({
...importSpecifier,
importTarget: normalizedImportPath
});
return fixer.replaceText(importExpression, `'${normalizedSpecifier}'`);
}
});
}
};
return {
ImportDeclaration: (node) => checkImportExpression(node.source),
ImportExpression: (node) => checkImportExpression(node.source),
ExportAllDeclaration: (node) => checkImportExpression(node.source),
ExportNamedDeclaration: (node) => checkImportExpression(node.source)
};
}
};
//# sourceMappingURL=normalized-imports.js.map