diff --git a/packages/compiler-core/__tests__/utils.spec.ts b/packages/compiler-core/__tests__/utils.spec.ts index 4fc4e8d16a5..20543689fdd 100644 --- a/packages/compiler-core/__tests__/utils.spec.ts +++ b/packages/compiler-core/__tests__/utils.spec.ts @@ -81,6 +81,8 @@ test('isMemberExpression', () => { expect(isMemberExpression('obj[arr[ret[bar]]]')).toBe(true) expect(isMemberExpression('obj[arr[ret[bar]]].baz')).toBe(true) expect(isMemberExpression('obj[1 + 1]')).toBe(true) + expect(isMemberExpression('obj[1][2]')).toBe(true) + expect(isMemberExpression('obj[1][2].foo[3].bar.baz')).toBe(true) // should warning expect(isMemberExpression('obj[foo')).toBe(false) expect(isMemberExpression('objfoo]')).toBe(false) diff --git a/packages/compiler-core/src/utils.ts b/packages/compiler-core/src/utils.ts index d69446d8909..2ff930e0d7e 100644 --- a/packages/compiler-core/src/utils.ts +++ b/packages/compiler-core/src/utils.ts @@ -56,14 +56,27 @@ const nonIdentifierRE = /^\d|[^\$\w]/ export const isSimpleIdentifier = (name: string): boolean => !nonIdentifierRE.test(name) -const memberExpRE = /^[A-Za-z_$\xA0-\uFFFF][\w$\xA0-\uFFFF]*(?:\s*\.\s*[A-Za-z_$\xA0-\uFFFF][\w$\xA0-\uFFFF]*|\[(.+)\])*$/ -export const isMemberExpression = (path: string): boolean => { +const createMemberExpRE = (re: string) => + new RegExp( + '^[A-Za-z_$\\xA0-\\uFFFF][\\w$\\xA0-\\uFFFF]*(?:\\s*\\.\\s*[A-Za-z_$\\xA0-\\uFFFF][\\w$\\xA0-\\uFFFF]*|\\[' + + re + + '\\])*$' + ) + +const matchMemberExpression = (path: string, re: RegExp): boolean => { if (!path) return false - const matched = memberExpRE.exec(path.trim()) + const matched = re.exec(path.trim()) if (!matched) return false if (!matched[1]) return true if (!/[\[\]]/.test(matched[1])) return true - return isMemberExpression(matched[1].trim()) + return matchMemberExpression(matched[1].trim(), re) +} + +export const isMemberExpression = (path: string): boolean => { + return ( + matchMemberExpression(path, createMemberExpRE('([^\\]]+)')) || + matchMemberExpression(path, createMemberExpRE('(.+)')) + ) } export function getInnerRange(