Browse Source

errors,tools: alphabetize-errors lint rule

To make sure errors in lib/internal/errors.js (are defined via `E`)
will stay in alphabetical order going forward.

PR-URL: https://github.com/nodejs/node/pull/15083
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
canary-base
Jon Moss 7 years ago
committed by Refael Ackermann
parent
commit
b12d77977e
No known key found for this signature in database GPG Key ID: CD704BD80FDDDB64
  1. 2
      lib/internal/errors.js
  2. 36
      tools/eslint-rules/alphabetize-errors.js

2
lib/internal/errors.js

@ -1,3 +1,5 @@
/* eslint-enable alphabetize-errors */
'use strict';
// The whole point behind this internal module is to allow Node.js to no

36
tools/eslint-rules/alphabetize-errors.js

@ -0,0 +1,36 @@
'use strict';
const message = 'Errors in lib/internal/errors.js must be alphabetized';
function errorForNode(node) {
return node.expression.arguments[0].value;
}
function isAlphabetized(previousNode, node) {
return errorForNode(previousNode).localeCompare(errorForNode(node)) < 0;
}
function isDefiningError(node) {
return node.expression &&
node.expression.type === 'CallExpression' &&
node.expression.callee &&
node.expression.callee.name === 'E';
}
module.exports = {
create: function(context) {
var previousNode;
return {
ExpressionStatement: function(node) {
if (isDefiningError(node)) {
if (previousNode && !isAlphabetized(previousNode, node)) {
context.report({ node: node, message: message });
}
previousNode = node;
}
}
};
}
};
Loading…
Cancel
Save