diff options
author | Jon Moss <me@jonathanmoss.me> | 2017-08-26 04:15:43 +0300 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2017-09-25 21:36:45 +0300 |
commit | 2e0313865bc72fc0ace64f887448613a613ef752 (patch) | |
tree | e39971cc4c4784660ed1e958f0ce4fd288d5e1cf /tools | |
parent | 6c221b83e33a22b142ebb2ead67e81fd23d6c177 (diff) |
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>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/eslint-rules/alphabetize-errors.js | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/tools/eslint-rules/alphabetize-errors.js b/tools/eslint-rules/alphabetize-errors.js new file mode 100644 index 00000000000..f4651de9a75 --- /dev/null +++ b/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; + } + } + }; + } +}; |