InternetProgramming/Lab3/node_modules/eslint-plugin-import/lib/rules/no-relative-packages.js
2023-12-15 14:42:22 +04:00

71 lines
9.5 KiB
JavaScript

'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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1yZWxhdGl2ZS1wYWNrYWdlcy5qcyJdLCJuYW1lcyI6WyJ0b1Bvc2l4UGF0aCIsImZpbGVQYXRoIiwicmVwbGFjZSIsImZpbmROYW1lZFBhY2thZ2UiLCJmb3VuZCIsImN3ZCIsInBrZyIsIm5hbWUiLCJwYXRoIiwiam9pbiIsImNoZWNrSW1wb3J0Rm9yUmVsYXRpdmVQYWNrYWdlIiwiY29udGV4dCIsImltcG9ydFBhdGgiLCJub2RlIiwicG90ZW50aWFsVmlvbGF0aW9uVHlwZXMiLCJpbmRleE9mIiwicmVzb2x2ZWRJbXBvcnQiLCJyZXNvbHZlZENvbnRleHQiLCJnZXRQaHlzaWNhbEZpbGVuYW1lIiwiZ2V0RmlsZW5hbWUiLCJpbXBvcnRQa2ciLCJjb250ZXh0UGtnIiwiaW1wb3J0QmFzZU5hbWUiLCJiYXNlbmFtZSIsImltcG9ydFJvb3QiLCJkaXJuYW1lIiwicHJvcGVyUGF0aCIsInJlbGF0aXZlIiwicHJvcGVySW1wb3J0IiwicmVwb3J0IiwibWVzc2FnZSIsImZpeCIsImZpeGVyIiwicmVwbGFjZVRleHQiLCJKU09OIiwic3RyaW5naWZ5IiwibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsImNhdGV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