Skip to content

Commit ce63935

Browse files
authored
Serialize inaccessible class declarations the same as class expressions in declaration emit (microsoft#49440)
1 parent 86ff3f6 commit ce63935

11 files changed

+271
-49
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5221,7 +5221,7 @@ namespace ts {
52215221
// Always use 'typeof T' for type of class, enum, and module objects
52225222
else if (symbol.flags & SymbolFlags.Class
52235223
&& !getBaseTypeVariableOfClass(symbol)
5224-
&& !(symbol.valueDeclaration && symbol.valueDeclaration.kind === SyntaxKind.ClassExpression && context.flags & NodeBuilderFlags.WriteClassExpressionAsTypeLiteral) ||
5224+
&& !(symbol.valueDeclaration && isClassLike(symbol.valueDeclaration) && context.flags & NodeBuilderFlags.WriteClassExpressionAsTypeLiteral && (!isClassDeclaration(symbol.valueDeclaration) || isSymbolAccessible(symbol, context.enclosingDeclaration, isInstanceType, /*computeAliases*/ false).accessibility !== SymbolAccessibility.Accessible)) ||
52255225
symbol.flags & (SymbolFlags.Enum | SymbolFlags.ValueModule) ||
52265226
shouldWriteTypeOfFunctionSymbol()) {
52275227
return symbolToTypeNode(symbol, context, isInstanceType);
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//// [declarationEmitClassMixinLocalClassDeclaration.ts]
2+
export type AnyFunction<Result = any> = (...input: any[]) => Result
3+
4+
export type AnyConstructor<Instance extends object = object, Static extends object = object> =
5+
(new (...input: any[]) => Instance) & Static
6+
7+
8+
type MixinHelperFunc = <A extends AnyConstructor, T>(required: [A], arg: T) => T extends AnyFunction<infer M> ? M : never
9+
10+
11+
export const Mixin: MixinHelperFunc = null as any
12+
13+
14+
export class Base {}
15+
16+
17+
export class XmlElement2 extends Mixin(
18+
[Base],
19+
(base: AnyConstructor<Base, typeof Base>) => {
20+
class XmlElement2 extends base {
21+
num: number = 0
22+
}
23+
return XmlElement2;
24+
}) { }
25+
26+
27+
//// [declarationEmitClassMixinLocalClassDeclaration.js]
28+
"use strict";
29+
var __extends = (this && this.__extends) || (function () {
30+
var extendStatics = function (d, b) {
31+
extendStatics = Object.setPrototypeOf ||
32+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
33+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
34+
return extendStatics(d, b);
35+
};
36+
return function (d, b) {
37+
if (typeof b !== "function" && b !== null)
38+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
39+
extendStatics(d, b);
40+
function __() { this.constructor = d; }
41+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
42+
};
43+
})();
44+
exports.__esModule = true;
45+
exports.XmlElement2 = exports.Base = exports.Mixin = void 0;
46+
exports.Mixin = null;
47+
var Base = /** @class */ (function () {
48+
function Base() {
49+
}
50+
return Base;
51+
}());
52+
exports.Base = Base;
53+
var XmlElement2 = /** @class */ (function (_super) {
54+
__extends(XmlElement2, _super);
55+
function XmlElement2() {
56+
return _super !== null && _super.apply(this, arguments) || this;
57+
}
58+
return XmlElement2;
59+
}((0, exports.Mixin)([Base], function (base) {
60+
var XmlElement2 = /** @class */ (function (_super) {
61+
__extends(XmlElement2, _super);
62+
function XmlElement2() {
63+
var _this = _super !== null && _super.apply(this, arguments) || this;
64+
_this.num = 0;
65+
return _this;
66+
}
67+
return XmlElement2;
68+
}(base));
69+
return XmlElement2;
70+
})));
71+
exports.XmlElement2 = XmlElement2;
72+
73+
74+
//// [declarationEmitClassMixinLocalClassDeclaration.d.ts]
75+
export declare type AnyFunction<Result = any> = (...input: any[]) => Result;
76+
export declare type AnyConstructor<Instance extends object = object, Static extends object = object> = (new (...input: any[]) => Instance) & Static;
77+
declare type MixinHelperFunc = <A extends AnyConstructor, T>(required: [A], arg: T) => T extends AnyFunction<infer M> ? M : never;
78+
export declare const Mixin: MixinHelperFunc;
79+
export declare class Base {
80+
}
81+
declare const XmlElement2_base: {
82+
new (): {
83+
num: number;
84+
};
85+
};
86+
export declare class XmlElement2 extends XmlElement2_base {
87+
}
88+
export {};
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
=== tests/cases/compiler/declarationEmitClassMixinLocalClassDeclaration.ts ===
2+
export type AnyFunction<Result = any> = (...input: any[]) => Result
3+
>AnyFunction : Symbol(AnyFunction, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 0, 0))
4+
>Result : Symbol(Result, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 0, 24))
5+
>input : Symbol(input, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 0, 41))
6+
>Result : Symbol(Result, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 0, 24))
7+
8+
export type AnyConstructor<Instance extends object = object, Static extends object = object> =
9+
>AnyConstructor : Symbol(AnyConstructor, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 0, 67))
10+
>Instance : Symbol(Instance, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 2, 27))
11+
>Static : Symbol(Static, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 2, 60))
12+
13+
(new (...input: any[]) => Instance) & Static
14+
>input : Symbol(input, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 3, 10))
15+
>Instance : Symbol(Instance, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 2, 27))
16+
>Static : Symbol(Static, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 2, 60))
17+
18+
19+
type MixinHelperFunc = <A extends AnyConstructor, T>(required: [A], arg: T) => T extends AnyFunction<infer M> ? M : never
20+
>MixinHelperFunc : Symbol(MixinHelperFunc, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 3, 48))
21+
>A : Symbol(A, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 6, 24))
22+
>AnyConstructor : Symbol(AnyConstructor, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 0, 67))
23+
>T : Symbol(T, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 6, 49))
24+
>required : Symbol(required, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 6, 53))
25+
>A : Symbol(A, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 6, 24))
26+
>arg : Symbol(arg, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 6, 67))
27+
>T : Symbol(T, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 6, 49))
28+
>T : Symbol(T, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 6, 49))
29+
>AnyFunction : Symbol(AnyFunction, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 0, 0))
30+
>M : Symbol(M, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 6, 106))
31+
>M : Symbol(M, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 6, 106))
32+
33+
34+
export const Mixin: MixinHelperFunc = null as any
35+
>Mixin : Symbol(Mixin, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 9, 12))
36+
>MixinHelperFunc : Symbol(MixinHelperFunc, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 3, 48))
37+
38+
39+
export class Base {}
40+
>Base : Symbol(Base, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 9, 49))
41+
42+
43+
export class XmlElement2 extends Mixin(
44+
>XmlElement2 : Symbol(XmlElement2, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 12, 20))
45+
>Mixin : Symbol(Mixin, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 9, 12))
46+
47+
[Base],
48+
>Base : Symbol(Base, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 9, 49))
49+
50+
(base: AnyConstructor<Base, typeof Base>) => {
51+
>base : Symbol(base, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 17, 5))
52+
>AnyConstructor : Symbol(AnyConstructor, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 0, 67))
53+
>Base : Symbol(Base, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 9, 49))
54+
>Base : Symbol(Base, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 9, 49))
55+
56+
class XmlElement2 extends base {
57+
>XmlElement2 : Symbol(XmlElement2, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 17, 50))
58+
>base : Symbol(base, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 17, 5))
59+
60+
num: number = 0
61+
>num : Symbol(XmlElement2.num, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 18, 40))
62+
}
63+
return XmlElement2;
64+
>XmlElement2 : Symbol(XmlElement2, Decl(declarationEmitClassMixinLocalClassDeclaration.ts, 17, 50))
65+
66+
}) { }
67+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
=== tests/cases/compiler/declarationEmitClassMixinLocalClassDeclaration.ts ===
2+
export type AnyFunction<Result = any> = (...input: any[]) => Result
3+
>AnyFunction : AnyFunction<Result>
4+
>input : any[]
5+
6+
export type AnyConstructor<Instance extends object = object, Static extends object = object> =
7+
>AnyConstructor : AnyConstructor<Instance, Static>
8+
9+
(new (...input: any[]) => Instance) & Static
10+
>input : any[]
11+
12+
13+
type MixinHelperFunc = <A extends AnyConstructor, T>(required: [A], arg: T) => T extends AnyFunction<infer M> ? M : never
14+
>MixinHelperFunc : <A extends AnyConstructor<object, object>, T>(required: [A], arg: T) => T extends AnyFunction<infer M> ? M : never
15+
>required : [A]
16+
>arg : T
17+
18+
19+
export const Mixin: MixinHelperFunc = null as any
20+
>Mixin : MixinHelperFunc
21+
>null as any : any
22+
>null : null
23+
24+
25+
export class Base {}
26+
>Base : Base
27+
28+
29+
export class XmlElement2 extends Mixin(
30+
>XmlElement2 : XmlElement2
31+
>Mixin( [Base], (base: AnyConstructor<Base, typeof Base>) => { class XmlElement2 extends base { num: number = 0 } return XmlElement2; }) : XmlElement2
32+
>Mixin : MixinHelperFunc
33+
34+
[Base],
35+
>[Base] : [typeof Base]
36+
>Base : typeof Base
37+
38+
(base: AnyConstructor<Base, typeof Base>) => {
39+
>(base: AnyConstructor<Base, typeof Base>) => { class XmlElement2 extends base { num: number = 0 } return XmlElement2; } : (base: AnyConstructor<Base, typeof Base>) => typeof XmlElement2
40+
>base : AnyConstructor<Base, typeof Base>
41+
>Base : typeof Base
42+
43+
class XmlElement2 extends base {
44+
>XmlElement2 : XmlElement2
45+
>base : Base
46+
47+
num: number = 0
48+
>num : number
49+
>0 : 0
50+
}
51+
return XmlElement2;
52+
>XmlElement2 : typeof XmlElement2
53+
54+
}) { }
55+

