-
-
Notifications
You must be signed in to change notification settings - Fork 696
Added ignoredObjectNames
option to vue/no-async-in-computed-properties
#2927
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
Changes from 4 commits
0a556c5
fdff3d9
0f4d3f3
ce07db6
276be27
86eec3c
d5805cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'eslint-plugin-vue': minor | ||
--- | ||
|
||
Added `ignoredObjectNames` option to vue/no-async-in-computed-properties | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,22 +38,69 @@ function isTimedFunction(node) { | |
) | ||
} | ||
|
||
/** | ||
* Get the root object name from a member expression chain | ||
* @param {MemberExpression} memberExpr | ||
* @returns {string|null} | ||
*/ | ||
function getRootObjectName(memberExpr) { | ||
let current = memberExpr.object | ||
|
||
while (current) { | ||
switch (current.type) { | ||
case 'MemberExpression': { | ||
current = utils.skipChainExpression(current.object) | ||
break | ||
} | ||
case 'CallExpression': { | ||
const calleeExpr = utils.skipChainExpression(current.callee) | ||
if (calleeExpr.type === 'MemberExpression') { | ||
current = calleeExpr.object | ||
} else if (calleeExpr.type === 'Identifier') { | ||
return calleeExpr.name | ||
} else { | ||
return null | ||
} | ||
break | ||
} | ||
case 'Identifier': { | ||
return current.name | ||
} | ||
default: { | ||
return null | ||
} | ||
} | ||
} | ||
|
||
return null | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function parameter Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
|
||
/** | ||
* @param {CallExpression} node | ||
* @param {Set<string>} ignoredObjectNames | ||
*/ | ||
function isPromise(node) { | ||
function isPromise(node, ignoredObjectNames) { | ||
const callee = utils.skipChainExpression(node.callee) | ||
if (callee.type === 'MemberExpression') { | ||
const name = utils.getStaticPropertyName(callee) | ||
return ( | ||
name && | ||
// hello.PROMISE_FUNCTION() | ||
(PROMISE_FUNCTIONS.has(name) || | ||
// Promise.PROMISE_METHOD() | ||
(callee.object.type === 'Identifier' && | ||
if ( | ||
!name || | ||
(!PROMISE_FUNCTIONS.has(name) && | ||
!( | ||
callee.object.type === 'Identifier' && | ||
callee.object.name === 'Promise' && | ||
PROMISE_METHODS.has(name))) | ||
) | ||
PROMISE_METHODS.has(name) | ||
waynzh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
)) | ||
) { | ||
return false | ||
} | ||
|
||
const rootObjectName = getRootObjectName(callee) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
if (rootObjectName && ignoredObjectNames.has(rootObjectName)) { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
return false | ||
} | ||
|
@@ -85,7 +132,20 @@ module.exports = { | |
url: 'https://eslint.vuejs.org/rules/no-async-in-computed-properties.html' | ||
}, | ||
fixable: null, | ||
schema: [], | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
ignoredObjectNames: { | ||
type: 'array', | ||
items: { type: 'string' }, | ||
uniqueItems: true, | ||
additionalItems: false | ||
} | ||
}, | ||
additionalProperties: false | ||
} | ||
], | ||
messages: { | ||
unexpectedInFunction: | ||
'Unexpected {{expressionName}} in computed function.', | ||
|
@@ -95,6 +155,9 @@ module.exports = { | |
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
const options = context.options[0] || {} | ||
const ignoredObjectNames = new Set(options.ignoredObjectNames || []) | ||
|
||
/** @type {Map<ObjectExpression, ComponentComputedProperty[]>} */ | ||
const computedPropertiesMap = new Map() | ||
/** @type {(FunctionExpression | ArrowFunctionExpression)[]} */ | ||
|
@@ -217,7 +280,7 @@ module.exports = { | |
if (!scopeStack) { | ||
return | ||
} | ||
if (isPromise(node)) { | ||
if (isPromise(node, ignoredObjectNames)) { | ||
verify( | ||
node, | ||
scopeStack.body, | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -324,6 +324,52 @@ ruleTester.run('no-async-in-computed-properties', rule, { | |||||
sourceType: 'module', | ||||||
ecmaVersion: 2020 | ||||||
} | ||||||
}, | ||||||
{ | ||||||
filename: 'test.vue', | ||||||
code: ` | ||||||
export default { | ||||||
computed: { | ||||||
foo: function () { | ||||||
return z.catch( | ||||||
z.string().check(z.minLength(2)), | ||||||
'default' | ||||||
).then(val => val).finally(() => {}) | ||||||
} | ||||||
} | ||||||
} | ||||||
`, | ||||||
options: [{ ignoredObjectNames: ['z'] }], | ||||||
languageOptions | ||||||
}, | ||||||
{ | ||||||
filename: 'test.vue', | ||||||
code: ` | ||||||
<script setup> | ||||||
import { computed } from 'vue' | ||||||
|
||||||
const numberWithCatch = computed(() => z.number().catch(42)) | ||||||
</script>`, | ||||||
options: [{ ignoredObjectNames: ['z'] }], | ||||||
languageOptions: { | ||||||
parser, | ||||||
sourceType: 'module', | ||||||
ecmaVersion: 2020 | ||||||
} | ||||||
}, | ||||||
{ | ||||||
filename: 'test.vue', | ||||||
code: ` | ||||||
export default { | ||||||
computed: { | ||||||
foo: function () { | ||||||
return z.a.b.c.d.e.f.method().catch(err => err).finally(() => {}) | ||||||
|
return z.a.b.c.d.e.f.method().catch(err => err).finally(() => {}) | |
return z.a?.b!.['c'][d].e.f.method().catch(err => err).finally(() => {}) |
(The non-null assertion !
needs the typescript-eslint parser, so maybe this should be a separate test)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just pushed a commit that adds a skipWrapper
function to handle complex member expression chains with optional chaining support.
Decided not to use TypeScript AST node types directly since that could lead to a bunch of type conflicts...
Uh oh!
There was an error while loading. Please reload this page.