-
-
Notifications
You must be signed in to change notification settings - Fork 681
Add rules: no-dupe-keys
and no-reserved-keys
#88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
michalsnik
merged 1 commit into
vuejs:master
from
armano2:patch-12-no-duplicate-field-names
Aug 1, 2017
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Prevents duplication of field names (no-dupe-keys) | ||
|
||
This rule prevents to use duplicated names. | ||
|
||
## :book: Rule Details | ||
|
||
This rule is aimed at preventing duplicated property names. | ||
|
||
:-1: Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
export default { | ||
props: { | ||
foo: String | ||
}, | ||
computed: { | ||
foo: { | ||
get () { | ||
} | ||
} | ||
}, | ||
data: { | ||
foo: null | ||
}, | ||
methods: { | ||
foo () { | ||
} | ||
} | ||
} | ||
``` | ||
|
||
:+1: Examples of **correct** code for this rule: | ||
|
||
```js | ||
export default { | ||
props: ['foo'], | ||
computed: { | ||
bar () { | ||
} | ||
}, | ||
data () { | ||
return { | ||
dat: null | ||
} | ||
}, | ||
methods: { | ||
test () { | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## :wrench: Options | ||
|
||
This rule has an object option: | ||
|
||
`"groups"`: [] (default) array of additional groups to search for duplicates. | ||
|
||
### Example: | ||
|
||
``` | ||
vue/no-dupe-keys: [2, { | ||
groups: ['asyncComputed'] | ||
}] | ||
``` | ||
|
||
:-1: Examples of **incorrect** code for this configuration | ||
|
||
```js | ||
export default { | ||
computed: { | ||
foo () {} | ||
}, | ||
asyncComputed: { | ||
foo () {} | ||
} | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Prevent overwrite reserved keys (no-reservered-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-dupe-keys: [2, { | ||
reserved: ['foo'] | ||
}] | ||
``` | ||
|
||
:-1: Examples of **incorrect** code for this configuration | ||
|
||
```js | ||
export default { | ||
computed: { | ||
foo () {} | ||
} | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* @fileoverview Prevents duplication of field names. | ||
* @author Armano | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
const GROUP_NAMES = ['props', 'computed', 'data', 'methods'] | ||
|
||
function create (context) { | ||
const usedNames = [] | ||
|
||
const options = context.options[0] || {} | ||
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 (usedNames.indexOf(o.name) !== -1) { | ||
context.report({ | ||
node: o.node, | ||
message: "Duplicated key '{{name}}'.", | ||
data: { | ||
name: o.name | ||
} | ||
}) | ||
} | ||
usedNames.push(o.name) | ||
} | ||
}) | ||
} | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Prevents duplication of field names.', | ||
category: 'Possible Errors', | ||
recommended: false | ||
}, | ||
fixable: null, // or "code" or "whitespace" | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
groups: { | ||
type: 'array' | ||
} | ||
}, | ||
additionalProperties: false | ||
} | ||
] | ||
}, | ||
|
||
create | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, // or "code" or "whitespace" | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
reserved: { | ||
type: 'array' | ||
}, | ||
groups: { | ||
type: 'array' | ||
} | ||
}, | ||
additionalProperties: false | ||
} | ||
] | ||
}, | ||
|
||
create | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add test for this method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michalsnik added