Skip to content

Commit 23d360c

Browse files
armano2michalsnik
authored andcommitted
Rename rule no-reservered-keys to no-reserved-keys. (#157)
fixes #155
1 parent becfb8a commit 23d360c

File tree

4 files changed

+236
-2
lines changed

4 files changed

+236
-2
lines changed

Diff for: docs/rules/no-reserved-keys.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Prevent overwrite reserved keys (no-reserved-keys)
2+
3+
This rule prevents to use reserved names from to avoid conflicts and unexpected behavior.
4+
5+
## Rule Details
6+
7+
:-1: Examples of **incorrect** code for this rule:
8+
9+
```js
10+
export default {
11+
props: {
12+
$el: String
13+
},
14+
computed: {
15+
$on: {
16+
get () {
17+
}
18+
}
19+
},
20+
data: {
21+
_foo: null
22+
},
23+
methods: {
24+
$nextTick () {
25+
}
26+
}
27+
}
28+
```
29+
30+
## :wrench: Options
31+
32+
This rule has an object option:
33+
34+
`"reserved"`: [] (default) array of dissalowed names inside `groups`.
35+
36+
`"groups"`: [] (default) array of additional groups to search for duplicates.
37+
38+
### Example:
39+
40+
```
41+
vue/no-reserved-keys: [2, {
42+
reserved: ['foo', 'foo2'],
43+
groups: ['asyncComputed']
44+
}]
45+
```
46+
47+
:-1: Examples of **incorrect** code for this configuration
48+
49+
```js
50+
export default {
51+
asyncComputed: {
52+
foo2 () {}
53+
},
54+
computed: {
55+
foo () {}
56+
}
57+
}
58+
```

Diff for: lib/rules/no-reserved-keys.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* @fileoverview Prevent overwrite reserved keys
3+
* @author Armano
4+
*/
5+
'use strict'
6+
7+
const utils = require('../utils')
8+
9+
// ------------------------------------------------------------------------------
10+
// Rule Definition
11+
// ------------------------------------------------------------------------------
12+
13+
const RESERVED_KEYS = require('../utils/vue-reserved.json')
14+
const GROUP_NAMES = ['props', 'computed', 'data', 'methods']
15+
16+
function create (context) {
17+
const options = context.options[0] || {}
18+
const reservedKeys = new Set(RESERVED_KEYS.concat(options.reserved || []))
19+
const groups = new Set(GROUP_NAMES.concat(options.groups || []))
20+
21+
// ----------------------------------------------------------------------
22+
// Public
23+
// ----------------------------------------------------------------------
24+
25+
return utils.executeOnVue(context, (obj) => {
26+
const properties = utils.iterateProperties(obj, groups)
27+
for (const o of properties) {
28+
if (o.groupName === 'data' && o.name[0] === '_') {
29+
context.report({
30+
node: o.node,
31+
message: "Keys starting with with '_' are reserved in '{{name}}' group.",
32+
data: {
33+
name: o.name
34+
}
35+
})
36+
} else if (reservedKeys.has(o.name)) {
37+
context.report({
38+
node: o.node,
39+
message: "Key '{{name}}' is reserved.",
40+
data: {
41+
name: o.name
42+
}
43+
})
44+
}
45+
}
46+
})
47+
}
48+
49+
module.exports = {
50+
meta: {
51+
docs: {
52+
description: 'Prevent overwrite reserved keys.',
53+
category: 'Possible Errors',
54+
recommended: false
55+
},
56+
fixable: null,
57+
schema: [
58+
{
59+
type: 'object',
60+
properties: {
61+
reserved: {
62+
type: 'array'
63+
},
64+
groups: {
65+
type: 'array'
66+
}
67+
},
68+
additionalProperties: false
69+
}
70+
]
71+
},
72+
73+
create
74+
}

Diff for: lib/rules/no-reservered-keys.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ module.exports = {
5151
docs: {
5252
description: 'Prevent overwrite reserved keys.',
5353
category: 'Possible Errors',
54-
recommended: false
54+
recommended: false,
55+
replacedBy: ['no-reserved-keys']
5556
},
56-
fixable: null, // or "code" or "whitespace"
57+
deprecated: true,
58+
fixable: null,
5759
schema: [
5860
{
5961
type: 'object',

Diff for: tests/lib/rules/no-reserved-keys.js

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* @fileoverview Prevent overwrite reserved keys
3+
* @author Armano
4+
*/
5+
'use strict'
6+
7+
// ------------------------------------------------------------------------------
8+
// Requirements
9+
// ------------------------------------------------------------------------------
10+
11+
const rule = require('../../../lib/rules/no-reserved-keys')
12+
const RuleTester = require('eslint').RuleTester
13+
14+
const parserOptions = {
15+
ecmaVersion: 7,
16+
sourceType: 'module',
17+
ecmaFeatures: { experimentalObjectRestSpread: true }
18+
}
19+
20+
// ------------------------------------------------------------------------------
21+
// Tests
22+
// ------------------------------------------------------------------------------
23+
24+
const ruleTester = new RuleTester()
25+
ruleTester.run('no-reserved-keys', rule, {
26+
valid: [
27+
{
28+
filename: 'test.vue',
29+
code: `
30+
export default {
31+
props: ['foo'],
32+
computed: {
33+
bar () {
34+
}
35+
},
36+
data () {
37+
return {
38+
dat: null
39+
}
40+
},
41+
methods: {
42+
_foo () {},
43+
test () {
44+
}
45+
}
46+
}
47+
`,
48+
parserOptions
49+
}
50+
],
51+
52+
invalid: [
53+
{
54+
filename: 'test.js',
55+
code: `
56+
new Vue({
57+
props: {
58+
$el: String
59+
}
60+
})
61+
`,
62+
parserOptions: { ecmaVersion: 6 },
63+
errors: [{
64+
message: "Key '$el' is reserved.",
65+
line: 4
66+
}]
67+
},
68+
{
69+
filename: 'test.js',
70+
code: `
71+
new Vue({
72+
data: {
73+
_foo: String
74+
}
75+
})
76+
`,
77+
parserOptions: { ecmaVersion: 6 },
78+
errors: [{
79+
message: "Keys starting with with '_' are reserved in '_foo' group.",
80+
line: 4
81+
}]
82+
},
83+
{
84+
filename: 'test.js',
85+
code: `
86+
new Vue({
87+
foo: {
88+
bar: String
89+
}
90+
})
91+
`,
92+
options: [{ reserved: ['bar'], groups: ['foo'] }],
93+
parserOptions: { ecmaVersion: 6 },
94+
errors: [{
95+
message: "Key 'bar' is reserved.",
96+
line: 4
97+
}]
98+
}
99+
]
100+
})

0 commit comments

Comments
 (0)