-
-
Notifications
You must be signed in to change notification settings - Fork 681
feat: add no-deprecated-delete-set rule #2540
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
ota-meshi
merged 6 commits into
vuejs:master
from
waynzh:feature/no-deprecated-delete-set
Sep 12, 2024
+565
−0
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d1479f2
feat: add no-deprecated-delete-set rule
waynzh 72e3c31
feat: address comments
waynzh 4a4ad4b
feat: update category
waynzh ce2d8d5
feat: update set
waynzh 85043f3
feat: use ReferenceTracker
waynzh 9bbf8b8
refactor
waynzh 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
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,52 @@ | ||
--- | ||
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> | ||
|
||
## :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, del } from 'vue' | ||
export default { | ||
mounted () { | ||
/* ✗ BAD */ | ||
this.$set(obj, key, value) | ||
this.$delete(obj, key) | ||
|
||
Vue.set(obj, key, value) | ||
Vue.delete(obj, key) | ||
|
||
set(obj, key, value) | ||
del(obj, key) | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :books: Further Reading | ||
|
||
- [Migration Guide - Removed APIs](https://v3-migration.vuejs.org/breaking-changes/#removed-apis) | ||
|
||
## :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) |
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
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,132 @@ | ||
/** | ||
* @author Wayne Zhang | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
const { ReferenceTracker } = require('@eslint-community/eslint-utils') | ||
|
||
/** | ||
* @typedef {import('@eslint-community/eslint-utils').TYPES.TraceMap} TraceMap | ||
*/ | ||
|
||
/** @type {TraceMap} */ | ||
const deletedImportApisMap = { | ||
set: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
del: { | ||
[ReferenceTracker.CALL]: true | ||
} | ||
} | ||
const deprecatedApis = new Set(['set', 'delete']) | ||
const deprecatedDollarApis = new Set(['$set', '$delete']) | ||
|
||
/** | ||
* @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: undefined, | ||
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.has(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 | ||
} | ||
|
||
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), | ||
{ | ||
/** @param {Program} node */ | ||
Program(node) { | ||
const tracker = new ReferenceTracker(utils.getScope(context, node)) | ||
|
||
// import { set } from 'vue'; set() | ||
const esmTraceMap = { | ||
vue: { | ||
[ReferenceTracker.ESM]: true, | ||
...deletedImportApisMap | ||
} | ||
} | ||
// const { set } = require('vue'); set() | ||
const cjsTraceMap = { | ||
vue: { | ||
...deletedImportApisMap | ||
} | ||
} | ||
|
||
for (const { node } of [ | ||
...tracker.iterateEsmReferences(esmTraceMap), | ||
...tracker.iterateCjsReferences(cjsTraceMap) | ||
]) { | ||
const refNode = /** @type {CallExpression} */ (node) | ||
context.report({ | ||
node: refNode.callee, | ||
messageId: 'deprecated' | ||
}) | ||
} | ||
} | ||
} | ||
) | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.