From 502d6c87e251d2a0659934dc407d83c592c94670 Mon Sep 17 00:00:00 2001 From: jpradelle Date: Tue, 23 Jul 2024 18:16:54 +0200 Subject: [PATCH] fix (quoted-expression): false positive in quoted-expressions (#209) With 43081j patch --- src/rules/quoted-expressions.ts | 13 +++++---- src/test/rules/quoted-expressions_test.ts | 32 +++++++++++++++++++++++ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/rules/quoted-expressions.ts b/src/rules/quoted-expressions.ts index c0f365c..5057845 100644 --- a/src/rules/quoted-expressions.ts +++ b/src/rules/quoted-expressions.ts @@ -37,6 +37,7 @@ const rule: Rule.RuleModule = { create(context): Rule.RuleListener { // variables should be defined here const alwaysQuote = context.options[0] === 'always'; + const quotePattern = /=(["'])?$/; //---------------------------------------------------------------------- // Helpers @@ -57,20 +58,18 @@ const rule: Rule.RuleModule = { const expression = node.quasi.expressions[i]; const previousQuasi = node.quasi.quasis[i]; const nextQuasi = node.quasi.quasis[i + 1]; - const isAttribute = /=["']?$/.test(previousQuasi.value.raw); + const quoteMatch = previousQuasi.value.raw.match(quotePattern); // don't care about non-attribute bindings - if (!isAttribute) { + if (!quoteMatch) { continue; } + const hasStartQuote = quoteMatch[1] !== undefined; const isQuoted = - (previousQuasi.value.raw.endsWith('="') && - nextQuasi.value.raw.startsWith('"')) || - (previousQuasi.value.raw.endsWith("='") && - nextQuasi.value.raw.startsWith("'")); + hasStartQuote && nextQuasi.value.raw.startsWith(quoteMatch[1]); - if (alwaysQuote && !isQuoted) { + if (alwaysQuote && !hasStartQuote) { context.report({ node: expression, messageId: 'alwaysQuote', diff --git a/src/test/rules/quoted-expressions_test.ts b/src/test/rules/quoted-expressions_test.ts index ddb3ff2..aec2774 100644 --- a/src/test/rules/quoted-expressions_test.ts +++ b/src/test/rules/quoted-expressions_test.ts @@ -43,6 +43,38 @@ ruleTester.run('quoted-expressions', rule, { { code: "html``", options: ['always'] + }, + { + code: 'html`${expr}`', + options: ['always'] + }, + { + code: 'html`${expr}`', + options: ['always'] + }, + { + code: 'html`${expr}`', + options: ['always'] + }, + { + code: 'html`${expr}`', + options: ['always'] + }, + { + code: 'html`${expr}`', + options: ['always'] + }, + { + code: 'html`${expr}`', + options: ['always'] + }, + { + code: 'html`

${expr}

`', + options: ['never'] + }, + { + code: 'html`

${expr}

`', + options: ['never'] } ],