-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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: fix logic for top-level this
in no-invalid-this and no-eval
#15712
Conversation
"use strict";
this; I just had 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.
💯
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.
The change to no-eval
LGTM, but I believe the existing behavior of no-invalid-this
was already correct. In your demo link, I would expect errors from both rules. no-invalid-this
"Disallows this
keywords outside of classes or class-like objects." At the top level of a script, from the perspective of no-invalid-this
, it's still outside of a class or class-like object, so this
is still invalid even though it does have a value. The name of the rule is somewhat poor here, but the docs do a better job of conveying its purpose, which is consistent with the existing behavior.
I believe the For example: // not strict
function foo() {
this.alert('foo');
} If the intent was to "disallow The same applies to top-level this. The following code is not reported by // not strict
this.alert('foo'); But the following is: "use strict";
this.alert('foo'); That is the bug, as there's no difference between these two cases, both |
In my opinion, we should change "Disallows /* eslint no-invalid-this: "error" */
this.alert('foo');
() => {
this.alert('foo');
}
function foo() {
this.alert('foo');
} |
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.
Based on what you've shared, the current behavior of no-invalid-this
does appear to be more like "where this
is not undefined
" than "inside classes and class-like objects", and this change as written is consistent with the current behavior, so LGTM. Your suggested wording change to the stated purpose in the docs also LGTM. Since the behavior already seems to have drifted from the docs somewhat, I'm fine updating the docs as part of this bug fix or leaving it for a follow-up PR.
I'll close-reopen to try to run checks. |
I updated the docs for I also noticed that |
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [eslint](https://eslint.org) ([source](https://github.com/eslint/eslint)) | devDependencies | minor | [`8.11.0` -> `8.12.0`](https://renovatebot.com/diffs/npm/eslint/8.11.0/8.12.0) | --- ### Release Notes <details> <summary>eslint/eslint</summary> ### [`v8.12.0`](https://github.com/eslint/eslint/releases/v8.12.0) [Compare Source](eslint/eslint@v8.11.0...v8.12.0) #### Features - [`685a67a`](eslint/eslint@685a67a) feat: fix logic for top-level `this` in no-invalid-this and no-eval ([#​15712](eslint/eslint#15712)) (Milos Djermanovic) #### Chores - [`18f5e05`](eslint/eslint@18f5e05) chore: padding-line-between-statements remove useless `additionalItems` ([#​15706](eslint/eslint#15706)) (Martin Sadovy) </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/1256 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>
…slint#15712) * feat: fix logic for top-level `this` in no-invalid-this and no-eval * add test with `this.eval` to no-invalid-this * update docs to clarify `no-invalid-this`
…o-eval (eslint#15712)" This reverts commit 68b6cdc.
Prerequisites checklist
What is the purpose of this pull request? (put an "X" next to an item)
[ ] Documentation update
[x] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
This bug fix produces more warnings from the
no-eval
rule, and thus this change is marked asfeat
.Tell us about your environment:
What parser (default,
@babel/eslint-parser
,@typescript-eslint/parser
, etc.) are you using?default
Please show your full configuration:
Configuration
What did you do? Please include the actual source code causing the issue.
Online Demo
What did you expect to happen?
No
no-invalid-this
errors, oneno-eval
error.At the top level of scripts,
this
always refers to the global object, regardless of"use strict"
.In browsers, the above code alerts
foo
, sothis
is valid andthis.eval()
is aneval
call.What actually happened? Please include the actual, raw output from ESLint.
One
no-invalid-this
error, nono-eval
errors.What changes did you make? (Give an overview)
Fixed the
no-invalid-this
to treatthis
at the top level of scripts as valid, regardless of strict mode.Fixed the
no-eval
rule to treatthis.eval
at the top level of scripts as access to the globaleval
, regardless of strict mode.Is there anything you'd like reviewers to focus on?