12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- '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;
- }
- var filePath = void 0;
- if (source[0] !== '.' && source[0] !== '/') {
- filePath = source;
- } else {
- filePath = _path2['default'].resolve(_path2['default'].dirname(filename), source);
- }
- 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 }] } };
|