From e77024915e2aa7d50696511575f20d1f88243ec1 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Thu, 18 May 2017 09:11:26 -0700 Subject: [PATCH] Test:decl emit for class expressions as type lits Add test and update baselines --- ...arationEmitExpressionInExtends4.errors.txt | 8 +- .../emitClassExpressionInDeclarationFile.js | 148 ++++++++++++++++++ ...itClassExpressionInDeclarationFile.symbols | 84 ++++++++++ ...emitClassExpressionInDeclarationFile.types | 91 +++++++++++ .../emitClassExpressionInDeclarationFile.ts | 30 ++++ ...findAllRefsForVariableInExtendsClause01.ts | 2 +- tests/cases/fourslash/renameJsExports02.ts | 4 +- 7 files changed, 357 insertions(+), 10 deletions(-) create mode 100644 tests/baselines/reference/emitClassExpressionInDeclarationFile.js create mode 100644 tests/baselines/reference/emitClassExpressionInDeclarationFile.symbols create mode 100644 tests/baselines/reference/emitClassExpressionInDeclarationFile.types create mode 100644 tests/cases/compiler/emitClassExpressionInDeclarationFile.ts diff --git a/tests/baselines/reference/declarationEmitExpressionInExtends4.errors.txt b/tests/baselines/reference/declarationEmitExpressionInExtends4.errors.txt index 54905212836..569e5aa355f 100644 --- a/tests/baselines/reference/declarationEmitExpressionInExtends4.errors.txt +++ b/tests/baselines/reference/declarationEmitExpressionInExtends4.errors.txt @@ -1,23 +1,17 @@ -tests/cases/compiler/declarationEmitExpressionInExtends4.ts(1,10): error TS4060: Return type of exported function has or is using private name 'D'. tests/cases/compiler/declarationEmitExpressionInExtends4.ts(5,17): error TS2315: Type 'D' is not generic. -tests/cases/compiler/declarationEmitExpressionInExtends4.ts(5,17): error TS4020: 'extends' clause of exported class 'C' has or is using private name 'D'. tests/cases/compiler/declarationEmitExpressionInExtends4.ts(9,18): error TS2304: Cannot find name 'SomeUndefinedFunction'. tests/cases/compiler/declarationEmitExpressionInExtends4.ts(14,18): error TS2304: Cannot find name 'SomeUndefinedFunction'. tests/cases/compiler/declarationEmitExpressionInExtends4.ts(14,18): error TS4020: 'extends' clause of exported class 'C3' has or is using private name 'SomeUndefinedFunction'. -==== tests/cases/compiler/declarationEmitExpressionInExtends4.ts (6 errors) ==== +==== tests/cases/compiler/declarationEmitExpressionInExtends4.ts (4 errors) ==== function getSomething() { - ~~~~~~~~~~~~ -!!! error TS4060: Return type of exported function has or is using private name 'D'. return class D { } } class C extends getSomething() { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2315: Type 'D' is not generic. - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS4020: 'extends' clause of exported class 'C' has or is using private name 'D'. } diff --git a/tests/baselines/reference/emitClassExpressionInDeclarationFile.js b/tests/baselines/reference/emitClassExpressionInDeclarationFile.js new file mode 100644 index 00000000000..423546bcd4e --- /dev/null +++ b/tests/baselines/reference/emitClassExpressionInDeclarationFile.js @@ -0,0 +1,148 @@ +//// [emitClassExpressionInDeclarationFile.ts] +export var simpleExample = class { + static getTags() { } + tags() { } +} +export var circularReference = class C { + static getTags(c: C): C { return c } + tags(c: C): C { return c } +} + +// repro from #15066 +export class FooItem { + foo(): void { } + name?: string; +} + +export type Constructor = new(...args: any[]) => T; +export function WithTags>(Base: T) { + return class extends Base { + static getTags(): void { } + tags(): void { } + } +} + +export class Test extends WithTags(FooItem) {} + +const test = new Test(); + +Test.getTags() +test.tags(); + + +//// [emitClassExpressionInDeclarationFile.js] +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + return function (d, b) { + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +exports.__esModule = true; +exports.simpleExample = (function () { + function class_1() { + } + class_1.getTags = function () { }; + class_1.prototype.tags = function () { }; + return class_1; +}()); +exports.circularReference = (function () { + function C() { + } + C.getTags = function (c) { return c; }; + C.prototype.tags = function (c) { return c; }; + return C; +}()); +// repro from #15066 +var FooItem = (function () { + function FooItem() { + } + FooItem.prototype.foo = function () { }; + return FooItem; +}()); +exports.FooItem = FooItem; +function WithTags(Base) { + return (function (_super) { + __extends(class_2, _super); + function class_2() { + return _super !== null && _super.apply(this, arguments) || this; + } + class_2.getTags = function () { }; + class_2.prototype.tags = function () { }; + return class_2; + }(Base)); +} +exports.WithTags = WithTags; +var Test = (function (_super) { + __extends(Test, _super); + function Test() { + return _super !== null && _super.apply(this, arguments) || this; + } + return Test; +}(WithTags(FooItem))); +exports.Test = Test; +var test = new Test(); +Test.getTags(); +test.tags(); + + +//// [emitClassExpressionInDeclarationFile.d.ts] +export declare var simpleExample: { + new (): { + tags(): void; + }; + prototype: { + tags(): void; + }; + getTags(): void; +}; +export declare var circularReference: { + new (): { + tags(c: any): any; + }; + prototype: { + tags(c: any): any; + }; + getTags(c: { + tags(c: any): any; + }): { + tags(c: any): any; + }; +}; +export declare class FooItem { + foo(): void; + name?: string; +} +export declare type Constructor = new (...args: any[]) => T; +export declare function WithTags>(Base: T): { + new (...args: any[]): { + tags(): void; + foo(): void; + name?: string; + }; + prototype: { + tags(): void; + foo(): void; + name?: string; + }; + getTags(): void; +} & T; +declare const Test_base: { + new (...args: any[]): { + tags(): void; + foo(): void; + name?: string; + }; + prototype: { + tags(): void; + foo(): void; + name?: string; + }; + getTags(): void; +} & typeof FooItem; +export declare class Test extends Test_base { +} diff --git a/tests/baselines/reference/emitClassExpressionInDeclarationFile.symbols b/tests/baselines/reference/emitClassExpressionInDeclarationFile.symbols new file mode 100644 index 00000000000..537838937c5 --- /dev/null +++ b/tests/baselines/reference/emitClassExpressionInDeclarationFile.symbols @@ -0,0 +1,84 @@ +=== tests/cases/compiler/emitClassExpressionInDeclarationFile.ts === +export var simpleExample = class { +>simpleExample : Symbol(simpleExample, Decl(emitClassExpressionInDeclarationFile.ts, 0, 10)) + + static getTags() { } +>getTags : Symbol(simpleExample.getTags, Decl(emitClassExpressionInDeclarationFile.ts, 0, 34)) + + tags() { } +>tags : Symbol(simpleExample.tags, Decl(emitClassExpressionInDeclarationFile.ts, 1, 24)) +} +export var circularReference = class C { +>circularReference : Symbol(circularReference, Decl(emitClassExpressionInDeclarationFile.ts, 4, 10)) +>C : Symbol(C, Decl(emitClassExpressionInDeclarationFile.ts, 4, 30)) + + static getTags(c: C): C { return c } +>getTags : Symbol(C.getTags, Decl(emitClassExpressionInDeclarationFile.ts, 4, 40)) +>c : Symbol(c, Decl(emitClassExpressionInDeclarationFile.ts, 5, 19)) +>C : Symbol(C, Decl(emitClassExpressionInDeclarationFile.ts, 4, 30)) +>C : Symbol(C, Decl(emitClassExpressionInDeclarationFile.ts, 4, 30)) +>c : Symbol(c, Decl(emitClassExpressionInDeclarationFile.ts, 5, 19)) + + tags(c: C): C { return c } +>tags : Symbol(C.tags, Decl(emitClassExpressionInDeclarationFile.ts, 5, 40)) +>c : Symbol(c, Decl(emitClassExpressionInDeclarationFile.ts, 6, 9)) +>C : Symbol(C, Decl(emitClassExpressionInDeclarationFile.ts, 4, 30)) +>C : Symbol(C, Decl(emitClassExpressionInDeclarationFile.ts, 4, 30)) +>c : Symbol(c, Decl(emitClassExpressionInDeclarationFile.ts, 6, 9)) +} + +// repro from #15066 +export class FooItem { +>FooItem : Symbol(FooItem, Decl(emitClassExpressionInDeclarationFile.ts, 7, 1)) + + foo(): void { } +>foo : Symbol(FooItem.foo, Decl(emitClassExpressionInDeclarationFile.ts, 10, 22)) + + name?: string; +>name : Symbol(FooItem.name, Decl(emitClassExpressionInDeclarationFile.ts, 11, 19)) +} + +export type Constructor = new(...args: any[]) => T; +>Constructor : Symbol(Constructor, Decl(emitClassExpressionInDeclarationFile.ts, 13, 1)) +>T : Symbol(T, Decl(emitClassExpressionInDeclarationFile.ts, 15, 24)) +>args : Symbol(args, Decl(emitClassExpressionInDeclarationFile.ts, 15, 33)) +>T : Symbol(T, Decl(emitClassExpressionInDeclarationFile.ts, 15, 24)) + +export function WithTags>(Base: T) { +>WithTags : Symbol(WithTags, Decl(emitClassExpressionInDeclarationFile.ts, 15, 54)) +>T : Symbol(T, Decl(emitClassExpressionInDeclarationFile.ts, 16, 25)) +>Constructor : Symbol(Constructor, Decl(emitClassExpressionInDeclarationFile.ts, 13, 1)) +>FooItem : Symbol(FooItem, Decl(emitClassExpressionInDeclarationFile.ts, 7, 1)) +>Base : Symbol(Base, Decl(emitClassExpressionInDeclarationFile.ts, 16, 57)) +>T : Symbol(T, Decl(emitClassExpressionInDeclarationFile.ts, 16, 25)) + + return class extends Base { +>Base : Symbol(Base, Decl(emitClassExpressionInDeclarationFile.ts, 16, 57)) + + static getTags(): void { } +>getTags : Symbol((Anonymous class).getTags, Decl(emitClassExpressionInDeclarationFile.ts, 17, 31)) + + tags(): void { } +>tags : Symbol((Anonymous class).tags, Decl(emitClassExpressionInDeclarationFile.ts, 18, 34)) + } +} + +export class Test extends WithTags(FooItem) {} +>Test : Symbol(Test, Decl(emitClassExpressionInDeclarationFile.ts, 21, 1)) +>WithTags : Symbol(WithTags, Decl(emitClassExpressionInDeclarationFile.ts, 15, 54)) +>FooItem : Symbol(FooItem, Decl(emitClassExpressionInDeclarationFile.ts, 7, 1)) + +const test = new Test(); +>test : Symbol(test, Decl(emitClassExpressionInDeclarationFile.ts, 25, 5)) +>Test : Symbol(Test, Decl(emitClassExpressionInDeclarationFile.ts, 21, 1)) + +Test.getTags() +>Test.getTags : Symbol((Anonymous class).getTags, Decl(emitClassExpressionInDeclarationFile.ts, 17, 31)) +>Test : Symbol(Test, Decl(emitClassExpressionInDeclarationFile.ts, 21, 1)) +>getTags : Symbol((Anonymous class).getTags, Decl(emitClassExpressionInDeclarationFile.ts, 17, 31)) + +test.tags(); +>test.tags : Symbol((Anonymous class).tags, Decl(emitClassExpressionInDeclarationFile.ts, 18, 34)) +>test : Symbol(test, Decl(emitClassExpressionInDeclarationFile.ts, 25, 5)) +>tags : Symbol((Anonymous class).tags, Decl(emitClassExpressionInDeclarationFile.ts, 18, 34)) + diff --git a/tests/baselines/reference/emitClassExpressionInDeclarationFile.types b/tests/baselines/reference/emitClassExpressionInDeclarationFile.types new file mode 100644 index 00000000000..3d5190262de --- /dev/null +++ b/tests/baselines/reference/emitClassExpressionInDeclarationFile.types @@ -0,0 +1,91 @@ +=== tests/cases/compiler/emitClassExpressionInDeclarationFile.ts === +export var simpleExample = class { +>simpleExample : typeof simpleExample +>class { static getTags() { } tags() { }} : typeof simpleExample + + static getTags() { } +>getTags : () => void + + tags() { } +>tags : () => void +} +export var circularReference = class C { +>circularReference : typeof C +>class C { static getTags(c: C): C { return c } tags(c: C): C { return c }} : typeof C +>C : typeof C + + static getTags(c: C): C { return c } +>getTags : (c: C) => C +>c : C +>C : C +>C : C +>c : C + + tags(c: C): C { return c } +>tags : (c: C) => C +>c : C +>C : C +>C : C +>c : C +} + +// repro from #15066 +export class FooItem { +>FooItem : FooItem + + foo(): void { } +>foo : () => void + + name?: string; +>name : string +} + +export type Constructor = new(...args: any[]) => T; +>Constructor : Constructor +>T : T +>args : any[] +>T : T + +export function WithTags>(Base: T) { +>WithTags : >(Base: T) => { new (...args: any[]): (Anonymous class); prototype: WithTags.(Anonymous class); getTags(): void; } & T +>T : T +>Constructor : Constructor +>FooItem : FooItem +>Base : T +>T : T + + return class extends Base { +>class extends Base { static getTags(): void { } tags(): void { } } : { new (...args: any[]): (Anonymous class); prototype: WithTags.(Anonymous class); getTags(): void; } & T +>Base : FooItem + + static getTags(): void { } +>getTags : () => void + + tags(): void { } +>tags : () => void + } +} + +export class Test extends WithTags(FooItem) {} +>Test : Test +>WithTags(FooItem) : WithTags.(Anonymous class) & FooItem +>WithTags : >(Base: T) => { new (...args: any[]): (Anonymous class); prototype: WithTags.(Anonymous class); getTags(): void; } & T +>FooItem : typeof FooItem + +const test = new Test(); +>test : Test +>new Test() : Test +>Test : typeof Test + +Test.getTags() +>Test.getTags() : void +>Test.getTags : () => void +>Test : typeof Test +>getTags : () => void + +test.tags(); +>test.tags() : void +>test.tags : () => void +>test : Test +>tags : () => void + diff --git a/tests/cases/compiler/emitClassExpressionInDeclarationFile.ts b/tests/cases/compiler/emitClassExpressionInDeclarationFile.ts new file mode 100644 index 00000000000..f99bcbd7efd --- /dev/null +++ b/tests/cases/compiler/emitClassExpressionInDeclarationFile.ts @@ -0,0 +1,30 @@ +// @declaration: true +export var simpleExample = class { + static getTags() { } + tags() { } +} +export var circularReference = class C { + static getTags(c: C): C { return c } + tags(c: C): C { return c } +} + +// repro from #15066 +export class FooItem { + foo(): void { } + name?: string; +} + +export type Constructor = new(...args: any[]) => T; +export function WithTags>(Base: T) { + return class extends Base { + static getTags(): void { } + tags(): void { } + } +} + +export class Test extends WithTags(FooItem) {} + +const test = new Test(); + +Test.getTags() +test.tags(); diff --git a/tests/cases/fourslash/findAllRefsForVariableInExtendsClause01.ts b/tests/cases/fourslash/findAllRefsForVariableInExtendsClause01.ts index a61f925c97e..9d79d97d419 100644 --- a/tests/cases/fourslash/findAllRefsForVariableInExtendsClause01.ts +++ b/tests/cases/fourslash/findAllRefsForVariableInExtendsClause01.ts @@ -3,4 +3,4 @@ ////var [|{| "isWriteAccess": true, "isDefinition": true |}Base|] = class { }; ////class C extends [|Base|] { } -verify.singleReferenceGroup("var Base: typeof Base"); +verify.singleReferenceGroup("var Base: {\n new (): {};\n prototype: {};\n}"); diff --git a/tests/cases/fourslash/renameJsExports02.ts b/tests/cases/fourslash/renameJsExports02.ts index 94c84980784..ef39bbc83b6 100644 --- a/tests/cases/fourslash/renameJsExports02.ts +++ b/tests/cases/fourslash/renameJsExports02.ts @@ -10,7 +10,7 @@ const [r0, r1] = test.ranges(); verify.referenceGroups(r0, [ { definition: "(local class) A", ranges: [r0] }, - { definition: "const A: typeof A", ranges: [r1] } + { definition: "const A: {\n new (): {};\n prototype: {};\n}", ranges: [r1] } ]); -verify.singleReferenceGroup("const A: typeof A", [r1]); +verify.singleReferenceGroup("const A: {\n new (): {};\n prototype: {};\n}", [r1]);