Skip to content

Commit 20e9416

Browse files
committed
Use ESM
1 parent 8e6ee62 commit 20e9416

File tree

7 files changed

+69
-90
lines changed

7 files changed

+69
-90
lines changed

.gitignore

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
.DS_Store
22
*.log
3-
.nyc_output/
43
coverage/
54
node_modules/
6-
hast-util-from-parse5.js
7-
hast-util-from-parse5.min.js
85
yarn.lock

.prettierignore

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
coverage/
2-
hast-util-from-parse5.js
3-
hast-util-from-parse5.min.js
42
*.html
5-
*.json
63
*.md

index.js

+15-18
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
'use strict'
2-
3-
var s = require('hastscript/svg')
4-
var h = require('hastscript')
5-
var find = require('property-information/find')
6-
var html = require('property-information/html')
7-
var svg = require('property-information/svg')
8-
var vfileLocation = require('vfile-location')
9-
var ns = require('web-namespaces')
10-
11-
module.exports = wrapper
1+
import {h, s} from 'hastscript'
2+
import {html, svg, find} from 'property-information'
3+
import vfileLocation from 'vfile-location'
4+
import {webNamespaces} from 'web-namespaces'
125

136
var own = {}.hasOwnProperty
147

@@ -22,7 +15,7 @@ var map = {
2215
}
2316

