Skip to content

Commit

Permalink
Flow: add support for 'implies' type guard variant (prettier#16272)
Browse files Browse the repository at this point in the history
Co-authored-by: fisker Cheung <lionkay@gmail.com>
  • Loading branch information
gkz and fisker committed May 11, 2024
1 parent 5a51577 commit fb9f2c1
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 9 deletions.
15 changes: 15 additions & 0 deletions changelog_unreleased/flow/16272.md
@@ -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
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
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
@@ -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
@@ -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
@@ -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
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

0 comments on commit fb9f2c1

Please sign in to comment.