221 lines
25 KiB
JavaScript
221 lines
25 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
var _importType = require('../core/importType');
|
||
|
|
||
|
var _importType2 = _interopRequireDefault(_importType);
|
||
|
|
||
|
var _staticRequire = require('../core/staticRequire');
|
||
|
|
||
|
var _staticRequire2 = _interopRequireDefault(_staticRequire);
|
||
|
|
||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||
|
|
||
|
const defaultGroups = ['builtin', 'external', 'parent', 'sibling', 'index'];
|
||
|
|
||
|
// REPORTING
|
||
|
|
||
|
function reverse(array) {
|
||
|
return array.map(function (v) {
|
||
|
return {
|
||
|
name: v.name,
|
||
|
rank: -v.rank,
|
||
|
node: v.node
|
||
|
};
|
||
|
}).reverse();
|
||
|
}
|
||
|
|
||
|
function findOutOfOrder(imported) {
|
||
|
if (imported.length === 0) {
|
||
|
return [];
|
||
|
}
|
||
|
let maxSeenRankNode = imported[0];
|
||
|
return imported.filter(function (importedModule) {
|
||
|
const res = importedModule.rank < maxSeenRankNode.rank;
|
||
|
if (maxSeenRankNode.rank < importedModule.rank) {
|
||
|
maxSeenRankNode = importedModule;
|
||
|
}
|
||
|
return res;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function reportOutOfOrder(context, imported, outOfOrder, order) {
|
||
|
outOfOrder.forEach(function (imp) {
|
||
|
const found = imported.find(function hasHigherRank(importedItem) {
|
||
|
return importedItem.rank > imp.rank;
|
||
|
});
|
||
|
context.report(imp.node, '`' + imp.name + '` import should occur ' + order + ' import of `' + found.name + '`');
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function makeOutOfOrderReport(context, imported) {
|
||
|
const outOfOrder = findOutOfOrder(imported);
|
||
|
if (!outOfOrder.length) {
|
||
|
return;
|
||
|
}
|
||
|
// There are things to report. Try to minimize the number of reported errors.
|
||
|
const reversedImported = reverse(imported);
|
||
|
const reversedOrder = findOutOfOrder(reversedImported);
|
||
|
if (reversedOrder.length < outOfOrder.length) {
|
||
|
reportOutOfOrder(context, reversedImported, reversedOrder, 'after');
|
||
|
return;
|
||
|
}
|
||
|
reportOutOfOrder(context, imported, outOfOrder, 'before');
|
||
|
}
|
||
|
|
||
|
// DETECTING
|
||
|
|
||
|
function computeRank(context, ranks, name, type) {
|
||
|
return ranks[(0, _importType2.default)(name, context)] + (type === 'import' ? 0 : 100);
|
||
|
}
|
||
|
|
||
|
function registerNode(context, node, name, type, ranks, imported) {
|
||
|
const rank = computeRank(context, ranks, name, type);
|
||
|
if (rank !== -1) {
|
||
|
imported.push({ name, rank, node });
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function isInVariableDeclarator(node) {
|
||
|
return node && (node.type === 'VariableDeclarator' || isInVariableDeclarator(node.parent));
|
||
|
}
|
||
|
|
||
|
const types = ['builtin', 'external', 'internal', 'parent', 'sibling', 'index'];
|
||
|
|
||
|
// Creates an object with type-rank pairs.
|
||
|
// Example: { index: 0, sibling: 1, parent: 1, external: 1, builtin: 2, internal: 2 }
|
||
|
// Will throw an error if it contains a type that does not exist, or has a duplicate
|
||
|
function convertGroupsToRanks(groups) {
|
||
|
const rankObject = groups.reduce(function (res, group, index) {
|
||
|
if (typeof group === 'string') {
|
||
|
group = [group];
|
||
|
}
|
||
|
group.forEach(function (groupItem) {
|
||
|
if (types.indexOf(groupItem) === -1) {
|
||
|
throw new Error('Incorrect configuration of the rule: Unknown type `' + JSON.stringify(groupItem) + '`');
|
||
|
}
|
||
|
if (res[groupItem] !== undefined) {
|
||
|
throw new Error('Incorrect configuration of the rule: `' + groupItem + '` is duplicated');
|
||
|
}
|
||
|
res[groupItem] = index;
|
||
|
});
|
||
|
return res;
|
||
|
}, {});
|
||
|
|
||
|
const omittedTypes = types.filter(function (type) {
|
||
|
return rankObject[type] === undefined;
|
||
|
});
|
||
|
|
||
|
return omittedTypes.reduce(function (res, type) {
|
||
|
res[type] = groups.length;
|
||
|
return res;
|
||
|
}, rankObject);
|
||
|
}
|
||
|
|
||
|
function makeNewlinesBetweenReport(context, imported, newlinesBetweenImports) {
|
||
|
const getNumberOfEmptyLinesBetween = (currentImport, previousImport) => {
|
||
|
const linesBetweenImports = context.getSourceCode().lines.slice(previousImport.node.loc.end.line, currentImport.node.loc.start.line - 1);
|
||
|
|
||
|
return linesBetweenImports.filter(line => !line.trim().length).length;
|
||
|
};
|
||
|
let previousImport = imported[0];
|
||
|
|
||
|
imported.slice(1).forEach(function (currentImport) {
|
||
|
const emptyLinesBetween = getNumberOfEmptyLinesBetween(currentImport, previousImport);
|
||
|
|
||
|
if (newlinesBetweenImports === 'always' || newlinesBetweenImports === 'always-and-inside-groups') {
|
||
|
if (currentImport.rank !== previousImport.rank && emptyLinesBetween === 0) {
|
||
|
context.report(previousImport.node, 'There should be at least one empty line between import groups');
|
||
|
} else if (currentImport.rank === previousImport.rank && emptyLinesBetween > 0 && newlinesBetweenImports !== 'always-and-inside-groups') {
|
||
|
context.report(previousImport.node, 'There should be no empty line within import group');
|
||
|
}
|
||
|
} else {
|
||
|
if (emptyLinesBetween > 0) {
|
||
|
context.report(previousImport.node, 'There should be no empty line between import groups');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
previousImport = currentImport;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
meta: {
|
||
|
docs: {},
|
||
|
|
||
|
schema: [{
|
||
|
type: 'object',
|
||
|
properties: {
|
||
|
groups: {
|
||
|
type: 'array'
|
||
|
},
|
||
|
'newlines-between': {
|
||
|
enum: ['ignore', 'always', 'always-and-inside-groups', 'never']
|
||
|
}
|
||
|
},
|
||
|
additionalProperties: false
|
||
|
}]
|
||
|
},
|
||
|
|
||
|
create: function importOrderRule(context) {
|
||
|
const options = context.options[0] || {};
|
||
|
const newlinesBetweenImports = options['newlines-between'] || 'ignore';
|
||
|
let ranks;
|
||
|
|
||
|
try {
|
||
|
ranks = convertGroupsToRanks(options.groups || defaultGroups);
|
||
|
} catch (error) {
|
||
|
// Malformed configuration
|
||
|
return {
|
||
|
Program: function (node) {
|
||
|
context.report(node, error.message);
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
let imported = [];
|
||
|
let level = 0;
|
||
|
|
||
|
function incrementLevel() {
|
||
|
level++;
|
||
|
}
|
||
|
function decrementLevel() {
|
||
|
level--;
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
ImportDeclaration: function handleImports(node) {
|
||
|
if (node.specifiers.length) {
|
||
|
// Ignoring unassigned imports
|
||
|
const name = node.source.value;
|
||
|
registerNode(context, node, name, 'import', ranks, imported);
|
||
|
}
|
||
|
},
|
||
|
CallExpression: function handleRequires(node) {
|
||
|
if (level !== 0 || !(0, _staticRequire2.default)(node) || !isInVariableDeclarator(node.parent)) {
|
||
|
return;
|
||
|
}
|
||
|
const name = node.arguments[0].value;
|
||
|
registerNode(context, node, name, 'require', ranks, imported);
|
||
|
},
|
||
|
'Program:exit': function reportAndReset() {
|
||
|
makeOutOfOrderReport(context, imported);
|
||
|
|
||
|
if (newlinesBetweenImports !== 'ignore') {
|
||
|
makeNewlinesBetweenReport(context, imported, newlinesBetweenImports);
|
||
|
}
|
||
|
|
||
|
imported = [];
|
||
|
},
|
||
|
FunctionDeclaration: incrementLevel,
|
||
|
FunctionExpression: incrementLevel,
|
||
|
ArrowFunctionExpression: incrementLevel,
|
||
|
BlockStatement: incrementLevel,
|
||
|
ObjectExpression: incrementLevel,
|
||
|
'FunctionDeclaration:exit': decrementLevel,
|
||
|
'FunctionExpression:exit': decrementLevel,
|
||
|
'ArrowFunctionExpression:exit': decrementLevel,
|
||
|
'BlockStatement:exit': decrementLevel,
|
||
|
'ObjectExpression:exit': decrementLevel
|
||
|
};
|
||
|
}
|
||
|
};
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInJ1bGVzL29yZGVyLmpzIl0sIm5hbWVzIjpbImRlZmF1bHRHcm91cHMiLCJyZXZlcnNlIiwiYXJyYXkiLCJtYXAiLCJ2Iiwib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