Preserve newlines and tabs when autofixing quote marks #2224
Conversation
Thanks for your interest in palantir/tslint, @moritzg91! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request. |
src/rules/quotemarkRule.ts
Outdated
@@ -84,7 +84,9 @@ class QuotemarkWalker extends Lint.RuleWalker { | |||
const expectedQuoteMark = node.parent!.kind === ts.SyntaxKind.JsxAttribute ? this.jsxQuoteMark : this.quoteMark; | |||
const actualQuoteMark = node.getText()[0]; | |||
if (actualQuoteMark !== expectedQuoteMark && !(this.avoidEscape && node.text.includes(expectedQuoteMark))) { | |||
const escapedText = node.text.replace(new RegExp(expectedQuoteMark, "g"), `\\${expectedQuoteMark}`); | |||
let escapedText = node.text.replace(new RegExp(expectedQuoteMark, "g"), `\\${expectedQuoteMark}`); |
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.
try getting node.getText()
, which will return a string with escaped characters, so you don't have to do the second replace. Otherwise you'd need to add a more replacement characters like "\" and "\r".
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.
makes sense, updated to use getText.
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 good, thanks for the contribution!
I'm using tslint 4.5.1 but the problem with the escaped characters still seems to be present. I'm having this { code: "var {\nx,y\n} = y", options: ["never"] }, turned into { code: 'var {
x,y
} = y', options: ["never"] }, |
can't reproduce on 4.5.1. if it's still an issue, please open a ticket |
I should have checked using the command line, @nchen63 you are correct. The issue I'm seeing is with my IDE. I'm using
It has a feature to fix the issues (uses the output provided by tslint) but it seems that this is the culprit and not tslint. I'll update and see if they fixed it, otherwise I'll report the issue. EDIT: It's fixed. |
PR checklist
What changes did you make?
Simple change in quotemarkRule::visitStringLiteral. Search the escaped text for \n and \t and replace with escaped versions. Also added a string containing tab/newline to the test cases for the quotemark rule.
Is there anything you'd like reviewers to focus on?
First PR to this project, so I'm sorry if I missed something obvious