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-deprecated-events-api
rule (#1097)
Co-authored-by: Yosuke Ota <otameshiyo23@gmail.com>
- Loading branch information
1 parent
cadec3b
commit 35efedc
Showing
6 changed files
with
278 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,57 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-deprecated-events-api | ||
description: disallow using deprecated events api | ||
--- | ||
# vue/no-deprecated-events-api | ||
> disallow using deprecated events api | ||
- :gear: This rule is included in all of `"plugin:vue/vue3-essential"`, `"plugin:vue/vue3-strongly-recommended"` and `"plugin:vue/vue3-recommended"`. | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports use of deprecated `$on`, `$off` `$once` api. (in Vue.js 3.0.0+). | ||
|
||
<eslint-code-block :rules="{'vue/no-deprecated-events-api': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
/* ✗ BAD */ | ||
export default { | ||
mounted () { | ||
this.$on('start', function(args) { | ||
console.log('start') | ||
}) | ||
this.$emit('start') | ||
} | ||
} | ||
/* ✓ GOOD */ | ||
import mitt from 'mitt' | ||
const emitter = mitt() | ||
export default { | ||
mounted () { | ||
emitter.on('start', function(args) { | ||
console.log('start') | ||
}) | ||
emitter.emit('start') | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :books: Further reading | ||
|
||
- [RFC: events api change](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0020-events-api-change.md) | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-deprecated-events-api.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-deprecated-events-api.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
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,57 @@ | ||
/** | ||
* @fileoverview disallow using deprecated events api | ||
* @author yoyo930021 | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'disallow using deprecated events api', | ||
categories: ['vue3-essential'], | ||
url: 'https://eslint.vuejs.org/rules/no-deprecated-events-api.html' | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
noDeprecatedEventsApi: 'The Events api `$on`, `$off` `$once` is deprecated. Using external library instead, for example mitt.' | ||
} | ||
}, | ||
|
||
create (context) { | ||
const forbiddenNodes = [] | ||
|
||
return Object.assign( | ||
{ | ||
'CallExpression > MemberExpression > ThisExpression' (node) { | ||
if (!['$on', '$off', '$once'].includes(node.parent.property.name)) return | ||
forbiddenNodes.push(node.parent.parent) | ||
} | ||
}, | ||
utils.executeOnVue(context, (obj) => { | ||
forbiddenNodes.forEach(node => { | ||
if ( | ||
node.loc.start.line >= obj.loc.start.line && | ||
node.loc.end.line <= obj.loc.end.line | ||
) { | ||
context.report({ | ||
node, | ||
messageId: 'noDeprecatedEventsApi' | ||
}) | ||
} | ||
}) | ||
}) | ||
) | ||
} | ||
} |
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,161 @@ | ||
/* eslint-disable eslint-plugin/consistent-output */ | ||
/** | ||
* @fileoverview disallow using deprecated events api | ||
* @author yoyo930021 | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const rule = require('../../../lib/rules/no-deprecated-events-api') | ||
|
||
const RuleTester = require('eslint').RuleTester | ||
|
||
const parserOptions = { | ||
ecmaVersion: 2018, | ||
sourceType: 'module' | ||
} | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester() | ||
ruleTester.run('no-deprecated-events-api', rule, { | ||
|
||
valid: [ | ||
{ | ||
filename: 'test.js', | ||
code: ` | ||
createApp({ | ||
mounted () { | ||
this.$emit('start') | ||
} | ||
}) | ||
`, | ||
parserOptions | ||
}, | ||
{ | ||
filename: 'test.js', | ||
code: ` | ||
createApp({ | ||
methods: { | ||
click () { | ||
this.$emit('click') | ||
} | ||
} | ||
}) | ||
`, | ||
parserOptions | ||
}, | ||
{ | ||
filename: 'test.js', | ||
code: ` | ||
const another = function () { | ||
this.$on('start', args => { | ||
console.log('start') | ||
}) | ||
} | ||
createApp({ | ||
mounted () { | ||
this.$emit('start') | ||
} | ||
}) | ||
`, | ||
parserOptions | ||
}, | ||
{ | ||
filename: 'test.js', | ||
code: ` | ||
app.component('some-comp', { | ||
mounted () { | ||
this.$emit('start') | ||
} | ||
}) | ||
`, | ||
parserOptions | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
mounted () { | ||
this.$emit('start') | ||
} | ||
} | ||
`, | ||
parserOptions | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
import mitt from 'mitt' | ||
const emitter = mitt() | ||
export default { | ||
setup () { | ||
emitter.on('foo', e => console.log('foo', e)) | ||
emitter.emit('foo', { a: 'b' }) | ||
emitter.off('foo', onFoo) | ||
} | ||
} | ||
`, | ||
parserOptions | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
filename: 'test.js', | ||
code: ` | ||
app.component('some-comp', { | ||
mounted () { | ||
this.$on('start', function (args) { | ||
console.log('start', args) | ||
}) | ||
} | ||
}) | ||
`, | ||
parserOptions, | ||
errors: [{ | ||
message: 'The Events api `$on`, `$off` `$once` is deprecated. Using external library instead, for example mitt.', | ||
line: 4 | ||
}] | ||
}, | ||
{ | ||
filename: 'test.js', | ||
code: ` | ||
app.component('some-comp', { | ||
mounted () { | ||
this.$off('start') | ||
} | ||
}) | ||
`, | ||
parserOptions, | ||
errors: [{ | ||
message: 'The Events api `$on`, `$off` `$once` is deprecated. Using external library instead, for example mitt.', | ||
line: 4 | ||
}] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
mounted () { | ||
this.$once('start', function () { | ||
console.log('start') | ||
}) | ||
} | ||
} | ||
`, | ||
parserOptions, | ||
errors: [{ | ||
message: 'The Events api `$on`, `$off` `$once` is deprecated. Using external library instead, for example mitt.', | ||
line: 4 | ||
}] | ||
} | ||
] | ||
}) |