Skip to content
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

feat: add no-deprecated-delete-set rule #2540

Merged
merged 6 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Rules in this category are enabled for all presets provided by eslint-plugin-vue
| [vue/no-computed-properties-in-data](./no-computed-properties-in-data.md) | disallow accessing computed properties in `data` | | :three::two::warning: |
| [vue/no-custom-modifiers-on-v-model](./no-custom-modifiers-on-v-model.md) | disallow custom modifiers on v-model used on the component | | :two::warning: |
| [vue/no-deprecated-data-object-declaration](./no-deprecated-data-object-declaration.md) | disallow using deprecated object declaration on data (in Vue.js 3.0.0+) | :wrench: | :three::warning: |
| [vue/no-deprecated-delete-set](./no-deprecated-delete-set.md) | disallow using deprecated `$delete` and `$set` (in Vue.js 3.0.0+) | | :three::warning: |
| [vue/no-deprecated-destroyed-lifecycle](./no-deprecated-destroyed-lifecycle.md) | disallow using deprecated `destroyed` and `beforeDestroy` lifecycle hooks (in Vue.js 3.0.0+) | :wrench: | :three::warning: |
| [vue/no-deprecated-dollar-listeners-api](./no-deprecated-dollar-listeners-api.md) | disallow using deprecated `$listeners` (in Vue.js 3.0.0+) | | :three::warning: |
| [vue/no-deprecated-dollar-scopedslots-api](./no-deprecated-dollar-scopedslots-api.md) | disallow using deprecated `$scopedSlots` (in Vue.js 3.0.0+) | :wrench: | :three::warning: |
Expand Down
49 changes: 49 additions & 0 deletions docs/rules/no-deprecated-delete-set.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/no-deprecated-delete-set
description: disallow using deprecated `$delete` and `$set` (in Vue.js 3.0.0+)
---

# vue/no-deprecated-delete-set

