GoScrobble/web/node_modules/eslint-plugin-import/lib/rules/first.js

138 lines
19 KiB
JavaScript
Raw Normal View History

2022-04-25 02:47:15 +00:00
'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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9maXJzdC5qcyJdLCJuYW1lcyI6WyJnZXRJbXBvcnRWYWx1ZSIsIm5vZGUiLCJ0eXBlIiwic291cmNlIiwidmFsdWUiLCJtb2R1bGVSZWZlcmVuY2UiLCJleHByZXNzaW9uIiwibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJkb2NzIiwidXJsIiwiZml4YWJsZSIsInNjaGVtYSIsImNyZWF0ZSIsImNvbnRleHQiLCJpc1Bvc3NpYmxlRGlyZWN0aXZlIiwibiIsImJvZHkiLCJhYnNvbHV0ZUZpcnN0Iiwib3B0aW9ucyIsIm1lc3NhZ2UiLCJzb3VyY2VDb2RlIiwiZ2V0U291cmNlQ29kZSIsIm9yaWdpblNvdXJjZUNvZGUiLCJnZXRUZXh0Iiwibm9uSW1wb3J0Q291bnQiLCJhbnlFeHByZXNzaW9ucyIsImFueVJlbGF0aXZlIiwibGFzdExlZ2FsSW1wIiwiZXJyb3JJbmZvcyIsInNob3VsZFNvcnQiLCJsYXN0U29ydE5vZGVzSW5kZXgiLCJmb3JFYWNoIiwiaW5kZXgiLCJ0ZXN0IiwicmVwb3J0IiwiZ2V0RGVjbGFyZWRWYXJpYWJsZXMiLCJ2YXJpYWJsZSIsInJlZmVyZW5jZXMiLCJsZW5ndGgiLCJyZWZlcmVuY2UiLCJpZGVudGlmaWVyIiwicmFuZ2UiLCJwdXNoIiwiZXJyb3JJbmZvIiwiaW5mb3MiLCJmaXgiLCJmaXhlciIsImluc2VydFRleHRBZnRlciIsInNvcnROb2RlcyIsInNsaWNlIiwicmVtb3ZlRml4ZXJzIiwibWFwIiwiX2Vycm9ySW5mbyIsInJlb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