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

Spreading props causes issues with unused-fields #116

Open
VictorGlindasPaf opened this issue Apr 16, 2021 · 1 comment
Open

Spreading props causes issues with unused-fields #116

VictorGlindasPaf opened this issue Apr 16, 2021 · 1 comment

Comments

@VictorGlindasPaf
Copy link

Passing fields into components by spreading shows them as unused by the relay/unused-fields, while passing them one by one does not cause any issues.

type Props = {
  data: ParentComponent_data;
};

const ParentComponent: FC<Props> = ({ data }) => {
  const { title, ...rest } = data;
  return <SharedComponent title={title} {...rest} />;
}

export default createFragmentContainer(ParentComponent, {
  data: graphql`
    fragment ParentComponent_data on Data {
       name # <- Triggers a warning about not being used!
       title
    }
  `,
});
@shogunsea
Copy link

I think this is essentially because this rule only does very naive static analysis of what properties are referenced in javascript file:

function visitMemberExpression(node) {
if (node.property.type === 'Identifier') {
foundMemberAccesses[node.property.name] = true;
}
}

it does not track how a piece of data are passed down to the child components and figure out if subfields are used in that child component.

Also there's a 2nd issue of this naive implementation, it can produce false positives because it's only checking the name of the properties, regardless of from which object it's being accessed, so the following is actually invalid case (foo.bar unsued) but test still passes:

    graphql\`fragment foo on Foo {
      bar
      baz {
        bar
      }
    }\`;
    foo.baz.bar;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants