Skip to content

Commit

Permalink
no-switch-case-fall-though: accespt different comment formats (palant…
Browse files Browse the repository at this point in the history
…ir#2983)

[enhancement] `no-switch-case-fall-through` matches `// falls through` comments case insensitive and allows trailing text
  • Loading branch information
ajafff authored and HyphnKnight committed Apr 9, 2018
1 parent b8a6c6d commit 1251199
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
7 changes: 2 additions & 5 deletions src/rules/noSwitchCaseFallThroughRule.ts
Expand Up @@ -93,10 +93,7 @@ export class NoSwitchCaseFallThroughWalker extends Lint.AbstractWalker<void> {

private isFallThroughAllowed(clause: ts.CaseOrDefaultClause): boolean {
const comments = ts.getLeadingCommentRanges(this.sourceFile.text, clause.end);
return comments !== undefined && comments.some((comment) => commentText(comment, this.sourceFile).trim() === "falls through");
return comments !== undefined &&
comments.some((comment) => /^\s*falls through\b/i.test(this.sourceFile.text.slice(comment.pos + 2, comment.end)));
}
}

function commentText({ pos, end, kind }: ts.CommentRange, sourceFile: ts.SourceFile): string {
return sourceFile.text.slice(pos + 2, kind === ts.SyntaxKind.MultiLineCommentTrivia ? end - 2 : end);
}
17 changes: 15 additions & 2 deletions test/rules/no-switch-case-fall-through/test.ts.lint
Expand Up @@ -125,7 +125,20 @@ switch (foo) {
foobar(); // this case clause falls through and returns in the default clause
default:
~~~~~~~ [expected a 'break' before 'default']
return;
return;
}
case 2:
}
}

switch (foo) {
case 1:
foo;
// Falls through
case 2:
foo;
// Falls through.
case 3:
foo;
// Falls through -- for reasons
default:
}

0 comments on commit 1251199

Please sign in to comment.