Skip to content

Commit 35eb3ce

Browse files
authored
Merge pull request #1390 from jseminck/no-unused-prop-types-flow-read-only-v2
Support read-only props in Flow for no-unused-prop-types
2 parents 9e090ed + 44f03b7 commit 35eb3ce

File tree

3 files changed

+55
-24
lines changed

3 files changed

+55
-24
lines changed

lib/rules/no-unused-prop-types.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,26 @@ module.exports = {
301301
return tokens.length && tokens[0].value === '...';
302302
}
303303

304+
/**
305+
* Removes quotes from around an identifier.
306+
* @param {string} the identifier to strip
307+
*/
308+
function stripQuotes(string) {
309+
return string.replace(/^\'|\'$/g, '');
310+
}
311+
304312
/**
305313
* Retrieve the name of a key node
306314
* @param {ASTNode} node The AST node with the key.
307315
* @return {string} the name of the key
308316
*/
309317
function getKeyValue(node) {
310318
if (node.type === 'ObjectTypeProperty') {
311-
const tokens = context.getFirstTokens(node, 1);
312-
return tokens[0].value;
319+
const tokens = context.getFirstTokens(node, 2);
320+
return (tokens[0].value === '+' || tokens[0].value === '-'
321+
? tokens[1].value
322+
: stripQuotes(tokens[0].value)
323+
);
313324
}
314325
const key = node.key || node.argument;
315326
return key.type === 'Identifier' ? key.name : key.value;

lib/rules/prop-types.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,7 @@ module.exports = {
334334
* @param {string} the identifier to strip
335335
*/
336336
function stripQuotes(string) {
337-
if (string[0] === '\'' || string[0] === '"' && string[0] === string[string.length - 1]) {
338-
return string.slice(1, string.length - 1);
339-
}
340-
return string;
337+
return string.replace(/^\'|\'$/g, '');
341338
}
342339

343340
/**

tests/lib/rules/no-unused-prop-types.js

+41-18
Original file line numberDiff line numberDiff line change
@@ -2047,27 +2047,50 @@ ruleTester.run('no-unused-prop-types', rule, {
20472047
}, {
20482048
// issue #106
20492049
code: `
2050-
import React from 'react';
2051-
import SharedPropTypes from './SharedPropTypes';
2050+
import React from 'react';
2051+
import SharedPropTypes from './SharedPropTypes';
20522052
2053-
export default class A extends React.Component {
2054-
render() {
2055-
return (
2056-
<span
2057-
a={this.props.a}
2058-
b={this.props.b}
2059-
c={this.props.c}>
2060-
{this.props.children}
2061-
</span>
2062-
);
2053+
export default class A extends React.Component {
2054+
render() {
2055+
return (
2056+
<span
2057+
a={this.props.a}
2058+
b={this.props.b}
2059+
c={this.props.c}>
2060+
{this.props.children}
2061+
</span>
2062+
);
2063+
}
20632064
}
2064-
}
20652065
2066-
A.propTypes = {
2067-
a: React.PropTypes.string,
2068-
...SharedPropTypes // eslint-disable-line object-shorthand
2069-
};
2070-
`,
2066+
A.propTypes = {
2067+
a: React.PropTypes.string,
2068+
...SharedPropTypes // eslint-disable-line object-shorthand
2069+
};
2070+
`,
2071+
parser: 'babel-eslint'
2072+
}, {
2073+
// issue #933
2074+
code: `
2075+
type Props = {
2076+
+foo: number
2077+
}
2078+
class MyComponent extends React.Component {
2079+
render() {
2080+
return <div>{this.props.foo}</div>
2081+
}
2082+
}
2083+
`,
2084+
parser: 'babel-eslint'
2085+
}, {
2086+
code: `
2087+
type Props = {
2088+
\'completed?\': boolean,
2089+
};
2090+
const Hello = (props: Props): React.Element => {
2091+
return <div>{props[\'completed?\']}</div>;
2092+
}
2093+
`,
20712094
parser: 'babel-eslint'
20722095
}, {
20732096
code: `

0 commit comments

Comments
 (0)