Skip to content

Don't latebind global JS property assignments #61593

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3432,6 +3432,9 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
bindExportsPropertyAssignment(node as BindableStaticPropertyAssignmentExpression);
}
else if (hasDynamicName(node)) {
if (!parentSymbol) {
return;
}
Comment on lines 3434 to +3437
Copy link
Preview

Copilot AI Apr 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The added check prevents binding for dynamic property assignments when no parent symbol is present, avoiding unintended symbol creation in global files. Consider adding an inline comment here to clarify why binding is skipped in this scenario for future maintainability.

Suggested change
else if (hasDynamicName(node)) {
if (!parentSymbol) {
return;
}
else if (hasDynamicName(node)) {
// Skip binding for dynamic property assignments when no parent symbol is present.
// This prevents unintended symbol creation in global files, ensuring correctness.
if (!parentSymbol) {
return;
}

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really agree, but I would change my mind if a human spoke up saying that they do.

bindAnonymousDeclaration(node, SymbolFlags.Property | SymbolFlags.Assignment, InternalSymbolName.Computed);
const sym = bindPotentiallyMissingNamespaces(parentSymbol, node.left.expression, isTopLevelNamespaceAssignment(node.left), /*isPrototypeProperty*/ false, /*containerIsClass*/ false);
addLateBoundAssignmentDeclarationToSymbol(node, sym);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//// [tests/cases/conformance/salsa/lateboundWindowToplevelAssignment.ts] ////

=== lateboundWindowToplevelAssignment.js ===
const UPDATE_MARKER_FUNC = 'updateMarkerPosition';
>UPDATE_MARKER_FUNC : Symbol(UPDATE_MARKER_FUNC, Decl(lateboundWindowToplevelAssignment.js, 0, 5))

window[UPDATE_MARKER_FUNC] = () => {};
>window : Symbol(window, Decl(lib.dom.d.ts, --, --))
>UPDATE_MARKER_FUNC : Symbol(UPDATE_MARKER_FUNC, Decl(lateboundWindowToplevelAssignment.js, 0, 5))

window[UPDATE_MARKER_FUNC]();
>window : Symbol(window, Decl(lib.dom.d.ts, --, --))
>UPDATE_MARKER_FUNC : Symbol(UPDATE_MARKER_FUNC, Decl(lateboundWindowToplevelAssignment.js, 0, 5))

28 changes: 28 additions & 0 deletions tests/baselines/reference/lateboundWindowToplevelAssignment.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [tests/cases/conformance/salsa/lateboundWindowToplevelAssignment.ts] ////

=== lateboundWindowToplevelAssignment.js ===
const UPDATE_MARKER_FUNC = 'updateMarkerPosition';
>UPDATE_MARKER_FUNC : "updateMarkerPosition"
> : ^^^^^^^^^^^^^^^^^^^^^^
>'updateMarkerPosition' : "updateMarkerPosition"
> : ^^^^^^^^^^^^^^^^^^^^^^

window[UPDATE_MARKER_FUNC] = () => {};
>window[UPDATE_MARKER_FUNC] = () => {} : () => void
> : ^^^^^^^^^^
>window[UPDATE_MARKER_FUNC] : error
>window : Window & typeof globalThis
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^
>UPDATE_MARKER_FUNC : "updateMarkerPosition"
> : ^^^^^^^^^^^^^^^^^^^^^^
>() => {} : () => void
> : ^^^^^^^^^^

window[UPDATE_MARKER_FUNC]();
>window[UPDATE_MARKER_FUNC]() : error
>window[UPDATE_MARKER_FUNC] : error
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having type error shows that binding didn't happen.

>window : Window & typeof globalThis
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^
>UPDATE_MARKER_FUNC : "updateMarkerPosition"
> : ^^^^^^^^^^^^^^^^^^^^^^

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @filename: lateboundWindowToplevelAssignment.js
// @allowJs: true
// @checkJs: true
// @noEmit: true
const UPDATE_MARKER_FUNC = 'updateMarkerPosition';
window[UPDATE_MARKER_FUNC] = () => {};
window[UPDATE_MARKER_FUNC]();