no-unassigned-import.js 7.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. 'use strict';var _path = require('path');var _path2 = _interopRequireDefault(_path);
  2. var _minimatch = require('minimatch');var _minimatch2 = _interopRequireDefault(_minimatch);
  3. var _staticRequire = require('../core/staticRequire');var _staticRequire2 = _interopRequireDefault(_staticRequire);
  4. var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
  5. function report(context, node) {
  6. context.report({
  7. node,
  8. message: 'Imported module should be assigned' });
  9. }
  10. function testIsAllow(globs, filename, source) {
  11. if (!Array.isArray(globs)) {
  12. return false; // default doesn't allow any patterns
  13. }
  14. let filePath;
  15. if (source[0] !== '.' && source[0] !== '/') {// a node module
  16. filePath = source;
  17. } else {
  18. filePath = _path2.default.resolve(_path2.default.dirname(filename), source); // get source absolute path
  19. }
  20. return globs.find(glob =>
  21. (0, _minimatch2.default)(filePath, glob) ||
  22. (0, _minimatch2.default)(filePath, _path2.default.join(process.cwd(), glob))) !==
  23. undefined;
  24. }
  25. function create(context) {
  26. const options = context.options[0] || {};
  27. const filename = context.getFilename();
  28. const isAllow = source => testIsAllow(options.allow, filename, source);
  29. return {
  30. ImportDeclaration(node) {
  31. if (node.specifiers.length === 0 && !isAllow(node.source.value)) {
  32. report(context, node);
  33. }
  34. },
  35. ExpressionStatement(node) {
  36. if (node.expression.type === 'CallExpression' &&
  37. (0, _staticRequire2.default)(node.expression) &&
  38. !isAllow(node.expression.arguments[0].value)) {
  39. report(context, node.expression);
  40. }
  41. } };
  42. }
  43. module.exports = {
  44. create,
  45. meta: {
  46. type: 'suggestion',
  47. docs: {
  48. url: (0, _docsUrl2.default)('no-unassigned-import') },
  49. schema: [
  50. {
  51. 'type': 'object',
  52. 'properties': {
  53. 'devDependencies': { 'type': ['boolean', 'array'] },
  54. 'optionalDependencies': { 'type': ['boolean', 'array'] },
  55. 'peerDependencies': { 'type': ['boolean', 'array'] },
  56. 'allow': {
  57. 'type': 'array',
  58. 'items': {
  59. 'type': 'string' } } },
  60. 'additionalProperties': false }] } };
  61. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby11bmFzc2lnbmVkLWltcG9ydC5qcyJdLCJuYW1lcyI6WyJyZXBvcnQiLCJjb250ZXh0Iiwibm9kZSIsIm1lc3NhZ2UiLCJ0ZXN0SXNBbGxvdyIsImdsb2JzIiwiZmlsZW5hbWUiLCJzb3VyY2UiLCJBcnJheSIsImlzQXJyYXkiLCJmaWxlUGF0aCIsInBhdGgiLCJyZXNvbHZlIiwiZGlybmFtZSIsImZpbmQiLCJnbG9iIiwiam9pbiIsInByb2Nlc3MiLCJjd2QiLCJ1bmRlZmluZWQiLCJjcmVhdGUiLCJvcHRpb25zIiwiZ2V0RmlsZW5hbWUiLCJpc0FsbG93IiwiYWxsb3ciLCJJbXBvcnREZWNsYXJhdGlvbiIsInNwZWNpZmllcnMiLCJsZW5ndGgiLCJ2YWx1ZSIsIkV4cHJlc3Npb25TdGF0ZW1lbnQiLCJleHByZXNzaW9uIiwidHlwZSIsImFyZ3VtZW50cyIsIm1vZHVsZSIsImV4cG9ydHMiLCJtZXRhIiwiZG9jcyIsInVybCIsInNjaGVtYSJdLCJtYXBwaW5ncyI6ImFBQUEsNEI7QUFDQSxzQzs7QUFFQSxzRDtBQUNBLHFDOztBQUVBLFNBQVNBLE1BQVQsQ0FBZ0JDLE9BQWhCLEVBQXlCQyxJQUF6QixFQUErQjtBQUM3QkQsVUFBUUQsTUFBUixDQUFlO0FBQ2JFLFFBRGE7QUFFYkMsYUFBUyxvQ0FGSSxFQUFmOztBQUlEOztBQUVELFNBQVNDLFdBQVQsQ0FBcUJDLEtBQXJCLEVBQTRCQyxRQUE1QixFQUFzQ0MsTUFBdEMsRUFBOEM7QUFDNUMsT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