Skip to content

Commit a91efe1

Browse files
committed
Apply sugestion from code review.
1 parent 74ae0a3 commit a91efe1

File tree

7 files changed

+78
-55
lines changed

7 files changed

+78
-55
lines changed

docs/rules/no-dupe-keys.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Prevent duplicate field names (no-dupe-keys)
1+
# Prevents duplication of field names (no-dupe-keys)
22

33
This rule prevents to use duplicated names.
44

@@ -54,13 +54,13 @@ export default {
5454

5555
This rule has an object option:
5656

57-
`"scope"`: [] (default) array of additional scopes to search for duplicates.
57+
`"groups"`: [] (default) array of additional groups to search for duplicates.
5858

5959
### Example:
6060

6161
```
6262
vue/no-dupe-keys: [2, {
63-
scope: ['asyncComputed']
63+
groups: ['asyncComputed']
6464
}]
6565
```
6666

docs/rules/no-reservered-keys.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ export default {
3131

3232
This rule has an object option:
3333

34-
`"reserved"`: [] (default) array of dissalowed names inside `scopes`.
34+
`"reserved"`: [] (default) array of dissalowed names inside `groups`.
3535

36-
`"scope"`: [] (default) array of additional scopes to search for duplicates.
36+
`"groups"`: [] (default) array of additional groups to search for duplicates.
3737

3838
### Example:
3939

lib/rules/no-dupe-keys.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @fileoverview Prevent duplicate field names
2+
* @fileoverview Prevents duplication of field names.
33
* @author Armano
44
*/
55
'use strict'
@@ -10,30 +10,24 @@ const utils = require('../utils')
1010
// Rule Definition
1111
// ------------------------------------------------------------------------------
1212

13-
const SCOPE_NAMES = new Set(['props', 'computed', 'data', 'methods'])
13+
const GROUP_NAMES = ['props', 'computed', 'data', 'methods']
1414

1515
function create (context) {
1616
const usedNames = []
17-
const scopeNames = SCOPE_NAMES
1817

1918
const options = context.options[0] || {}
20-
21-
if (options.scope) {
22-
options.scope.forEach(name => {
23-
scopeNames.add(name)
24-
})
25-
}
19+
const groups = new Set([...GROUP_NAMES, ...options.groups || []])
2620

2721
// ----------------------------------------------------------------------
2822
// Public
2923
// ----------------------------------------------------------------------
3024

31-
return utils.executeOnVueComponent(context, (obj) => {
32-
utils.iterateProperties(obj, scopeNames, (name, node, groupName) => {
25+
return utils.executeOnVue(context, (obj) => {
26+
utils.iterateProperties(obj, groups, (name, node, groupName) => {
3327
if (usedNames.indexOf(name) !== -1) {
3428
context.report({
3529
node: node,
36-
message: "Duplicate key '{{name}}'.",
30+
message: "Duplicated key '{{name}}'.",
3731
data: {
3832
name
3933
}
@@ -51,7 +45,7 @@ function create (context) {
5145
module.exports = {
5246
meta: {
5347
docs: {
54-
description: 'Prevent duplicate field names',
48+
description: 'Prevents duplication of field names.',
5549
category: 'Possible Errors',
5650
recommended: false
5751
},
@@ -60,7 +54,7 @@ module.exports = {
6054
{
6155
type: 'object',
6256
properties: {
63-
scope: {
57+
groups: {
6458
type: 'array'
6559
}
6660
},

lib/rules/no-reservered-keys.js

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,32 @@ const utils = require('../utils')
1010
// Rule Definition
1111
// ------------------------------------------------------------------------------
1212

13-
const RESERVER_NAMES = new Set(require('../utils/vue-reserved.json'))
14-
const SCOPE_NAMES = new Set(['props', 'computed', 'data', 'methods'])
13+
const RESERVED_KEYS = require('../utils/vue-reserved.json')
14+
const GROUP_NAMES = ['props', 'computed', 'data', 'methods']
1515

1616
function create (context) {
17-
const reservedNames = RESERVER_NAMES
18-
const scopeNames = SCOPE_NAMES
19-
2017
const options = context.options[0] || {}
21-
if (options.reserved) {
22-
options.reserved.forEach(name => {
23-
reservedNames.add(name)
24-
})
25-
}
26-
if (options.scope) {
27-
options.scope.forEach(name => {
28-
scopeNames.add(name)
29-
})
30-
}
18+
const reservedKeys = new Set([...RESERVED_KEYS, ...options.reserved || []])
19+
const groups = new Set([...GROUP_NAMES, ...options.groups || []])
3120

3221
// ----------------------------------------------------------------------
3322
// Public
3423
// ----------------------------------------------------------------------
3524

36-
return utils.executeOnVueComponent(context, (obj) => {
37-
utils.iterateProperties(obj, scopeNames, (name, node, groupName) => {
38-
if (groupName === 'data' && name.substr(0, 1) === '_') {
25+
return utils.executeOnVue(context, (obj) => {
26+
utils.iterateProperties(obj, groups, (name, node, groupName) => {
27+
if (groupName === 'data' && name[0] === '_') {
3928
context.report({
4029
node: node,
41-
message: "Field '{{name}}' which start with _ in data is reserved.",
30+
message: "Keys starting with with '_' are reserved in '{{name}}' group.",
4231
data: {
4332
name
4433
}
4534
})
46-
} else if (reservedNames.has(name)) {
35+
} else if (reservedKeys.has(name)) {
4736
context.report({
4837
node: node,
49-
message: "Reserved key '{{name}}'.",
38+
message: "Key '{{name}}' is reserved.",
5039
data: {
5140
name
5241
}
@@ -59,7 +48,7 @@ function create (context) {
5948
module.exports = {
6049
meta: {
6150
docs: {
62-
description: 'Prevent overwrite reserved keys',
51+
description: 'Prevent overwrite reserved keys.',
6352
category: 'Possible Errors',
6453
recommended: false
6554
},
@@ -71,7 +60,7 @@ module.exports = {
7160
reserved: {
7261
type: 'array'
7362
},
74-
scope: {
63+
groups: {
7564
type: 'array'
7665
}
7766
},

lib/utils/index.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ module.exports = {
442442
}
443443
},
444444

445-
iterateProperties (node, scopeNames, cb) {
445+
iterateProperties (node, groups, cb) {
446446
node.properties
447-
.filter(p => p.type === 'Property' && scopeNames.has(this.getStaticPropertyName(p.key)))
447+
.filter(p => p.type === 'Property' && groups.has(this.getStaticPropertyName(p.key)))
448448
.forEach(node => {
449449
const name = this.getStaticPropertyName(node.key)
450450
if (node.value.type === 'ArrayExpression') {
@@ -457,7 +457,14 @@ module.exports = {
457457
})
458458
},
459459

460+
/**
461+
* Interate over all elements inside ArrayExpression
462+
* @param {ASTNode} node Node to check
463+
* @param {string} groupName Name of parent group
464+
* @param {*} cb Callback function to iterate over nodes
465+
*/
460466
iterateArrayExpression (node, groupName, cb) {
467+
assert(node.type === 'ArrayExpression')
461468
node.elements.forEach(item => {
462469
const name = this.getStaticPropertyName(item)
463470
if (name) {
@@ -466,7 +473,14 @@ module.exports = {
466473
})
467474
},
468475

476+
/**
477+
* Interate over all elements inside ObjectExpression
478+
* @param {ASTNode} node Node to check
479+
* @param {string} groupName Name of parent group
480+
* @param {*} cb Callback function to iterate over nodes
481+
*/
469482
iterateObjectExpression (node, groupName, cb) {
483+
assert(node.type === 'ObjectExpression')
470484
node.properties.forEach(item => {
471485
const name = this.getStaticPropertyName(item)
472486
if (name) {
@@ -475,7 +489,14 @@ module.exports = {
475489
})
476490
},
477491

492+
/**
493+
* Interate over all elements inside FunctionExpression
494+
* @param {ASTNode} node Node to check
495+
* @param {string} groupName Name of parent group
496+
* @param {*} cb Callback function to iterate over nodes
497+
*/
478498
iterateFunctionExpression (node, groupName, cb) {
499+
assert(node.type === 'FunctionExpression')
479500
if (node.body.type === 'BlockStatement') {
480501
node.body.body.forEach(item => {
481502
if (item.type === 'ReturnStatement' && item.argument.type === 'ObjectExpression') {

tests/lib/rules/no-dupe-keys.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @fileoverview Prevent duplicate field names
2+
* @fileoverview Prevents duplication of field names.
33
* @author Armano
44
*/
55
'use strict'
@@ -98,13 +98,13 @@ ruleTester.run('no-dupe-keys', rule, {
9898
`,
9999
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
100100
errors: [{
101-
message: 'Duplicate key \'foo\'.',
101+
message: 'Duplicated key \'foo\'.',
102102
line: 5
103103
}, {
104-
message: 'Duplicate key \'foo\'.',
104+
message: 'Duplicated key \'foo\'.',
105105
line: 10
106106
}, {
107-
message: 'Duplicate key \'foo\'.',
107+
message: 'Duplicated key \'foo\'.',
108108
line: 14
109109
}]
110110
},
@@ -132,15 +132,34 @@ ruleTester.run('no-dupe-keys', rule, {
132132
`,
133133
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
134134
errors: [{
135-
message: 'Duplicate key \'foo\'.',
135+
message: 'Duplicated key \'foo\'.',
136136
line: 7
137137
}, {
138-
message: 'Duplicate key \'foo\'.',
138+
message: 'Duplicated key \'foo\'.',
139139
line: 13
140140
}, {
141-
message: 'Duplicate key \'foo\'.',
141+
message: 'Duplicated key \'foo\'.',
142142
line: 16
143143
}]
144+
},
145+
{
146+
filename: 'test.js',
147+
code: `
148+
new Vue({
149+
foo: {
150+
bar: String
151+
},
152+
data: {
153+
bar: null
154+
},
155+
})
156+
`,
157+
options: [{ groups: ['foo'] }],
158+
parserOptions: { ecmaVersion: 6 },
159+
errors: [{
160+
message: 'Duplicated key \'bar\'.',
161+
line: 7
162+
}]
144163
}
145164
]
146165
})

tests/lib/rules/no-reservered-keys.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ ruleTester.run('no-reservered-keys', rule, {
5555
`,
5656
parserOptions: { ecmaVersion: 6 },
5757
errors: [{
58-
message: 'Reserved key \'$el\'.',
58+
message: "Key '$el' is reserved.",
5959
line: 4
6060
}]
6161
},
@@ -70,7 +70,7 @@ ruleTester.run('no-reservered-keys', rule, {
7070
`,
7171
parserOptions: { ecmaVersion: 6 },
7272
errors: [{
73-
message: "Field '_foo' which start with _ in data is reserved.",
73+
message: "Keys starting with with '_' are reserved in '_foo' group.",
7474
line: 4
7575
}]
7676
},
@@ -83,10 +83,10 @@ ruleTester.run('no-reservered-keys', rule, {
8383
}
8484
})
8585
`,
86-
options: [{ reserved: ['bar'], scope: ['foo'] }],
86+
options: [{ reserved: ['bar'], groups: ['foo'] }],
8787
parserOptions: { ecmaVersion: 6 },
8888
errors: [{
89-
message: "Reserved key 'bar'.",
89+
message: "Key 'bar' is reserved.",
9090
line: 4
9191
}]
9292
}

0 commit comments

Comments
 (0)