Skip to content

Commit 9583f81

Browse files
committed
Merge pull request #124 from Cellule/prop_types_destructuring
Fix `prop-types` desctructuring with properties as string (fixes #118)
2 parents 1ab838c + c73d9b8 commit 9583f81

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

lib/rules/prop-types.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,16 @@ module.exports = function(context) {
164164
return tokens.length && tokens[0].value === '...';
165165
}
166166

167+
function getKeyValue(node) {
168+
var key = node.key;
169+
if (key) {
170+
if (key.type === 'Identifier') {
171+
return key.name;
172+
}
173+
return key.value;
174+
}
175+
}
176+
167177
/**
168178
* Iterates through a properties node, like a customized forEach.
169179
* @param {Object[]} properties Array of properties to iterate.
@@ -174,11 +184,10 @@ module.exports = function(context) {
174184
if (properties.length && typeof fn === 'function') {
175185
for (var i = 0, j = properties.length; i < j; i++) {
176186
var node = properties[i];
177-
var key = node.key;
178-
var keyName = key.type === 'Identifier' ? key.name : key.value;
187+
var key = getKeyValue(node);
179188

180189
var value = node.value;
181-
fn(keyName, value);
190+
fn(key, value);
182191
}
183192
}
184193
}
@@ -331,7 +340,7 @@ module.exports = function(context) {
331340
} else if (
332341
node.parent.parent.declarations &&
333342
node.parent.parent.declarations[0].id.properties &&
334-
node.parent.parent.declarations[0].id.properties[0].key.name
343+
getKeyValue(node.parent.parent.declarations[0].id.properties[0])
335344
) {
336345
type = 'destructuring';
337346
}
@@ -355,10 +364,13 @@ module.exports = function(context) {
355364
if (hasSpreadOperator(properties[i])) {
356365
continue;
357366
}
358-
usedPropTypes.push({
359-
name: properties[i].key.name,
360-
node: properties[i]
361-
});
367+
var propName = getKeyValue(properties[i]);
368+
if (propName) {
369+
usedPropTypes.push({
370+
name: propName,
371+
node: properties[i]
372+
});
373+
}
362374
}
363375
break;
364376
default:

tests/lib/rules/prop-types.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,23 @@ eslintTester.addRuleTest('lib/rules/prop-types', {
415415
classes: true,
416416
jsx: true
417417
}
418+
}, {
419+
code: [
420+
'class Hello extends React.Component {',
421+
' render() {',
422+
' var { ',
423+
' propX,',
424+
' "aria-controls": ariaControls, ',
425+
' ...props } = this.props;',
426+
' return <div>Hello</div>;',
427+
' }',
428+
'}',
429+
'Hello.propTypes = {',
430+
' "propX": React.PropTypes.string,',
431+
' "aria-controls": React.PropTypes.string',
432+
'};'
433+
].join('\n'),
434+
parser: 'babel-eslint'
418435
}
419436
],
420437

@@ -736,6 +753,25 @@ eslintTester.addRuleTest('lib/rules/prop-types', {
736753
{message: '\'numb.propX\' is missing in props validation for Hello'},
737754
{message: '\'stri.tooString\' is missing in props validation for Hello'}
738755
]
756+
}, {
757+
code: [
758+
'class Hello extends React.Component {',
759+
' render() {',
760+
' var { ',
761+
' "aria-controls": ariaControls, ',
762+
' propX,',
763+
' ...props } = this.props;',
764+
' return <div>Hello</div>;',
765+
' }',
766+
'}',
767+
'Hello.propTypes = {',
768+
' "aria-controls": React.PropTypes.string',
769+
'};'
770+
].join('\n'),
771+
parser: 'babel-eslint',
772+
errors: [
773+
{message: '\'propX\' is missing in props validation for Hello'}
774+
]
739775
}
740776
]
741777
});

0 commit comments

Comments
 (0)