From b2657137631468e67bbfa1d2d26b590dc9e3cd8c Mon Sep 17 00:00:00 2001 From: Lucas Azzola Date: Sat, 27 May 2017 13:19:17 +1000 Subject: [PATCH] fix(typescript): handle ExperimentalSpreadOperator, optional method, and abstract interface (#1763) --- src/clean-ast.js | 15 +++++++++++++++ src/printer.js | 3 +++ .../__snapshots__/jsfmt.spec.js.snap | 11 +++++++++++ tests/typescript_class/methods.ts | 3 +++ .../__snapshots__/jsfmt.spec.js.snap | 10 ++++++++++ tests/typescript_interface/abstract.ts | 3 +++ tests/typescript_interface/jsfmt.spec.js | 1 + 7 files changed, 46 insertions(+) create mode 100644 tests/typescript_class/methods.ts create mode 100644 tests/typescript_interface/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/typescript_interface/abstract.ts create mode 100644 tests/typescript_interface/jsfmt.spec.js diff --git a/src/clean-ast.js b/src/clean-ast.js index d4f855989..77892e7b4 100644 --- a/src/clean-ast.js +++ b/src/clean-ast.js @@ -66,6 +66,21 @@ function massageAST(ast) { delete newObj.params; } + // (TypeScript) Ignore `static` in `constructor(static p) {}` + // and `export` in `constructor(export p) {}` + if ( + ast.type === "TSParameterProperty" && + ast.accessibility === null && + !ast.readonly + ) { + return { + type: "Identifier", + name: ast.parameter.name, + typeAnnotation: newObj.parameter.typeAnnotation, + decorators: newObj.decorators + }; + } + // We convert
to
if (ast.type === "JSXOpeningElement") { delete newObj.selfClosing; diff --git a/src/printer.js b/src/printer.js index 896c1a38c..018e223f7 100644 --- a/src/printer.js +++ b/src/printer.js @@ -356,6 +356,7 @@ function genericPrintNoParens(path, options, print, args) { case "SpreadElementPattern": case "RestProperty": case "ExperimentalRestProperty": + case "ExperimentalSpreadProperty": case "SpreadProperty": case "SpreadPropertyPattern": case "RestElement": @@ -779,6 +780,7 @@ function genericPrintNoParens(path, options, print, args) { } case "TSInterfaceDeclaration": parts.push( + n.abstract ? "abstract " : "", printTypeScriptModifiers(path, options, print), "interface ", path.call(print, "id"), @@ -2352,6 +2354,7 @@ function genericPrintNoParens(path, options, print, args) { case "TSMethodSignature": parts.push( path.call(print, "name"), + n.questionToken ? "?" : "", printFunctionTypeParameters(path, options, print), printFunctionParams(path, print, options) ); diff --git a/tests/typescript_class/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_class/__snapshots__/jsfmt.spec.js.snap index f90500277..4642f551d 100644 --- a/tests/typescript_class/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_class/__snapshots__/jsfmt.spec.js.snap @@ -14,3 +14,14 @@ interface AudioBufferList { } `; + +exports[`methods.ts 1`] = ` +class X { + optionalMethod?() {} +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class X { + optionalMethod?() {} +} + +`; diff --git a/tests/typescript_class/methods.ts b/tests/typescript_class/methods.ts new file mode 100644 index 000000000..a1ced7543 --- /dev/null +++ b/tests/typescript_class/methods.ts @@ -0,0 +1,3 @@ +class X { + optionalMethod?() {} +} diff --git a/tests/typescript_interface/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_interface/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..791df4b62 --- /dev/null +++ b/tests/typescript_interface/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,10 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`abstract.ts 1`] = ` +abstract interface I { + +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +abstract interface I {} + +`; diff --git a/tests/typescript_interface/abstract.ts b/tests/typescript_interface/abstract.ts new file mode 100644 index 000000000..e5e1cb201 --- /dev/null +++ b/tests/typescript_interface/abstract.ts @@ -0,0 +1,3 @@ +abstract interface I { + +} diff --git a/tests/typescript_interface/jsfmt.spec.js b/tests/typescript_interface/jsfmt.spec.js new file mode 100644 index 000000000..bc085c48c --- /dev/null +++ b/tests/typescript_interface/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, { parser: "typescript" });