2417
// Wrapper to normalise options.
25-
function wrapper(ast, options) {
18+
export function fromParse5(ast, options) {
2619
var settings = options || {}
2720
var file
2821

@@ -35,7 +28,7 @@ function wrapper(ast, options) {
3528

3629
return transform(ast, {
3730
schema: settings.space === 'svg' ? svg : html,
38-
file: file,
31+
file,
3932
verbose: settings.verbose
4033
})
4134
}
@@ -49,7 +42,7 @@ function transform(ast, config) {
4942
var position
5043

5144
if (fn === element) {
52-
config.schema = ast.namespaceURI === ns.svg ? svg : html
45+
config.schema = ast.namespaceURI === webNamespaces.svg ? svg : html
5346
}
5447

5548
if (ast.childNodes) {
@@ -89,7 +82,7 @@ function nodes(children, config) {
8982
function root(ast, children, config) {
9083
var result = {
9184
type: 'root',
92-
children: children,
85+
children,
9386
data: {quirksMode: ast.mode === 'quirks' || ast.mode === 'limited-quirks'}
9487
}
9588
var doc
@@ -154,7 +147,7 @@ function element(ast, children, config) {
154147
result.content = transform(ast.content, config)
155148

156149
if ((start || end) && config.file) {
157-
result.content.position = {start: start, end: end}
150+
result.content.position = {start, end}
158151
}
159152
}
160153

@@ -181,7 +174,11 @@ function location(node, location, config) {
181174
props = {}
182175

183176
for (key in location.attrs) {
184-
props[find(config.schema, key).property] = position(location.attrs[key])
177+
if (own.call(location.attrs, key)) {
178+
props[find(config.schema, key).property] = position(
179+
location.attrs[key]
180+
)
181+
}
185182
}
186183

187184
node.data = {
@@ -208,7 +205,7 @@ function position(loc) {
208205
column: loc.endCol,
209206
offset: loc.endOffset
210207
})
211-
return start || end ? {start: start, end: end} : null
208+
return start || end ? {start, end} : null
212209
}
213210

214211
function point(point) {

package.json

+20-34
Original file line numberDiff line numberDiff line change
@@ -23,44 +23,39 @@
2323
"contributors": [
2424
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
2525
],
26+
"sideEffects": false,
27+
"type": "module",
28+
"main": "index.js",
29+
"types": "types/index.d.ts",
2630
"files": [
27-
"index.js",
28-
"types/index.d.ts"
31+
"types/index.d.ts",
32+
"index.js"
2933
],
30-
"types": "types/index.d.ts",
3134
"dependencies": {
32-
"@types/parse5": "^5.0.0",
33-
"hastscript": "^6.0.0",
34-
"property-information": "^5.0.0",
35+
"@types/parse5": "^6.0.0",
36+
"hastscript": "^7.0.0",
37+
"property-information": "^6.0.0",
3538
"vfile": "^4.0.0",
3639
"vfile-location": "^3.2.0",
37-
"web-namespaces": "^1.0.0"
40+
"web-namespaces": "^2.0.0"
3841
},
3942
"devDependencies": {
40-
"browserify": "^17.0.0",
41-
"dtslint": "^4.0.0",
42-
"is-hidden": "^1.0.0",
43-
"not": "^0.1.0",
44-
"nyc": "^15.0.0",
43+
"c8": "^7.0.0",
44+
"is-hidden": "^2.0.0",
4545
"parse5": "^6.0.0",
4646
"prettier": "^2.0.0",
4747
"remark-cli": "^9.0.0",
4848
"remark-preset-wooorm": "^8.0.0",
4949
"tape": "^5.0.0",
50-
"tinyify": "^3.0.0",
5150
"to-vfile": "^6.0.0",
52-
"unist-util-visit": "^2.0.0",
53-
"xo": "^0.38.0"
51+
"unist-util-visit": "^3.0.0",
52+
"xo": "^0.39.0"
5453
},
5554
"scripts": {
5655
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
57-
"build-bundle": "browserify index.js -s hastUtilFromParse5 -o hast-util-from-parse5.js",
58-
"build-mangle": "browserify index.js -s hastUtilFromParse5 -o hast-util-from-parse5.min.js -p tinyify",
59-
"build": "npm run build-bundle && npm run build-mangle",
60-
"test-api": "node test",
61-
"test-coverage": "nyc --reporter lcov tape test",
62-
"test-types": "dtslint types",
63-
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
56+
"test-api": "node test/index.js",
57+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
58+
"test": "npm run format && npm run test-coverage"
6459
},
6560
"prettier": {
6661
"tabWidth": 2,
@@ -72,23 +67,14 @@
7267
},
7368
"xo": {
7469
"prettier": true,
75-
"esnext": false,
7670
"rules": {
77-
"unicorn/no-fn-reference-in-iterator": "off",
78-
"unicorn/prefer-optional-catch-binding": "off",
79-
"guard-for-in": "off"
71+
"no-var": "off",
72+
"prefer-arrow-callback": "off"
8073
},
8174
"ignores": [
82-
"hast-util-from-parse5.js",
83-
"**/*.ts"
75+
"types/"
8476
]
8577
},
86-
"nyc": {
87-
"check-coverage": true,
88-
"lines": 100,
89-
"functions": 100,
90-
"branches": 100
91-
},
9278
"remarkConfig": {
9379
"plugins": [
9480
"preset-wooorm"

readme.md

+10-4
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
@@ -30,10 +33,10 @@ Say we have the following file, `example.html`:
3033
And our script, `example.js`, looks as follows:
3134

3235
```js
33-
var vfile = require('to-vfile')
34-
var parse5 = require('parse5')
35-
var inspect = require('unist-util-inspect')
36-
var fromParse5 = require('hast-util-from-parse5')
36+
import vfile from 'to-vfile'
37+
import parse5 from 'parse5'
38+
import {inspect} from 'unist-util-inspect'
39+
import {fromParse5} from 'hast-util-from-parse5'
3740

3841
var file = vfile.readSync('example.html')
3942
var p5ast = parse5.parse(String(file), {sourceCodeLocationInfo: true})
@@ -68,6 +71,9 @@ root[2] (1:1-2:1, 0-70)
6871

6972
## API
7073

74+
This package exports the following identifiers: `fromParse5`.
75+
There is no default export.
76+
7177
### `fromParse5(ast[, file|options])`
7278

7379
Transform [Parse5’s AST][ast] to a [**hast**][hast] [*tree*][tree].

test/fixtures/attributes/index.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@
2222
"tagName": "p",
2323
"properties": {
2424
"id": "foo",
25-
"className": [
26-
"bar",
27-
"baz"
28-
],
25+
"className": ["bar", "baz"],
2926
"dataQux": "quux"
3027
},
3128
"children": [],

test/index.js

+23-24
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
'use strict'
2-
3-
var fs = require('fs')
4-
var path = require('path')
5-
var assert = require('assert')
6-
var test = require('tape')
7-
var not = require('not')
8-
var hidden = require('is-hidden')
9-
var vfile = require('to-vfile')
10-
var parse5 = require('parse5')
11-
var visit = require('unist-util-visit')
12-
var fromParse5 = require('..')
1+
import fs from 'fs'
2+
import path from 'path'
3+
import assert from 'assert'
4+
import test from 'tape'
5+
import {isHidden} from 'is-hidden'
6+
import parse5 from 'parse5'
7+
import {visit} from 'unist-util-visit'
8+
import vfile from 'to-vfile'
9+
import {fromParse5} from '../index.js'
1310

1411
var join = path.join
1512

@@ -348,11 +345,13 @@ test('hast-util-from-parse5', function (t) {
348345

349346
test('fixtures', function (t) {
350347
var base = join('test', 'fixtures')
351-
var files = fs.readdirSync(base).filter(not(hidden))
348+
var files = fs.readdirSync(base)
352349
var index = -1
353350

354351
while (++index < files.length) {
355-
each(files[index])
352+
if (!isHidden(files[index])) {
353+
each(files[index])
354+
}
356355
}
357356

358357
t.end()
@@ -366,14 +365,14 @@ test('fixtures', function (t) {
366365

367366
st.plan(4)
368367

369-
checkYesYes(st, fixture, options)
370-
checkNoYes(st, fixture, options)
371-
checkYesNo(st, fixture, options)
372-
checkNoNo(st, fixture, options)
368+
checkYesYes(st, options)
369+
checkNoYes(st, options)
370+
checkYesNo(st, options)
371+
checkNoNo(st, options)
373372
})
374373
}
375374

376-
function checkYesYes(t, fixture, options) {
375+
function checkYesYes(t, options) {
377376
var input = parse5.parse(String(options.file), {
378377
sourceCodeLocationInfo: true
379378
})
@@ -382,7 +381,7 @@ test('fixtures', function (t) {
382381

383382
try {
384383
expected = JSON.parse(fs.readFileSync(options.out))
385-
} catch (_) {
384+
} catch {
386385
// New fixture.
387386
fs.writeFileSync(options.out, JSON.stringify(actual, 0, 2) + '\n')
388387
return
@@ -392,7 +391,7 @@ test('fixtures', function (t) {
392391
t.deepEqual(actual, expected, 'p5 w/ position, hast w/ intent of position')
393392
}
394393

395-
function checkNoYes(t, fixture, options) {
394+
function checkNoYes(t, options) {
396395
var input = parse5.parse(String(options.file))
397396
var actual = fromParse5(input, {file: options.file, verbose: true})
398397
var expected = JSON.parse(fs.readFileSync(options.out))
@@ -403,7 +402,7 @@ test('fixtures', function (t) {
403402
t.deepEqual(actual, expected, 'p5 w/o position, hast w/ intent of position')
404403
}
405404

406-
function checkYesNo(t, fixture, options) {
405+
function checkYesNo(t, options) {
407406
var input = parse5.parse(String(options.file), {
408407
sourceCodeLocationInfo: true
409408
})
@@ -416,7 +415,7 @@ test('fixtures', function (t) {
416415
t.deepEqual(actual, expected, 'p5 w/ position, hast w/o intent of position')
417416
}
418417

419-
function checkNoNo(t, fixture, options) {
418+
function checkNoNo(t, options) {
420419
var input = parse5.parse(String(options.file))
421420
var actual = fromParse5(input)
422421
var expected = JSON.parse(fs.readFileSync(options.out))
@@ -452,7 +451,7 @@ function cleaner(node) {
452451
function log(label, actual, expected) {
453452
try {
454453
assert.deepStrictEqual(actual, expected, label)
455-
} catch (_) {
454+
} catch {
456455
console.log('actual:%s:', label)
457456
console.dir(actual, {depth: null})
458457
console.log('expected:%s:', label)

0 commit comments

Comments
 (0)