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

Improve detection of hooks #10

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"scripts": {
"build": "esy dune build -p ReactRulesOfHooksPpx",
"watch": "esy dune build -p ReactRulesOfHooksPpx --watch",
"test": "esy b dune runtest",
"test": "esy b dune runtest --force",
"promote": "esy dune promote",
"format": "esy dune build @fmt --auto-promote",
"utop": "esy dune utop lib -- -implicit-bindings"
Expand Down
30 changes: 2 additions & 28 deletions src/Ppx.re
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ let startsWith = (affix, str) => {

type acc = {
isInsideConditional: bool,
isInsideJSX: bool,
locations: list(Location.t),
};

Expand All @@ -189,46 +188,21 @@ let findConditionalHooks = {
startsWith("use", name);
};

let containsJSX = (attrs: attributes) => {
let someAttrIsJsx =
List.find_opt(({attr_name, _}) => attr_name.txt == "JSX", attrs);

Option.is_some(someAttrIsJsx);
};

let linter = {
as _;
inherit class Ast_traverse.fold(acc) as super;
pub! expression = (t, acc) => {
let acc =
super#expression(
t,
{
...acc,
isInsideJSX: acc.isInsideJSX || containsJSX(t.pexp_attributes),
},
);

switch (t.pexp_desc) {
| Pexp_apply({pexp_desc: Pexp_ident({txt: lident, _}), _}, _args)
when isAHook(lident) && acc.isInsideConditional => {
...acc,
locations: [t.pexp_loc, ...acc.locations],
}
| Pexp_apply({pexp_desc: Pexp_ident({txt: lident, _}), _}, _args)
when isAHook(lident) && acc.isInsideJSX => {
when isAHook(lident) => {
...acc,
locations: [t.pexp_loc, ...acc.locations],
isInsideJSX: false,
}
| Pexp_sequence(_, exp) =>
let acc =
super#expression(
exp,
{...acc, isInsideJSX: containsJSX(exp.pexp_attributes)},
);

acc;
| Pexp_match(_expr, listOfExpr) =>
List.fold_left(
(acc, expr) =>
Expand Down Expand Up @@ -270,7 +244,7 @@ let conditionalHooksLinter = (structure: Parsetree.structure) => {
let {locations, _} =
findConditionalHooks(
structure,
{isInsideConditional: false, isInsideJSX: false, locations: []},
{isInsideConditional: false, locations: []},
);

let lintErrors =
Expand Down