-
-
Notifications
You must be signed in to change notification settings - Fork 475
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(lint): implement a noGlobalEval rule #1133
Conversation
✅ Deploy Preview for biomejs canceled.
|
crates/biome_js_analyze/tests/specs/correctness/noGlobalEval/invalid.js
Outdated
Show resolved
Hide resolved
@nissy-dev For instance, the initial implementation couldn't detect cases where eval was being used with this. or window., and I struggled to come up with an efficient implementation to catch these. Additionally, ensuring that the diagnostic highlights are appropriate depending on the violation type was challenging for me. I'm concerned that my continued attempts might delay the progress of this project, and I believe it would be more beneficial for someone with more expertise to take over. |
Hi @you-5805, thanks for letting us know the obstacles you encounter. Do you think we could ship a minimal and consistent implementation? "Complex" tests could be commented for the time being and addressed in another PR. By the way, I don't to which tests you refer to. Sometimes we decide to diverge from the ESLint rule when it makes sense. |
e135a98
to
910b080
Compare
@Conaclos
First, regarding the test cases for the ESLint rule, there are currently two patterns that cannot pass (can't trigger an error) with the current implementation. The second pattern is the invocation of eval using the behavior where (0, x) evaluates to x, such as in (0, eval)('foo'). This should match JsCallExpression, but I am still considering how to implement a static determination that the callee is global eval. Additionally, the format of the diagnostics is not ideal as it is.
And the output for a variable declaration would be:
|
@you-5805 I think you are setting the expectation too high.
This is a specific case that we can implement in another PR. Similarly,
We could only highlight If you prefer, I can take the PR :) |
crates/biome_js_analyze/src/semantic_analyzers/nursery/no_global_eval.rs
Outdated
Show resolved
Hide resolved
910b080
to
bd659aa
Compare
// TODO Fix to prevent errors for these cases | ||
// function foo() { | ||
// var eval = "foo"; | ||
// window[eval]("foo"); | ||
// } | ||
|
||
// function foo() { | ||
// var eval = "foo"; | ||
// global[eval]("foo"); | ||
// } | ||
|
||
// function foo() { | ||
// var eval = "foo"; | ||
// globalThis[eval]("foo"); | ||
// } | ||
|
||
// function foo(eval) { | ||
// eval("var a = 0"); | ||
// } |
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.
Following four test cases, eval is bound to local scope variables, and the global eval is not being called. Therefore, the error for noGlobalEval should not be displayed. I understand that if eval is bound to a local scope variable, it should be treated as None with the following process, but it seems not to work as expected.
model.binding(&reference).is_none().then_some(())
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 the bug is coming from the semantic model. I will investigate the case.
@Conaclos If possible, I would like to proceed with this as a TODO, and go ahead with the review and merge of this PR. |
3a94669
to
d8b7123
Compare
Thanks for your contribution! I improved the docs and the diagnostics. For now, we can ship the rule ❤️ |
d8b7123
to
cf2582d
Compare
CodSpeed Performance ReportMerging #1133 will not alter performanceFalling back to comparing Summary
|
cf2582d
to
e57c0ba
Compare
Summary
Impletment correctness/noGlobalEval
fixes #1088
Test Plan
All existing tests has passed.
wip: Added snapshot tests has passed.
Problem
Seeing the error output in valid.js.snap, it seems that biome_js_syntax::global_identifier, which was suggested to use for this issue, determines local scope
eval
variable as a global_identifier. I have no idea why this doesn't work correctly.I looked into it a bit and it appears that in strict mode JavaScript, eval is treated like a reserved word, and the usage above does not seem to work.
So it might be better to drop the implementation that allows a variable named eval in the local scope. What do you think? If that's ok with you, I will update valid.js and the snapshot.