> disallow using deprecated `$delete` and `$set` (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>
- :gear: This rule is included in all of `"plugin:vue/vue3-essential"`, `*.configs["flat/essential"]`, `"plugin:vue/vue3-strongly-recommended"`, `*.configs["flat/strongly-recommended"]`, `"plugin:vue/vue3-recommended"` and `*.configs["flat/recommended"]`.

## :book: Rule Details

This rule reports use of deprecated `$delete` and `$set`. (in Vue.js 3.0.0+).

<eslint-code-block :rules="{'vue/no-deprecated-delete-set': ['error']}">

```vue
<script>
import { set as st, delete as del } from 'vue'
waynzh marked this conversation as resolved.
Show resolved Hide resolved
export default {
mounted () {
/* ✗ BAD */
this.$set(obj, key, value)
this.$delete(obj, key)

Vue.set(obj, key, value)
Vue.delete(obj, key)

st(obj, key, value)
waynzh marked this conversation as resolved.
Show resolved Hide resolved
del(obj, key)
}
}
</script>
```

</eslint-code-block>

## :wrench: Options

Nothing.

waynzh marked this conversation as resolved.
Show resolved Hide resolved
## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-deprecated-delete-set.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-deprecated-delete-set.js)
1 change: 1 addition & 0 deletions lib/configs/flat/vue3-essential.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = [
'vue/no-child-content': 'error',
'vue/no-computed-properties-in-data': 'error',
'vue/no-deprecated-data-object-declaration': 'error',
'vue/no-deprecated-delete-set': 'error',
'vue/no-deprecated-destroyed-lifecycle': 'error',
'vue/no-deprecated-dollar-listeners-api': 'error',
'vue/no-deprecated-dollar-scopedslots-api': 'error',
Expand Down
1 change: 1 addition & 0 deletions lib/configs/vue3-essential.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
'vue/no-child-content': 'error',
'vue/no-computed-properties-in-data': 'error',
'vue/no-deprecated-data-object-declaration': 'error',
'vue/no-deprecated-delete-set': 'error',
'vue/no-deprecated-destroyed-lifecycle': 'error',
'vue/no-deprecated-dollar-listeners-api': 'error',
'vue/no-deprecated-dollar-scopedslots-api': 'error',
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
'no-constant-condition': require('./rules/no-constant-condition'),
'no-custom-modifiers-on-v-model': require('./rules/no-custom-modifiers-on-v-model'),
'no-deprecated-data-object-declaration': require('./rules/no-deprecated-data-object-declaration'),
'no-deprecated-delete-set': require('./rules/no-deprecated-delete-set'),
'no-deprecated-destroyed-lifecycle': require('./rules/no-deprecated-destroyed-lifecycle'),
'no-deprecated-dollar-listeners-api': require('./rules/no-deprecated-dollar-listeners-api'),
'no-deprecated-dollar-scopedslots-api': require('./rules/no-deprecated-dollar-scopedslots-api'),
Expand Down Expand Up @@ -279,7 +280,7 @@
vue: require('./processor')
},
environments: {
// TODO Remove in the next major version

Check warning on line 283 in lib/index.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected 'todo' comment: 'TODO Remove in the next major version'
/** @deprecated */
'setup-compiler-macros': {
globals: {
Expand Down
141 changes: 141 additions & 0 deletions lib/rules/no-deprecated-delete-set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/**
* @author Wayne Zhang
* See LICENSE file in root directory for full license.
*/
'use strict'

const utils = require('../utils')
const { findVariable } = require('@eslint-community/eslint-utils')

const DeprecatedApis = ['set', 'delete']
const DeprecatedDollarApis = new Set(DeprecatedApis.map((item) => `$${item}`))
waynzh marked this conversation as resolved.
Show resolved Hide resolved

/**
* @param {Expression|Super} node
*/
function isVue(node) {
return node.type === 'Identifier' && node.name === 'Vue'
}

module.exports = {
meta: {
type: 'problem',
docs: {
description:
'disallow using deprecated `$delete` and `$set` (in Vue.js 3.0.0+)',
categories: ['vue3-essential'],
waynzh marked this conversation as resolved.
Show resolved Hide resolved
url: 'https://eslint.vuejs.org/rules/no-deprecated-delete-set.html'
},
fixable: null,
schema: [],
messages: {
deprecated: 'The `$delete`, `$set` is deprecated.'
}
},
/** @param {RuleContext} context */
create(context) {
/**
* @param {Identifier} identifier
* @param {RuleContext} context
* @returns {CallExpression|undefined}
*/
function getVueDeprecatedCallExpression(identifier, context) {
// Instance API: this.$set()
if (
DeprecatedDollarApis.has(identifier.name) &&
identifier.parent.type === 'MemberExpression' &&
utils.isThis(identifier.parent.object, context) &&
identifier.parent.parent.type === 'CallExpression' &&
identifier.parent.parent.callee === identifier.parent
) {
return identifier.parent.parent
}

// Vue 2 Global API: Vue.set()
if (
DeprecatedApis.includes(identifier.name) &&
identifier.parent.type === 'MemberExpression' &&
isVue(identifier.parent.object) &&
identifier.parent.parent.type === 'CallExpression' &&
identifier.parent.parent.callee === identifier.parent
) {
return identifier.parent.parent
}

// Vue 3 Global API
if (
identifier.parent.type === 'CallExpression' &&
identifier.parent.callee === identifier
) {
const variable = findVariable(
waynzh marked this conversation as resolved.
Show resolved Hide resolved
utils.getScope(context, identifier),
identifier
)

if (variable != null && variable.defs.length === 1) {
const def = variable.defs[0]

// import { set as st } from 'vue'; st()
if (
def.type === 'ImportBinding' &&
def.node.type === 'ImportSpecifier' &&
def.node.imported.type === 'Identifier' &&
DeprecatedApis.includes(def.node.imported.name) &&
def.node.parent.type === 'ImportDeclaration' &&
def.node.parent.source.value === 'vue'
) {
return identifier.parent
}

// const { set, delete } = require('vue'); set()
if (
def.type === 'Variable' &&
def.node.type === 'VariableDeclarator' &&
def.node.id.type === 'ObjectPattern' &&
def.node.init?.type === 'CallExpression' &&
def.node.init.callee.type === 'Identifier' &&
def.node.init.callee.name === 'require' &&
def.node.init.arguments.length === 1 &&
def.node.init.arguments[0].type === 'Literal' &&
def.node.init.arguments[0].value === 'vue'
) {
const properties = def.node.id.properties
for (const prop of properties) {
if (
prop.type === 'Property' &&
prop.key.type === 'Identifier' &&
DeprecatedApis.includes(prop.key.name) &&
prop.value.type === 'Identifier' &&
prop.value.name === identifier.name
) {
return identifier.parent
}
}
}
}
}

return undefined
}

const nodeVisitor = {
/** @param {Identifier} node */
Identifier(node) {
const callExpression = getVueDeprecatedCallExpression(node, context)
if (!callExpression) {
return
}

context.report({
node,
messageId: 'deprecated'
})
}
}

return utils.compositingVisitors(
utils.defineVueVisitor(context, nodeVisitor),
utils.defineScriptSetupVisitor(context, nodeVisitor)
)
}
}
Loading
Loading