diff --git a/changelog_unreleased/javascript/14262.md b/changelog_unreleased/javascript/14262.md new file mode 100644 index 000000000000..bbce6e74aba6 --- /dev/null +++ b/changelog_unreleased/javascript/14262.md @@ -0,0 +1,16 @@ +#### Recognize `@satisfies` in Closure-style type casts (#14262 by @fisker) + + +```jsx +// Input +const a = /** @satisfies {Record} */ ({hello: 1337}); +const b = /** @type {Record} */ ({hello: 1337}); + +// Prettier stable +const a = /** @satisfies {Record} */ { hello: 1337 }; +const b = /** @type {Record} */ ({ hello: 1337 }); + +// Prettier main +const a = /** @satisfies {Record} */ ({hello: 1337}); +const b = /** @type {Record} */ ({hello: 1337}); +``` diff --git a/src/language-js/comments.js b/src/language-js/comments.js index cb6e151eb8b9..a9b7f75d3842 100644 --- a/src/language-js/comments.js +++ b/src/language-js/comments.js @@ -31,6 +31,7 @@ const { } = require("./utils/index.js"); const { locStart, locEnd } = require("./loc.js"); const isBlockComment = require("./utils/is-block-comment.js"); +const isTypeCastComment = require("./utils/is-type-cast-comment.js"); /** * @typedef {import("./types/estree").Node} Node @@ -955,21 +956,6 @@ function getCommentChildNodes(node, options) { } } -/** - * @param {Comment} comment - * @returns {boolean} - */ -function isTypeCastComment(comment) { - return ( - isBlockComment(comment) && - comment.value[0] === "*" && - // TypeScript expects the type to be enclosed in curly brackets, however - // Closure Compiler accepts types in parens and even without any delimiters at all. - // That's why we just search for "@type". - /@type\b/.test(comment.value) - ); -} - /** * @param {AstPath} path * @returns {boolean} @@ -1005,7 +991,6 @@ module.exports = { handleOwnLineComment, handleEndOfLineComment, handleRemainingComment, - isTypeCastComment, getCommentChildNodes, willPrintOwnComments, }; diff --git a/src/language-js/utils/is-type-cast-comment.js b/src/language-js/utils/is-type-cast-comment.js index 4f0ad4dcf360..c3d259089dd0 100644 --- a/src/language-js/utils/is-type-cast-comment.js +++ b/src/language-js/utils/is-type-cast-comment.js @@ -16,8 +16,8 @@ function isTypeCastComment(comment) { comment.value[0] === "*" && // TypeScript expects the type to be enclosed in curly brackets, however // Closure Compiler accepts types in parens and even without any delimiters at all. - // That's why we just search for "@type". - /@type\b/.test(comment.value) + // That's why we just search for "@type" and "@satisfies". + /@(?:type|satisfies)\b/.test(comment.value) ); } diff --git a/tests/format/js/comments-closure-typecast/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/comments-closure-typecast/__snapshots__/jsfmt.spec.js.snap index 722445beceb8..7fc196e42681 100644 --- a/tests/format/js/comments-closure-typecast/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/js/comments-closure-typecast/__snapshots__/jsfmt.spec.js.snap @@ -571,6 +571,24 @@ const objectWithComment2 = /** @type MyType */ ( ================================================================================ `; +exports[`satisfies.js format 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +module.exports = /** @satisfies {Record} */ ({ + hello: 1337, +}); + +=====================================output===================================== +module.exports = /** @satisfies {Record} */ ({ + hello: 1337, +}); + +================================================================================ +`; + exports[`styled-components.js format 1`] = ` ====================================options===================================== parsers: ["babel"] diff --git a/tests/format/js/comments-closure-typecast/satisfies.js b/tests/format/js/comments-closure-typecast/satisfies.js new file mode 100644 index 000000000000..791fc7e47aca --- /dev/null +++ b/tests/format/js/comments-closure-typecast/satisfies.js @@ -0,0 +1,3 @@ +module.exports = /** @satisfies {Record} */ ({ + hello: 1337, +});