-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
feat: allow global type declaration in no-var
#19714
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
Changes from 1 commit
abd7209
abbdd88
2678514
366a4cf
b3ab2a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -346,9 +346,19 @@ module.exports = { | |
|
|
||
| return { | ||
| "VariableDeclaration:exit"(node) { | ||
| if (node.kind === "var") { | ||
remcohaszing marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| report(node); | ||
| if (node.kind !== "var") { | ||
| return; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In typescript-eslint/typescript-eslint#7941, the proposal is to require
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I see what you're saying... However, I think it's not such a good idea to have two separate rules, especially in two separate plugins, handle overlapping and possibly-conflicting logic. It's like if return-await were actually two separate rules for when What do you think of the following proposal instead? What if we draw the line such that this rule, Concretely, that would mean I propose that all of the following, currently documented as incorrect, would be permitted by declare var x: number
declare namespace ns {
var x: number
}
declare module 'module' {
var x: number
}and instead would be banned by a new typescript-eslint rule.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be clear, I most prefer having all the logic handled in this But if we're not going to enforce
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the most correct implementation is as you suggested in #19714 (comment).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually this even applies to regular code, not just declarations. For example, the following file is also a global script as far as TypeScript is concerned. It doesn’t even matter if it’s JavaScript or TypeScript syntax. var a = 1How far should the There are only 2 cases where the user should definitely use
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The original intent of this rule is to ban the use of
At this point I'd say no. This feels like a step too far. A better approach for a user is to disable this rule just in those files. |
||
| } | ||
|
|
||
| if ( | ||
| node.parent.type === "TSModuleBlock" && | ||
| node.parent.parent.type === "TSModuleDeclaration" && | ||
| node.parent.parent.global | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| report(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.
On the rule logic - I was thinking this should also apply to top-level definitions in (non-module) declaration files, right? Been a while since I've worked with that particular setup. But something like this?
Uh oh!
There was an error while loading. Please reload this page.
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.
Here's a little minimal repository demonstrating this behavior for reference: https://github.com/kirkwaiblinger/tsglobalvardemo
Uh oh!
There was an error while loading. Please reload this page.
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.
Come to think of it, it actually doesn't matter whether it's a
.d.tsfile or not. The same behavior applies withdeclared variables in ordinary.tsfiles.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.
What matters is whether TypeScript considers file to be a script or a module. If a module contains imports or exports, it’s a module. Otherwise it’s a script. In a script everything you declare is global. In a module you can declare globals with
declare global.So this module:
is equivalent to this script:
However, TypeScript also has the option
moduleDetection. If this is set toforce, everything source file considered a module. This makes it ambiguous whether the latter is a local variable declaration or a global. We could assume default options. ThemoduleDetectionoption doesn’t apply to declaration files.