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

Flow: add support for 'implies' type guard variant #16272

Merged
merged 2 commits into from
May 11, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions changelog_unreleased/flow/16272.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#### Support Flow's 'implies' type guard variant (#16272 by @gkz)

Adds support for Flow's `implies` type guard variant. Also updates the `flow-parser` dependency.

<!-- prettier-ignore -->
```jsx
// Input
declare function f(x: mixed): implies x is T;

// Prettier stable
// error

// Prettier main
declare function f(x: mixed): implies x is T;
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"fast-json-stable-stringify": "2.1.0",
"file-entry-cache": "7.0.2",
"find-cache-dir": "5.0.0",
"flow-parser": "0.235.1",
"flow-parser": "0.236.0",
"get-east-asian-width": "1.2.0",
"get-stdin": "9.0.0",
"graphql": "16.8.1",
Expand Down
16 changes: 13 additions & 3 deletions src/language-js/print/type-annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,8 @@ function printArrayType(print) {
}

/*
- `TSTypeQuery`
- `TypeofTypeAnnotation`
- `TSTypeQuery` (TypeScript)
- `TypeofTypeAnnotation` (flow)
*/
function printTypeQuery({ node }, print) {
const argumentPropertyName =
Expand All @@ -553,10 +553,20 @@ function printTypeQuery({ node }, print) {
return ["typeof ", print(argumentPropertyName), print(typeArgsPropertyName)];
}

/*
- `TSTypePredicate` (TypeScript)
- `TypePredicate` (flow)
*/
function printTypePredicate(path, print) {
const { node } = path;
const prefix =
node.type === "TSTypePredicate" && node.asserts
? "asserts "
: node.type === "TypePredicate" && node.kind
? `${node.kind} `
: "";
return [
node.asserts ? "asserts " : "",
prefix,
print("parameterName"),
node.typeAnnotation
? [" is ", printTypeAnnotationProperty(path, print)]
Expand Down
33 changes: 33 additions & 0 deletions tests/format/flow/type-guards/__snapshots__/format.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`test.js [babel-flow] format 1`] = `
"Missing semicolon. (1:36)
> 1 | declare function basic(x: mixed): x is string;
| ^
2 |
3 | declare function asserts(x: mixed): asserts x is string;
4 |
Cause: Missing semicolon. (1:35)"
`;

exports[`test.js format 1`] = `
====================================options=====================================
parsers: ["flow"]
printWidth: 80
| printWidth
=====================================input======================================
declare function basic(x: mixed): x is string;

declare function asserts(x: mixed): asserts x is string;

declare function implies(x: mixed): implies x is string;

=====================================output=====================================
declare function basic(x: mixed): x is string;

declare function asserts(x: mixed): asserts x is string;

declare function implies(x: mixed): implies x is string;

================================================================================
`;
5 changes: 5 additions & 0 deletions tests/format/flow/type-guards/format.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
runFormatTest(import.meta, ["flow"], {
errors: {
"babel-flow": ["test.js"],
},
});
5 changes: 5 additions & 0 deletions tests/format/flow/type-guards/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare function basic(x: mixed): x is string;

declare function asserts(x: mixed): asserts x is string;

declare function implies(x: mixed): implies x is string;
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4430,10 +4430,10 @@ __metadata:
languageName: node
linkType: hard

"flow-parser@npm:0.235.1":
version: 0.235.1
resolution: "flow-parser@npm:0.235.1"
checksum: 10/2c158e940deb2f3312dc91fe0890118295c6dc4823930cf18039bc6facd434504008a7b914a5a5be2f4d30274e0b31f8621d05417e5e4579e4df1f3fdd7cc949
"flow-parser@npm:0.236.0":
version: 0.236.0
resolution: "flow-parser@npm:0.236.0"
checksum: 10/b556cbe0450659676be80d494f581d11c7e5d83f4a1ae83f28975548c01455aaf06addcd364a1cde427757e4a6f1e373b298acb393bf99dcfdb4f85a3c07b078
languageName: node
linkType: hard

Expand Down Expand Up @@ -7445,7 +7445,7 @@ __metadata:
fast-json-stable-stringify: "npm:2.1.0"
file-entry-cache: "npm:7.0.2"
find-cache-dir: "npm:5.0.0"
flow-parser: "npm:0.235.1"
flow-parser: "npm:0.236.0"
get-east-asian-width: "npm:1.2.0"
get-stdin: "npm:9.0.0"
graphql: "npm:16.8.1"
Expand Down