From 01b2545eca644b71f999aa918c4c71f4ae195026 Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Sun, 2 Jul 2017 12:30:38 +0200 Subject: [PATCH] no-switch-case-fall-though: accespt different comment formats [enhancement] `no-switch-case-fall-through` matches `// falls through` comments case insensitive and allows trailing text --- src/rules/noSwitchCaseFallThroughRule.ts | 7 ++----- .../no-switch-case-fall-through/test.ts.lint | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/rules/noSwitchCaseFallThroughRule.ts b/src/rules/noSwitchCaseFallThroughRule.ts index c6a35c8fac7..e05057267e6 100644 --- a/src/rules/noSwitchCaseFallThroughRule.ts +++ b/src/rules/noSwitchCaseFallThroughRule.ts @@ -93,10 +93,7 @@ export class NoSwitchCaseFallThroughWalker extends Lint.AbstractWalker { 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); -} diff --git a/test/rules/no-switch-case-fall-through/test.ts.lint b/test/rules/no-switch-case-fall-through/test.ts.lint index 9a37931a68e..1b0a1511e2a 100644 --- a/test/rules/no-switch-case-fall-through/test.ts.lint +++ b/test/rules/no-switch-case-fall-through/test.ts.lint @@ -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: -} \ No newline at end of file +} + +switch (foo) { + case 1: + foo; + // Falls through + case 2: + foo; + // Falls through. + case 3: + foo; + // Falls through -- for reasons + default: +}