hluisson / eslint-plugin-jsx-expressions

Rules for safe logical expressions in JSX
95 stars 12 forks source link

Member of object and object can be undefined #5

Closed DrChrispoper closed 2 years ago

DrChrispoper commented 2 years ago

Thanks for the amazing plugin!

I have a use case, one that made us look into having a rule like this. That doesn't seem to be caught yet.

const profile: $Profile | undefined
...
<div>
  {profile?.viewCount && <h2>Hey</h2>}
</div>
alexfoxy commented 2 years ago

For reference here is a test which should throw an error:

    {
      code: [
        'const first = { foo: 0 };',
        "<App>{first?.foo && <Foo/>}</App>",
      ].join("\n"),
      errors: [{ messageId: "conditionErrorFalseyString" }],
      parserOptions: {
        ecmaFeatures: {
          jsx: true,
        },
      },
      output: [
        'const first = { foo: 0 };',
        "<App>{!!first?.foo && <Foo/>}</App>",
      ].join("\n"),
      filename: "react.tsx",
    },
hluisson commented 2 years ago

Thanks for reporting. This should be fixed in the latest release, 1.3.1, which has just been published.