first.js 18 KB

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