diff --git a/flow/compiler.js b/flow/compiler.js index ad09a2e2ebe..b93eee7133f 100644 --- a/flow/compiler.js +++ b/flow/compiler.js @@ -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; declare type ASTAttr = { diff --git a/src/compiler/codegen/index.js b/src/compiler/codegen/index.js index c96b3a38511..ec0dcbe3ef5 100644 --- a/src/compiler/codegen/index.js +++ b/src/compiler/codegen/index.js @@ -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)}` } diff --git a/src/compiler/parser/index.js b/src/compiler/parser/index.js index cdeb257eda4..8040435f5a2 100644 --- a/src/compiler/parser/index.js +++ b/src/compiler/parser/index.js @@ -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 = /^\[.*\]$/ @@ -581,6 +582,10 @@ function findPrevElement (children: Array): 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 = [] }