|
| 1 | +/** |
| 2 | + * @author Wayne Zhang |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const utils = require('../utils') |
| 8 | +const { ReferenceTracker } = require('@eslint-community/eslint-utils') |
| 9 | + |
| 10 | +/** |
| 11 | + * @typedef {import('@eslint-community/eslint-utils').TYPES.TraceMap} TraceMap |
| 12 | + */ |
| 13 | + |
| 14 | +/** @type {TraceMap} */ |
| 15 | +const deletedImportApisMap = { |
| 16 | + set: { |
| 17 | + [ReferenceTracker.CALL]: true |
| 18 | + }, |
| 19 | + del: { |
| 20 | + [ReferenceTracker.CALL]: true |
| 21 | + } |
| 22 | +} |
| 23 | +const deprecatedApis = new Set(['set', 'delete']) |
| 24 | +const deprecatedDollarApis = new Set(['$set', '$delete']) |
| 25 | + |
| 26 | +/** |
| 27 | + * @param {Expression|Super} node |
| 28 | + */ |
| 29 | +function isVue(node) { |
| 30 | + return node.type === 'Identifier' && node.name === 'Vue' |
| 31 | +} |
| 32 | + |
| 33 | +module.exports = { |
| 34 | + meta: { |
| 35 | + type: 'problem', |
| 36 | + docs: { |
| 37 | + description: |
| 38 | + 'disallow using deprecated `$delete` and `$set` (in Vue.js 3.0.0+)', |
| 39 | + categories: undefined, |
| 40 | + url: 'https://eslint.vuejs.org/rules/no-deprecated-delete-set.html' |
| 41 | + }, |
| 42 | + fixable: null, |
| 43 | + schema: [], |
| 44 | + messages: { |
| 45 | + deprecated: 'The `$delete`, `$set` is deprecated.' |
| 46 | + } |
| 47 | + }, |
| 48 | + /** @param {RuleContext} context */ |
| 49 | + create(context) { |
| 50 | + /** |
| 51 | + * @param {Identifier} identifier |
| 52 | + * @param {RuleContext} context |
| 53 | + * @returns {CallExpression|undefined} |
| 54 | + */ |
| 55 | + function getVueDeprecatedCallExpression(identifier, context) { |
| 56 | + // Instance API: this.$set() |
| 57 | + if ( |
| 58 | + deprecatedDollarApis.has(identifier.name) && |
| 59 | + identifier.parent.type === 'MemberExpression' && |
| 60 | + utils.isThis(identifier.parent.object, context) && |
| 61 | + identifier.parent.parent.type === 'CallExpression' && |
| 62 | + identifier.parent.parent.callee === identifier.parent |
| 63 | + ) { |
| 64 | + return identifier.parent.parent |
| 65 | + } |
| 66 | + |
| 67 | + // Vue 2 Global API: Vue.set() |
| 68 | + if ( |
| 69 | + deprecatedApis.has(identifier.name) && |
| 70 | + identifier.parent.type === 'MemberExpression' && |
| 71 | + isVue(identifier.parent.object) && |
| 72 | + identifier.parent.parent.type === 'CallExpression' && |
| 73 | + identifier.parent.parent.callee === identifier.parent |
| 74 | + ) { |
| 75 | + return identifier.parent.parent |
| 76 | + } |
| 77 | + |
| 78 | + return undefined |
| 79 | + } |
| 80 | + |
| 81 | + const nodeVisitor = { |
| 82 | + /** @param {Identifier} node */ |
| 83 | + Identifier(node) { |
| 84 | + const callExpression = getVueDeprecatedCallExpression(node, context) |
| 85 | + if (!callExpression) { |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + context.report({ |
| 90 | + node, |
| 91 | + messageId: 'deprecated' |
| 92 | + }) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return utils.compositingVisitors( |
| 97 | + utils.defineVueVisitor(context, nodeVisitor), |
| 98 | + utils.defineScriptSetupVisitor(context, nodeVisitor), |
| 99 | + { |
| 100 | + /** @param {Program} node */ |
| 101 | + Program(node) { |
| 102 | + const tracker = new ReferenceTracker(utils.getScope(context, node)) |
| 103 | + |
| 104 | + // import { set } from 'vue'; set() |
| 105 | + const esmTraceMap = { |
| 106 | + vue: { |
| 107 | + [ReferenceTracker.ESM]: true, |
| 108 | + ...deletedImportApisMap |
| 109 | + } |
| 110 | + } |
| 111 | + // const { set } = require('vue'); set() |
| 112 | + const cjsTraceMap = { |
| 113 | + vue: { |
| 114 | + ...deletedImportApisMap |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + for (const { node } of [ |
| 119 | + ...tracker.iterateEsmReferences(esmTraceMap), |
| 120 | + ...tracker.iterateCjsReferences(cjsTraceMap) |
| 121 | + ]) { |
| 122 | + const refNode = /** @type {CallExpression} */ (node) |
| 123 | + context.report({ |
| 124 | + node: refNode.callee, |
| 125 | + messageId: 'deprecated' |
| 126 | + }) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + ) |
| 131 | + } |
| 132 | +} |
0 commit comments