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.

131 lines
16 KiB

  1. 'use strict';var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
  2. module.exports = {
  3. meta: {
  4. type: 'suggestion',
  5. docs: {
  6. url: (0, _docsUrl2.default)('first') },
  7. fixable: 'code',
  8. schema: [
  9. {
  10. type: 'string',
  11. enum: ['absolute-first', 'disable-absolute-first'] }] },
  12. create: function (context) {
  13. function isPossibleDirective(node) {
  14. return node.type === 'ExpressionStatement' &&
  15. node.expression.type === 'Literal' &&
  16. typeof node.expression.value === 'string';
  17. }
  18. return {
  19. 'Program': function (n) {
  20. const body = n.body,
  21. absoluteFirst = context.options[0] === 'absolute-first',
  22. message = 'Import in body of module; reorder to top.',
  23. sourceCode = context.getSourceCode(),
  24. originSourceCode = sourceCode.getText();
  25. let nonImportCount = 0,
  26. anyExpressions = false,
  27. anyRelative = false,
  28. lastLegalImp = null,
  29. errorInfos = [],
  30. shouldSort = true,
  31. lastSortNodesIndex = 0;
  32. body.forEach(function (node, index) {
  33. if (!anyExpressions && isPossibleDirective(node)) {
  34. return;
  35. }
  36. anyExpressions = true;
  37. if (node.type === 'ImportDeclaration') {
  38. if (absoluteFirst) {
  39. if (/^\./.test(node.source.value)) {
  40. anyRelative = true;
  41. } else if (anyRelative) {
  42. context.report({
  43. node: node.source,
  44. message: 'Absolute imports should come before relative imports.' });
  45. }
  46. }
  47. if (nonImportCount > 0) {
  48. for (let variable of context.getDeclaredVariables(node)) {
  49. if (!shouldSort) break;
  50. const references = variable.references;
  51. if (references.length) {
  52. for (let reference of references) {
  53. if (reference.identifier.range[0] < node.range[1]) {
  54. shouldSort = false;
  55. break;
  56. }
  57. }
  58. }
  59. }
  60. shouldSort && (lastSortNodesIndex = errorInfos.length);
  61. errorInfos.push({
  62. node,
  63. range: [body[index - 1].range[1], node.range[1]] });
  64. } else {
  65. lastLegalImp = node;
  66. }
  67. } else {
  68. nonImportCount++;
  69. }
  70. });
  71. if (!errorInfos.length) return;
  72. errorInfos.forEach(function (errorInfo, index) {
  73. const node = errorInfo.node,
  74. infos = {
  75. node,
  76. message };
  77. if (index < lastSortNodesIndex) {
  78. infos.fix = function (fixer) {
  79. return fixer.insertTextAfter(node, '');
  80. };
  81. } else if (index === lastSortNodesIndex) {
  82. const sortNodes = errorInfos.slice(0, lastSortNodesIndex + 1);
  83. infos.fix = function (fixer) {
  84. const removeFixers = sortNodes.map(function (_errorInfo) {
  85. return fixer.removeRange(_errorInfo.range);
  86. }),
  87. range = [0, removeFixers[removeFixers.length - 1].range[1]];
  88. let insertSourceCode = sortNodes.map(function (_errorInfo) {
  89. const nodeSourceCode = String.prototype.slice.apply(
  90. originSourceCode, _errorInfo.range);
  91. if (/\S/.test(nodeSourceCode[0])) {
  92. return '\n' + nodeSourceCode;
  93. }
  94. return nodeSourceCode;
  95. }).join(''),
  96. insertFixer = null,
  97. replaceSourceCode = '';
  98. if (!lastLegalImp) {
  99. insertSourceCode =
  100. insertSourceCode.trim() + insertSourceCode.match(/^(\s+)/)[0];
  101. }
  102. insertFixer = lastLegalImp ?
  103. fixer.insertTextAfter(lastLegalImp, insertSourceCode) :
  104. fixer.insertTextBefore(body[0], insertSourceCode);
  105. const fixers = [insertFixer].concat(removeFixers);
  106. fixers.forEach(function (computedFixer, i) {
  107. replaceSourceCode += originSourceCode.slice(
  108. fixers[i - 1] ? fixers[i - 1].range[1] : 0, computedFixer.range[0]) +
  109. computedFixer.text;
  110. });
  111. return fixer.replaceTextRange(range, replaceSourceCode);
  112. };
  113. }
  114. context.report(infos);
  115. });
  116. } };
  117. } };
  118. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9maXJzdC5qcyJdLCJuYW1lcyI6WyJtb2R1bGUiLCJleHBvc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