0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-27 22:16:50 +01:00
nodejs/tools/eslint-rules/alphabetize-primordials.js
Antoine du Hamel a596af0819
tools: add lint rule to keep primordials in ASCII order
PR-URL: https://github.com/nodejs/node/pull/52592
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
2024-04-21 16:53:08 +00:00

50 lines
1.5 KiB
JavaScript

'use strict';
const prefix = 'Out of ASCIIbetical order - ';
const opStr = ' >= ';
module.exports = {
meta: {
schema: [{
type: 'object',
properties: {
enforceTopPosition: { type: 'boolean' },
},
additionalProperties: false,
}],
},
create: function(context) {
const enforceTopPosition = context.options.every((option) => option.enforceTopPosition !== false);
return {
'VariableDeclaration:not(Program>:first-child+*) > VariableDeclarator[init.name="primordials"]'(node) {
if (enforceTopPosition) {
context.report({
node,
message: 'destructuring from primordials should be the first expression after "use strict"',
});
}
},
'VariableDeclaration > VariableDeclarator[init.name="primordials"]'({ id: node }) {
const { loc } = node;
if (loc.start.line === loc.end.line) {
context.report({
node,
message: 'destructuring from primordials should be multiline',
});
}
},
'VariableDeclaration > VariableDeclarator[init.name="primordials"] Property:not(:first-child)'(node) {
const { properties } = node.parent;
const prev = properties[properties.indexOf(node) - 1].key.name;
const curr = node.key.name;
if (prev >= curr) {
const message = [prefix, prev, opStr, curr].join('');
context.report({ node, message });
}
},
};
},
};