first.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 () {function create(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 () {function Program(n) {
  25. var body = n.body;
  26. if (!body) {
  27. return;
  28. }
  29. var absoluteFirst = context.options[0] === 'absolute-first';
  30. var message = 'Import in body of module; reorder to top.';
  31. var sourceCode = context.getSourceCode();
  32. var originSourceCode = sourceCode.getText();
  33. var nonImportCount = 0;
  34. var anyExpressions = false;
  35. var anyRelative = false;
  36. var lastLegalImp = null;
  37. var errorInfos = [];
  38. var shouldSort = true;
  39. var lastSortNodesIndex = 0;
  40. body.forEach(function (node, index) {
  41. if (!anyExpressions && isPossibleDirective(node)) {
  42. return;
  43. }
  44. anyExpressions = true;
  45. if (node.type === 'ImportDeclaration' || node.type === 'TSImportEqualsDeclaration') {
  46. if (absoluteFirst) {
  47. if (/^\./.test(getImportValue(node))) {
  48. anyRelative = true;
  49. } else if (anyRelative) {
  50. context.report({
  51. node: node.type === 'ImportDeclaration' ? node.source : node.moduleReference,
  52. message: 'Absolute imports should come before relative imports.' });
  53. }
  54. }
  55. if (nonImportCount > 0) {var _iteratorNormalCompletion = true;var _didIteratorError = false;var _iteratorError = undefined;try {
  56. for (var _iterator = context.getDeclaredVariables(node)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {var variable = _step.value;
  57. if (!shouldSort) break;
  58. var references = variable.references;
  59. if (references.length) {var _iteratorNormalCompletion2 = true;var _didIteratorError2 = false;var _iteratorError2 = undefined;try {
  60. for (var _iterator2 = references[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {var reference = _step2.value;
  61. if (reference.identifier.range[0] < node.range[1]) {
  62. shouldSort = false;
  63. break;
  64. }
  65. }} catch (err) {_didIteratorError2 = true;_iteratorError2 = err;} finally {try {if (!_iteratorNormalCompletion2 && _iterator2['return']) {_iterator2['return']();}} finally {if (_didIteratorError2) {throw _iteratorError2;}}}
  66. }
  67. }} catch (err) {_didIteratorError = true;_iteratorError = err;} finally {try {if (!_iteratorNormalCompletion && _iterator['return']) {_iterator['return']();}} finally {if (_didIteratorError) {throw _iteratorError;}}}
  68. shouldSort && (lastSortNodesIndex = errorInfos.length);
  69. errorInfos.push({
  70. node: node,
  71. range: [body[index - 1].range[1], node.range[1]] });
  72. } else {
  73. lastLegalImp = node;
  74. }
  75. } else {
  76. nonImportCount++;
  77. }
  78. });
  79. if (!errorInfos.length) return;
  80. errorInfos.forEach(function (errorInfo, index) {
  81. var node = errorInfo.node;
  82. var infos = {
  83. node: node,
  84. message: message };
  85. if (index < lastSortNodesIndex) {
  86. infos.fix = function (fixer) {
  87. return fixer.insertTextAfter(node, '');
  88. };
  89. } else if (index === lastSortNodesIndex) {
  90. var sortNodes = errorInfos.slice(0, lastSortNodesIndex + 1);
  91. infos.fix = function (fixer) {
  92. var removeFixers = sortNodes.map(function (_errorInfo) {
  93. return fixer.removeRange(_errorInfo.range);
  94. });
  95. var range = [0, removeFixers[removeFixers.length - 1].range[1]];
  96. var insertSourceCode = sortNodes.map(function (_errorInfo) {
  97. var nodeSourceCode = String.prototype.slice.apply(
  98. originSourceCode, _errorInfo.range);
  99. if (/\S/.test(nodeSourceCode[0])) {
  100. return '\n' + nodeSourceCode;
  101. }
  102. return nodeSourceCode;
  103. }).join('');
  104. var insertFixer = null;
  105. var replaceSourceCode = '';
  106. if (!lastLegalImp) {
  107. insertSourceCode =
  108. insertSourceCode.trim() + insertSourceCode.match(/^(\s+)/)[0];
  109. }
  110. insertFixer = lastLegalImp ?
  111. fixer.insertTextAfter(lastLegalImp, insertSourceCode) :
  112. fixer.insertTextBefore(body[0], insertSourceCode);
  113. var fixers = [insertFixer].concat(removeFixers);
  114. fixers.forEach(function (computedFixer, i) {
  115. replaceSourceCode += originSourceCode.slice(
  116. fixers[i - 1] ? fixers[i - 1].range[1] : 0, computedFixer.range[0]) +
  117. computedFixer.text;
  118. });
  119. return fixer.replaceTextRange(range, replaceSourceCode);
  120. };
  121. }
  122. context.report(infos);
  123. });
  124. }return Program;}() };
  125. }return create;}() };
  126. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9maXJzdC5qcyJdLCJuYW1lcyI6WyJnZXRJbXBvcnRWYWx1ZSIsIm5vZGUiLCJ0eXBlIiwic291cmNlIiwidmFsdWUiLCJtb2R1bGVSZWZlcmVuY2UiLCJleHByZXNzaW9uIiwibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJkb2NzIiwidXJsIiwiZml4YWJsZSIsInNjaGVtYSIsImNyZWF0ZSIsImNvbnRleHQiLCJpc1Bvc3NpYmxlRGlyZWN0aXZlIiwibiIsImJvZHkiLCJhYnNvbHV0ZUZpcnN0Iiwib3B0aW9ucyIsIm1lc3NhZ2UiLCJzb3VyY2VDb2RlIiwiZ2V0U291cmNlQ29kZSIsIm9yaWdpblNvdXJjZUNvZGUiLCJnZXRUZXh0Iiwibm9uSW1wb3J0Q291bn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hbHNlO1xuICAgICAgICBsZXQgYW55UmVsYXRpdmUgPSBmYWxzZTtcbiAgICAgICAgbGV0IGxhc3RMZWdhbEltcCA9IG51bGw7XG4gICAgICAgIGNvbnN0IGVycm9ySW5mb3MgPSBbXTtcbiAgICAgICAgbGV0IHNob3VsZFNvcnQgPSB0cnVlO1xuICAgICAgICBsZXQgbGFzdFNvcnROb2Rlc0luZGV4ID0gMDtcbiAgICAgICAgYm9keS5mb3JFYWNoKGZ1bmN0aW9uIChub2RlLCBpbmRleCkge1xuICAgICAgICAgIGlmICghYW55RXhwcmVzc2lvbnMgJiYgaXNQb3NzaWJsZURpcmVjdGl2ZShub2RlKSkge1xuICAgICAgICAgICAgcmV0dXJuO1xuICAgICAgICAgIH1cblxuICAgICAgICAgIGFueUV4cHJlc3Npb25zID0gdHJ1ZTtcblxuICAgICAgICAgIGlmIChub2RlLnR5cGUgPT09ICdJbXBvcnREZWNsYXJhdGlvbicgfHwgbm9kZS50eXBlID09PSAnVFNJbXBvcnRFcXVhbHNEZWNsYXJhdGlvbicpIHtcbiAgICAgICAgICAgIGlmIChhYnNvbHV0ZUZpcnN0KSB7XG4gICAgICAgICAgICAgIGlmICgvXlxcLi8udGVzdChnZXRJbXBvcnRWYWx1ZShub2RlKSkpIHtcbiAgICAgICAgICAgICAgICBhbnlSZWxhdGl2ZSA9IHRydWU7XG4gICAgICAgICAgICAgIH0gZWxzZSBpZiAoYW55UmVsYXRpdmUpIHtcbiAgICAgICAgICAgICAgICBjb250ZXh0LnJlcG9ydCh7XG4gICAgICAgICAgICAgICAgICBub2RlOiBub2RlLnR5cGUgPT09ICdJbXBvcnREZWNsYXJhdGlvbicgPyBub2RlLnNvdXJjZSA6IG5vZGUubW9kdWxlUmVmZXJlbmNlLFxuICAgICAgICAgICAgICAgICAgbWVzc2FnZTogJ0Fic29sdXRlIGltcG9ydHMgc2hvdWxkIGNvbWUgYmVmb3JlIHJlbGF0aXZlIGltcG9ydHMuJyxcbiAgICAgICAgICAgICAgICB9KTtcbiAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgfVxuICAgICAgICAgICAgaWYgKG5vbkltcG9ydENvdW50ID4gMCkge1xuICAgICAgICAgICAgICBmb3IgKGNvbnN0IHZhcmlhYmxlIG9mIGNvbnRleHQuZ2V0RGVjbGFyZWRWYXJpYWJsZXMobm9kZSkpIHtcbiAgICAgICAgICAgICAgICBpZiAoIXNob3VsZFNvcnQpIGJyZWFrO1xuICAgICAgICAgICAgICAgIGNvbnN0IHJlZmVyZW5jZXMgPSB2YXJpYWJsZS5yZWZlcmVuY2VzO1xuICAgICAgICAgICAgICAgIGlmIChyZWZlcmVuY2VzLmxlbmd0aCkge1xuICAgICAgICAgICAgICAgICAgZm9yIChjb25zdCByZWZlcmVuY2Ugb2YgcmVmZXJlbmNlcykge1xuICAgICAgICAgICAgICAgICAgICBpZiAocmVmZXJlbmNlLmlkZW50aWZpZXIucmFuZ2VbMF0gPCBub2RlLnJhbmdlWzFdKSB7XG4gICAgICAgICAgICAgICAgICAgICAgc2hvdWxkU29ydCA9IGZhbHNlO1xuICAgICAgICAgICAgICAgICAgICAgIGJyZWFrO1xuICAgICAgICAgICAgICAgICAgICB9XG4gICAgICAgICAgICAgICAgICB9XG4gICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgICB9XG4gICAgICAgICAgICAgIHNob3VsZFNvcnQgJiYgKGxhc3RTb3J0Tm9kZXNJbmRleCA9IGVycm9ySW5mb3MubGVuZ3RoKTtcbiAgICAgICAgICAgICAgZXJyb3JJbmZvcy5wdXNoKHtcbiAgICAgICAgICAgICAgICBub2RlLFxuICAgICAgICAgICAgICAgIHJhbmdlOiBbYm9keVtpbmRleCAtIDFdLnJhbmdlWzFdLCBub2RlLnJhbmdlWzFdXSxcbiAgICAgICAgICAgICAgfSk7XG4gICAgICAgICAgICB9IGVsc2Uge1xuICAgICAgICAgICAgICBsYXN0TGVnYWxJbXAgPSBub2RlO1xuICAgICAgICAgICAgfVxuICAgICAgICAgIH0gZWxzZSB7XG4gICAgICAgICAgICBub25JbXBvcnRDb3VudCsrO1xuICAgICAgICAgIH1cbiAgICAgICAgfSk7XG4gICAgICAgIGlmICghZXJyb3JJbmZvcy5sZW5ndGgpIHJldHVybjtcbiAgICAgICAgZXJyb3JJbmZvcy5mb3JFYWNoKGZ1bmN0aW9uIChlcnJvckluZm8sIGluZGV4KSB7XG4gICAgICAgICAgY29uc3Qgbm9kZSA9IGVycm9ySW5mby5ub2RlO1xuICAgICAgICAgIGNvbnN0IGluZm9zID0ge1xuICAgICAgICAgICAgbm9kZSxcbiAgICAgICAgICAgIG1lc3NhZ2UsXG4gICAgICAgICAgfTtcbiAgICAgICAgICBpZiAoaW5kZXggPCBsYXN0U29ydE5vZGVzSW5kZXgpIHtcbiAgICAgICAgICAgIGluZm9zLmZpeCA9IGZ1bmN0aW9uIChmaXhlcikge1xuICAgICAgICAgICAgICByZXR1cm4gZml4ZXIuaW5zZXJ0VGV4dEFmdGVyKG5vZGUsICcnKTtcbiAgICAgICAgICAgIH07XG4gICAgICAgICAgfSBlbHNlIGlmIChpbmRleCA9PT0gbGFzdFNvcnROb2Rlc0luZGV4KSB7XG4gICAgICAgICAgICBjb25zdCBzb3J0Tm9kZXMgPSBlcnJvckluZm9zLnNsaWNlKDAsIGxhc3RTb3J0Tm9kZXNJbmRleCArIDEpO1xuICAgICAgICAgICAgaW5mb3MuZml4ID0gZnVuY3Rpb24gKGZpeGVyKSB7XG4gICAgICAgICAgICAgIGNvbnN0IHJlbW92ZUZpeGVycyA9IHNvcnROb2Rlcy5tYXAoZnVuY3Rpb24gKF9lcnJvckluZm8pIHtcbiAgICAgICAgICAgICAgICByZXR1cm4gZml4ZXIucmVtb3ZlUmFuZ2UoX2Vycm9ySW5mby5yYW5nZSk7XG4gICAgICAgICAgICAgIH0pO1xuICAgICAgICAgICAgICBjb25zdCByYW5nZSA9IFswLCByZW1vdmVGaXhlcnNbcmVtb3ZlRml4ZXJzLmxlbmd0aCAtIDFdLnJhbmdlWzFdXTtcbiAgICAgICAgICAgICAgbGV0IGluc2VydFNvdXJjZUNvZGUgPSBzb3J0Tm9kZXMubWFwKGZ1bmN0aW9uIChfZXJyb3JJbmZvKSB7XG4gICAgICAgICAgICAgICAgY29uc3Qgbm9kZVNvdXJjZUNvZGUgPSBTdHJpbmcucHJvdG90eXBlLnNsaWNlLmFwcGx5KFxuICAgICAgICAgICAgICAgICAgb3JpZ2luU291cmNlQ29kZSwgX2Vycm9ySW5mby5yYW5nZSxcbiAgICAgICAgICAgICAgICApO1xuICAgICAgICAgICAgICAgIGlmICgvXFxTLy50ZXN0KG5vZGVTb3VyY2VDb2RlWzBdKSkge1xuICAgICAgICAgICAgICAgICAgcmV0dXJuICdcXG4nICsgbm9kZVNvdXJjZUNvZGU7XG4gICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgICAgIHJldHVybiBub2RlU291cmNlQ29kZTtcbiAgICAgICAgICAgICAgfSkuam9pbignJyk7XG4gICAgICAgICAgICAgIGxldCBpbnNlcnRGaXhlciA9IG51bGw7XG4gICAgICAgICAgICAgIGxldCByZXBsYWNlU291cmNlQ29kZSA9ICcnO1xuICAgICAgICAgICAgICBpZiAoIWxhc3RMZWdhbEltcCkge1xuICAgICAgICAgICAgICAgIGluc2VydFNvdXJjZUNvZGUgPVxuICAgICAgICAgICAgICAgICAgICBpbnNlcnRTb3VyY2VDb2RlLnRyaW0oKSArIGluc2VydFNvdXJjZUNvZGUubWF0Y2goL14oXFxzKykvKVswXTtcbiAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgICBpbnNlcnRGaXhlciA9IGxhc3RMZWdhbEltcCA/XG4gICAgICAgICAgICAgICAgZml4ZXIuaW5zZXJ0VGV4dEFmdGVyKGxhc3RMZWdhbEltcCwgaW5zZXJ0U291cmNlQ29kZSkgOlxuICAgICAgICAgICAgICAgIGZpeGVyLmluc2VydFRleHRCZWZvcmUoYm9keVswXSwgaW5zZXJ0U291cmNlQ29kZSk7XG4gICAgICAgICAgICAgIGNvbnN0IGZpeGVycyA9IFtpbnNlcnRGaXhlcl0uY29uY2F0KHJlbW92ZUZpeGVycyk7XG4gICAgICAgICAgICAgIGZpeGVycy5mb3JFYWNoKGZ1bmN0aW9uIChjb21wdXRlZEZpeGVyLCBpKSB7XG4gICAgICAgICAgICAgICAgcmVwbGFjZVNvdXJjZUNvZGUgKz0gKG9yaWdpblNvdXJjZUNvZGUuc2xpY2UoXG4gICAgICAgICAgICAgICAgICBmaXhlcnNbaSAtIDFdID8gZml4ZXJzW2kgLSAxXS5yYW5nZVsxXSA6IDAsIGNvbXB1dGVkRml4ZXIucmFuZ2VbMF0sXG4gICAgICAgICAgICAgICAgKSArIGNvbXB1dGVkRml4ZXIudGV4dCk7XG4gICAgICAgICAgICAgIH0pO1xuICAgICAgICAgICAgICByZXR1cm4gZml4ZXIucmVwbGFjZVRleHRSYW5nZShyYW5nZSwgcmVwbGFjZVNvdXJjZUNvZGUpO1xuICAgICAgICAgICAgfTtcbiAgICAgICAgICB9XG4gICAgICAgICAgY29udGV4dC5yZXBvcnQoaW5mb3MpO1xuICAgICAgICB9KTtcbiAgICAgIH0sXG4gICAgfTtcbiAgfSxcbn07XG4iXX0=