Skip to content

Commit

Permalink
[flow][codemod] Only keep react import for component syntax if it use…
Browse files Browse the repository at this point in the history
…d ref param

Reviewed By: alexmckenley

Differential Revision: D54970188

fbshipit-source-id: 4051327310bad13036ea5d32373c858c5f2dbb6b
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Mar 18, 2024
1 parent e3bac53 commit f12aabe
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/codemods/remove_react_import.ml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,24 @@ let has_unaccounted_react_value_usage_visitor =
t'

method! component_declaration loc c =
this#update_acc (fun _ -> true);
let open Ast.Statement.ComponentDeclaration in
let { params = (_, { Params.params; _ }); _ } = c in
if
Base.List.exists
params
~f:(fun
( _,
{
Param.name =
( Param.Identifier (_, { Ast.Identifier.name; _ })
| Param.StringLiteral (_, { Ast.StringLiteral.value = name; _ }) );
_;
}
)
-> name = "ref"
)
then
this#update_acc (fun _ -> true);
super#component_declaration loc c

method! identifier ((_, { Ast.Identifier.name; _ }) as id) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
type Foo = React.AbstractComponent<{}>;
<div />;

component Foo() {
return <div />;
}

>>> Launching report...

Stats:
Files changed: 1
Removed instances: 1
Still useful instances: 3
Still useful instances: 4

4 changes: 4 additions & 0 deletions tests/codemod_remove_react_import/removed.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ import * as React from 'react';

type Foo = React.AbstractComponent<{}>;
<div />;

component Foo() {
return <div />;
}
3 changes: 3 additions & 0 deletions tests/codemod_remove_react_import/still_useful_4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as React from 'react';

component C('ref' as ref: any) { return <div /> };

0 comments on commit f12aabe

Please sign in to comment.