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.

178 lines
19 KiB

  1. 'use strict';
  2. var _staticRequire = require('../core/staticRequire');var _staticRequire2 = _interopRequireDefault(_staticRequire);
  3. var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);
  4. var _debug = require('debug');var _debug2 = _interopRequireDefault(_debug);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
  5. const log = (0, _debug2.default)('eslint-plugin-import:rules:newline-after-import');
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. /**
  10. * @fileoverview Rule to enforce new line after import not followed by another import.
  11. * @author Radek Benkel
  12. */function containsNodeOrEqual(outerNode, innerNode) {return outerNode.range[0] <= innerNode.range[0] && outerNode.range[1] >= innerNode.range[1];}
  13. function getScopeBody(scope) {
  14. if (scope.block.type === 'SwitchStatement') {
  15. log('SwitchStatement scopes not supported');
  16. return null;
  17. }const
  18. body = scope.block.body;
  19. if (body && body.type === 'BlockStatement') {
  20. return body.body;
  21. }
  22. return body;
  23. }
  24. function findNodeIndexInScopeBody(body, nodeToFind) {
  25. return body.findIndex(node => containsNodeOrEqual(node, nodeToFind));
  26. }
  27. function getLineDifference(node, nextNode) {
  28. return nextNode.loc.start.line - node.loc.end.line;
  29. }
  30. function isClassWithDecorator(node) {
  31. return node.type === 'ClassDeclaration' && node.decorators && node.decorators.length;
  32. }
  33. function isExportDefaultClass(node) {
  34. return node.type === 'ExportDefaultDeclaration' && node.declaration.type === 'ClassDeclaration';
  35. }
  36. module.exports = {
  37. meta: {
  38. type: 'layout',
  39. docs: {
  40. url: (0, _docsUrl2.default)('newline-after-import') },
  41. fixable: 'whitespace',
  42. schema: [
  43. {
  44. 'type': 'object',
  45. 'properties': {
  46. 'count': {
  47. 'type': 'integer',
  48. 'minimum': 1 } },
  49. 'additionalProperties': false }] },
  50. create: function (context) {
  51. let level = 0;
  52. const requireCalls = [];
  53. function checkForNewLine(node, nextNode, type) {
  54. if (isExportDefaultClass(nextNode)) {
  55. let classNode = nextNode.declaration;
  56. if (isClassWithDecorator(classNode)) {
  57. nextNode = classNode.decorators[0];
  58. }
  59. } else if (isClassWithDecorator(nextNode)) {
  60. nextNode = nextNode.decorators[0];
  61. }
  62. const options = context.options[0] || { count: 1 };
  63. const lineDifference = getLineDifference(node, nextNode);
  64. const EXPECTED_LINE_DIFFERENCE = options.count + 1;
  65. if (lineDifference < EXPECTED_LINE_DIFFERENCE) {
  66. let column = node.loc.start.column;
  67. if (node.loc.start.line !== node.loc.end.line) {
  68. column = 0;
  69. }
  70. context.report({
  71. loc: {
  72. line: node.loc.end.line,
  73. column },
  74. message: `Expected ${options.count} empty line${options.count > 1 ? 's' : ''} \
  75. after ${type} statement not followed by another ${type}.`,
  76. fix: fixer => fixer.insertTextAfter(
  77. node,
  78. '\n'.repeat(EXPECTED_LINE_DIFFERENCE - lineDifference)) });
  79. }
  80. }
  81. function incrementLevel() {
  82. level++;
  83. }
  84. function decrementLevel() {
  85. level--;
  86. }
  87. function checkImport(node) {const
  88. parent = node.parent;
  89. const nodePosition = parent.body.indexOf(node);
  90. const nextNode = parent.body[nodePosition + 1];
  91. // skip "export import"s
  92. if (node.type === 'TSImportEqualsDeclaration' && node.isExport) {
  93. return;
  94. }
  95. if (nextNode && nextNode.type !== 'ImportDeclaration' && (nextNode.type !== 'TSImportEqualsDeclaration' || nextNode.isExport)) {
  96. checkForNewLine(node, nextNode, 'import');
  97. }
  98. }
  99. return {
  100. ImportDeclaration: checkImport,
  101. TSImportEqualsDeclaration: checkImport,
  102. CallExpression: function (node) {
  103. if ((0, _staticRequire2.default)(node) && level === 0) {
  104. requireCalls.push(node);
  105. }
  106. },
  107. 'Program:exit': function () {
  108. log('exit processing for', context.getFilename());
  109. const scopeBody = getScopeBody(context.getScope());
  110. log('got scope:', scopeBody);
  111. requireCalls.forEach(function (node, index) {
  112. const nodePosition = findNodeIndexInScopeBody(scopeBody, node);
  113. log('node position in scope:', nodePosition);
  114. const statementWithRequireCall = scopeBody[nodePosition];
  115. const nextStatement = scopeBody[nodePosition + 1];
  116. const nextRequireCall = requireCalls[index + 1];
  117. if (nextRequireCall && containsNodeOrEqual(statementWithRequireCall, nextRequireCall)) {
  118. return;
  119. }
  120. if (nextStatement && (
  121. !nextRequireCall || !containsNodeOrEqual(nextStatement, nextRequireCall))) {
  122. checkForNewLine(statementWithRequireCall, nextStatement, 'require');
  123. }
  124. });
  125. },
  126. FunctionDeclaration: incrementLevel,
  127. FunctionExpression: incrementLevel,
  128. ArrowFunctionExpression: incrementLevel,
  129. BlockStatement: incrementLevel,
  130. ObjectExpression: incrementLevel,
  131. Decorator: incrementLevel,
  132. 'FunctionDeclaration:exit': decrementLevel,
  133. 'FunctionExpression:exit': decrementLevel,
  134. 'ArrowFunctionExpression:exit': decrementLevel,
  135. 'BlockStatement:exit': decrementLevel,
  136. 'ObjectExpression:exit': decrementLevel,
  137. 'Decorator:exit': decrementLevel };
  138. } };
  139. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uZXdsaW5lLWFmdGVyLWltcG9ydC5qcyJdLCJuYW1lcyI6WyJsb2ciLCJjb250YWluc05vZGVPckVxdWFsIiwib3V0ZXJOb2RlIiwiaW5uZXJOb2RlIiwicmFuZ2UiLCJnZXRTY29wZUJvZHkiLCJzY29wZSIsImJsb2NrIiwidHlwZSIsImJvZHkiLCJmaW5kTm9kZUluZGV4SW5TY29wZUJvZHkiLCJub2RlVG9GaW5kIiwiZmluZEluZGV4Iiwibm9kZSIsImdldExpbmVEaWZmZXJlbmNlIiwibmV4dE5vZGUiLCJsb2MiLCJzdGFydCIsImxpbmUiLCJlbmQiLCJpc0NsYXNzV2l0aERlY29yYXRvciIsImRlY29yYXRvcnMiLCJsZW5ndGgiLCJpc0V4cG9ydERlZmF1bHRDbGFz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