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.

216 lines
24 KiB

  1. 'use strict';var _declaredScope = require('eslint-module-utils/declaredScope');var _declaredScope2 = _interopRequireDefault(_declaredScope);
  2. var _ExportMap = require('../ExportMap');var _ExportMap2 = _interopRequireDefault(_ExportMap);
  3. var _importDeclaration = require('../importDeclaration');var _importDeclaration2 = _interopRequireDefault(_importDeclaration);
  4. var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
  5. module.exports = {
  6. meta: {
  7. type: 'problem',
  8. docs: {
  9. url: (0, _docsUrl2.default)('namespace') },
  10. schema: [
  11. {
  12. type: 'object',
  13. properties: {
  14. allowComputed: {
  15. description: 'If `false`, will report computed (and thus, un-lintable) references to namespace members.',
  16. type: 'boolean',
  17. default: false } },
  18. additionalProperties: false }] },
  19. create: function namespaceRule(context) {
  20. // read options
  21. var _ref =
  22. context.options[0] || {},_ref$allowComputed = _ref.allowComputed;const allowComputed = _ref$allowComputed === undefined ? false : _ref$allowComputed;
  23. const namespaces = new Map();
  24. function makeMessage(last, namepath) {
  25. return `'${last.name}' not found in ${namepath.length > 1 ? 'deeply ' : ''}imported namespace '${namepath.join('.')}'.`;
  26. }
  27. return {
  28. // pick up all imports at body entry time, to properly respect hoisting
  29. Program(_ref2) {let body = _ref2.body;
  30. function processBodyStatement(declaration) {
  31. if (declaration.type !== 'ImportDeclaration') return;
  32. if (declaration.specifiers.length === 0) return;
  33. const imports = _ExportMap2.default.get(declaration.source.value, context);
  34. if (imports == null) return null;
  35. if (imports.errors.length) {
  36. imports.reportErrors(context, declaration);
  37. return;
  38. }
  39. for (const specifier of declaration.specifiers) {
  40. switch (specifier.type) {
  41. case 'ImportNamespaceSpecifier':
  42. if (!imports.size) {
  43. context.report(
  44. specifier,
  45. `No exported names found in module '${declaration.source.value}'.`);
  46. }
  47. namespaces.set(specifier.local.name, imports);
  48. break;
  49. case 'ImportDefaultSpecifier':
  50. case 'ImportSpecifier':{
  51. const meta = imports.get(
  52. // default to 'default' for default http://i.imgur.com/nj6qAWy.jpg
  53. specifier.imported ? specifier.imported.name : 'default');
  54. if (!meta || !meta.namespace) {break;}
  55. namespaces.set(specifier.local.name, meta.namespace);
  56. break;
  57. }}
  58. }
  59. }
  60. body.forEach(processBodyStatement);
  61. },
  62. // same as above, but does not add names to local map
  63. ExportNamespaceSpecifier(namespace) {
  64. var declaration = (0, _importDeclaration2.default)(context);
  65. var imports = _ExportMap2.default.get(declaration.source.value, context);
  66. if (imports == null) return null;
  67. if (imports.errors.length) {
  68. imports.reportErrors(context, declaration);
  69. return;
  70. }
  71. if (!imports.size) {
  72. context.report(
  73. namespace,
  74. `No exported names found in module '${declaration.source.value}'.`);
  75. }
  76. },
  77. // todo: check for possible redefinition
  78. MemberExpression(dereference) {
  79. if (dereference.object.type !== 'Identifier') return;
  80. if (!namespaces.has(dereference.object.name)) return;
  81. if ((0, _declaredScope2.default)(context, dereference.object.name) !== 'module') return;
  82. if (dereference.parent.type === 'AssignmentExpression' && dereference.parent.left === dereference) {
  83. context.report(
  84. dereference.parent,
  85. `Assignment to member of namespace '${dereference.object.name}'.`);
  86. }
  87. // go deep
  88. var namespace = namespaces.get(dereference.object.name);
  89. var namepath = [dereference.object.name];
  90. // while property is namespace and parent is member expression, keep validating
  91. while (namespace instanceof _ExportMap2.default && dereference.type === 'MemberExpression') {
  92. if (dereference.computed) {
  93. if (!allowComputed) {
  94. context.report(
  95. dereference.property,
  96. `Unable to validate computed reference to imported namespace '${dereference.object.name}'.`);
  97. }
  98. return;
  99. }
  100. if (!namespace.has(dereference.property.name)) {
  101. context.report(
  102. dereference.property,
  103. makeMessage(dereference.property, namepath));
  104. break;
  105. }
  106. const exported = namespace.get(dereference.property.name);
  107. if (exported == null) return;
  108. // stash and pop
  109. namepath.push(dereference.property.name);
  110. namespace = exported.namespace;
  111. dereference = dereference.parent;
  112. }
  113. },
  114. VariableDeclarator(_ref3) {let id = _ref3.id,init = _ref3.init;
  115. if (init == null) return;
  116. if (init.type !== 'Identifier') return;
  117. if (!namespaces.has(init.name)) return;
  118. // check for redefinition in intermediate scopes
  119. if ((0, _declaredScope2.default)(context, init.name) !== 'module') return;
  120. // DFS traverse child namespaces
  121. function testKey(pattern, namespace) {let path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [init.name];
  122. if (!(namespace instanceof _ExportMap2.default)) return;
  123. if (pattern.type !== 'ObjectPattern') return;
  124. for (const property of pattern.properties) {
  125. if (
  126. property.type === 'ExperimentalRestProperty' ||
  127. property.type === 'RestElement' ||
  128. !property.key)
  129. {
  130. continue;
  131. }
  132. if (property.key.type !== 'Identifier') {
  133. context.report({
  134. node: property,
  135. message: 'Only destructure top-level names.' });
  136. continue;
  137. }
  138. if (!namespace.has(property.key.name)) {
  139. context.report({
  140. node: property,
  141. message: makeMessage(property.key, path) });
  142. continue;
  143. }
  144. path.push(property.key.name);
  145. const dependencyExportMap = namespace.get(property.key.name);
  146. // could be null when ignored or ambiguous
  147. if (dependencyExportMap !== null) {
  148. testKey(property.value, dependencyExportMap.namespace, path);
  149. }
  150. path.pop();
  151. }
  152. }
  153. testKey(id, namespaces.get(init.name));
  154. },
  155. JSXMemberExpression(_ref4) {let object = _ref4.object,property = _ref4.property;
  156. if (!namespaces.has(object.name)) return;
  157. var namespace = namespaces.get(object.name);
  158. if (!namespace.has(property.name)) {
  159. context.report({
  160. node: property,
  161. message: makeMessage(property, [object.name]) });
  162. }
  163. } };
  164. } };
  165. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uYW1lc3BhY2UuanMiXSwibmFtZXMiOlsibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsInVybCIsInNjaGVtYSIsInByb3BlcnRpZXMiLCJhbGx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