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

Improve new line after { detection in TSMappedType #14659

Merged
merged 7 commits into from Apr 10, 2023
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
24 changes: 24 additions & 0 deletions changelog_unreleased/typescript/14659.md
@@ -0,0 +1,24 @@
#### Improve new line detection in mapped type (#14659 by @fisker)

<!-- prettier-ignore -->
```ts
// Input
type A1 = { [A in B]:
T}
type A2 = {
[A in B]:T}

// Prettier stable
type A1 = {
[A in B]: T;
};
type A2 = {
[A in B]: T;
};

// Prettier main
type A1 = { [A in B]: T };
type A2 = {
[A in B]: T;
};
```
20 changes: 12 additions & 8 deletions src/language-js/print/mapped-type.js
@@ -1,6 +1,6 @@
import { printDanglingComments } from "../../main/comments/print.js";
import hasNewlineInRange from "../../utils/has-newline-in-range.js";
import { locStart, locEnd } from "../loc.js";
import { locStart } from "../loc.js";
import {
group,
softline,
Expand Down Expand Up @@ -54,23 +54,27 @@ function printTypeScriptMappedTypeModifier(tokenNode, keyword) {

function printTypescriptMappedType(path, options, print) {
const { node } = path;
// Break after `{` like `printObject`
const shouldBreak = hasNewlineInRange(
options.originalText,
locStart(node),
locEnd(node)
// Ideally, this should be the next token after `{`, but there is no node starts with it.
locStart(node.typeParameter)
);

return group(
[
"{",
indent([
options.bracketSpacing ? line : softline,
print("typeParameter"),
node.optional
? printTypeScriptMappedTypeModifier(node.optional, "?")
: "",
node.typeAnnotation ? ": " : "",
print("typeAnnotation"),
group([
print("typeParameter"),
node.optional
? printTypeScriptMappedTypeModifier(node.optional, "?")
: "",
node.typeAnnotation ? ": " : "",
print("typeAnnotation"),
]),
options.semi ? ifBreak(";") : "",
]),
printDanglingComments(path, options),
Expand Down
@@ -0,0 +1,39 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`break-mode.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
type A1 = { readonly [A in B]: T}
type A2 = {
readonly [A in B]: T}
type A3 = { readonly
[A in B]: T}
type A4 = { readonly [
A in B]: T}
type A5 = { readonly [A in B]
: T}
type A6 = { readonly [A in B]:
T}
type A7 = { readonly [A in B]: T
}

=====================================output=====================================
type A1 = { readonly [A in B]: T };
type A2 = {
readonly [A in B]: T;
};
type A3 = {
readonly [A in B]: T;
};
type A4 = {
readonly [A in B]: T;
};
type A5 = { readonly [A in B]: T };
type A6 = { readonly [A in B]: T };
type A7 = { readonly [A in B]: T };

================================================================================
`;
13 changes: 13 additions & 0 deletions tests/format/typescript/mapped-type/break-mode/break-mode.ts
@@ -0,0 +1,13 @@
type A1 = { readonly [A in B]: T}
type A2 = {
readonly [A in B]: T}
type A3 = { readonly
[A in B]: T}
type A4 = { readonly [
A in B]: T}
type A5 = { readonly [A in B]
: T}
type A6 = { readonly [A in B]:
T}
type A7 = { readonly [A in B]: T
}
@@ -0,0 +1 @@
run_spec(import.meta, ["typescript"]);