Skip to content

Commit c53fa49

Browse files
committed
Add getStaticPropertyName & apply sugestions from code review
1 parent 1cd4b98 commit c53fa49

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

lib/rules/require-prop-types.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,25 @@ function create (context) {
1212
// ----------------------------------------------------------------------
1313

1414
function objectHasType (nodes) {
15-
return !!(nodes
15+
const typeProperty = nodes.properties
1616
.filter(p =>
17-
p.key.type === 'Identifier' &&
18-
p.key.name === 'type' &&
17+
utils.getStaticPropertyName(p.key) === 'type' &&
1918
(
2019
p.value.type !== 'ArrayExpression' ||
2120
p.value.elements.length > 0
2221
)
23-
)[0])
22+
)[0]
23+
return Boolean(typeProperty)
2424
}
2525

2626
function checkProperties (items) {
2727
items.map(cp => {
2828
if (cp.type !== 'Property') {
2929
return
3030
}
31-
const key = cp.key.name
3231
let hasType = true
3332
if (cp.value.type === 'ObjectExpression') { // foo: {
34-
hasType = objectHasType(cp.value.properties)
33+
hasType = objectHasType(cp.value)
3534
} else if (cp.value.type === 'ArrayExpression') { // foo: [
3635
hasType = cp.value.elements.length > 0
3736
} else if (cp.value.type === 'FunctionExpression' || cp.value.type === 'ArrowFunctionExpression') {
@@ -42,7 +41,7 @@ function create (context) {
4241
node: cp,
4342
message: 'Prop "{{name}}" should define at least it\'s type.',
4443
data: {
45-
name: key
44+
name: cp.key.name
4645
}
4746
})
4847
}

lib/utils/index.js

+42
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,48 @@ module.exports = {
272272
return members.reverse()
273273
},
274274

275+
/**
276+
* Gets the property name of a given node.
277+
* @param {ASTNode} node - The node to get.
278+
* @return {string|null} The property name if static. Otherwise, null.
279+
*/
280+
getStaticPropertyName (node) {
281+
let prop
282+
switch (node && node.type) {
283+
case 'Property':
284+
case 'MethodDefinition':
285+
prop = node.key
286+
break
287+
case 'MemberExpression':
288+
prop = node.property
289+
break
290+
case 'Literal':
291+
case 'TemplateLiteral':
292+
case 'Identifier':
293+
prop = node
294+
break
295+
// no default
296+
}
297+
298+
switch (prop && prop.type) {
299+
case 'Literal':
300+
return String(prop.value)
301+
case 'TemplateLiteral':
302+
if (prop.expressions.length === 0 && prop.quasis.length === 1) {
303+
return prop.quasis[0].value.cooked
304+
}
305+
break
306+
case 'Identifier':
307+
if (!node.computed) {
308+
return prop.name
309+
}
310+
break
311+
// no default
312+
}
313+
314+
return null
315+
},
316+
275317
/**
276318
* Get all computed properties by looking at all component's properties
277319
* @param {ObjectExpression} Object with component definition

tests/lib/rules/require-prop-types.js

+13
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ ruleTester.run('require-prop-types', rule, {
5555
}
5656
`,
5757
parserOptions: { ecmaVersion: 6, sourceType: 'module' }
58+
},
59+
{
60+
filename: 'test.vue',
61+
code: `
62+
export default {
63+
props: {
64+
foo: {
65+
['type']: String
66+
}
67+
}
68+
}
69+
`,
70+
parserOptions: { ecmaVersion: 6, sourceType: 'module' }
5871
}
5972
],
6073

0 commit comments

Comments
 (0)