Skip to content
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

fix(compiler-core): check the validity of the member expression #3915

Closed
wants to merge 1 commit 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: 2 additions & 0 deletions packages/compiler-core/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 17 additions & 4 deletions packages/compiler-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down