diff --git a/lib/rules/no-unused-prop-types.js b/lib/rules/no-unused-prop-types.js index f595ce58ee..3b3b4093c5 100644 --- a/lib/rules/no-unused-prop-types.js +++ b/lib/rules/no-unused-prop-types.js @@ -258,6 +258,14 @@ module.exports = { return tokens.length && tokens[0].value === '...'; } + /** + * Removes quotes from around an identifier. + * @param {string} the identifier to strip + */ + function stripQuotes(string) { + return string.replace(/^\'|\'$/g, ''); + } + /** * Retrieve the name of a key node * @param {ASTNode} node The AST node with the key. @@ -265,8 +273,11 @@ module.exports = { */ function getKeyValue(node) { if (node.type === 'ObjectTypeProperty') { - const tokens = context.getFirstTokens(node, 1); - return tokens[0].value; + const tokens = context.getFirstTokens(node, 2); + return (tokens[0].value === '+' || tokens[0].value === '-' + ? tokens[1].value + : stripQuotes(tokens[0].value) + ); } const key = node.key || node.argument; return key.type === 'Identifier' ? key.name : key.value; diff --git a/lib/rules/prop-types.js b/lib/rules/prop-types.js index 6e7f48dfc8..5276474058 100644 --- a/lib/rules/prop-types.js +++ b/lib/rules/prop-types.js @@ -333,10 +333,7 @@ module.exports = { * @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; + return string.replace(/^\'|\'$/g, ''); } /** diff --git a/tests/lib/rules/no-unused-prop-types.js b/tests/lib/rules/no-unused-prop-types.js index 2a8b4557b8..783cac6f79 100644 --- a/tests/lib/rules/no-unused-prop-types.js +++ b/tests/lib/rules/no-unused-prop-types.js @@ -2032,27 +2032,50 @@ ruleTester.run('no-unused-prop-types', rule, { }, { // issue #106 code: ` - import React from 'react'; - import SharedPropTypes from './SharedPropTypes'; + import React from 'react'; + import SharedPropTypes from './SharedPropTypes'; - export default class A extends React.Component { - render() { - return ( - - {this.props.children} - - ); + export default class A extends React.Component { + render() { + return ( + + {this.props.children} + + ); + } } - } - A.propTypes = { - a: React.PropTypes.string, - ...SharedPropTypes // eslint-disable-line object-shorthand - }; - `, + A.propTypes = { + a: React.PropTypes.string, + ...SharedPropTypes // eslint-disable-line object-shorthand + }; + `, + parser: 'babel-eslint' + }, { + // issue #933 + code: ` + type Props = { + +foo: number + } + class MyComponent extends React.Component { + render() { + return