no-invalid-template-strings: fix for escaped template expressions #3116
no-invalid-template-strings: fix for escaped template expressions #3116
Conversation
|
@@ -62,10 +62,12 @@ export class Rule extends Lint.Rules.OptionallyTypedRule { | |||
public static FAILURE_STRING_ALPHABETICAL(name: string): string { | |||
return `The key '${name}' is not sorted alphabetically`; | |||
} | |||
/* tslint:disable:no-shadowed-variable */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry, master was broken, please revert these lines and merge master with #3118
nevermind, I did the merge |
* Support for ignoring case: '\${template-expression}' | ||
*/ | ||
const unescapedText: string = node.getFullText(); | ||
const preceedingCharacter = unescapedText.substr(unescapedText.search(/\$\{/) - 1, 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this regex should be saved as a constant at the top of this function
/** | ||
* Support for ignoring case: '\${template-expression}' | ||
*/ | ||
const unescapedText: string = node.getFullText(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the : string
typedef necessary?
*/ | ||
const unescapedText: string = node.getFullText(); | ||
const preceedingCharacter = unescapedText.substr(unescapedText.search(/\$\{/) - 1, 1); | ||
if (preceedingCharacter === "\\") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused, how does this work for L1 and L3 of the test.ts.lint
file you edited?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the characters preceeding the lines' ${} expressions are backslashes. "\" is an escaped backslash. Could be made clearer somehow probably.
…ed to const at top of method
Build error is from 8000ms timeout |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like I'm a bit too late with my review
/** | ||
* Support for ignoring case: '\${template-expression}' | ||
*/ | ||
const unescapedText = node.getFullText(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getFullText()
returns the text including leading comments and whitespace. Use getText(sourceFile)
instead.
/** | ||
* Finds instances of '${' | ||
*/ | ||
const findTemplateString = new RegExp(/\$\{/); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why use new RegExp
instead of an regex literal?
/** | ||
* Finds instances of '${' | ||
*/ | ||
const findTemplateString = new RegExp(/\$\{/); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also needs the g
flag
const findTemplateString = new RegExp(/\$\{/); | ||
|
||
const index = node.text.search(findTemplateString); | ||
if (index !== -1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this needs to be a loop. otherwise you only check the first occurence and don't look further
this one should result in an error: ``"${foo}bar${baz}"
*/ | ||
const unescapedText = node.getFullText(); | ||
const preceedingCharacter = unescapedText.substr(unescapedText.search(findTemplateString) - 1, 1); | ||
if (isBackslash(preceedingCharacter)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you also need to check if the backslash is escaped with a backslash.
this one should result in an error: "\\${foo}"
ah, sorry about that. I filed #3128 to track that your comments get addressed before the next release |
PR checklist
Changelog entry
[bugfix]
no-invalid-template-strings
allows backslash-prefixed template expressions