From a8fe401b26a019b5cab10817807d49204335a711 Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Sat, 17 Feb 2018 00:40:43 -0600 Subject: [PATCH] Fix cases with missing semi when printing DeclareExportDeclaration (#3979) --- src/language-js/printer-estree.js | 10 ++- .../__snapshots__/jsfmt.spec.js.snap | 6 +- .../__snapshots__/jsfmt.spec.js.snap | 82 +++++++++++++++++++ .../flow_type_declarations/declare_export.js | 1 + tests/flow_type_declarations/jsfmt.spec.js | 1 + 5 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 tests/flow_type_declarations/declare_export.js diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 5c4515413..a73586783 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -3485,7 +3485,9 @@ function printExportDeclaration(path, options, print) { const semi = options.semi ? ";" : ""; const parts = ["export "]; - if (decl["default"] || decl.type === "ExportDefaultDeclaration") { + const isDefault = decl["default"] || decl.type === "ExportDefaultDeclaration"; + + if (isDefault) { parts.push("default "); } @@ -3497,10 +3499,12 @@ function printExportDeclaration(path, options, print) { parts.push(path.call(print, "declaration")); if ( - decl.type === "ExportDefaultDeclaration" && + isDefault && (decl.declaration.type !== "ClassDeclaration" && decl.declaration.type !== "FunctionDeclaration" && - decl.declaration.type !== "TSAbstractClassDeclaration") + decl.declaration.type !== "TSAbstractClassDeclaration" && + decl.declaration.type !== "DeclareClass" && + decl.declaration.type !== "DeclareFunction") ) { parts.push(semi); } diff --git a/tests/flow/declare_export/__snapshots__/jsfmt.spec.js.snap b/tests/flow/declare_export/__snapshots__/jsfmt.spec.js.snap index 6e430513e..7de365958 100644 --- a/tests/flow/declare_export/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/declare_export/__snapshots__/jsfmt.spec.js.snap @@ -142,7 +142,7 @@ declare export default () => number; * @flow */ -declare export default () => number +declare export default () => number; `; @@ -159,7 +159,7 @@ declare export default () =>number; * @flow */ -declare export default () => number +declare export default () => number; `; @@ -251,7 +251,7 @@ declare export var str: string; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* @flow */ -declare export default number +declare export default number; declare export var str: string; `; diff --git a/tests/flow_type_declarations/__snapshots__/jsfmt.spec.js.snap b/tests/flow_type_declarations/__snapshots__/jsfmt.spec.js.snap index d93499662..20c36519c 100644 --- a/tests/flow_type_declarations/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow_type_declarations/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,19 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`declare_export.js 1`] = ` +declare export default 5; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +declare export default 5; + +`; + +exports[`declare_export.js 2`] = ` +declare export default 5; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +declare export default 5 + +`; + exports[`declare_type.js 1`] = ` declare type A = string; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -7,6 +21,13 @@ declare type A = string; `; +exports[`declare_type.js 2`] = ` +declare type A = string; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +declare type A = string + +`; + exports[`declare_var.js 1`] = ` declare var bool: React$PropType$Primitive; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -14,6 +35,13 @@ declare var bool: React$PropType$Primitive; `; +exports[`declare_var.js 2`] = ` +declare var bool: React$PropType$Primitive; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +declare var bool: React$PropType$Primitive + +`; + exports[`long.js 1`] = ` export type AdamPlacementValidationSingleErrorKey = 'SOME_FANCY_TARGETS.GLOBAL_TARGET'; @@ -35,6 +63,27 @@ type SomeOtherLongLongLongLongLongLongLongLongLongLongLongLongLongLongKey = Some `; +exports[`long.js 2`] = ` +export type AdamPlacementValidationSingleErrorKey = + 'SOME_FANCY_TARGETS.GLOBAL_TARGET'; + +export type SomeReallyLongLongLongLongLongLongLongLongLongLongLongLongLongLongKey = true; + +export type SomeOtherLongLongLongLongLongLongLongLongLongLongLongLongLongLongKey = null; + +type SomeOtherLongLongLongLongLongLongLongLongLongLongLongLongLongLongKey = SomeReallyLongLongLongLongLongLongIdentifier; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +export type AdamPlacementValidationSingleErrorKey = + "SOME_FANCY_TARGETS.GLOBAL_TARGET" + +export type SomeReallyLongLongLongLongLongLongLongLongLongLongLongLongLongLongKey = true + +export type SomeOtherLongLongLongLongLongLongLongLongLongLongLongLongLongLongKey = null + +type SomeOtherLongLongLongLongLongLongLongLongLongLongLongLongLongLongKey = SomeReallyLongLongLongLongLongLongIdentifier + +`; + exports[`opaque.js 1`] = ` declare export opaque type Foo; declare export opaque type Bar; @@ -67,3 +116,36 @@ opaque type union = { type: "A" } | { type: "B" }; opaque type overloads = ((x: string) => number) & ((x: number) => string); `; + +exports[`opaque.js 2`] = ` +declare export opaque type Foo; +declare export opaque type Bar; +declare export opaque type Baz: Foo; +declare export opaque type Foo: Bar; +declare export opaque type Foo: Bar; +declare export opaque type Foo: Bar; +opaque type ID = string; +opaque type Foo = Bar; +opaque type Maybe = _Maybe; +export opaque type Foo = number; +opaque type union = + | {type: "A"} + | {type: "B"}; +opaque type overloads = + & ((x: string) => number) + & ((x: number) => string); +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +declare export opaque type Foo +declare export opaque type Bar +declare export opaque type Baz: Foo +declare export opaque type Foo: Bar +declare export opaque type Foo: Bar +declare export opaque type Foo: Bar +opaque type ID = string +opaque type Foo = Bar +opaque type Maybe = _Maybe +export opaque type Foo = number +opaque type union = { type: "A" } | { type: "B" } +opaque type overloads = ((x: string) => number) & ((x: number) => string) + +`; diff --git a/tests/flow_type_declarations/declare_export.js b/tests/flow_type_declarations/declare_export.js new file mode 100644 index 000000000..75b7fac8e --- /dev/null +++ b/tests/flow_type_declarations/declare_export.js @@ -0,0 +1 @@ +declare export default 5; diff --git a/tests/flow_type_declarations/jsfmt.spec.js b/tests/flow_type_declarations/jsfmt.spec.js index c1ba82f46..c5f8baf64 100644 --- a/tests/flow_type_declarations/jsfmt.spec.js +++ b/tests/flow_type_declarations/jsfmt.spec.js @@ -1 +1,2 @@ run_spec(__dirname, ["flow", "babylon"]); +run_spec(__dirname, ["flow", "babylon"], { semi: false });