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
Conversation
@jfmengels, thanks for your PR! By analyzing the annotation information on this pull request, we identified @nzakas, @alberto and @kaicataldo to be potential reviewers |
LGTM |
We definitely only change 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."); |
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.
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
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.
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.
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.
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.)
Looks good to me (except a nit pick), thank you! (Though I'm happier if this rule is fixable 😜) |
Fyi: lodash supports es2015 syntax in its template, so thats a reason to maybe not make it recomended? |
So am I understanding it right that I should set Also, since ESLint now doesn't support 0.1X versions of Node.js, can and should I use template literals myself, @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 @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 @mysticatea About the fixing part, I don't mind doing it, but I have a few concerns. |
Yes. we cannot change
Yes, the fixing is difficult. |
LGTM |
Updated with regards to @mysticatea's comments.
Let's do that. I can help with that again :) |
}, | ||
|
||
create: function(context) { | ||
var regex = /\$\{.*\}/; |
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.
Maybe better to do [^/}]+
instead of .*
(more specific)
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.
Do you mean [^}]
? I agree, that would be better 👍
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 |
For the rule name, IMO ideally it would be For the warning, I'm thinking something like 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. |
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. |
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
That sounds good to me. |
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 I'm not sure about Any other team members have thoughts on naming? |
|
|
|
Gotcha. Thanks for the explanation. Shall I go ahead with |
Seems like |
LGTM |
I've updated the PR:
@nzakas Do you still have concerns about the content of the rule documentation? |
LGTM |
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) | |||
|
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.
"disallow template literal placeholder syntax in regular strings"
(To fit with other rule docs)
LGTM |
LGTM |
LGTM |
Updated with @nzakas's comments |
@jfmengels Thanks so much for your contribution and being willing to work with us to get this merged! |
Yay! 🎉 Glad to have been able to contribute :) |
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:
conf/eslint.json
, as it triggered a lot of errorsPossible Errors
category, which sounds like the most appropriate to me