Skip to content

Commit 1347b26

Browse files
committed
Instead of passing callback return generator function
1 parent 239dff6 commit 1347b26

File tree

3 files changed

+50
-45
lines changed

3 files changed

+50
-45
lines changed

lib/rules/no-dupe-keys.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,19 @@ function create (context) {
2323
// ----------------------------------------------------------------------
2424

2525
return utils.executeOnVue(context, (obj) => {
26-
utils.iterateProperties(obj, groups, (name, node, groupName) => {
27-
if (usedNames.indexOf(name) !== -1) {
26+
const properties = utils.iterateProperties(obj, groups)
27+
for (const o of properties) {
28+
if (usedNames.indexOf(o.name) !== -1) {
2829
context.report({
29-
node: node,
30+
node: o.node,
3031
message: "Duplicated key '{{name}}'.",
3132
data: {
32-
name
33+
name: o.name
3334
}
3435
})
3536
}
36-
usedNames.push(name)
37-
})
37+
usedNames.push(o.name)
38+
}
3839
})
3940
}
4041

lib/rules/no-reservered-keys.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,26 @@ function create (context) {
2323
// ----------------------------------------------------------------------
2424

2525
return utils.executeOnVue(context, (obj) => {
26-
utils.iterateProperties(obj, groups, (name, node, groupName) => {
27-
if (groupName === 'data' && name[0] === '_') {
26+
const properties = utils.iterateProperties(obj, groups)
27+
for (const o of properties) {
28+
if (o.groupName === 'data' && o.name[0] === '_') {
2829
context.report({
29-
node: node,
30+
node: o.node,
3031
message: "Keys starting with with '_' are reserved in '{{name}}' group.",
3132
data: {
32-
name
33+
name: o.name
3334
}
3435
})
35-
} else if (reservedKeys.has(name)) {
36+
} else if (reservedKeys.has(o.name)) {
3637
context.report({
37-
node: node,
38+
node: o.node,
3839
message: "Key '{{name}}' is reserved.",
3940
data: {
40-
name
41+
name: o.name
4142
}
4243
})
4344
}
44-
})
45+
}
4546
})
4647
}
4748

lib/utils/index.js

+34-31
Original file line numberDiff line numberDiff line change
@@ -442,67 +442,70 @@ module.exports = {
442442
}
443443
},
444444

445-
iterateProperties (node, groups, cb) {
446-
node.properties
447-
.filter(p => p.type === 'Property' && groups.has(this.getStaticPropertyName(p.key)))
448-
.forEach(node => {
449-
const name = this.getStaticPropertyName(node.key)
450-
if (node.value.type === 'ArrayExpression') {
451-
this.iterateArrayExpression(node.value, name, cb)
452-
} else if (node.value.type === 'ObjectExpression') {
453-
this.iterateObjectExpression(node.value, name, cb)
454-
} else if (node.value.type === 'FunctionExpression') {
455-
this.iterateFunctionExpression(node.value, name, cb)
456-
}
457-
})
445+
/**
446+
* Return generator with all properties
447+
* @param {ASTNode} node Node to check
448+
* @param {string} groupName Name of parent group
449+
*/
450+
* iterateProperties (node, groups) {
451+
const nodes = node.properties.filter(p => p.type === 'Property' && groups.has(this.getStaticPropertyName(p.key)))
452+
for (const item of nodes) {
453+
const name = this.getStaticPropertyName(item.key)
454+
if (item.value.type === 'ArrayExpression') {
455+
yield * this.iterateArrayExpression(item.value, name)
456+
} else if (item.value.type === 'ObjectExpression') {
457+
yield * this.iterateObjectExpression(item.value, name)
458+
} else if (item.value.type === 'FunctionExpression') {
459+
yield * this.iterateFunctionExpression(item.value, name)
460+
}
461+
}
458462
},
459463

460464
/**
461-
* Interate over all elements inside ArrayExpression
465+
* Return generator with all elements inside ArrayExpression
462466
* @param {ASTNode} node Node to check
463467
* @param {string} groupName Name of parent group
464-
* @param {*} cb Callback function to iterate over nodes
465468
*/
466-
iterateArrayExpression (node, groupName, cb) {
469+
* iterateArrayExpression (node, groupName) {
467470
assert(node.type === 'ArrayExpression')
468-
node.elements.forEach(item => {
471+
for (const item of node.elements) {
469472
const name = this.getStaticPropertyName(item)
470473
if (name) {
471-
cb(name, item, groupName)
474+
const obj = { name, groupName, node: item }
475+
yield obj
472476
}
473-
})
477+
}
474478
},
475479

476480
/**
477-
* Interate over all elements inside ObjectExpression
481+
* Return generator with all elements inside ObjectExpression
478482
* @param {ASTNode} node Node to check
479483
* @param {string} groupName Name of parent group
480-
* @param {*} cb Callback function to iterate over nodes
481484
*/
482-
iterateObjectExpression (node, groupName, cb) {
485+
* iterateObjectExpression (node, groupName) {
483486
assert(node.type === 'ObjectExpression')
484-
node.properties.forEach(item => {
487+
for (const item of node.properties) {
485488
const name = this.getStaticPropertyName(item)
486489
if (name) {
487-
cb(name, item.key, groupName)
490+
const obj = { name, groupName, node: item.key }
491+
yield obj
488492
}
489-
})
493+
}
490494
},
491495

492496
/**
493-
* Interate over all elements inside FunctionExpression
497+
* Return generator with all elements inside FunctionExpression
494498
* @param {ASTNode} node Node to check
495499
* @param {string} groupName Name of parent group
496-
* @param {*} cb Callback function to iterate over nodes
497500
*/
498-
iterateFunctionExpression (node, groupName, cb) {
501+
* iterateFunctionExpression (node, groupName) {
499502
assert(node.type === 'FunctionExpression')
500503
if (node.body.type === 'BlockStatement') {
501-
node.body.body.forEach(item => {
504+
for (const item of node.body.body) {
502505
if (item.type === 'ReturnStatement' && item.argument.type === 'ObjectExpression') {
503-
this.iterateObjectExpression(item.argument, groupName, cb)
506+
yield * this.iterateObjectExpression(item.argument, groupName)
504507
}
505-
})
508+
}
506509
}
507510
}
508511
}

0 commit comments

Comments
 (0)