File tree Expand file tree Collapse file tree
changelog_unreleased/javascript Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ ```
Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ const {
3131} = require ( "./utils/index.js" ) ;
3232const { locStart, locEnd } = require ( "./loc.js" ) ;
3333const 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- / @ t y p e \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} ;
Original file line number Diff line number Diff 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- / @ t y p e \b / . test ( comment . value )
19+ // That's why we just search for "@type" and "@satisfies" .
20+ / @ (?: t y p e | s a t i s f i e s ) \b / . test ( comment . value )
2121 ) ;
2222}
2323
Original file line number Diff line number Diff 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+
574592exports[` styled - components .js format 1 ` ] = `
575593==================================== options ==================================== =
576594parsers : [" babel" ]
Original file line number Diff line number Diff line change 1+ module . exports = /** @satisfies {Record<string, string> } */ ( {
2+ hello : 1337 ,
3+ } ) ;
You can’t perform that action at this time.
0 commit comments