Skip to content

Commit

Permalink
no-invalid-template-strings logic errors (palantir#3131)
Browse files Browse the repository at this point in the history
  • Loading branch information
aervin_ authored and HyphnKnight committed Apr 9, 2018
1 parent 0354df1 commit 68c167d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
32 changes: 11 additions & 21 deletions src/rules/noInvalidTemplateStringsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,18 @@ function walk(ctx: Lint.WalkContext<void>) {
});

function check(node: ts.StringLiteral): void {
/**
* Finds instances of '${'
*/
const findTemplateString = new RegExp(/\$\{/);

const index = node.text.search(findTemplateString);
if (index !== -1) {
/**
* Support for ignoring case: '\${template-expression}'
*/
const unescapedText = node.getFullText();
const preceedingCharacter = unescapedText.substr(unescapedText.search(findTemplateString) - 1, 1);
if (isBackslash(preceedingCharacter)) {
return;
const text = node.getText(ctx.sourceFile);
const findTemplateStrings = /\\*\$\{/g;
let instance = findTemplateStrings.exec(text);
while (instance !== null) {
const matchLength = instance[0].length;
const backslashCount = matchLength - 2;
const instanceIsEscaped = backslashCount % 2 === 1;
if (!instanceIsEscaped) {
const start = node.getStart() + (instance.index + backslashCount);
ctx.addFailureAt(start, 2, Rule.FAILURE_STRING);
}

const textStart = node.getStart() + 1;
ctx.addFailureAt(textStart + index, 2, Rule.FAILURE_STRING);
instance = findTemplateStrings.exec(text);
}
}
}

function isBackslash(character: string): boolean {
return character === "\\";
}
24 changes: 21 additions & 3 deletions test/rules/no-invalid-template-strings/test.ts.lint
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
new (Tab.mixins.Controlled('MemberComments',Tab.mixins.Templated('<div mbr-comment-tab=\${which}></div>',Tab.mixins.AlwaysHasData())))
new (Tab.mixins.Controlled('MemberComments',Tab.mixins.Templated('<div mbr-comment-tab=\${a}></div>',Tab.mixins.AlwaysHasData())))

new (Tab.mixins.Controlled('MemberComments',Tab.mixins.Templated('<div mbr-comment-tab=\${which}></div>',Tab.mixins.AlwaysHasData('\${which}'))))
new (Tab.mixins.Controlled('MemberComments',Tab.mixins.Templated('<div mbr-comment-tab=\${b}></div>',Tab.mixins.AlwaysHasData('\${c}'))))

new (Tab.mixins.Controlled('MemberComments',Tab.mixins.Templated('<div mbr-comment-tab=\\\\\\${which}></div>',Tab.mixins.AlwaysHasData())))
Tab.mixins.Templated('<div mbr-comment-tab=\${which}></div>',Tab.mixins.AlwaysHasData('\\${d}'))
~~ [0]

Tab.mixins.Templated(`<div mbr-comment-tab=\${e}></div>`,Tab.mixins.AlwaysHasData('${f}'))
~~ [0]

Tab.mixins.Templated('<div mbr-comment-tab=${g}></div>',Tab.mixins.AlwaysHasData('${h}'), Tab.mixin.SomeMethod('${i}'))
~~ [0]
~~ [0]
~~ [0]

new (Tab.mixins.Controlled('MemberComments',Tab.mixins.Templated('<div mbr-comment-tab=\\${j}></div>',Tab.mixins.AlwaysHasData())))
~~ [0]

new (Tab.mixins.Templated('<div mbr-comment-tab=\\\\\${b}></div>',Tab.mixins.AlwaysHasData('\\\\${c}'))))
~~ [0]

"\${a} = ${a}";
~~ [0]

"One plus one is ${1 + 1}.";
~~ [0]
Expand Down

0 comments on commit 68c167d

Please sign in to comment.