From 99e4bae321fd395d75fdfd92e94c68977b386531 Mon Sep 17 00:00:00 2001 From: Danny Arnold Date: Fri, 21 Apr 2017 16:57:26 +0200 Subject: [PATCH] add TSConstructorType (#1367) * add TSConstructorType * combine condition * combine strings --- src/printer.js | 11 ++++++++++- .../constructorType/__snapshots__/jsfmt.spec.js.snap | 8 ++++++++ .../types/constructorType/cunstructorType.ts | 1 + .../conformance/types/constructorType/jsfmt.spec.js | 1 + 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tests/typescript/conformance/types/constructorType/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/typescript/conformance/types/constructorType/cunstructorType.ts create mode 100644 tests/typescript/conformance/types/constructorType/jsfmt.spec.js diff --git a/src/printer.js b/src/printer.js index b3afc61f5..fe9cc3dc9 100644 --- a/src/printer.js +++ b/src/printer.js @@ -1515,8 +1515,10 @@ function genericPrintNoParens(path, options, print, args) { path.getParentNode().constraint) && // TypeScript should not have a colon in TSFirstTypeNode nodes // `a is number` + // or TSConstructorType nodes !(path.getParentNode().type === "TypeAnnotation" && - path.getParentNode().typeAnnotation.type === 'TSFirstTypeNode') + (path.getParentNode().typeAnnotation.type === 'TSFirstTypeNode' || + path.getParentNode().typeAnnotation.type === "TSConstructorType")) ) { parts.push(": "); } @@ -1972,6 +1974,13 @@ function genericPrintNoParens(path, options, print, args) { path.call(print, "indexType"), "]" ]) + case "TSConstructorType": + return concat([ + "new(", + join(", ", path.map(print, "parameters")), + ") => ", + path.call(print, "typeAnnotation"), + ]) // TODO case "ClassHeritage": // TODO diff --git a/tests/typescript/conformance/types/constructorType/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/conformance/types/constructorType/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..3961f1fac --- /dev/null +++ b/tests/typescript/conformance/types/constructorType/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,8 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`cunstructorType.ts 1`] = ` +var d: new(x: number) => void; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +var d: new(x: number) => void; + +`; diff --git a/tests/typescript/conformance/types/constructorType/cunstructorType.ts b/tests/typescript/conformance/types/constructorType/cunstructorType.ts new file mode 100644 index 000000000..2308d1d36 --- /dev/null +++ b/tests/typescript/conformance/types/constructorType/cunstructorType.ts @@ -0,0 +1 @@ +var d: new(x: number) => void; diff --git a/tests/typescript/conformance/types/constructorType/jsfmt.spec.js b/tests/typescript/conformance/types/constructorType/jsfmt.spec.js new file mode 100644 index 000000000..bc085c48c --- /dev/null +++ b/tests/typescript/conformance/types/constructorType/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, { parser: "typescript" });