Skip to content

fix #10184: Recognize assigment in v-if and do not throw warning #10193

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

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion flow/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ declare type ModuleOptions = {
};

declare type ASTModifiers = { [key: string]: boolean };
declare type ASTIfCondition = { exp: ?string; block: ASTElement };
declare type ASTIfCondition = { exp: ?string; alias?: string; block: ASTElement };
declare type ASTIfConditions = Array<ASTIfCondition>;

declare type ASTAttr = {
Expand Down
14 changes: 12 additions & 2 deletions src/compiler/codegen/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,21 @@ function genIfConditions (

const condition = conditions.shift()
if (condition.exp) {
return `(${condition.exp})?${
let ternaryExp = `(${condition.exp})?${
genTernaryExp(condition.block)
}:${
genIfConditions(conditions, state, altGen, altEmpty)
}`
}`;
if (condition.alias) {
// When there is assignment in `condition.exp`

// Wrap an IIFE (Immediately Invoked Function Expression) so that the variable of `condition.alias` will be local, and therefore no warning will be thrown.
return `function(){var ${condition.alias}; return ${ternaryExp}}()`
}
else {
return ternaryExp
}

} else {
return `${genTernaryExp(condition.block)}`
}
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const dirRE = process.env.VBIND_PROP_SHORTHAND
? /^v-|^@|^:|^\.|^#/
: /^v-|^@|^:|^#/
export const forAliasRE = /([\s\S]*?)\s+(?:in|of)\s+([\s\S]*)/
export const assignmentInIfRE = /([a-zA-Z][a-zA-Z0-9_$]*)\s+(?:=)\s+([\s\S]*)/
export const forIteratorRE = /,([^,\}\]]*)(?:,([^,\}\]]*))?$/
const stripParensRE = /^\(|\)$/g
const dynamicArgRE = /^\[.*\]$/
Expand Down Expand Up @@ -581,6 +582,10 @@ function findPrevElement (children: Array<any>): ASTElement | void {
}

export function addIfCondition (el: ASTElement, condition: ASTIfCondition) {
let m
if ((m = condition.exp && condition.exp.match(assignmentInIfRE))){
condition.alias = m[1];
}
if (!el.ifConditions) {
el.ifConditions = []
}
Expand Down