odin-javascript-exercises/node_modules/eslint-plugin-import/lib/rules/no-extraneous-dependencies.js

154 lines
17 KiB
JavaScript
Raw Normal View History

2017-12-15 10:56:14 -08:00
'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 _importType = require('../core/importType');
var _importType2 = _interopRequireDefault(_importType);
var _staticRequire = require('../core/staticRequire');
var _staticRequire2 = _interopRequireDefault(_staticRequire);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function getDependencies(context, packageDir) {
try {
const packageContent = packageDir ? JSON.parse(_fs2.default.readFileSync(_path2.default.join(packageDir, 'package.json'), 'utf8')) : _readPkgUp2.default.sync({ cwd: context.getFilename(), normalize: false }).pkg;
if (!packageContent) {
return null;
}
return {
dependencies: packageContent.dependencies || {},
devDependencies: packageContent.devDependencies || {},
optionalDependencies: packageContent.optionalDependencies || {},
peerDependencies: packageContent.peerDependencies || {}
};
} catch (e) {
if (packageDir && 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) {
if ((0, _importType2.default)(name, context) !== 'external') {
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: {
docs: {},
schema: [{
'type': 'object',
'properties': {
'devDependencies': { 'type': ['boolean', 'array'] },
'optionalDependencies': { 'type': ['boolean', 'array'] },
'peerDependencies': { 'type': ['boolean', 'array'] },
'packageDir': { 'type': 'string' }
},
'additionalProperties': false
}]
},
create: function (context) {
const options = context.options[0] || {};
const filename = context.getFilename();
const deps = getDependencies(context, options.packageDir);
if (!deps) {
return {};
}
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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInJ1bGVzL25vLWV4dHJhbmVv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