Skip to content

Commit

Permalink
Remove hacks for single-element unions/intersections (#10457)
Browse files Browse the repository at this point in the history
* Remove hacks for degenerate unions/intersection

* Add changelog
  • Loading branch information
thorn0 committed Mar 3, 2021
1 parent 5f8fee4 commit 37aa31e
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 29 deletions.
13 changes: 13 additions & 0 deletions changelog_unreleased/typescript/10457.md
@@ -0,0 +1,13 @@
#### Fix misplaced comments in unions and intersections (#10457 by @thorn0)

<!-- prettier-ignore -->
```ts
// Input
type Foo = "1" | "2" /* two */ | "3";

// Prettier stable
type Foo = "1" | "2" | /* two */ "3";

// Prettier main
type Foo = "1" | "2" /* two */ | "3";
```
8 changes: 8 additions & 0 deletions src/language-js/clean.js
Expand Up @@ -197,6 +197,14 @@ function clean(ast, newObj, parent) {
if (ast.type === "InterpreterDirective") {
newObj.value = newObj.value.trimEnd();
}

// Prettier removes degenerate union and intersection types with only one member.
if (
(ast.type === "TSIntersectionType" || ast.type === "TSUnionType") &&
ast.types.length === 1
) {
return newObj.types[0];
}
}

clean.ignoredProperties = ignoredProperties;
Expand Down
17 changes: 0 additions & 17 deletions src/language-js/comments.js
Expand Up @@ -886,22 +886,6 @@ function isRealFunctionLikeNode(node) {
);
}

/**
* @param {Node} enclosingNode
* @returns {RegExp | void}
*/
function getGapRegex(enclosingNode) {
if (
enclosingNode &&
enclosingNode.type !== "BinaryExpression" &&
enclosingNode.type !== "LogicalExpression"
) {
// Support degenerate single-element unions and intersections.
// E.g.: `type A = /* 1 */ & B`
return /^[\s&(|]*$/;
}
}

/**
* @param {any} node
* @returns {Node[] | void}
Expand Down Expand Up @@ -980,7 +964,6 @@ module.exports = {
handleEndOfLineComment,
handleRemainingComment,
isTypeCastComment,
getGapRegex,
getCommentChildNodes,
willPrintOwnComments,
};
6 changes: 4 additions & 2 deletions src/language-js/needs-parens.js
Expand Up @@ -405,8 +405,10 @@ function needsParens(path, options) {
case "TSUnionType":
case "TSIntersectionType":
if (
parent.type === "TSUnionType" ||
parent.type === "TSIntersectionType"
(parent.type === "TSUnionType" ||
parent.type === "TSIntersectionType") &&
parent.types.length > 1 &&
(!node.types || node.types.length > 1)
) {
return true;
}
Expand Down
9 changes: 0 additions & 9 deletions src/language-js/parse-postprocess.js
Expand Up @@ -129,15 +129,6 @@ function postprocess(ast, options) {
node.typeAnnotation.range = [locStart(node), locEnd(node)];
return node.typeAnnotation;
}
case "TSUnionType":
case "TSIntersectionType":
if (node.types.length === 1) {
const [firstType] = node.types;
// override loc, so that comments are attached properly
firstType.range = [locStart(node), locEnd(node)];
return firstType;
}
break;
case "TSTypeParameter":
// babel-ts
if (typeof node.name === "string") {
Expand Down
1 change: 0 additions & 1 deletion src/language-js/printer-estree.js
Expand Up @@ -1088,6 +1088,5 @@ module.exports = {
endOfLine: handleComments.handleEndOfLineComment,
remaining: handleComments.handleRemainingComment,
},
getGapRegex: handleComments.getGapRegex,
getCommentChildNodes: handleComments.getCommentChildNodes,
};
32 changes: 32 additions & 0 deletions tests/typescript/union/__snapshots__/jsfmt.spec.js.snap
@@ -1,5 +1,37 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`comments.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
type A1 = a /* 1 */ | b;
type A2 = a | /* 1 */ b;
type A3 = (a /* 1 */) | b;
type A4 = a | (/* 1 */ b);
type A5 = (a) /* 1 */ | b;
type A6 = a | /* 1 */ (b);
type B1 = a /* 1 */ /* 2 */ | b;
type B2 = a /* 1 */ | /* 2 */ b;
type B3 = a | /* 1 */ /* 2 */ b;
=====================================output=====================================
type A1 = a /* 1 */ | b;
type A2 = a | /* 1 */ b;
type A3 = a /* 1 */ | b;
type A4 = a | /* 1 */ b;
type A5 = a /* 1 */ | b;
type A6 = a | /* 1 */ b;
type B1 = a /* 1 */ /* 2 */ | b;
type B2 = a /* 1 */ | /* 2 */ b;
type B3 = a | /* 1 */ /* 2 */ b;
================================================================================
`;

exports[`inlining.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
Expand Down
10 changes: 10 additions & 0 deletions tests/typescript/union/comments.ts
@@ -0,0 +1,10 @@
type A1 = a /* 1 */ | b;
type A2 = a | /* 1 */ b;
type A3 = (a /* 1 */) | b;
type A4 = a | (/* 1 */ b);
type A5 = (a) /* 1 */ | b;
type A6 = a | /* 1 */ (b);

type B1 = a /* 1 */ /* 2 */ | b;
type B2 = a /* 1 */ | /* 2 */ b;
type B3 = a | /* 1 */ /* 2 */ b;

0 comments on commit 37aa31e

Please sign in to comment.