Skip to content

Commit

Permalink
remove quotes when considering the name of a proptype
Browse files Browse the repository at this point in the history
The added test illustrates the failing case. Proptypes with quotes
were referred to by the quoted name, instead of by the bare name,
for flow types.
  • Loading branch information
Ethan Goldberg committed Mar 29, 2017
1 parent bfdd722 commit d525a4f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/rules/prop-types.js
Expand Up @@ -300,6 +300,17 @@ module.exports = {
return tokens.length && tokens[0].value === '...';
}

/**
* Removes quotes from around an identifier.
* @param {string} the identifier to strip
*/
function stripQuotes(string) {
if (string[0] === '\'' || string[0] === '"') {
return string.slice(1, string.length - 1);
}
return string;
}

/**
* Retrieve the name of a key node
* @param {ASTNode} node The AST node with the key.
Expand All @@ -310,7 +321,7 @@ module.exports = {
var tokens = context.getFirstTokens(node, 2);
return (tokens[0].value === '+' || tokens[0].value === '-'
? tokens[1].value
: tokens[0].value
: stripQuotes(tokens[0].value)
);
}
var key = node.key || node.argument;
Expand Down
10 changes: 10 additions & 0 deletions tests/lib/rules/prop-types.js
Expand Up @@ -1155,6 +1155,16 @@ ruleTester.run('prop-types', rule, {
'}'
].join('\n'),
parser: 'babel-eslint'
}, {
code: [
'type Props = {',
' \'completed?\': boolean,',
'};',
'const Hello = (props: Props): React.Element => {',
' return <div>{props[\'completed?\']}</div>;',
'}'
].join('\n'),
parser: 'babel-eslint'
}, {
code: [
'Card.propTypes = {',
Expand Down

0 comments on commit d525a4f

Please sign in to comment.