From 6dbef8681e829199900b2b030492e6544bcf31ba Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Wed, 19 Aug 2020 23:51:49 +0900 Subject: [PATCH] Fix interface extends with long type parameters (#8981) * Try the same way as #8967 * Update src/language-js/printer-estree.js Co-authored-by: fisker Cheung Co-authored-by: fisker Cheung --- src/language-js/printer-estree.js | 29 ++++++- .../__snapshots__/jsfmt.spec.js.snap | 83 +++++++++++++++++++ .../typescript/interface/comments-generic.ts | 6 ++ tests/typescript/interface/generic.ts | 8 ++ 4 files changed, 122 insertions(+), 4 deletions(-) create mode 100644 tests/typescript/interface/generic.ts diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 1f8955ea6..e718b9961 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -2605,6 +2605,7 @@ function printPathNoParens(path, options, print, args) { parts.push("interface"); const partsGroup = []; + const extendsParts = []; if (n.type !== "InterfaceTypeAnnotation") { partsGroup.push( @@ -2614,9 +2615,16 @@ function printPathNoParens(path, options, print, args) { ); } + const shouldIndentOnlyHeritageClauses = + n.typeParameters && !hasTrailingLineComment(n.typeParameters); + if (n.extends && n.extends.length !== 0) { - partsGroup.push( - line, + extendsParts.push( + shouldIndentOnlyHeritageClauses + ? ifBreak(" ", line, { + groupId: getTypeParametersGroupId(n.typeParameters), + }) + : line, "extends ", (n.extends.length === 1 ? identity : indent)( join(concat([",", line]), path.map(print, "extends")) @@ -2628,9 +2636,22 @@ function printPathNoParens(path, options, print, args) { (n.id && hasTrailingComment(n.id)) || (n.extends && n.extends.length !== 0) ) { - parts.push(group(indent(concat(partsGroup)))); + const printedExtends = concat(extendsParts); + if (shouldIndentOnlyHeritageClauses) { + parts.push( + group( + concat( + partsGroup.concat( + ifBreak(indent(printedExtends), printedExtends) + ) + ) + ) + ); + } else { + parts.push(group(indent(concat(partsGroup.concat(printedExtends))))); + } } else { - parts.push(...partsGroup); + parts.push(...partsGroup, ...extendsParts); } parts.push(" ", path.call(print, "body")); diff --git a/tests/typescript/interface/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/interface/__snapshots__/jsfmt.spec.js.snap index 228f14520..385967dbd 100644 --- a/tests/typescript/interface/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/interface/__snapshots__/jsfmt.spec.js.snap @@ -105,6 +105,12 @@ interface ReallyReallyLongName3< extends BaseInterface // 3 {} +interface Foo< + FOOOOOOOOOOOOOOOOOOOOOOOOOO, + FOOOOOOOOOOOOOOOOOOOOOOOOOO, + FOOOOOOOOOOOOOOOOOOOOOOOOOO +> // comments + extends Foo {} =====================================output===================================== interface ReallyReallyLongName< @@ -132,6 +138,13 @@ interface ReallyReallyLongName3< // 3 } +interface Foo< + FOOOOOOOOOOOOOOOOOOOOOOOOOO, + FOOOOOOOOOOOOOOOOOOOOOOOOOO, + FOOOOOOOOOOOOOOOOOOOOOOOOOO + > // comments + extends Foo {} + ================================================================================ `; @@ -165,6 +178,12 @@ interface ReallyReallyLongName3< extends BaseInterface // 3 {} +interface Foo< + FOOOOOOOOOOOOOOOOOOOOOOOOOO, + FOOOOOOOOOOOOOOOOOOOOOOOOOO, + FOOOOOOOOOOOOOOOOOOOOOOOOOO +> // comments + extends Foo {} =====================================output===================================== interface ReallyReallyLongName< @@ -192,6 +211,70 @@ interface ReallyReallyLongName3< // 3 } +interface Foo< + FOOOOOOOOOOOOOOOOOOOOOOOOOO, + FOOOOOOOOOOOOOOOOOOOOOOOOOO, + FOOOOOOOOOOOOOOOOOOOOOOOOOO + > // comments + extends Foo {} + +================================================================================ +`; + +exports[`generic.ts - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +interface Foo + extends Foo {} + +interface Foo< + FOOOOOOOOOOOOOOOOOOOOOOOOOO, + FOOOOOOOOOOOOOOOOOOOOOOOOOO, + FOOOOOOOOOOOOOOOOOOOOOOOOOO +> extends Foo {} + +=====================================output===================================== +interface Foo + extends Foo {} + +interface Foo< + FOOOOOOOOOOOOOOOOOOOOOOOOOO, + FOOOOOOOOOOOOOOOOOOOOOOOOOO, + FOOOOOOOOOOOOOOOOOOOOOOOOOO +> extends Foo {} + +================================================================================ +`; + +exports[`generic.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +interface Foo + extends Foo {} + +interface Foo< + FOOOOOOOOOOOOOOOOOOOOOOOOOO, + FOOOOOOOOOOOOOOOOOOOOOOOOOO, + FOOOOOOOOOOOOOOOOOOOOOOOOOO +> extends Foo {} + +=====================================output===================================== +interface Foo + extends Foo {} + +interface Foo< + FOOOOOOOOOOOOOOOOOOOOOOOOOO, + FOOOOOOOOOOOOOOOOOOOOOOOOOO, + FOOOOOOOOOOOOOOOOOOOOOOOOOO +> extends Foo {} + ================================================================================ `; diff --git a/tests/typescript/interface/comments-generic.ts b/tests/typescript/interface/comments-generic.ts index 015bb7bfa..1c8e0e8a0 100644 --- a/tests/typescript/interface/comments-generic.ts +++ b/tests/typescript/interface/comments-generic.ts @@ -22,3 +22,9 @@ interface ReallyReallyLongName3< extends BaseInterface // 3 {} +interface Foo< + FOOOOOOOOOOOOOOOOOOOOOOOOOO, + FOOOOOOOOOOOOOOOOOOOOOOOOOO, + FOOOOOOOOOOOOOOOOOOOOOOOOOO +> // comments + extends Foo {} diff --git a/tests/typescript/interface/generic.ts b/tests/typescript/interface/generic.ts new file mode 100644 index 000000000..1dab02216 --- /dev/null +++ b/tests/typescript/interface/generic.ts @@ -0,0 +1,8 @@ +interface Foo + extends Foo {} + +interface Foo< + FOOOOOOOOOOOOOOOOOOOOOOOOOO, + FOOOOOOOOOOOOOOOOOOOOOOOOOO, + FOOOOOOOOOOOOOOOOOOOOOOOOOO +> extends Foo {}