Skip to content

Commit 8e00c11

Browse files
committed
Remove attributes, selfClosing from JSXOpeningFragment
1 parent e95308f commit 8e00c11

File tree

4 files changed

+60
-6
lines changed

4 files changed

+60
-6
lines changed

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/**
22
* @typedef {import('unist').Node} UnistNode
33
* @typedef {import('estree-jsx').Node} EstreeNode
4-
* @typedef {import('estree-util-visit').Visitor} Visitor
54
*
65
* @typedef Options
76
* Configuration (optional).
@@ -46,6 +45,13 @@ export function fromEstree(estree, options = {}) {
4645
key !== 'loc' &&
4746
key !== 'raw'))
4847
) {
48+
if (
49+
node.type === 'JSXOpeningFragment' &&
50+
(key === 'attributes' || key === 'selfClosing')
51+
) {
52+
continue
53+
}
54+
4955
/** @type {unknown} */
5056
// @ts-expect-error: indexable.
5157
let value = node[key]

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"devDependencies": {
4444
"@types/tape": "^4.0.0",
4545
"acorn": "^8.0.0",
46+
"acorn-jsx": "^5.0.0",
4647
"c8": "^7.0.0",
4748
"prettier": "^2.0.0",
4849
"remark-cli": "^11.0.0",

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ It:
3232
* makes sure nodes are plain JSON
3333
* adds unist positions
3434
* normalizes `.bigint`
35+
* remove `attributes`, `selfClosing` from `JSXOpeningFragment`
3536
* removes certain discouraged fields
3637

3738
## When should I use this?

test.js

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import test from 'tape'
2-
import {parse} from 'acorn'
2+
import {Parser} from 'acorn'
3+
import jsx from 'acorn-jsx'
34
import {fromEstree} from './index.js'
45

6+
const parser = Parser.extend(jsx())
7+
58
test('esast-util-from-estree', (t) => {
69
t.deepEqual(
7-
// @ts-expect-error Similar enough.
8-
fromEstree(parse('console.log(1)', {locations: true, ecmaVersion: 2021})),
10+
fromEstree(
11+
// @ts-expect-error Similar enough.
12+
parser.parse('console.log(1)', {locations: true, ecmaVersion: 2021})
13+
),
914
{
1015
type: 'Program',
1116
body: [
@@ -72,7 +77,8 @@ test('esast-util-from-estree', (t) => {
7277
t.deepEqual(
7378
fromEstree(
7479
// @ts-expect-error Hush, it’s fine.
75-
parse('/(?:)/', {locations: true, ecmaVersion: 2021}).body[0].expression
80+
parser.parse('/(?:)/', {locations: true, ecmaVersion: 2021}).body[0]
81+
.expression
7682
),
7783
{
7884
type: 'Literal',
@@ -86,6 +92,46 @@ test('esast-util-from-estree', (t) => {
8692
'should transform regexes'
8793
)
8894

95+
t.deepEqual(
96+
fromEstree(
97+
// @ts-expect-error Hush, it’s fine.
98+
parser.parse('<>b</>', {locations: true, ecmaVersion: 2021}).body[0]
99+
.expression
100+
),
101+
{
102+
type: 'JSXFragment',
103+
openingFragment: {
104+
type: 'JSXOpeningFragment',
105+
position: {
106+
start: {line: 1, column: 1, offset: 0},
107+
end: {line: 1, column: 3, offset: 2}
108+
}
109+
},
110+
closingFragment: {
111+
type: 'JSXClosingFragment',
112+
position: {
113+
start: {line: 1, column: 4, offset: 3},
114+
end: {line: 1, column: 7, offset: 6}
115+
}
116+
},
117+
children: [
118+
{
119+
type: 'JSXText',
120+
value: 'b',
121+
position: {
122+
start: {line: 1, column: 3, offset: 2},
123+
end: {line: 1, column: 4, offset: 3}
124+
}
125+
}
126+
],
127+
position: {
128+
start: {line: 1, column: 1, offset: 0},
129+
end: {line: 1, column: 7, offset: 6}
130+
}
131+
},
132+
'should transform jsx fragments'
133+
)
134+
89135
const bigInts = [
90136
['1n', 'dec'],
91137
['0X1n', 'hex, cap'],
@@ -100,7 +146,7 @@ test('esast-util-from-estree', (t) => {
100146
while (++index < bigInts.length) {
101147
const tree = fromEstree(
102148
// @ts-expect-error Hush, it’s fine.
103-
parse(bigInts[index][0], {locations: true, ecmaVersion: 2021})
149+
parser.parse(bigInts[index][0], {locations: true, ecmaVersion: 2021})
104150
)
105151

106152
t.deepEqual(

0 commit comments

Comments
 (0)