Skip to content

Commit ccb213d

Browse files
authored
Merge pull request #1132 from ethanjgoldberg/fix-quoted-type-annotations
remove quotes when considering the name of a proptype
2 parents 7ebcd48 + ec94608 commit ccb213d

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/rules/prop-types.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,17 @@ module.exports = {
300300
return tokens.length && tokens[0].value === '...';
301301
}
302302

303+
/**
304+
* Removes quotes from around an identifier.
305+
* @param {string} the identifier to strip
306+
*/
307+
function stripQuotes(string) {
308+
if (string[0] === '\'' || string[0] === '"' && string[0] === string[string.length - 1]) {
309+
return string.slice(1, string.length - 1);
310+
}
311+
return string;
312+
}
313+
303314
/**
304315
* Retrieve the name of a key node
305316
* @param {ASTNode} node The AST node with the key.
@@ -310,7 +321,7 @@ module.exports = {
310321
var tokens = context.getFirstTokens(node, 2);
311322
return (tokens[0].value === '+' || tokens[0].value === '-'
312323
? tokens[1].value
313-
: tokens[0].value
324+
: stripQuotes(tokens[0].value)
314325
);
315326
}
316327
var key = node.key || node.argument;

tests/lib/rules/prop-types.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,16 @@ ruleTester.run('prop-types', rule, {
11051105
'}'
11061106
].join('\n'),
11071107
parser: 'babel-eslint'
1108+
}, {
1109+
code: [
1110+
'type Props = {',
1111+
' \'completed?\': boolean,',
1112+
'};',
1113+
'const Hello = (props: Props): React.Element => {',
1114+
' return <div>{props[\'completed?\']}</div>;',
1115+
'}'
1116+
].join('\n'),
1117+
parser: 'babel-eslint'
11081118
}, {
11091119
code: [
11101120
'Card.propTypes = {',

0 commit comments

Comments
 (0)