From d270343f96bb2a7fb93decbab31c10f74d73f525 Mon Sep 17 00:00:00 2001 From: edvardchen <> Date: Fri, 26 Jul 2019 10:26:25 +0800 Subject: [PATCH] feat: skip literal in SwitchCase statement don't affect litreals in its block fix #2 --- lib/rules/no-literal-string.js | 4 ++++ tests/lib/rules/no-literal-string.js | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/lib/rules/no-literal-string.js b/lib/rules/no-literal-string.js index 60ec8f1..ee08c14 100644 --- a/lib/rules/no-literal-string.js +++ b/lib/rules/no-literal-string.js @@ -197,6 +197,10 @@ module.exports = { if (isValidFunctionCall(parent)) visited.add(node); }, + 'SwitchCase > Literal'(node) { + visited.add(node); + }, + 'Literal:exit'(node) { // visited and passed linting if (visited.has(node)) return; diff --git a/tests/lib/rules/no-literal-string.js b/tests/lib/rules/no-literal-string.js index 42c92fa..51cffbc 100644 --- a/tests/lib/rules/no-literal-string.js +++ b/tests/lib/rules/no-literal-string.js @@ -31,6 +31,7 @@ ruleTester.run('no-literal-string', rule, { valid: [ { code: 'import("hello")' }, { code: "name === 'Android' || name === 'iOS'" }, + { code: "switch(a){ case 'a': break; default: break;}" }, { code: 'import name from "hello";' }, { code: 'a.indexOf("ios")' }, { code: 'a.includes("ios")' }, @@ -61,6 +62,10 @@ ruleTester.run('no-literal-string', rule, { invalid: [ { code: 'a + "b"', errors }, + { + code: "switch(a){ case 'a': var a ='b'; break; default: break;}", + errors + }, { code: 'export const a = "hello_string";', errors }, { code: 'const a = "foo";', errors }, { code: 'const a = call("Ffo");', errors },