diff options
author | Rich Trott <rtrott@gmail.com> | 2016-04-15 21:22:12 -0700 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2016-04-18 17:19:16 -0700 |
commit | ca698330ac284bdb8b236edcb4f07daa108b858c (patch) | |
tree | 2cd71baa5acad41688c29525d55db301799f1e7a | |
parent | 31600735f40e02935b936802ff42d66d49e9769b (diff) | |
download | android-node-v8-ca698330ac284bdb8b236edcb4f07daa108b858c.tar.gz android-node-v8-ca698330ac284bdb8b236edcb4f07daa108b858c.tar.bz2 android-node-v8-ca698330ac284bdb8b236edcb4f07daa108b858c.zip |
tools: lint for alignment of variable assignments
Enforce alignment/indentation on variable assignments that span multiple
lines.
PR-URL: https://github.com/nodejs/node/pull/6242
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Johan Bergström <bugs@bergstroem.nu>
Reviewed-By: James M Snell <jasnell@gmail.com>
-rw-r--r-- | .eslintrc | 4 | ||||
-rw-r--r-- | tools/eslint-rules/align-multiline-assignment.js | 68 |
2 files changed, 70 insertions, 2 deletions
@@ -13,7 +13,7 @@ rules: no-duplicate-case: 2 no-empty-character-class: 2 no-ex-assign: 2 - no-extra-boolean-cast : 2 + no-extra-boolean-cast: 2 no-extra-parens: [2, "functions"] no-extra-semi: 2 no-func-assign: 2 @@ -86,7 +86,7 @@ rules: # Custom rules in tools/eslint-rules new-with-error: [2, "Error", "RangeError", "TypeError", "SyntaxError", "ReferenceError"] - + align-multiline-assignment: 2 # Global scoped method and vars globals: diff --git a/tools/eslint-rules/align-multiline-assignment.js b/tools/eslint-rules/align-multiline-assignment.js new file mode 100644 index 0000000000..69f1f33d4d --- /dev/null +++ b/tools/eslint-rules/align-multiline-assignment.js @@ -0,0 +1,68 @@ +/** + * @fileoverview Align multiline variable assignments + * @author Rich Trott + */ +'use strict'; + +//------------------------------------------------------------------------------ +// Rule Definition +//------------------------------------------------------------------------------ +function getBinaryExpressionStarts(binaryExpression, starts = []) { + function getStartsFromOneSide(side, starts) { + starts.push(side.loc.start); + if (side.type === 'BinaryExpression') { + starts = getBinaryExpressionStarts(side, starts); + } + return starts; + } + + starts = getStartsFromOneSide(binaryExpression.left, starts); + starts = getStartsFromOneSide(binaryExpression.right, starts); + return starts; +} + +function checkExpressionAlignment(expression) { + if (!expression) + return; + + var msg = ''; + + switch (expression.type) { + case 'BinaryExpression': + var starts = getBinaryExpressionStarts(expression); + var startLine = starts[0].line; + const startColumn = starts[0].column; + starts.forEach((loc) => { + if (loc.line > startLine) { + startLine = loc.line; + if (loc.column !== startColumn) { + msg = 'Misaligned multiline assignment'; + } + } + }); + break; + } + return msg; +} + +function testAssignment(context, node) { + const msg = checkExpressionAlignment(node.right); + if (msg) + context.report(node, msg); +} + +function testDeclaration(context, node) { + node.declarations.forEach((declaration) => { + const msg = checkExpressionAlignment(declaration.init); + // const start = declaration.init.loc.start; + if (msg) + context.report(node, msg); + }); +} + +module.exports = function(context) { + return { + 'AssignmentExpression': (node) => testAssignment(context, node), + 'VariableDeclaration': (node) => testDeclaration(context, node) + }; +}; |