Skip to content

Commit 6e32815

Browse files
committed
Use ESM
1 parent 7ef089b commit 6e32815

File tree

6 files changed

+117
-119
lines changed

6 files changed

+117
-119
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
.DS_Store
22
*.log
3-
.nyc_output/
43
coverage/
54
node_modules/
65
yarn.lock

.prettierignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
coverage/
2-
*.json
32
*.md

index.js

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
'use strict'
2-
3-
module.exports = buildJsx
4-
5-
var walk = require('estree-walker').walk
6-
var isIdentifierName = require('estree-util-is-identifier-name').name
1+
import {walk} from 'estree-walker'
2+
import {name as isIdentifierName} from 'estree-util-is-identifier-name'
73

84
var regex = /@(jsx|jsxFrag|jsxImportSource|jsxRuntime)\s+(\S+)/g
95

10-
function buildJsx(tree, options) {
6+
export function buildJsx(tree, options) {
117
var settings = options || {}
128
var automatic = settings.runtime === 'automatic'
139
var annotations = {}
1410
var imports = {}
1511

16-
walk(tree, {enter: enter, leave: leave})
12+
walk(tree, {enter, leave})
1713

1814
return tree
1915

@@ -109,10 +105,10 @@ function buildJsx(tree, options) {
109105
})
110106
}
111107

