From 0d7a69a6ab0549c0c6b05f904b3325d96f823ca3 Mon Sep 17 00:00:00 2001 From: edvardchen Date: Sun, 2 Aug 2020 15:53:52 +0800 Subject: [PATCH] feat: ignore UPPER_CASE arrays closes #12 --- lib/rules/no-literal-string.js | 6 ++++++ tests/lib/rules/no-literal-string.js | 1 + 2 files changed, 7 insertions(+) diff --git a/lib/rules/no-literal-string.js b/lib/rules/no-literal-string.js index 3f1de43..f5892c7 100644 --- a/lib/rules/no-literal-string.js +++ b/lib/rules/no-literal-string.js @@ -347,6 +347,12 @@ module.exports = { if (isUpperCase(node.parent.id.name)) visited.add(node); }, + 'VariableDeclarator > ArrayExpression > Literal'(node) { + // allow statements like const A_B = ["test"] + const declarator = getNearestAncestor(node, 'VariableDeclarator'); + if (isUpperCase(declarator.id.name)) visited.add(node); + }, + 'Property > Literal'(node) { const { parent } = node; // if node is key of property, skip diff --git a/tests/lib/rules/no-literal-string.js b/tests/lib/rules/no-literal-string.js index 144357c..9288526 100644 --- a/tests/lib/rules/no-literal-string.js +++ b/tests/lib/rules/no-literal-string.js @@ -73,6 +73,7 @@ ruleTester.run('no-literal-string', rule, { }, { code: 'const a = "FOO";' }, { code: 'var A_B = "world";' }, + { code: 'var A_B = ["world"];' }, { code: 'var a = {["A_B"]: "hello world"};' }, { code: 'var a = {[A_B]: "hello world"};' }, { code: 'var a = {A_B: "hello world"};' },