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.

141 lines
3.8 KiB

  1. 'use strict'
  2. exports.__esModule = true
  3. /**
  4. * Returns an object of node visitors that will call
  5. * 'visitor' with every discovered module path.
  6. *
  7. * todo: correct function prototype for visitor
  8. * @param {Function(String)} visitor [description]
  9. * @param {[type]} options [description]
  10. * @return {object}
  11. */
  12. exports.default = function visitModules(visitor, options) {
  13. // if esmodule is not explicitly disabled, it is assumed to be enabled
  14. options = Object.assign({ esmodule: true }, options)
  15. let ignoreRegExps = []
  16. if (options.ignore != null) {
  17. ignoreRegExps = options.ignore.map(p => new RegExp(p))
  18. }
  19. function checkSourceValue(source, importer) {
  20. if (source == null) return //?
  21. // handle ignore
  22. if (ignoreRegExps.some(re => re.test(source.value))) return
  23. // fire visitor
  24. visitor(source, importer)
  25. }
  26. // for import-y declarations
  27. function checkSource(node) {
  28. checkSourceValue(node.source, node)
  29. }
  30. // for esmodule dynamic `import()` calls
  31. function checkImportCall(node) {
  32. if (node.callee.type !== 'Import') return
  33. if (node.arguments.length !== 1) return
  34. const modulePath = node.arguments[0]
  35. if (modulePath.type !== 'Literal') return
  36. if (typeof modulePath.value !== 'string') return
  37. checkSourceValue(modulePath, node)
  38. }
  39. // for CommonJS `require` calls
  40. // adapted from @mctep: http://git.io/v4rAu
  41. function checkCommon(call) {
  42. if (call.callee.type !== 'Identifier') return
  43. if (call.callee.name !== 'require') return
  44. if (call.arguments.length !== 1) return
  45. const modulePath = call.arguments[0]
  46. if (modulePath.type !== 'Literal') return
  47. if (typeof modulePath.value !== 'string') return
  48. checkSourceValue(modulePath, call)
  49. }
  50. function checkAMD(call) {
  51. if (call.callee.type !== 'Identifier') return
  52. if (call.callee.name !== 'require' &&
  53. call.callee.name !== 'define') return
  54. if (call.arguments.length !== 2) return
  55. const modules = call.arguments[0]
  56. if (modules.type !== 'ArrayExpression') return
  57. for (let element of modules.elements) {
  58. if (element.type !== 'Literal') continue
  59. if (typeof element.value !== 'string') continue
  60. if (element.value === 'require' ||
  61. element.value === 'exports') continue // magic modules: http://git.io/vByan
  62. checkSourceValue(element, element)
  63. }
  64. }
  65. const visitors = {}
  66. if (options.esmodule) {
  67. Object.assign(visitors, {
  68. 'ImportDeclaration': checkSource,
  69. 'ExportNamedDeclaration': checkSource,
  70. 'ExportAllDeclaration': checkSource,
  71. 'CallExpression': checkImportCall,
  72. })
  73. }
  74. if (options.commonjs || options.amd) {
  75. const currentCallExpression = visitors['CallExpression']
  76. visitors['CallExpression'] = function (call) {
  77. if (currentCallExpression) currentCallExpression(call)
  78. if (options.commonjs) checkCommon(call)
  79. if (options.amd) checkAMD(call)
  80. }
  81. }
  82. return visitors
  83. }
  84. /**
  85. * make an options schema for the module visitor, optionally
  86. * adding extra fields.
  87. */
  88. function makeOptionsSchema(additionalProperties) {
  89. const base = {
  90. 'type': 'object',
  91. 'properties': {
  92. 'commonjs': { 'type': 'boolean' },
  93. 'amd': { 'type': 'boolean' },
  94. 'esmodule': { 'type': 'boolean' },
  95. 'ignore': {
  96. 'type': 'array',
  97. 'minItems': 1,
  98. 'items': { 'type': 'string' },
  99. 'uniqueItems': true,
  100. },
  101. },
  102. 'additionalProperties': false,
  103. }
  104. if (additionalProperties){
  105. for (let key in additionalProperties) {
  106. base.properties[key] = additionalProperties[key]
  107. }
  108. }
  109. return base
  110. }
  111. exports.makeOptionsSchema = makeOptionsSchema
  112. /**
  113. * json schema object for options parameter. can be used to build
  114. * rule options schema object.
  115. * @type {Object}
  116. */
  117. exports.optionsSchema = makeOptionsSchema()