151 lines
16 KiB
JavaScript
151 lines
16 KiB
JavaScript
'use strict';
|
|
|
|
var _ignore = require('eslint-module-utils/ignore');
|
|
|
|
var _moduleVisitor = require('eslint-module-utils/moduleVisitor');
|
|
|
|
var _moduleVisitor2 = _interopRequireDefault(_moduleVisitor);
|
|
|
|
var _resolve = require('eslint-module-utils/resolve');
|
|
|
|
var _resolve2 = _interopRequireDefault(_resolve);
|
|
|
|
var _path = require('path');
|
|
|
|
var _path2 = _interopRequireDefault(_path);
|
|
|
|
var _docsUrl = require('../docsUrl');
|
|
|
|
var _docsUrl2 = _interopRequireDefault(_docsUrl);
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
/**
|
|
* convert a potentially relative path from node utils into a true
|
|
* relative path.
|
|
*
|
|
* ../ -> ..
|
|
* ./ -> .
|
|
* .foo/bar -> ./.foo/bar
|
|
* ..foo/bar -> ./..foo/bar
|
|
* foo/bar -> ./foo/bar
|
|
*
|
|
* @param relativePath {string} relative posix path potentially missing leading './'
|
|
* @returns {string} relative posix path that always starts with a ./
|
|
**/
|
|
function toRelativePath(relativePath) {
|
|
const stripped = relativePath.replace(/\/$/g, ''); // Remove trailing /
|
|
|
|
return (/^((\.\.)|(\.))($|\/)/.test(stripped) ? stripped : `./${stripped}`
|
|
);
|
|
} /**
|
|
* @fileOverview Ensures that there are no useless path segments
|
|
* @author Thomas Grainger
|
|
*/
|
|
|
|
function normalize(fn) {
|
|
return toRelativePath(_path2.default.posix.normalize(fn));
|
|
}
|
|
|
|
function countRelativeParents(pathSegments) {
|
|
return pathSegments.reduce((sum, pathSegment) => pathSegment === '..' ? sum + 1 : sum, 0);
|
|
}
|
|
|
|
module.exports = {
|
|
meta: {
|
|
type: 'suggestion',
|
|
docs: {
|
|
url: (0, _docsUrl2.default)('no-useless-path-segments')
|
|
},
|
|
|
|
schema: [{
|
|
type: 'object',
|
|
properties: {
|
|
commonjs: { type: 'boolean' },
|
|
noUselessIndex: { type: 'boolean' }
|
|
},
|
|
additionalProperties: false
|
|
}],
|
|
|
|
fixable: 'code'
|
|
},
|
|
|
|
create(context) {
|
|
const currentDir = _path2.default.dirname(context.getFilename());
|
|
const options = context.options[0];
|
|
|
|
function checkSourceValue(source) {
|
|
const importPath = source.value;
|
|
|
|
|
|
function reportWithProposedPath(proposedPath) {
|
|
context.report({
|
|
node: source,
|
|
// Note: Using messageIds is not possible due to the support for ESLint 2 and 3
|
|
message: `Useless path segments for "${importPath}", should be "${proposedPath}"`,
|
|
fix: fixer => proposedPath && fixer.replaceText(source, JSON.stringify(proposedPath))
|
|
});
|
|
}
|
|
|
|
// Only relative imports are relevant for this rule --> Skip checking
|
|
if (!importPath.startsWith('.')) {
|
|
return;
|
|
}
|
|
|
|
// Report rule violation if path is not the shortest possible
|
|
const resolvedPath = (0, _resolve2.default)(importPath, context);
|
|
const normedPath = normalize(importPath);
|
|
const resolvedNormedPath = (0, _resolve2.default)(normedPath, context);
|
|
if (normedPath !== importPath && resolvedPath === resolvedNormedPath) {
|
|
return reportWithProposedPath(normedPath);
|
|
}
|
|
|
|
const fileExtensions = (0, _ignore.getFileExtensions)(context.settings);
|
|
const regexUnnecessaryIndex = new RegExp(`.*\\/index(\\${Array.from(fileExtensions).join('|\\')})?$`);
|
|
|
|
// Check if path contains unnecessary index (including a configured extension)
|
|
if (options && options.noUselessIndex && regexUnnecessaryIndex.test(importPath)) {
|
|
const parentDirectory = _path2.default.dirname(importPath);
|
|
|
|
// Try to find ambiguous imports
|
|
if (parentDirectory !== '.' && parentDirectory !== '..') {
|
|
for (let fileExtension of fileExtensions) {
|
|
if ((0, _resolve2.default)(`${parentDirectory}${fileExtension}`, context)) {
|
|
return reportWithProposedPath(`${parentDirectory}/`);
|
|
}
|
|
}
|
|
}
|
|
|
|
return reportWithProposedPath(parentDirectory);
|
|
}
|
|
|
|
// Path is shortest possible + starts from the current directory --> Return directly
|
|
if (importPath.startsWith('./')) {
|
|
return;
|
|
}
|
|
|
|
// Path is not existing --> Return directly (following code requires path to be defined)
|
|
if (resolvedPath === undefined) {
|
|
return;
|
|
}
|
|
|
|
const expected = _path2.default.relative(currentDir, resolvedPath); // Expected import path
|
|
const expectedSplit = expected.split(_path2.default.sep); // Split by / or \ (depending on OS)
|
|
const importPathSplit = importPath.replace(/^\.\//, '').split('/');
|
|
const countImportPathRelativeParents = countRelativeParents(importPathSplit);
|
|
const countExpectedRelativeParents = countRelativeParents(expectedSplit);
|
|
const diff = countImportPathRelativeParents - countExpectedRelativeParents;
|
|
|
|
// Same number of relative parents --> Paths are the same --> Return directly
|
|
if (diff <= 0) {
|
|
return;
|
|
}
|
|
|
|
// Report and propose minimal number of required relative parents
|
|
return reportWithProposedPath(toRelativePath(importPathSplit.slice(0, countExpectedRelativeParents).concat(importPathSplit.slice(countImportPathRelativeParents + diff)).join('/')));
|
|
}
|
|
|
|
return (0, _moduleVisitor2.default)(checkSourceValue, options);
|
|
}
|
|
};
|
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby11c2VsZXNzLXBhdGgtc2VnbWVudHMuanMiXSwibmFtZXMiOlsidG9SZWxhdGl2ZVBhdGgiLCJyZWxhdGl2ZVBhdGgiLCJzdHJpcHBlZCIsInJlcGxhY2UiLCJ0ZXN0Iiwibm9ybWFsaXplIiwiZm4iLCJwYXRoIiwicG9zaXgiLCJjb3VudFJlbGF0aXZlUGFyZW50cyIsInBhdGhTZWdtZW50cyIsInJlZHVjZSIsInN1bSIsInBhdGhTZWdtZW50IiwibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsInVybCIsInNjaGVtYSIsInByb3BlcnRpZXMiLCJjb21tb25qcyIsIm5vVXNlbGVzc0luZGV4IiwiYWRkaXRpb25hbFByb3BlcnRpZXMiLCJmaXhhYmxlIiwiY3JlYXRlIiwiY29udGV4dCIsImN1cnJlbnRE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