79 lines
8.8 KiB
JavaScript
79 lines
8.8 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: node,
|
||
|
message: 'Imported module should be assigned' });
|
||
|
|
||
|
}
|
||
|
|
||
|
function testIsAllow(globs, filename, source) {
|
||
|
if (!Array.isArray(globs)) {
|
||
|
return false; // default doesn't allow any patterns
|
||
|
}
|
||
|
|
||
|
var filePath = void 0;
|
||
|
|
||
|
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(function (glob) {return (0, _minimatch2['default'])(filePath, glob) ||
|
||
|
(0, _minimatch2['default'])(filePath, _path2['default'].join(process.cwd(), glob));}) !==
|
||
|
undefined;
|
||
|
}
|
||
|
|
||
|
function create(context) {
|
||
|
var options = context.options[0] || {};
|
||
|
var filename = context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename();
|
||
|
var isAllow = function isAllow(source) {return testIsAllow(options.allow, filename, source);};
|
||
|
|
||
|
return {
|
||
|
ImportDeclaration: function () {function ImportDeclaration(node) {
|
||
|
if (node.specifiers.length === 0 && !isAllow(node.source.value)) {
|
||
|
report(context, node);
|
||
|
}
|
||
|
}return ImportDeclaration;}(),
|
||
|
ExpressionStatement: function () {function ExpressionStatement(node) {
|
||
|
if (
|
||
|
node.expression.type === 'CallExpression' &&
|
||
|
(0, _staticRequire2['default'])(node.expression) &&
|
||
|
!isAllow(node.expression.arguments[0].value))
|
||
|
{
|
||
|
report(context, node.expression);
|
||
|
}
|
||
|
}return ExpressionStatement;}() };
|
||
|
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
create: create,
|
||
|
meta: {
|
||
|
type: 'suggestion',
|
||
|
docs: {
|
||
|
category: 'Style guide',
|
||
|
description: 'Forbid unassigned imports',
|
||
|
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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby11bmFzc2lnbmVkLWltcG9ydC5qcyJdLCJuYW1lcyI6WyJyZXBvcnQiLCJjb250ZXh0Iiwibm9kZSIsIm1lc3NhZ2UiLCJ0ZXN0SXNBbGxvdyIsImdsb2JzIiwiZmlsZW5hbWUiLCJzb3VyY2UiLCJBcnJheSIsImlzQXJyYXkiLCJmaWxlUGF0aCIsInBhdGgiLCJyZXNvbHZlIiwiZGlybmFtZSIsImZpbmQiLCJnbG9iIiwiam9pbiIsInByb2Nlc3MiLCJjd2QiLCJ1bmRlZmluZWQiLCJjcmVhdGUiLCJvcHRpb25zIiwiZ2V0UGh5c2ljYWxGaWxlbmFtZSIsImdldEZpbGVuYW1lIiwiaXNBbGxvdyIsImFsbG93IiwiSW1wb3J0RGVjbGFyYXRpb24iLCJzcGVjaWZpZXJzIiwibGVuZ3RoIiwidmFsdWUiLCJFeHByZXNzaW9uU3RhdGVtZW50IiwiZXhwcmVzc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