123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- '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 };}
- function toRelativePath(relativePath) {
- var stripped = relativePath.replace(/\/$/g, '');
- return (/^((\.\.)|(\.))($|\/)/.test(stripped) ? stripped : './' + String(stripped));
- }
- 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,
-
- 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;}() });
- }
-
- if (!importPath.startsWith('.')) {
- return;
- }
-
- 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('|\\')) + ')?$');
-
- if (options && options.noUselessIndex && regexUnnecessaryIndex.test(importPath)) {
- var parentDirectory = _path2['default'].dirname(importPath);
-
- 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);
- }
-
- if (importPath.startsWith('./')) {
- return;
- }
-
- if (resolvedPath === undefined) {
- return;
- }
- var expected = _path2['default'].relative(currentDir, resolvedPath);
- var expectedSplit = expected.split(_path2['default'].sep);
- var importPathSplit = importPath.replace(/^\.\//, '').split('/');
- var countImportPathRelativeParents = countRelativeParents(importPathSplit);
- var countExpectedRelativeParents = countRelativeParents(expectedSplit);
- var diff = countImportPathRelativeParents - countExpectedRelativeParents;
-
- if (diff <= 0) {
- return;
- }
-
- return reportWithProposedPath(
- toRelativePath(
- importPathSplit.
- slice(0, countExpectedRelativeParents).
- concat(importPathSplit.slice(countImportPathRelativeParents + diff)).
- join('/')));
- }
- return (0, _moduleVisitor2['default'])(checkSourceValue, options);
- }return create;}() };
|