InternetProgramming/Lab3/node_modules/eslint-plugin-import/lib/rules/no-extraneous-dependencies.js

300 lines
37 KiB
JavaScript
Raw Normal View History

2023-12-15 14:42:22 +04:00
'use strict';var _slicedToArray = function () {function sliceIterator(arr, i) {var _arr = [];var _n = true;var _d = false;var _e = undefined;try {for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {_arr.push(_s.value);if (i && _arr.length === i) break;}} catch (err) {_d = true;_e = err;} finally {try {if (!_n && _i["return"]) _i["return"]();} finally {if (_d) throw _e;}}return _arr;}return function (arr, i) {if (Array.isArray(arr)) {return arr;} else if (Symbol.iterator in Object(arr)) {return sliceIterator(arr, i);} else {throw new TypeError("Invalid attempt to destructure non-iterable instance");}};}();var _path = require('path');var _path2 = _interopRequireDefault(_path);
var _fs = require('fs');var _fs2 = _interopRequireDefault(_fs);
var _pkgUp = require('eslint-module-utils/pkgUp');var _pkgUp2 = _interopRequireDefault(_pkgUp);
var _minimatch = require('minimatch');var _minimatch2 = _interopRequireDefault(_minimatch);
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 _packagePath = require('../core/packagePath');
var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}
var depFieldCache = new Map();
function hasKeys() {var obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return Object.keys(obj).length > 0;
}
function arrayOrKeys(arrayOrObject) {
return Array.isArray(arrayOrObject) ? arrayOrObject : Object.keys(arrayOrObject);
}
function readJSON(jsonPath, throwException) {
try {
return JSON.parse(_fs2['default'].readFileSync(jsonPath, 'utf8'));
} catch (err) {
if (throwException) {
throw err;
}
}
}
function extractDepFields(pkg) {
return {
dependencies: pkg.dependencies || {},
devDependencies: pkg.devDependencies || {},
optionalDependencies: pkg.optionalDependencies || {},
peerDependencies: pkg.peerDependencies || {},
// BundledDeps should be in the form of an array, but object notation is also supported by
// `npm`, so we convert it to an array if it is an object
bundledDependencies: arrayOrKeys(pkg.bundleDependencies || pkg.bundledDependencies || []) };
}
function getPackageDepFields(packageJsonPath, throwAtRead) {
if (!depFieldCache.has(packageJsonPath)) {
var depFields = extractDepFields(readJSON(packageJsonPath, throwAtRead));
depFieldCache.set(packageJsonPath, depFields);
}
return depFieldCache.get(packageJsonPath);
}
function getDependencies(context, packageDir) {
var paths = [];
try {
var packageContent = {
dependencies: {},
devDependencies: {},
optionalDependencies: {},
peerDependencies: {},
bundledDependencies: [] };
if (packageDir && packageDir.length > 0) {
if (!Array.isArray(packageDir)) {
paths = [_path2['default'].resolve(packageDir)];
} else {
paths = packageDir.map(function (dir) {return _path2['default'].resolve(dir);});
}
}
if (paths.length > 0) {
// use rule config to find package.json
paths.forEach(function (dir) {
var packageJsonPath = _path2['default'].join(dir, 'package.json');
var _packageContent = getPackageDepFields(packageJsonPath, true);
Object.keys(packageContent).forEach(function (depsKey) {
Object.assign(packageContent[depsKey], _packageContent[depsKey]);
});
});
} else {
var packageJsonPath = (0, _pkgUp2['default'])({
cwd: context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename(),
normalize: false });
// use closest package.json
Object.assign(
packageContent,
getPackageDepFields(packageJsonPath, false));
}
if (![
packageContent.dependencies,
packageContent.devDependencies,
packageContent.optionalDependencies,
packageContent.peerDependencies,
packageContent.bundledDependencies].
some(hasKeys)) {
return null;
}
return packageContent;
} catch (e) {
if (paths.length > 0 && e.code === 'ENOENT') {
context.report({
message: 'The package.json file could not be found.',
loc: { line: 0, column: 0 } });
}
if (e.name === 'JSONError' || e instanceof SyntaxError) {
context.report({
message: 'The package.json file could not be parsed: ' + String(e.message),
loc: { line: 0, column: 0 } });
}
return null;
}
}
function missingErrorMessage(packageName) {
return '\'' + String(packageName) + '\' should be listed in the project\'s dependencies. Run \'npm i -S ' + String(packageName) + '\' to add it';
}
function devDepErrorMessage(packageName) {
return '\'' + String(packageName) + '\' should be listed in the project\'s dependencies, not devDependencies.';
}
function optDepErrorMessage(packageName) {
return '\'' + String(packageName) + '\' should be listed in the project\'s dependencies, not optionalDependencies.';
}
function getModuleOriginalName(name) {var _name$split =
name.split('/'),_name$split2 = _slicedToArray(_name$split, 2),first = _name$split2[0],second = _name$split2[1];
return first.startsWith('@') ? String(first) + '/' + String(second) : first;
}
function getModuleRealName(resolved) {
return (0, _packagePath.getFilePackageName)(resolved);
}
function checkDependencyDeclaration(deps, packageName, declarationStatus) {
var newDeclarationStatus = declarationStatus || {
isInDeps: false,
isInDevDeps: false,
isInOptDeps: false,
isInPeerDeps: false,
isInBundledDeps: false };
// in case of sub package.json inside a module
// check the dependencies on all hierarchy
var packageHierarchy = [];
var packageNameParts = packageName ? packageName.split('/') : [];
packageNameParts.forEach(function (namePart, index) {
if (!namePart.startsWith('@')) {
var ancestor = packageNameParts.slice(0, index + 1).join('/');
packageHierarchy.push(ancestor);
}
});
return packageHierarchy.reduce(function (result, ancestorName) {return {
isInDeps: result.isInDeps || deps.dependencies[ancestorName] !== undefined,
isInDevDeps: result.isInDevDeps || deps.devDependencies[ancestorName] !== undefined,
isInOptDeps: result.isInOptDeps || deps.optionalDependencies[ancestorName] !== undefined,
isInPeerDeps: result.isInPeerDeps || deps.peerDependencies[ancestorName] !== undefined,
isInBundledDeps:
result.isInBundledDeps || deps.bundledDependencies.indexOf(ancestorName) !== -1 };},
newDeclarationStatus);
}
function reportIfMissing(context, deps, depsOptions, node, name) {
// Do not report when importing types unless option is enabled
if (
!depsOptions.verifyTypeImports && (
node.importKind === 'type' ||
node.importKind === 'typeof' ||
Array.isArray(node.specifiers) && node.specifiers.length && node.specifiers.every(function (specifier) {return specifier.importKind === 'type' || specifier.importKind === 'typeof';})))
{
return;
}
var typeOfImport = (0, _importType2['default'])(name, context);
if (
typeOfImport !== 'external' && (
typeOfImport !== 'internal' || !depsOptions.verifyInternalDeps))
{
return;
}
var resolved = (0, _resolve2['default'])(name, context);
if (!resolved) {return;}
var importPackageName = getModuleOriginalName(name);
var declarationStatus = checkDependencyDeclaration(deps, importPackageName);
if (
declarationStatus.isInDeps ||
depsOptions.allowDevDeps && declarationStatus.isInDevDeps ||
depsOptions.allowPeerDeps && declarationStatus.isInPeerDeps ||
depsOptions.allowOptDeps && declarationStatus.isInOptDeps ||
depsOptions.allowBundledDeps && declarationStatus.isInBundledDeps)
{
return;
}
// test the real name from the resolved package.json
// if not aliased imports (alias/react for example), importPackageName can be misinterpreted
var realPackageName = getModuleRealName(resolved);
if (realPackageName && realPackageName !== importPackageName) {
declarationStatus = checkDependencyDeclaration(deps, realPackageName, declarationStatus);
if (
declarationStatus.isInDeps ||
depsOptions.allowDevDeps && declarationStatus.isInDevDeps ||
depsOptions.allowPeerDeps && declarationStatus.isInPeerDeps ||
depsOptions.allowOptDeps && declarationStatus.isInOptDeps ||
depsOptions.allowBundledDeps && declarationStatus.isInBundledDeps)
{
return;
}
}
if (declarationStatus.isInDevDeps && !depsOptions.allowDevDeps) {
context.report(node, devDepErrorMessage(realPackageName || importPackageName));
return;
}
if (declarationStatus.isInOptDeps && !depsOptions.allowOptDeps) {
context.report(node, optDepErrorMessage(realPackageName || importPackageName));
return;
}
context.report(node, missingErrorMessage(realPackageName || importPackageName));
}
function testConfig(config, filename) {
// Simplest configuration first, either a boolean or nothing.
if (typeof config === 'boolean' || typeof config === 'undefined') {
return config;
}
// Array of globs.
return config.some(function (c) {return (0, _minimatch2['default'])(filename, c) ||
(0, _minimatch2['default'])(filename, _path2['default'].join(process.cwd(), c));});
}
module.exports = {
meta: {
type: 'problem',
docs: {
category: 'Helpful warnings',
description: 'Forbid the use of extraneous packages.',
url: (0, _docsUrl2['default'])('no-extraneous-dependencies') },
schema: [
{
type: 'object',
properties: {
devDependencies: { type: ['boolean', 'array'] },
optionalDependencies: { type: ['boolean', 'array'] },
peerDependencies: { type: ['boolean', 'array'] },
bundledDependencies: { type: ['boolean', 'array'] },
packageDir: { type: ['string', 'array'] },
includeInternal: { type: ['boolean'] },
includeTypes: { type: ['boolean'] } },
additionalProperties: false }] },
create: function () {function create(context) {
var options = context.options[0] || {};
var filename = context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename();
var deps = getDependencies(context, options.packageDir) || extractDepFields({});
var depsOptions = {
allowDevDeps: testConfig(options.devDependencies, filename) !== false,
allowOptDeps: testConfig(options.optionalDependencies, filename) !== false,
allowPeerDeps: testConfig(options.peerDependencies, filename) !== false,
allowBundledDeps: testConfig(options.bundledDependencies, filename) !== false,
verifyInternalDeps: !!options.includeInternal,
verifyTypeImports: !!options.includeTypes };
return (0, _moduleVisitor2['default'])(function (source, node) {
reportIfMissing(context, deps, depsOptions, node, source.value);
}, { commonjs: true });
}return create;}(),
'Program:exit': function () {function ProgramExit() {
depFieldCache.clear();
}return ProgramExit;}() };
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1leHRyYW5lb3VzLWRlcGVuZGVuY2llcy5qcyJdLCJuYW1lcyI6WyJkZXBGaWVsZENhY2hlIiwiTWFwIiwiaGFzS2V5cyIsIm9iaiIsIk9iamVjdCIsImtleXMiLCJsZW5ndGgiLCJhcnJheU9yS2V5cyIsImFycmF5T3JPYmplY3QiLCJBcnJheSIsImlzQXJyYXkiLCJyZWFkSlNPTiIsImpzb25QYXRoIiwidGhyb3dFeGNlcHRpb24iLCJKU09OIiwicGFyc2UiLCJmcyIsInJlYWRGaWxlU3luYyIsImVyciIsImV4dHJhY3REZXBGaWVsZHMiLCJwa2ciLCJkZXBlbmRlbmNpZXMiLCJkZXZEZXBlbmRlbmNpZXMiLCJvcHRpb25hbERlcGVuZGVuY2llcyIsInBlZXJEZXBlbmRlbmNpZXMiLCJidW5kbGVkRGVwZW5kZW5jaWV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