Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: no-template-curly-in-string rule (fixes #6186) #6767

Merged
merged 7 commits into from Aug 9, 2016

Conversation

jfmengels
Copy link
Contributor

@jfmengels jfmengels commented Jul 26, 2016

Adds the new rule wrong-quotes-for-template-string, and fixes #6186.

Let me know if and what I need to change or if I missed a step.

Notes:

  • I turned the rule off in conf/eslint.json, as it triggered a lot of errors
  • I put the rule into the Possible Errors category, which sounds like the most appropriate to me
  • I made it recommended, but maybe you only add new recommended rules on major versions. Should I turn it off?

@mention-bot
Copy link

@jfmengels, thanks for your PR! By analyzing the annotation information on this pull request, we identified @nzakas, @alberto and @kaicataldo to be potential reviewers

@eslintbot
Copy link

LGTM

@platinumazure
Copy link
Member

We definitely only change eslint:recommended on major releases.

Also, could you provide a link to an overview of the prefixed template string syntax and associated semantics? Just for my own education if nothing else 😄

return {
Literal: function(node) {
if (typeof node.value === "string" && regex.test(node.value)) {
context.report(node, "Possible use of template string inside a regular string.");
Copy link
Member

Choose a reason for hiding this comment

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

This overload is old style.
The following code is more proper 😃

context.report({
    node: node, 
    message: "Possible use of template string inside a regular string."
});

http://eslint.org/docs/developer-guide/working-with-rules#contextreport

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ha, that's what I usually use, but I checked an existing rule to be consistent with the project style, and must have copied it from one that wasn't updated 😅

Am I allowed to use object literal syntax?

context.report({
    node,
    message: "Possible use of template string inside a regular string."
});

Sidenote: for my own plugins, I found it pretty nice to have the following syntax, but didn't use it as it isn't the norm here.

const create = (context) => {} // of function create() {}, whatever

const schema = [];

module.exports = {
    create,
    meta: {
        schema,
        docs: {
            description: "Warn when using template string syntax in regular strings",
            category: "Possible Errors",
            recommended: true
        }
    }
};

Don't plan on adding it here now, but food for thought.

Copy link
Member

Choose a reason for hiding this comment

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

I think we can use property shorthands (except get and set. {get, set} will cause syntax error for Node 4).
But it might be modified when we enable object-shorthand rule (related in #6407).

(FWIW, I would not like arrow functions here.)

@mysticatea
Copy link
Member

Looks good to me (except a nit pick), thank you!

(Though I'm happier if this rule is fixable 😜)

@lukeapage
Copy link
Contributor

Fyi: lodash supports es2015 syntax in its template, so thats a reason to maybe not make it recomended?

@jfmengels
Copy link
Contributor Author

jfmengels commented Jul 27, 2016

So am I understanding it right that I should set recommended to false?

Also, since ESLint now doesn't support 0.1X versions of Node.js, can and should I use template literals myself, const, etc. where appropriate?

@platinumazure See https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals#Tagged_template_literals. Should I put it in the rule description? It's not all that relevant to this rule IMO, as you can't put wrong quotes with it templateFunction'${foo}' as that would be a syntax error.

@lukeapage Huh, didn't even know that. From what I understand, I should not set it as recommended anyway, but it's a valid concern. Should I add this use case in the When not to use it section, or a note somewhere else? Sounds like a pretty limited scenario that would fit a eslint-disable comment IMO, but might be worth noting.

@mysticatea About the fixing part, I don't mind doing it, but I have a few concerns.
From what I understand, I should take the string literal and replace it by the same string but replace it by something that has backtick quotes.
Should I replace any \' / \" that are inside the string (or at least the ones corresponding to the used quotes).
Should I prepend any backtick quotes by a \?
For the last two questions, should I do that in the literal parts and/or in the place holders? I'd say both, but I'll have to test it out well before being sure.

@mysticatea
Copy link
Member

mysticatea commented Jul 27, 2016

So am I understanding it right that I should set recommended to false?

Yes. we cannot change eslint:recommended for now.
When we do a major update, like #6403 and #4953, we will evaluate rules and will change eslint:recommended.

About the fixing part, I don't mind doing it, but I have a few concerns.

Yes, the fixing is difficult.
We can do it carefully after this PR is merged.

@eslintbot
Copy link

LGTM

@jfmengels
Copy link
Contributor Author

jfmengels commented Jul 27, 2016

Updated with regards to @mysticatea's comments.

Yes, the fixing is difficult. We can do it carefully after this PR is merged.

Let's do that. I can help with that again :)

},

create: function(context) {
var regex = /\$\{.*\}/;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe better to do [^/}]+ instead of .* (more specific)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean [^}]? I agree, that would be better 👍

@nzakas
Copy link
Member

nzakas commented Jul 27, 2016

Code looks okay, but I think we need to rethink the rule name and description. This rule isn't about quotes, it's about embedded expressions, and I think it should be warning about that. So maybe no-confusing-substring or something similar? And the warning would be something like, "Unexpected template string expression."

@jfmengels
Copy link
Contributor Author

For the rule name, IMO ideally it would be no-template-literal-placeholder-in-string, ...but it's too long. I would be fine with no-confusing-substring, but I think we can do better as it doesn't say what kind of confusion we're reporting (what if at some point, some other sources of confusion are added to the language?)

For the warning, I'm thinking something like
'Unexpected template literal place holder in regular string. Did you mean to use "`" quotes?'
or
'Unexpected template literal place holder in regular string. Did you mean to use a template literal?'

Maybe there's too much technical jargon, and I don't think there are questions in warning messages in core rules, so maybe not a form you like.

🚲🏠 mode on.

@nzakas
Copy link
Member

nzakas commented Jul 28, 2016

what if at some point, some other sources of confusion are added to the language?

Then we can update this rule to include it, which is what I was going for. :)

The message needs to be shorter and we don't tend to give advice on how to fix the problem. We can probably get away with "Unexpected template literal placeholder in string." but not much more. We have documentation so these messages can be succinct.

@jfmengels
Copy link
Contributor Author

jfmengels commented Jul 28, 2016

what if at some point, some other sources of confusion are added to the language?

Then we can update this rule to include it, which is what I was going for. :)

Doesn't seem like a good idea to me as it kind of defeats the purpose of having one rule per possible error or stylistic issue. If the source of confusion is intimately related to template strings, sure, but otherwise I'd say it should be in a different rule. Therefore, I don't like no-confusing-substring. Don't have much better in mind though... Something like no-placeholder-in-string maybe?

We can probably get away with "Unexpected template literal placeholder in string."

That sounds good to me.

@nzakas
Copy link
Member

nzakas commented Jul 29, 2016

Doesn't seem like a good idea to me as it kind of defeats the purpose of having one rule per possible error or stylistic issue

I'm not sure where you got this, but that's not a goal of the project. Indeed, we try to group things into rules where there are commonalities. The strict rule, for example, warns when you're missing a directive, when a directive is in the wrong place, and when a directive is unnecessary.

I'm not sure about no-placeholder-in-string because I'm not sure "placeholder" is particularly clear on its own (also, it's not a placeholder in this context, it's just a substring).

Any other team members have thoughts on naming?

@ilyavolodin
Copy link
Member

no-template-in-string maybe?

@ljharb
Copy link
Sponsor Contributor

ljharb commented Jul 29, 2016

no-accidental-template-literal-notation-in-strings?

@mysticatea
Copy link
Member

no-template-curly-in-string?

@jfmengels
Copy link
Contributor Author

I'm not sure where you got this, but that's not a goal of the project. Indeed, we try to group things into rules where there are commonalities.

Gotcha. Thanks for the explanation.

Shall I go ahead with no-template-curly-in-string, or do you wish for more bikeshedding?

@nzakas
Copy link
Member

nzakas commented Aug 2, 2016

Seems like no-template-curly-in-string has support, so I'll go along with that.

@eslintbot
Copy link

LGTM

@jfmengels jfmengels changed the title New: wrong-quotes-for-template-string rule (fixes #6186) New: no-template-curly-in-string rule (fixes #6186) Aug 2, 2016
@jfmengels
Copy link
Contributor Author

I've updated the PR:

  • Changed rule name
  • Simplified the regex
  • Changed the error message

@nzakas Do you still have concerns about the content of the rule documentation?

@eslintbot
Copy link

LGTM

@ilyavolodin
Copy link
Member

LGTM, but waiting another day for others to look

@@ -0,0 +1,33 @@
# Warn when using template string syntax in regular strings (no-template-curly-in-string)

Copy link
Member

Choose a reason for hiding this comment

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

"disallow template literal placeholder syntax in regular strings"

(To fit with other rule docs)

@eslintbot
Copy link

LGTM

@eslintbot
Copy link

LGTM

@eslintbot
Copy link

LGTM

@jfmengels
Copy link
Contributor Author

Updated with @nzakas's comments

@ilyavolodin
Copy link
Member

@jfmengels Thanks so much for your contribution and being willing to work with us to get this merged!

@ilyavolodin ilyavolodin merged commit be68f0b into eslint:master Aug 9, 2016
@jfmengels
Copy link
Contributor Author

Yay! 🎉 Glad to have been able to contribute :)

@jfmengels jfmengels deleted the issue-6186 branch August 9, 2016 09:00
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proposed new rule: wrong-quotes-for-template-string
10 participants