no-dynamic-require.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. 'use strict';var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}
  2. function isRequire(node) {
  3. return node &&
  4. node.callee &&
  5. node.callee.type === 'Identifier' &&
  6. node.callee.name === 'require' &&
  7. node.arguments.length >= 1;
  8. }
  9. function isDynamicImport(node) {
  10. return node &&
  11. node.callee &&
  12. node.callee.type === 'Import';
  13. }
  14. function isStaticValue(arg) {
  15. return arg.type === 'Literal' ||
  16. arg.type === 'TemplateLiteral' && arg.expressions.length === 0;
  17. }
  18. var dynamicImportErrorMessage = 'Calls to import() should use string literals';
  19. module.exports = {
  20. meta: {
  21. type: 'suggestion',
  22. docs: {
  23. url: (0, _docsUrl2['default'])('no-dynamic-require') },
  24. schema: [
  25. {
  26. type: 'object',
  27. properties: {
  28. esmodule: {
  29. type: 'boolean' } },
  30. additionalProperties: false }] },
  31. create: function () {function create(context) {
  32. var options = context.options[0] || {};
  33. return {
  34. CallExpression: function () {function CallExpression(node) {
  35. if (!node.arguments[0] || isStaticValue(node.arguments[0])) {
  36. return;
  37. }
  38. if (isRequire(node)) {
  39. return context.report({
  40. node: node,
  41. message: 'Calls to require() should use string literals' });
  42. }
  43. if (options.esmodule && isDynamicImport(node)) {
  44. return context.report({
  45. node: node,
  46. message: dynamicImportErrorMessage });
  47. }
  48. }return CallExpression;}(),
  49. ImportExpression: function () {function ImportExpression(node) {
  50. if (!options.esmodule || isStaticValue(node.source)) {
  51. return;
  52. }
  53. return context.report({
  54. node: node,
  55. message: dynamicImportErrorMessage });
  56. }return ImportExpression;}() };
  57. }return create;}() };
  58. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1keW5hbWljLXJlcXVpcm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