From e1eecfd7eb24922c7147ddca20fa8d7bce87c5ac Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Tue, 6 Oct 2020 15:09:22 +0900 Subject: [PATCH] TypeScript: Preserve a last separator for ignored object and interface types (#9318) * Preserve ignored TypeScript signature * Add Changelog --- changelog_unreleased/typescript/pr-9318.md | 21 ++ src/language-js/printer-estree.js | 8 +- .../__snapshots__/jsfmt.spec.js.snap | 312 ++++++++++++++++++ tests/typescript/interface/ignore.ts | 78 +++++ 4 files changed, 418 insertions(+), 1 deletion(-) create mode 100644 changelog_unreleased/typescript/pr-9318.md diff --git a/changelog_unreleased/typescript/pr-9318.md b/changelog_unreleased/typescript/pr-9318.md new file mode 100644 index 000000000..3a0319863 --- /dev/null +++ b/changelog_unreleased/typescript/pr-9318.md @@ -0,0 +1,21 @@ +#### Preserve a last separator for ignored object and interface types (#9318 by @sosukesuzuki) + +```ts +// Input +let x: { + // prettier-ignore + y: z; +}; + +// Prettier stable +let x: { + // prettier-ignore + y: z;; +}; + +// Prettier master +let x: { + // prettier-ignore + y: z; +}; +``` diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 74c78b6a7..d511eac14 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -1184,7 +1184,13 @@ function printPathNoParens(path, options, print, args) { const canHaveTrailingSeparator = !( n.inexact || - (lastElem && lastElem.type === "RestElement") + (lastElem && lastElem.type === "RestElement") || + (lastElem && + (lastElem.type === "TSPropertySignature" || + lastElem.type === "TSCallSignatureDeclaration" || + lastElem.type === "TSMethodSignature" || + lastElem.type === "TSConstructSignatureDeclaration") && + hasNodeIgnoreComment(lastElem)) ); let content; diff --git a/tests/typescript/interface/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/interface/__snapshots__/jsfmt.spec.js.snap index 385967dbd..393512f52 100644 --- a/tests/typescript/interface/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/interface/__snapshots__/jsfmt.spec.js.snap @@ -314,6 +314,84 @@ interface T { new(): T; } +interface I { + // prettier-ignore + x: y; +} + +interface I { + // prettier-ignore + x: y, +} + +interface I { + // prettier-ignore + x: y +} + +interface I { + // prettier-ignore + x: y; + y: x +} + +interface I { + // prettier-ignore + x: y, + y: x +} + +interface I { + // prettier-ignore + x: y + y: x +} + +interface I { + // prettier-ignore + (): void; +} + +interface I { + // prettier-ignore + (): void, +} + +interface I { + // prettier-ignore + (): void +} + +interface I { + // prettier-ignore + foo(): void; +} + +interface I { + // prettier-ignore + foo(): void, +} + +interface I { + // prettier-ignore + foo(): void +} + +interface I { + // prettier-ignore + new (); +} + +interface I { + // prettier-ignore + new (), +} + +interface I { + // prettier-ignore + new () +} + =====================================output===================================== interface Interface { // prettier-ignore @@ -344,6 +422,84 @@ interface T { new (): T } +interface I { + // prettier-ignore + x: y; +} + +interface I { + // prettier-ignore + x: y, +} + +interface I { + // prettier-ignore + x: y +} + +interface I { + // prettier-ignore + x: y; + y: x +} + +interface I { + // prettier-ignore + x: y, + y: x +} + +interface I { + // prettier-ignore + x: y + y: x +} + +interface I { + // prettier-ignore + (): void; +} + +interface I { + // prettier-ignore + (): void, +} + +interface I { + // prettier-ignore + (): void +} + +interface I { + // prettier-ignore + foo(): void; +} + +interface I { + // prettier-ignore + foo(): void, +} + +interface I { + // prettier-ignore + foo(): void +} + +interface I { + // prettier-ignore + new (); +} + +interface I { + // prettier-ignore + new (), +} + +interface I { + // prettier-ignore + new () +} + ================================================================================ `; @@ -382,6 +538,84 @@ interface T { new(): T; } +interface I { + // prettier-ignore + x: y; +} + +interface I { + // prettier-ignore + x: y, +} + +interface I { + // prettier-ignore + x: y +} + +interface I { + // prettier-ignore + x: y; + y: x +} + +interface I { + // prettier-ignore + x: y, + y: x +} + +interface I { + // prettier-ignore + x: y + y: x +} + +interface I { + // prettier-ignore + (): void; +} + +interface I { + // prettier-ignore + (): void, +} + +interface I { + // prettier-ignore + (): void +} + +interface I { + // prettier-ignore + foo(): void; +} + +interface I { + // prettier-ignore + foo(): void, +} + +interface I { + // prettier-ignore + foo(): void +} + +interface I { + // prettier-ignore + new (); +} + +interface I { + // prettier-ignore + new (), +} + +interface I { + // prettier-ignore + new () +} + =====================================output===================================== interface Interface { // prettier-ignore @@ -412,6 +646,84 @@ interface T { new (): T; } +interface I { + // prettier-ignore + x: y; +} + +interface I { + // prettier-ignore + x: y, +} + +interface I { + // prettier-ignore + x: y +} + +interface I { + // prettier-ignore + x: y; + y: x; +} + +interface I { + // prettier-ignore + x: y, + y: x; +} + +interface I { + // prettier-ignore + x: y + y: x; +} + +interface I { + // prettier-ignore + (): void; +} + +interface I { + // prettier-ignore + (): void, +} + +interface I { + // prettier-ignore + (): void +} + +interface I { + // prettier-ignore + foo(): void; +} + +interface I { + // prettier-ignore + foo(): void, +} + +interface I { + // prettier-ignore + foo(): void +} + +interface I { + // prettier-ignore + new (); +} + +interface I { + // prettier-ignore + new (), +} + +interface I { + // prettier-ignore + new () +} + ================================================================================ `; diff --git a/tests/typescript/interface/ignore.ts b/tests/typescript/interface/ignore.ts index a75ca1ebe..c7cec08c2 100644 --- a/tests/typescript/interface/ignore.ts +++ b/tests/typescript/interface/ignore.ts @@ -26,3 +26,81 @@ interface T { new(): T; new(): T; } + +interface I { + // prettier-ignore + x: y; +} + +interface I { + // prettier-ignore + x: y, +} + +interface I { + // prettier-ignore + x: y +} + +interface I { + // prettier-ignore + x: y; + y: x +} + +interface I { + // prettier-ignore + x: y, + y: x +} + +interface I { + // prettier-ignore + x: y + y: x +} + +interface I { + // prettier-ignore + (): void; +} + +interface I { + // prettier-ignore + (): void, +} + +interface I { + // prettier-ignore + (): void +} + +interface I { + // prettier-ignore + foo(): void; +} + +interface I { + // prettier-ignore + foo(): void, +} + +interface I { + // prettier-ignore + foo(): void +} + +interface I { + // prettier-ignore + new (); +} + +interface I { + // prettier-ignore + new (), +} + +interface I { + // prettier-ignore + new () +}