From bb037eb218cbcf914575fe95d9f43e87ffab69a9 Mon Sep 17 00:00:00 2001 From: Matthew Leffler Date: Wed, 25 Sep 2019 08:51:50 -0400 Subject: [PATCH] TypeScript: Improve argument expansion with `as` type expressions (#6471) --- CHANGELOG.unreleased.md | 2 +- src/language-js/printer-estree.js | 4 +- .../__snapshots__/jsfmt.spec.js.snap | 87 +++++++++++++++++++ .../argument_expansion.js | 31 +++++++ .../jsfmt.spec.js | 1 + 5 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 tests/typescript_argument_expansion/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/typescript_argument_expansion/argument_expansion.js create mode 100644 tests/typescript_argument_expansion/jsfmt.spec.js diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index cd68b4a9..6cbf3d08 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -709,4 +709,4 @@ const fooooooooooooooo: SomeThing = looooooooooooooooooooooooooooooongN [@g-harel]: https://github.com/g-harel [@jounqin]: https://github.com/JounQin [@bakkot]: https://gibhub.com/bakkot -[@thorn0]: https://github.com/thorn0 +[@thorn0]: https://github.com/thorn0 \ No newline at end of file diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 425d6d59..aa6f8af8 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -3756,8 +3756,8 @@ function couldGroupArg(arg) { (arg.properties.length > 0 || arg.comments)) || (arg.type === "ArrayExpression" && (arg.elements.length > 0 || arg.comments)) || - arg.type === "TSTypeAssertion" || - arg.type === "TSAsExpression" || + (arg.type === "TSTypeAssertion" && couldGroupArg(arg.expression)) || + (arg.type === "TSAsExpression" && couldGroupArg(arg.expression)) || arg.type === "FunctionExpression" || (arg.type === "ArrowFunctionExpression" && // we want to avoid breaking inside composite return types but not simple keywords diff --git a/tests/typescript_argument_expansion/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_argument_expansion/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..b7530896 --- /dev/null +++ b/tests/typescript_argument_expansion/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,87 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`argument_expansion.js 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +const bar = [1,2,3].reduce((carry, value) => { + return [...carry, value]; +}, ([] as unknown) as number[]); + +const bar = [1,2,3].reduce((carry, value) => { + return [...carry, value]; +}, >[]); + +const bar = [1,2,3].reduce((carry, value) => { + return [...carry, value]; +}, ([1, 2, 3] as unknown) as number[]); + +const bar = [1,2,3].reduce((carry, value) => { + return [...carry, value]; +}, >[1, 2, 3]); + +const bar = [1,2,3].reduce((carry, value) => { + return {...carry, [value]: true}; +}, ({} as unknown) as {[key: number]: boolean}); + +const bar = [1,2,3].reduce((carry, value) => { + return {...carry, [value]: true}; +}, <{[key: number]: boolean}>{}); + +const bar = [1,2,3].reduce((carry, value) => { + return {...carry, [value]: true}; +}, ({1: true} as unknown) as {[key: number]: boolean}); + +const bar = [1,2,3].reduce((carry, value) => { + return {...carry, [value]: true}; +}, <{[key: number]: boolean}>{1: true}); + +=====================================output===================================== +const bar = [1, 2, 3].reduce((carry, value) => { + return [...carry, value]; +}, ([] as unknown) as number[]); + +const bar = [1, 2, 3].reduce((carry, value) => { + return [...carry, value]; +}, >[]); + +const bar = [1, 2, 3].reduce( + (carry, value) => { + return [...carry, value]; + }, + ([1, 2, 3] as unknown) as number[] +); + +const bar = [1, 2, 3].reduce( + (carry, value) => { + return [...carry, value]; + }, + >[1, 2, 3] +); + +const bar = [1, 2, 3].reduce((carry, value) => { + return { ...carry, [value]: true }; +}, ({} as unknown) as { [key: number]: boolean }); + +const bar = [1, 2, 3].reduce((carry, value) => { + return { ...carry, [value]: true }; +}, <{ [key: number]: boolean }>{}); + +const bar = [1, 2, 3].reduce( + (carry, value) => { + return { ...carry, [value]: true }; + }, + ({ 1: true } as unknown) as { [key: number]: boolean } +); + +const bar = [1, 2, 3].reduce( + (carry, value) => { + return { ...carry, [value]: true }; + }, + <{ [key: number]: boolean }>{ 1: true } +); + +================================================================================ +`; diff --git a/tests/typescript_argument_expansion/argument_expansion.js b/tests/typescript_argument_expansion/argument_expansion.js new file mode 100644 index 00000000..361d2833 --- /dev/null +++ b/tests/typescript_argument_expansion/argument_expansion.js @@ -0,0 +1,31 @@ +const bar = [1,2,3].reduce((carry, value) => { + return [...carry, value]; +}, ([] as unknown) as number[]); + +const bar = [1,2,3].reduce((carry, value) => { + return [...carry, value]; +}, >[]); + +const bar = [1,2,3].reduce((carry, value) => { + return [...carry, value]; +}, ([1, 2, 3] as unknown) as number[]); + +const bar = [1,2,3].reduce((carry, value) => { + return [...carry, value]; +}, >[1, 2, 3]); + +const bar = [1,2,3].reduce((carry, value) => { + return {...carry, [value]: true}; +}, ({} as unknown) as {[key: number]: boolean}); + +const bar = [1,2,3].reduce((carry, value) => { + return {...carry, [value]: true}; +}, <{[key: number]: boolean}>{}); + +const bar = [1,2,3].reduce((carry, value) => { + return {...carry, [value]: true}; +}, ({1: true} as unknown) as {[key: number]: boolean}); + +const bar = [1,2,3].reduce((carry, value) => { + return {...carry, [value]: true}; +}, <{[key: number]: boolean}>{1: true}); diff --git a/tests/typescript_argument_expansion/jsfmt.spec.js b/tests/typescript_argument_expansion/jsfmt.spec.js new file mode 100644 index 00000000..2ea3bb6e --- /dev/null +++ b/tests/typescript_argument_expansion/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, ["typescript"]);