Skip to content

Commit 9842b29

Browse files
authored
Recognize @satisfies in Closure-style type casts (#14262)
1 parent bc18fa4 commit 9842b29

5 files changed

Lines changed: 40 additions & 18 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#### Recognize `@satisfies` in Closure-style type casts (#14262 by @fisker)
2+
3+
<!-- prettier-ignore -->
4+
```jsx
5+
// Input
6+
const a = /** @satisfies {Record<string, string>} */ ({hello: 1337});
7+
const b = /** @type {Record<string, string>} */ ({hello: 1337});
8+
9+
// Prettier stable
10+
const a = /** @satisfies {Record<string, string>} */ { hello: 1337 };
11+
const b = /** @type {Record<string, string>} */ ({ hello: 1337 });
12+
13+
// Prettier main
14+
const a = /** @satisfies {Record<string, string>} */ ({hello: 1337});
15+
const b = /** @type {Record<string, string>} */ ({hello: 1337});
16+
```

src/language-js/comments.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const {
3131
} = require("./utils/index.js");
3232
const { locStart, locEnd } = require("./loc.js");
3333
const isBlockComment = require("./utils/is-block-comment.js");
34+
const isTypeCastComment = require("./utils/is-type-cast-comment.js");
3435

3536
/**
3637
* @typedef {import("./types/estree").Node} Node
@@ -955,21 +956,6 @@ function getCommentChildNodes(node, options) {
955956
}
956957
}
957958

958-
/**
959-
* @param {Comment} comment
960-
* @returns {boolean}
961-
*/
962-
function isTypeCastComment(comment) {
963-
return (
964-
isBlockComment(comment) &&
965-
comment.value[0] === "*" &&
966-
// TypeScript expects the type to be enclosed in curly brackets, however
967-
// Closure Compiler accepts types in parens and even without any delimiters at all.
968-
// That's why we just search for "@type".
969-
/@type\b/.test(comment.value)
970-
);
971-
}
972-
973959
/**
974960
* @param {AstPath} path
975961
* @returns {boolean}
@@ -1005,7 +991,6 @@ module.exports = {
1005991
handleOwnLineComment,
1006992
handleEndOfLineComment,
1007993
handleRemainingComment,
1008-
isTypeCastComment,
1009994
getCommentChildNodes,
1010995
willPrintOwnComments,
1011996
};

src/language-js/utils/is-type-cast-comment.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ function isTypeCastComment(comment) {
1616
comment.value[0] === "*" &&
1717
// TypeScript expects the type to be enclosed in curly brackets, however
1818
// Closure Compiler accepts types in parens and even without any delimiters at all.
19-
// That's why we just search for "@type".
20-
/@type\b/.test(comment.value)
19+
// That's why we just search for "@type" and "@satisfies".
20+
/@(?:type|satisfies)\b/.test(comment.value)
2121
);
2222
}
2323

tests/format/js/comments-closure-typecast/__snapshots__/jsfmt.spec.js.snap

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,24 @@ const objectWithComment2 = /** @type MyType */ (
571571
================================================================================
572572
`;
573573
574+
exports[`satisfies.js format 1`] = `
575+
====================================options=====================================
576+
parsers: ["babel"]
577+
printWidth: 80
578+
| printWidth
579+
=====================================input======================================
580+
module.exports = /** @satisfies {Record<string, string>} */ ({
581+
hello: 1337,
582+
});
583+
584+
=====================================output=====================================
585+
module.exports = /** @satisfies {Record<string, string>} */ ({
586+
hello: 1337,
587+
});
588+
589+
================================================================================
590+
`;
591+
574592
exports[`styled-components.js format 1`] = `
575593
====================================options=====================================
576594
parsers: ["babel"]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = /** @satisfies {Record<string, string>} */ ({
2+
hello: 1337,
3+
});

0 commit comments

Comments
 (0)