From 6d8a24866ade7ea1206e5ab162ead220be58528c Mon Sep 17 00:00:00 2001 From: David Sancho Date: Wed, 14 Oct 2020 11:32:41 +0200 Subject: [PATCH] test: Configure linting tests --- dune-project | 2 +- esy.lock/index.json | 2 +- package.json | 2 +- src/Ppx.re | 28 ++++++++++++++++------------ test/ConditionalHooks.expected | 6 ++++++ test/dune | 18 +++++------------- 6 files changed, 30 insertions(+), 28 deletions(-) diff --git a/dune-project b/dune-project index 929c696..45acd3f 100644 --- a/dune-project +++ b/dune-project @@ -1 +1 @@ -(lang dune 2.0) +(lang dune 2.7) diff --git a/esy.lock/index.json b/esy.lock/index.json index 8cf3b16..0e36f25 100644 --- a/esy.lock/index.json +++ b/esy.lock/index.json @@ -1,5 +1,5 @@ { - "checksum": "b32a10db9aedc97152e7592e66ff78e0", + "checksum": "477a4258f08222855a7e5ee2bf5ea73e", "root": "react-rules-of-hooks-ppx@link-dev:./package.json", "node": { "reason-react@0.9.1@d41d8cd9": { diff --git a/package.json b/package.json index 1d47717..1567969 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/Ppx.re b/src/Ppx.re index d431b81..42a570d 100644 --- a/src/Ppx.re +++ b/src/Ppx.re @@ -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; }; @@ -178,17 +182,17 @@ 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) => @@ -196,12 +200,11 @@ let findConditionalHooks = { 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}); @@ -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) => { diff --git a/test/ConditionalHooks.expected b/test/ConditionalHooks.expected index e69de29..56e0ca6 100644 --- a/test/ConditionalHooks.expected +++ b/test/ConditionalHooks.expected @@ -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 ] diff --git a/test/dune b/test/dune index 533ee81..e154ce2 100644 --- a/test/dune +++ b/test/dune @@ -9,7 +9,7 @@ ; expect the process to fail, capturing stderr (with-stderr-to %{targets} - (bash "! ./%{pp} %{input}") + (bash "! ./%{pp} -no-color -impl %{input}") ) ) ) @@ -33,13 +33,7 @@ (: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") ) ) @@ -47,11 +41,9 @@ (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}) ) ) )