diff --git a/Changes.md b/Changes.md index 5a830cf1..d876c9a6 100644 --- a/Changes.md +++ b/Changes.md @@ -1,4 +1,5 @@ # master +- Fix issue where `emptyArray` was reported unused for lowercase components without children. (See https://github.com/reason-association/reanalyze/issues/85). # 2.12.0 - Support OCaml 4.11 and 4.12. diff --git a/examples/deadcode/src/EmptyArray.bs.js b/examples/deadcode/src/EmptyArray.bs.js new file mode 100644 index 00000000..6e55be63 --- /dev/null +++ b/examples/deadcode/src/EmptyArray.bs.js @@ -0,0 +1,19 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as React from "react"; + +function EmptyArray$Z(Props) { + return React.createElement("br", undefined); +} + +var Z = { + make: EmptyArray$Z +}; + +React.createElement(EmptyArray$Z, {}); + +export { + Z , + +} +/* Not a pure module */ diff --git a/examples/deadcode/src/EmptyArray.res b/examples/deadcode/src/EmptyArray.res new file mode 100644 index 00000000..9c44bd9a --- /dev/null +++ b/examples/deadcode/src/EmptyArray.res @@ -0,0 +1,10 @@ +// @@config({flags : ["-dsource"]}); + +module Z = { + @react.component + let make = () => { +
+ } +} + +let _ = diff --git a/examples/deadcode/src/deadcode.txt b/examples/deadcode/src/deadcode.txt index c8ce9062..21a2d588 100644 --- a/examples/deadcode/src/deadcode.txt +++ b/examples/deadcode/src/deadcode.txt @@ -401,6 +401,14 @@ addValueDeclaration +make DynamicallyLoadedComponent.re:2:4 path:+DynamicallyLoadedComponent addValueReference DynamicallyLoadedComponent.re:2:32 --> DynamicallyLoadedComponent.re:2:13 addValueReference DynamicallyLoadedComponent.re:2:19 --> React.re:7:0 + Scanning EmptyArray.cmt Source:EmptyArray.res + addValueDeclaration +make EmptyArray.res:5:6 path:+EmptyArray.Z + DeadOptionalArgs.addReferences ReactDOMRe.createDOMElementVariadic called with optional argNames: argNamesMaybe: EmptyArray.res:6:5 + addValueDeclaration +emptyArray EmptyArray.res:6:5 path:+EmptyArray.Z + addDummyReference _none_:1:-1 --> EmptyArray.res:6:5 + addValueReference EmptyArray.res:6:5 --> ReactDOMRe.re:114:0 + addValueReference EmptyArray.res:10:9 --> EmptyArray.res:5:6 + addValueReference EmptyArray.res:10:9 --> React.re:18:0 Scanning ErrorHandler.cmt Source:ErrorHandler.re addValueDeclaration +notify ErrorHandler.re:7:6 path:+ErrorHandler.Make addValueDeclaration +x ErrorHandler.re:12:4 path:+ErrorHandler @@ -1877,6 +1885,7 @@ File References DeadValueTest.rei -->> DeadValueTest.re Docstrings.re -->> DynamicallyLoadedComponent.re -->> React.re + EmptyArray.res -->> React.re, ReactDOMRe.re ErrorHandler.re -->> ErrorHandler.rei -->> ErrorHandler.re EverythingLiveHere.re -->> @@ -2088,6 +2097,8 @@ File References Live Value +Docstrings.+one: 0 references () [0] Live Value +Docstrings.+signMessage: 0 references () [0] Live Value +Docstrings.+flat: 0 references () [0] + Live Value +EmptyArray.Z.+emptyArray: 1 references (_none_:1:-1) [0] + Live Value +EmptyArray.Z.+make: 1 references (EmptyArray.res:10:9) [0] Dead Value +EverythingLiveHere.+z: 0 references () [0] Dead Value +EverythingLiveHere.+y: 0 references () [0] Dead Value +EverythingLiveHere.+x: 0 references () [0] diff --git a/src/DeadValue.re b/src/DeadValue.re index 96a1a850..280c4587 100644 --- a/src/DeadValue.re +++ b/src/DeadValue.re @@ -138,7 +138,20 @@ let collectExpr = (super, self, e: Typedtree.expression) => { let locFrom = e.exp_loc; switch (e.exp_desc) { | Texp_ident(_path, _, {Types.val_loc: {loc_ghost: false, _} as locTo}) => - addValueReference(~addFileReference=true, ~locFrom, ~locTo) + if (locFrom == locTo && _path |> Path.name == "emptyArray") { + // Work around lowercase jsx with no children producing an artifact `emptyArray` + // which is called from its own location as many things are generated on the same location. + if (Common.Cli.debug^) { + Log_.item( + "addDummyReference %s --> %s@.", + Location.none.loc_start |> posToString, + locTo.loc_start |> posToString, + ); + }; + ValueReferences.add(locTo.loc_start, Location.none.loc_start); + } else { + addValueReference(~addFileReference=true, ~locFrom, ~locTo); + } | Texp_apply( {