-
-
Notifications
You must be signed in to change notification settings - Fork 664
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
vue/no-deprecated-model-definition
rule (#2238)
- Loading branch information
1 parent
52a9966
commit eddf098
Showing
5 changed files
with
395 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
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,76 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-deprecated-model-definition | ||
description: disallow deprecated `model` definition (in Vue.js 3.0.0+) | ||
--- | ||
# vue/no-deprecated-model-definition | ||
|
||
> disallow deprecated `model` definition (in Vue.js 3.0.0+) | ||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> | ||
- :bulb: Some problems reported by this rule are manually fixable by editor [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions). | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports use of the component `model` option, which has been deprecated in Vue.js 3.0.0+. | ||
|
||
See [Migration Guide – `v-model`](https://v3-migration.vuejs.org/breaking-changes/v-model.html) for more details. | ||
|
||
<eslint-code-block :rules="{'vue/no-deprecated-model-definition': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
export default defineComponent({ | ||
model: { | ||
prop: 'my-value', | ||
event: 'input' | ||
} | ||
}) | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
```json | ||
{ | ||
"vue/no-deprecated-model-definition": ["error", { | ||
"allowVue3Compat": true | ||
}] | ||
} | ||
``` | ||
|
||
### `"allowVue3Compat": true` | ||
|
||
Allow `model` definitions with prop/event names that match the Vue.js 3.0.0+ `v-model` syntax, e.g. `fooBar`/`update:fooBar`. | ||
|
||
<eslint-code-block :rules="{'vue/no-deprecated-model-definition': ['error', { allowVue3Compat: true }]}"> | ||
|
||
```vue | ||
<script> | ||
export default defineComponent({ | ||
model: { | ||
prop: 'fooBar', | ||
event: 'update:fooBar' | ||
} | ||
}) | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :couple: Related Rules | ||
|
||
- [vue/valid-model-definition](./valid-model-definition.md) (for Vue.js 2.x) | ||
- [vue/no-v-model-argument](./no-v-model-argument.md) (for Vue.js 2.x) | ||
|
||
## :books: Further Reading | ||
|
||
- [Migration Guide – `v-model`](https://v3-migration.vuejs.org/breaking-changes/v-model.html) | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-deprecated-model-definition.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-deprecated-model-definition.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,112 @@ | ||
/** | ||
* @author Flo Edelmann | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
/** | ||
* @param {RuleContext} context | ||
* @param {ASTNode} node | ||
*/ | ||
function reportWithoutSuggestion(context, node) { | ||
context.report({ | ||
node, | ||
messageId: 'deprecatedModel' | ||
}) | ||
} | ||
|
||
/** | ||
* @param {ObjectExpression} node | ||
* @param {string} key | ||
* @returns {Literal | undefined} | ||
*/ | ||
function findPropertyValue(node, key) { | ||
const property = node.properties.find( | ||
(property) => | ||
property.type === 'Property' && | ||
property.key.type === 'Identifier' && | ||
property.key.name === key | ||
) | ||
if ( | ||
!property || | ||
property.type !== 'Property' || | ||
property.value.type !== 'Literal' | ||
) { | ||
return undefined | ||
} | ||
return property.value | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'disallow deprecated `model` definition (in Vue.js 3.0.0+)', | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/no-deprecated-model-definition.html' | ||
}, | ||
fixable: null, | ||
hasSuggestions: true, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: { | ||
allowVue3Compat: { | ||
type: 'boolean' | ||
} | ||
} | ||
} | ||
], | ||
messages: { | ||
deprecatedModel: '`model` definition is deprecated.', | ||
renameEvent: 'Rename event to `{{expectedEventName}}`.' | ||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
const allowVue3Compat = Boolean(context.options[0]?.allowVue3Compat) | ||
|
||
return utils.executeOnVue(context, (obj) => { | ||
const modelProperty = utils.findProperty(obj, 'model') | ||
if (!modelProperty || modelProperty.value.type !== 'ObjectExpression') { | ||
return | ||
} | ||
|
||
if (!allowVue3Compat) { | ||
reportWithoutSuggestion(context, modelProperty) | ||
return | ||
} | ||
|
||
const propName = findPropertyValue(modelProperty.value, 'prop') | ||
const eventName = findPropertyValue(modelProperty.value, 'event') | ||
|
||
if (!propName || !eventName) { | ||
reportWithoutSuggestion(context, modelProperty) | ||
return | ||
} | ||
|
||
const expectedEventName = `update:${propName.value}` | ||
if (eventName.value !== expectedEventName) { | ||
context.report({ | ||
node: modelProperty, | ||
messageId: 'deprecatedModel', | ||
suggest: [ | ||
{ | ||
messageId: 'renameEvent', | ||
data: { expectedEventName }, | ||
fix(fixer) { | ||
return fixer.replaceTextRange( | ||
[eventName.range[0] + 1, eventName.range[1] - 1], | ||
expectedEventName | ||
) | ||
} | ||
} | ||
] | ||
}) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.