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

no-switch-case-fall-though: accept different comment formats #2983

Merged
merged 1 commit into from
Jul 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/rules/noSwitchCaseFallThroughRule.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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:
}