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 <lionkay@gmail.com>

Co-authored-by: fisker Cheung <lionkay@gmail.com>
This commit is contained in:
Sosuke Suzukiandfisker Cheung authored and GitHub committed 2020-08-19 23:51:49 +09:00
1 parent d1a9d7420b
commit 6dbef8681e
4 files changed
+122 -4

No files matched your search

+25 -4
View File
@@ -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"));
@@ -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<FOOOOOOOOOOOOOOOOOOOOOOOOOO,FOOOOOOOOOOOOOOOOOOOOOOO>
extends Foo {}
interface Foo<
FOOOOOOOOOOOOOOOOOOOOOOOOOO,
FOOOOOOOOOOOOOOOOOOOOOOOOOO,
FOOOOOOOOOOOOOOOOOOOOOOOOOO
> extends Foo {}
=====================================output=====================================
interface Foo<FOOOOOOOOOOOOOOOOOOOOOOOOOO, FOOOOOOOOOOOOOOOOOOOOOOO>
extends Foo {}
interface Foo<
FOOOOOOOOOOOOOOOOOOOOOOOOOO,
FOOOOOOOOOOOOOOOOOOOOOOOOOO,
FOOOOOOOOOOOOOOOOOOOOOOOOOO
> extends Foo {}
================================================================================
`;
exports[`generic.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
interface Foo<FOOOOOOOOOOOOOOOOOOOOOOOOOO,FOOOOOOOOOOOOOOOOOOOOOOO>
extends Foo {}
interface Foo<
FOOOOOOOOOOOOOOOOOOOOOOOOOO,
FOOOOOOOOOOOOOOOOOOOOOOOOOO,
FOOOOOOOOOOOOOOOOOOOOOOOOOO
> extends Foo {}
=====================================output=====================================
interface Foo<FOOOOOOOOOOOOOOOOOOOOOOOOOO, FOOOOOOOOOOOOOOOOOOOOOOO>
extends Foo {}
interface Foo<
FOOOOOOOOOOOOOOOOOOOOOOOOOO,
FOOOOOOOOOOOOOOOOOOOOOOOOOO,
FOOOOOOOOOOOOOOOOOOOOOOOOOO
> extends Foo {}
================================================================================
`;
@@ -22,3 +22,9 @@ interface ReallyReallyLongName3<
extends BaseInterface // 3
{}
interface Foo<
FOOOOOOOOOOOOOOOOOOOOOOOOOO,
FOOOOOOOOOOOOOOOOOOOOOOOOOO,
FOOOOOOOOOOOOOOOOOOOOOOOOOO
> // comments
extends Foo {}
+8
View File
@@ -0,0 +1,8 @@
interface Foo<FOOOOOOOOOOOOOOOOOOOOOOOOOO,FOOOOOOOOOOOOOOOOOOOOOOO>
extends Foo {}
interface Foo<
FOOOOOOOOOOOOOOOOOOOOOOOOOO,
FOOOOOOOOOOOOOOOOOOOOOOOOOO,
FOOOOOOOOOOOOOOOOOOOOOOOOOO
> extends Foo {}