odin-js-fundamentals-part-5/node_modules/eslint-plugin-import/lib/rules/first.js

144 lines
20 KiB
JavaScript
Raw Normal View History

2024-01-11 08:52:05 +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: {
category: 'Style guide',
description: 'Ensure all imports appear before other statements.',
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;
if (!body) {
return;
}
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' + String(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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9maXJzdC5qcyJdLCJuYW1lcyI6WyJnZXRJbXBvcnRWYWx1ZSIsIm5vZGUiLCJ0eXBlIiwic291cmNlIiwidmFsdWUiLCJtb2R1bGVSZWZlcmVuY2UiLCJleHByZXNzaW9uIiwibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJkb2NzIiwiY2F0ZWdvcnkiLCJkZXNjcmlwdGlvbiIsInVybCIsImZpeGFibGUiLCJzY2hlbWEiLCJjcmVhdGUiLCJjb250ZXh0IiwiaXNQb3NzaWJsZURpcmVjdGl2ZSIsIlByb2dyYW0iLCJuIiwiYm9keSIsImFic29sdXRlRmlyc3QiLCJvcHRpb25zIiwibWVzc2FnZSIsInNvdXJjZUNvZGUiLCJnZXRTb3VyY2VDb2RlIiwib3JpZ2luU291cmNlQ29kZSIsImdldFRleHQiLCJub25JbXBvcnRDb3VudCIsImFueUV4cHJlc3Npb25zIiwiYW55UmVsYXRpdmUiLCJsYXN0TGVnYWxJbXAiLCJlcnJvckluZm9zIiwic2hvdWxkU29ydCIsImxhc3RTb3J0Tm9kZXNJbmRleCIsImZvckVhY2giLCJpbmRleCIsInRlc3QiLCJyZXBvcnQiLCJnZXREZWNsYXJlZFZhcmlhYmxlcyIsInZhcmlhYmxlIiwicmVmZXJlbmNlcyIsImxlbmd0aCIsInJlZmVyZW5jZSIsImlkZW50aWZpZXIiLCJyYW5nZSIsInB1c2giLCJlcnJvckluZm8iLCJpbmZvcyIsImZpeCIsImZpeGVyIiwiaW5zZXJ0VGV4dEFmdGVyIiwic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