Skip to content

Commit 9944d30

Browse files
committed
Move stripQuotes logic to no-unused-prop-types as well and add a test case
1 parent 8958efe commit 9944d30

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
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-1
Original file line numberDiff line numberDiff line change
@@ -2030,7 +2030,6 @@ ruleTester.run('no-unused-prop-types', rule, {
20302030
parser: 'babel-eslint',
20312031
options: [{skipShapeProps: false}]
20322032
}, {
2033-
// issue #933
20342033
code: `
20352034
type Props = {
20362035
+foo: number
@@ -2042,6 +2041,16 @@ ruleTester.run('no-unused-prop-types', rule, {
20422041
}
20432042
`,
20442043
parser: 'babel-eslint'
2044+
}, {
2045+
code: `
2046+
type Props = {
2047+
\'completed?\': boolean,
2048+
};
2049+
const Hello = (props: Props): React.Element => {
2050+
return <div>{props[\'completed?\']}</div>;
2051+
}
2052+
`,
2053+
parser: 'babel-eslint'
20452054
}
20462055
],
20472056

0 commit comments

Comments
 (0)