Skip to content

Fixed crash in vue/no-async-in-computed-properties, vue/no-setup-props-destructure and vue/no-watch-after-await #1227

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
merged 1 commit into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions lib/rules/no-async-in-computed-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ module.exports = {
create(context) {
/**
* @typedef {object} ScopeStack
* @property {ScopeStack} upper
* @property {ScopeStack | null} upper
* @property {BlockStatement | Expression} body
*/
/** @type {Map<ObjectExpression, ComponentComputedProperty[]>} */
const computedPropertiesMap = new Map()
/** @type {ScopeStack} */
let scopeStack
/** @type {ScopeStack | null} */
let scopeStack = null

const expressionTypes = {
promise: 'asynchronous action',
Expand All @@ -105,11 +105,14 @@ module.exports = {
verify(node, node.body, 'async', computedPropertiesMap.get(vueNode))
}

scopeStack = { upper: scopeStack, body: node.body }
scopeStack = {
upper: scopeStack,
body: node.body
}
}

function onFunctionExit() {
scopeStack = scopeStack.upper
scopeStack = scopeStack && scopeStack.upper
}

/**
Expand Down Expand Up @@ -146,6 +149,9 @@ module.exports = {
':function:exit': onFunctionExit,

NewExpression(node, { node: vueNode }) {
if (!scopeStack) {
return
}
if (
node.callee.type === 'Identifier' &&
node.callee.name === 'Promise'
Expand All @@ -160,6 +166,9 @@ module.exports = {
},

CallExpression(node, { node: vueNode }) {
if (!scopeStack) {
return
}
if (isPromise(node)) {
verify(
node,
Expand All @@ -178,6 +187,9 @@ module.exports = {
},

AwaitExpression(node, { node: vueNode }) {
if (!scopeStack) {
return
}
verify(
node,
scopeStack.body,
Expand Down
10 changes: 5 additions & 5 deletions lib/rules/no-bare-strings-in-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ module.exports = {
/** @param {RuleContext} context */
create(context) {
/**
* @typedef { { upper: ElementStack, name: string, attrs: Set<string> } } ElementStack
* @typedef { { upper: ElementStack | null, name: string, attrs: Set<string> } } ElementStack
*/
const opts = context.options[0] || {}
/** @type {string[]} */
Expand All @@ -173,8 +173,8 @@ module.exports = {
'gu'
)

/** @type {ElementStack} */
let elementStack
/** @type {ElementStack | null} */
let elementStack = null
/**
* Gets the bare string from given string
* @param {string} str
Expand Down Expand Up @@ -231,11 +231,11 @@ module.exports = {
}
},
'VElement:exit'() {
elementStack = elementStack.upper
elementStack = elementStack && elementStack.upper
},
/** @param {VAttribute|VDirective} node */
VAttribute(node) {
if (!node.value) {
if (!node.value || !elementStack) {
return
}
if (node.directive === false) {
Expand Down
27 changes: 16 additions & 11 deletions lib/rules/no-lifecycle-after-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ module.exports = {
*/
/**
* @typedef {object} ScopeStack
* @property {ScopeStack} upper
* @property {ScopeStack | null} upper
* @property {FunctionDeclaration | FunctionExpression | ArrowFunctionExpression} functionNode
*/
/** @type {Set<ESNode>} */
const lifecycleHookCallNodes = new Set()
/** @type {Map<FunctionDeclaration | FunctionExpression | ArrowFunctionExpression, SetupFunctionData>} */
const setupFunctions = new Map()

/** @type {ScopeStack} */
let scopeStack
/** @type {ScopeStack | null} */
let scopeStack = null

return Object.assign(
{
Expand All @@ -81,7 +81,10 @@ module.exports = {
},
utils.defineVueVisitor(context, {
':function'(node) {
scopeStack = { upper: scopeStack, functionNode: node }
scopeStack = {
upper: scopeStack,
functionNode: node
}
},
onSetupFunctionEnter(node) {
setupFunctions.set(node, {
Expand All @@ -90,18 +93,20 @@ module.exports = {
})
},
AwaitExpression() {
const setupFunctionData = setupFunctions.get(
scopeStack && scopeStack.functionNode
)
if (!scopeStack) {
return
}
const setupFunctionData = setupFunctions.get(scopeStack.functionNode)
if (!setupFunctionData) {
return
}
setupFunctionData.afterAwait = true
},
CallExpression(node) {
const setupFunctionData = setupFunctions.get(
scopeStack && scopeStack.functionNode
)
if (!scopeStack) {
return
}
const setupFunctionData = setupFunctions.get(scopeStack.functionNode)
if (!setupFunctionData || !setupFunctionData.afterAwait) {
return
}
Expand All @@ -118,7 +123,7 @@ module.exports = {
}
},
':function:exit'(node) {
scopeStack = scopeStack.upper
scopeStack = scopeStack && scopeStack.upper

setupFunctions.delete(node)
}
Expand Down
19 changes: 14 additions & 5 deletions lib/rules/no-setup-props-destructure.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,20 @@ module.exports = {
}
/**
* @typedef {object} ScopeStack
* @property {ScopeStack} upper
* @property {ScopeStack | null} upper
* @property {FunctionDeclaration | FunctionExpression | ArrowFunctionExpression} functionNode
*/
/**
* @type {ScopeStack}
* @type {ScopeStack | null}
*/
let scopeStack
let scopeStack = null

return utils.defineVueVisitor(context, {
':function'(node) {
scopeStack = { upper: scopeStack, functionNode: node }
scopeStack = {
upper: scopeStack,
functionNode: node
}
},
onSetupFunctionEnter(node) {
const propsParam = utils.unwrapAssignmentPattern(node.params[0])
Expand Down Expand Up @@ -113,6 +116,9 @@ module.exports = {
setupScopePropsReferenceIds.set(node, propsReferenceIds)
},
VariableDeclarator(node) {
if (!scopeStack) {
return
}
const propsReferenceIds = setupScopePropsReferenceIds.get(
scopeStack.functionNode
)
Expand All @@ -122,6 +128,9 @@ module.exports = {
verify(node.id, node.init, propsReferenceIds)
},
AssignmentExpression(node) {
if (!scopeStack) {
return
}
const propsReferenceIds = setupScopePropsReferenceIds.get(
scopeStack.functionNode
)
Expand All @@ -131,7 +140,7 @@ module.exports = {
verify(node.left, node.right, propsReferenceIds)
},
':function:exit'(node) {
scopeStack = scopeStack.upper
scopeStack = scopeStack && scopeStack.upper

setupScopePropsReferenceIds.delete(node)
}
Expand Down
21 changes: 17 additions & 4 deletions lib/rules/no-side-effects-in-computed-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,26 @@ module.exports = {
/** @type {Map<ObjectExpression, ComponentComputedProperty[]>} */
const computedPropertiesMap = new Map()

/** @type { { upper: any, body: null | BlockStatement | Expression } } */
let scopeStack = { upper: null, body: null }
/**
* @typedef {object} ScopeStack
* @property {ScopeStack | null} upper
* @property {BlockStatement | Expression | null} body
*/
/**
* @type {ScopeStack | null}
*/
let scopeStack = null

/** @param {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration} node */
function onFunctionEnter(node) {
scopeStack = { upper: scopeStack, body: node.body }
scopeStack = {
upper: scopeStack,
body: node.body
}
}

function onFunctionExit() {
scopeStack = scopeStack.upper
scopeStack = scopeStack && scopeStack.upper
}

return utils.defineVueVisitor(context, {
Expand All @@ -59,6 +69,9 @@ module.exports = {
node,
{ node: vueNode }
) {
if (!scopeStack) {
return
}
const targetBody = scopeStack.body
const computedProperty = /** @type {ComponentComputedProperty[]} */ (computedPropertiesMap.get(
vueNode
Expand Down
20 changes: 10 additions & 10 deletions lib/rules/no-template-shadow.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ module.exports = {

/**
* @typedef {object} ScopeStack
* @property {ScopeStack} parent
* @property {ScopeStack | null} parent
* @property {Identifier[]} nodes
*/
/** @type {ScopeStack} */
let scope
/** @type {ScopeStack | null} */
let scopeStack = null

// ----------------------------------------------------------------------
// Public
Expand All @@ -55,18 +55,18 @@ module.exports = {
{
/** @param {VElement} node */
VElement(node) {
scope = {
parent: scope,
nodes: scope
? scope.nodes.slice() // make copy
scopeStack = {
parent: scopeStack,
nodes: scopeStack
? scopeStack.nodes.slice() // make copy
: []
}
if (node.variables) {
for (const variable of node.variables) {
const varNode = variable.id
const name = varNode.name
if (
scope.nodes.some((node) => node.name === name) ||
scopeStack.nodes.some((node) => node.name === name) ||
jsVars.has(name)
) {
context.report({
Expand All @@ -79,13 +79,13 @@ module.exports = {
}
})
} else {
scope.nodes.push(varNode)
scopeStack.nodes.push(varNode)
}
}
}
},
'VElement:exit'() {
scope = scope.parent
scopeStack = scopeStack && scopeStack.parent
}
},
utils.executeOnVue(context, (obj) => {
Expand Down
19 changes: 14 additions & 5 deletions lib/rules/no-watch-after-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ module.exports = {

/**
* @typedef {object} ScopeStack
* @property {ScopeStack} upper
* @property {ScopeStack | null} upper
* @property {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration} functionNode
*/
/** @type {ScopeStack} */
let scopeStack
/** @type {ScopeStack | null} */
let scopeStack = null

return Object.assign(
{
Expand All @@ -88,7 +88,10 @@ module.exports = {
utils.defineVueVisitor(context, {
/** @param {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration} node */
':function'(node) {
scopeStack = { upper: scopeStack, functionNode: node }
scopeStack = {
upper: scopeStack,
functionNode: node
}
},
onSetupFunctionEnter(node) {
setupFunctions.set(node, {
Expand All @@ -97,6 +100,9 @@ module.exports = {
})
},
AwaitExpression() {
if (!scopeStack) {
return
}
const setupFunctionData = setupFunctions.get(scopeStack.functionNode)
if (!setupFunctionData) {
return
Expand All @@ -105,6 +111,9 @@ module.exports = {
},
/** @param {CallExpression} node */
CallExpression(node) {
if (!scopeStack) {
return
}
const setupFunctionData = setupFunctions.get(scopeStack.functionNode)
if (!setupFunctionData || !setupFunctionData.afterAwait) {
return
Expand All @@ -119,7 +128,7 @@ module.exports = {
},
/** @param {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration} node */
':function:exit'(node) {
scopeStack = scopeStack.upper
scopeStack = scopeStack && scopeStack.upper

setupFunctions.delete(node)
}
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/require-direct-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ module.exports = {

/**
* @typedef {object} ScopeStack
* @property {ScopeStack} upper
* @property {ScopeStack | null} upper
* @property {boolean} withinVue3FunctionalBody
*/

/** @type { { body: BlockStatement, hasReturnArgument: boolean } } */
let maybeVue3Functional
/** @type {ScopeStack} */
let scopeStack
/** @type {ScopeStack | null} */
let scopeStack = null

return {
/** @param {Declaration | Expression} node */
Expand Down
Loading