-
-
Notifications
You must be signed in to change notification settings - Fork 368
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
Add prefer-global-this
rule
#2410
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
We can use GlobalReferenceTracker or use |
This comment was marked as outdated.
This comment was marked as outdated.
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 rule should accept and pass all known window
properties, so it likely needs to import a list from npm.
Pass
window.innerHeight
window.addEventListener()
window.name = 'Future'
This comment was marked as resolved.
This comment was marked as resolved.
I agree, actual |
@fregante I don't think it can be an automatic list as https://developer.mozilla.org/en-US/docs/Web/API/Window lists a lot of non-window-specific properties too. For example, |
Interestingly, that appears to be true. I was under the impression that
As "automatic" as a list manually written and published by someone else, following the same meaning, that's what I meant 😃 But yeah the list isn't huge so a copy-paste from that page and manual exclusion isn't a huge problem, at least to get this started. |
This comment was marked as resolved.
This comment was marked as resolved.
I would prefer to maintain it here. The list should not be too long. Most properties on |
You can probably find the main ones here: https://html.spec.whatwg.org/multipage/nav-history-apis.html#the-window-object |
This comment was marked as outdated.
This comment was marked as outdated.
I can help filter out some of these. From a first look, it varies:
For the first two categories, they should either use globalThis or nothing at all (unless they’re used as edit: however this would make the logic more complicated, so never mind the rule name change suggestion. |
But it's "don't use window, or self, or global". I think "prefer |
What I was suggesting is that you should still prefer nothing at all over Edit: I opened #2419 |
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.
Please try to reuse existing utilities.
7290783
to
ff43465
Compare
@sindresorhus This is already resolved. see the test case https://github.com/sindresorhus/eslint-plugin-unicorn/pull/2410/files#diff-560654447874168910d7716e1da47d1f8f03eebfc3f5eaa14f7977133d1d1882R57-R64 |
@fisker Could you please review it again and let me know if there is anything that needs to change |
I did not reviewed this PR carefully. Inspired by the implementation of eslint built-in rule no-restricted-globals. I provide a simple demo to implement it. {
create: (context) => ({
Program: (node) => {
const banned = ["global", "self"];
// Report variables declared elsewhere
const scope = context.sourceCode.getScope(node);
scope.variables.forEach((v) => {
if (banned.includes(v.name)) {
v.references.forEach((ref) => {
context.report({node:ref.identifier, message: "xxxxxx"});
});
}
});
// Report variables not declared at all
scope.through.forEach((ref) => {
if (banned.includes(ref.identifier.name)) {
context.report({node:ref.identifier, message: "xxxxxx"});
}
});
},
}),
} |
This comment was marked as resolved.
This comment was marked as resolved.
Nice, it works without change any test case and the code is more concise |
rules/prefer-global-this.js
Outdated
@param {import('eslint').Rule.RuleContext} context | ||
@param {import('estree').Identifier} node | ||
*/ | ||
function reportProblem(context, node) { |
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.
In this function, we should completely ban self
and global
, as they a not standard.
As for the window
, if the api only works on browser, window can be used. Otherwise, window
should be banned too.
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.
Worker APIs should use self
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.
Should they? I think self
was chosen because there's no window
, but globalThis
should still be preferred there I think.
- Service workers introduction: 2014
globalThis
introduction: 2019
I think using self
in service workers is more of a historical artifact rather than a necessity. We can discuss/change this later though, I don't want to hold this PR any longer 🙏
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.
@fregante Can you open an issue?
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.
Great work, and sorry for the delay.
Nice work, @axetroy 👍 |
Just upgraded my version of the plugin and started getting lint errors for this rule on code like this: export function detectPrefersDarkMode() {
return (
typeof window !== 'undefined' &&
window.matchMedia('(prefers-color-scheme: dark)').matches
);
} Shouldn't this type of usage be excluded from this rule since |
It works for me. That function does exist in your browser. Are you sure that's not just your test env crashing? You probably want to replace all that with: |
So this rule is failing for me on this line: https://github.com/dotcore64/react-stay-scrolled/blob/master/src%2Findex.js#L11 Since It would be cool if these edge cases could be handled |
@perrin4869 see #2468 |
'dispatchEvent', | ||
|
||
'self', | ||
'location', |
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.
why allowing self.location
in workers but not window.location
?
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.
See #2458
Close #978