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

Chore: refactor template literal feature detection in 'quotes' rule. #11125

Conversation

bmish
Copy link
Sponsor Member

@bmish bmish commented Nov 25, 2018

What is the purpose of this pull request? (put an "X" next to item)

[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[X] Other, please explain:

What changes did you make? (Give an overview)

No behavior change. This change is a refactoring that extracts a helper function for detecting the usage of a template literal feature (which is used to determine if a template literal should be allowed by the 'quotes' rule). This refactoring separates that detection logic from the rest of the 'quotes' rule logic.

Is there anything you'd like reviewers to focus on?

Nothing specifically.

@eslint-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label Nov 25, 2018
@bmish bmish force-pushed the quotes-rule-refactor-template-literal-feature-detection branch 3 times, most recently from 1cbb105 to b83d5e5 Compare November 26, 2018 03:44
@@ -228,6 +228,34 @@ module.exports = {
}
}

/**
* Checks whether or not a given TemplateLiteral node is using a template literal feature.
Copy link
Member

Choose a reason for hiding this comment

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

it seems confusing to me. all templateLiteral nodes are using template literal feature, no?

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

For example, the TemplateLiteral node `hello world` is not using a template literal feature, whereas `hello ${name}` is using one. This matters because the 'quotes' rule may want to disallow an unnecessary template literal from being used, when a simple string literal 'hello world' would suffice.

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 maybe the word "feature" is confusing here -- I've seen these referred to as "template expressions".

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

Yeah, my example in the last comment is using a template expression. But expressions are just one feature of template literals. This function is checking if any of the three template literal features are in use.

Copy link
Member

Choose a reason for hiding this comment

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

In that case I'm also confused -- what three features are you referring to?

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

The features are:

  1. Expression interpolation
  2. Tagged templates
  3. Multi-line strings (linebreaks)

The official documentation mentions these too: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

Note that the existing code here was checking for these same features, but I'm trying to make these checks clearer by moving them into a helper function.

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

Is there still confusion around this? Happy to make improvements.

Copy link
Member

Choose a reason for hiding this comment

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

It might be a good idea to list the three features explicitly in the comment since it seems like the word "feature" has the potential to cause confusion. Aside from that, this change looks good to me.

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

Have another look. I updated the wording of the comment and the function name. Hope this helps. (I was trying to avoid listing the features in the comment as that would be unnecessary repetition of what's in the code directly below).

@aladdin-add aladdin-add added rule Relates to ESLint's core rules evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion chore This change is not user-facing and removed triage An ESLint team member will look at this issue soon labels Nov 27, 2018
@platinumazure
Copy link
Member

Could you please share what problem you're trying to solve? What motivated you to make this change?

@bmish
Copy link
Sponsor Member Author

bmish commented Dec 1, 2018

@platinumazure thanks for the question. There's no problem. My goal was just to improve the code quality with a small refactoring. It came up because I was thinking about rule improvements that I could make in this area, and I thought this refactoring would make future changes easier.

@bmish bmish force-pushed the quotes-rule-refactor-template-literal-feature-detection branch from b83d5e5 to 2df365b Compare December 1, 2018 02:23
@bmish bmish force-pushed the quotes-rule-refactor-template-literal-feature-detection branch from 2df365b to 35131ba Compare December 1, 2018 02:26
Copy link
Member

@platinumazure platinumazure left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@platinumazure platinumazure added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Dec 7, 2018
Copy link
Member

@not-an-aardvark not-an-aardvark left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for contributing!

@not-an-aardvark not-an-aardvark merged commit dd7b0cb into eslint:master Dec 8, 2018
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Jun 7, 2019
@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 Jun 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion chore This change is not user-facing rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants