Skip to content

Commit

Permalink
[Refactor] no-unues-prop-types, no-unused-state: add failing test c…
Browse files Browse the repository at this point in the history
…ases
  • Loading branch information
Konrad Madej committed Jan 14, 2020
1 parent a18650f commit 7c32709
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
93 changes: 93 additions & 0 deletions tests/lib/rules/no-unused-prop-types.js
Expand Up @@ -5210,6 +5210,99 @@ ruleTester.run('no-unused-prop-types', rule, {
errors: [{
message: '\'unUsedProp\' PropType is defined but prop is never used'
}]
}, {
code: `
const Foo = (props) => {
const { foo } = props as unknown;
(props as unknown).bar as unknown;
return <></>;
};
Foo.propTypes = {
fooUnused,
barUnused,
};
`,
parser: parsers.TYPESCRIPT_ESLINT,
errors: [{
message: '\'fooUnused\' PropType is defined but prop is never used'
}, {
message: '\'barUnused\' PropType is defined but prop is never used'
}]
}, {
code: `
class Foo extends React.Component {
static propTypes = {
prevPropUnused,
nextPropUnused,
setStatePropUnused,
thisPropsAliasDestructPropUnused,
thisPropsAliasPropUnused,
thisDestructPropsAliasDestructPropUnused,
thisDestructPropsAliasPropUnused,
thisDestructPropsDestructPropUnused,
thisPropsDestructPropUnused,
thisPropsPropUnused,
};
componentDidUpdate(prevProps) {
(prevProps as unknown).prevProp as unknown;
}
shouldComponentUpdate(nextProps) {
(nextProps as unknown).nextProp as unknown;
}
stateProps() {
((this as unknown).setState as unknown)((_, props) => (props as unknown).setStateProp as unknown);
}
thisPropsAlias() {
const props = (this as unknown).props as unknown;
const { thisPropsAliasDestructProp } = props as unknown;
(props as unknown).thisPropsAliasProp as unknown;
}
thisDestructPropsAlias() {
const { props } = this as unknown;
const { thisDestructPropsAliasDestructProp } = props as unknown;
(props as unknown).thisDestructPropsAliasProp as unknown;
}
render() {
const { props: { thisDestructPropsDestructProp } } = this as unknown;
const { thisPropsDestructProp } = (this as unknown).props as unknown;
((this as unknown).props as unknown).thisPropsProp as unknown;
return null;
}
}
`,
parser: parsers.TYPESCRIPT_ESLINT,
errors: [{
message: '\'prevPropUnused\' PropType is defined but prop is never used'
}, {
message: '\'nextPropUnused\' PropType is defined but prop is never used'
}, {
message: '\'setStatePropUnused\' PropType is defined but prop is never used'
}, {
message: '\'thisPropsAliasDestructPropUnused\' PropType is defined but prop is never used'
}, {
message: '\'thisPropsAliasPropUnused\' PropType is defined but prop is never used'
}, {
message: '\'thisDestructPropsAliasDestructPropUnused\' PropType is defined but prop is never used'
}, {
message: '\'thisDestructPropsAliasPropUnused\' PropType is defined but prop is never used'
}, {
message: '\'thisDestructPropsDestructPropUnused\' PropType is defined but prop is never used'
}, {
message: '\'thisPropsDestructPropUnused\' PropType is defined but prop is never used'
}, {
message: '\'thisPropsPropUnused\' PropType is defined but prop is never used'
}]
}

/* , {
Expand Down
64 changes: 64 additions & 0 deletions tests/lib/rules/no-unused-state.js
Expand Up @@ -1171,6 +1171,70 @@ eslintTester.run('no-unused-state', rule, {
`,
parser: parsers.BABEL_ESLINT,
errors: getErrorMessages(['initial'])
}, {
code: `
class Foo extends Component {
state = {
thisStateAliasPropUnused,
thisStateAliasRestPropUnused,
thisDestructStateAliasPropUnused,
thisDestructStateAliasRestPropUnused,
thisDestructStateDestructRestPropUnused,
thisSetStatePropUnused,
thisSetStateRestPropUnused,
} as unknown
constructor() {
// other methods of defining state props
((this as unknown).state as unknown) = { thisStatePropUnused } as unknown;
((this as unknown).setState as unknown)({ thisStateDestructPropUnused } as unknown);
((this as unknown).setState as unknown)(state => ({ thisDestructStateDestructPropUnused } as unknown));
}
thisStateAlias() {
const state = (this as unknown).state as unknown;
(state as unknown).thisStateAliasProp as unknown;
const { ...thisStateAliasRest } = state as unknown;
(thisStateAliasRest as unknown).thisStateAliasRestProp as unknown;
}
thisDestructStateAlias() {
const { state } = this as unknown;
(state as unknown).thisDestructStateAliasProp as unknown;
const { ...thisDestructStateAliasRest } = state as unknown;
(thisDestructStateAliasRest as unknown).thisDestructStateAliasRestProp as unknown;
}
thisSetState() {
((this as unknown).setState as unknown)(state => (state as unknown).thisSetStateProp as unknown);
((this as unknown).setState as unknown)(({ ...thisSetStateRest }) => (thisSetStateRest as unknown).thisSetStateRestProp as unknown);
}
render() {
((this as unknown).state as unknown).thisStateProp as unknown;
const { thisStateDestructProp } = (this as unknown).state as unknown;
const { state: { thisDestructStateDestructProp, ...thisDestructStateDestructRest } } = this as unknown;
(thisDestructStateDestructRest as unknown).thisDestructStateDestructRestProp as unknown;
return null;
}
}
`,
parser: parsers.TYPESCRIPT_ESLINT,
errors: getErrorMessages([
'thisStateAliasPropUnused',
'thisStateAliasRestPropUnused',
'thisDestructStateAliasPropUnused',
'thisDestructStateAliasRestPropUnused',
'thisDestructStateDestructRestPropUnused',
'thisSetStatePropUnused',
'thisSetStateRestPropUnused',
'thisStatePropUnused',
'thisStateDestructPropUnused',
'thisDestructStateDestructPropUnused'
])
}
]
});

0 comments on commit 7c32709

Please sign in to comment.