Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
Don't error for misplaced braces for 'else' if there are no braces (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-hanson authored and nchen63 committed Dec 17, 2016
1 parent 555549f commit 2a9f3d7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/rules/oneLineRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ class OneLineWalker extends Lint.RuleWalker {
public visitIfStatement(node: ts.IfStatement) {
const sourceFile = node.getSourceFile();
const thenStatement = node.thenStatement;
if (thenStatement.kind === ts.SyntaxKind.Block) {
const thenIsBlock = thenStatement.kind === ts.SyntaxKind.Block;
if (thenIsBlock) {
const expressionCloseParen = node.getChildAt(3);
const thenOpeningBrace = thenStatement.getChildAt(0);
this.handleOpeningBrace(expressionCloseParen, thenOpeningBrace);
Expand All @@ -83,7 +84,7 @@ class OneLineWalker extends Lint.RuleWalker {
const elseOpeningBrace = elseStatement.getChildAt(0);
this.handleOpeningBrace(elseKeyword, elseOpeningBrace);
}
if (this.hasOption(OPTION_ELSE)) {
if (thenIsBlock && this.hasOption(OPTION_ELSE)) {
const thenStatementEndLine = sourceFile.getLineAndCharacterOfPosition(thenStatement.getEnd()).line;
const elseKeywordLine = sourceFile.getLineAndCharacterOfPosition(elseKeyword.getStart()).line;
if (thenStatementEndLine !== elseKeywordLine) {
Expand Down
6 changes: 6 additions & 0 deletions test/rules/one-line/all/test.js.lint
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,9 @@ let geoConfig
timeout: 5000,
enableHighAccuracy: false
};

// No error if there are no braces
if (true)
true;
else
false;
6 changes: 6 additions & 0 deletions test/rules/one-line/all/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,9 @@ let geoConfig: {

declare module "*";
declare module "someLibrary/*";

// No error if there are no braces
if (true)
true;
else
false;
8 changes: 7 additions & 1 deletion test/rules/one-line/none/test.js.lint
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ while(x < 10){
x++;
}

class BarBooBaz
class BarBooBaz
{

}
Expand All @@ -90,3 +90,9 @@ let geoConfig
timeout: 5000,
enableHighAccuracy: false
};

// No error if there are no braces
if (true)
true;
else
false;
8 changes: 7 additions & 1 deletion test/rules/one-line/none/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function f():
return 5;
}

class BarBooBaz
class BarBooBaz
{

}
Expand Down Expand Up @@ -122,3 +122,9 @@ let geoConfig: {

declare module "*";
declare module "someLibrary/*";

// No error if there are no braces
if (true)
true;
else
false;

0 comments on commit 2a9f3d7

Please sign in to comment.