Skip to content

Commit d841a94

Browse files
committed
Add unit test for getPropsProperties
1 parent 5bef08a commit d841a94

File tree

9 files changed

+109
-10
lines changed

9 files changed

+109
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.idea
2+
*.iml
23
/.nyc_output
34
/coverage
45
/tests/integrations/*/node_modules

lib/rules/prop-name-casing.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function create (context) {
3737

3838
return utils.executeOnVue(context, (obj) => {
3939
const items = utils.getPropsProperties(obj)
40+
.props
4041
.filter(cp => cp.key.type === 'Literal' || (cp.key.type === 'Identifier' && !cp.node.computed))
4142

4243
for (const item of items) {

lib/rules/require-default-prop.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = {
2121
schema: []
2222
},
2323

24-
create: function (context) {
24+
create (context) {
2525
// ----------------------------------------------------------------------
2626
// Helpers
2727
// ----------------------------------------------------------------------

lib/rules/require-prop-type-constructor.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ module.exports = {
7171

7272
return utils.executeOnVueComponent(context, (obj) => {
7373
const properties = utils.getPropsProperties(obj)
74+
.props
7475
.filter(cp => cp.value)
7576

7677
for (const p of properties) {

lib/rules/require-prop-types.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,15 @@ module.exports = {
7272
return utils.executeOnVue(context, (obj) => {
7373
const properties = utils.getPropsProperties(obj)
7474

75-
for (const cp of properties) {
76-
checkProperty(cp.key, cp.value, cp.node)
75+
if (properties.type === 'ArrayExpression') {
76+
context.report({
77+
node: properties.node,
78+
message: 'Props should at least define their types.'
79+
})
80+
} else {
81+
for (const cp of properties.props) {
82+
checkProperty(cp.key, cp.value, cp.node)
83+
}
7784
}
7885
})
7986
}

lib/rules/require-valid-default-prop.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ module.exports = {
8989

9090
return utils.executeOnVue(context, obj => {
9191
const properties = utils.getPropsProperties(obj)
92+
.props
9293
.filter(cp => cp.value && cp.value.type === 'ObjectExpression')
9394

9495
for (const prop of properties) {

lib/utils/index.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,8 @@ module.exports = {
367367

368368
/**
369369
* Get all props by looking at all component's properties
370-
* @param {ObjectExpression} Object with component definition
371-
* @return {Array<{key: ASTNode, value: ASTNode | null, node: ASTNode}>} Array of props in format:
370+
* @param {ObjectExpression} componentObject Object with component definition
371+
* @return {Object} Array of props in format: { node: ASTNode, type: string, props: Array<{key: ASTNode, value: ASTNode | null, node: ASTNode}> }
372372
*/
373373
getPropsProperties (componentObject) {
374374
const propsNode = componentObject.properties
@@ -379,7 +379,13 @@ module.exports = {
379379
(p.value.type === 'ObjectExpression' || p.value.type === 'ArrayExpression')
380380
)
381381

382-
if (!propsNode) { return [] }
382+
if (!propsNode) {
383+
return {
384+
node: null,
385+
type: null,
386+
props: []
387+
}
388+
}
383389

384390
let props
385391

@@ -397,12 +403,16 @@ module.exports = {
397403
})
398404
}
399405

400-
return props
406+
return {
407+
node: propsNode,
408+
type: propsNode.value.type,
409+
props: props
410+
}
401411
},
402412

403413
/**
404414
* Get all computed properties by looking at all component's properties
405-
* @param {ObjectExpression} Object with component definition
415+
* @param {ObjectExpression} componentObject Object with component definition
406416
* @return {Array} Array of computed properties in format: [{key: String, value: ASTNode}]
407417
*/
408418
getComputedProperties (componentObject) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ ruleTester.run('require-prop-types', rule, {
154154
`,
155155
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
156156
errors: [{
157-
message: 'Prop "foo" should define at least its type.',
157+
message: 'Props should at least define their types.',
158158
line: 3
159159
}]
160160
},
@@ -167,7 +167,7 @@ ruleTester.run('require-prop-types', rule, {
167167
`,
168168
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
169169
errors: [{
170-
message: 'Prop "foo" should define at least its type.',
170+
message: 'Props should at least define their types.',
171171
line: 3
172172
}]
173173
},

tests/lib/utils/index.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,81 @@ describe('getRegisteredComponents', () => {
247247
)
248248
})
249249
})
250+
251+
describe('getComputedProperties', () => {
252+
let node
253+
254+
const parse = function (code) {
255+
const data = babelEslint.parse(code).body[0].declarations[0].init
256+
return utils.getPropsProperties(data)
257+
}
258+
259+
it('should return empty array when there is no props property', () => {
260+
node = parse(`const test = {
261+
name: 'test',
262+
data() {
263+
return {}
264+
}
265+
}`)
266+
267+
assert.notOk(node.type)
268+
assert.notOk(node.node)
269+
assert.equal(node.props.length,0)
270+
})
271+
272+
it('should return computed props', () => {
273+
node = parse(`const test = {
274+
name: 'test',
275+
data() {
276+
return {}
277+
},
278+
props: {
279+
...foo,
280+
a: String,
281+
b: {},
282+
c: [String],
283+
d
284+
}
285+
}`)
286+
287+
assert.ok(node.node)
288+
assert.equal(node.type, 'ObjectExpression', 'it detects correct type')
289+
assert.equal(node.props.length, 4, 'it detects all props' )
290+
291+
assert.ok(node.props[0].value)
292+
assert.ok(node.props[0].key.type === 'Identifier')
293+
assert.ok(node.props[0].node.type === 'Property')
294+
assert.ok(node.props[0].value.type === 'Identifier')
295+
296+
assert.ok(node.props[1].value)
297+
assert.ok(node.props[1].key.type === 'Identifier')
298+
assert.ok(node.props[1].node.type === 'Property')
299+
assert.ok(node.props[1].value.type === 'ObjectExpression')
300+
301+
assert.ok(node.props[2].value)
302+
assert.ok(node.props[2].key.type === 'Identifier')
303+
assert.ok(node.props[2].node.type === 'Property')
304+
assert.ok(node.props[2].value.type === 'ArrayExpression')
305+
306+
assert.ok(node.props[3].value)
307+
assert.ok(node.props[3].key.type === node.props[3].value.type)
308+
assert.ok(node.props[3].node.type === 'Property')
309+
})
310+
311+
it('should return computed from array props', () => {
312+
node = parse(`const test = {
313+
name: 'test',
314+
data() {
315+
return {}
316+
},
317+
props: ['a', b, \`c\`, null]
318+
}`)
319+
320+
assert.ok(node.node)
321+
assert.equal(node.type, 'ArrayExpression', 'it detects correct type')
322+
assert.equal(node.props.length, 1, 'it detects all props' )
323+
324+
assert.notOk(node.props[0].value)
325+
assert.ok(node.props[0].key.type === 'Literal')
326+
})
327+
})

0 commit comments

Comments
 (0)