Skip to content

Commit

Permalink
fix(v-model): support calling methods in v-model expression
Browse files Browse the repository at this point in the history
close #3993
  • Loading branch information
yyx990803 committed Jul 15, 2021
1 parent 395572b commit 5af718b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/compiler-core/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ test('isMemberExpression', () => {
expect(isMemberExpression('obj[1][2].foo[3].bar.baz')).toBe(true)
expect(isMemberExpression(`a[b[c.d]][0]`)).toBe(true)
expect(isMemberExpression('obj?.foo')).toBe(true)
expect(isMemberExpression('foo().test')).toBe(true)

// strings
expect(isMemberExpression(`a['foo' + bar[baz]["qux"]]`)).toBe(true)
Expand Down
25 changes: 24 additions & 1 deletion packages/compiler-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const isSimpleIdentifier = (name: string): boolean =>
const enum MemberExpLexState {
inMemberExp,
inBrackets,
inParens,
inString
}

Expand All @@ -79,6 +80,7 @@ export const isMemberExpression = (path: string): boolean => {
let state = MemberExpLexState.inMemberExp
let stateStack: MemberExpLexState[] = []
let currentOpenBracketCount = 0
let currentOpenParensCount = 0
let currentStringType: "'" | '"' | '`' | null = null

for (let i = 0; i < path.length; i++) {
Expand All @@ -89,6 +91,10 @@ export const isMemberExpression = (path: string): boolean => {
stateStack.push(state)
state = MemberExpLexState.inBrackets
currentOpenBracketCount++
} else if (char === '(') {
stateStack.push(state)
state = MemberExpLexState.inParens
currentOpenParensCount++
} else if (
!(i === 0 ? validFirstIdentCharRE : validIdentCharRE).test(char)
) {
Expand All @@ -108,6 +114,23 @@ export const isMemberExpression = (path: string): boolean => {
}
}
break
case MemberExpLexState.inParens:
if (char === `'` || char === `"` || char === '`') {
stateStack.push(state)
state = MemberExpLexState.inString
currentStringType = char
} else if (char === `(`) {
currentOpenParensCount++
} else if (char === `)`) {
// if the exp ends as a call then it should not be considered valid
if (i === path.length - 1) {
return false
}
if (!--currentOpenParensCount) {
state = stateStack.pop()!
}
}
break
case MemberExpLexState.inString:
if (char === currentStringType) {
state = stateStack.pop()!
Expand All @@ -116,7 +139,7 @@ export const isMemberExpression = (path: string): boolean => {
break
}
}
return !currentOpenBracketCount
return !currentOpenBracketCount && !currentOpenParensCount
}

export function getInnerRange(
Expand Down

0 comments on commit 5af718b

Please sign in to comment.