124 lines
16 KiB
JavaScript
124 lines
16 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
var _docsUrl = require('../docsUrl');
|
||
|
|
||
|
var _docsUrl2 = _interopRequireDefault(_docsUrl);
|
||
|
|
||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||
|
|
||
|
module.exports = {
|
||
|
meta: {
|
||
|
type: 'suggestion',
|
||
|
docs: {
|
||
|
url: (0, _docsUrl2.default)('first')
|
||
|
},
|
||
|
fixable: 'code'
|
||
|
},
|
||
|
|
||
|
create: function (context) {
|
||
|
function isPossibleDirective(node) {
|
||
|
return node.type === 'ExpressionStatement' && node.expression.type === 'Literal' && typeof node.expression.value === 'string';
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
'Program': function (n) {
|
||
|
const body = n.body,
|
||
|
absoluteFirst = context.options[0] === 'absolute-first',
|
||
|
message = 'Import in body of module; reorder to top.',
|
||
|
sourceCode = context.getSourceCode(),
|
||
|
originSourceCode = sourceCode.getText();
|
||
|
let nonImportCount = 0,
|
||
|
anyExpressions = false,
|
||
|
anyRelative = false,
|
||
|
lastLegalImp = null,
|
||
|
errorInfos = [],
|
||
|
shouldSort = true,
|
||
|
lastSortNodesIndex = 0;
|
||
|
body.forEach(function (node, index) {
|
||
|
if (!anyExpressions && isPossibleDirective(node)) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
anyExpressions = true;
|
||
|
|
||
|
if (node.type === 'ImportDeclaration') {
|
||
|
if (absoluteFirst) {
|
||
|
if (/^\./.test(node.source.value)) {
|
||
|
anyRelative = true;
|
||
|
} else if (anyRelative) {
|
||
|
context.report({
|
||
|
node: node.source,
|
||
|
message: 'Absolute imports should come before relative imports.'
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
if (nonImportCount > 0) {
|
||
|
for (let variable of context.getDeclaredVariables(node)) {
|
||
|
if (!shouldSort) break;
|
||
|
const references = variable.references;
|
||
|
if (references.length) {
|
||
|
for (let reference of references) {
|
||
|
if (reference.identifier.range[0] < node.range[1]) {
|
||
|
shouldSort = false;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
shouldSort && (lastSortNodesIndex = errorInfos.length);
|
||
|
errorInfos.push({
|
||
|
node,
|
||
|
range: [body[index - 1].range[1], node.range[1]]
|
||
|
});
|
||
|
} else {
|
||
|
lastLegalImp = node;
|
||
|
}
|
||
|
} else {
|
||
|
nonImportCount++;
|
||
|
}
|
||
|
});
|
||
|
if (!errorInfos.length) return;
|
||
|
errorInfos.forEach(function (errorInfo, index) {
|
||
|
const node = errorInfo.node,
|
||
|
infos = {
|
||
|
node,
|
||
|
message
|
||
|
};
|
||
|
if (index < lastSortNodesIndex) {
|
||
|
infos.fix = function (fixer) {
|
||
|
return fixer.insertTextAfter(node, '');
|
||
|
};
|
||
|
} else if (index === lastSortNodesIndex) {
|
||
|
const sortNodes = errorInfos.slice(0, lastSortNodesIndex + 1);
|
||
|
infos.fix = function (fixer) {
|
||
|
const removeFixers = sortNodes.map(function (_errorInfo) {
|
||
|
return fixer.removeRange(_errorInfo.range);
|
||
|
}),
|
||
|
range = [0, removeFixers[removeFixers.length - 1].range[1]];
|
||
|
let insertSourceCode = sortNodes.map(function (_errorInfo) {
|
||
|
const nodeSourceCode = String.prototype.slice.apply(originSourceCode, _errorInfo.range);
|
||
|
if (/\S/.test(nodeSourceCode[0])) {
|
||
|
return '\n' + nodeSourceCode;
|
||
|
}
|
||
|
return nodeSourceCode;
|
||
|
}).join(''),
|
||
|
insertFixer = null,
|
||
|
replaceSourceCode = '';
|
||
|
if (!lastLegalImp) {
|
||
|
insertSourceCode = insertSourceCode.trim() + insertSourceCode.match(/^(\s+)/)[0];
|
||
|
}
|
||
|
insertFixer = lastLegalImp ? fixer.insertTextAfter(lastLegalImp, insertSourceCode) : fixer.insertTextBefore(body[0], insertSourceCode);
|
||
|
const 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);
|
||
|
});
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
};
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9maXJzdC5qcyJdLCJuYW1lcyI6WyJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIsInR5cGUiLCJkb2NzIiwidXJsIiwiZml4YWJsZSIsImNyZWF0ZSIsImNvbnRleHQiLCJpc1Bvc3NpYmxlRGlyZWN0aXZlIiwibm9kZSIsImV4cHJlc3Npb24iLCJ2YWx1ZSIsIm4iLCJib2R5IiwiYWJzb2x1dGVGaXJzdCIsIm9wdGlvbnMiLCJtZXNzYWdlIiwic291cmNlQ29kZSIsImdldFNvdXJjZUNvZGUiLCJvcmlnaW5Tb3VyY2VDb2RlIiwiZ2V0VGV4dCIsIm5vbkltcG9ydENvdW50IiwiYW55RXhwcmVzc2lvbnMiLCJhbnlSZWxhdGl2ZSIsImxhc3RMZWdhbEltcCIsImVycm9ySW5mb3MiLCJzaG91bGRTb3J0IiwibGFzdFNvcnROb2Rlc0luZGV4IiwiZm9yRWFjaCIsImluZGV4IiwidGVzdCIsInNvdXJjZSIsInJlcG9ydCIsInZhcmlhYmxlIiwiZ2V0RGVjbGFyZWRWYXJpYWJsZXMiLCJyZWZlcmVuY2VzIiwibGVuZ3RoIiwicmVmZXJlbmNlIiwiaWRlbnRpZmllciIsInJhbmdlIiwicHVzaCIsImVycm9ySW5mbyIsImluZm9zIiwiZml4IiwiZml4ZXIiLCJpbnNlcnRUZXh0QWZ0ZXIiLCJzb3J0Tm9kZXMiLCJzbGljZSIsInJlbW92ZUZpeGVycyIsIm1hcCIsIl9l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