Skip to content

Commit 05a7281

Browse files
zpaogajus
authored andcommitted
fix: don't crash when encountering unparameterized Arrays (#402)
While these are disallowed in Flow, `typeof Array` is a valid type, which could trigger this. Rather than try to enumerate other possible cases, I simply check to make sure we don't crash when we hit them.
1 parent 397b7a1 commit 05a7281

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/rules/arrayStyle/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ export default (defaultConfig, simpleType) => {
4747
// verbose
4848
GenericTypeAnnotation (node) {
4949
if (node.id.name === 'Array') {
50-
if (node.typeParameters.params.length === 1) {
50+
// Don't report on un-parameterized Array annotations. There are valid cases for this,
51+
// but regardless, we should not crash when encountering them.
52+
if (node.typeParameters && node.typeParameters.params.length === 1) {
5153
const elementTypeNode = node.typeParameters.params[0];
5254
const rawElementType = context.getSourceCode().getText(elementTypeNode);
5355
const inlinedType = inlineType(rawElementType);

tests/rules/assertions/arrayStyleSimpleType.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ export default {
128128
onlyFilesWithFlowAnnotation: true
129129
}
130130
}
131+
},
132+
133+
// While this isn't valid flow, we shouldn't disallow it.
134+
{
135+
code: 'type X = Array'
136+
},
137+
138+
// Valid flow.
139+
{
140+
code: 'type X = typeof Array'
131141
}
132142
]
133143
};

0 commit comments

Comments
 (0)