From 3cb1691463bf254efbe45766b647e276f130e25a Mon Sep 17 00:00:00 2001 From: Mitchell Wills Date: Wed, 18 Jul 2018 14:57:57 -0700 Subject: [PATCH] Reduce false positive rate in no-invalid-template-strings (#3769) Don't flag unmatched ${ in a regular string Also flag the entire expression, not just the opening brace --- src/rules/noInvalidTemplateStringsRule.ts | 7 ++--- .../no-invalid-template-strings/test.ts.lint | 28 ++++++++++++------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/rules/noInvalidTemplateStringsRule.ts b/src/rules/noInvalidTemplateStringsRule.ts index ec7512b698a..0409e4a5c59 100644 --- a/src/rules/noInvalidTemplateStringsRule.ts +++ b/src/rules/noInvalidTemplateStringsRule.ts @@ -51,15 +51,14 @@ function walk(ctx: Lint.WalkContext) { function check(node: ts.StringLiteral): void { const text = node.getText(ctx.sourceFile); - const findTemplateStrings = /\\*\$\{/g; + const findTemplateStrings = /(\\*)(\$\{.+?\})/g; let instance = findTemplateStrings.exec(text); while (instance !== null) { - const matchLength = instance[0].length; - const backslashCount = matchLength - 2; + const backslashCount = instance[1].length; const instanceIsEscaped = backslashCount % 2 === 1; if (!instanceIsEscaped) { const start = node.getStart() + (instance.index + backslashCount); - ctx.addFailureAt(start, 2, Rule.FAILURE_STRING); + ctx.addFailureAt(start, instance[2].length, Rule.FAILURE_STRING); } instance = findTemplateStrings.exec(text); } diff --git a/test/rules/no-invalid-template-strings/test.ts.lint b/test/rules/no-invalid-template-strings/test.ts.lint index 057e99fc454..ac4fd54f7d2 100644 --- a/test/rules/no-invalid-template-strings/test.ts.lint +++ b/test/rules/no-invalid-template-strings/test.ts.lint @@ -3,31 +3,39 @@ new (Tab.mixins.Controlled('MemberComments',Tab.mixins.Templated('
',Tab.mixins.AlwaysHasData('\${c}')))) Tab.mixins.Templated('
',Tab.mixins.AlwaysHasData('\\${d}')) - ~~ [0] + ~~~~ [0] Tab.mixins.Templated(`
`,Tab.mixins.AlwaysHasData('${f}')) - ~~ [0] + ~~~~ [0] Tab.mixins.Templated('
',Tab.mixins.AlwaysHasData('${h}'), Tab.mixin.SomeMethod('${i}')) - ~~ [0] - ~~ [0] - ~~ [0] + ~~~~ [0] + ~~~~ [0] + ~~~~ [0] new (Tab.mixins.Controlled('MemberComments',Tab.mixins.Templated('
',Tab.mixins.AlwaysHasData()))) - ~~ [0] + ~~~~ [0] new (Tab.mixins.Templated('
',Tab.mixins.AlwaysHasData('\\\\${c}')))) - ~~ [0] + ~~~~ [0] "\${a} = ${a}"; - ~~ [0] + ~~~~ [0] "One plus one is ${1 + 1}."; - ~~ [0] + ~~~~~~~~ [0] 'One plus one is ${1 + 1}.'; - ~~ [0] + ~~~~~~~~ [0] `One plus one is ${1 + 1}.`; +"${"; + +"${" + '}'; + +"${() => {}} ${abc}" + ~~~~~~~~~~ [0] + ~~~~~~ [0] + [0]: Interpolation will only work for template strings.