tests/baselines/reference/jsDeclarationsParameterTagReusesInputNodeInEmit1.errors.txt

Lines changed: 0 additions & 44 deletions
This file was deleted.

tests/baselines/reference/jsDeclarationsParameterTagReusesInputNodeInEmit1.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,35 @@ declare namespace BaseFactory {
6767
}
6868
declare class Base {
6969
}
70+
//// [file.d.ts]
71+
type couldntThinkOfAny = {
72+
(): {};
73+
Base: {
74+
new (): {};
75+
};
76+
};
77+
/** @typedef {import('./base')} BaseFactory */
78+
/**
79+
* @callback BaseFactoryFactory
80+
* @param {import('./base')} factory
81+
*/
82+
/** @enum {import('./base')} */
83+
declare const couldntThinkOfAny: {};
84+
/**
85+
*
86+
* @param {InstanceType<BaseFactory["Base"]>} base
87+
* @returns {InstanceType<BaseFactory["Base"]>}
88+
*/
89+
declare function test(base: InstanceType<BaseFactory["Base"]>): InstanceType<BaseFactory["Base"]>;
90+
type BaseFactory = {
91+
(): {};
92+
Base: {
93+
new (): {};
94+
};
95+
};
96+
type BaseFactoryFactory = (factory: {
97+
(): {};
98+
Base: {
99+
new (): {};
100+
};
101+
}) => any;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// @declaration: true
2+
export type AnyFunction<Result = any> = (...input: any[]) => Result
3+
4+
export type AnyConstructor<Instance extends object = object, Static extends object = object> =
5+
(new (...input: any[]) => Instance) & Static
6+
7+
8+
type MixinHelperFunc = <A extends AnyConstructor, T>(required: [A], arg: T) => T extends AnyFunction<infer M> ? M : never
9+
10+
11+
export const Mixin: MixinHelperFunc = null as any
12+
13+
14+
export class Base {}
15+
16+
17+
export class XmlElement2 extends Mixin(
18+
[Base],
19+
(base: AnyConstructor<Base, typeof Base>) => {
20+
class XmlElement2 extends base {
21+
num: number = 0
22+
}
23+
return XmlElement2;
24+
}) { }

tests/cases/fourslash/getDeclarationDiagnostics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// @Filename: inputFile1.ts
77
//// module m {
88
//// export function foo() {
9-
//// class C implements I { }
9+
//// class C implements I { private a; }
1010
//// interface I { }
1111
//// return C;
1212
//// }

tests/cases/fourslash/getSemanticDiagnosticForDeclaration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// @declaration: true
55
//// export function /*1*/foo/*2*/() {
66
//// interface privateInterface {}
7-
//// class Bar implements privateInterface { }
7+
//// class Bar implements privateInterface { private a; }
88
//// return Bar;
99
//// }
1010

tests/cases/fourslash/shims-pp/getSemanticDiagnostics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// @declaration: true
55
//// export function /*1*/foo/*2*/() {
66
//// interface privateInterface {}
7-
//// class Bar implements privateInterface { }
7+
//// class Bar implements privateInterface { private a; }
88
//// return Bar;
99
//// }
1010

0 commit comments

Comments
 (0)