webhook-action/node_modules/eslint-plugin-import/lib/rules/first.js
2020-08-26 09:57:08 +10:00

132 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',
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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9maXJzdC5qcyJdLCJuYW1lcyI6WyJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIsInR5cGUiLCJkb2NzIiwidXJsIiwiZml4YWJsZSIsInNjaGVtYSIsImVudW0iLCJjcmVhdGUiLCJjb250ZXh0IiwiaXNQb3NzaWJsZURpcmVjdGl2ZSIsIm5vZGUiLCJleHByZXNzaW9uIiwidmFsdWUiLCJuIiwiYm9keSIsImFic29sdXRlRmlyc3QiLCJvcHRpb25zIiwibWVzc2FnZSIsInNvdXJjZUNvZGUiLCJnZXRTb3VyY2VDb2RlIiwib3JpZ2luU291cmNlQ29kZSIsImdldFRleHQiLCJub25JbXBvcnRDb3VudCIsImFueUV4cHJlc3Npb25zIiwiYW55UmVsYXRpdmUiLCJsYXN0TGVnYWxJb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ZiBtb2R1bGU7IHJlb3JkZXIgdG8gdG9wLidcbiAgICAgICAgICAgICwgc291cmNlQ29kZSA9IGNvbnRleHQuZ2V0U291cmNlQ29kZSgpXG4gICAgICAgICAgICAsIG9yaWdpblNvdXJjZUNvZGUgPSBzb3VyY2VDb2RlLmdldFRleHQoKVxuICAgICAgICBsZXQgbm9uSW1wb3J0Q291bnQgPSAwXG4gICAgICAgICAgLCBhbnlFeHByZXNzaW9ucyA9IGZhbHNlXG4gICAgICAgICAgLCBhbnlSZWxhdGl2ZSA9IGZhbHNlXG4gICAgICAgICAgLCBsYXN0TGVnYWxJbXAgPSBudWxsXG4gICAgICAgICAgLCBlcnJvckluZm9zID0gW11cbiAgICAgICAgICAsIHNob3VsZFNvcnQgPSB0cnVlXG4gICAgICAgICAgLCBsYXN0U29ydE5vZGVzSW5kZXggPSAwXG4gICAgICAgIGJvZHkuZm9yRWFjaChmdW5jdGlvbiAobm9kZSwgaW5kZXgpe1xuICAgICAgICAgIGlmICghYW55RXhwcmVzc2lvbnMgJiYgaXNQb3NzaWJsZURpcmVjdGl2ZShub2RlKSkge1xuICAgICAgICAgICAgcmV0dXJuXG4gICAgICAgICAgfVxuXG4gICAgICAgICAgYW55RXhwcmVzc2lvbnMgPSB0cnVlXG5cbiAgICAgICAgICBpZiAobm9kZS50eXBlID09PSAnSW1wb3J0RGVjbGFyYXRpb24nKSB7XG4gICAgICAgICAgICBpZiAoYWJzb2x1dGVGaXJzdCkge1xuICAgICAgICAgICAgICBpZiAoL15cXC4vLnRlc3Qobm9kZS5zb3VyY2UudmFsdWUpKSB7XG4gICAgICAgICAgICAgICAgYW55UmVsYXRpdmUgPSB0cnVlXG4gICAgICAgICAgICAgIH0gZWxzZSBpZiAoYW55UmVsYXRpdmUpIHtcbiAgICAgICAgICAgICAgICBjb250ZXh0LnJlcG9ydCh7XG4gICAgICAgICAgICAgICAgICBub2RlOiBub2RlLnNvdXJjZSxcbiAgICAgICAgICAgICAgICAgIG1lc3NhZ2U6ICdBYnNvbHV0ZSBpbXBvcnRzIHNob3VsZCBjb21lIGJlZm9yZSByZWxhdGl2ZSBpbXBvcnRzLicsXG4gICAgICAgICAgICAgICAgfSlcbiAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgfVxuICAgICAgICAgICAgaWYgKG5vbkltcG9ydENvdW50ID4gMCkge1xuICAgICAgICAgICAgICBmb3IgKGxldCB2YXJpYWJsZSBvZiBjb250ZXh0LmdldERlY2xhcmVkVmFyaWFibGVzKG5vZGUpKSB7XG4gICAgICAgICAgICAgICAgaWYgKCFzaG91bGRTb3J0KSBicmVha1xuICAgICAgICAgICAgICAgIGNvbnN0IHJlZmVyZW5jZXMgPSB2YXJpYWJsZS5yZWZlcmVuY2VzXG4gICAgICAgICAgICAgICAgaWYgKHJlZmVyZW5jZXMubGVuZ3RoKSB7XG4gICAgICAgICAgICAgICAgICBmb3IgKGxldCByZWZlcmVuY2Ugb2YgcmVmZXJlbmNlcykge1xuICAgICAgICAgICAgICAgICAgICBpZiAocmVmZXJlbmNlLmlkZW50aWZpZXIucmFuZ2VbMF0gPCBub2RlLnJhbmdlWzFdKSB7XG4gICAgICAgICAgICAgICAgICAgICAgc2hvdWxkU29ydCA9IGZhbHNlXG4gICAgICAgICAgICAgICAgICAgICAgYnJlYWtcbiAgICAgICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgICBzaG91bGRTb3J0ICYmIChsYXN0U29ydE5vZGVzSW5kZXggPSBlcnJvckluZm9zLmxlbmd0aClcbiAgICAgICAgICAgICAgZXJyb3JJbmZvcy5wdXNoKHtcbiAgICAgICAgICAgICAgICBub2RlLFxuICAgICAgICAgICAgICAgIHJhbmdlOiBbYm9keVtpbmRleCAtIDFdLnJhbmdlWzFdLCBub2RlLnJhbmdlWzFdXSxcbiAgICAgICAgICAgICAgfSlcbiAgICAgICAgICAgIH0gZWxzZSB7XG4gICAgICAgICAgICAgIGxhc3RMZWdhbEltcCA9IG5vZGVcbiAgICAgICAgICAgIH1cbiAgICAgICAgICB9IGVsc2Uge1xuICAgICAgICAgICAgbm9uSW1wb3J0Q291bnQrK1xuICAgICAgICAgIH1cbiAgICAgICAgfSlcbiAgICAgICAgaWYgKCFlcnJvckluZm9zLmxlbmd0aCkgcmV0dXJuXG4gICAgICAgIGVycm9ySW5mb3MuZm9yRWFjaChmdW5jdGlvbiAoZXJyb3JJbmZvLCBpbmRleCkge1xuICAgICAgICAgIGNvbnN0IG5vZGUgPSBlcnJvckluZm8ubm9kZVxuICAgICAgICAgICAgICAsIGluZm9zID0ge1xuICAgICAgICAgICAgICAgIG5vZGUsXG4gICAgICAgICAgICAgICAgbWVzc2FnZSxcbiAgICAgICAgICAgICAgfVxuICAgICAgICAgIGlmIChpbmRleCA8IGxhc3RTb3J0Tm9kZXNJbmRleCkge1xuICAgICAgICAgICAgaW5mb3MuZml4ID0gZnVuY3Rpb24gKGZpeGVyKSB7XG4gICAgICAgICAgICAgIHJldHVybiBmaXhlci5pbnNlcnRUZXh0QWZ0ZXIobm9kZSwgJycpXG4gICAgICAgICAgICB9XG4gICAgICAgICAgfSBlbHNlIGlmIChpbmRleCA9PT0gbGFzdFNvcnROb2Rlc0luZGV4KSB7XG4gICAgICAgICAgICBjb25zdCBzb3J0Tm9kZXMgPSBlcnJvckluZm9zLnNsaWNlKDAsIGxhc3RTb3J0Tm9kZXNJbmRleCArIDEpXG4gICAgICAgICAgICBpbmZvcy5maXggPSBmdW5jdGlvbiAoZml4ZXIpIHtcbiAgICAgICAgICAgICAgY29uc3QgcmVtb3ZlRml4ZXJzID0gc29ydE5vZGVzLm1hcChmdW5jdGlvbiAoX2Vycm9ySW5mbykge1xuICAgICAgICAgICAgICAgICAgICByZXR1cm4gZml4ZXIucmVtb3ZlUmFuZ2UoX2Vycm9ySW5mby5yYW5nZSlcbiAgICAgICAgICAgICAgICAgIH0pXG4gICAgICAgICAgICAgICAgICAsIHJhbmdlID0gWzAsIHJlbW92ZUZpeGVyc1tyZW1vdmVGaXhlcnMubGVuZ3RoIC0gMV0ucmFuZ2VbMV1dXG4gICAgICAgICAgICAgIGxldCBpbnNlcnRTb3VyY2VDb2RlID0gc29ydE5vZGVzLm1hcChmdW5jdGlvbiAoX2Vycm9ySW5mbykge1xuICAgICAgICAgICAgICAgICAgICBjb25zdCBub2RlU291cmNlQ29kZSA9IFN0cmluZy5wcm90b3R5cGUuc2xpY2UuYXBwbHkoXG4gICAgICAgICAgICAgICAgICAgICAgb3JpZ2luU291cmNlQ29kZSwgX2Vycm9ySW5mby5yYW5nZVxuICAgICAgICAgICAgICAgICAgICApXG4gICAgICAgICAgICAgICAgICAgIGlmICgvXFxTLy50ZXN0KG5vZGVTb3VyY2VDb2RlWzBdKSkge1xuICAgICAgICAgICAgICAgICAgICAgIHJldHVybiAnXFxuJyArIG5vZGVTb3VyY2VDb2RlXG4gICAgICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgICAgICAgcmV0dXJuIG5vZGVTb3VyY2VDb2RlXG4gICAgICAgICAgICAgICAgICB9KS5qb2luKCcnKVxuICAgICAgICAgICAgICAgICwgaW5zZXJ0Rml4ZXIgPSBudWxsXG4gICAgICAgICAgICAgICAgLCByZXBsYWNlU291cmNlQ29kZSA9ICcnXG4gICAgICAgICAgICAgIGlmICghbGFzdExlZ2FsSW1wKSB7XG4gICAgICAgICAgICAgICAgICBpbnNlcnRTb3VyY2VDb2RlID1cbiAgICAgICAgICAgICAgICAgICAgaW5zZXJ0U291cmNlQ29kZS50cmltKCkgKyBpbnNlcnRTb3VyY2VDb2RlLm1hdGNoKC9eKFxccyspLylbMF1cbiAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgICBpbnNlcnRGaXhlciA9IGxhc3RMZWdhbEltcCA/XG4gICAgICAgICAgICAgICAgICAgICAgICAgICAgZml4ZXIuaW5zZXJ0VGV4dEFmdGVyKGxhc3RMZWdhbEltcCwgaW5zZXJ0U291cmNlQ29kZSkgOlxuICAgICAgICAgICAgICAgICAgICAgICAgICAgIGZpeGVyLmluc2VydFRleHRCZWZvcmUoYm9keVswXSwgaW5zZXJ0U291cmNlQ29kZSlcbiAgICAgICAgICAgICAgY29uc3QgZml4ZXJzID0gW2luc2VydEZpeGVyXS5jb25jYXQocmVtb3ZlRml4ZXJzKVxuICAgICAgICAgICAgICBmaXhlcnMuZm9yRWFjaChmdW5jdGlvbiAoY29tcHV0ZWRGaXhlciwgaSkge1xuICAgICAgICAgICAgICAgIHJlcGxhY2VTb3VyY2VDb2RlICs9IChvcmlnaW5Tb3VyY2VDb2RlLnNsaWNlKFxuICAgICAgICAgICAgICAgICAgZml4ZXJzW2kgLSAxXSA/IGZpeGVyc1tpIC0gMV0ucmFuZ2VbMV0gOiAwLCBjb21wdXRlZEZpeGVyLnJhbmdlWzBdXG4gICAgICAgICAgICAgICAgKSArIGNvbXB1dGVkRml4ZXIudGV4dClcbiAgICAgICAgICAgICAgfSlcbiAgICAgICAgICAgICAgcmV0dXJuIGZpeGVyLnJlcGxhY2VUZXh0UmFuZ2UocmFuZ2UsIHJlcGxhY2VTb3VyY2VDb2RlKVxuICAgICAgICAgICAgfVxuICAgICAgICAgIH1cbiAgICAgICAgICBjb250ZXh0LnJlcG9ydChpbmZvcylcbiAgICAgICAgfSlcbiAgICAgIH0sXG4gICAgfVxuICB9LFxufVxuIl19