-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Fix class name references #55262
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
Fix class name references #55262
Changes from all commits
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 | ||||
---|---|---|---|---|---|---|
|
@@ -846,7 +846,6 @@ import { | |||||
NodeCheckFlags, | ||||||
NodeFlags, | ||||||
nodeHasName, | ||||||
nodeIsDecorated, | ||||||
nodeIsMissing, | ||||||
nodeIsPresent, | ||||||
nodeIsSynthesized, | ||||||
|
@@ -1070,7 +1069,7 @@ import { | |||||
WhileStatement, | ||||||
WideningContext, | ||||||
WithStatement, | ||||||
YieldExpression, | ||||||
YieldExpression | ||||||
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.
Suggested change
Well, in any case, probably best to leave it until we can figure out what's happening. This is of course a nit (still looking at the PR) |
||||||
} from "./_namespaces/ts"; | ||||||
import * as moduleSpecifiers from "./_namespaces/ts.moduleSpecifiers"; | ||||||
import * as performance from "./_namespaces/ts.performance"; | ||||||
|
@@ -27964,38 +27963,20 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |||||
|
||||||
let declaration = localOrExportSymbol.valueDeclaration; | ||||||
if (declaration && localOrExportSymbol.flags & SymbolFlags.Class) { | ||||||
// Due to the emit for class decorators, any reference to the class from inside of the class body | ||||||
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 check my understanding: before, the _a reference was only generated for ClassExpressions and decorated classes. Now it happens for any class declaration/expression that references itself in its body. Is that correct? 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. We won't always generate the reference, but now we will always set the flag. It's just that the cases where we need the flag set were no longer restricted to the cases mentioned in the comment. |
||||||
// must instead be rewritten to point to a temporary variable to avoid issues with the double-bind | ||||||
// behavior of class names in ES6. | ||||||
if (declaration.kind === SyntaxKind.ClassDeclaration | ||||||
&& nodeIsDecorated(legacyDecorators, declaration as ClassDeclaration)) { | ||||||
let container = getContainingClass(node); | ||||||
while (container !== undefined) { | ||||||
if (container === declaration && container.name !== node) { | ||||||
getNodeLinks(declaration).flags |= NodeCheckFlags.ClassWithConstructorReference; | ||||||
getNodeLinks(node).flags |= NodeCheckFlags.ConstructorReferenceInClass; | ||||||
break; | ||||||
} | ||||||
|
||||||
container = getContainingClass(container); | ||||||
} | ||||||
} | ||||||
else if (declaration.kind === SyntaxKind.ClassExpression) { | ||||||
// When we emit a class expression with static members that contain a reference | ||||||
// to the constructor in the initializer, we will need to substitute that | ||||||
// binding with an alias as the class name is not in scope. | ||||||
// When we downlevel classes we may emit some code outside of the class body. Due to the fact the | ||||||
// class name is double-bound, we must ensure we mark references to the class name so that we can | ||||||
// emit an alias to the class later. | ||||||
if (isClassLike(declaration) && declaration.name !== node) { | ||||||
let container = getThisContainer(node, /*includeArrowFunctions*/ false, /*includeClassComputedPropertyName*/ false); | ||||||
while (container.kind !== SyntaxKind.SourceFile) { | ||||||
if (container.parent === declaration) { | ||||||
if (isPropertyDeclaration(container) && isStatic(container) || isClassStaticBlockDeclaration(container)) { | ||||||
getNodeLinks(declaration).flags |= NodeCheckFlags.ClassWithConstructorReference; | ||||||
getNodeLinks(node).flags |= NodeCheckFlags.ConstructorReferenceInClass; | ||||||
} | ||||||
break; | ||||||
} | ||||||
|
||||||
while (container.kind !== SyntaxKind.SourceFile && container.parent !== declaration) { | ||||||
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. as I read it, this 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. No, they are equivalent, but I feel this is a bit easier to read and reason over. 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. Agree, the new one is much easier to read. |
||||||
container = getThisContainer(container, /*includeArrowFunctions*/ false, /*includeClassComputedPropertyName*/ false); | ||||||
} | ||||||
|
||||||
if (container.kind !== SyntaxKind.SourceFile) { | ||||||
getNodeLinks(declaration).flags |= NodeCheckFlags.ContainsConstructorReference; | ||||||
getNodeLinks(container).flags |= NodeCheckFlags.ContainsConstructorReference; | ||||||
getNodeLinks(node).flags |= NodeCheckFlags.ConstructorReference; | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6022,15 +6022,13 @@ export const enum NodeCheckFlags { | |
ContainsCapturedBlockScopeBinding = 1 << 13, // Part of a loop that contains block scoped variable captured in closure | ||
CapturedBlockScopedBinding = 1 << 14, // Block-scoped binding that is captured in some function | ||
BlockScopedBindingInLoop = 1 << 15, // Block-scoped binding with declaration nested inside iteration statement | ||
ClassWithBodyScopedClassBinding = 1 << 16, // Decorated class that contains a binding to itself inside of the class body. | ||
BodyScopedClassBinding = 1 << 17, // Binding to a decorated class inside of the class's body. | ||
Comment on lines
-6025
to
-6026
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. Should we shift the other bits down or is it no big deal to leave them empty until later? 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. this is a driveby cleanup, right? I don't see any removal of BodyScopedClassBinding in the rest of the PR. 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. All references to |
||
NeedsLoopOutParameter = 1 << 18, // Block scoped binding whose value should be explicitly copied outside of the converted loop | ||
AssignmentsMarked = 1 << 19, // Parameter assignments have been marked | ||
ClassWithConstructorReference = 1 << 20, // Class that contains a binding to its constructor inside of the class body. | ||
ConstructorReferenceInClass = 1 << 21, // Binding to a class constructor inside of the class's body. | ||
ContainsClassWithPrivateIdentifiers = 1 << 22, // Marked on all block-scoped containers containing a class with private identifiers. | ||
ContainsSuperPropertyInStaticInitializer = 1 << 23, // Marked on all block-scoped containers containing a static initializer with 'super.x' or 'super[x]'. | ||
InCheckIdentifier = 1 << 24, | ||
NeedsLoopOutParameter = 1 << 16, // Block scoped binding whose value should be explicitly copied outside of the converted loop | ||
AssignmentsMarked = 1 << 17, // Parameter assignments have been marked | ||
ContainsConstructorReference = 1 << 18, // Class or class element that contains a binding that references the class constructor. | ||
ConstructorReference = 1 << 29, // Binding to a class constructor inside of the class's body. | ||
ContainsClassWithPrivateIdentifiers = 1 << 20, // Marked on all block-scoped containers containing a class with private identifiers. | ||
ContainsSuperPropertyInStaticInitializer = 1 << 21, // Marked on all block-scoped containers containing a static initializer with 'super.x' or 'super[x]'. | ||
InCheckIdentifier = 1 << 22, | ||
} | ||
|
||
/** @internal */ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//// [tests/cases/compiler/classNameReferencesInStaticElements.ts] //// | ||
|
||
//// [classNameReferencesInStaticElements.ts] | ||
// https://github.com/microsoft/TypeScript/issues/54607 | ||
class Foo { | ||
static { console.log(this, Foo) } | ||
static x = () => { console.log(this, Foo) } | ||
static y = function(this: unknown) { console.log(this, Foo) } | ||
|
||
#x() { console.log(Foo); } | ||
x() { this.#x(); } | ||
} | ||
|
||
const oldFoo = Foo; | ||
(Foo as any) = null; | ||
oldFoo.x(); | ||
oldFoo.y(); | ||
new oldFoo().x(); | ||
|
||
//// [classNameReferencesInStaticElements.js] | ||
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { | ||
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); | ||
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); | ||
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); | ||
}; | ||
var _Foo_instances, _a, _Foo_x; | ||
// https://github.com/microsoft/TypeScript/issues/54607 | ||
class Foo { | ||
constructor() { | ||
_Foo_instances.add(this); | ||
} | ||
x() { __classPrivateFieldGet(this, _Foo_instances, "m", _Foo_x).call(this); } | ||
} | ||
_a = Foo, _Foo_instances = new WeakSet(), _Foo_x = function _Foo_x() { console.log(_a); }; | ||
(() => { | ||
console.log(_a, _a); | ||
})(); | ||
Foo.x = () => { console.log(_a, _a); }; | ||
Foo.y = function () { console.log(this, _a); }; | ||
const oldFoo = Foo; | ||
Foo = null; | ||
oldFoo.x(); | ||
oldFoo.y(); | ||
new oldFoo().x(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,5 +24,5 @@ let Testing123 = Testing123_1 = class Testing123 { | |
exports.Testing123 = Testing123; | ||
Testing123.prop1 = Testing123_1.prop0; | ||
exports.Testing123 = Testing123 = Testing123_1 = __decorate([ | ||
Something({ v: () => Testing123_1 }) | ||
Something({ v: () => Testing123 }) | ||
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 don't understand how this diff happened. I assume it's because of the first block in the checker getting deleted, but I thought the edit to the second block would cause the reference to still get generated as Testing123_1. 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.
|
||
], Testing123); |
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.
Do you know what you ran which did this? Was it organize imports? Something else? Hoping to get to the bottom of these so we can be consistent (or, I guess formatting will prevent that).
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.
Probably organize imports.
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.
Hm, just tested running organize on
checker.ts
and this didn't happen, even when reordering things, really odd.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 it was a result of running Organize Imports to remove an unused import. I just checked locally and that does remove the trailing
,
.