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.

188 lines
19 KiB

  1. 'use strict';var _path = require('path');var _path2 = _interopRequireDefault(_path);
  2. var _resolve = require('eslint-module-utils/resolve');var _resolve2 = _interopRequireDefault(_resolve);
  3. var _importType = require('../core/importType');
  4. var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
  5. const enumValues = { enum: ['always', 'ignorePackages', 'never'] };
  6. const patternProperties = {
  7. type: 'object',
  8. patternProperties: { '.*': enumValues } };
  9. const properties = {
  10. type: 'object',
  11. properties: {
  12. 'pattern': patternProperties,
  13. 'ignorePackages': { type: 'boolean' } } };
  14. function buildProperties(context) {
  15. const result = {
  16. defaultConfig: 'never',
  17. pattern: {},
  18. ignorePackages: false };
  19. context.options.forEach(obj => {
  20. // If this is a string, set defaultConfig to its value
  21. if (typeof obj === 'string') {
  22. result.defaultConfig = obj;
  23. return;
  24. }
  25. // If this is not the new structure, transfer all props to result.pattern
  26. if (obj.pattern === undefined && obj.ignorePackages === undefined) {
  27. Object.assign(result.pattern, obj);
  28. return;
  29. }
  30. // If pattern is provided, transfer all props
  31. if (obj.pattern !== undefined) {
  32. Object.assign(result.pattern, obj.pattern);
  33. }
  34. // If ignorePackages is provided, transfer it to result
  35. if (obj.ignorePackages !== undefined) {
  36. result.ignorePackages = obj.ignorePackages;
  37. }
  38. });
  39. if (result.defaultConfig === 'ignorePackages') {
  40. result.defaultConfig = 'always';
  41. result.ignorePackages = true;
  42. }
  43. return result;
  44. }
  45. module.exports = {
  46. meta: {
  47. type: 'suggestion',
  48. docs: {
  49. url: (0, _docsUrl2.default)('extensions') },
  50. schema: {
  51. anyOf: [
  52. {
  53. type: 'array',
  54. items: [enumValues],
  55. additionalItems: false },
  56. {
  57. type: 'array',
  58. items: [
  59. enumValues,
  60. properties],
  61. additionalItems: false },
  62. {
  63. type: 'array',
  64. items: [properties],
  65. additionalItems: false },
  66. {
  67. type: 'array',
  68. items: [patternProperties],
  69. additionalItems: false },
  70. {
  71. type: 'array',
  72. items: [
  73. enumValues,
  74. patternProperties],
  75. additionalItems: false }] } },
  76. create: function (context) {
  77. const props = buildProperties(context);
  78. function getModifier(extension) {
  79. return props.pattern[extension] || props.defaultConfig;
  80. }
  81. function isUseOfExtensionRequired(extension, isPackage) {
  82. return getModifier(extension) === 'always' && (!props.ignorePackages || !isPackage);
  83. }
  84. function isUseOfExtensionForbidden(extension) {
  85. return getModifier(extension) === 'never';
  86. }
  87. function isResolvableWithoutExtension(file) {
  88. const extension = _path2.default.extname(file);
  89. const fileWithoutExtension = file.slice(0, -extension.length);
  90. const resolvedFileWithoutExtension = (0, _resolve2.default)(fileWithoutExtension, context);
  91. return resolvedFileWithoutExtension === (0, _resolve2.default)(file, context);
  92. }
  93. function isExternalRootModule(file) {
  94. const slashCount = file.split('/').length - 1;
  95. if ((0, _importType.isScopedModule)(file) && slashCount <= 1) return true;
  96. if ((0, _importType.isExternalModule)(file, context, (0, _resolve2.default)(file, context)) && !slashCount) return true;
  97. return false;
  98. }
  99. function checkFileExtension(node) {const
  100. source = node.source;
  101. // bail if the declaration doesn't have a source, e.g. "export { foo };"
  102. if (!source) return;
  103. const importPathWithQueryString = source.value;
  104. // don't enforce anything on builtins
  105. if ((0, _importType.isBuiltIn)(importPathWithQueryString, context.settings)) return;
  106. const importPath = importPathWithQueryString.replace(/\?(.*)$/, '');
  107. // don't enforce in root external packages as they may have names with `.js`.
  108. // Like `import Decimal from decimal.js`)
  109. if (isExternalRootModule(importPath)) return;
  110. const resolvedPath = (0, _resolve2.default)(importPath, context);
  111. // get extension from resolved path, if possible.
  112. // for unresolved, use source value.
  113. const extension = _path2.default.extname(resolvedPath || importPath).substring(1);
  114. // determine if this is a module
  115. const isPackage = (0, _importType.isExternalModule)(importPath, context.settings) ||
  116. (0, _importType.isScoped)(importPath);
  117. if (!extension || !importPath.endsWith(`.${extension}`)) {
  118. const extensionRequired = isUseOfExtensionRequired(extension, isPackage);
  119. const extensionForbidden = isUseOfExtensionForbidden(extension);
  120. if (extensionRequired && !extensionForbidden) {
  121. context.report({
  122. node: source,
  123. message:
  124. `Missing file extension ${extension ? `"${extension}" ` : ''}for "${importPathWithQueryString}"` });
  125. }
  126. } else if (extension) {
  127. if (isUseOfExtensionForbidden(extension) && isResolvableWithoutExtension(importPath)) {
  128. context.report({
  129. node: source,
  130. message: `Unexpected use of file extension "${extension}" for "${importPathWithQueryString}"` });
  131. }
  132. }
  133. }
  134. return {
  135. ImportDeclaration: checkFileExtension,
  136. ExportNamedDeclaration: checkFileExtension };
  137. } };
  138. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9leHRlbnNpb25zLmpzIl0sIm5hbWVzIjpbImVudW1WYWx1ZXMiLCJlbnVtIiwicGF0dGVyblByb3BlcnRpZXMiLCJ0eXBlIiwicHJvcGVydGllcyIsImJ1aWxkUHJvcGVydGllcyIsImNvbnRleHQiLCJyZXN1bHQiLCJkZWZhdWx0Q29uZmlnIiwicGF0dGVybiIsImlnbm9yZVBhY2thZ2VzIiwib3B0aW9ucyIsImZvckVhY2giLCJvYmoiLCJ1bmRlZmluZWQiLCJPYmplY3QiLCJhc3NpZ24iLCJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIsImRvY3MiLCJ1cmwiLCJzY2hlbWEiLCJhbnlPZiIsIml0ZW1zIiwiYWRkaXRpb25hbEl0ZW1zIiwiY3JlYXRlIiwicHJvcHMiLCJnZXRNb2RpZmllciIsImV4dGVuc2lvbiIsImlzVXNlT2ZFeHRlbnNpb25SZXF1aXJlZCIsImlzUGFja2FnZSIsImlzVXNlT2ZFeHRlbnNpb25Gb3JiaWRkZW4iLCJpc1Jlc29sdmFibGVXaXRob3V0RXh0ZW5zaW9uIiwiZmlsZSIsInBhdGgiLCJleH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