no-dynamic-require.js 7.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. category: 'Static analysis',
  24. description: 'Forbid `require()` calls with expressions.',
  25. url: (0, _docsUrl2['default'])('no-dynamic-require') },
  26. schema: [
  27. {
  28. type: 'object',
  29. properties: {
  30. esmodule: {
  31. type: 'boolean' } },
  32. additionalProperties: false }] },
  33. create: function () {function create(context) {
  34. var options = context.options[0] || {};
  35. return {
  36. CallExpression: function () {function CallExpression(node) {
  37. if (!node.arguments[0] || isStaticValue(node.arguments[0])) {
  38. return;
  39. }
  40. if (isRequire(node)) {
  41. return context.report({
  42. node: node,
  43. message: 'Calls to require() should use string literals' });
  44. }
  45. if (options.esmodule && isDynamicImport(node)) {
  46. return context.report({
  47. node: node,
  48. message: dynamicImportErrorMessage });
  49. }
  50. }return CallExpression;}(),
  51. ImportExpression: function () {function ImportExpression(node) {
  52. if (!options.esmodule || isStaticValue(node.source)) {
  53. return;
  54. }
  55. return context.report({
  56. node: node,
  57. message: dynamicImportErrorMessage });
  58. }return ImportExpression;}() };
  59. }return create;}() };
  60. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4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