Skip to content

Commit 2041e62

Browse files
committed
Use ESM
1 parent 697da23 commit 2041e62

File tree

6 files changed

+78
-68
lines changed

6 files changed

+78
-68
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
yarn.lock

.prettierignore

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

index.js

+50-35
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
'use strict'
1+
import sax from 'sax'
2+
import Message from 'vfile-message'
23

3-
var Parser = require('sax').SAXParser
4-
var Message = require('vfile-message')
5-
6-
module.exports = fromXml
4+
var Parser = sax.SAXParser
75

86
var fromCharCode = String.fromCharCode
97

108
var search = /\r?\n|\r/g
119

12-
function fromXml(doc) {
10+
export function fromXml(doc) {
1311
var parser = new Parser(true, {position: true, strictEntities: true})
1412
var stack = [{type: 'root', children: []}]
1513
var position = now()
@@ -32,16 +30,17 @@ function fromXml(doc) {
3230

3331
function onerror(error) {
3432
var index = error.message.indexOf('\nLine')
35-
/* istanbul ignore next
36-
* - The substring should always be included, but this guards against
37-
* changes in newer sax versions */
33+
// The substring should always be included, but this guards against
34+
// changes in newer sax versions.
35+
/* c8 ignore next */
3836
fail(index === -1 ? error.message : error.message.slice(0, index), 'sax')
3937
}
4038

4139
function onsgmldeclaration() {
4240
fail('Unexpected SGML declaration', 'unexpected-sgml')
4341
}
4442

43+
// eslint-disable-next-line complexity
4544
function ondoctype(value) {
4645
var node = {type: 'doctype', name: '', public: null, system: null}
4746
var index = -1
@@ -97,24 +96,39 @@ function fromXml(doc) {
9796
// Done.
9897
} else if (isSpace(code)) {
9998
// As expected.
100-
} else if (code === 80 /* `P` */) {
101-
state = 'IN_EID'
102-
returnState = 'AFTER_PUBLIC'
103-
buffer = 'PUBLIC'
104-
bufferIndex = 0
105-
} else if (code === 83 /* `S` */) {
106-
state = 'IN_EID'
107-
returnState = 'AFTER_SYSTEM'
108-
buffer = 'SYSTEM'
109-
bufferIndex = 0
110-
} else if (code === 91 /* `[` */) {
111-
fail('Unexpected internal subset', 'doctype-internal-subset')
112-
} else {
113-
fail(
114-
'Expected external identifier (`PUBLIC` or `SYSTEM`), whitespace, or doctype end',
115-
'doctype-external-identifier'
116-
)
117-
}
99+
} else
100+
switch (code) {
101+
case 80: {
102+
state = 'IN_EID'
103+
returnState = 'AFTER_PUBLIC'
104+
buffer = 'PUBLIC'
105+
bufferIndex = 0
106+
107+
break
108+
}
109+
110+
case 83: {
111+
state = 'IN_EID'
112+
returnState = 'AFTER_SYSTEM'
113+
buffer = 'SYSTEM'
114+
bufferIndex = 0
115+
116+
break
117+
}
118+
119+
case 91: {
120+
fail('Unexpected internal subset', 'doctype-internal-subset')
121+
122+
break
123+
}
124+
125+
default: {
126+
fail(
127+
'Expected external identifier (`PUBLIC` or `SYSTEM`), whitespace, or doctype end',
128+
'doctype-external-identifier'
129+
)
130+
}
131+
}
118132

119133
break
120134
case 'IN_EID':
@@ -203,9 +217,9 @@ function fromXml(doc) {
203217

204218
break
205219
case 'IN_SYSTEM_LITERAL':
206-
/* istanbul ignore next
207-
* - Handled by SAX, but keep it to guard against changes in newer sax
208-
* versions. */
220+
// Handled by SAX, but keep it to guard against changes in newer sax
221+
// versions.
222+
/* c8 ignore next 5 */
209223
if (code === null /* EOF */) {
210224
fail(
211225
'Expected quote or apostrophe to end system literal',
@@ -232,7 +246,8 @@ function fromXml(doc) {
232246
}
233247

234248
break
235-
/* istanbul ignore next - Guard against new states */
249+
// Guard against new states.
250+
/* c8 ignore next 2 */
236251
default:
237252
throw new Error('Unhandled state `' + state + '`')
238253
}
@@ -252,7 +267,7 @@ function fromXml(doc) {
252267
}
253268

254269
function oncomment(value) {
255-
var node = {type: 'comment', value: value}
270+
var node = {type: 'comment', value}
256271

257272
// Comment has a positional bug… 😢
258273
// They end right before the last character (`>`), so let’s add that:
@@ -276,7 +291,7 @@ function fromXml(doc) {
276291
}
277292

278293
function ontext(value) {
279-
var node = {type: 'text', value: value}
294+
var node = {type: 'text', value}
280295
// Text has a positional bug… 😢
281296
// When they are added, the position is already at the next token.
282297
// So let’s reverse that.
@@ -343,7 +358,7 @@ function fromXml(doc) {
343358

344359
// See: <https://www.w3.org/TR/xml/#NT-NameStartChar>
345360
function isNameStartChar(code) {
346-
return /[:A-Z_a-z\xc0-\xd6\xd8-\xf6\xf8-\u02ff\u0370-\u037d\u037f-\u1fff\u200c\u200d\u2070-\u218f\u2c00-\u2fef\u3001-\ud7ff\uf900-\ufdcf\ufdf0-\ufffd]/.test(
361+
return /[:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]/.test(
347362
fromCharCode(code)
348363
)
349364
}
@@ -352,7 +367,7 @@ function isNameStartChar(code) {
352367
function isNameChar(code) {
353368
return (
354369
isNameStartChar(code) ||
355-
/[-.\d\xb7\u0300-\u036f\u203f\u2040]/.test(fromCharCode(code))
370+
/[-.\d\u00B7\u0300-\u036F\u203F\u2040]/.test(fromCharCode(code))
356371
)
357372
}
358373

package.json

+10-18
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
"contributors": [
2929
"Titus Wormer <[email protected]> (https://wooorm.com)"
3030
],
31+
"sideEffects": false,
32+
"type": "module",
33+
"main": "index.js",
3134
"files": [
3235
"index.js"
3336
],
@@ -36,27 +39,20 @@
3639
"vfile-message": "^2.0.0"
3740
},
3841
"devDependencies": {
39-
"is-hidden": "^1.0.0",
40-
"negate": "^1.0.0",
41-
"nyc": "^15.0.0",
42+
"c8": "^7.0.0",
43+
"is-hidden": "^2.0.0",
4244
"prettier": "^2.0.0",
4345
"remark-cli": "^9.0.0",
4446
"remark-preset-wooorm": "^8.0.0",
4547
"tape": "^5.0.0",
46-
"xo": "^0.38.0"
48+
"xo": "^0.39.0"
4749
},
4850
"scripts": {
4951
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
50-
"test-api": "node test",
51-
"test-coverage": "nyc --reporter lcov tape test/index.js",
52+
"test-api": "node test/index.js",
53+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
5254
"test": "npm run format && npm run test-coverage"
5355
},
54-
"nyc": {
55-
"check-coverage": true,
56-
"lines": 100,
57-
"functions": 100,
58-
"branches": 100
59-
},
6056
"prettier": {
6157
"tabWidth": 2,
6258
"useTabs": false,
@@ -67,14 +63,10 @@
6763
},
6864
"xo": {
6965
"prettier": true,
70-
"esnext": false,
7166
"rules": {
72-
"complexity": "off",
7367
"no-misleading-character-class": "off",
74-
"unicorn/escape-case": "off",
75-
"unicorn/no-hex-escape": "off",
76-
"unicorn/no-fn-reference-in-iterator": "off",
77-
"unicorn/prefer-optional-catch-binding": "off"
68+
"no-var": "off",
69+
"prefer-arrow-callback": "off"
7870
}
7971
},
8072
"remarkConfig": {

readme.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
## Install
1414

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

1720
```sh
@@ -33,8 +36,8 @@ Say we have the following XML file, `example.xml`:
3336
And our script, `example.js`, looks as follows:
3437

3538
```js
36-
var fs = require('fs')
37-
var fromXml = require('xast-util-from-xml')
39+
import fs from 'fs'
40+
import {fromXml} from 'xast-util-from-xml'
3841

3942
var doc = fs.readFileSync('example.xml')
4043

@@ -85,6 +88,9 @@ Now, running `node example` yields (positional info removed for brevity):
8588

8689
## API
8790

91+
This package exports the following identifiers: `fromXml`.
92+
There is no default export.
93+
8894
### `fromXml(doc)`
8995

9096
Parse XML to a **[xast][]** tree.

test/index.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
'use strict'
2-
3-
var fs = require('fs')
4-
var path = require('path')
5-
var test = require('tape')
6-
var negate = require('negate')
7-
var hidden = require('is-hidden')
8-
var fromXml = require('..')
1+
import fs from 'fs'
2+
import path from 'path'
3+
import test from 'tape'
4+
import {isHidden} from 'is-hidden'
5+
import {fromXml} from '../index.js'
96

107
var join = path.join
118

@@ -208,11 +205,13 @@ test('xast-util-from-xml', function (t) {
208205

209206
test('fixtures', function (t) {
210207
var base = join('test', 'fixtures')
211-
var files = fs.readdirSync(base).filter(negate(hidden))
208+
var files = fs.readdirSync(base)
212209
var index = -1
213210

214211
while (++index < files.length) {
215-
each(files[index])
212+
if (!isHidden(files[index])) {
213+
each(files[index])
214+
}
216215
}
217216

218217
t.end()
@@ -225,7 +224,7 @@ test('fixtures', function (t) {
225224

226225
try {
227226
expected = JSON.parse(fs.readFileSync(fp))
228-
} catch (_) {
227+
} catch {
229228
// New fixture.
230229
fs.writeFileSync(fp, JSON.stringify(actual, 0, 2) + '\n')
231230
return

0 commit comments

Comments
 (0)