Skip to content

Commit

Permalink
test: Configure linting tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davesnx committed Oct 14, 2020
1 parent ff5844d commit 6d8a248
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion dune-project
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(lang dune 2.0)
(lang dune 2.7)
2 changes: 1 addition & 1 deletion esy.lock/index.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@opam/merlin": "*",
"ocaml": "4.10.0",
"@esy-ocaml/reason": ">= 3.6.0 < 4.0.0",
"@opam/dune": "> 1.11.4",
"@opam/dune": "2.7.1",
"@opam/ocaml-migrate-parsetree": "^1.7.3",
"@opam/ppxlib": "0.14.0",
"@reason-native/rely": "^3.2.1",
Expand Down
28 changes: 16 additions & 12 deletions src/Ppx.re
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ let useEffectExpand = (e: Parsetree.expression) =>
};

let startsWith = (affix, str) => {
let start = String.sub(str, 0, String.length(affix));
let start =
try(String.sub(str, 0, String.length(affix))) {
| _ => ""
};

start == affix;
};

Expand All @@ -178,30 +182,29 @@ let findConditionalHooks = {
startsWith("use", name);
};

{
let linter = {
as _;
inherit class Ast_traverse.fold(acc) as super;
pub! expression = (t, acc) => {
let acc = super#expression(t, acc);
switch (t.pexp_desc) {
| Pexp_apply({pexp_desc: Pexp_ident({txt: lident, _}), _}, _args)
when isAHook(lident) && acc.isInside =>
let f = getName(lident);
print_endline("hook " ++ f);
let acc = super#expression(t, acc);
{...acc, locations: [t.pexp_loc, ...acc.locations]};
when isAHook(lident) && acc.isInside => {
...acc,
locations: [t.pexp_loc, ...acc.locations],
}
| Pexp_match(_expr, listOfExpr) =>
List.fold_left(
(acc, expr) =>
super#expression(expr.pc_rhs, {...acc, isInside: true}),
acc,
listOfExpr,
)

| Pexp_while(_cond, expr) =>
super#expression(expr, {...acc, isInside: true})
| Pexp_for(_, _, _, _, expr) =>
super#expression(expr, {...acc, isInside: true})
| Pexp_ifthenelse(ifExpr, thenExpr, elseExpr) =>
| Pexp_ifthenelse(ifExpr, thenExpr, elseExpr) when !acc.isInside =>
let acc = super#expression(ifExpr, {...acc, isInside: true});
let acc = super#expression(thenExpr, {...acc, isInside: true});

Expand All @@ -211,12 +214,13 @@ let findConditionalHooks = {
| None => acc
};

{...acc, isInside: false};
{...acc, isInside: true};
| _ => super#expression(t, acc)
};
}
}#
structure;
};

linter#structure;
};

let conditionalHooksLinter = (structure: Parsetree.structure) => {
Expand Down
6 changes: 6 additions & 0 deletions test/ConditionalHooks.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[@@@ocaml.ppwarning "Hooks can't be inside conditionals, neither loops."]
let useMouseHook () = ()
let make ~randomProp =
if randomProp == "state" then useMouseHook ();
((div ~children:[] ())
[@JSX ])[@@react.component ]
18 changes: 5 additions & 13 deletions test/dune
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
; expect the process to fail, capturing stderr
(with-stderr-to
%{targets}
(bash "! ./%{pp} %{input}")
(bash "! ./%{pp} -no-color -impl %{input}")
)
)
)
Expand All @@ -33,25 +33,17 @@
(:input ConditionalHooks.ml)
)
(action
(progn
; expect the process to fail, capturing stderr
(with-stderr-to
%{targets}
(bash "! ./%{pp} %{input}")
)
)
(bash "! ./%{pp} -no-color -impl %{input} > %{targets} || true")
)
)

(rule
(targets ConditionalHooks.ml)
(deps ConditionalHooks.re)
(action
(progn
(with-stdout-to
%{targets}
(run refmt --parse=re --print=ml %{deps})
)
(with-stdout-to
%{targets}
(run refmt --parse=re --print=ml %{deps})
)
)
)
Expand Down

0 comments on commit 6d8a248

Please sign in to comment.