Skip to content

Commit

Permalink
Merge pull request #1132 from ethanjgoldberg/fix-quoted-type-annotations
Browse files Browse the repository at this point in the history
remove quotes when considering the name of a proptype
  • Loading branch information
ljharb committed May 26, 2017
2 parents 7ebcd48 + ec94608 commit ccb213d
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] === '"' && string[0] === string[string.length - 1]) {
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 @@ -1105,6 +1105,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 ccb213d

Please sign in to comment.