Skip to content

Commit 65ecfd2

Browse files
committed
Use ESM
1 parent 4db9ed2 commit 65ecfd2

File tree

9 files changed

+72
-78
lines changed

9 files changed

+72
-78
lines changed

.gitignore

-1
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
test/jsx-*.js

.prettierignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
coverage/
2-
*.json
32
*.md

index.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
'use strict'
2-
3-
module.exports = x
4-
51
// Creating xast elements.
6-
function x(name, attributes) {
2+
export function x(name, attributes) {
73
var node =
8-
name == null
4+
name === undefined || name === null
95
? {type: 'root', children: []}
10-
: {type: 'element', name: name, attributes: {}, children: []}
6+
: {type: 'element', name, attributes: {}, children: []}
117
var index = 1
128
var key
139

14-
if (name != null && typeof name !== 'string') {
10+
if (name !== undefined && name !== null && typeof name !== 'string') {
1511
throw new Error('Expected element name, got `' + name + '`')
1612
}
1713

1814
// Handle props.
1915
if (attributes) {
2016
if (
21-
name == null ||
17+
name === undefined ||
18+
name === null ||
2219
typeof attributes === 'string' ||
2320
typeof attributes === 'number' ||
2421
'length' in attributes
@@ -28,7 +25,12 @@ function x(name, attributes) {
2825
} else {
2926
for (key in attributes) {
3027
// Ignore nullish and NaN values.
31-
if (attributes[key] != null && attributes[key] === attributes[key]) {
28+
if (
29+
attributes[key] !== undefined &&
30+
attributes[key] !== null &&
31+
(typeof attributes[key] !== 'number' ||
32+
!Number.isNaN(attributes[key]))
33+
) {
3234
node.attributes[key] = String(attributes[key])
3335
}
3436
}
@@ -46,7 +48,7 @@ function x(name, attributes) {
4648
function addChild(nodes, value) {
4749
var index = -1
4850

49-
if (value == null) {
51+
if (value === undefined || value === null) {
5052
// Empty.
5153
} else if (typeof value === 'string' || typeof value === 'number') {
5254
nodes.push({type: 'text', value: String(value)})

package.json

+20-30
Original file line numberDiff line numberDiff line change
@@ -25,41 +25,38 @@
2525
"contributors": [
2626
"Titus Wormer <[email protected]> (https://wooorm.com)"
2727
],
28+
"sideEffects": false,
29+
"type": "module",
30+
"main": "index.js",
31+
"types": "types",
2832
"files": [
29-
"index.js",
30-
"types/index.d.ts"
33+
"types/index.d.ts",
34+
"index.js"
3135
],
32-
"types": "types",
3336
"dependencies": {
3437
"@types/xast": "^1.0.0"
3538
},
3639
"devDependencies": {
3740
"@babel/core": "^7.0.0",
3841
"@babel/plugin-syntax-jsx": "^7.0.0",
3942
"@babel/plugin-transform-react-jsx": "^7.0.0",
43+
"astring": "^1.0.0",
4044
"buble": "^0.20.0",
41-
"dtslint": "^4.0.0",
42-
"nyc": "^15.0.0",
45+
"c8": "^7.0.0",
46+
"estree-util-build-jsx": "^2.0.0",
4347
"prettier": "^2.0.0",
4448
"remark-cli": "^9.0.0",
4549
"remark-preset-wooorm": "^8.0.0",
4650
"tape": "^5.0.0",
47-
"unist-builder": "^2.0.0",
48-
"xo": "^0.38.0"
51+
"unist-builder": "^3.0.0",
52+
"xo": "^0.39.0"
4953
},
5054
"scripts": {
5155
"generate": "node script/generate-jsx",
5256
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
53-
"test-api": "node test",
54-
"test-coverage": "nyc --reporter lcov tape test/index.js",
55-
"test-types": "dtslint types",
56-
"test": "npm run generate && npm run format && npm run test-coverage && npm run test-types"
57-
},
58-
"nyc": {
59-
"check-coverage": true,
60-
"lines": 100,
61-
"functions": 100,
62-
"branches": 100
57+
"test-api": "node test/index.js",
58+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
59+
"test": "npm run generate && npm run format && npm run test-coverage"
6360
},
6461
"prettier": {
6562
"tabWidth": 2,
@@ -71,20 +68,13 @@
7168
},
7269
"xo": {
7370
"prettier": true,
74-
"esnext": false,
7571
"rules": {
76-
"eqeqeq": [
77-
"error",
78-
"always",
79-
{
80-
"null": "ignore"
81-
}
82-
],
83-
"guard-for-in": "off",
84-
"no-eq-null": "off",
85-
"no-self-compare": "off",
86-
"unicorn/prefer-number-properties": "off"
87-
}
72+
"no-var": "off",
73+
"prefer-arrow-callback": "off"
74+
},
75+
"ignore": [
76+
"types/"
77+
]
8878
},
8979
"remarkConfig": {
9080
"plugins": [

readme.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
## Install
1515

16+
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
17+
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
18+
1619
[npm][]:
1720

1821
```sh
@@ -22,8 +25,8 @@ npm install xastscript
2225
## Use
2326

2427
```js
25-
var u = require('unist-builder')
26-
var x = require('xastscript')
28+
import {u} from 'unist-builder'
29+
import {x} from 'xastscript'
2730

2831
// Children as an array:
2932
console.log(
@@ -188,8 +191,8 @@ If a [`Root`][root] node is given, its children are used instead.
188191
The example above (omitting the second) can then be written like so:
189192

190193
```jsx
191-
var u = require('unist-builder')
192-
var x = require('xastscript')
194+
import {u} from 'unist-builder'
195+
import {x} from 'xastscript'
193196

194197
console.log(
195198
<album id={123}>
@@ -224,9 +227,8 @@ mode), and pass `pragma: 'x'` and `pragmaFrag: 'null'`.
224227
Babel also lets you configure this in a script:
225228

226229
```jsx
227-
/** @jsx x */
228-
/** @jsxFrag null */
229-
var x = require('xastscript')
230+
/** @jsx x @jsxFrag null */
231+
import {x} from 'xastscript'
230232

231233
console.log(<music />)
232234
```
@@ -241,7 +243,7 @@ TypeScript also lets you configure this in a script:
241243
```tsx
242244
/** @jsx x */
243245
/** @jsxFrag null */
244-
import * as x from 'xastscript'
246+
import {x} from 'xastscript'
245247

246248
console.log(<music />)
247249
```

script/generate-jsx.js

+20-12
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
1-
'use strict'
2-
3-
var fs = require('fs')
4-
var path = require('path')
5-
var buble = require('buble')
6-
var babel = require('@babel/core')
1+
import fs from 'fs'
2+
import path from 'path'
3+
import babel from '@babel/core'
4+
import {Parser} from 'acorn'
5+
import acornJsx from 'acorn-jsx'
6+
import {generate} from 'astring'
7+
import {buildJsx} from 'estree-util-build-jsx'
78

89
var doc = String(fs.readFileSync(path.join('test', 'jsx.jsx')))
910

1011
fs.writeFileSync(
11-
path.join('test', 'jsx-buble.js'),
12-
buble.transform(doc.replace(/'name'/, "'jsx (buble)'"), {
13-
jsx: 'x',
14-
jsxFragment: 'null'
15-
}).code
12+
path.join('test', 'jsx-build-jsx.js'),
13+
generate(
14+
buildJsx(
15+
// @ts-ignore Acorn nodes are assignable to ESTree nodes.
16+
Parser.extend(acornJsx()).parse(
17+
doc.replace(/'name'/, "'jsx (estree-util-build-jsx, classic)'"),
18+
{sourceType: 'module', ecmaVersion: 2021}
19+
),
20+
{pragma: 'x', pragmaFrag: 'null'}
21+
)
22+
)
1623
)
1724

1825
fs.writeFileSync(
1926
path.join('test', 'jsx-babel.js'),
20-
babel.transform(doc.replace(/'name'/, "'jsx (babel)'"), {
27+
// @ts-ignore Result always given.
28+
babel.transform(doc.replace(/'name'/, "'jsx (babel, classic)'"), {
2129
plugins: [
2230
['@babel/plugin-transform-react-jsx', {pragma: 'x', pragmaFrag: 'null'}]
2331
]

test/core.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var x = require('..')
1+
import test from 'tape'
2+
import {x} from '../index.js'
53

64
test('xastscript', function (t) {
75
t.equal(typeof x, 'function', 'should expose a function')
@@ -39,7 +37,7 @@ test('xastscript', function (t) {
3937
)
4038

4139
t.deepEqual(
42-
x('y', {a: null, b: undefined, c: NaN}),
40+
x('y', {a: null, b: undefined, c: Number.NaN}),
4341
{type: 'element', name: 'y', attributes: {}, children: []},
4442
'should ignore null, undefined, and NaN attribute values'
4543
)

test/index.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict'
2-
31
/* eslint-disable import/no-unassigned-import */
4-
require('./core')
5-
require('./jsx-babel')
6-
require('./jsx-buble')
2+
import './core.js'
3+
import './jsx-babel.js'
4+
import './jsx-build-jsx.js'
75
/* eslint-enable import/no-unassigned-import */

test/jsx.jsx

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var u = require('unist-builder')
5-
var x = require('..')
1+
import test from 'tape'
2+
import {u} from 'unist-builder'
3+
import {x} from '../index.js'
64

75
test('name', function (t) {
86
t.deepEqual(<a />, x('a'), 'should support a self-closing element')

0 commit comments

Comments
 (0)