From 92cf9452e9b102520ea1e93ee133cc2f6bd8d633 Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Mon, 18 Jan 2021 09:45:37 +0200 Subject: [PATCH] Disable Babel's error recovery for omitted call arguments (#10065) --- changelog_unreleased/javascript/9787.md | 6 ++- src/language-js/parser-babel.js | 3 ++ src/language-js/utils.js | 18 +++---- .../invalid/__snapshots__/jsfmt.spec.js.snap | 28 +++++++++++ .../jsfmt.spec.js | 1 + .../null-arguments-item.js | 0 .../__snapshots__/jsfmt.spec.js.snap | 50 ------------------- .../js/tuple/__snapshots__/jsfmt.spec.js.snap | 21 +++----- tests/js/tuple/jsfmt.spec.js | 8 ++- 9 files changed, 58 insertions(+), 77 deletions(-) create mode 100644 tests/js/call/invalid/__snapshots__/jsfmt.spec.js.snap rename tests/js/call/{null-arguments-item => invalid}/jsfmt.spec.js (84%) rename tests/js/call/{null-arguments-item => invalid}/null-arguments-item.js (100%) delete mode 100644 tests/js/call/null-arguments-item/__snapshots__/jsfmt.spec.js.snap diff --git a/changelog_unreleased/javascript/9787.md b/changelog_unreleased/javascript/9787.md index 8a8c45ee4..e24fb76a2 100644 --- a/changelog_unreleased/javascript/9787.md +++ b/changelog_unreleased/javascript/9787.md @@ -1,4 +1,4 @@ -#### Print null items in arguments (#9787 by @sosukesuzuki) +#### Disable Babel's error recovery for omitted call arguments (#9787 by @sosukesuzuki, #10065 by @thorn0) ```js @@ -9,5 +9,7 @@ foo("a", , "b"); TypeError: Cannot read property 'type' of null // Prettier master -foo("a", , "b"); +[error] stdin: SyntaxError: Argument expression expected. (1:10) +[error] > 1 | foo("a", , "b"); +[error] | ^ ``` diff --git a/src/language-js/parser-babel.js b/src/language-js/parser-babel.js index 9abcc93e9..c778462b3 100644 --- a/src/language-js/parser-babel.js +++ b/src/language-js/parser-babel.js @@ -164,6 +164,9 @@ const messagesShouldThrow = new Set([ // FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction // https://github.com/babel/babel/blob/a023b6456cac4505096028f91c5b78829955bfc2/packages/babel-parser/src/plugins/flow.js#L118 "Type parameters must come after the async keyword, e.g. instead of ` async () => {}`, use `async () => {}`", + // Rethrow on omitted call arguments: foo("a", , "b"); + // ErrorMessages.UnexpectedToken + "Unexpected token ','", ]); function shouldRethrowRecoveredError(error) { diff --git a/src/language-js/utils.js b/src/language-js/utils.js index 4605e8490..415816d3d 100644 --- a/src/language-js/utils.js +++ b/src/language-js/utils.js @@ -834,18 +834,16 @@ function isFunctionCompositionArgs(args) { } let count = 0; for (const arg of args) { - if (arg) { - if (isFunctionOrArrowExpression(arg)) { - count += 1; - if (count > 1) { + if (isFunctionOrArrowExpression(arg)) { + count += 1; + if (count > 1) { + return true; + } + } else if (isCallOrOptionalCallExpression(arg)) { + for (const childArg of arg.arguments) { + if (isFunctionOrArrowExpression(childArg)) { return true; } - } else if (isCallOrOptionalCallExpression(arg)) { - for (const childArg of arg.arguments) { - if (isFunctionOrArrowExpression(childArg)) { - return true; - } - } } } } diff --git a/tests/js/call/invalid/__snapshots__/jsfmt.spec.js.snap b/tests/js/call/invalid/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..23947f3ac --- /dev/null +++ b/tests/js/call/invalid/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,28 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`null-arguments-item.js [babel] format 1`] = ` +"Unexpected token ',' (1:12) +> 1 | foor('a', , 'b'); + | ^ + 2 | foo('looooooooooooooooooooooooooooooooooooooooooooooong', , 'looooooooooooooooooooooooooooooooooooooooooooooong'); + 3 | foo(\\"a\\", + 4 | //1" +`; + +exports[`null-arguments-item.js [espree] format 1`] = ` +"Unexpected token , (1:11) +> 1 | foor('a', , 'b'); + | ^ + 2 | foo('looooooooooooooooooooooooooooooooooooooooooooooong', , 'looooooooooooooooooooooooooooooooooooooooooooooong'); + 3 | foo(\\"a\\", + 4 | //1" +`; + +exports[`null-arguments-item.js [meriyah] format 1`] = ` +"[1:11]: Unexpected token: ',' (1:11) +> 1 | foor('a', , 'b'); + | ^ + 2 | foo('looooooooooooooooooooooooooooooooooooooooooooooong', , 'looooooooooooooooooooooooooooooooooooooooooooooong'); + 3 | foo(\\"a\\", + 4 | //1" +`; diff --git a/tests/js/call/null-arguments-item/jsfmt.spec.js b/tests/js/call/invalid/jsfmt.spec.js similarity index 84% rename from tests/js/call/null-arguments-item/jsfmt.spec.js rename to tests/js/call/invalid/jsfmt.spec.js index c57dff579..0142a0836 100644 --- a/tests/js/call/null-arguments-item/jsfmt.spec.js +++ b/tests/js/call/invalid/jsfmt.spec.js @@ -1,5 +1,6 @@ run_spec(__dirname, ["babel"], { errors: { + babel: true, espree: true, meriyah: true, }, diff --git a/tests/js/call/null-arguments-item/null-arguments-item.js b/tests/js/call/invalid/null-arguments-item.js similarity index 100% rename from tests/js/call/null-arguments-item/null-arguments-item.js rename to tests/js/call/invalid/null-arguments-item.js diff --git a/tests/js/call/null-arguments-item/__snapshots__/jsfmt.spec.js.snap b/tests/js/call/null-arguments-item/__snapshots__/jsfmt.spec.js.snap deleted file mode 100644 index a09473f69..000000000 --- a/tests/js/call/null-arguments-item/__snapshots__/jsfmt.spec.js.snap +++ /dev/null @@ -1,50 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`null-arguments-item.js [espree] format 1`] = ` -"Unexpected token , (1:11) -> 1 | foor('a', , 'b'); - | ^ - 2 | foo('looooooooooooooooooooooooooooooooooooooooooooooong', , 'looooooooooooooooooooooooooooooooooooooooooooooong'); - 3 | foo(\\"a\\", - 4 | //1" -`; - -exports[`null-arguments-item.js [meriyah] format 1`] = ` -"[1:11]: Unexpected token: ',' (1:11) -> 1 | foor('a', , 'b'); - | ^ - 2 | foo('looooooooooooooooooooooooooooooooooooooooooooooong', , 'looooooooooooooooooooooooooooooooooooooooooooooong'); - 3 | foo(\\"a\\", - 4 | //1" -`; - -exports[`null-arguments-item.js format 1`] = ` -====================================options===================================== -parsers: ["babel"] -printWidth: 80 - | printWidth -=====================================input====================================== -foor('a', , 'b'); -foo('looooooooooooooooooooooooooooooooooooooooooooooong', , 'looooooooooooooooooooooooooooooooooooooooooooooong'); -foo("a", - //1 - , "b"); -foo("a", /* comment */, "b"); - -=====================================output===================================== -foor("a", , "b"); -foo( - "looooooooooooooooooooooooooooooooooooooooooooooong", - , - "looooooooooooooooooooooooooooooooooooooooooooooong" -); -foo( - "a", - , - //1 - "b" -); -foo("a" /* comment */, , "b"); - -================================================================================ -`; diff --git a/tests/js/tuple/__snapshots__/jsfmt.spec.js.snap b/tests/js/tuple/__snapshots__/jsfmt.spec.js.snap index 68255cd61..0f922022f 100644 --- a/tests/js/tuple/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/tuple/__snapshots__/jsfmt.spec.js.snap @@ -48,6 +48,13 @@ assert(rest[1] === 3); ================================================================================ `; +exports[`invalid-tuple-holes.js [babel] format 1`] = ` +"Unexpected token ',' (1:4) +> 1 | #[,] + | ^ + 2 | " +`; + exports[`invalid-tuple-holes.js [espree] format 1`] = ` "Unexpected character '#' (1:1) > 1 | #[,] @@ -62,20 +69,6 @@ exports[`invalid-tuple-holes.js [meriyah] format 1`] = ` 2 | " `; -exports[`invalid-tuple-holes.js format 1`] = ` -====================================options===================================== -parsers: ["babel"] -printWidth: 80 - | printWidth -=====================================input====================================== -#[,] - -=====================================output===================================== -#[,]; - -================================================================================ -`; - exports[`syntax.js [espree] format 1`] = ` "Unexpected character '#' (1:1) > 1 | #[] diff --git a/tests/js/tuple/jsfmt.spec.js b/tests/js/tuple/jsfmt.spec.js index 0fab4456d..71a06d090 100644 --- a/tests/js/tuple/jsfmt.spec.js +++ b/tests/js/tuple/jsfmt.spec.js @@ -1 +1,7 @@ -run_spec(__dirname, ["babel"], { errors: { espree: true, meriyah: true } }); +run_spec(__dirname, ["babel"], { + errors: { + babel: ["invalid-tuple-holes.js"], + espree: true, + meriyah: true, + }, +});