Skip to content

Commit 78b75f1

Browse files
committed
Refactor code-style
1 parent e228e86 commit 78b75f1

File tree

3 files changed

+97
-69
lines changed

3 files changed

+97
-69
lines changed

lib/index.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
/**
2-
* @typedef {import('unist').Node} UnistNode
32
* @typedef {import('estree-jsx').Node} EstreeNode
4-
*
3+
* @typedef {import('unist').Node} UnistNode
4+
*/
5+
6+
/**
57
* @typedef Options
6-
* Configuration (optional).
8+
* Configuration.
79
* @property {boolean | null | undefined} [dirty=false]
8-
* Leave discouraged fields in the tree.
10+
* Leave discouraged fields in the tree (default: `false`).
911
*/
1012

11-
import {positionFromEstree} from 'unist-util-position-from-estree'
13+
import {ok as assert} from 'devlop'
1214
import {visit} from 'estree-util-visit'
15+
import {positionFromEstree} from 'unist-util-position-from-estree'
1316

1417
const own = {}.hasOwnProperty
1518

@@ -37,7 +40,7 @@ export function fromEstree(estree, options = {}) {
3740
? parent
3841
: // @ts-expect-error: indexable.
3942
parent[field]
40-
/** @type {string | number | undefined} */
43+
/** @type {number | string | undefined} */
4144
const prop = index === undefined ? field : index
4245
/** @type {UnistNode} */
4346
const copy = {}
@@ -60,8 +63,7 @@ export function fromEstree(estree, options = {}) {
6063
continue
6164
}
6265

63-
/** @type {unknown} */
64-
// @ts-expect-error: indexable.
66+
indexable(node)
6567
let value = node[key]
6668

6769
// If this is a bigint or regex literal, reset value.
@@ -90,7 +92,7 @@ export function fromEstree(estree, options = {}) {
9092
}
9193
}
9294

93-
// @ts-expect-error: indexable.
95+
indexable(copy)
9496
copy[key] = value
9597
}
9698
}
@@ -100,12 +102,27 @@ export function fromEstree(estree, options = {}) {
100102
if (prop === undefined) {
101103
tail = copy
102104
} else {
103-
// @ts-expect-error: indexable.
105+
indexable(context)
104106
context[prop] = copy
105107
}
106108
}
107109
})
108110

109-
// @ts-expect-error: always one node.
111+
assert(tail, 'expected a node')
110112
return tail
111113
}
114+
115+
/**
116+
* TypeScript helper to check if something is indexable (any object is
117+
* indexable in JavaScript).
118+
*
119+
* @param {unknown} value
120+
* Thing to check.
121+
* @returns {asserts value is Record<string, unknown>}
122+
* Nothing.
123+
* @throws {Error}
124+
* When `value` is not an object.
125+
*/
126+
function indexable(value) {
127+
assert(value && typeof value === 'object', 'expected object')
128+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"types": "index.d.ts",
3939
"dependencies": {
4040
"@types/estree-jsx": "^1.0.0",
41+
"devlop": "^1.0.0",
4142
"estree-util-visit": "^2.0.0",
4243
"unist-util-position-from-estree": "^2.0.0"
4344
},

test.js

Lines changed: 68 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1+
/**
2+
* @typedef {import('estree-jsx').Program} EstreeProgram
3+
*/
4+
15
import assert from 'node:assert/strict'
26
import test from 'node:test'
37
import {Parser} from 'acorn'
48
import jsx from 'acorn-jsx'
59
import {fromEstree} from './index.js'
6-
import * as mod from './index.js'
710

811
const parser = Parser.extend(jsx())
912

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+
})
1627

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), {
2329
type: 'Program',
2430
body: [
2531
{
@@ -78,35 +84,35 @@ test('fromEstree', () => {
7884
start: {line: 1, column: 1, offset: 0},
7985
end: {line: 1, column: 15, offset: 14}
8086
}
81-
},
82-
'should transform'
83-
)
87+
})
88+
})
8489

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), {
9298
type: 'Literal',
9399
value: null,
94100
regex: {pattern: '(?:)', flags: ''},
95101
position: {
96102
start: {line: 1, column: 1, offset: 0},
97103
end: {line: 1, column: 7, offset: 6}
98104
}
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')
102114

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), {
110116
type: 'JSXFragment',
111117
openingFragment: {
112118
type: 'JSXOpeningFragment',
@@ -136,32 +142,36 @@ test('fromEstree', () => {
136142
start: {line: 1, column: 1, offset: 0},
137143
end: {line: 1, column: 7, offset: 6}
138144
}
139-
},
140-
'should transform jsx fragments'
141-
)
145+
})
146+
})
142147

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
153159

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)
159173

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+
})
167177
})

0 commit comments

Comments
 (0)