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

Add "backtick" option for quotemark #4029

Merged
merged 1 commit into from
Jul 19, 2018
Merged

Add "backtick" option for quotemark #4029

merged 1 commit into from
Jul 19, 2018

Conversation

ericbf
Copy link
Contributor

@ericbf ericbf commented Jul 12, 2018

This lets users enforce only backtick "`" strings for everywhere that it’s permissible.

See #539
The corresponding option in eslint is called "backtick": http://eslint.org/docs/rules/quotes
See https://ponyfoo.com/articles/template-literals-strictly-better-strings

PR checklist

Overview of change:

Adds an option "backtick" to "quotemark" rule, which would allow users to enforce "`" only strings where permissible.

CHANGELOG.md entry:

[rule-change], [enhancement]

@palantirtech
Copy link
Member

Thanks for your interest in palantir/tslint, @ericferreiralife! 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.

Copy link

@giladgray giladgray left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ericferreiralife thanks! this is a sweet feature 👏
a few requests to tighten up the code and we're g2g.

@@ -37,13 +43,14 @@ export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "quotemark",
description: "Requires single or double quotes for string literals.",
description: "Requires single or double quotes or backticks for string literals.",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Enforces quote character for string literals."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

jsxQuoteFromOption[jsxPref] as JSX_QUOTE_MARK :
quoteMark !== "`" ?
quoteMark :
'"',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can all of this logic be moved inside getQuotemarkPreference and getJSXQuotemarkPreference?
may need another argument, but it will be much more legible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, doing that

const quoteMark = getQuotemarkPreference(args) === OPTION_SINGLE ? "'" : '"';
const pref = getQuotemarkPreference(args);
const jsxPref = getJSXQuotemarkPreference(args);
const quoteMark = quoteFromOption[pref != undefined ? pref : OPTION_DOUBLE] as QUOTE_MARK;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactor this logic. aiming for simply const quoteMark = getQuotemark(args);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup

}
}

return undefined;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return the default value instead of nothing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already doing that as part of the refactor

const jsxQuoteFromOption = {
[OPTION_JSX_SINGLE]: "'",
[OPTION_JSX_DOUBLE]: '"',
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move these out to static scope

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing that as part of the refactor.

@ericbf
Copy link
Contributor Author

ericbf commented Jul 13, 2018

Any updates here?

@ericbf
Copy link
Contributor Author

ericbf commented Jul 15, 2018

Hey @giladgray I pushed those changes you requested. Any follow up?

This lets users enforce only backtick "`" strings for everywhere that it's permissible. This also simplifies some logic in the preferred quote determination, combining some disparate logic into the nice self-packaged functions.

> See #539
> The corresponding option in eslint is called "backtick": http://eslint.org/docs/rules/quotes
> See https://ponyfoo.com/articles/template-literals-strictly-better-strings
@ericbf
Copy link
Contributor Author

ericbf commented Jul 17, 2018

Any new changes requested here?

@ericbf
Copy link
Contributor Author

ericbf commented Jul 17, 2018

@giladgray @adidahiya can anyone take a look here?

Copy link
Contributor

@johnwiseheart johnwiseheart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@johnwiseheart johnwiseheart merged commit 559d880 into palantir:master Jul 19, 2018
@ericbf ericbf deleted the add-quotemark-backtick branch July 19, 2018 19:11
@ShayBox
Copy link

ShayBox commented Sep 8, 2018

Any reason setting the option to backtick now would recommend using double quotes? Does it with both of the vscode extensions, seems it doesn't understand the new rule option.

EDIT: Nvm the rule just doesn't work at all, not listed as an option on the docs, seems this merged PR didn't actually stay in effect

@ericbf
Copy link
Contributor Author

ericbf commented Nov 4, 2018

@ShayBox let me look into that. This code was committed after the latest release, so it's not included in v5.11.0

@JasCodes
Copy link

JasCodes commented Dec 5, 2018

@ericbf Any idea on next release? Its been while.

@ericbf
Copy link
Contributor Author

ericbf commented Dec 10, 2018

@Jas99 I'm wondering the same thing... @johnwiseheart?

@johnwiseheart
Copy link
Contributor

johnwiseheart commented Dec 10, 2018 via email

@alexeymrkn
Copy link

Still ignores the backtick option and defaults to double in v5.12.0

@johnwiseheart
Copy link
Contributor

I tried you believe there's a bug in the backtick option (haven't tried it yet myself), opening a new issue would be your best bet.

@johnwiseheart
Copy link
Contributor

In fact, #4402 might be the issue to follow for that one.

@adidahiya
Copy link
Contributor

@alexeymrkn I'm not seeing that issue in 5.12.0, backtick works as intended for me. Please file a new issue with more details if your problem persists.

@adidahiya
Copy link
Contributor

Actually, I found some issues and confusing behavior with avoid-template and backticks, will try to fix them in #4408

@spencersmb
Copy link

I feel like something isn't working right. I don't get the option for backticks.
Using webstorm(2018.3.4), ts-lint(5.12.1) & typescript(3.3.3)

screen shot 2019-02-09 at 11 28 04 am

Any ideas why it's doing this?
Thanks!

@ericbf
Copy link
Contributor Author

ericbf commented Feb 9, 2019

I've been using the option with great success in my projects, not in webstorm. Does just putting the option in work for you? Does it just not show up as an option?

@spencersmb
Copy link

@ericbf It kinda works - it does this to a tsx file for example:
screen shot 2019-02-10 at 1 57 51 pm

It changed my imports, but it did not change my props on components, instead it made them double quotes. Now my imports can't be read lol. (the other backticks were already present)

@ericbf
Copy link
Contributor Author

ericbf commented Feb 10, 2019

Yes, I've seen the behavior on imports. Apparently ECMA only allows single or double quoted strings in imports, not no template literals. I've been putting // tslint:disable:quotemark and a respective enable above and below my imports to keep using the backtick option as a workaround.

@JoshuaKGoldberg
Copy link
Contributor

@spencersmb perhaps your editor's TSLint version isn't up-to-date? The dropdown/autocompletion options are auto-generated from the metadata in the rule.

Re your imports: that looks like a bug in the rule 😢; would you mind filing a new issue as a bug report? I'm curious about your source code, etc.

@ericbf
Copy link
Contributor Author

ericbf commented Feb 10, 2019

@JoshuaKGoldberg @spencersmb I'll open another issue to refer to the situations where typescript/ecma doesn't like backticks at all.

@ericbf
Copy link
Contributor Author

ericbf commented Feb 10, 2019

Opened #4514

@spencersmb
Copy link

spencersmb commented Feb 11, 2019

@JoshuaKGoldberg @ericbf Thanks for starting that ticket, I added on to it my info. I have to tell webstorm the path to Ts-lint module after I install it via npm so it should be running the latest version.
If you want to take a look at my repo here it is ->
https://github.com/spencersmb/etShop-gatsby

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.

10 participants