Skip to content

Commit c7c7191

Browse files
committed
Move stripQuotes logic to no-unused-prop-types as well and add a test case
1 parent cf5e8a3 commit c7c7191

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/rules/no-unused-prop-types.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,17 @@ module.exports = {
258258
return tokens.length && tokens[0].value === '...';
259259
}
260260

261+
/**
262+
* Removes quotes from around an identifier.
263+
* @param {string} the identifier to strip
264+
*/
265+
function stripQuotes(string) {
266+
if (string[0] === '\'' || string[0] === '"' && string[0] === string[string.length - 1]) {
267+
return string.slice(1, string.length - 1);
268+
}
269+
return string;
270+
}
271+
261272
/**
262273
* Retrieve the name of a key node
263274
* @param {ASTNode} node The AST node with the key.
@@ -266,7 +277,7 @@ module.exports = {
266277
function getKeyValue(node) {
267278
if (node.type === 'ObjectTypeProperty') {
268279
const tokens = context.getFirstTokens(node, {count: 1, filter: tokenNode => ['Identifier', 'String'].includes(tokenNode.type)});
269-
return tokens[0].value;
280+
return stripQuotes(tokens[0].value);
270281
}
271282
const key = node.key || node.argument;
272283
return key.type === 'Identifier' ? key.name : key.value;

tests/lib/rules/no-unused-prop-types.js

+10
Original file line numberDiff line numberDiff line change
@@ -2067,6 +2067,16 @@ ruleTester.run('no-unused-prop-types', rule, {
20672067
}
20682068
`,
20692069
parser: 'babel-eslint'
2070+
}, {
2071+
code: `
2072+
type Props = {
2073+
\'completed?\': boolean,
2074+
};
2075+
const Hello = (props: Props): React.Element => {
2076+
return <div>{props[\'completed?\']}</div>;
2077+
}
2078+
`,
2079+
parser: 'babel-eslint'
20702080
}
20712081
],
20722082

0 commit comments

Comments
 (0)