+27
-3
lines changedFilter options
+27
-3
lines changed Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
1
1
import { Position } from '../src/ast'
2
-
import { getInnerRange, advancePositionWithClone } from '../src/utils'
2
+
import {
3
+
getInnerRange,
4
+
advancePositionWithClone,
5
+
isMemberExpression
6
+
} from '../src/utils'
3
7
4
8
function p(line: number, column: number, offset: number): Position {
5
9
return { column, line, offset }
@@ -67,3 +71,19 @@ describe('getInnerRange', () => {
67
71
expect(loc2.end.offset).toBe(7)
68
72
})
69
73
})
74
+
75
+
test('isMemberExpression', () => {
76
+
// should work
77
+
expect(isMemberExpression('obj.foo')).toBe(true)
78
+
expect(isMemberExpression('obj[foo]')).toBe(true)
79
+
expect(isMemberExpression('obj[arr[0]]')).toBe(true)
80
+
expect(isMemberExpression('obj[arr[ret.bar]]')).toBe(true)
81
+
expect(isMemberExpression('obj[arr[ret[bar]]]')).toBe(true)
82
+
expect(isMemberExpression('obj[arr[ret[bar]]].baz')).toBe(true)
83
+
expect(isMemberExpression('obj[1 + 1]')).toBe(true)
84
+
// should warning
85
+
expect(isMemberExpression('obj[foo')).toBe(false)
86
+
expect(isMemberExpression('objfoo]')).toBe(false)
87
+
expect(isMemberExpression('obj[arr[0]')).toBe(false)
88
+
expect(isMemberExpression('obj[arr0]]')).toBe(false)
89
+
})
Original file line number Diff line number Diff line change
@@ -56,10 +56,14 @@ const nonIdentifierRE = /^\d|[^\$\w]/
56
56
export const isSimpleIdentifier = (name: string): boolean =>
57
57
!nonIdentifierRE.test(name)
58
58
59
-
const memberExpRE = /^[A-Za-z_$\xA0-\uFFFF][\w$\xA0-\uFFFF]*(?:\s*\.\s*[A-Za-z_$\xA0-\uFFFF][\w$\xA0-\uFFFF]*|\[[^\]]+\])*$/
59
+
const memberExpRE = /^[A-Za-z_$\xA0-\uFFFF][\w$\xA0-\uFFFF]*(?:\s*\.\s*[A-Za-z_$\xA0-\uFFFF][\w$\xA0-\uFFFF]*|\[(.+)\])*$/
60
60
export const isMemberExpression = (path: string): boolean => {
61
61
if (!path) return false
62
-
return memberExpRE.test(path.trim())
62
+
const matched = memberExpRE.exec(path.trim())
63
+
if (!matched) return false
64
+
if (!matched[1]) return true
65
+
if (!/[\[\]]/.test(matched[1])) return true
66
+
return isMemberExpression(matched[1].trim())
63
67
}
64
68
65
69
export function getInnerRange(
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4