diff --git a/changelog_unreleased/flow/11398.md b/changelog_unreleased/flow/11398.md new file mode 100644 index 000000000..02b564d6f --- /dev/null +++ b/changelog_unreleased/flow/11398.md @@ -0,0 +1,26 @@ +#### Print leading semi before Flow variance sigils (#11398 by @noppa) + + +```jsx +// Input +class A { + +one = function() {}; + -two = val(); + +#privOne = val(); + static +#privTwo = val(); +} +// Prettier stable +class A { + +one = function() {} + -two = val() + +#privOne = val() + static +#privTwo = val() +} +// Prettier main +class A { + +one = function() {}; + -two = val(); + +#privOne = val() + static +#privTwo = val() +} +``` diff --git a/src/language-js/print/statement.js b/src/language-js/print/statement.js index 9dc33a742..c7573faf7 100644 --- a/src/language-js/print/statement.js +++ b/src/language-js/print/statement.js @@ -201,6 +201,17 @@ function shouldPrintSemicolonAfterClassProperty(node, nextNode) { } } + // Flow variance sigil +/- requires semi if there's no + // "declare" or "static" keyword before it. + if ( + isClassProperty(nextNode) && + nextNode.variance && + !nextNode.static && + !nextNode.declare + ) { + return true; + } + switch (nextNode.type) { case "ClassProperty": case "PropertyDefinition": diff --git a/tests/format/flow/no-semi/__snapshots__/jsfmt.spec.js.snap b/tests/format/flow/no-semi/__snapshots__/jsfmt.spec.js.snap index 2a25e9c70..36cf0be3c 100644 --- a/tests/format/flow/no-semi/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/flow/no-semi/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,7 @@ exports[`comments.js - {"semi":false} format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow", "babel", "babel-flow"] printWidth: 80 semi: false | printWidth @@ -37,7 +37,7 @@ x exports[`comments.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow", "babel", "babel-flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -69,9 +69,66 @@ x; ================================================================================ `; +exports[`flow-class-properties.js - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["flow", "babel", "babel-flow"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +class A { + +one = function() {}; + -two = val(); + static +three = val(); + +#privOne = val(); + static +#privTwo = val(); + +[computed] = val(); +} + +=====================================output===================================== +class A { + +one = function () {}; + -two = val() + static +three = val(); + +#privOne = val() + static +#privTwo = val(); + +[computed] = val() +} + +================================================================================ +`; + +exports[`flow-class-properties.js format 1`] = ` +====================================options===================================== +parsers: ["flow", "babel", "babel-flow"] +printWidth: 80 + | printWidth +=====================================input====================================== +class A { + +one = function() {}; + -two = val(); + static +three = val(); + +#privOne = val(); + static +#privTwo = val(); + +[computed] = val(); +} + +=====================================output===================================== +class A { + +one = function () {}; + -two = val(); + static +three = val(); + +#privOne = val(); + static +#privTwo = val(); + +[computed] = val(); +} + +================================================================================ +`; + exports[`flow-interfaces.js - {"semi":false} format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow", "babel", "babel-flow"] printWidth: 80 semi: false | printWidth @@ -157,7 +214,7 @@ interface E { exports[`flow-interfaces.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow", "babel", "babel-flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -242,7 +299,7 @@ interface E { exports[`no-semi.js - {"semi":false} format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow", "babel", "babel-flow"] printWidth: 80 semi: false | printWidth @@ -263,7 +320,7 @@ semi: false exports[`no-semi.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow", "babel", "babel-flow"] printWidth: 80 | printWidth =====================================input====================================== diff --git a/tests/format/flow/no-semi/flow-class-properties.js b/tests/format/flow/no-semi/flow-class-properties.js new file mode 100644 index 000000000..e6cd0014e --- /dev/null +++ b/tests/format/flow/no-semi/flow-class-properties.js @@ -0,0 +1,8 @@ +class A { + +one = function() {}; + -two = val(); + static +three = val(); + +#privOne = val(); + static +#privTwo = val(); + +[computed] = val(); +} diff --git a/tests/format/flow/no-semi/jsfmt.spec.js b/tests/format/flow/no-semi/jsfmt.spec.js index 2f93d9870..b01ec0778 100644 --- a/tests/format/flow/no-semi/jsfmt.spec.js +++ b/tests/format/flow/no-semi/jsfmt.spec.js @@ -1,2 +1,2 @@ -run_spec(__dirname, ["flow", "babel"]); -run_spec(__dirname, ["flow", "babel"], { semi: false }); +run_spec(__dirname, ["flow", "babel", "babel-flow"]); +run_spec(__dirname, ["flow", "babel", "babel-flow"], { semi: false });