webhook/node_modules/eslint-plugin-import/lib/rules/first.js

132 lines
16 KiB
JavaScript
Raw Normal View History

'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',
schema: [
{
type: 'string',
enum: ['absolute-first', 'disable-absolute-first'] }] },
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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9maXJzdC5qcyJdLCJuYW1lcyI6WyJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIsInR5cGUiLCJkb2NzIiwidXJsIiwiZml4YWJsZSIsInNjaGVtYSIsImVudW0iLCJjcmVhdGUiLCJjb250ZXh0IiwiaXNQb3NzaWJsZURpcmVjdGl2ZSIsIm5vZGUiLCJleHByZXNzaW9uIiwidmFsdWUiLCJuIiwiYm9keSIsImFic29sdXRlRmlyc3QiLCJvcHRpb25zIiwibWVzc2FnZSIsInNvdXJjZUNvZGUiLCJnZXRTb3VyY2VDb2RlIiwib3JpZ2luU291cmNlQ29kZSIsImdldFRleHQiLCJub25JbXBvcnRDb3VudCIsImFueUV4cHJlc3Npb25zIiwiYW55UmVsYXRpdmUiLCJsYXN0TGVnYWxJbXAiLCJlcnJv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