Skip to content

Commit

Permalink
[flow-remove-types] handle as cast with generics (#9208)
Browse files Browse the repository at this point in the history
Summary:
Added support for correctly removing `as` casts when generics are involved.

Input:
```js
'm' as $NonMaybeType<string>;
```

Output before (current):
```js
'm'    $NonMaybeType        ;
```

Output after (expected):
```js
'm'                         ;
```

Pull Request resolved: #9208

Reviewed By: gkz

Differential Revision: D62982339

fbshipit-source-id: 286d8627b84bd800f260946ec0eb4a93e25f97ac
  • Loading branch information
jbroma authored and facebook-github-bot committed Sep 19, 2024
1 parent dd825da commit b41fa0e
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 4 deletions.
6 changes: 5 additions & 1 deletion packages/flow-remove-types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ var removeFlowVisitor = {
AsExpression: function (context, node, ast) {
var typeIdx = findTokenIndexAtStartOfNode(ast.tokens, node.typeAnnotation);
removeNode(context, ast.tokens[typeIdx - 1]); // `as` token
removeNode(context, node.typeAnnotation);
if (node.typeAnnotation.type === 'GenericTypeAnnotation') {
removeNode(context, ast.tokens[typeIdx]);
} else {
removeNode(context, node.typeAnnotation);
}
},

AsConstExpression: function (context, node, ast) {
Expand Down

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion packages/flow-remove-types/test/expected-pretty-inlinemap.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/flow-remove-types/test/expected-pretty.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ const kk = function(a,) {}
1;
[1];

// `as` cast with generics
'm';
['a', 'b', 'c'];
['x', 'y', 'z'];
const ga = {a: 'b'};
const gb = {a: 'x', b: 1};

// `as const`
's';
['s'];
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,13 @@ a ,) {}
1 ;
[1] ;

// `as` cast with generics
'm' ;
['a', 'b', 'c'] ;
['x', 'y', 'z'] ;
const ga = {a: 'b'} ;
const gb = {a: 'x', b: 1} ;

// `as const`
's' ;
['s'] ;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/flow-remove-types/test/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,13 @@ a ,) {}
1 ;
[1] ;

// `as` cast with generics
'm' ;
['a', 'b', 'c'] ;
['x', 'y', 'z'] ;
const ga = {a: 'b'} ;
const gb = {a: 'x', b: 1} ;

// `as const`
's' ;
['s'] ;
7 changes: 7 additions & 0 deletions packages/flow-remove-types/test/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,13 @@ a: string,) {}
1 as number as mixed;
[1] as [1];

// `as` cast with generics
'm' as $NonMaybeType<string>;
['a', 'b', 'c'] as $Keys<{a: string, b: string, c: number}>;
['x', 'y', 'z'] as $Values<{a: 'x', b: 'y', c: 'z'}>;
const ga = {a: 'b'} as $Rest<{a: string, c: number}, {c: number}>;
const gb = {a: 'x', b: 1} as $Shape<{a: string, b: number}>;

// `as const`
's' as const;
['s'] as const;

0 comments on commit b41fa0e

Please sign in to comment.