From 6fe0e10c2a41228f88e0eae1c188aaa25ddf27cf Mon Sep 17 00:00:00 2001 From: Ethan Goldberg Date: Tue, 28 Mar 2017 19:34:38 -0700 Subject: [PATCH 1/2] remove quotes when considering the name of a proptype 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. --- lib/rules/prop-types.js | 13 ++++++++++++- tests/lib/rules/prop-types.js | 10 ++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/rules/prop-types.js b/lib/rules/prop-types.js index 00fc1b2ba2..3e18d0aa73 100644 --- a/lib/rules/prop-types.js +++ b/lib/rules/prop-types.js @@ -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. @@ -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; diff --git a/tests/lib/rules/prop-types.js b/tests/lib/rules/prop-types.js index 9649f57eef..bd00fca966 100644 --- a/tests/lib/rules/prop-types.js +++ b/tests/lib/rules/prop-types.js @@ -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
{props[\'completed?\']}
;', + '}' + ].join('\n'), + parser: 'babel-eslint' }, { code: [ 'Card.propTypes = {', From ec94608fa63c30fec631fa52ee180a2e1bc22f15 Mon Sep 17 00:00:00 2001 From: Ethan Goldberg Date: Fri, 26 May 2017 11:52:52 -0700 Subject: [PATCH 2/2] review changes --- lib/rules/prop-types.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/prop-types.js b/lib/rules/prop-types.js index 3e18d0aa73..eec5cc4f08 100644 --- a/lib/rules/prop-types.js +++ b/lib/rules/prop-types.js @@ -305,7 +305,7 @@ module.exports = { * @param {string} the identifier to strip */ function stripQuotes(string) { - if (string[0] === '\'' || string[0] === '"') { + if (string[0] === '\'' || string[0] === '"' && string[0] === string[string.length - 1]) { return string.slice(1, string.length - 1); } return string;