diff --git a/changelog_unreleased/javascript/10334.md b/changelog_unreleased/javascript/10334.md new file mode 100644 index 000000000..8d41c6fe8 --- /dev/null +++ b/changelog_unreleased/javascript/10334.md @@ -0,0 +1,22 @@ +#### Fix ASI protection for private fields (#10334 by @thorn0) + + +```jsx +// Input +class C { + #field = 'value'; + ["method"]() {} +} + +// Prettier stable (with --no-semi) +class C { + #field = "value" + ["method"]() {} +} + +// Prettier main (with --no-semi) +class C { + #field = "value"; + ["method"]() {} +} +``` diff --git a/src/language-js/print/statement.js b/src/language-js/print/statement.js index cc70d2f38..24f5cdbc7 100644 --- a/src/language-js/print/statement.js +++ b/src/language-js/print/statement.js @@ -59,7 +59,8 @@ function printStatementSequence(path, options, print, property) { parts.push(";"); } else if ( node.type === "ClassProperty" || - node.type === "PropertyDefinition" + node.type === "PropertyDefinition" || + node.type === "ClassPrivateProperty" ) { // `ClassBody` don't allow `EmptyStatement`, // so we can use `statements` to get next node diff --git a/tests/js/no-semi/__snapshots__/jsfmt.spec.js.snap b/tests/js/no-semi/__snapshots__/jsfmt.spec.js.snap index 609486589..5435656fc 100644 --- a/tests/js/no-semi/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/no-semi/__snapshots__/jsfmt.spec.js.snap @@ -1007,3 +1007,64 @@ aReallyLongLine012345678901234567890123456789012345678901234567890123456789 * ================================================================================ `; + +exports[`private-field.js [espree] format 1`] = ` +"Unexpected character '#' (2:3) + 1 | class C { +> 2 | #field = 'value'; + | ^ + 3 | [\\"method\\"]() {} + 4 | } + 5 |" +`; + +exports[`private-field.js - {"semi":false} [espree] format 1`] = ` +"Unexpected character '#' (2:3) + 1 | class C { +> 2 | #field = 'value'; + | ^ + 3 | [\\"method\\"]() {} + 4 | } + 5 |" +`; + +exports[`private-field.js - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +class C { + #field = 'value'; + ["method"]() {} +} + +=====================================output===================================== +class C { + #field = "value"; + ["method"]() {} +} + +================================================================================ +`; + +exports[`private-field.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow"] +printWidth: 80 + | printWidth +=====================================input====================================== +class C { + #field = 'value'; + ["method"]() {} +} + +=====================================output===================================== +class C { + #field = "value"; + ["method"]() {} +} + +================================================================================ +`; diff --git a/tests/js/no-semi/jsfmt.spec.js b/tests/js/no-semi/jsfmt.spec.js index 98d0477a5..c5222b708 100644 --- a/tests/js/no-semi/jsfmt.spec.js +++ b/tests/js/no-semi/jsfmt.spec.js @@ -1,4 +1,4 @@ -const errors = { espree: ["class.js"] }; +const errors = { espree: ["class.js", "private-field.js"] }; run_spec(__dirname, ["babel", "flow"], { errors }); run_spec(__dirname, ["babel", "flow"], { semi: false, errors }); diff --git a/tests/js/no-semi/private-field.js b/tests/js/no-semi/private-field.js new file mode 100644 index 000000000..feca225a4 --- /dev/null +++ b/tests/js/no-semi/private-field.js @@ -0,0 +1,4 @@ +class C { + #field = 'value'; + ["method"]() {} +}