From 91bcde9859dcae08b0a7ae0c06bf7706659f5605 Mon Sep 17 00:00:00 2001 From: Lucas Azzola Date: Sun, 28 May 2017 00:24:54 +1000 Subject: [PATCH] fix(typescript): fix unstable optional method (#1770) --- src/printer.js | 2 +- tests/typescript_method/__snapshots__/jsfmt.spec.js.snap | 8 ++++++++ tests/typescript_method/jsfmt.spec.js | 1 + tests/typescript_method/type_literal_optional_method.ts | 1 + 4 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 tests/typescript_method/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/typescript_method/jsfmt.spec.js create mode 100644 tests/typescript_method/type_literal_optional_method.ts diff --git a/src/printer.js b/src/printer.js index 36ee8ed6..32ba5f90 100644 --- a/src/printer.js +++ b/src/printer.js @@ -2354,7 +2354,7 @@ function genericPrintNoParens(path, options, print, args) { case "TSMethodSignature": parts.push( path.call(print, "name"), - n.questionToken ? "?" : "", + n.questionToken && !n.name.optional ? "?" : "", printFunctionTypeParameters(path, options, print), printFunctionParams(path, print, options) ); diff --git a/tests/typescript_method/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_method/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..c821e79a --- /dev/null +++ b/tests/typescript_method/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,8 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`type_literal_optional_method.ts 1`] = ` +var v: { e?(): number }; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +var v: { e?(): number }; + +`; diff --git a/tests/typescript_method/jsfmt.spec.js b/tests/typescript_method/jsfmt.spec.js new file mode 100644 index 00000000..bc085c48 --- /dev/null +++ b/tests/typescript_method/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, { parser: "typescript" }); diff --git a/tests/typescript_method/type_literal_optional_method.ts b/tests/typescript_method/type_literal_optional_method.ts new file mode 100644 index 00000000..548b506e --- /dev/null +++ b/tests/typescript_method/type_literal_optional_method.ts @@ -0,0 +1 @@ +var v: { e?(): number };