-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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 globalThis #29332
Merged
Merged
Add globalThis #29332
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
a5484dd
Restore original code from bind-toplevel-this
sandersn f650492
Working in JS, but the symbol is not right.
sandersn 4bab559
Check in TS also; update some tests
sandersn d62a8d8
Update baselines
sandersn f07c5d6
Handle type references to globalThis
sandersn 80558a4
Restore former behaviour of implicitThis errors
sandersn e3fbe5c
Test values with type globalThis
sandersn 88c6f7d
Add esnext declaration for globalThis
sandersn f1ebbad
Merge branch 'master' into add-globalThis
sandersn 4215a76
Switch to symbol-based approach
sandersn 620177f
Merge branch 'master' into add-globalThis
sandersn d48cc4c
Do not suggest globals for completions at toplevel
sandersn 719cc35
Add tests of element and property access
sandersn 6b18334
Look up globalThis using normal resolution
sandersn 7a3d714
Update fourslash tests
sandersn fc10811
Merge branch 'master' into add-globalThis
sandersn 9fe7d87
Add missed fourslash test update
sandersn 9db574a
Remove esnext.globalthis.d.ts too
sandersn 3b27dc4
Add chained globalThis self-lookup test
sandersn 1bd4a0d
Merge branch 'master' into add-globalThis
sandersn e5216f6
Merge branch 'master' into add-globalThis
sandersn d4d5be4
Attempt at making globalThis readonly
sandersn 3ac9fac
Merge branch 'master' into add-globalThis
sandersn 00312cd
Add/update tests
sandersn 25ad4d1
Merge branch 'master' into add-globalThis
sandersn 6b674f2
Merge branch 'master' into add-globalThis
sandersn 517dc15
Addres PR comments:
sandersn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,8 +88,16 @@ namespace ts { | |
const emitResolver = createResolver(); | ||
const nodeBuilder = createNodeBuilder(); | ||
|
||
const globals = createSymbolTable(); | ||
const undefinedSymbol = createSymbol(SymbolFlags.Property, "undefined" as __String); | ||
undefinedSymbol.declarations = []; | ||
|
||
const globalThisSymbol = createSymbol(SymbolFlags.Module, "globalThis" as __String, CheckFlags.Readonly); | ||
globalThisSymbol.exports = globals; | ||
globalThisSymbol.valueDeclaration = createNode(SyntaxKind.Identifier) as Identifier; | ||
(globalThisSymbol.valueDeclaration as Identifier).escapedText = "globalThis" as __String; | ||
globals.set(globalThisSymbol.escapedName, globalThisSymbol); | ||
|
||
const argumentsSymbol = createSymbol(SymbolFlags.Property, "arguments" as __String); | ||
const requireSymbol = createSymbol(SymbolFlags.Property, "require" as __String); | ||
|
||
|
@@ -310,9 +318,9 @@ namespace ts { | |
getAccessibleSymbolChain, | ||
getTypePredicateOfSignature: getTypePredicateOfSignature as (signature: Signature) => TypePredicate, // TODO: GH#18217 | ||
resolveExternalModuleSymbol, | ||
tryGetThisTypeAt: node => { | ||
tryGetThisTypeAt: (node, includeGlobalThis) => { | ||
node = getParseTreeNode(node); | ||
return node && tryGetThisTypeAt(node); | ||
return node && tryGetThisTypeAt(node, includeGlobalThis); | ||
}, | ||
getTypeArgumentConstraint: nodeIn => { | ||
const node = getParseTreeNode(nodeIn, isTypeNode); | ||
|
@@ -459,7 +467,6 @@ namespace ts { | |
|
||
const enumNumberIndexInfo = createIndexInfo(stringType, /*isReadonly*/ true); | ||
|
||
const globals = createSymbolTable(); | ||
interface DuplicateInfoForSymbol { | ||
readonly firstFileLocations: Node[]; | ||
readonly secondFileLocations: Node[]; | ||
|
@@ -9703,7 +9710,7 @@ namespace ts { | |
} | ||
|
||
function getLiteralTypeFromProperties(type: Type, include: TypeFlags) { | ||
return getUnionType(map(getPropertiesOfType(type), t => getLiteralTypeFromProperty(t, include))); | ||
return getUnionType(map(getPropertiesOfType(type), p => getLiteralTypeFromProperty(p, include))); | ||
} | ||
|
||
function getNonEnumNumberIndexInfo(type: Type) { | ||
|
@@ -16981,25 +16988,27 @@ namespace ts { | |
captureLexicalThis(node, container); | ||
} | ||
|
||
const type = tryGetThisTypeAt(node, container); | ||
if (!type && noImplicitThis) { | ||
// With noImplicitThis, functions may not reference 'this' if it has type 'any' | ||
const diag = error( | ||
node, | ||
capturedByArrowFunction && container.kind === SyntaxKind.SourceFile ? | ||
Diagnostics.The_containing_arrow_function_captures_the_global_value_of_this_which_implicitly_has_type_any : | ||
Diagnostics.this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation); | ||
if (!isSourceFile(container)) { | ||
const outsideThis = tryGetThisTypeAt(container); | ||
if (outsideThis) { | ||
addRelatedInfo(diag, createDiagnosticForNode(container, Diagnostics.An_outer_value_of_this_is_shadowed_by_this_container)); | ||
const type = tryGetThisTypeAt(node, /*includeGlobalThis*/ true, container); | ||
if (noImplicitThis) { | ||
const globalThisType = getTypeOfSymbol(globalThisSymbol); | ||
if (type === globalThisType && capturedByArrowFunction) { | ||
error(node, Diagnostics.The_containing_arrow_function_captures_the_global_value_of_this); | ||
} | ||
else if (!type) { | ||
// With noImplicitThis, functions may not reference 'this' if it has type 'any' | ||
const diag = error(node, Diagnostics.this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation); | ||
if (!isSourceFile(container)) { | ||
const outsideThis = tryGetThisTypeAt(container); | ||
if (outsideThis && outsideThis !== globalThisType) { | ||
addRelatedInfo(diag, createDiagnosticForNode(container, Diagnostics.An_outer_value_of_this_is_shadowed_by_this_container)); | ||
} | ||
} | ||
} | ||
} | ||
return type || anyType; | ||
} | ||
|
||
function tryGetThisTypeAt(node: Node, container = getThisContainer(node, /*includeArrowFunctions*/ false)): Type | undefined { | ||
function tryGetThisTypeAt(node: Node, includeGlobalThis = true, container = getThisContainer(node, /*includeArrowFunctions*/ false)): Type | undefined { | ||
const isInJS = isInJSFile(node); | ||
if (isFunctionLike(container) && | ||
(!isInParameterInitializerBeforeContainingFunction(node) || getThisParameter(container))) { | ||
|
@@ -17046,6 +17055,16 @@ namespace ts { | |
return getFlowTypeOfReference(node, type); | ||
} | ||
} | ||
if (isSourceFile(container)) { | ||
// look up in the source file's locals or exports | ||
if (container.commonJsModuleIndicator) { | ||
const fileSymbol = getSymbolOfNode(container); | ||
return fileSymbol && getTypeOfSymbol(fileSymbol); | ||
} | ||
else if (includeGlobalThis) { | ||
return getTypeOfSymbol(globalThisSymbol); | ||
} | ||
} | ||
} | ||
|
||
function getClassNameFromPrototypeMethod(container: Node) { | ||
|
@@ -19343,6 +19362,12 @@ namespace ts { | |
if (isJSLiteralType(leftType)) { | ||
return anyType; | ||
} | ||
if (leftType.symbol === globalThisSymbol) { | ||
if (noImplicitAny) { | ||
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. Same here, but for |
||
error(right, Diagnostics.Element_implicitly_has_an_any_type_because_type_0_has_no_index_signature, typeToString(leftType)); | ||
} | ||
return anyType; | ||
} | ||
if (right.escapedText && !checkAndReportErrorForExtendingInterface(node)) { | ||
reportNonexistentProperty(right, leftType.flags & TypeFlags.TypeParameter && (leftType as TypeParameter).isThisType ? apparentType : leftType); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Rather than switching on
noImplicitThis
out here, shouldn't we useerrorOrSuggestion
(switching on noImplicitThis) instead oferror
so we get suggestions for these issues even whennoImplicitThis
is off?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.
Suggestions are only used for triggering codefixes, I think. And there aren't any codefixes for these errors. If both of these are true, then let's just wait until we have a codefix for them.
And I can't think of a good codefix for any of the errors except perhaps "The containing arrow function captures the global value of this", which would convert the arrow function to a function expression.