odin-js-fundamentals-part-5/node_modules/eslint-plugin-import/lib/rules/no-useless-path-segments.js

147 lines
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.filter(function (x) {return x === '..';}).length;
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
category: 'Static analysis',
description: 'Forbid unnecessary path segments in import and require statements.',
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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby11c2VsZXNzLXBhdGgtc2VnbWVudHMuanMiXSwibmFtZXMiOlsidG9SZWxhdGl2ZVBhdGgiLCJyZWxhdGl2ZVBhdGgiLCJzdHJpcHBlZCIsInJlcGxhY2UiLCJ0ZXN0Iiwibm9ybWFsaXplIiwiZm4iLCJwYXRoIiwicG9zaXgiLCJjb3VudFJlbGF0aXZlUGFyZW50cyIsInBhdGhTZWdtZW50cyIsImZpbHRlciIsIngiLCJsZW5ndGgiLCJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIsInR5cGUiLCJkb2NzIiwiY2F0ZWdvcnkiLCJkZXNjcmlwdGlvbiIsInVybCIsImZpeGFibGUiLCJzY2hlbWEiLCJwcm9wZXJ0aWVzIiwiY29tbW9uanMiLCJub1VzZWxlc3NJbmRleCIsImFkZGl0aW9uYWxQcm9wZXJ0aWVzIiwiY3JlYXRlIiwiY29udGV4dCIsImN1cnJlbnREaXIiLCJkaXJuYW1lIiwiZ2V0UGh5c2ljYWxGaWxlbmFtZSIsImdldEZpbGVuYW1lIiwib3B0aW9ucyIsImNoZWNrU291cmNlVmFsdWUiLCJzb3VyY2UiLCJpbXBvcnRQYXRoIiwidmFsdWUiLCJyZXBvcnRXaXRoUHJvcG9zZWRQYXRoIiwicHJvcG9zZWRQYXRoIiwicmVwb3J0Iiwibm9kZSIsIm1lc3NhZ2UiLCJmaXgiLCJmaXhlciIsInJlcGxh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