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

no-invalid-template-strings: fix for escaped template expressions #3116

Merged
merged 6 commits into from Aug 14, 2017

Conversation

aervin
Copy link
Contributor

@aervin aervin commented Aug 10, 2017

PR checklist

  • Addresses an existing issue: #2738
  • bugfix
    • Includes tests
  • Documentation update

Changelog entry

[bugfix] no-invalid-template-strings allows backslash-prefixed template expressions

@aervin
Copy link
Contributor Author

aervin commented Aug 10, 2017

npm run lint was giving a lint error for no-shadowed-var?

@@ -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 */
Copy link
Contributor

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

@adidahiya
Copy link
Contributor

nevermind, I did the merge

* Support for ignoring case: '\${template-expression}'
*/
const unescapedText: string = node.getFullText();
const preceedingCharacter = unescapedText.substr(unescapedText.search(/\$\{/) - 1, 1);
Copy link
Contributor

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();
Copy link
Contributor

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 === "\\") {
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@aervin
Copy link
Contributor Author

aervin commented Aug 12, 2017

Build error is from 8000ms timeout

@adidahiya adidahiya merged commit 0461fe0 into palantir:master Aug 14, 2017
Copy link
Contributor

@ajafff ajafff left a 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();
Copy link
Contributor

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(/\$\{/);
Copy link
Contributor

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(/\$\{/);
Copy link
Contributor

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) {
Copy link
Contributor

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)) {
Copy link
Contributor

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}"

@adidahiya
Copy link
Contributor

ah, sorry about that. I filed #3128 to track that your comments get addressed before the next release

HyphnKnight pushed a commit to HyphnKnight/tslint that referenced this pull request Apr 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants