|
| 1 | +import { TransformContext } from '../src' |
1 | 2 | import { Position } from '../src/ast'
|
2 | 3 | import {
|
3 | 4 | getInnerRange,
|
4 | 5 | advancePositionWithClone,
|
5 |
| - isMemberExpression, |
| 6 | + isMemberExpressionNode, |
| 7 | + isMemberExpressionBrowser, |
6 | 8 | toValidAssetId
|
7 | 9 | } from '../src/utils'
|
8 | 10 |
|
@@ -73,40 +75,60 @@ describe('getInnerRange', () => {
|
73 | 75 | })
|
74 | 76 | })
|
75 | 77 |
|
76 |
| -test('isMemberExpression', () => { |
77 |
| - // should work |
78 |
| - expect(isMemberExpression('obj.foo')).toBe(true) |
79 |
| - expect(isMemberExpression('obj[foo]')).toBe(true) |
80 |
| - expect(isMemberExpression('obj[arr[0]]')).toBe(true) |
81 |
| - expect(isMemberExpression('obj[arr[ret.bar]]')).toBe(true) |
82 |
| - expect(isMemberExpression('obj[arr[ret[bar]]]')).toBe(true) |
83 |
| - expect(isMemberExpression('obj[arr[ret[bar]]].baz')).toBe(true) |
84 |
| - expect(isMemberExpression('obj[1 + 1]')).toBe(true) |
85 |
| - expect(isMemberExpression(`obj[x[0]]`)).toBe(true) |
86 |
| - expect(isMemberExpression('obj[1][2]')).toBe(true) |
87 |
| - expect(isMemberExpression('obj[1][2].foo[3].bar.baz')).toBe(true) |
88 |
| - expect(isMemberExpression(`a[b[c.d]][0]`)).toBe(true) |
89 |
| - expect(isMemberExpression('obj?.foo')).toBe(true) |
90 |
| - expect(isMemberExpression('foo().test')).toBe(true) |
91 |
| - |
92 |
| - // strings |
93 |
| - expect(isMemberExpression(`a['foo' + bar[baz]["qux"]]`)).toBe(true) |
94 |
| - |
95 |
| - // multiline whitespaces |
96 |
| - expect(isMemberExpression('obj \n .foo \n [bar \n + baz]')).toBe(true) |
97 |
| - expect(isMemberExpression(`\n model\n.\nfoo \n`)).toBe(true) |
98 |
| - |
99 |
| - // should fail |
100 |
| - expect(isMemberExpression('a \n b')).toBe(false) |
101 |
| - expect(isMemberExpression('obj[foo')).toBe(false) |
102 |
| - expect(isMemberExpression('objfoo]')).toBe(false) |
103 |
| - expect(isMemberExpression('obj[arr[0]')).toBe(false) |
104 |
| - expect(isMemberExpression('obj[arr0]]')).toBe(false) |
105 |
| - expect(isMemberExpression('123[a]')).toBe(false) |
106 |
| - expect(isMemberExpression('a + b')).toBe(false) |
107 |
| - expect(isMemberExpression('foo()')).toBe(false) |
108 |
| - expect(isMemberExpression('a?b:c')).toBe(false) |
109 |
| - expect(isMemberExpression(`state['text'] = $event`)).toBe(false) |
| 78 | +describe('isMemberExpression', () => { |
| 79 | + function commonAssertions(fn: (str: string) => boolean) { |
| 80 | + // should work |
| 81 | + expect(fn('obj.foo')).toBe(true) |
| 82 | + expect(fn('obj[foo]')).toBe(true) |
| 83 | + expect(fn('obj[arr[0]]')).toBe(true) |
| 84 | + expect(fn('obj[arr[ret.bar]]')).toBe(true) |
| 85 | + expect(fn('obj[arr[ret[bar]]]')).toBe(true) |
| 86 | + expect(fn('obj[arr[ret[bar]]].baz')).toBe(true) |
| 87 | + expect(fn('obj[1 + 1]')).toBe(true) |
| 88 | + expect(fn(`obj[x[0]]`)).toBe(true) |
| 89 | + expect(fn('obj[1][2]')).toBe(true) |
| 90 | + expect(fn('obj[1][2].foo[3].bar.baz')).toBe(true) |
| 91 | + expect(fn(`a[b[c.d]][0]`)).toBe(true) |
| 92 | + expect(fn('obj?.foo')).toBe(true) |
| 93 | + expect(fn('foo().test')).toBe(true) |
| 94 | + |
| 95 | + // strings |
| 96 | + expect(fn(`a['foo' + bar[baz]["qux"]]`)).toBe(true) |
| 97 | + |
| 98 | + // multiline whitespaces |
| 99 | + expect(fn('obj \n .foo \n [bar \n + baz]')).toBe(true) |
| 100 | + expect(fn(`\n model\n.\nfoo \n`)).toBe(true) |
| 101 | + |
| 102 | + // should fail |
| 103 | + expect(fn('a \n b')).toBe(false) |
| 104 | + expect(fn('obj[foo')).toBe(false) |
| 105 | + expect(fn('objfoo]')).toBe(false) |
| 106 | + expect(fn('obj[arr[0]')).toBe(false) |
| 107 | + expect(fn('obj[arr0]]')).toBe(false) |
| 108 | + expect(fn('123[a]')).toBe(false) |
| 109 | + expect(fn('a + b')).toBe(false) |
| 110 | + expect(fn('foo()')).toBe(false) |
| 111 | + expect(fn('a?b:c')).toBe(false) |
| 112 | + expect(fn(`state['text'] = $event`)).toBe(false) |
| 113 | + } |
| 114 | + |
| 115 | + test('browser', () => { |
| 116 | + commonAssertions(isMemberExpressionBrowser) |
| 117 | + }) |
| 118 | + |
| 119 | + test('node', () => { |
| 120 | + const ctx = { expressionPlugins: ['typescript'] } as any as TransformContext |
| 121 | + const fn = (str: string) => isMemberExpressionNode(str, ctx) |
| 122 | + commonAssertions(fn) |
| 123 | + |
| 124 | + // TS-specific checks |
| 125 | + expect(fn('foo as string')).toBe(true) |
| 126 | + expect(fn(`foo.bar as string`)).toBe(true) |
| 127 | + expect(fn(`foo['bar'] as string`)).toBe(true) |
| 128 | + expect(fn(`foo[bar as string]`)).toBe(true) |
| 129 | + expect(fn(`foo() as string`)).toBe(false) |
| 130 | + expect(fn(`a + b as string`)).toBe(false) |
| 131 | + }) |
110 | 132 | })
|
111 | 133 |
|
112 | 134 | test('toValidAssetId', () => {
|
|
0 commit comments