Skip to content
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

Fixed JS emit when downleveling class static blocks on anonymous default-exported class #60886

Open
wants to merge 1 commit 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
6 changes: 3 additions & 3 deletions src/compiler/transformers/classFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1911,9 +1911,9 @@ export function transformClassFields(context: TransformationContext): (x: Source
// HasLexicalDeclaration (N) : Determines if the argument identifier has a binding in this environment record that was created using
// a lexical declaration such as a LexicalDeclaration or a ClassDeclaration.

const staticProperties = getStaticPropertiesAndClassStaticBlock(node);
if (some(staticProperties)) {
addPropertyOrClassStaticBlockStatements(statements, staticProperties, factory.getInternalName(node));
const staticDeclarations = getStaticPropertiesAndClassStaticBlock(node);
if (some(staticDeclarations)) {
addPropertyOrClassStaticBlockStatements(statements, staticDeclarations, factory.getInternalName(node));
}
}

Expand Down
17 changes: 10 additions & 7 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import {
isCatchClause,
isClassElement,
isClassLike,
isClassStaticBlockDeclaration,
isComputedPropertyName,
isDecorator,
isElementAccessExpression,
Expand Down Expand Up @@ -221,10 +222,11 @@ const enum ClassFacts {
HasStaticInitializedProperties = 1 << 0,
HasClassOrConstructorParameterDecorators = 1 << 1,
HasMemberDecorators = 1 << 2,
IsExportOfNamespace = 1 << 3,
IsNamedExternalExport = 1 << 4,
IsDefaultExternalExport = 1 << 5,
IsDerivedClass = 1 << 6,
HasStaticBlocks = 1 << 3,
IsExportOfNamespace = 1 << 4,
IsNamedExternalExport = 1 << 5,
IsDefaultExternalExport = 1 << 6,
IsDerivedClass = 1 << 7,

HasAnyDecorators = HasClassOrConstructorParameterDecorators | HasMemberDecorators,
MayNeedImmediatelyInvokedFunctionExpression = HasAnyDecorators | HasStaticInitializedProperties,
Expand Down Expand Up @@ -856,6 +858,7 @@ export function transformTypeScript(context: TransformationContext): Transformer
function getClassFacts(node: ClassDeclaration) {
let facts = ClassFacts.None;
if (some(getProperties(node, /*requireInitializer*/ true, /*isStatic*/ true))) facts |= ClassFacts.HasStaticInitializedProperties;
if (node.transformFlags & TransformFlags.ContainsClassFields && some(node.members, isClassStaticBlockDeclaration)) facts |= ClassFacts.HasStaticBlocks;
const extendsClauseElement = getEffectiveBaseTypeNode(node);
if (extendsClauseElement && skipOuterExpressions(extendsClauseElement.expression).kind !== SyntaxKind.NullKeyword) facts |= ClassFacts.IsDerivedClass;
if (classOrConstructorParameterIsDecorated(legacyDecorators, node)) facts |= ClassFacts.HasClassOrConstructorParameterDecorators;
Expand Down Expand Up @@ -885,7 +888,8 @@ export function transformTypeScript(context: TransformationContext): Transformer
if (
!isClassLikeDeclarationWithTypeScriptSyntax(node) &&
!classOrConstructorParameterIsDecorated(legacyDecorators, node) &&
!isExportOfNamespace(node)
!isExportOfNamespace(node) &&
!(facts & ClassFacts.HasStaticBlocks && languageVersion < ScriptTarget.ES2022)
) {
return factory.updateClassDeclaration(
node,
Expand Down Expand Up @@ -916,8 +920,7 @@ export function transformTypeScript(context: TransformationContext): Transformer
}

const needsName = moveModifiers && !node.name ||
facts & ClassFacts.HasMemberDecorators ||
facts & ClassFacts.HasStaticInitializedProperties;
facts & (ClassFacts.HasMemberDecorators | ClassFacts.HasStaticInitializedProperties | ClassFacts.HasStaticBlocks);

const name = needsName ?
node.name ?? factory.getGeneratedNameForNode(node) :
Expand Down
41 changes: 41 additions & 0 deletions tests/baselines/reference/classStaticBlock29(target=es2015).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//// [tests/cases/conformance/classes/classStaticBlock/classStaticBlock29.ts] ////

//// [classStaticBlock29.ts]
// https://github.com/microsoft/TypeScript/issues/60879

export default class {
static demo: number;
static {
this.demo = 1;
}
}

export function makeCls() {
return class {
static demo: number;
static {
this.demo = 1;
}
};
}


//// [classStaticBlock29.js]
// https://github.com/microsoft/TypeScript/issues/60879
var _a;
class default_1 {
}
_a = default_1;
(() => {
_a.demo = 1;
})();
export default default_1;
export function makeCls() {
var _b;
return _b = class {
},
(() => {
_b.demo = 1;
})(),
_b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//// [tests/cases/conformance/classes/classStaticBlock/classStaticBlock29.ts] ////

=== classStaticBlock29.ts ===
// https://github.com/microsoft/TypeScript/issues/60879

export default class {
static demo: number;
>demo : Symbol(default.demo, Decl(classStaticBlock29.ts, 2, 22))

static {
this.demo = 1;
>this.demo : Symbol(default.demo, Decl(classStaticBlock29.ts, 2, 22))
>this : Symbol(default, Decl(classStaticBlock29.ts, 0, 0))
>demo : Symbol(default.demo, Decl(classStaticBlock29.ts, 2, 22))
}
}

export function makeCls() {
>makeCls : Symbol(makeCls, Decl(classStaticBlock29.ts, 7, 1))

return class {
static demo: number;
>demo : Symbol((Anonymous class).demo, Decl(classStaticBlock29.ts, 10, 16))

static {
this.demo = 1;
>this.demo : Symbol((Anonymous class).demo, Decl(classStaticBlock29.ts, 10, 16))
>this : Symbol((Anonymous class), Decl(classStaticBlock29.ts, 10, 8))
>demo : Symbol((Anonymous class).demo, Decl(classStaticBlock29.ts, 10, 16))
}
};
}

53 changes: 53 additions & 0 deletions tests/baselines/reference/classStaticBlock29(target=es2015).types
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//// [tests/cases/conformance/classes/classStaticBlock/classStaticBlock29.ts] ////

=== classStaticBlock29.ts ===
// https://github.com/microsoft/TypeScript/issues/60879

export default class {
static demo: number;
>demo : number
> : ^^^^^^

static {
this.demo = 1;
>this.demo = 1 : 1
> : ^
>this.demo : number
> : ^^^^^^
>this : typeof default
> : ^^^^^^^^^^^^^^
>demo : number
> : ^^^^^^
>1 : 1
> : ^
}
}

export function makeCls() {
>makeCls : () => typeof (Anonymous class)
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

return class {
>class { static demo: number; static { this.demo = 1; } } : typeof (Anonymous class)
> : ^^^^^^^^^^^^^^^^^^^^^^^^

static demo: number;
>demo : number
> : ^^^^^^

static {
this.demo = 1;
>this.demo = 1 : 1
> : ^
>this.demo : number
> : ^^^^^^
>this : typeof (Anonymous class)
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>demo : number
> : ^^^^^^
>1 : 1
> : ^
}
};
}

38 changes: 38 additions & 0 deletions tests/baselines/reference/classStaticBlock29(target=es2022).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//// [tests/cases/conformance/classes/classStaticBlock/classStaticBlock29.ts] ////

//// [classStaticBlock29.ts]
// https://github.com/microsoft/TypeScript/issues/60879

export default class {
static demo: number;
static {
this.demo = 1;
}
}

export function makeCls() {
return class {
static demo: number;
static {
this.demo = 1;
}
};
}


//// [classStaticBlock29.js]
// https://github.com/microsoft/TypeScript/issues/60879
export default class {
static demo;
static {
this.demo = 1;
}
}
export function makeCls() {
return class {
static demo;
static {
this.demo = 1;
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//// [tests/cases/conformance/classes/classStaticBlock/classStaticBlock29.ts] ////

=== classStaticBlock29.ts ===
// https://github.com/microsoft/TypeScript/issues/60879

export default class {
static demo: number;
>demo : Symbol(default.demo, Decl(classStaticBlock29.ts, 2, 22))

static {
this.demo = 1;
>this.demo : Symbol(default.demo, Decl(classStaticBlock29.ts, 2, 22))
>this : Symbol(default, Decl(classStaticBlock29.ts, 0, 0))
>demo : Symbol(default.demo, Decl(classStaticBlock29.ts, 2, 22))
}
}

export function makeCls() {
>makeCls : Symbol(makeCls, Decl(classStaticBlock29.ts, 7, 1))

return class {
static demo: number;
>demo : Symbol((Anonymous class).demo, Decl(classStaticBlock29.ts, 10, 16))

static {
this.demo = 1;
>this.demo : Symbol((Anonymous class).demo, Decl(classStaticBlock29.ts, 10, 16))
>this : Symbol((Anonymous class), Decl(classStaticBlock29.ts, 10, 8))
>demo : Symbol((Anonymous class).demo, Decl(classStaticBlock29.ts, 10, 16))
}
};
}

53 changes: 53 additions & 0 deletions tests/baselines/reference/classStaticBlock29(target=es2022).types
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//// [tests/cases/conformance/classes/classStaticBlock/classStaticBlock29.ts] ////

=== classStaticBlock29.ts ===
// https://github.com/microsoft/TypeScript/issues/60879

export default class {
static demo: number;
>demo : number
> : ^^^^^^

static {
this.demo = 1;
>this.demo = 1 : 1
> : ^
>this.demo : number
> : ^^^^^^
>this : typeof default
> : ^^^^^^^^^^^^^^
>demo : number
> : ^^^^^^
>1 : 1
> : ^
}
}

export function makeCls() {
>makeCls : () => typeof (Anonymous class)
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

return class {
>class { static demo: number; static { this.demo = 1; } } : typeof (Anonymous class)
> : ^^^^^^^^^^^^^^^^^^^^^^^^

static demo: number;
>demo : number
> : ^^^^^^

static {
this.demo = 1;
>this.demo = 1 : 1
> : ^
>this.demo : number
> : ^^^^^^
>this : typeof (Anonymous class)
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>demo : number
> : ^^^^^^
>1 : 1
> : ^
}
};
}

50 changes: 50 additions & 0 deletions tests/baselines/reference/classStaticBlock29(target=es5).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//// [tests/cases/conformance/classes/classStaticBlock/classStaticBlock29.ts] ////

//// [classStaticBlock29.ts]
// https://github.com/microsoft/TypeScript/issues/60879

export default class {
static demo: number;
static {
this.demo = 1;
}
}

export function makeCls() {
return class {
static demo: number;
static {
this.demo = 1;
}
};
}


//// [classStaticBlock29.js]
"use strict";
// https://github.com/microsoft/TypeScript/issues/60879
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeCls = makeCls;
var default_1 = /** @class */ (function () {
function default_1() {
}
return default_1;
}());
_a = default_1;
(function () {
_a.demo = 1;
})();
exports.default = default_1;
function makeCls() {
var _b;
return _b = /** @class */ (function () {
function class_1() {
}
return class_1;
}()),
(function () {
_b.demo = 1;
})(),
_b;
}
Loading
Loading