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

69 lines
9.2 KiB
JavaScript
Raw Normal View History

2022-06-23 02:27:43 +02: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: {
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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1yZWxhdGl2ZS1wYWNrYWdlcy5qcyJdLCJuYW1lcyI6WyJ0b1Bvc2l4UGF0aCIsImZpbGVQYXRoIiwicmVwbGFjZSIsImZpbmROYW1lZFBhY2thZ2UiLCJmb3VuZCIsImN3ZCIsInBrZyIsIm5hbWUiLCJwYXRoIiwiam9pbiIsImNoZWNrSW1wb3J0Rm9yUmVsYXRpdmVQYWNrYWdlIiwiY29udGV4dCIsImltcG9ydFBhdGgiLCJub2RlIiwicG90ZW50aWFsVmlvbGF0aW9uVHlwZXMiLCJpbmRleE9mIiwicmVzb2x2ZWRJbXBvcnQiLCJyZXNvbHZlZENvbnRleHQiLCJnZXRQaHlzaWNhbEZpbGVuYW1lIiwiZ2V0RmlsZW5hbWUiLCJpbXBvcnRQa2ciLCJjb250ZXh0UGtnIiwiaW1wb3J0QmFzZU5hbWUiLCJiYXNlbmFtZSIsImltcG9ydFJvb3QiLCJkaXJuYW1lIiwicHJvcGVyUGF0aCIsInJlbGF0aXZlIiwicHJvcGVySW1wb3J0IiwicmVwb3J0IiwibWVzc2FnZSIsImZpeCIsImZpeGVyIiwicmVwbGFjZVRleHQiLCJKU09OIiwic3RyaW5naWZ5IiwibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsInVybCIsImZpeGFibGUiLCJzY2hlbWEiLCJjcmVh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