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

eslint showing error when using variable from parent scope #2352

Closed
afucher opened this issue Jul 18, 2019 · 11 comments · Fixed by #2699
Closed

eslint showing error when using variable from parent scope #2352

afucher opened this issue Jul 18, 2019 · 11 comments · Fixed by #2699

Comments

@afucher
Copy link

afucher commented Jul 18, 2019

Tell us about your environment

  • ESLint Version: 6.0.1
  • Node Version: v10.15.3
  • npm Version: 6.4.1

What parser (default, Babel-ESLint, etc.) are you using?
default
Please show your full configuration:

Configuration

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

import React from 'react';

interface Props {
    readonly history: number[];
}

export const MyHistory = (props: Props) => {
    const renderContent = () => {
        if (props.history.length > 0) {
            return <div> My history here</div>
        }
        return <div> No historical events</div>
    };

    return renderContent();
};
npm run eslint --fix --ext .tsx --ext .ts src

What did you expect to happen?
eslint show no errors

What actually happened? Please include the actual, raw output from ESLint.

  9:19  error  'history' is missing in props validation         react/prop-types
  9:27  error  'history.length' is missing in props validation  react/prop-types

Additional info: issue first created on eslint repo eslint/eslint#12004

@ljharb
Copy link
Member

ljharb commented Jul 18, 2019

renderContent is being detected as a component; but either way you should be destructuring props directly inside your component, not later.

@afucher
Copy link
Author

afucher commented Jul 18, 2019

@ljharb, you mean like this?

import React from 'react';

interface Props {
    readonly history: number[];
}

export const MyHistory = (props: Props) => {
    let history: number[] = props.history;
    const renderContent = () => {
        if (history.length > 0) {
            return <div> My history here</div>
        }
        return <div> No historical events</div>
    };

    return renderContent();
};

If yes, I'm still getting error:

  10:21  error  'history.length' is missing in props validation  react/prop-types

@ljharb
Copy link
Member

ljharb commented Jul 18, 2019

gotcha, in that case it seems like a bug.

@vjdv
Copy link

vjdv commented Jul 18, 2019

i'm having a likely issue

I have following component:

// eslint-disable-next-line
function DayDiv({ today, selection, active, picked, d, daySelecter }) {
  const selected = active && selection.getDate() === d;
  const today_flg = active && dayEquals(today, setDateFor(selection, d));
  return (
    <div className={cx("day", active && "active", picked && "picked", selected && "selected", today_flg && "today")} data-value={d} onClick={daySelecter}>
      {d}
    </div>
  );
}

is warning
'selection.getMonth' is missing in props validation react/prop-types

DayDiv is used only on a Calendar component, Calendar is exported, DayDiv is not, that's why the eslint-disable-next-line line, before updating this warning was not appearing

@ljharb
Copy link
Member

ljharb commented Jul 18, 2019

Even not exported, it needs propTypes; separately, you’d need the disable at each prop’s usage site as well (when a property is accessed off of it)

@vjdv
Copy link

vjdv commented Jul 18, 2019

yeah, but the prop is selection, which warning was disabled in line 1, why now the attributes of the props are evaluated too?

@ljharb
Copy link
Member

ljharb commented Jul 18, 2019

Because a recent update improved prop detection. Prop shapes should be typed too.

@modestfake
Copy link

Is there a way to opt-out from nested props validation? It's so annoying to have this error for list.length, list.map(), list.filter, something.toString(), etc. Why should I validate built-in JavaScript methods and properties?

@ljharb
Copy link
Member

ljharb commented Aug 14, 2019

If it’s typed as an array, array methods should just work. Is it?

@denisbabineau
Copy link

+1 running into this as well with nested scope:

const Comp = () => (<Formik render={props => (<>{props.status}</>)} />);

Complains that status is missing in props validation for Comp but it doesn't belong to it.

It looks like it's just looking for the textual name props and not analyzing the AST properly because if I rename to props2 the lint goes away.

@jzabala
Copy link
Contributor

jzabala commented Jul 8, 2020

This one is fixed too with #2699

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

Successfully merging a pull request may close this issue.

6 participants