From 0fdb6c7078a230ea52c71c4c0b02215e76bf9ec2 Mon Sep 17 00:00:00 2001 From: Flo Edelmann Date: Thu, 28 Oct 2021 00:08:29 +0200 Subject: [PATCH 1/6] Support `asyncData` in `vue/no-undef-properties` --- lib/rules/no-undef-properties.js | 7 ++++++- lib/rules/no-unused-properties.js | 1 + lib/utils/index.js | 2 +- tests/lib/rules/no-undef-properties.js | 17 +++++++++++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/rules/no-undef-properties.js b/lib/rules/no-undef-properties.js index fdd506cc5..ad37922b3 100644 --- a/lib/rules/no-undef-properties.js +++ b/lib/rules/no-undef-properties.js @@ -32,6 +32,7 @@ const { // ------------------------------------------------------------------------------ const GROUP_PROPERTY = 'props' +const GROUP_ASYNC_DATA = 'asyncData' // Nuxt.js const GROUP_DATA = 'data' const GROUP_COMPUTED_PROPERTY = 'computed' const GROUP_METHODS = 'methods' @@ -334,6 +335,7 @@ module.exports = { node, new Set([ GROUP_PROPERTY, + GROUP_ASYNC_DATA, GROUP_DATA, GROUP_COMPUTED_PROPERTY, GROUP_SETUP, @@ -342,7 +344,10 @@ module.exports = { ]) )) { const propertyMap = - prop.groupName === GROUP_DATA && + ( + prop.groupName === GROUP_DATA || + prop.groupName === GROUP_ASYNC_DATA + ) && prop.type === 'object' && prop.property.value.type === 'ObjectExpression' ? getObjectPropertyMap(prop.property.value) diff --git a/lib/rules/no-unused-properties.js b/lib/rules/no-unused-properties.js index 0a36c6579..11b8b1804 100644 --- a/lib/rules/no-unused-properties.js +++ b/lib/rules/no-unused-properties.js @@ -69,6 +69,7 @@ const PROPERTY_LABEL = { setup: 'property returned from `setup()`', // not use + asyncData: 'async data', watch: 'watch', provide: 'provide', inject: 'inject', diff --git a/lib/utils/index.js b/lib/utils/index.js index 5c49d15f2..ac94af2d1 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -23,7 +23,7 @@ * @typedef { {key: string | null, value: BlockStatement | null} } ComponentComputedProperty */ /** - * @typedef { 'props' | 'data' | 'computed' | 'setup' | 'watch' | 'methods' | 'provide' | 'inject' | 'expose' } GroupName + * @typedef { 'props' | 'asyncData' | 'data' | 'computed' | 'setup' | 'watch' | 'methods' | 'provide' | 'inject' | 'expose' } GroupName * @typedef { { type: 'array', name: string, groupName: GroupName, node: Literal | TemplateLiteral } } ComponentArrayPropertyData * @typedef { { type: 'object', name: string, groupName: GroupName, node: Identifier | Literal | TemplateLiteral, property: Property } } ComponentObjectPropertyData * @typedef { ComponentArrayPropertyData | ComponentObjectPropertyData } ComponentPropertyData diff --git a/tests/lib/rules/no-undef-properties.js b/tests/lib/rules/no-undef-properties.js index 09e16bda9..f432a8d0e 100644 --- a/tests/lib/rules/no-undef-properties.js +++ b/tests/lib/rules/no-undef-properties.js @@ -67,6 +67,23 @@ tester.run('no-undef-properties', rule, { ` }, + { + filename: 'test.vue', + code: ` + + + ` + }, //default ignores { filename: 'test.vue', From e05d60e5c4698389ae7c16c93779e0c6df53c8b5 Mon Sep 17 00:00:00 2001 From: Flo Edelmann Date: Thu, 28 Oct 2021 00:12:28 +0200 Subject: [PATCH 2/6] Support `asyncData` in `vue/no-dupe-keys` --- lib/rules/no-dupe-keys.js | 2 +- tests/lib/rules/no-dupe-keys.js | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/rules/no-dupe-keys.js b/lib/rules/no-dupe-keys.js index 8b5e8e54c..d7aa20e8c 100644 --- a/lib/rules/no-dupe-keys.js +++ b/lib/rules/no-dupe-keys.js @@ -14,7 +14,7 @@ const utils = require('../utils') // Rule Definition // ------------------------------------------------------------------------------ /** @type {GroupName[]} */ -const GROUP_NAMES = ['props', 'computed', 'data', 'methods', 'setup'] +const GROUP_NAMES = ['props', 'computed', 'data', 'asyncData', 'methods', 'setup'] module.exports = { meta: { diff --git a/tests/lib/rules/no-dupe-keys.js b/tests/lib/rules/no-dupe-keys.js index d85655669..9e01f0856 100644 --- a/tests/lib/rules/no-dupe-keys.js +++ b/tests/lib/rules/no-dupe-keys.js @@ -709,6 +709,29 @@ ruleTester.run('no-dupe-keys', rule, { } ] }, + { + filename: 'test.vue', + code: ` + export default { + asyncData() { + return { + foo: 1 + } + }, + data() { + return { + foo: 2 + } + }, + } + `, + errors: [ + { + message: "Duplicated key 'foo'.", + line: 10 + } + ] + }, { filename: 'test.js', code: ` From 6eee8ad357e4d427e4a658ffc840d774407bf49f Mon Sep 17 00:00:00 2001 From: Flo Edelmann Date: Thu, 28 Oct 2021 00:17:57 +0200 Subject: [PATCH 3/6] Support `asyncData` in `vue/no-reserved-keys` --- lib/rules/no-reserved-keys.js | 4 ++-- tests/lib/rules/no-reserved-keys.js | 36 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/rules/no-reserved-keys.js b/lib/rules/no-reserved-keys.js index 5b73d9c1f..1b36ffbe5 100644 --- a/lib/rules/no-reserved-keys.js +++ b/lib/rules/no-reserved-keys.js @@ -16,7 +16,7 @@ const utils = require('../utils') const RESERVED_KEYS = require('../utils/vue-reserved.json') /** @type {GroupName[]} */ -const GROUP_NAMES = ['props', 'computed', 'data', 'methods', 'setup'] +const GROUP_NAMES = ['props', 'computed', 'data', 'asyncData', 'methods', 'setup'] module.exports = { meta: { @@ -76,7 +76,7 @@ module.exports = { utils.executeOnVue(context, (obj) => { const properties = utils.iterateProperties(obj, groups) for (const o of properties) { - if (o.groupName === 'data' && o.name[0] === '_') { + if ((o.groupName === 'data' || o.groupName === 'asyncData') && o.name[0] === '_') { context.report({ node: o.node, messageId: 'startsWithUnderscore', diff --git a/tests/lib/rules/no-reserved-keys.js b/tests/lib/rules/no-reserved-keys.js index 02a1e79b9..b262ea44f 100644 --- a/tests/lib/rules/no-reserved-keys.js +++ b/tests/lib/rules/no-reserved-keys.js @@ -156,6 +156,25 @@ ruleTester.run('no-reserved-keys', rule, { } ] }, + { + filename: 'test.js', + code: ` + new Vue({ + asyncData () { + return { + $el: '' + } + } + }) + `, + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + message: "Key '$el' is reserved.", + line: 5 + } + ] + }, { filename: 'test.js', code: ` @@ -209,6 +228,23 @@ ruleTester.run('no-reserved-keys', rule, { } ] }, + { + filename: 'test.js', + code: ` + new Vue({ + asyncData: () => ({ + _foo: String + }) + }) + `, + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + message: "Keys starting with '_' are reserved in '_foo' group.", + line: 4 + } + ] + }, { filename: 'test.js', code: ` From 59cc76d9673dbbc132f6cf03e3f4d296ea735285 Mon Sep 17 00:00:00 2001 From: Flo Edelmann Date: Thu, 28 Oct 2021 00:20:59 +0200 Subject: [PATCH 4/6] Support `asyncData` in `vue/no-template-shadow` --- lib/rules/no-template-shadow.js | 2 +- tests/lib/rules/no-template-shadow.js | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/rules/no-template-shadow.js b/lib/rules/no-template-shadow.js index f66f8a701..abca72e2f 100644 --- a/lib/rules/no-template-shadow.js +++ b/lib/rules/no-template-shadow.js @@ -19,7 +19,7 @@ const utils = require('../utils') // ------------------------------------------------------------------------------ /** @type {GroupName[]} */ -const GROUP_NAMES = ['props', 'computed', 'data', 'methods', 'setup'] +const GROUP_NAMES = ['props', 'computed', 'data', 'asyncData', 'methods', 'setup'] module.exports = { meta: { diff --git a/tests/lib/rules/no-template-shadow.js b/tests/lib/rules/no-template-shadow.js index 1f1daa184..cd66b06ab 100644 --- a/tests/lib/rules/no-template-shadow.js +++ b/tests/lib/rules/no-template-shadow.js @@ -250,6 +250,28 @@ ruleTester.run('no-template-shadow', rule, { } ] }, + { + filename: 'test.vue', + code: ` + `, + errors: [ + { + message: "Variable 'i' is already declared in the upper scope.", + type: 'Identifier', + line: 2 + } + ] + }, { filename: 'test.vue', code: `