112-
if (specifiers.length) {
108+
if (specifiers.length > 0) {
113109
node.body.unshift({
114110
type: 'ImportDeclaration',
115-
specifiers: specifiers,
111+
specifiers,
116112
source: {
117113
type: 'Literal',
118114
value:
@@ -184,7 +180,7 @@ function buildJsx(tree, options) {
184180
// in them and what’s spread in.
185181
while (++index < attributes.length) {
186182
if (attributes[index].type === 'JSXSpreadAttribute') {
187-
if (fields.length) {
183+
if (fields.length > 0) {
188184
objects.push({type: 'ObjectExpression', properties: fields})
189185
fields = []
190186
}
@@ -218,7 +214,7 @@ function buildJsx(tree, options) {
218214
)
219215
}
220216

221-
if (automatic && children.length) {
217+
if (automatic && children.length > 0) {
222218
fields.push({
223219
type: 'Property',
224220
key: {type: 'Identifier', name: 'children'},
@@ -232,7 +228,7 @@ function buildJsx(tree, options) {
232228
parameters = children
233229
}
234230

235-
if (fields.length) {
231+
if (fields.length > 0) {
236232
objects.push({type: 'ObjectExpression', properties: fields})
237233
}
238234

@@ -247,7 +243,7 @@ function buildJsx(tree, options) {
247243
callee: toMemberExpression('Object.assign'),
248244
arguments: objects
249245
}
250-
} else if (objects.length) {
246+
} else if (objects.length > 0) {
251247
props = objects[0]
252248
}
253249

@@ -269,7 +265,7 @@ function buildJsx(tree, options) {
269265
// Classic.
270266
else {
271267
// There are props or children.
272-
if (props || parameters.length) {
268+
if (props || parameters.length > 0) {
273269
parameters.unshift(props || {type: 'Literal', value: null})
274270
}
275271

@@ -281,11 +277,7 @@ function buildJsx(tree, options) {
281277
parameters.unshift(name)
282278

283279
this.replace(
284-
create(node, {
285-
type: 'CallExpression',
286-
callee: callee,
287-
arguments: parameters
288-
})
280+
create(node, {type: 'CallExpression', callee, arguments: parameters})
289281
)
290282
}
291283
}
@@ -312,7 +304,7 @@ function toProperty(node) {
312304
return create(node, {
313305
type: 'Property',
314306
key: toIdentifier(node.name),
315-
value: value,
307+
value,
316308
kind: 'init'
317309
})
318310
}

package.json

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,21 @@
3131
"contributors": [
3232
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
3333
],
34+
"sideEffects": false,
35+
"type": "module",
36+
"main": "index.js",
3437
"files": [
3538
"index.js"
3639
],
3740
"dependencies": {
38-
"estree-util-is-identifier-name": "^1.0.0",
39-
"estree-walker": "^2.0.0"
41+
"estree-util-is-identifier-name": "^2.0.0",
42+
"estree-walker": "^3.0.0"
4043
},
4144
"devDependencies": {
4245
"acorn": "^8.0.0",
4346
"acorn-jsx": "^5.0.0",
4447
"astring": "^1.0.0",
48+
"c8": "^7.6.0",
4549
"escodegen": "^2.0.0",
4650
"nyc": "^15.0.0",
4751
"prettier": "^2.0.0",
@@ -53,8 +57,8 @@
5357
},
5458
"scripts": {
5559
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
56-
"test-api": "node test",
57-
"test-coverage": "nyc --reporter lcov tape test.js",
60+
"test-api": "node test.js",
61+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
5862
"test": "npm run format && npm run test-coverage"
5963
},
6064
"prettier": {
@@ -67,20 +71,11 @@
6771
},
6872
"xo": {
6973
"prettier": true,
70-
"esnext": false,
7174
"rules": {
72-
"guard-for-in": "off",
73-
"max-depth": "off",
74-
"unicorn/explicit-length-check": "off",
75-
"unicorn/prefer-number-properties": "off"
75+
"no-var": "off",
76+
"prefer-arrow-callback": "off"
7677
}
7778
},
78-
"nyc": {
79-
"check-coverage": true,
80-
"lines": 100,
81-
"functions": 100,
82-
"branches": 100
83-
},
8479
"remarkConfig": {
8580
"plugins": [
8681
"preset-wooorm"

readme.md

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ So let’s make that two implementations.
1616

1717
## Install
1818

19+
This package is ESM only: Node 12+ is needed to use it and it must be `import`ed
20+
instead of `require`d.
21+
1922
[npm][]:
2023

2124
```sh
@@ -27,7 +30,7 @@ npm install estree-util-build-jsx
2730
Say we have the following file, `example.jsx`:
2831

2932
```js
30-
var x = require('xastscript')
33+
import x from 'xastscript'
3134

3235
console.log(
3336
<album id={123}>
@@ -49,25 +52,28 @@ console.log(
4952
And our script, `example.js`, looks as follows:
5053

5154
```js
52-
var fs = require('fs')
53-
var acorn = require('acorn')
54-
var jsx = require('acorn-jsx')
55-
var astring = require('astring')
56-
var build = require('estree-util-build-jsx')
55+
import fs from 'fs'
56+
import {Parser} from 'acorn'
57+
import jsx from 'acorn-jsx'
58+
import astring from 'astring'
59+
import {buildJsx} from 'estree-util-build-jsx'
5760

5861
var doc = fs.readFileSync('example.jsx')
5962

60-
var tree = acorn.Parser.extend(jsx()).parse(doc)
63+
var tree = Parser.extend(jsx()).parse(doc, {
64+
sourceType: 'module',
65+
ecmaVersion: 2020
66+
})
6167

62-
build(tree, {pragma: 'x', pragmaFrag: 'null'})
68+
buildJsx(tree, {pragma: 'x', pragmaFrag: 'null'})
6369

6470
console.log(astring.generate(tree))
6571
```
6672

6773
Now, running `node example` yields:
6874

6975
```js
70-
var x = require('xastscript');
76+
import x from 'xastscript';
7177
console.log(x("album", {
7278
id: 123
7379
}, x("name", null, "Born in the U.S.A."), x("artist", null, "Bruce Springsteen"), x("releasedate", {
@@ -82,6 +88,9 @@ console.log(x(null, null, 1 + 1, x("self-closing"), x("x", Object.assign({
8288

8389
## API
8490

91+
This package exports the following identifiers: `buildJsx`.
92+
There is no default export.
93+
8594
### `buildJsx(tree, options?)`
8695

8796
Turn JSX in `tree` ([`Program`][program]) into hyperscript calls.
@@ -125,15 +134,17 @@ This is done automatically by [`espree`][espree].
125134
For [`acorn`][acorn], it can be done like so:
126135

127136
```js
128-
var acorn = require('acorn')
129-
var jsx = require('acorn-jsx')
137+
import {Parser} from 'acorn'
138+
import jsx from 'acorn-jsx'
139+
140+
var doc = ''
130141

131142
var comments = []
132-
var tree = acorn.Parser.extend(jsx()).parse(doc, {onComment: comments})
143+
var tree = Parser.extend(jsx()).parse(doc, {onComment: comments})
133144
tree.comments = comments
134145
```
135146

136-
In almost all cases, this utility is the same as the babel plugin, except that
147+
In almost all cases, this utility is the same as the Babel plugin, except that
137148
they work on slightly different syntax trees.
138149

139150
Some differences:

0 commit comments

Comments
 (0)