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

Preserve newlines and tabs when autofixing quote marks #2224

Merged
merged 3 commits into from Feb 22, 2017

Conversation

moritzg91
Copy link
Contributor

@moritzg91 moritzg91 commented Feb 20, 2017

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

@palantirtech
Copy link
Member

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.

@@ -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}`);
Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor

@nchen63 nchen63 left a 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!

@jmlopez-rod
Copy link
Contributor

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

@nchen63
Copy link
Contributor

nchen63 commented Mar 14, 2017

can't reproduce on 4.5.1. if it's still an issue, please open a ticket

@jmlopez-rod
Copy link
Contributor

jmlopez-rod commented Mar 14, 2017

I should have checked using the command line, @nchen63 you are correct. The issue I'm seeing is with my IDE. I'm using

IntelliJ IDEA 2017.1 EAP
Build #IU-171.3224.1, built on February 14, 2017
IntelliJ IDEA EAP User
Expiration date: March 16, 2017
JRE: 1.8.0_112-release-b702 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Mac OS X 10.12

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.

screen shot 2017-03-14 at 1 06 09 pm

I'll update and see if they fixed it, otherwise I'll report the issue.

EDIT: It's fixed.

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

4 participants