88 lines
7.9 KiB
JavaScript
88 lines
7.9 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
var _path = require('path');
|
||
|
|
||
|
var _path2 = _interopRequireDefault(_path);
|
||
|
|
||
|
var _minimatch = require('minimatch');
|
||
|
|
||
|
var _minimatch2 = _interopRequireDefault(_minimatch);
|
||
|
|
||
|
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 report(context, node) {
|
||
|
context.report({
|
||
|
node,
|
||
|
message: 'Imported module should be assigned'
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function testIsAllow(globs, filename, source) {
|
||
|
if (!Array.isArray(globs)) {
|
||
|
return false; // default doesn't allow any patterns
|
||
|
}
|
||
|
|
||
|
let filePath;
|
||
|
|
||
|
if (source[0] !== '.' && source[0] !== '/') {
|
||
|
// a node module
|
||
|
filePath = source;
|
||
|
} else {
|
||
|
filePath = _path2.default.resolve(_path2.default.dirname(filename), source); // get source absolute path
|
||
|
}
|
||
|
|
||
|
return globs.find(glob => (0, _minimatch2.default)(filePath, glob) || (0, _minimatch2.default)(filePath, _path2.default.join(process.cwd(), glob))) !== undefined;
|
||
|
}
|
||
|
|
||
|
function create(context) {
|
||
|
const options = context.options[0] || {};
|
||
|
const filename = context.getFilename();
|
||
|
const isAllow = source => testIsAllow(options.allow, filename, source);
|
||
|
|
||
|
return {
|
||
|
ImportDeclaration(node) {
|
||
|
if (node.specifiers.length === 0 && !isAllow(node.source.value)) {
|
||
|
report(context, node);
|
||
|
}
|
||
|
},
|
||
|
ExpressionStatement(node) {
|
||
|
if (node.expression.type === 'CallExpression' && (0, _staticRequire2.default)(node.expression) && !isAllow(node.expression.arguments[0].value)) {
|
||
|
report(context, node.expression);
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
create,
|
||
|
meta: {
|
||
|
type: 'suggestion',
|
||
|
docs: {
|
||
|
url: (0, _docsUrl2.default)('no-unassigned-import')
|
||
|
},
|
||
|
schema: [{
|
||
|
'type': 'object',
|
||
|
'properties': {
|
||
|
'devDependencies': { 'type': ['boolean', 'array'] },
|
||
|
'optionalDependencies': { 'type': ['boolean', 'array'] },
|
||
|
'peerDependencies': { 'type': ['boolean', 'array'] },
|
||
|
'allow': {
|
||
|
'type': 'array',
|
||
|
'items': {
|
||
|
'type': 'string'
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
'additionalProperties': false
|
||
|
}]
|
||
|
}
|
||
|
};
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby11bmFzc2lnbmVkLWltcG9ydC5qcyJdLCJuYW1lcyI6WyJyZXBvcnQiLCJjb250ZXh0Iiwibm9kZSIsIm1lc3NhZ2UiLCJ0ZXN0SXNBbGxvdyIsImdsb2JzIiwiZmlsZW5hbWUiLCJzb3VyY2UiLCJBcnJheSIsImlzQXJyYXkiLCJmaWxlUGF0aCIsInBhdGgiLCJyZXNvbHZlIiwiZGlybmFtZSIsImZpbmQiLCJnbG9iIiwiam9pbiIsInByb2Nlc3MiLCJjd2QiLCJ1bmRlZmluZWQiLCJjcmVhdGUiLCJvcHRpb25zIiwiZ2V0RmlsZW5hbWUiLCJpc0FsbG93IiwiYWxsb3ciLCJJbXBvcnREZWNsYXJhdGlvbiIsInNwZWNpZmllcnMiLCJsZW5ndGgiLCJ2YWx1ZSIsIkV4cHJlc3Npb25TdGF0ZW1lbnQiLCJleHByZXNzaW9uIiwidHlwZSIsImFyZ3VtZW50cyIsIm1vZHVsZSIsImV4cG9ydHMiLCJtZXRhIiwiZG9jcyIsInVybCIsInN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