From 08a653b99f8dd7aad187507d23761948793c61e6 Mon Sep 17 00:00:00 2001 From: Edvard Chen Date: Sat, 13 Jul 2024 16:24:09 +0800 Subject: [PATCH] feat: allow to ignore tag template via callees closes #122 --- lib/rules/no-literal-string.js | 7 ++++++ .../should-validate-template.js | 25 +++++++++++-------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/rules/no-literal-string.js b/lib/rules/no-literal-string.js index 3891ab7..057dd24 100644 --- a/lib/rules/no-literal-string.js +++ b/lib/rules/no-literal-string.js @@ -249,6 +249,13 @@ module.exports = { }, 'CallExpression:exit': endIndicator, + TaggedTemplateExpression(node) { + indicatorStack.push( + isValidFunctionCall(context, options, { callee: node.tag }) + ); + }, + 'TaggedTemplateExpression:exit': endIndicator, + 'SwitchCase > Literal'(node) { indicatorStack.push(true); }, diff --git a/tests/lib/rules/no-literal-string/should-validate-template.js b/tests/lib/rules/no-literal-string/should-validate-template.js index 3a4a8a7..ef92d15 100644 --- a/tests/lib/rules/no-literal-string/should-validate-template.js +++ b/tests/lib/rules/no-literal-string/should-validate-template.js @@ -1,7 +1,5 @@ -const testFile = require('../../helpers/testFile'); const runTest = require('../../helpers/runTest'); -const options = [{ mode: 'all', 'should-validate-template': true }]; const cases = { valid: [ { @@ -9,29 +7,34 @@ const cases = { var a = \`12345\` var a = \`\` `, - options, + options: [{ mode: 'all', 'should-validate-template': true }], }, { code: 'var a = `hello ${abc} world`', + options: [{ mode: 'jsx-only', 'should-validate-template': true }], + }, + + { + code: 'const StyledParagaph = styled.p`some css here`', options: [ { - mode: 'jsx-only', + mode: 'all', + callees: { exclude: ['styled.*'] }, 'should-validate-template': true, }, ], }, ], invalid: [ - { code: 'var a = `hello ${abc} world`', options, errors: 1 }, + { + code: 'var a = `hello ${abc} world`', + options: [{ mode: 'all', 'should-validate-template': true }], + errors: 1, + }, { code: 'var a = "hello world"; <>`abcd`', - options: [ - { - mode: 'jsx-only', - 'should-validate-template': true, - }, - ], + options: [{ mode: 'jsx-only', 'should-validate-template': true }], errors: 1, }, ],