Skip to content
This repository was archived by the owner on Mar 25, 2021. It is now read-only.

Commit 0461fe0

Browse files
aervin_adidahiya
authored andcommitted
no-invalid-template-strings: fix for escaped template expressions (#3116)
1 parent 3ce020a commit 0461fe0

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/rules/noInvalidTemplateStringsRule.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,28 @@ function walk(ctx: Lint.WalkContext<void>) {
5050
});
5151

5252
function check(node: ts.StringLiteral): void {
53-
const idx = node.text.search(/\$\{/);
54-
if (idx !== -1) {
53+
/**
54+
* Finds instances of '${'
55+
*/
56+
const findTemplateString = new RegExp(/\$\{/);
57+
58+
const index = node.text.search(findTemplateString);
59+
if (index !== -1) {
60+
/**
61+
* Support for ignoring case: '\${template-expression}'
62+
*/
63+
const unescapedText = node.getFullText();
64+
const preceedingCharacter = unescapedText.substr(unescapedText.search(findTemplateString) - 1, 1);
65+
if (isBackslash(preceedingCharacter)) {
66+
return;
67+
}
68+
5569
const textStart = node.getStart() + 1;
56-
ctx.addFailureAt(textStart + idx, 2, Rule.FAILURE_STRING);
70+
ctx.addFailureAt(textStart + index, 2, Rule.FAILURE_STRING);
5771
}
5872
}
5973
}
74+
75+
function isBackslash(character: string): boolean {
76+
return character === "\\";
77+
}

test/rules/no-invalid-template-strings/test.ts.lint

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
new (Tab.mixins.Controlled('MemberComments',Tab.mixins.Templated('<div mbr-comment-tab=\${which}></div>',Tab.mixins.AlwaysHasData())))
2+
3+
new (Tab.mixins.Controlled('MemberComments',Tab.mixins.Templated('<div mbr-comment-tab=\${which}></div>',Tab.mixins.AlwaysHasData('\${which}'))))
4+
5+
new (Tab.mixins.Controlled('MemberComments',Tab.mixins.Templated('<div mbr-comment-tab=\\\\\\${which}></div>',Tab.mixins.AlwaysHasData())))
6+
17
"One plus one is ${1 + 1}.";
28
~~ [0]
39

0 commit comments

Comments
 (0)