From fff743024c38c9ee3d838ee0eb92e0c10045e701 Mon Sep 17 00:00:00 2001 From: Lucas Azzola Date: Thu, 27 Apr 2017 02:25:58 +1000 Subject: [PATCH] Add TSParameterProperty, TSAbstractClassDeclaration and TSAbstractMethodDefnition (#1410) * feat(typescript): implement TSParameterProperty * test(typescript): add TypeScript's classes/constructorDeclarations/constructorParameters tests * feat(typescript): implement TSAbstractClassDeclaration and TSAbstractMethodDefinition * test(typescript): add TypeScript's classes/classDeclarations/classAbstractKeyword tests * fix(build): use join instead of spread operator * fix(typescript): fix semicolon handling * fix(typescript): handle accessibility modfiers in TSAbstractMethodDefinition * test(typescript): remove invalid test results * test(typescript): remove unimplemented abstract properties from tests * test(typescript): add snapshots for abstract keyword newline behavior --- src/fast-path.js | 4 +- src/printer.js | 43 +- .../__snapshots__/jsfmt.spec.js.snap | 875 ++++++++++++++++++ .../classAbstractAccessor.ts | 7 + .../classAbstractAsIdentifier.ts | 5 + ...bstractAssignabilityConstructorFunction.ts | 6 + .../classAbstractClinterfaceAssignability.ts | 13 + .../classAbstractConstructorAssignability.ts | 14 + .../classAbstractCrashedOnce.ts | 9 + .../classAbstractExtends.ts | 16 + .../classAbstractFactoryFunction.ts | 17 + .../classAbstractGeneric.ts | 25 + .../classAbstractImportInstantiation.ts | 7 + .../classAbstractInAModule.ts | 7 + .../classAbstractInheritance.ts | 21 + .../classAbstractInstantiations1.ts | 19 + .../classAbstractInstantiations2.ts | 50 + .../classAbstractMethodInNonAbstractClass.ts | 7 + .../classAbstractMethodWithImplementation.ts | 3 + .../classAbstractMixedWithModifiers.ts | 15 + .../classAbstractOverloads.ts | 24 + .../classAbstractOverrideWithAbstract.ts | 23 + .../classAbstractProperties.ts | 6 + .../classAbstractSingleLineDecl.ts | 12 + .../classAbstractSuperCalls.ts | 25 + .../classAbstractUsingAbstractMethod1.ts | 17 + .../classAbstractUsingAbstractMethods2.ts | 27 + .../classAbstractWithInterface.ts | 0 .../classAbstractKeyword/jsfmt.spec.js | 1 + .../__snapshots__/jsfmt.spec.js.snap | 420 +++++++++ ...constructorDefaultValuesReferencingThis.ts | 11 + ...structorImplementationWithDefaultValues.ts | 20 + ...tructorImplementationWithDefaultValues2.ts | 20 + .../constructorOverloadsWithDefaultValues.ts | 13 + ...structorOverloadsWithOptionalParameters.ts | 13 + .../constructorParameterProperties.ts | 20 + .../constructorParameterProperties2.ts | 31 + .../declarationEmitReadonly.ts | 5 + .../constructorParameters/jsfmt.spec.js | 1 + .../readonlyConstructorAssignment.ts | 38 + .../readonlyInConstructorParameters.ts | 13 + .../constructorParameters/readonlyReadonly.ts | 4 + .../thisType/__snapshots__/jsfmt.spec.js.snap | 2 +- .../abstract/__snapshots__/jsfmt.spec.js.snap | 32 + .../abstract/abstractNewlineHandling.ts | 15 + .../typescript/custom/abstract/jsfmt.spec.js | 1 + 46 files changed, 1951 insertions(+), 6 deletions(-) create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractAccessor.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractAsIdentifier.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractAssignabilityConstructorFunction.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractClinterfaceAssignability.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractConstructorAssignability.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractCrashedOnce.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractExtends.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractFactoryFunction.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractGeneric.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractImportInstantiation.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInAModule.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInheritance.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInstantiations1.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInstantiations2.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractMethodInNonAbstractClass.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractMethodWithImplementation.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractMixedWithModifiers.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractOverloads.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractOverrideWithAbstract.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractProperties.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractSingleLineDecl.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractSuperCalls.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractUsingAbstractMethod1.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractUsingAbstractMethods2.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractWithInterface.ts create mode 100644 tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/jsfmt.spec.js create mode 100644 tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorDefaultValuesReferencingThis.ts create mode 100644 tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorImplementationWithDefaultValues.ts create mode 100644 tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorImplementationWithDefaultValues2.ts create mode 100644 tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorOverloadsWithDefaultValues.ts create mode 100644 tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorOverloadsWithOptionalParameters.ts create mode 100644 tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties.ts create mode 100644 tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties2.ts create mode 100644 tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/declarationEmitReadonly.ts create mode 100644 tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/jsfmt.spec.js create mode 100644 tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/readonlyConstructorAssignment.ts create mode 100644 tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/readonlyInConstructorParameters.ts create mode 100644 tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/readonlyReadonly.ts create mode 100644 tests/typescript/custom/abstract/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/typescript/custom/abstract/abstractNewlineHandling.ts create mode 100644 tests/typescript/custom/abstract/jsfmt.spec.js diff --git a/src/fast-path.js b/src/fast-path.js index 383f1ac4..7eabba71 100644 --- a/src/fast-path.js +++ b/src/fast-path.js @@ -67,7 +67,9 @@ function getNodeHelper(path, count) { for (var i = s.length - 1; i >= 0; i -= 2) { var value = s[i]; - if (n.Node.check(value) && --count < 0) { + // Temp: This can be removed when `ast-types` knows that TSNodes are Nodes. + var isTsNode = value && value.type && value.type.startsWith('TS'); + if ((isTsNode || n.Node.check(value)) && --count < 0) { return value; } } diff --git a/src/printer.js b/src/printer.js index eb191b56..c91d5946 100644 --- a/src/printer.js +++ b/src/printer.js @@ -401,9 +401,16 @@ function genericPrintNoParens(path, options, print, args) { ); } case "MethodDefinition": + case "TSAbstractMethodDefinition": if (n.static) { parts.push("static "); } + if (n.accessibility) { + parts.push(n.accessibility + " "); + } + if (n.type === "TSAbstractMethodDefinition") { + parts.push("abstract "); + } parts.push(printMethod(path, options, print)); @@ -1468,6 +1475,7 @@ function genericPrintNoParens(path, options, print, args) { return concat(parts); case "ClassDeclaration": case "ClassExpression": + case "TSAbstractClassDeclaration": return concat(printClass(path, options, print)); case "TemplateElement": return join(literalline, n.value.raw.split("\n")); @@ -1926,6 +1934,17 @@ function genericPrintNoParens(path, options, print, args) { parts.push(": ") parts.push(path.call(print, "typeAnnotation")); + return concat(parts); + case "TSParameterProperty": + if (n.accessibility) { + parts.push(n.accessibility + " "); + } + if (n.isReadonly) { + parts.push("readonly "); + } + + parts.push(path.call(print, "parameter")); + return concat(parts); case "TSTypeReference": parts.push(path.call(print, "typeName")) @@ -2158,6 +2177,7 @@ function printPropertyKey(path, options, print) { function printMethod(path, options, print) { var node = path.getNode(); + var semi = options.semi ? ";" : ""; var kind = node.kind; var parts = []; @@ -2197,11 +2217,15 @@ function printMethod(path, options, print) { }, "value"), path.call(p => printReturnType(p, print), "value") ]) - ), - " ", - path.call(print, "value", "body") + ) ); + if (!node.value.body || node.value.body.length === 0) { + parts.push(semi); + } else { + parts.push(" ", path.call(print, "value", "body")); + } + return concat(parts); } @@ -2679,7 +2703,16 @@ function getFlowVariance(path) { function printClass(path, options, print) { const n = path.getValue(); - const parts = ["class"]; + const parts = []; + + if (n.accessibility) { + parts.push(n.accessibility + " "); + } + if (n.type === "TSAbstractClassDeclaration") { + parts.push("abstract "); + } + + parts.push("class"); if (n.id) { parts.push(" ", path.call(print, "id"), path.call(print, "typeParameters")); @@ -3545,6 +3578,8 @@ function classChildNeedsASIProtection(node) { return node.computed; // flow case "MethodDefinition": + // typescript + case "TSAbstractMethodDefinition": // babylon case "ClassMethod": { const isAsync = node.value ? node.value.async : node.async; diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..3c46f2bb --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,875 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`classAbstractAccessor.ts 1`] = ` + +abstract class A { + abstract get a(); + abstract get aa() { return 1; } + abstract set b(x: string); + abstract set bb(x: string) {} +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +abstract class A { + abstract get a(); + abstract get aa() { + return 1; + } + abstract set b(x: string); + abstract set bb(x: string) {} +} + +`; + +exports[`classAbstractAsIdentifier.ts 1`] = ` +class abstract { + foo() { return 1; } +} + +new abstract;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class abstract { + foo() { + return 1; + } +} + +new abstract(); + +`; + +exports[`classAbstractAssignabilityConstructorFunction.ts 1`] = ` +abstract class A { } + +var AAA: new() => A; + +AAA = A; +AAA = "asdf";~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +abstract class A {} + +var AAA: new() => A; + +AAA = A; +AAA = "asdf"; + +`; + +exports[`classAbstractClinterfaceAssignability.ts 1`] = ` + +var I: IConstructor; + +abstract class A { + x: number; + static y: number; +} + +var AA: typeof A; +AA = I; + +var AAA: typeof I; +AAA = A;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +var I: IConstructor; + +abstract class A { + x: number; + static y: number; +} + +var AA: typeof A; +AA = I; + +var AAA: typeof I; +AAA = A; + +`; + +exports[`classAbstractConstructorAssignability.ts 1`] = ` + +class A {} + +abstract class B extends A {} + +class C extends B {} + +var AA : typeof A = B; +var BB : typeof B = A; +var CC : typeof C = B; + +new AA; +new BB; +new CC;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class A {} + +abstract class B extends A {} + +class C extends B {} + +var AA: typeof A = B; +var BB: typeof B = A; +var CC: typeof C = B; + +new AA(); +new BB(); +new CC(); + +`; + +exports[`classAbstractCrashedOnce.ts 1`] = ` +abstract class foo { + protected abstract test(); +} + +class bar extends foo { + test() { + } +} +var x = new bar();~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +abstract class foo { + protected abstract test(); +} + +class bar extends foo { + test() {} +} +var x = new bar(); + +`; + +exports[`classAbstractExtends.ts 1`] = ` + +class A { + foo() {} +} + +abstract class B extends A { + abstract bar(); +} + +class C extends B { } + +abstract class D extends B {} + +class E extends B { + bar() {} +}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class A { + foo() {} +} + +abstract class B extends A { + abstract bar(); +} + +class C extends B {} + +abstract class D extends B {} + +class E extends B { + bar() {} +} + +`; + +exports[`classAbstractFactoryFunction.ts 1`] = ` + +class A {} +abstract class B extends A {} + +function NewA(Factory: typeof A) { + return new A; +} + +function NewB(Factory: typeof B) { + return new B; +} + +NewA(A); +NewA(B); + +NewB(A); +NewB(B);~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class A {} +abstract class B extends A {} + +function NewA(Factory: typeof A) { + return new A(); +} + +function NewB(Factory: typeof B) { + return new B(); +} + +NewA(A); +NewA(B); + +NewB(A); +NewB(B); + +`; + +exports[`classAbstractGeneric.ts 1`] = ` +abstract class A { + t: T; + + abstract foo(): T; + abstract bar(t: T); +} + +abstract class B extends A {} + +class C extends A {} + +class D extends A {} + +class E extends A { + foo() { return this.t; } +} + +class F extends A { + bar(t : T) {} +} + +class G extends A { + foo() { return this.t; } + bar(t: T) { } +}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +abstract class A { + t: T; + + abstract foo(): T; + abstract bar(t: T); +} + +abstract class B extends A {} + +class C extends A {} + +class D extends A {} + +class E extends A { + foo() { + return this.t; + } +} + +class F extends A { + bar(t: T) {} +} + +class G extends A { + foo() { + return this.t; + } + bar(t: T) {} +} + +`; + +exports[`classAbstractImportInstantiation.ts 1`] = ` + +export abstract class A {} + +new A; + + +new myA; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +export abstract class A {} + +new A(); + +new myA(); + +`; + +exports[`classAbstractInAModule.ts 1`] = ` + +export abstract class A {} +export class B extends A {} + + +new M.A; +new M.B;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +export abstract class A {} +export class B extends A {} + +new M.A(); +new M.B(); + +`; + +exports[`classAbstractInheritance.ts 1`] = ` +abstract class A {} + +abstract class B extends A {} + +class C extends A {} + +abstract class AA { + abstract foo(); +} + +abstract class BB extends AA {} + +class CC extends AA {} + +class DD extends BB {} + +abstract class EE extends BB {} + +class FF extends CC {} + +abstract class GG extends CC {}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +abstract class A {} + +abstract class B extends A {} + +class C extends A {} + +abstract class AA { + abstract foo(); +} + +abstract class BB extends AA {} + +class CC extends AA {} + +class DD extends BB {} + +abstract class EE extends BB {} + +class FF extends CC {} + +abstract class GG extends CC {} + +`; + +exports[`classAbstractInstantiations1.ts 1`] = ` + +abstract class A {} + +class B extends A {} + +abstract class C extends B {} + +new A; +new A(1); +new B; +new C; + +var a : A; +var b : B; +var c : C; + +a = new B; +b = new B; +c = new B; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +abstract class A {} + +class B extends A {} + +abstract class C extends B {} + +new A(); +new A(1); +new B(); +new C(); + +var a: A; +var b: B; +var c: C; + +a = new B(); +b = new B(); +c = new B(); + +`; + +exports[`classAbstractInstantiations2.ts 1`] = ` +class A { +} + +abstract class B { + foo(): number { return this.bar(); } + abstract bar() : number; +} + +new B; + +var BB: typeof B = B; +var AA: typeof A = BB; +new AA; + +function constructB(Factory : typeof B) { + new Factory; +} + +var BB = B; +new BB; + +var x : any = C; +new x; + +class C extends B { } + +abstract class D extends B { } + +class E extends B { + bar() { return 1; } +} + +abstract class F extends B { + abstract foo() : number; + bar() { return 2; } +} + +abstract class G { + abstract qux(x : number) : string; + abstract qux() : number; + y : number; + abstract quz(x : number, y : string) : boolean; + + abstract nom(): boolean; + nom(x : number): boolean; +} + +class H { + abstract baz() : number; +}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class A {} + +abstract class B { + foo(): number { + return this.bar(); + } + abstract bar(): number; +} + +new B(); + +var BB: typeof B = B; +var AA: typeof A = BB; +new AA(); + +function constructB(Factory: typeof B) { + new Factory(); +} + +var BB = B; +new BB(); + +var x: any = C; +new x(); + +class C extends B {} + +abstract class D extends B {} + +class E extends B { + bar() { + return 1; + } +} + +abstract class F extends B { + abstract foo(): number; + bar() { + return 2; + } +} + +abstract class G { + abstract qux(x: number): string; + abstract qux(): number; + y: number; + abstract quz(x: number, y: string): boolean; + + abstract nom(): boolean; + nom(x: number): boolean; +} + +class H { + abstract baz(): number; +} + +`; + +exports[`classAbstractMethodInNonAbstractClass.ts 1`] = ` +class A { + abstract foo(); +} + +class B { + abstract foo() {} +}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class A { + abstract foo(); +} + +class B { + abstract foo() {} +} + +`; + +exports[`classAbstractMethodWithImplementation.ts 1`] = ` +abstract class A { + abstract foo() {} +}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +abstract class A { + abstract foo() {} +} + +`; + +exports[`classAbstractMixedWithModifiers.ts 1`] = ` +abstract class A { + abstract foo_a(); + + public abstract foo_b(); + protected abstract foo_c(); + private abstract foo_d(); + + abstract public foo_bb(); + abstract protected foo_cc(); + abstract private foo_dd(); + + abstract static foo_d(); + + static abstract foo_e(); +}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +abstract class A { + abstract foo_a(); + + public abstract foo_b(); + protected abstract foo_c(); + private abstract foo_d(); + + public abstract foo_bb(); + protected abstract foo_cc(); + private abstract foo_dd(); + + static abstract foo_d(); + + static abstract foo_e(); +} + +`; + +exports[`classAbstractOverloads.ts 1`] = ` +abstract class A { + abstract foo(); + abstract foo() : number; + abstract foo(); + + abstract bar(); + bar(); + abstract bar(); + + abstract baz(); + baz(); + abstract baz(); + baz() {} + + qux(); +} + +abstract class B { + abstract foo() : number; + abstract foo(); + x : number; + abstract foo(); + abstract foo(); +}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +abstract class A { + abstract foo(); + abstract foo(): number; + abstract foo(); + + abstract bar(); + bar(); + abstract bar(); + + abstract baz(); + baz(); + abstract baz(); + baz() {} + + qux(); +} + +abstract class B { + abstract foo(): number; + abstract foo(); + x: number; + abstract foo(); + abstract foo(); +} + +`; + +exports[`classAbstractOverrideWithAbstract.ts 1`] = ` +class A { + foo() {} +} + +abstract class B extends A { + abstract foo(); +} + +abstract class AA { + foo() {} + abstract bar(); +} + +abstract class BB extends AA { + abstract foo(); + bar () {} +} + +class CC extends BB {} + +class DD extends BB { + foo() {} +}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class A { + foo() {} +} + +abstract class B extends A { + abstract foo(); +} + +abstract class AA { + foo() {} + abstract bar(); +} + +abstract class BB extends AA { + abstract foo(); + bar() {} +} + +class CC extends BB {} + +class DD extends BB { + foo() {} +} + +`; + +exports[`classAbstractProperties.ts 1`] = ` +abstract class A { + abstract foo_x() : number; + public abstract foo_y() : number; + protected abstract foo_z() : number; + private abstract foo_w() : number; +}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +abstract class A { + abstract foo_x(): number; + public abstract foo_y(): number; + protected abstract foo_z(): number; + private abstract foo_w(): number; +} + +`; + +exports[`classAbstractSingleLineDecl.ts 1`] = ` +abstract class A {} + +abstract +class B {} + +abstract + +class C {} + +new A; +new B; +new C;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +abstract class A {} + +abstract; +class B {} + +abstract; + +class C {} + +new A(); +new B(); +new C(); + +`; + +exports[`classAbstractSuperCalls.ts 1`] = ` + +class A { + foo() { return 1; } +} + +abstract class B extends A { + abstract foo(); + bar() { super.foo(); } + baz() { return this.foo; } +} + +class C extends B { + foo() { return 2; } + qux() { return super.foo() || super.foo; } + norf() { return super.bar(); } +} + +class AA { + foo() { return 1; } + bar() { return this.foo(); } +} + +abstract class BB extends AA { + abstract foo(); +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class A { + foo() { + return 1; + } +} + +abstract class B extends A { + abstract foo(); + bar() { + super.foo(); + } + baz() { + return this.foo; + } +} + +class C extends B { + foo() { + return 2; + } + qux() { + return super.foo() || super.foo; + } + norf() { + return super.bar(); + } +} + +class AA { + foo() { + return 1; + } + bar() { + return this.foo(); + } +} + +abstract class BB extends AA { + abstract foo(); +} + +`; + +exports[`classAbstractUsingAbstractMethod1.ts 1`] = ` +abstract class A { + abstract foo() : number; +} + +class B extends A { + foo() { return 1; } +} + +abstract class C extends A { + abstract foo() : number; +} + +var a = new B; +a.foo(); + +a = new C; +a.foo();~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +abstract class A { + abstract foo(): number; +} + +class B extends A { + foo() { + return 1; + } +} + +abstract class C extends A { + abstract foo(): number; +} + +var a = new B(); +a.foo(); + +a = new C(); +a.foo(); + +`; + +exports[`classAbstractUsingAbstractMethods2.ts 1`] = ` +class A { + abstract foo(); +} + +class B extends A {} + +abstract class C extends A {} + +class D extends A { + foo() {} +} + +abstract class E extends A { + foo() {} +} + +abstract class AA { + abstract foo(); +} + +class BB extends AA {} + +abstract class CC extends AA {} + +class DD extends AA { + foo() {} +}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class A { + abstract foo(); +} + +class B extends A {} + +abstract class C extends A {} + +class D extends A { + foo() {} +} + +abstract class E extends A { + foo() {} +} + +abstract class AA { + abstract foo(); +} + +class BB extends AA {} + +abstract class CC extends AA {} + +class DD extends AA { + foo() {} +} + +`; + +exports[`classAbstractWithInterface.ts 1`] = ` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +`; diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractAccessor.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractAccessor.ts new file mode 100644 index 00000000..50accd4d --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractAccessor.ts @@ -0,0 +1,7 @@ + +abstract class A { + abstract get a(); + abstract get aa() { return 1; } + abstract set b(x: string); + abstract set bb(x: string) {} +} diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractAsIdentifier.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractAsIdentifier.ts new file mode 100644 index 00000000..4c251a03 --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractAsIdentifier.ts @@ -0,0 +1,5 @@ +class abstract { + foo() { return 1; } +} + +new abstract; \ No newline at end of file diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractAssignabilityConstructorFunction.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractAssignabilityConstructorFunction.ts new file mode 100644 index 00000000..05393a59 --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractAssignabilityConstructorFunction.ts @@ -0,0 +1,6 @@ +abstract class A { } + +var AAA: new() => A; + +AAA = A; +AAA = "asdf"; \ No newline at end of file diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractClinterfaceAssignability.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractClinterfaceAssignability.ts new file mode 100644 index 00000000..3eeb5ec2 --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractClinterfaceAssignability.ts @@ -0,0 +1,13 @@ + +var I: IConstructor; + +abstract class A { + x: number; + static y: number; +} + +var AA: typeof A; +AA = I; + +var AAA: typeof I; +AAA = A; \ No newline at end of file diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractConstructorAssignability.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractConstructorAssignability.ts new file mode 100644 index 00000000..ecef21cc --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractConstructorAssignability.ts @@ -0,0 +1,14 @@ + +class A {} + +abstract class B extends A {} + +class C extends B {} + +var AA : typeof A = B; +var BB : typeof B = A; +var CC : typeof C = B; + +new AA; +new BB; +new CC; \ No newline at end of file diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractCrashedOnce.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractCrashedOnce.ts new file mode 100644 index 00000000..a8a604b3 --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractCrashedOnce.ts @@ -0,0 +1,9 @@ +abstract class foo { + protected abstract test(); +} + +class bar extends foo { + test() { + } +} +var x = new bar(); \ No newline at end of file diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractExtends.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractExtends.ts new file mode 100644 index 00000000..0aa57b75 --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractExtends.ts @@ -0,0 +1,16 @@ + +class A { + foo() {} +} + +abstract class B extends A { + abstract bar(); +} + +class C extends B { } + +abstract class D extends B {} + +class E extends B { + bar() {} +} \ No newline at end of file diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractFactoryFunction.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractFactoryFunction.ts new file mode 100644 index 00000000..73d33a34 --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractFactoryFunction.ts @@ -0,0 +1,17 @@ + +class A {} +abstract class B extends A {} + +function NewA(Factory: typeof A) { + return new A; +} + +function NewB(Factory: typeof B) { + return new B; +} + +NewA(A); +NewA(B); + +NewB(A); +NewB(B); \ No newline at end of file diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractGeneric.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractGeneric.ts new file mode 100644 index 00000000..a6069de9 --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractGeneric.ts @@ -0,0 +1,25 @@ +abstract class A { + t: T; + + abstract foo(): T; + abstract bar(t: T); +} + +abstract class B extends A {} + +class C extends A {} + +class D extends A {} + +class E extends A { + foo() { return this.t; } +} + +class F extends A { + bar(t : T) {} +} + +class G extends A { + foo() { return this.t; } + bar(t: T) { } +} \ No newline at end of file diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractImportInstantiation.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractImportInstantiation.ts new file mode 100644 index 00000000..a9288607 --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractImportInstantiation.ts @@ -0,0 +1,7 @@ + +export abstract class A {} + +new A; + + +new myA; diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInAModule.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInAModule.ts new file mode 100644 index 00000000..5a93dbf7 --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInAModule.ts @@ -0,0 +1,7 @@ + +export abstract class A {} +export class B extends A {} + + +new M.A; +new M.B; \ No newline at end of file diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInheritance.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInheritance.ts new file mode 100644 index 00000000..ced15607 --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInheritance.ts @@ -0,0 +1,21 @@ +abstract class A {} + +abstract class B extends A {} + +class C extends A {} + +abstract class AA { + abstract foo(); +} + +abstract class BB extends AA {} + +class CC extends AA {} + +class DD extends BB {} + +abstract class EE extends BB {} + +class FF extends CC {} + +abstract class GG extends CC {} \ No newline at end of file diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInstantiations1.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInstantiations1.ts new file mode 100644 index 00000000..7338c514 --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInstantiations1.ts @@ -0,0 +1,19 @@ + +abstract class A {} + +class B extends A {} + +abstract class C extends B {} + +new A; +new A(1); +new B; +new C; + +var a : A; +var b : B; +var c : C; + +a = new B; +b = new B; +c = new B; diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInstantiations2.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInstantiations2.ts new file mode 100644 index 00000000..aeea24e8 --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractInstantiations2.ts @@ -0,0 +1,50 @@ +class A { +} + +abstract class B { + foo(): number { return this.bar(); } + abstract bar() : number; +} + +new B; + +var BB: typeof B = B; +var AA: typeof A = BB; +new AA; + +function constructB(Factory : typeof B) { + new Factory; +} + +var BB = B; +new BB; + +var x : any = C; +new x; + +class C extends B { } + +abstract class D extends B { } + +class E extends B { + bar() { return 1; } +} + +abstract class F extends B { + abstract foo() : number; + bar() { return 2; } +} + +abstract class G { + abstract qux(x : number) : string; + abstract qux() : number; + y : number; + abstract quz(x : number, y : string) : boolean; + + abstract nom(): boolean; + nom(x : number): boolean; +} + +class H { + abstract baz() : number; +} \ No newline at end of file diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractMethodInNonAbstractClass.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractMethodInNonAbstractClass.ts new file mode 100644 index 00000000..98a2091b --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractMethodInNonAbstractClass.ts @@ -0,0 +1,7 @@ +class A { + abstract foo(); +} + +class B { + abstract foo() {} +} \ No newline at end of file diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractMethodWithImplementation.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractMethodWithImplementation.ts new file mode 100644 index 00000000..432613ca --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractMethodWithImplementation.ts @@ -0,0 +1,3 @@ +abstract class A { + abstract foo() {} +} \ No newline at end of file diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractMixedWithModifiers.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractMixedWithModifiers.ts new file mode 100644 index 00000000..d3c2546a --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractMixedWithModifiers.ts @@ -0,0 +1,15 @@ +abstract class A { + abstract foo_a(); + + public abstract foo_b(); + protected abstract foo_c(); + private abstract foo_d(); + + abstract public foo_bb(); + abstract protected foo_cc(); + abstract private foo_dd(); + + abstract static foo_d(); + + static abstract foo_e(); +} \ No newline at end of file diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractOverloads.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractOverloads.ts new file mode 100644 index 00000000..c30a03f8 --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractOverloads.ts @@ -0,0 +1,24 @@ +abstract class A { + abstract foo(); + abstract foo() : number; + abstract foo(); + + abstract bar(); + bar(); + abstract bar(); + + abstract baz(); + baz(); + abstract baz(); + baz() {} + + qux(); +} + +abstract class B { + abstract foo() : number; + abstract foo(); + x : number; + abstract foo(); + abstract foo(); +} \ No newline at end of file diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractOverrideWithAbstract.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractOverrideWithAbstract.ts new file mode 100644 index 00000000..6c69e5df --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractOverrideWithAbstract.ts @@ -0,0 +1,23 @@ +class A { + foo() {} +} + +abstract class B extends A { + abstract foo(); +} + +abstract class AA { + foo() {} + abstract bar(); +} + +abstract class BB extends AA { + abstract foo(); + bar () {} +} + +class CC extends BB {} + +class DD extends BB { + foo() {} +} \ No newline at end of file diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractProperties.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractProperties.ts new file mode 100644 index 00000000..775f4c31 --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractProperties.ts @@ -0,0 +1,6 @@ +abstract class A { + abstract foo_x() : number; + public abstract foo_y() : number; + protected abstract foo_z() : number; + private abstract foo_w() : number; +} \ No newline at end of file diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractSingleLineDecl.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractSingleLineDecl.ts new file mode 100644 index 00000000..374a3c08 --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractSingleLineDecl.ts @@ -0,0 +1,12 @@ +abstract class A {} + +abstract +class B {} + +abstract + +class C {} + +new A; +new B; +new C; \ No newline at end of file diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractSuperCalls.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractSuperCalls.ts new file mode 100644 index 00000000..2bad576e --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractSuperCalls.ts @@ -0,0 +1,25 @@ + +class A { + foo() { return 1; } +} + +abstract class B extends A { + abstract foo(); + bar() { super.foo(); } + baz() { return this.foo; } +} + +class C extends B { + foo() { return 2; } + qux() { return super.foo() || super.foo; } + norf() { return super.bar(); } +} + +class AA { + foo() { return 1; } + bar() { return this.foo(); } +} + +abstract class BB extends AA { + abstract foo(); +} diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractUsingAbstractMethod1.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractUsingAbstractMethod1.ts new file mode 100644 index 00000000..015ded7a --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractUsingAbstractMethod1.ts @@ -0,0 +1,17 @@ +abstract class A { + abstract foo() : number; +} + +class B extends A { + foo() { return 1; } +} + +abstract class C extends A { + abstract foo() : number; +} + +var a = new B; +a.foo(); + +a = new C; +a.foo(); \ No newline at end of file diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractUsingAbstractMethods2.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractUsingAbstractMethods2.ts new file mode 100644 index 00000000..e333811c --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractUsingAbstractMethods2.ts @@ -0,0 +1,27 @@ +class A { + abstract foo(); +} + +class B extends A {} + +abstract class C extends A {} + +class D extends A { + foo() {} +} + +abstract class E extends A { + foo() {} +} + +abstract class AA { + abstract foo(); +} + +class BB extends AA {} + +abstract class CC extends AA {} + +class DD extends AA { + foo() {} +} \ No newline at end of file diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractWithInterface.ts b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractWithInterface.ts new file mode 100644 index 00000000..e69de29b diff --git a/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/jsfmt.spec.js b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/jsfmt.spec.js new file mode 100644 index 00000000..bc085c48 --- /dev/null +++ b/tests/typescript/conformance/classes/classDeclarations/classAbstractKeyword/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, { parser: "typescript" }); diff --git a/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..2e88a1b8 --- /dev/null +++ b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,420 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`constructorDefaultValuesReferencingThis.ts 1`] = ` +class C { + constructor(x = this) { } +} + +class D { + constructor(x = this) { } +} + +class E { + constructor(public x = this) { } +}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class C { + constructor(x = this) {} +} + +class D { + constructor(x = this) {} +} + +class E { + constructor(public x = this) {} +} + +`; + +exports[`constructorImplementationWithDefaultValues.ts 1`] = ` +class C { + constructor(x); + constructor(x = 1) { + var y = x; + } +} + +class D { + constructor(x); + constructor(x:T = null) { + var y = x; + } +} + +class E { + constructor(x); + constructor(x: T = null) { + var y = x; + } +}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class C { + constructor(x); + constructor(x = 1) { + var y = x; + } +} + +class D { + constructor(x); + constructor(x: T = null) { + var y = x; + } +} + +class E { + constructor(x); + constructor(x: T = null) { + var y = x; + } +} + +`; + +exports[`constructorImplementationWithDefaultValues2.ts 1`] = ` +class C { + constructor(x); + constructor(public x: string = 1) { + var y = x; + } +} + +class D { + constructor(x: T, y: U); + constructor(x: T = 1, public y: U = x) { + var z = x; + } +} + +class E { + constructor(x); + constructor(x: T = new Date()) { + var y = x; + } +}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class C { + constructor(x); + constructor(public x: string = 1) { + var y = x; + } +} + +class D { + constructor(x: T, y: U); + constructor(x: T = 1, public y: U = x) { + var z = x; + } +} + +class E { + constructor(x); + constructor(x: T = new Date()) { + var y = x; + } +} + +`; + +exports[`constructorOverloadsWithDefaultValues.ts 1`] = ` +class C { + foo: string; + constructor(x = 1); + constructor() { + } +} + +class D { + foo: string; + constructor(x = 1); + constructor() { + } +}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class C { + foo: string; + constructor(x = 1); + constructor() {} +} + +class D { + foo: string; + constructor(x = 1); + constructor() {} +} + +`; + +exports[`constructorOverloadsWithOptionalParameters.ts 1`] = ` +class C { + foo: string; + constructor(x?, y?: any[]); + constructor() { + } +} + +class D { + foo: string; + constructor(x?, y?: any[]); + constructor() { + } +}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class C { + foo: string; + constructor(x?, y?: any[]); + constructor() {} +} + +class D { + foo: string; + constructor(x?, y?: any[]); + constructor() {} +} + +`; + +exports[`constructorParameterProperties.ts 1`] = ` +class C { + y: string; + constructor(private x: string, protected z: string) { } +} + +var c: C; +var r = c.y; +var r2 = c.x; // error +var r3 = c.z; // error + +class D { + y: T; + constructor(a: T, private x: T, protected z: T) { } +} + +var d: D; +var r = d.y; +var r2 = d.x; // error +var r3 = d.a; // error +var r4 = d.z; // error +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class C { + y: string; + constructor(private x: string, protected z: string) {} +} + +var c: C; +var r = c.y; +var r2 = c.x; // error +var r3 = c.z; // error + +class D { + y: T; + constructor(a: T, private x: T, protected z: T) {} +} + +var d: D; +var r = d.y; +var r2 = d.x; // error +var r3 = d.a; // error +var r4 = d.z; // error + +`; + +exports[`constructorParameterProperties2.ts 1`] = ` +class C { + y: number; + constructor(y: number) { } // ok +} + +var c: C; +var r = c.y; + +class D { + y: number; + constructor(public y: number) { } +} + +var d: D; +var r2 = d.y; + +class E { + y: number; + constructor(private y: number) { } +} + +var e: E; +var r3 = e.y; // error + +class F { + y: number; + constructor(protected y: number) { } +} + +var f: F; +var r4 = f.y; // error +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class C { + y: number; + constructor(y: number) {} // ok +} + +var c: C; +var r = c.y; + +class D { + y: number; + constructor(public y: number) {} +} + +var d: D; +var r2 = d.y; + +class E { + y: number; + constructor(private y: number) {} +} + +var e: E; +var r3 = e.y; // error + +class F { + y: number; + constructor(protected y: number) {} +} + +var f: F; +var r4 = f.y; // error + +`; + +exports[`declarationEmitReadonly.ts 1`] = ` +// @declaration: true + +class C { + constructor(readonly x: number) {} +}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// @declaration: true + +class C { + constructor(readonly x: number) {} +} + +`; + +exports[`readonlyConstructorAssignment.ts 1`] = ` +// Tests that readonly parameter properties behave like regular readonly properties + +class A { + constructor(readonly x: number) { + this.x = 0; + } +} + +class B extends A { + constructor(x: number) { + super(x); + // Fails, x is readonly + this.x = 1; + } +} + +class C extends A { + // This is the usual behavior of readonly properties: + // if one is redeclared in a base class, then it can be assigned to. + constructor(readonly x: number) { + super(x); + this.x = 1; + } +} + +class D { + constructor(private readonly x: number) { + this.x = 0; + } +} + +// Fails, can't redeclare readonly property +class E extends D { + constructor(readonly x: number) { + super(x); + this.x = 1; + } +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// Tests that readonly parameter properties behave like regular readonly properties + +class A { + constructor(readonly x: number) { + this.x = 0; + } +} + +class B extends A { + constructor(x: number) { + super(x); + // Fails, x is readonly + this.x = 1; + } +} + +class C extends A { + // This is the usual behavior of readonly properties: + // if one is redeclared in a base class, then it can be assigned to. + constructor(readonly x: number) { + super(x); + this.x = 1; + } +} + +class D { + constructor(private readonly x: number) { + this.x = 0; + } +} + +// Fails, can't redeclare readonly property +class E extends D { + constructor(readonly x: number) { + super(x); + this.x = 1; + } +} + +`; + +exports[`readonlyInConstructorParameters.ts 1`] = ` +class C { + constructor(readonly x: number) {} +} +new C(1).x = 2; + +class E { + constructor(readonly public x: number) {} +} + +class F { + constructor(private readonly x: number) {} +} +new F(1).x;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class C { + constructor(readonly x: number) {} +} +new C(1).x = 2; + +class E { + constructor(public readonly x: number) {} +} + +class F { + constructor(private readonly x: number) {} +} +new F(1).x; + +`; + +exports[`readonlyReadonly.ts 1`] = ` +class C { + readonly readonly x: number; + constructor(readonly readonly y: number) {} +}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class C { + x: number; + constructor(readonly y: number) {} +} + +`; diff --git a/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorDefaultValuesReferencingThis.ts b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorDefaultValuesReferencingThis.ts new file mode 100644 index 00000000..9209dcd7 --- /dev/null +++ b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorDefaultValuesReferencingThis.ts @@ -0,0 +1,11 @@ +class C { + constructor(x = this) { } +} + +class D { + constructor(x = this) { } +} + +class E { + constructor(public x = this) { } +} \ No newline at end of file diff --git a/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorImplementationWithDefaultValues.ts b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorImplementationWithDefaultValues.ts new file mode 100644 index 00000000..bdbcd7f9 --- /dev/null +++ b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorImplementationWithDefaultValues.ts @@ -0,0 +1,20 @@ +class C { + constructor(x); + constructor(x = 1) { + var y = x; + } +} + +class D { + constructor(x); + constructor(x:T = null) { + var y = x; + } +} + +class E { + constructor(x); + constructor(x: T = null) { + var y = x; + } +} \ No newline at end of file diff --git a/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorImplementationWithDefaultValues2.ts b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorImplementationWithDefaultValues2.ts new file mode 100644 index 00000000..6e717ffc --- /dev/null +++ b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorImplementationWithDefaultValues2.ts @@ -0,0 +1,20 @@ +class C { + constructor(x); + constructor(public x: string = 1) { + var y = x; + } +} + +class D { + constructor(x: T, y: U); + constructor(x: T = 1, public y: U = x) { + var z = x; + } +} + +class E { + constructor(x); + constructor(x: T = new Date()) { + var y = x; + } +} \ No newline at end of file diff --git a/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorOverloadsWithDefaultValues.ts b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorOverloadsWithDefaultValues.ts new file mode 100644 index 00000000..c2c3facf --- /dev/null +++ b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorOverloadsWithDefaultValues.ts @@ -0,0 +1,13 @@ +class C { + foo: string; + constructor(x = 1); + constructor() { + } +} + +class D { + foo: string; + constructor(x = 1); + constructor() { + } +} \ No newline at end of file diff --git a/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorOverloadsWithOptionalParameters.ts b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorOverloadsWithOptionalParameters.ts new file mode 100644 index 00000000..3ec148a9 --- /dev/null +++ b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorOverloadsWithOptionalParameters.ts @@ -0,0 +1,13 @@ +class C { + foo: string; + constructor(x?, y?: any[]); + constructor() { + } +} + +class D { + foo: string; + constructor(x?, y?: any[]); + constructor() { + } +} \ No newline at end of file diff --git a/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties.ts b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties.ts new file mode 100644 index 00000000..f4d63da1 --- /dev/null +++ b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties.ts @@ -0,0 +1,20 @@ +class C { + y: string; + constructor(private x: string, protected z: string) { } +} + +var c: C; +var r = c.y; +var r2 = c.x; // error +var r3 = c.z; // error + +class D { + y: T; + constructor(a: T, private x: T, protected z: T) { } +} + +var d: D; +var r = d.y; +var r2 = d.x; // error +var r3 = d.a; // error +var r4 = d.z; // error diff --git a/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties2.ts b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties2.ts new file mode 100644 index 00000000..687b47b8 --- /dev/null +++ b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties2.ts @@ -0,0 +1,31 @@ +class C { + y: number; + constructor(y: number) { } // ok +} + +var c: C; +var r = c.y; + +class D { + y: number; + constructor(public y: number) { } +} + +var d: D; +var r2 = d.y; + +class E { + y: number; + constructor(private y: number) { } +} + +var e: E; +var r3 = e.y; // error + +class F { + y: number; + constructor(protected y: number) { } +} + +var f: F; +var r4 = f.y; // error diff --git a/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/declarationEmitReadonly.ts b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/declarationEmitReadonly.ts new file mode 100644 index 00000000..76b41eab --- /dev/null +++ b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/declarationEmitReadonly.ts @@ -0,0 +1,5 @@ +// @declaration: true + +class C { + constructor(readonly x: number) {} +} \ No newline at end of file diff --git a/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/jsfmt.spec.js b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/jsfmt.spec.js new file mode 100644 index 00000000..bc085c48 --- /dev/null +++ b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, { parser: "typescript" }); diff --git a/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/readonlyConstructorAssignment.ts b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/readonlyConstructorAssignment.ts new file mode 100644 index 00000000..aeecf085 --- /dev/null +++ b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/readonlyConstructorAssignment.ts @@ -0,0 +1,38 @@ +// Tests that readonly parameter properties behave like regular readonly properties + +class A { + constructor(readonly x: number) { + this.x = 0; + } +} + +class B extends A { + constructor(x: number) { + super(x); + // Fails, x is readonly + this.x = 1; + } +} + +class C extends A { + // This is the usual behavior of readonly properties: + // if one is redeclared in a base class, then it can be assigned to. + constructor(readonly x: number) { + super(x); + this.x = 1; + } +} + +class D { + constructor(private readonly x: number) { + this.x = 0; + } +} + +// Fails, can't redeclare readonly property +class E extends D { + constructor(readonly x: number) { + super(x); + this.x = 1; + } +} diff --git a/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/readonlyInConstructorParameters.ts b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/readonlyInConstructorParameters.ts new file mode 100644 index 00000000..dbcdff7a --- /dev/null +++ b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/readonlyInConstructorParameters.ts @@ -0,0 +1,13 @@ +class C { + constructor(readonly x: number) {} +} +new C(1).x = 2; + +class E { + constructor(readonly public x: number) {} +} + +class F { + constructor(private readonly x: number) {} +} +new F(1).x; \ No newline at end of file diff --git a/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/readonlyReadonly.ts b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/readonlyReadonly.ts new file mode 100644 index 00000000..8fb611a0 --- /dev/null +++ b/tests/typescript/conformance/classes/constructorDeclarations/constructorParameters/readonlyReadonly.ts @@ -0,0 +1,4 @@ +class C { + readonly readonly x: number; + constructor(readonly readonly y: number) {} +} \ No newline at end of file diff --git a/tests/typescript/conformance/types/thisType/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/conformance/types/thisType/__snapshots__/jsfmt.spec.js.snap index 209d0952..9d1319ff 100644 --- a/tests/typescript/conformance/types/thisType/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/conformance/types/thisType/__snapshots__/jsfmt.spec.js.snap @@ -6,7 +6,7 @@ declare class MyArray extends Array { } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ class MyArray extends Array { - sort(compareFn?: (a: T, b: T) => number): this + sort(compareFn?: (a: T, b: T) => number): this; } `; diff --git a/tests/typescript/custom/abstract/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/custom/abstract/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..b67ac0c2 --- /dev/null +++ b/tests/typescript/custom/abstract/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,32 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`abstractNewlineHandling.ts 1`] = ` +var +abstract +class X {} + +const +abstract +class Y {} + +export let +abstract +class Y {} + +let +abstract +export class Y {} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +var abstract; +class X {} + +const abstract; +class Y {} + +export let abstract; +class Y {} + +let abstract; +export class Y {} + +`; diff --git a/tests/typescript/custom/abstract/abstractNewlineHandling.ts b/tests/typescript/custom/abstract/abstractNewlineHandling.ts new file mode 100644 index 00000000..4487225f --- /dev/null +++ b/tests/typescript/custom/abstract/abstractNewlineHandling.ts @@ -0,0 +1,15 @@ +var +abstract +class X {} + +const +abstract +class Y {} + +export let +abstract +class Y {} + +let +abstract +export class Y {} diff --git a/tests/typescript/custom/abstract/jsfmt.spec.js b/tests/typescript/custom/abstract/jsfmt.spec.js new file mode 100644 index 00000000..bc085c48 --- /dev/null +++ b/tests/typescript/custom/abstract/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, { parser: "typescript" });