PromoCursed/node_modules/eslint-plugin-import/lib/rules/no-relative-packages.js

71 lines
9.5 KiB
JavaScript
Raw Normal View History

2024-08-20 23:25:37 +04:00
'use strict';var _path = require('path');var _path2 = _interopRequireDefault(_path);
var _readPkgUp = require('eslint-module-utils/readPkgUp');var _readPkgUp2 = _interopRequireDefault(_readPkgUp);
var _resolve = require('eslint-module-utils/resolve');var _resolve2 = _interopRequireDefault(_resolve);
var _moduleVisitor = require('eslint-module-utils/moduleVisitor');var _moduleVisitor2 = _interopRequireDefault(_moduleVisitor);
var _importType = require('../core/importType');var _importType2 = _interopRequireDefault(_importType);
var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}
/** @param {string} filePath */
function toPosixPath(filePath) {
return filePath.replace(/\\/g, '/');
}
function findNamedPackage(filePath) {
var found = (0, _readPkgUp2['default'])({ cwd: filePath });
if (found.pkg && !found.pkg.name) {
return findNamedPackage(_path2['default'].join(found.path, '../..'));
}
return found;
}
function checkImportForRelativePackage(context, importPath, node) {
var potentialViolationTypes = ['parent', 'index', 'sibling'];
if (potentialViolationTypes.indexOf((0, _importType2['default'])(importPath, context)) === -1) {
return;
}
var resolvedImport = (0, _resolve2['default'])(importPath, context);
var resolvedContext = context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename();
if (!resolvedImport || !resolvedContext) {
return;
}
var importPkg = findNamedPackage(resolvedImport);
var contextPkg = findNamedPackage(resolvedContext);
if (importPkg.pkg && contextPkg.pkg && importPkg.pkg.name !== contextPkg.pkg.name) {
var importBaseName = _path2['default'].basename(importPath);
var importRoot = _path2['default'].dirname(importPkg.path);
var properPath = _path2['default'].relative(importRoot, resolvedImport);
var properImport = _path2['default'].join(
importPkg.pkg.name,
_path2['default'].dirname(properPath),
importBaseName === _path2['default'].basename(importRoot) ? '' : importBaseName);
context.report({
node: node,
message: 'Relative import from another package is not allowed. Use `' + String(properImport) + '` instead of `' + String(importPath) + '`',
fix: function () {function fix(fixer) {return fixer.replaceText(node, JSON.stringify(toPosixPath(properImport)));}return fix;}() });
}
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
category: 'Static analysis',
description: 'Forbid importing packages through relative paths.',
url: (0, _docsUrl2['default'])('no-relative-packages') },
fixable: 'code',
schema: [(0, _moduleVisitor.makeOptionsSchema)()] },
create: function () {function create(context) {
return (0, _moduleVisitor2['default'])(function (source) {return checkImportForRelativePackage(context, source.value, source);}, context.options[0]);
}return create;}() };
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1yZWxhdGl2ZS1wYWNrYWdlcy5qcyJdLCJuYW1lcyI6WyJ0b1Bvc2l4UGF0aCIsImZpbGVQYXRoIiwicmVwbGFjZSIsImZpbmROYW1lZFBhY2thZ2UiLCJmb3VuZCIsImN3ZCIsInBrZyIsIm5hbWUiLCJwYXRoIiwiam9pbiIsImNoZWNrSW1wb3J0Rm9yUmVsYXRpdmVQYWNrYWdlIiwiY29udGV4dCIsImltcG9ydFBhdGgiLCJub2RlIiwicG90ZW50aWFsVmlvbGF0aW9uVHlwZXMiLCJpbmRleE9mIiwicmVzb2x2ZWRJbXBvcnQiLCJyZXNvbHZlZENvbnRleHQiLCJnZXRQaHlzaWNhbEZpbGVuYW1lIiwiZ2V0RmlsZW5hbWUiLCJpbXBvcnRQa2ciLCJjb250ZXh0UGtnIiwiaW1wb3J0QmFzZU5hbWUiLCJiYXNlbmFtZSIsImltcG9ydFJvb3QiLCJkaXJuYW1lIiwicHJvcGVyUGF0aCIsInJlbGF0aXZlIiwicHJvcGVySW1wb3J0IiwicmVwb3J0IiwibWVzc2FnZSIsImZpeCIsImZpeGVyIiwicmVwbGFjZVRleHQiLCJKU09OIiwic3RyaW5naWZ5IiwibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsImNh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