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

Recognize @satisfies in Closure-style type casts #14262

Merged
merged 2 commits into from Feb 7, 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
16 changes: 16 additions & 0 deletions changelog_unreleased/javascript/14262.md
@@ -0,0 +1,16 @@
#### Recognize `@satisfies` in Closure-style type casts (#14262 by @fisker)

<!-- prettier-ignore -->
```jsx
// Input
const a = /** @satisfies {Record<string, string>} */ ({hello: 1337});
const b = /** @type {Record<string, string>} */ ({hello: 1337});

// Prettier stable
const a = /** @satisfies {Record<string, string>} */ { hello: 1337 };
const b = /** @type {Record<string, string>} */ ({ hello: 1337 });

// Prettier main
const a = /** @satisfies {Record<string, string>} */ ({hello: 1337});
const b = /** @type {Record<string, string>} */ ({hello: 1337});
```
17 changes: 1 addition & 16 deletions src/language-js/comments.js
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -1005,7 +991,6 @@ module.exports = {
handleOwnLineComment,
handleEndOfLineComment,
handleRemainingComment,
isTypeCastComment,
getCommentChildNodes,
willPrintOwnComments,
};
4 changes: 2 additions & 2 deletions src/language-js/utils/is-type-cast-comment.js
Expand Up @@ -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)
);
}

Expand Down
Expand Up @@ -571,6 +571,24 @@ const objectWithComment2 = /** @type MyType */ (
================================================================================
`;

exports[`satisfies.js format 1`] = `
====================================options=====================================
parsers: ["babel"]
printWidth: 80
| printWidth
=====================================input======================================
module.exports = /** @satisfies {Record<string, string>} */ ({
hello: 1337,
});

=====================================output=====================================
module.exports = /** @satisfies {Record<string, string>} */ ({
hello: 1337,
});

================================================================================
`;

exports[`styled-components.js format 1`] = `
====================================options=====================================
parsers: ["babel"]
Expand Down
3 changes: 3 additions & 0 deletions tests/format/js/comments-closure-typecast/satisfies.js
@@ -0,0 +1,3 @@
module.exports = /** @satisfies {Record<string, string>} */ ({
hello: 1337,
});