web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
13 KiB

  1. 'use strict';
  2. var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
  3. const EXPORT_MESSAGE = 'Expected "export" or "export default"',
  4. IMPORT_MESSAGE = 'Expected "import" instead of "require()"'; /**
  5. * @fileoverview Rule to prefer ES6 to CJS
  6. * @author Jamund Ferguson
  7. */function normalizeLegacyOptions(options) {if (options.indexOf('allow-primitive-modules') >= 0) {
  8. return { allowPrimitiveModules: true };
  9. }
  10. return options[0] || {};
  11. }
  12. function allowPrimitive(node, options) {
  13. if (!options.allowPrimitiveModules) return false;
  14. if (node.parent.type !== 'AssignmentExpression') return false;
  15. return node.parent.right.type !== 'ObjectExpression';
  16. }
  17. function allowRequire(node, options) {
  18. return options.allowRequire;
  19. }
  20. function allowConditionalRequire(node, options) {
  21. return options.allowConditionalRequire !== false;
  22. }
  23. function validateScope(scope) {
  24. return scope.variableScope.type === 'module';
  25. }
  26. // https://github.com/estree/estree/blob/master/es5.md
  27. function isConditional(node) {
  28. if (
  29. node.type === 'IfStatement' ||
  30. node.type === 'TryStatement' ||
  31. node.type === 'LogicalExpression' ||
  32. node.type === 'ConditionalExpression')
  33. return true;
  34. if (node.parent) return isConditional(node.parent);
  35. return false;
  36. }
  37. //------------------------------------------------------------------------------
  38. // Rule Definition
  39. //------------------------------------------------------------------------------
  40. const schemaString = { enum: ['allow-primitive-modules'] };
  41. const schemaObject = {
  42. type: 'object',
  43. properties: {
  44. allowPrimitiveModules: { 'type': 'boolean' },
  45. allowRequire: { 'type': 'boolean' },
  46. allowConditionalRequire: { 'type': 'boolean' } },
  47. additionalProperties: false };
  48. module.exports = {
  49. meta: {
  50. type: 'suggestion',
  51. docs: {
  52. url: (0, _docsUrl2.default)('no-commonjs') },
  53. schema: {
  54. anyOf: [
  55. {
  56. type: 'array',
  57. items: [schemaString],
  58. additionalItems: false },
  59. {
  60. type: 'array',
  61. items: [schemaObject],
  62. additionalItems: false }] } },
  63. create: function (context) {
  64. const options = normalizeLegacyOptions(context.options);
  65. return {
  66. 'MemberExpression': function (node) {
  67. // module.exports
  68. if (node.object.name === 'module' && node.property.name === 'exports') {
  69. if (allowPrimitive(node, options)) return;
  70. context.report({ node, message: EXPORT_MESSAGE });
  71. }
  72. // exports.
  73. if (node.object.name === 'exports') {
  74. const isInScope = context.getScope().
  75. variables.
  76. some(variable => variable.name === 'exports');
  77. if (!isInScope) {
  78. context.report({ node, message: EXPORT_MESSAGE });
  79. }
  80. }
  81. },
  82. 'CallExpression': function (call) {
  83. if (!validateScope(context.getScope())) return;
  84. if (call.callee.type !== 'Identifier') return;
  85. if (call.callee.name !== 'require') return;
  86. if (call.arguments.length !== 1) return;
  87. var module = call.arguments[0];
  88. if (module.type !== 'Literal') return;
  89. if (typeof module.value !== 'string') return;
  90. if (allowRequire(call, options)) return;
  91. if (allowConditionalRequire(call, options) && isConditional(call.parent)) return;
  92. // keeping it simple: all 1-string-arg `require` calls are reported
  93. context.report({
  94. node: call.callee,
  95. message: IMPORT_MESSAGE });
  96. } };
  97. } };
  98. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1jb21tb25qcy5qcyJdLCJuYW1lcyI6WyJFWFBPUlRfTUVTU0FHRSIsIklNUE9SVF9NRVNTQUdFIiwibm9ybWFsaXplTGVnYWN5T3B0aW9ucyIsIm9wdGlvbnMiLCJpbmRleE9mIiwiYWxsb3dQcmltaXRpdmVNb2R1bGVzIiwiYWxsb3dQcmltaXRpdmUiLCJub2RlIiwicGFyZW50IiwidHlwZSIsInJpZ2h0IiwiYWxsb3dSZXF1aXJlIiwiYWxsb3dDb25kaXRpb25hbFJlcXVpcmUiLCJ2YWxpZGF0ZVNjb3BlIiwic2NvcGUi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