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

Update: check template literal in no-constant-condition (fixes #12815) #12816

Merged
merged 6 commits into from Jan 27, 2020
Merged

Update: check template literal in no-constant-condition (fixes #12815) #12816

merged 6 commits into from Jan 27, 2020

Conversation

yeonjuan
Copy link
Member

@yeonjuan yeonjuan commented Jan 20, 2020

Prerequisites checklist

  • I have read the contributing guidelines.
  • The team has reached consensus on the changes proposed in this pull request. If not, I understand that the evaluation process will begin with this pull request and won't be merged until the team has reached consensus.

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

[ ] Documentation update
[x] 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
[ ] Other, please explain:

What changes did you make? (Give an overview)

Fixed #12815 by add template literal checking.

  • overview
    This PR will make eslint to warn when a template literal(constant expression) placed in a test condition.
if (`a`) ;  // Error - Unexpected constant condition
while(`a`); // Error - Unexpected constant condition
if (`${"foo"}`); // Error - Unexpected constant condition

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

It needs a consensus of ESlint members.
Marked as anUpdate, as it will generate more warning, but I think it can be accepted as a bug fix.

@eslint-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label Jan 20, 2020
@yeonjuan yeonjuan changed the title Fix: check template literal in no-constant-condition (fixes #12815) Update: check template literal in no-constant-condition (fixes #12815) Jan 21, 2020
@mdjermanovic
Copy link
Member

I think this is certainly a correct change in behavior 👍

Just not sure how to label it (bug/enhancement/breaking).

@mdjermanovic
Copy link
Member

It might be nice to add a test with a tagged template literal, which would be a constant if there's no tag.

@yeonjuan
Copy link
Member Author

@mdjermanovic Thanks for commenting! I have a question about it. :)

which would be a constant if there's no tag.

What is the tagged template literal without a tag? Just mean template literal?

tag`template literal ${expression}`

@yeonjuan yeonjuan added accepted There is consensus among the team that this change meets the criteria for inclusion bug ESLint is working incorrectly enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Jan 22, 2020
@yeonjuan yeonjuan closed this Jan 22, 2020
@yeonjuan yeonjuan reopened this Jan 22, 2020
@yeonjuan
Copy link
Member Author

sorry for closing it, it was mistake.

@mdjermanovic
Copy link
Member

Sorry for the confusion! Was thinking of a test case like the one you added now: if (tag`a`)

(tagged template with a constant template literal)

lib/rules/no-constant-condition.js Outdated Show resolved Hide resolved
@mdjermanovic
Copy link
Member

While at template literals, wondering should the rule also report something like this:

if (`a${b}`) {}

This is similar to the logic for typeof b: it isn't a constant, but it's an expression that always evaluates to a truthy string value and thus may be treated as a constant when inBooleanPosition is true?

@kaicataldo
Copy link
Member

This is similar to the logic for typeof b: it isn't a constant, but it's an expression that always evaluates to a truthy string value and thus may be treated as a constant when inBooleanPosition is true?

I think this makes sense 👍

@yeonjuan
Copy link
Member Author

yeonjuan commented Jan 23, 2020

@mdjermanovic

This is similar to the logic for typeof b: it isn't a constant, but it's an expression that always evaluates to a truthy string value and thus may be treated as a constant when inBooleanPosition is true?

I agree. thanks for commenting :). I have some questions.
If then, should we change these cases to be also reported? And should I treat those cases in this pr?
online-demo

/*eslint no-constant-condition: "error"*/
if ("a" + b) {} // no-error

@mdjermanovic
Copy link
Member

/*eslint no-constant-condition: "error"*/
if ("a" + b) {} // no-error

I think this should be a separate issue or PR.

@yeonjuan yeonjuan closed this Jan 25, 2020
@yeonjuan yeonjuan reopened this Jan 25, 2020
@yeonjuan
Copy link
Member Author

@mdjermanovic
Thanks for the review. I changed it.
To note, There are other cases we should treat, which related to string concatenation generating the truthy result. (in both string literal and template literal)

if (`${"foo"}${foo}`) {};
if("foo" + foo) {};

And I think both cases can be fixed together in another PR. Thanks :)

Copy link
Member

@mdjermanovic mdjermanovic 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!

Copy link
Member

@kaicataldo kaicataldo 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!

@kaicataldo kaicataldo merged commit 0460748 into eslint:master Jan 27, 2020
@yeonjuan yeonjuan deleted the check-templ-literal-no-const-cond branch January 29, 2020 13:50
montmanu pushed a commit to montmanu/eslint that referenced this pull request Mar 4, 2020
…#12815) (eslint#12816)

* Fix: check template literal in no-constant-condition (fixes eslint#12815)

* add test cases

* add tagged template test cases

* treats truthy cases

* check template literal truthy cases

* fix typo, add test cases for cooked
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Jul 27, 2020
@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 Jul 27, 2020
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 bug ESLint is working incorrectly enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[no-constant-condition] overlooks template literal
3 participants