-
-
Notifications
You must be signed in to change notification settings - Fork 669
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: Add
vue/no-invalid-model-keys
rule (#1466)
* added no-invalid-model-keys rule * added no-invalid-model-keys to rules list * added no-invalid-model-keys tests * removed unused * added docs for no-invalid-model-keys * Update docs/rules/no-invalid-model-keys.md Co-authored-by: Yosuke Ota <[email protected]> * Update docs/rules/no-invalid-model-keys.md Co-authored-by: Yosuke Ota <[email protected]> * Update docs/rules/no-invalid-model-keys.md Co-authored-by: Yosuke Ota <[email protected]> * Update docs/rules/no-invalid-model-keys.md Co-authored-by: Yosuke Ota <[email protected]> * Update lib/rules/no-invalid-model-keys.js Co-authored-by: Yosuke Ota <[email protected]> * Update lib/rules/no-invalid-model-keys.js Co-authored-by: Yosuke Ota <[email protected]> * Update docs/rules/no-invalid-model-keys.md Co-authored-by: Yosuke Ota <[email protected]> * Update no-invalid-model-keys.md * Update no-invalid-model-keys.js * Update no-invalid-model-keys.js * Update no-invalid-model-keys.js * Update no-invalid-model-keys.js Co-authored-by: Alex Sokolov <[email protected]> Co-authored-by: Yosuke Ota <[email protected]>
- Loading branch information
1 parent
26d9920
commit 084bfe3
Showing
4 changed files
with
311 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,103 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-invalid-model-keys | ||
description: require valid keys in model option | ||
--- | ||
# vue/no-invalid-model-keys | ||
|
||
> require valid keys in model option | ||
|
||
## :book: Rule Details | ||
|
||
This rule is aimed at preventing invalid keys in model option. | ||
|
||
<eslint-code-block :rules="{'vue/no-invalid-model-keys': ['error']}"> | ||
```vue | ||
<script> | ||
/* ✓ GOOD */ | ||
export default { | ||
model: { | ||
prop: 'list', | ||
} | ||
} | ||
</script> | ||
``` | ||
</eslint-code-block> | ||
|
||
<eslint-code-block :rules="{'vue/no-invalid-model-keys': ['error']}"> | ||
```vue | ||
<script> | ||
/* ✓ GOOD */ | ||
export default { | ||
model: { | ||
event: 'update' | ||
} | ||
} | ||
</script> | ||
``` | ||
</eslint-code-block> | ||
|
||
<eslint-code-block :rules="{'vue/no-invalid-model-keys': ['error']}"> | ||
```vue | ||
<script> | ||
/* ✓ GOOD */ | ||
export default { | ||
model: { | ||
prop: 'list', | ||
event: 'update' | ||
} | ||
} | ||
</script> | ||
``` | ||
</eslint-code-block> | ||
|
||
<eslint-code-block :rules="{'vue/no-invalid-model-keys': ['error']}"> | ||
```vue | ||
<script> | ||
/* ✗ BAD */ | ||
export default { | ||
model: { | ||
prop: 'list', | ||
events: 'update' | ||
} | ||
} | ||
</script> | ||
``` | ||
</eslint-code-block> | ||
|
||
<eslint-code-block :rules="{'vue/no-invalid-model-keys': ['error']}"> | ||
```vue | ||
<script> | ||
/* ✗ BAD */ | ||
export default { | ||
model: { | ||
props: 'list', | ||
events: 'update' | ||
} | ||
} | ||
</script> | ||
``` | ||
</eslint-code-block> | ||
|
||
<eslint-code-block :rules="{'vue/no-invalid-model-keys': ['error']}"> | ||
```vue | ||
<script> | ||
/* ✗ BAD */ | ||
export default { | ||
model: { | ||
name: 'checked', | ||
props: 'list', | ||
event: 'update' | ||
} | ||
} | ||
</script> | ||
``` | ||
</eslint-code-block> | ||
|
||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-invalid-model-keys.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-invalid-model-keys.js) |
This file contains 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
This file contains 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,56 @@ | ||
/** | ||
* @fileoverview Requires valid keys in model option. | ||
* @author Alex Sokolov | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
/** | ||
* @typedef {import('../utils').GroupName} GroupName | ||
*/ | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
/** @type {GroupName[]} */ | ||
const GROUP_NAMES = ['model'] | ||
|
||
const VALID_MODEL_KEYS = ['prop', 'event'] | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'require valid keys in model option', | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/no-invalid-model-keys.html' | ||
}, | ||
fixable: null, | ||
schema: [] | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
const groups = new Set(GROUP_NAMES) | ||
|
||
// ---------------------------------------------------------------------- | ||
// Public | ||
// ---------------------------------------------------------------------- | ||
|
||
return utils.executeOnVue(context, (obj) => { | ||
const properties = utils.iterateProperties(obj, groups) | ||
|
||
for (const o of properties) { | ||
if (VALID_MODEL_KEYS.indexOf(o.name) === -1) { | ||
context.report({ | ||
node: o.node, | ||
message: "Invalid key '{{name}}' in model option.", | ||
data: { | ||
name: o.name | ||
} | ||
}) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains 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,151 @@ | ||
/** | ||
* @fileoverview Prevents invalid keys in model option. | ||
* @author Alex Sokolov | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const rule = require('../../../lib/rules/no-invalid-model-keys') | ||
const RuleTester = require('eslint').RuleTester | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
sourceType: 'module' | ||
} | ||
}) | ||
ruleTester.run('no-invalid-model-keys', rule, { | ||
valid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
model: { | ||
prop: 'list' | ||
} | ||
} | ||
` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
model: { | ||
event: 'update' | ||
} | ||
} | ||
` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
model: { | ||
prop: 'list', | ||
event: 'update' | ||
} | ||
} | ||
` | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
model: { | ||
props: 'list' | ||
} | ||
} | ||
`, | ||
errors: ["Invalid key 'props' in model option."] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
model: { | ||
events: 'update' | ||
} | ||
} | ||
`, | ||
errors: ["Invalid key 'events' in model option."] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
model: { | ||
props: 'list', | ||
event: 'update' | ||
} | ||
} | ||
`, | ||
errors: ["Invalid key 'props' in model option."] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
model: { | ||
prop: 'list', | ||
events: 'update' | ||
} | ||
} | ||
`, | ||
errors: ["Invalid key 'events' in model option."] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
model: { | ||
props: 'list', | ||
events: 'update' | ||
} | ||
} | ||
`, | ||
errors: [ | ||
"Invalid key 'props' in model option.", | ||
"Invalid key 'events' in model option." | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
model: { | ||
prop: 'checked', | ||
props: 'list', | ||
event: 'update' | ||
} | ||
} | ||
`, | ||
errors: ["Invalid key 'props' in model option."] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
model: { | ||
name: 'checked', | ||
props: 'list', | ||
event: 'update' | ||
} | ||
} | ||
`, | ||
errors: [ | ||
"Invalid key 'name' in model option.", | ||
"Invalid key 'props' in model option." | ||
] | ||
} | ||
] | ||
}) |