bigint-crypto-utils/node_modules/eslint-plugin-import/lib/rules/no-extraneous-dependencies.js

206 lines
22 KiB
JavaScript
Raw Normal View History

'use strict';
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _readPkgUp = require('read-pkg-up');
var _readPkgUp2 = _interopRequireDefault(_readPkgUp);
var _minimatch = require('minimatch');
var _minimatch2 = _interopRequireDefault(_minimatch);
var _resolve = require('eslint-module-utils/resolve');
var _resolve2 = _interopRequireDefault(_resolve);
var _importType = require('../core/importType');
var _importType2 = _interopRequireDefault(_importType);
var _staticRequire = require('../core/staticRequire');
var _staticRequire2 = _interopRequireDefault(_staticRequire);
var _docsUrl = require('../docsUrl');
var _docsUrl2 = _interopRequireDefault(_docsUrl);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function hasKeys() {
let obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return Object.keys(obj).length > 0;
}
function extractDepFields(pkg) {
return {
dependencies: pkg.dependencies || {},
devDependencies: pkg.devDependencies || {},
optionalDependencies: pkg.optionalDependencies || {},
peerDependencies: pkg.peerDependencies || {}
};
}
function getDependencies(context, packageDir) {
let paths = [];
try {
const packageContent = {
dependencies: {},
devDependencies: {},
optionalDependencies: {},
peerDependencies: {}
};
if (packageDir && packageDir.length > 0) {
if (!Array.isArray(packageDir)) {
paths = [_path2.default.resolve(packageDir)];
} else {
paths = packageDir.map(dir => _path2.default.resolve(dir));
}
}
if (paths.length > 0) {
// use rule config to find package.json
paths.forEach(dir => {
const _packageContent = extractDepFields(JSON.parse(_fs2.default.readFileSync(_path2.default.join(dir, 'package.json'), 'utf8')));
Object.keys(packageContent).forEach(depsKey => Object.assign(packageContent[depsKey], _packageContent[depsKey]));
});
} else {
// use closest package.json
Object.assign(packageContent, extractDepFields(_readPkgUp2.default.sync({ cwd: context.getFilename(), normalize: false }).pkg));
}
if (![packageContent.dependencies, packageContent.devDependencies, packageContent.optionalDependencies, packageContent.peerDependencies].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: ' + e.message,
loc: { line: 0, column: 0 }
});
}
return null;
}
}
function missingErrorMessage(packageName) {
return `'${packageName}' should be listed in the project's dependencies. ` + `Run 'npm i -S ${packageName}' to add it`;
}
function devDepErrorMessage(packageName) {
return `'${packageName}' should be listed in the project's dependencies, not devDependencies.`;
}
function optDepErrorMessage(packageName) {
return `'${packageName}' should be listed in the project's dependencies, ` + `not optionalDependencies.`;
}
function reportIfMissing(context, deps, depsOptions, node, name) {
// Do not report when importing types
if (node.importKind === 'type') {
return;
}
if ((0, _importType2.default)(name, context) !== 'external') {
return;
}
const resolved = (0, _resolve2.default)(name, context);
if (!resolved) {
return;
}
const splitName = name.split('/');
const packageName = splitName[0][0] === '@' ? splitName.slice(0, 2).join('/') : splitName[0];
const isInDeps = deps.dependencies[packageName] !== undefined;
const isInDevDeps = deps.devDependencies[packageName] !== undefined;
const isInOptDeps = deps.optionalDependencies[packageName] !== undefined;
const isInPeerDeps = deps.peerDependencies[packageName] !== undefined;
if (isInDeps || depsOptions.allowDevDeps && isInDevDeps || depsOptions.allowPeerDeps && isInPeerDeps || depsOptions.allowOptDeps && isInOptDeps) {
return;
}
if (isInDevDeps && !depsOptions.allowDevDeps) {
context.report(node, devDepErrorMessage(packageName));
return;
}
if (isInOptDeps && !depsOptions.allowOptDeps) {
context.report(node, optDepErrorMessage(packageName));
return;
}
context.report(node, missingErrorMessage(packageName));
}
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(c => (0, _minimatch2.default)(filename, c) || (0, _minimatch2.default)(filename, _path2.default.join(process.cwd(), c)));
}
module.exports = {
meta: {
type: 'problem',
docs: {
url: (0, _docsUrl2.default)('no-extraneous-dependencies')
},
schema: [{
'type': 'object',
'properties': {
'devDependencies': { 'type': ['boolean', 'array'] },
'optionalDependencies': { 'type': ['boolean', 'array'] },
'peerDependencies': { 'type': ['boolean', 'array'] },
'packageDir': { 'type': ['string', 'array'] }
},
'additionalProperties': false
}]
},
create: function (context) {
const options = context.options[0] || {};
const filename = context.getFilename();
const deps = getDependencies(context, options.packageDir) || extractDepFields({});
const depsOptions = {
allowDevDeps: testConfig(options.devDependencies, filename) !== false,
allowOptDeps: testConfig(options.optionalDependencies, filename) !== false,
allowPeerDeps: testConfig(options.peerDependencies, filename) !== false
// todo: use module visitor from module-utils core
};return {
ImportDeclaration: function (node) {
reportIfMissing(context, deps, depsOptions, node, node.source.value);
},
CallExpression: function handleRequires(node) {
if ((0, _staticRequire2.default)(node)) {
reportIfMissing(context, deps, depsOptions, node, node.arguments[0].value);
}
}
};
}
};
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1leHRyYW5lb3VzLWRlcGVuZGVuY2llcy5qcyJdLCJuYW1lcyI6WyJoYXNLZXlzIiwib2JqIiwiT2JqZWN0Iiwia2V5cyIsImxlbmd0aCIsImV4dHJhY3REZXBGaWVsZHMiLCJwa2ciLCJkZXBlbmRlbmNpZXMiLCJkZXZEZXBlbmRlbmNpZXMiLCJvcHRpb25hbERlcGVuZGVuY2llcyIsInBlZXJEZXBlbmRlbmNpZXMiLCJnZXREZXBlbmRlbmNpZXMiLCJjb250ZXh0IiwicGFja2FnZURpciIsInBhdGhzIiwicGFja2FnZUNvbnRlbnQiLCJBcnJheSIsImlzQXJyYXkiLCJwYXRoIiwicmVzb2x2ZSIsIm1hcCIsImRpciIsImZvckVhY2giLCJfcGFja2FnZUNvbnRlbnQiLCJKU09OIiwicGFyc2UiLCJmcyIsInJlYWRGaWxlU3luYyIs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