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

feat: valid-typeof: always ban undefined #15635

Merged
merged 6 commits into from
Mar 5, 2022
Merged

Conversation

Zzzen
Copy link
Contributor

@Zzzen Zzzen commented Feb 24, 2022

Prerequisites checklist

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

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

fixes #15591

What changes did you make? (Give an overview)

check undefined and report

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

should we use getStaticValue?

@eslint-github-bot eslint-github-bot bot added feature This change adds a new feature to ESLint triage An ESLint team member will look at this issue soon labels Feb 24, 2022
@mdjermanovic mdjermanovic added accepted There is consensus among the team that this change meets the criteria for inclusion rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Feb 24, 2022
lib/rules/valid-typeof.js Outdated Show resolved Hide resolved
@mdjermanovic
Copy link
Member

should we use getStaticValue?

I think we can evaluate that change separately from this PR / original issue. Just handling undefined identifiers looks good for the start, and I'd expect that the majority of cases where getStaticValue can determine a static value that is being compared to typeof expressions will be those with undefined identifiers.

lib/rules/valid-typeof.js Outdated Show resolved Hide resolved
lib/rules/valid-typeof.js Outdated Show resolved Hide resolved
tests/lib/rules/valid-typeof.js Outdated Show resolved Hide resolved
Comment on lines 104 to 111
suggest: [
{
messageId: "suggestString",
fix(fixer) {
return fixer.replaceText(sibling, '"undefined"');
}
}
]
Copy link
Member

Choose a reason for hiding this comment

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

The suggestion to replace undefined with "undefined" makes sense with requireStringLiterals: true, too.

Maybe we could switch the order of else-ifs:

} else if (sibling.type === "Identifier" && sibling.name === "undefined" && isReferenceToGlobalVariable(sibling)) {
    context.report({
        node: sibling,
        messageId: requireStringLiterals ? "notString" : "invalidValue",
        suggest: [
            {
                messageId: "suggestString",
                data: { type: "undefined" },
                fix(fixer) {
                    return fixer.replaceText(sibling, '"undefined"');
                }
            }
        ]
    });
} else if (requireStringLiterals && !isTypeofExpression(sibling)) {
    context.report({ node: sibling, messageId: "notString" });
}

Test case:

{
    code: "if (typeof bar !== undefined) {}",
    options: [{ requireStringLiterals: true }],
    errors: [
        {
            messageId: "notString",
            type: "Identifier",
            suggestions: [
                {
                    messageId: "suggestString",
                    data: { type: "undefined" },
                    output: 'if (typeof bar !== "undefined") {}'
                }
            ]
        }
    ]
},

Zzzen and others added 2 commits March 4, 2022 22:50
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

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

LGTM. Just waiting on @mdjermanovic to confirm his feedback has been addressed.

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 for contributing!

@mdjermanovic mdjermanovic merged commit 57b8a57 into eslint:main Mar 5, 2022
crapStone pushed a commit to Calciumdibromid/CaBr2 that referenced this pull request Mar 18, 2022
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [eslint](https://eslint.org) ([source](https://github.com/eslint/eslint)) | devDependencies | minor | [`8.10.0` -> `8.11.0`](https://renovatebot.com/diffs/npm/eslint/8.10.0/8.11.0) |

---

### Release Notes

<details>
<summary>eslint/eslint</summary>

### [`v8.11.0`](https://github.com/eslint/eslint/releases/v8.11.0)

[Compare Source](eslint/eslint@v8.10.0...v8.11.0)

#### Features

-   [`800bd25`](eslint/eslint@800bd25) feat: add `destructuredArrayIgnorePattern` option in `no-unused-vars` ([#&#8203;15649](eslint/eslint#15649)) (Nitin Kumar)
-   [`8933fe7`](eslint/eslint@8933fe7) feat: Catch `undefined` and `Boolean()` in no-constant-condition ([#&#8203;15613](eslint/eslint#15613)) (Jordan Eldredge)
-   [`f90fd9d`](eslint/eslint@f90fd9d) feat: Add ESLint favicon to the HTML report document ([#&#8203;15671](eslint/eslint#15671)) (Mahdi Hosseinzadeh)
-   [`57b8a57`](eslint/eslint@57b8a57) feat: `valid-typeof` always ban `undefined` ([#&#8203;15635](eslint/eslint#15635)) (Zzzen)

#### Bug Fixes

-   [`6814922`](eslint/eslint@6814922) fix: escaping for square brackets in ignore patterns ([#&#8203;15666](eslint/eslint#15666)) (Milos Djermanovic)
-   [`c178ce7`](eslint/eslint@c178ce7) fix: extend the autofix range in comma-dangle to ensure the last element ([#&#8203;15669](eslint/eslint#15669)) (Milos Djermanovic)

#### Documentation

-   [`c481cec`](eslint/eslint@c481cec) docs: add fast-eslint-8 to atom integrations (userguide) ([#&#8203;15695](eslint/eslint#15695)) (db developer)
-   [`d2255db`](eslint/eslint@d2255db) docs: Add clarification about `eslint-enable` ([#&#8203;15680](eslint/eslint#15680)) (dosisod)
-   [`8b9433c`](eslint/eslint@8b9433c) docs: add object pattern to first section of computed-property-spacing ([#&#8203;15679](eslint/eslint#15679)) (Milos Djermanovic)
-   [`de800c3`](eslint/eslint@de800c3) docs: link to minimatch docs added.  ([#&#8203;15688](eslint/eslint#15688)) (Gaurav Tewari)
-   [`8f675b1`](eslint/eslint@8f675b1) docs: sort-imports add single named import example ([#&#8203;15675](eslint/eslint#15675)) (Arye Eidelman)

#### Chores

-   [`385c9ad`](eslint/eslint@385c9ad) chore: rm trailing space in docs ([#&#8203;15689](eslint/eslint#15689)) (唯然)

</details>

---

### Configuration

📅 **Schedule**: At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, click this checkbox.

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).

Co-authored-by: cabr2-bot <cabr2.help@gmail.com>
Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1217
Reviewed-by: Epsilon_02 <epsilon_02@noreply.codeberg.org>
Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
srijan-deepsource pushed a commit to DeepSourceCorp/eslint that referenced this pull request May 30, 2022
* feat: `valid-typeof`: always ban `undefined`

* change error message and check definition of `undefined`

* check string literal first

* add suggestions

* Apply suggestions from code review

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* suggest string when `requireStringLiterals` is on

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
srijan-deepsource added a commit to DeepSourceCorp/eslint that referenced this pull request May 30, 2022
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Sep 2, 2022
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Sep 2, 2022
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 feature This change adds a new feature to ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule Change: (valid-typeof) undefined should be banned
4 participants