|
| 1 | +/** |
| 2 | + * @typedef {import('estree-jsx').Program} EstreeProgram |
| 3 | + */ |
| 4 | + |
1 | 5 | import assert from 'node:assert/strict'
|
2 | 6 | import test from 'node:test'
|
3 | 7 | import {Parser} from 'acorn'
|
4 | 8 | import jsx from 'acorn-jsx'
|
5 | 9 | import {fromEstree} from './index.js'
|
6 |
| -import * as mod from './index.js' |
7 | 10 |
|
8 | 11 | const parser = Parser.extend(jsx())
|
9 | 12 |
|
10 |
| -test('fromEstree', () => { |
11 |
| - assert.deepEqual( |
12 |
| - Object.keys(mod).sort(), |
13 |
| - ['fromEstree'], |
14 |
| - 'should expose the public api' |
15 |
| - ) |
| 13 | +test('fromEstree', async function (t) { |
| 14 | + await t.test('should expose the public api', async function () { |
| 15 | + assert.deepEqual(Object.keys(await import('./index.js')).sort(), [ |
| 16 | + 'fromEstree' |
| 17 | + ]) |
| 18 | + }) |
| 19 | + |
| 20 | + await t.test('should transform', async function () { |
| 21 | + /** @type {EstreeProgram} */ |
| 22 | + // @ts-expect-error: acorn looks like estree. |
| 23 | + const tree = parser.parse('console.log(1)', { |
| 24 | + locations: true, |
| 25 | + ecmaVersion: 2021 |
| 26 | + }) |
16 | 27 |
|
17 |
| - assert.deepEqual( |
18 |
| - fromEstree( |
19 |
| - // @ts-expect-error Similar enough. |
20 |
| - parser.parse('console.log(1)', {locations: true, ecmaVersion: 2021}) |
21 |
| - ), |
22 |
| - { |
| 28 | + assert.deepEqual(fromEstree(tree), { |
23 | 29 | type: 'Program',
|
24 | 30 | body: [
|
25 | 31 | {
|
@@ -78,35 +84,35 @@ test('fromEstree', () => {
|
78 | 84 | start: {line: 1, column: 1, offset: 0},
|
79 | 85 | end: {line: 1, column: 15, offset: 14}
|
80 | 86 | }
|
81 |
| - }, |
82 |
| - 'should transform' |
83 |
| - ) |
| 87 | + }) |
| 88 | + }) |
84 | 89 |
|
85 |
| - assert.deepEqual( |
86 |
| - fromEstree( |
87 |
| - // @ts-expect-error Hush, it’s fine. |
88 |
| - parser.parse('/(?:)/', {locations: true, ecmaVersion: 2021}).body[0] |
89 |
| - .expression |
90 |
| - ), |
91 |
| - { |
| 90 | + await t.test('should transform regexes', async function () { |
| 91 | + /** @type {EstreeProgram} */ |
| 92 | + // @ts-expect-error: acorn looks like estree. |
| 93 | + const tree = parser.parse('/(?:)/', {locations: true, ecmaVersion: 2021}) |
| 94 | + const statement = tree.body[0] |
| 95 | + assert(statement.type === 'ExpressionStatement') |
| 96 | + |
| 97 | + assert.deepEqual(fromEstree(statement.expression), { |
92 | 98 | type: 'Literal',
|
93 | 99 | value: null,
|
94 | 100 | regex: {pattern: '(?:)', flags: ''},
|
95 | 101 | position: {
|
96 | 102 | start: {line: 1, column: 1, offset: 0},
|
97 | 103 | end: {line: 1, column: 7, offset: 6}
|
98 | 104 | }
|
99 |
| - }, |
100 |
| - 'should transform regexes' |
101 |
| - ) |
| 105 | + }) |
| 106 | + }) |
| 107 | + |
| 108 | + await t.test('should transform jsx fragments', async function () { |
| 109 | + /** @type {EstreeProgram} */ |
| 110 | + // @ts-expect-error: acorn looks like estree. |
| 111 | + const tree = parser.parse('<>b</>', {locations: true, ecmaVersion: 2021}) |
| 112 | + const statement = tree.body[0] |
| 113 | + assert(statement.type === 'ExpressionStatement') |
102 | 114 |
|
103 |
| - assert.deepEqual( |
104 |
| - fromEstree( |
105 |
| - // @ts-expect-error Hush, it’s fine. |
106 |
| - parser.parse('<>b</>', {locations: true, ecmaVersion: 2021}).body[0] |
107 |
| - .expression |
108 |
| - ), |
109 |
| - { |
| 115 | + assert.deepEqual(fromEstree(statement.expression), { |
110 | 116 | type: 'JSXFragment',
|
111 | 117 | openingFragment: {
|
112 | 118 | type: 'JSXOpeningFragment',
|
@@ -136,32 +142,36 @@ test('fromEstree', () => {
|
136 | 142 | start: {line: 1, column: 1, offset: 0},
|
137 | 143 | end: {line: 1, column: 7, offset: 6}
|
138 | 144 | }
|
139 |
| - }, |
140 |
| - 'should transform jsx fragments' |
141 |
| - ) |
| 145 | + }) |
| 146 | + }) |
142 | 147 |
|
143 |
| - const bigInts = [ |
144 |
| - ['1n', 'dec'], |
145 |
| - ['0X1n', 'hex, cap'], |
146 |
| - ['0x1n', 'hex, low'], |
147 |
| - ['0O1n', 'oct, cap'], |
148 |
| - ['0o1n', 'oct, low'], |
149 |
| - ['0B1n', 'bin, cap'], |
150 |
| - ['0b1n', 'bin, low'] |
151 |
| - ] |
152 |
| - let index = -1 |
| 148 | + await t.test('should transform and normalize bigints', async function () { |
| 149 | + const bigInts = [ |
| 150 | + ['1n', 'dec'], |
| 151 | + ['0X1n', 'hex, cap'], |
| 152 | + ['0x1n', 'hex, low'], |
| 153 | + ['0O1n', 'oct, cap'], |
| 154 | + ['0o1n', 'oct, low'], |
| 155 | + ['0B1n', 'bin, cap'], |
| 156 | + ['0b1n', 'bin, low'] |
| 157 | + ] |
| 158 | + let index = -1 |
153 | 159 |
|
154 |
| - while (++index < bigInts.length) { |
155 |
| - const tree = fromEstree( |
156 |
| - // @ts-expect-error Hush, it’s fine. |
157 |
| - parser.parse(bigInts[index][0], {locations: true, ecmaVersion: 2021}) |
158 |
| - ) |
| 160 | + while (++index < bigInts.length) { |
| 161 | + /** @type {EstreeProgram} */ |
| 162 | + // @ts-expect-error: acorn looks like estree. |
| 163 | + const tree = parser.parse(bigInts[index][0], { |
| 164 | + locations: true, |
| 165 | + ecmaVersion: 2021 |
| 166 | + }) |
| 167 | + fromEstree(tree) |
| 168 | + const statement = tree.body[0] |
| 169 | + assert(statement.type === 'ExpressionStatement') |
| 170 | + const expression = statement.expression |
| 171 | + assert(expression.type === 'Literal') |
| 172 | + assert('bigint' in expression) |
159 | 173 |
|
160 |
| - assert.deepEqual( |
161 |
| - // @ts-expect-error Hush, it’s fine. |
162 |
| - tree.body[0].expression.bigint, |
163 |
| - '1', |
164 |
| - 'should transform and normalize bigints (`' + bigInts[index][1] + '`)' |
165 |
| - ) |
166 |
| - } |
| 174 | + assert.deepEqual(expression.bigint, '1') |
| 175 | + } |
| 176 | + }) |
167 | 177 | })
|
0 commit comments