-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Additional cases for prop-types and no-unused-prop-types #1939
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1801,34 +1801,6 @@ ruleTester.run('no-unused-prop-types', rule, { | |
' bar: PropTypes.bool', | ||
'};' | ||
].join('\n') | ||
}, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So these tests are now able to detect that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we could consider it a bug that the rule missed it previously. |
||
code: [ | ||
'type Person = {', | ||
' ...data,', | ||
' lastname: string', | ||
'};', | ||
'class Hello extends React.Component {', | ||
' props: Person;', | ||
' render () {', | ||
' return <div>Hello {this.props.firstname}</div>;', | ||
' }', | ||
'}' | ||
].join('\n'), | ||
parser: 'babel-eslint' | ||
}, { | ||
code: [ | ||
'type Person = {|', | ||
' ...data,', | ||
' lastname: string', | ||
'|};', | ||
'class Hello extends React.Component {', | ||
' props: Person;', | ||
' render () {', | ||
' return <div>Hello {this.props.firstname}</div>;', | ||
' }', | ||
'}' | ||
].join('\n'), | ||
parser: 'babel-eslint' | ||
}, { | ||
// The next two test cases are related to: https://github.com/yannickcr/eslint-plugin-react/issues/1183 | ||
code: [ | ||
|
@@ -2470,6 +2442,20 @@ ruleTester.run('no-unused-prop-types', rule, { | |
'}' | ||
].join('\n'), | ||
parser: 'babel-eslint' | ||
}, { | ||
code: [ | ||
'const foo = {};', | ||
'class Hello extends React.Component {', | ||
' render() {', | ||
' const {firstname, lastname} = this.props.name;', | ||
' return <div>{firstname} {lastname}</div>;', | ||
' }', | ||
'}', | ||
'Hello.propTypes = {', | ||
' name: PropTypes.shape(foo)', | ||
'};' | ||
].join('\n'), | ||
parser: 'babel-eslint' | ||
}, { | ||
// issue #933 | ||
code: [ | ||
|
@@ -2913,6 +2899,23 @@ ruleTester.run('no-unused-prop-types', rule, { | |
} | ||
`, | ||
parser: 'babel-eslint' | ||
}, { | ||
code: [ | ||
'import type {BasePerson} from \'./types\'', | ||
'type Props = {', | ||
' person: {', | ||
' ...$Exact<BasePerson>,', | ||
' lastname: string', | ||
' }', | ||
'};', | ||
'class Hello extends React.Component {', | ||
' props: Props;', | ||
' render () {', | ||
' return <div>Hello {this.props.person.firstname}</div>;', | ||
' }', | ||
'}' | ||
].join('\n'), | ||
parser: 'babel-eslint' | ||
} | ||
], | ||
|
||
|
@@ -4475,6 +4478,48 @@ ruleTester.run('no-unused-prop-types', rule, { | |
errors: [{ | ||
message: '\'lastname\' PropType is defined but prop is never used' | ||
}] | ||
}, { | ||
code: ` | ||
type Person = string; | ||
class Hello extends React.Component<{ person: Person }> { | ||
render () { | ||
return <div />; | ||
} | ||
} | ||
`, | ||
settings: {react: {flowVersion: '0.53'}}, | ||
errors: [{ | ||
message: '\'person\' PropType is defined but prop is never used' | ||
}], | ||
parser: 'babel-eslint' | ||
}, { | ||
code: ` | ||
type Person = string; | ||
class Hello extends React.Component<void, { person: Person }, void> { | ||
render () { | ||
return <div />; | ||
} | ||
} | ||
`, | ||
settings: {react: {flowVersion: '0.52'}}, | ||
errors: [{ | ||
message: '\'person\' PropType is defined but prop is never used' | ||
}], | ||
parser: 'babel-eslint' | ||
}, { | ||
code: ` | ||
function higherOrderComponent<P: { foo: string }>() { | ||
return class extends React.Component<P> { | ||
render() { | ||
return <div />; | ||
} | ||
} | ||
} | ||
`, | ||
errors: [{ | ||
message: '\'foo\' PropType is defined but prop is never used' | ||
}], | ||
parser: 'babel-eslint' | ||
}, { | ||
// issue #1506 | ||
code: [ | ||
|
@@ -4665,6 +4710,163 @@ ruleTester.run('no-unused-prop-types', rule, { | |
}, { | ||
message: '\'a.b.c\' PropType is defined but prop is never used' | ||
}] | ||
}, { | ||
code: ` | ||
type Props = { foo: string } | ||
function higherOrderComponent<Props>() { | ||
return class extends React.Component<Props> { | ||
render() { | ||
return <div />; | ||
} | ||
} | ||
} | ||
`, | ||
parser: 'babel-eslint', | ||
errors: [{ | ||
message: '\'foo\' PropType is defined but prop is never used' | ||
}] | ||
}, { | ||
code: [ | ||
'type Person = {', | ||
' ...data,', | ||
' lastname: string', | ||
'};', | ||
'class Hello extends React.Component {', | ||
' props: Person;', | ||
' render () {', | ||
' return <div>Hello {this.props.firstname}</div>;', | ||
' }', | ||
'}' | ||
].join('\n'), | ||
parser: 'babel-eslint', | ||
errors: [{ | ||
message: '\'lastname\' PropType is defined but prop is never used' | ||
}] | ||
}, { | ||
code: [ | ||
'type Person = {|', | ||
' ...data,', | ||
' lastname: string', | ||
'|};', | ||
'class Hello extends React.Component {', | ||
' props: Person;', | ||
' render () {', | ||
' return <div>Hello {this.props.firstname}</div>;', | ||
' }', | ||
'}' | ||
].join('\n'), | ||
parser: 'babel-eslint', | ||
errors: [{ | ||
message: '\'lastname\' PropType is defined but prop is never used' | ||
}] | ||
}, { | ||
code: [ | ||
'type Person = {', | ||
' ...$Exact<data>,', | ||
' lastname: string', | ||
'};', | ||
'class Hello extends React.Component {', | ||
' props: Person;', | ||
' render () {', | ||
' return <div>Hello {this.props.firstname}</div>;', | ||
' }', | ||
'}' | ||
].join('\n'), | ||
parser: 'babel-eslint', | ||
errors: [{ | ||
message: '\'lastname\' PropType is defined but prop is never used' | ||
}] | ||
}, { | ||
code: [ | ||
'import type {Data} from \'./Data\'', | ||
'type Person = {', | ||
' ...Data,', | ||
' lastname: string', | ||
'};', | ||
'class Hello extends React.Component {', | ||
' props: Person;', | ||
' render () {', | ||
' return <div>Hello {this.props.bar}</div>;', | ||
' }', | ||
'}' | ||
].join('\n'), | ||
parser: 'babel-eslint', | ||
errors: [{ | ||
message: '\'lastname\' PropType is defined but prop is never used' | ||
}] | ||
}, { | ||
code: [ | ||
'import type {Data} from \'some-libdef-like-flow-typed-provides\'', | ||
'type Person = {', | ||
' ...Data,', | ||
' lastname: string', | ||
'};', | ||
'class Hello extends React.Component {', | ||
' props: Person;', | ||
' render () {', | ||
' return <div>Hello {this.props.bar}</div>;', | ||
' }', | ||
'}' | ||
].join('\n'), | ||
parser: 'babel-eslint', | ||
errors: [{ | ||
message: '\'lastname\' PropType is defined but prop is never used' | ||
}] | ||
}, { | ||
code: [ | ||
'type Person = {', | ||
' ...data,', | ||
' lastname: string', | ||
'};', | ||
'class Hello extends React.Component {', | ||
' props: Person;', | ||
' render () {', | ||
' return <div>Hello {this.props.firstname}</div>;', | ||
' }', | ||
'}' | ||
].join('\n'), | ||
parser: 'babel-eslint', | ||
errors: [{ | ||
message: '\'lastname\' PropType is defined but prop is never used' | ||
}] | ||
}, { | ||
code: [ | ||
'type Person = {|', | ||
' ...data,', | ||
' lastname: string', | ||
'|};', | ||
'class Hello extends React.Component {', | ||
' props: Person;', | ||
' render () {', | ||
' return <div>Hello {this.props.firstname}</div>;', | ||
' }', | ||
'}' | ||
].join('\n'), | ||
parser: 'babel-eslint', | ||
errors: [{ | ||
message: '\'lastname\' PropType is defined but prop is never used' | ||
}] | ||
}, { | ||
code: [ | ||
'import type {BasePerson} from \'./types\'', | ||
'type Props = {', | ||
' person: {', | ||
' ...$Exact<BasePerson>,', | ||
' lastname: string', | ||
' }', | ||
'};', | ||
'class Hello extends React.Component {', | ||
' props: Props;', | ||
' render () {', | ||
' return <div>Hello {this.props.person.firstname}</div>;', | ||
' }', | ||
'}' | ||
].join('\n'), | ||
parser: 'babel-eslint', | ||
options: [{skipShapeProps: false}], | ||
errors: [{ | ||
message: '\'person.lastname\' PropType is defined but prop is never used' | ||
}] | ||
} | ||
|
||
/* , { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to the case with propTypes, if the shape contains a spread plus some properties e.g.
it would be unwise to throw away the information we have about
lastname
just because we have a spread. By setting an additional flag and leaving the children data in place, we can detect iflastname
is being used.