Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove hacks for single-element unions/intersections #10457

Merged
merged 2 commits into from Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this only used in js, should we remove it from comment attach?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather keep it. It's a reasonable extension point. The default regex (/^[\s(]*$/) is JS-specific. It might work well for languages that use parens in a way similar to how JS uses them, but it's also possible that other languages might need to override this.

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;