Skip to content
This repository has been archived by the owner on Jun 11, 2018. It is now read-only.

Commit

Permalink
ensure assignment-in-return statements are handled as well
Browse files Browse the repository at this point in the history
resolves #25
  • Loading branch information
Ma27 committed Jan 12, 2017
1 parent 5e3f092 commit c1cd594
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/util/findNextPunctuators.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ function gatherAssignmentsFromNodes(statements, source) {
});

return list.concat(punctuators);
case 'ReturnStatement':
if (stmt.argument && stmt.argument.type === 'AssignmentExpression') {
list.push(source.getTokensBetween(stmt.argument.left, stmt.argument.right).shift());
}
return list;
case 'ExportNamedDeclaration':
return list;
default:
return list;
}
Expand Down Expand Up @@ -73,7 +80,7 @@ function checkDeclaration(declaration, punctuators) {


function checkToken(stmt, previous, start) {
return -1 !== ['VariableDeclaration', 'AssignmentExpression', 'ExpressionStatement'].indexOf(stmt.type)
return -1 !== ['VariableDeclaration', 'AssignmentExpression', 'ExpressionStatement', 'ReturnStatement', 'ExportNamedDeclaration'].indexOf(stmt.type)
&& (typeof previous === 'undefined' || (start - previous.loc.end.line) === 1);
}

Expand Down
57 changes: 57 additions & 0 deletions test/lib/rules/var-spacing.rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,23 @@ ruleTester.run('var-spacing', rule, {
' test79 = 12;',
'}'
].join('\n')
}, {
code: [
'var a = "b";',
'function foo() {',
' var b = "c";',
' return a = b;',
'}'
].join('\n')
}, {
code: [
'var a = "b";',
'function foo() {',
' var b = "c",',
' d = "f";',
' return a = b;',
'}'
].join('\n')
}],
invalid: [{
code: [
Expand Down Expand Up @@ -1687,5 +1704,45 @@ ruleTester.run('var-spacing', rule, {
errors: [{
message: 'Invalid indent!'
}]
}, {
code: [
'var c;',
'function foo() {',
' var a = "b";',
' return c = a;',
'}'
].join('\n'),
errors: [{
message: 'Invalid indent!'
}],
output: [
'var c;',
'function foo() {',
' var a = "b";',
' return c = a;',
'}'
].join('\n')
}, {
code: [
'var a = "b";',
'function foo() {',
' var b = "c",',
' d = "f";',
' return a = b;',
'}'
].join('\n'),
errors: [{
message: 'Invalid indent!'
}, {
message: 'Invalid indent!'
}],
output: [
'var a = "b";',
'function foo() {',
' var b = "c",',
' d = "f";',
' return a = b;',
'}'
].join('\n')
}]
});

0 comments on commit c1cd594

Please sign in to comment.