Skip to content

Commit 7cfa1df

Browse files
authored
Fix regression in mixin emit by removing unneeded line of code (#34715)
* Fix regression in mixin emit by removing unneeded line of code * Double the test, double the fun
1 parent d892fd4 commit 7cfa1df

File tree

5 files changed

+316
-2
lines changed

5 files changed

+316
-2
lines changed

src/compiler/checker.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4092,8 +4092,6 @@ namespace ts {
40924092
else if (context.flags & NodeBuilderFlags.WriteClassExpressionAsTypeLiteral &&
40934093
type.symbol.valueDeclaration &&
40944094
isClassLike(type.symbol.valueDeclaration) &&
4095-
// Use `import` types for refs to other scopes, only anonymize something defined in the same scope
4096-
findAncestor(type.symbol.valueDeclaration, d => d === getSourceFileOfNode(context.enclosingDeclaration)) &&
40974095
!isValueSymbolAccessible(type.symbol, context.enclosingDeclaration)
40984096
) {
40994097
return createAnonymousTypeNode(type);
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
//// [tests/cases/compiler/anonClassDeclarationEmitIsAnon.ts] ////
2+
3+
//// [wrapClass.ts]
4+
export function wrapClass(param: any) {
5+
return class Wrapped {
6+
foo() {
7+
return param;
8+
}
9+
}
10+
}
11+
12+
export type Constructor<T = {}> = new (...args: any[]) => T;
13+
14+
export function Timestamped<TBase extends Constructor>(Base: TBase) {
15+
return class extends Base {
16+
timestamp = Date.now();
17+
};
18+
}
19+
20+
//// [index.ts]
21+
import { wrapClass, Timestamped } from "./wrapClass";
22+
23+
export default wrapClass(0);
24+
25+
// Simple class
26+
export class User {
27+
name = '';
28+
}
29+
30+
// User that is Timestamped
31+
export class TimestampedUser extends Timestamped(User) {
32+
constructor() {
33+
super();
34+
}
35+
}
36+
37+
//// [wrapClass.js]
38+
"use strict";
39+
var __extends = (this && this.__extends) || (function () {
40+
var extendStatics = function (d, b) {
41+
extendStatics = Object.setPrototypeOf ||
42+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
43+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
44+
return extendStatics(d, b);
45+
};
46+
return function (d, b) {
47+
extendStatics(d, b);
48+
function __() { this.constructor = d; }
49+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
50+
};
51+
})();
52+
exports.__esModule = true;
53+
function wrapClass(param) {
54+
return /** @class */ (function () {
55+
function Wrapped() {
56+
}
57+
Wrapped.prototype.foo = function () {
58+
return param;
59+
};
60+
return Wrapped;
61+
}());
62+
}
63+
exports.wrapClass = wrapClass;
64+
function Timestamped(Base) {
65+
return /** @class */ (function (_super) {
66+
__extends(class_1, _super);
67+
function class_1() {
68+
var _this = _super !== null && _super.apply(this, arguments) || this;
69+
_this.timestamp = Date.now();
70+
return _this;
71+
}
72+
return class_1;
73+
}(Base));
74+
}
75+
exports.Timestamped = Timestamped;
76+
//// [index.js]
77+
"use strict";
78+
var __extends = (this && this.__extends) || (function () {
79+
var extendStatics = function (d, b) {
80+
extendStatics = Object.setPrototypeOf ||
81+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
82+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
83+
return extendStatics(d, b);
84+
};
85+
return function (d, b) {
86+
extendStatics(d, b);
87+
function __() { this.constructor = d; }
88+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
89+
};
90+
})();
91+
exports.__esModule = true;
92+
var wrapClass_1 = require("./wrapClass");
93+
exports["default"] = wrapClass_1.wrapClass(0);
94+
// Simple class
95+
var User = /** @class */ (function () {
96+
function User() {
97+
this.name = '';
98+
}
99+
return User;
100+
}());
101+
exports.User = User;
102+
// User that is Timestamped
103+
var TimestampedUser = /** @class */ (function (_super) {
104+
__extends(TimestampedUser, _super);
105+
function TimestampedUser() {
106+
return _super.call(this) || this;
107+
}
108+
return TimestampedUser;
109+
}(wrapClass_1.Timestamped(User)));
110+
exports.TimestampedUser = TimestampedUser;
111+
112+
113+
//// [wrapClass.d.ts]
114+
export declare function wrapClass(param: any): {
115+
new (): {
116+
foo(): any;
117+
};
118+
};
119+
export declare type Constructor<T = {}> = new (...args: any[]) => T;
120+
export declare function Timestamped<TBase extends Constructor>(Base: TBase): {
121+
new (...args: any[]): {
122+
timestamp: number;
123+
};
124+
} & TBase;
125+
//// [index.d.ts]
126+
declare const _default: {
127+
new (): {
128+
foo(): any;
129+
};
130+
};
131+
export default _default;
132+
export declare class User {
133+
name: string;
134+
}
135+
declare const TimestampedUser_base: {
136+
new (...args: any[]): {
137+
timestamp: number;
138+
};
139+
} & typeof User;
140+
export declare class TimestampedUser extends TimestampedUser_base {
141+
constructor();
142+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
=== tests/cases/compiler/wrapClass.ts ===
2+
export function wrapClass(param: any) {
3+
>wrapClass : Symbol(wrapClass, Decl(wrapClass.ts, 0, 0))
4+
>param : Symbol(param, Decl(wrapClass.ts, 0, 26))
5+
6+
return class Wrapped {
7+
>Wrapped : Symbol(Wrapped, Decl(wrapClass.ts, 1, 10))
8+
9+
foo() {
10+
>foo : Symbol(Wrapped.foo, Decl(wrapClass.ts, 1, 26))
11+
12+
return param;
13+
>param : Symbol(param, Decl(wrapClass.ts, 0, 26))
14+
}
15+
}
16+
}
17+
18+
export type Constructor<T = {}> = new (...args: any[]) => T;
19+
>Constructor : Symbol(Constructor, Decl(wrapClass.ts, 6, 1))
20+
>T : Symbol(T, Decl(wrapClass.ts, 8, 24))
21+
>args : Symbol(args, Decl(wrapClass.ts, 8, 39))
22+
>T : Symbol(T, Decl(wrapClass.ts, 8, 24))
23+
24+
export function Timestamped<TBase extends Constructor>(Base: TBase) {
25+
>Timestamped : Symbol(Timestamped, Decl(wrapClass.ts, 8, 60))
26+
>TBase : Symbol(TBase, Decl(wrapClass.ts, 10, 28))
27+
>Constructor : Symbol(Constructor, Decl(wrapClass.ts, 6, 1))
28+
>Base : Symbol(Base, Decl(wrapClass.ts, 10, 55))
29+
>TBase : Symbol(TBase, Decl(wrapClass.ts, 10, 28))
30+
31+
return class extends Base {
32+
>Base : Symbol(Base, Decl(wrapClass.ts, 10, 55))
33+
34+
timestamp = Date.now();
35+
>timestamp : Symbol((Anonymous class).timestamp, Decl(wrapClass.ts, 11, 31))
36+
>Date.now : Symbol(DateConstructor.now, Decl(lib.es5.d.ts, --, --))
37+
>Date : Symbol(Date, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.scripthost.d.ts, --, --))
38+
>now : Symbol(DateConstructor.now, Decl(lib.es5.d.ts, --, --))
39+
40+
};
41+
}
42+
43+
=== tests/cases/compiler/index.ts ===
44+
import { wrapClass, Timestamped } from "./wrapClass";
45+
>wrapClass : Symbol(wrapClass, Decl(index.ts, 0, 8))
46+
>Timestamped : Symbol(Timestamped, Decl(index.ts, 0, 19))
47+
48+
export default wrapClass(0);
49+
>wrapClass : Symbol(wrapClass, Decl(index.ts, 0, 8))
50+
51+
// Simple class
52+
export class User {
53+
>User : Symbol(User, Decl(index.ts, 2, 28))
54+
55+
name = '';
56+
>name : Symbol(User.name, Decl(index.ts, 5, 19))
57+
}
58+
59+
// User that is Timestamped
60+
export class TimestampedUser extends Timestamped(User) {
61+
>TimestampedUser : Symbol(TimestampedUser, Decl(index.ts, 7, 1))
62+
>Timestamped : Symbol(Timestamped, Decl(index.ts, 0, 19))
63+
>User : Symbol(User, Decl(index.ts, 2, 28))
64+
65+
constructor() {
66+
super();
67+
}
68+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
=== tests/cases/compiler/wrapClass.ts ===
2+
export function wrapClass(param: any) {
3+
>wrapClass : (param: any) => typeof Wrapped
4+
>param : any
5+
6+
return class Wrapped {
7+
>class Wrapped { foo() { return param; } } : typeof Wrapped
8+
>Wrapped : typeof Wrapped
9+
10+
foo() {
11+
>foo : () => any
12+
13+
return param;
14+
>param : any
15+
}
16+
}
17+
}
18+
19+
export type Constructor<T = {}> = new (...args: any[]) => T;
20+
>Constructor : Constructor<T>
21+
>args : any[]
22+
23+
export function Timestamped<TBase extends Constructor>(Base: TBase) {
24+
>Timestamped : <TBase extends Constructor<{}>>(Base: TBase) => { new (...args: any[]): (Anonymous class); prototype: Timestamped<any>.(Anonymous class); } & TBase
25+
>Base : TBase
26+
27+
return class extends Base {
28+
>class extends Base { timestamp = Date.now(); } : { new (...args: any[]): (Anonymous class); prototype: Timestamped<any>.(Anonymous class); } & TBase
29+
>Base : {}
30+
31+
timestamp = Date.now();
32+
>timestamp : number
33+
>Date.now() : number
34+
>Date.now : () => number
35+
>Date : DateConstructor
36+
>now : () => number
37+
38+
};
39+
}
40+
41+
=== tests/cases/compiler/index.ts ===
42+
import { wrapClass, Timestamped } from "./wrapClass";
43+
>wrapClass : (param: any) => typeof Wrapped
44+
>Timestamped : <TBase extends import("tests/cases/compiler/wrapClass").Constructor<{}>>(Base: TBase) => { new (...args: any[]): (Anonymous class); prototype: Timestamped<any>.(Anonymous class); } & TBase
45+
46+
export default wrapClass(0);
47+
>wrapClass(0) : typeof Wrapped
48+
>wrapClass : (param: any) => typeof Wrapped
49+
>0 : 0
50+
51+
// Simple class
52+
export class User {
53+
>User : User
54+
55+
name = '';
56+
>name : string
57+
>'' : ""
58+
}
59+
60+
// User that is Timestamped
61+
export class TimestampedUser extends Timestamped(User) {
62+
>TimestampedUser : TimestampedUser
63+
>Timestamped(User) : Timestamped<typeof User>.(Anonymous class) & User
64+
>Timestamped : <TBase extends import("tests/cases/compiler/wrapClass").Constructor<{}>>(Base: TBase) => { new (...args: any[]): (Anonymous class); prototype: Timestamped<any>.(Anonymous class); } & TBase
65+
>User : typeof User
66+
67+
constructor() {
68+
super();
69+
>super() : void
70+
>super : { new (...args: any[]): Timestamped<typeof User>.(Anonymous class); prototype: Timestamped<any>.(Anonymous class); } & typeof User
71+
}
72+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// @declaration: true
2+
// @filename: wrapClass.ts
3+
export function wrapClass(param: any) {
4+
return class Wrapped {
5+
foo() {
6+
return param;
7+
}
8+
}
9+
}
10+
11+
export type Constructor<T = {}> = new (...args: any[]) => T;
12+
13+
export function Timestamped<TBase extends Constructor>(Base: TBase) {
14+
return class extends Base {
15+
timestamp = Date.now();
16+
};
17+
}
18+
19+
// @filename: index.ts
20+
import { wrapClass, Timestamped } from "./wrapClass";
21+
22+
export default wrapClass(0);
23+
24+
// Simple class
25+
export class User {
26+
name = '';
27+
}
28+
29+
// User that is Timestamped
30+
export class TimestampedUser extends Timestamped(User) {
31+
constructor() {
32+
super();
33+
}
34+
}

0 commit comments

Comments
 (0)