webhook/node_modules/eslint-plugin-import/lib/rules/no-useless-path-segments.js
2022-11-10 20:43:16 +10:00

145 lines
No EOL
20 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) {
var stripped = relativePath.replace(/\/$/g, ''); // Remove trailing /
return (/^((\.\.)|(\.))($|\/)/.test(stripped) ? stripped : './' + String(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(function (sum, pathSegment) {return pathSegment === '..' ? sum + 1 : sum;}, 0);
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
url: (0, _docsUrl2['default'])('no-useless-path-segments') },
fixable: 'code',
schema: [
{
type: 'object',
properties: {
commonjs: { type: 'boolean' },
noUselessIndex: { type: 'boolean' } },
additionalProperties: false }] },
create: function () {function create(context) {
var currentDir = _path2['default'].dirname(context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename());
var options = context.options[0];
function checkSourceValue(source) {var
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 "' + String(importPath) + '", should be "' + String(proposedPath) + '"',
fix: function () {function fix(fixer) {return proposedPath && fixer.replaceText(source, JSON.stringify(proposedPath));}return fix;}() });
}
// Only relative imports are relevant for this rule --> Skip checking
if (!importPath.startsWith('.')) {
return;
}
// Report rule violation if path is not the shortest possible
var resolvedPath = (0, _resolve2['default'])(importPath, context);
var normedPath = normalize(importPath);
var resolvedNormedPath = (0, _resolve2['default'])(normedPath, context);
if (normedPath !== importPath && resolvedPath === resolvedNormedPath) {
return reportWithProposedPath(normedPath);
}
var fileExtensions = (0, _ignore.getFileExtensions)(context.settings);
var regexUnnecessaryIndex = new RegExp('.*\\/index(\\' + String(
Array.from(fileExtensions).join('|\\')) + ')?$');
// Check if path contains unnecessary index (including a configured extension)
if (options && options.noUselessIndex && regexUnnecessaryIndex.test(importPath)) {
var parentDirectory = _path2['default'].dirname(importPath);
// Try to find ambiguous imports
if (parentDirectory !== '.' && parentDirectory !== '..') {var _iteratorNormalCompletion = true;var _didIteratorError = false;var _iteratorError = undefined;try {
for (var _iterator = fileExtensions[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {var fileExtension = _step.value;
if ((0, _resolve2['default'])('' + String(parentDirectory) + String(fileExtension), context)) {
return reportWithProposedPath(String(parentDirectory) + '/');
}
}} catch (err) {_didIteratorError = true;_iteratorError = err;} finally {try {if (!_iteratorNormalCompletion && _iterator['return']) {_iterator['return']();}} finally {if (_didIteratorError) {throw _iteratorError;}}}
}
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;
}
var expected = _path2['default'].relative(currentDir, resolvedPath); // Expected import path
var expectedSplit = expected.split(_path2['default'].sep); // Split by / or \ (depending on OS)
var importPathSplit = importPath.replace(/^\.\//, '').split('/');
var countImportPathRelativeParents = countRelativeParents(importPathSplit);
var countExpectedRelativeParents = countRelativeParents(expectedSplit);
var 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);
}return create;}() };
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby11c2VsZXNzLXBhdGgtc2VnbWVudHMuanMiXSwibmFtZXMiOlsidG9SZWxhdGl2ZVBhdGgiLCJyZWxhdGl2ZVBhdGgiLCJzdHJpcHBlZCIsInJlcGxhY2UiLCJ0ZXN0Iiwibm9ybWFsaXplIiwiZm4iLCJwYXRoIiwicG9zaXgiLCJjb3VudFJlbGF0aXZlUGFyZW50cyIsInBhdGhTZWdtZW50cyIsInJlZHVjZSIsInN1bSIsInBhdGhTZWdtZW50IiwibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsInVybCIsImZpeGFibGUiLCJzY2hlbWEiLCJwcm9wZXJ0aWVzIiwiY29tbW9uanMiLCJub1VzZWxlc3NJbmRleCIsImFkZGl0aW9uYWxQcm9wZXJ0aWVzIiwiY3JlYXRlIiwiY29udGV4dCIsImN1cnJlbnREaXIiLCJkaXJuYW1lIiwiZ2V0UGh5c2ljYWxGaWxlbmFtZSIsImdldEZpbGVuYW1lIiwib3B0aW9ucyIsImNoZWNrU291cmNlVmFsdWUiLCJzb3VyY2UiLCJpbXBvcnRQYXRoIiwidmFsdWUiLCJyZXBvcnRXaXRoUHJvcG9zZWRQYXRoIiwicHJvcG9zZWRQYXRoIiwicmVwb3J0Iiwibm9kZSIsIm1lc3NhZ2UiLCJmaXgiLCJmaXhlciIsInJlcGxhY2VUZXh0IiwiSlNPTiIsInN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