Skip to content

Commit

Permalink
Rename rule no-reservered-keys to no-reserved-keys. (#157)
Browse files Browse the repository at this point in the history
fixes #155
  • Loading branch information
armano2 authored and michalsnik committed Aug 15, 2017
1 parent becfb8a commit 23d360c
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 2 deletions.
58 changes: 58 additions & 0 deletions docs/rules/no-reserved-keys.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Prevent overwrite reserved keys (no-reserved-keys)

This rule prevents to use reserved names from to avoid conflicts and unexpected behavior.

## Rule Details

:-1: Examples of **incorrect** code for this rule:

```js
export default {
props: {
$el: String
},
computed: {
$on: {
get () {
}
}
},
data: {
_foo: null
},
methods: {
$nextTick () {
}
}
}
```

## :wrench: Options

This rule has an object option:

`"reserved"`: [] (default) array of dissalowed names inside `groups`.

`"groups"`: [] (default) array of additional groups to search for duplicates.

### Example:

```
vue/no-reserved-keys: [2, {
reserved: ['foo', 'foo2'],
groups: ['asyncComputed']
}]
```

:-1: Examples of **incorrect** code for this configuration

```js
export default {
asyncComputed: {
foo2 () {}
},
computed: {
foo () {}
}
}
```
74 changes: 74 additions & 0 deletions lib/rules/no-reserved-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* @fileoverview Prevent overwrite reserved keys
* @author Armano
*/
'use strict'

const utils = require('../utils')

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

const RESERVED_KEYS = require('../utils/vue-reserved.json')
const GROUP_NAMES = ['props', 'computed', 'data', 'methods']

function create (context) {
const options = context.options[0] || {}
const reservedKeys = new Set(RESERVED_KEYS.concat(options.reserved || []))
const groups = new Set(GROUP_NAMES.concat(options.groups || []))

// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------

return utils.executeOnVue(context, (obj) => {
const properties = utils.iterateProperties(obj, groups)
for (const o of properties) {
if (o.groupName === 'data' && o.name[0] === '_') {
context.report({
node: o.node,
message: "Keys starting with with '_' are reserved in '{{name}}' group.",
data: {
name: o.name
}
})
} else if (reservedKeys.has(o.name)) {
context.report({
node: o.node,
message: "Key '{{name}}' is reserved.",
data: {
name: o.name
}
})
}
}
})
}

module.exports = {
meta: {
docs: {
description: 'Prevent overwrite reserved keys.',
category: 'Possible Errors',
recommended: false
},
fixable: null,
schema: [
{
type: 'object',
properties: {
reserved: {
type: 'array'
},
groups: {
type: 'array'
}
},
additionalProperties: false
}
]
},

create
}
6 changes: 4 additions & 2 deletions lib/rules/no-reservered-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ module.exports = {
docs: {
description: 'Prevent overwrite reserved keys.',
category: 'Possible Errors',
recommended: false
recommended: false,
replacedBy: ['no-reserved-keys']
},
fixable: null, // or "code" or "whitespace"
deprecated: true,
fixable: null,
schema: [
{
type: 'object',
Expand Down
100 changes: 100 additions & 0 deletions tests/lib/rules/no-reserved-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* @fileoverview Prevent overwrite reserved keys
* @author Armano
*/
'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const rule = require('../../../lib/rules/no-reserved-keys')
const RuleTester = require('eslint').RuleTester

const parserOptions = {
ecmaVersion: 7,
sourceType: 'module',
ecmaFeatures: { experimentalObjectRestSpread: true }
}

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------

const ruleTester = new RuleTester()
ruleTester.run('no-reserved-keys', rule, {
valid: [
{
filename: 'test.vue',
code: `
export default {
props: ['foo'],
computed: {
bar () {
}
},
data () {
return {
dat: null
}
},
methods: {
_foo () {},
test () {
}
}
}
`,
parserOptions
}
],

invalid: [
{
filename: 'test.js',
code: `
new Vue({
props: {
$el: String
}
})
`,
parserOptions: { ecmaVersion: 6 },
errors: [{
message: "Key '$el' is reserved.",
line: 4
}]
},
{
filename: 'test.js',
code: `
new Vue({
data: {
_foo: String
}
})
`,
parserOptions: { ecmaVersion: 6 },
errors: [{
message: "Keys starting with with '_' are reserved in '_foo' group.",
line: 4
}]
},
{
filename: 'test.js',
code: `
new Vue({
foo: {
bar: String
}
})
`,
options: [{ reserved: ['bar'], groups: ['foo'] }],
parserOptions: { ecmaVersion: 6 },
errors: [{
message: "Key 'bar' is reserved.",
line: 4
}]
}
]
})

0 comments on commit 23d360c

Please sign in to comment.