mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-29 03:45:15 +00:00
138 lines
19 KiB
JavaScript
138 lines
19 KiB
JavaScript
'use strict';var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}
|
|
|
|
function getImportValue(node) {
|
|
return node.type === 'ImportDeclaration' ?
|
|
node.source.value :
|
|
node.moduleReference.expression.value;
|
|
}
|
|
|
|
module.exports = {
|
|
meta: {
|
|
type: 'suggestion',
|
|
docs: {
|
|
url: (0, _docsUrl2['default'])('first') },
|
|
|
|
fixable: 'code',
|
|
schema: [
|
|
{
|
|
type: 'string',
|
|
'enum': ['absolute-first', 'disable-absolute-first'] }] },
|
|
|
|
|
|
|
|
|
|
create: function () {function create(context) {
|
|
function isPossibleDirective(node) {
|
|
return node.type === 'ExpressionStatement' &&
|
|
node.expression.type === 'Literal' &&
|
|
typeof node.expression.value === 'string';
|
|
}
|
|
|
|
return {
|
|
'Program': function () {function Program(n) {
|
|
var body = n.body;
|
|
var absoluteFirst = context.options[0] === 'absolute-first';
|
|
var message = 'Import in body of module; reorder to top.';
|
|
var sourceCode = context.getSourceCode();
|
|
var originSourceCode = sourceCode.getText();
|
|
var nonImportCount = 0;
|
|
var anyExpressions = false;
|
|
var anyRelative = false;
|
|
var lastLegalImp = null;
|
|
var errorInfos = [];
|
|
var shouldSort = true;
|
|
var lastSortNodesIndex = 0;
|
|
body.forEach(function (node, index) {
|
|
if (!anyExpressions && isPossibleDirective(node)) {
|
|
return;
|
|
}
|
|
|
|
anyExpressions = true;
|
|
|
|
if (node.type === 'ImportDeclaration' || node.type === 'TSImportEqualsDeclaration') {
|
|
if (absoluteFirst) {
|
|
if (/^\./.test(getImportValue(node))) {
|
|
anyRelative = true;
|
|
} else if (anyRelative) {
|
|
context.report({
|
|
node: node.type === 'ImportDeclaration' ? node.source : node.moduleReference,
|
|
message: 'Absolute imports should come before relative imports.' });
|
|
|
|
}
|
|
}
|
|
if (nonImportCount > 0) {var _iteratorNormalCompletion = true;var _didIteratorError = false;var _iteratorError = undefined;try {
|
|
for (var _iterator = context.getDeclaredVariables(node)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {var variable = _step.value;
|
|
if (!shouldSort) break;
|
|
var references = variable.references;
|
|
if (references.length) {var _iteratorNormalCompletion2 = true;var _didIteratorError2 = false;var _iteratorError2 = undefined;try {
|
|
for (var _iterator2 = references[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {var reference = _step2.value;
|
|
if (reference.identifier.range[0] < node.range[1]) {
|
|
shouldSort = false;
|
|
break;
|
|
}
|
|
}} catch (err) {_didIteratorError2 = true;_iteratorError2 = err;} finally {try {if (!_iteratorNormalCompletion2 && _iterator2['return']) {_iterator2['return']();}} finally {if (_didIteratorError2) {throw _iteratorError2;}}}
|
|
}
|
|
}} catch (err) {_didIteratorError = true;_iteratorError = err;} finally {try {if (!_iteratorNormalCompletion && _iterator['return']) {_iterator['return']();}} finally {if (_didIteratorError) {throw _iteratorError;}}}
|
|
shouldSort && (lastSortNodesIndex = errorInfos.length);
|
|
errorInfos.push({
|
|
node: node,
|
|
range: [body[index - 1].range[1], node.range[1]] });
|
|
|
|
} else {
|
|
lastLegalImp = node;
|
|
}
|
|
} else {
|
|
nonImportCount++;
|
|
}
|
|
});
|
|
if (!errorInfos.length) return;
|
|
errorInfos.forEach(function (errorInfo, index) {
|
|
var node = errorInfo.node;
|
|
var infos = {
|
|
node: node,
|
|
message: message };
|
|
|
|
if (index < lastSortNodesIndex) {
|
|
infos.fix = function (fixer) {
|
|
return fixer.insertTextAfter(node, '');
|
|
};
|
|
} else if (index === lastSortNodesIndex) {
|
|
var sortNodes = errorInfos.slice(0, lastSortNodesIndex + 1);
|
|
infos.fix = function (fixer) {
|
|
var removeFixers = sortNodes.map(function (_errorInfo) {
|
|
return fixer.removeRange(_errorInfo.range);
|
|
});
|
|
var range = [0, removeFixers[removeFixers.length - 1].range[1]];
|
|
var insertSourceCode = sortNodes.map(function (_errorInfo) {
|
|
var nodeSourceCode = String.prototype.slice.apply(
|
|
originSourceCode, _errorInfo.range);
|
|
|
|
if (/\S/.test(nodeSourceCode[0])) {
|
|
return '\n' + nodeSourceCode;
|
|
}
|
|
return nodeSourceCode;
|
|
}).join('');
|
|
var insertFixer = null;
|
|
var replaceSourceCode = '';
|
|
if (!lastLegalImp) {
|
|
insertSourceCode =
|
|
insertSourceCode.trim() + insertSourceCode.match(/^(\s+)/)[0];
|
|
}
|
|
insertFixer = lastLegalImp ?
|
|
fixer.insertTextAfter(lastLegalImp, insertSourceCode) :
|
|
fixer.insertTextBefore(body[0], insertSourceCode);
|
|
var fixers = [insertFixer].concat(removeFixers);
|
|
fixers.forEach(function (computedFixer, i) {
|
|
replaceSourceCode += originSourceCode.slice(
|
|
fixers[i - 1] ? fixers[i - 1].range[1] : 0, computedFixer.range[0]) +
|
|
computedFixer.text;
|
|
});
|
|
return fixer.replaceTextRange(range, replaceSourceCode);
|
|
};
|
|
}
|
|
context.report(infos);
|
|
});
|
|
}return Program;}() };
|
|
|
|
}return create;}() };
|
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9maXJzdC5qcyJdLCJuYW1lcyI6WyJnZXRJbXBvcnRWYWx1ZSIsIm5vZGUiLCJ0eXBlIiwic291cmNlIiwidmFsdWUiLCJtb2R1bGVSZWZlcmVuY2UiLCJleHByZXNzaW9uIiwibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJkb2NzIiwidXJsIiwiZml4YWJsZSIsInNjaGVtYSIsImNyZWF0ZSIsImNvbnRleHQiLCJpc1Bvc3NpYmxlRGlyZWN0aXZlIiwibiIsImJvZHkiLCJhYnNvbHV0ZUZpcnN0Iiw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WZvcmUgcmVsYXRpdmUgaW1wb3J0cy4nLFxuICAgICAgICAgICAgICAgIH0pO1xuICAgICAgICAgICAgICB9XG4gICAgICAgICAgICB9XG4gICAgICAgICAgICBpZiAobm9uSW1wb3J0Q291bnQgPiAwKSB7XG4gICAgICAgICAgICAgIGZvciAoY29uc3QgdmFyaWFibGUgb2YgY29udGV4dC5nZXREZWNsYXJlZFZhcmlhYmxlcyhub2RlKSkge1xuICAgICAgICAgICAgICAgIGlmICghc2hvdWxkU29ydCkgYnJlYWs7XG4gICAgICAgICAgICAgICAgY29uc3QgcmVmZXJlbmNlcyA9IHZhcmlhYmxlLnJlZmVyZW5jZXM7XG4gICAgICAgICAgICAgICAgaWYgKHJlZmVyZW5jZXMubGVuZ3RoKSB7XG4gICAgICAgICAgICAgICAgICBmb3IgKGNvbnN0IHJlZmVyZW5jZSBvZiByZWZlcmVuY2VzKSB7XG4gICAgICAgICAgICAgICAgICAgIGlmIChyZWZlcmVuY2UuaWRlbnRpZmllci5yYW5nZVswXSA8IG5vZGUucmFuZ2VbMV0pIHtcbiAgICAgICAgICAgICAgICAgICAgICBzaG91bGRTb3J0ID0gZmFsc2U7XG4gICAgICAgICAgICAgICAgICAgICAgYnJlYWs7XG4gICAgICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgICB9XG4gICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgc2hvdWxkU29ydCAmJiAobGFzdFNvcnROb2Rlc0luZGV4ID0gZXJyb3JJbmZvcy5sZW5ndGgpO1xuICAgICAgICAgICAgICBlcnJvckluZm9zLnB1c2goe1xuICAgICAgICAgICAgICAgIG5vZGUsXG4gICAgICAgICAgICAgICAgcmFuZ2U6IFtib2R5W2luZGV4IC0gMV0ucmFuZ2VbMV0sIG5vZGUucmFuZ2VbMV1dLFxuICAgICAgICAgICAgICB9KTtcbiAgICAgICAgICAgIH0gZWxzZSB7XG4gICAgICAgICAgICAgIGxhc3RMZWdhbEltcCA9IG5vZGU7XG4gICAgICAgICAgICB9XG4gICAgICAgICAgfSBlbHNlIHtcbiAgICAgICAgICAgIG5vbkltcG9ydENvdW50Kys7XG4gICAgICAgICAgfVxuICAgICAgICB9KTtcbiAgICAgICAgaWYgKCFlcnJvckluZm9zLmxlbmd0aCkgcmV0dXJuO1xuICAgICAgICBlcnJvckluZm9zLmZvckVhY2goZnVuY3Rpb24gKGVycm9ySW5mbywgaW5kZXgpIHtcbiAgICAgICAgICBjb25zdCBub2RlID0gZXJyb3JJbmZvLm5vZGU7XG4gICAgICAgICAgY29uc3QgaW5mb3MgPSB7XG4gICAgICAgICAgICBub2RlLFxuICAgICAgICAgICAgbWVzc2FnZSxcbiAgICAgICAgICB9O1xuICAgICAgICAgIGlmIChpbmRleCA8IGxhc3RTb3J0Tm9kZXNJbmRleCkge1xuICAgICAgICAgICAgaW5mb3MuZml4ID0gZnVuY3Rpb24gKGZpeGVyKSB7XG4gICAgICAgICAgICAgIHJldHVybiBmaXhlci5pbnNlcnRUZXh0QWZ0ZXIobm9kZSwgJycpO1xuICAgICAgICAgICAgfTtcbiAgICAgICAgICB9IGVsc2UgaWYgKGluZGV4ID09PSBsYXN0U29ydE5vZGVzSW5kZXgpIHtcbiAgICAgICAgICAgIGNvbnN0IHNvcnROb2RlcyA9IGVycm9ySW5mb3Muc2xpY2UoMCwgbGFzdFNvcnROb2Rlc0luZGV4ICsgMSk7XG4gICAgICAgICAgICBpbmZvcy5maXggPSBmdW5jdGlvbiAoZml4ZXIpIHtcbiAgICAgICAgICAgICAgY29uc3QgcmVtb3ZlRml4ZXJzID0gc29ydE5vZGVzLm1hcChmdW5jdGlvbiAoX2Vycm9ySW5mbykge1xuICAgICAgICAgICAgICAgIHJldHVybiBmaXhlci5yZW1vdmVSYW5nZShfZXJyb3JJbmZvLnJhbmdlKTtcbiAgICAgICAgICAgICAgfSk7XG4gICAgICAgICAgICAgIGNvbnN0IHJhbmdlID0gWzAsIHJlbW92ZUZpeGVyc1tyZW1vdmVGaXhlcnMubGVuZ3RoIC0gMV0ucmFuZ2VbMV1dO1xuICAgICAgICAgICAgICBsZXQgaW5zZXJ0U291cmNlQ29kZSA9IHNvcnROb2Rlcy5tYXAoZnVuY3Rpb24gKF9lcnJvckluZm8pIHtcbiAgICAgICAgICAgICAgICBjb25zdCBub2RlU291cmNlQ29kZSA9IFN0cmluZy5wcm90b3R5cGUuc2xpY2UuYXBwbHkoXG4gICAgICAgICAgICAgICAgICBvcmlnaW5Tb3VyY2VDb2RlLCBfZXJyb3JJbmZvLnJhbmdlXG4gICAgICAgICAgICAgICAgKTtcbiAgICAgICAgICAgICAgICBpZiAoL1xcUy8udGVzdChub2RlU291cmNlQ29kZVswXSkpIHtcbiAgICAgICAgICAgICAgICAgIHJldHVybiAnXFxuJyArIG5vZGVTb3VyY2VDb2RlO1xuICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgICByZXR1cm4gbm9kZVNvdXJjZUNvZGU7XG4gICAgICAgICAgICAgIH0pLmpvaW4oJycpO1xuICAgICAgICAgICAgICBsZXQgaW5zZXJ0Rml4ZXIgPSBudWxsO1xuICAgICAgICAgICAgICBsZXQgcmVwbGFjZVNvdXJjZUNvZGUgPSAnJztcbiAgICAgICAgICAgICAgaWYgKCFsYXN0TGVnYWxJbXApIHtcbiAgICAgICAgICAgICAgICBpbnNlcnRTb3VyY2VDb2RlID1cbiAgICAgICAgICAgICAgICAgICAgaW5zZXJ0U291cmNlQ29kZS50cmltKCkgKyBpbnNlcnRTb3VyY2VDb2RlLm1hdGNoKC9eKFxccyspLylbMF07XG4gICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgaW5zZXJ0Rml4ZXIgPSBsYXN0TGVnYWxJbXAgP1xuICAgICAgICAgICAgICAgIGZpeGVyLmluc2VydFRleHRBZnRlcihsYXN0TGVnYWxJbXAsIGluc2VydFNvdXJjZUNvZGUpIDpcbiAgICAgICAgICAgICAgICBmaXhlci5pbnNlcnRUZXh0QmVmb3JlKGJvZHlbMF0sIGluc2VydFNvdXJjZUNvZGUpO1xuICAgICAgICAgICAgICBjb25zdCBmaXhlcnMgPSBbaW5zZXJ0Rml4ZXJdLmNvbmNhdChyZW1vdmVGaXhlcnMpO1xuICAgICAgICAgICAgICBmaXhlcnMuZm9yRWFjaChmdW5jdGlvbiAoY29tcHV0ZWRGaXhlciwgaSkge1xuICAgICAgICAgICAgICAgIHJlcGxhY2VTb3VyY2VDb2RlICs9IChvcmlnaW5Tb3VyY2VDb2RlLnNsaWNlKFxuICAgICAgICAgICAgICAgICAgZml4ZXJzW2kgLSAxXSA/IGZpeGVyc1tpIC0gMV0ucmFuZ2VbMV0gOiAwLCBjb21wdXRlZEZpeGVyLnJhbmdlWzBdXG4gICAgICAgICAgICAgICAgKSArIGNvbXB1dGVkRml4ZXIudGV4dCk7XG4gICAgICAgICAgICAgIH0pO1xuICAgICAgICAgICAgICByZXR1cm4gZml4ZXIucmVwbGFjZVRleHRSYW5nZShyYW5nZSwgcmVwbGFjZVNvdXJjZUNvZGUpO1xuICAgICAgICAgICAgfTtcbiAgICAgICAgICB9XG4gICAgICAgICAgY29udGV4dC5yZXBvcnQoaW5mb3MpO1xuICAgICAgICB9KTtcbiAgICAgIH0sXG4gICAgfTtcbiAgfSxcbn07XG4iXX0=
|