moroshko / react-scanner

Extract React components and props usage from code.
MIT License
563 stars 40 forks source link

Does not find component instances that are in JSX props of components imported from elsewhere #48

Closed mihkeleidast closed 2 years ago

mihkeleidast commented 2 years ago

Came across an edgecase, where if a component imported from the specified library is used as JSX inside of a prop of another component that is imported from elsewhere, it is not counted as a used instance.

Failing test (note that the expected report might not be 100% accurate, but it currently fails with "Actual: {}", which is definitely not correct):

Scan("props with jsx expressions", ({ getReport }) => {
  const report = getReport(
    "imported-from-in-prop-jsx.js",

    `
    import { Text } from "other-place";
    import { Box } from "basis";

    <Text foo={<Box foo={bar} />} />`,
    { importedFrom: "basis" }
  );

  assert.equal(report, {
    Box: {
      instances: [
        {
          props: {
            foo: "(JSXElement)",
          },
          propsSpread: false,
          location: {
            file: "imported-from-in-prop-jsx.js",
            start: {
              line: 4,
              column: 5,
            },
          },
        },
      ],
    },
  });
});