Skip to content

Commit

Permalink
[Fix] no-unused-prop-types/TypeScript: avoid crash on indexable int…
Browse files Browse the repository at this point in the history
…erface

Fixes #2687.
  • Loading branch information
ljharb committed Jun 30, 2020
1 parent 2db0080 commit 830bde7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/util/propTypes.js
Expand Up @@ -314,12 +314,14 @@ module.exports = function propTypesInstructions(context, components, utils) {
}

foundDeclaredPropertiesList.forEach((tsPropertySignature) => {
declaredPropTypes[tsPropertySignature.key.name] = {
fullName: tsPropertySignature.key.name,
name: tsPropertySignature.key.name,
node: tsPropertySignature,
isRequired: !tsPropertySignature.optional
};
if (tsPropertySignature.type !== 'TSIndexSignature') {

This comment has been minimized.

Copy link
@taion

taion Jun 30, 2020

Contributor

I'm not 100% sure what's valid in the TS AST here, but one would expect from the tsPropertySignature name of the binding that we'd assert that it's actually a TSPropertySignature instead of anything else.

This comment has been minimized.

Copy link
@ljharb

ljharb Jun 30, 2020

Author Member

that's an alternative; i was mainly trying to avoid the known crash. this way new kinds of things will crash, and we'll get bug reports :-)

declaredPropTypes[tsPropertySignature.key.name] = {
fullName: tsPropertySignature.key.name,
name: tsPropertySignature.key.name,
node: tsPropertySignature,
isRequired: !tsPropertySignature.optional
};
}
});
return false;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/lib/rules/no-unused-prop-types.js
Expand Up @@ -5555,6 +5555,23 @@ ruleTester.run('no-unused-prop-types', rule, {
}
]
},
{
code: [
'interface Foo {',
' foo: string;',
' [blah: string]: number;',
'}',
'const Hello = ({bar}: Foo) => {',
' return <div>Hello {bar}</div>;',
'}'
].join('\n'),
parser: parsers.TYPESCRIPT_ESLINT,
errors: [
{
message: '\'foo\' PropType is defined but prop is never used'
}
]
},
{
code: [
'const Hello = ({firstname}: {firstname: string, lastname: string}) => {',
Expand Down

0 comments on commit 830bde7

Please sign in to comment.