Skip to content

Commit

Permalink
Use ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed May 1, 2021
1 parent 8e6ee62 commit 20e9416
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 90 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
hast-util-from-parse5.js
hast-util-from-parse5.min.js
yarn.lock
3 changes: 0 additions & 3 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
coverage/
hast-util-from-parse5.js
hast-util-from-parse5.min.js
*.html
*.json
*.md
33 changes: 15 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
'use strict'

var s = require('hastscript/svg')
var h = require('hastscript')
var find = require('property-information/find')
var html = require('property-information/html')
var svg = require('property-information/svg')
var vfileLocation = require('vfile-location')
var ns = require('web-namespaces')

module.exports = wrapper
import {h, s} from 'hastscript'
import {html, svg, find} from 'property-information'
import vfileLocation from 'vfile-location'
import {webNamespaces} from 'web-namespaces'

var own = {}.hasOwnProperty

Expand All @@ -22,7 +15,7 @@ var map = {
}

// Wrapper to normalise options.
function wrapper(ast, options) {
export function fromParse5(ast, options) {
var settings = options || {}
var file

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

return transform(ast, {
schema: settings.space === 'svg' ? svg : html,
file: file,
file,
verbose: settings.verbose
})
}
Expand All @@ -49,7 +42,7 @@ function transform(ast, config) {
var position

if (fn === element) {
config.schema = ast.namespaceURI === ns.svg ? svg : html
config.schema = ast.namespaceURI === webNamespaces.svg ? svg : html
}

if (ast.childNodes) {
Expand Down Expand Up @@ -89,7 +82,7 @@ function nodes(children, config) {
function root(ast, children, config) {
var result = {
type: 'root',
children: children,
children,
data: {quirksMode: ast.mode === 'quirks' || ast.mode === 'limited-quirks'}
}
var doc
Expand Down Expand Up @@ -154,7 +147,7 @@ function element(ast, children, config) {
result.content = transform(ast.content, config)

if ((start || end) && config.file) {
result.content.position = {start: start, end: end}
result.content.position = {start, end}
}
}

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

for (key in location.attrs) {
props[find(config.schema, key).property] = position(location.attrs[key])
if (own.call(location.attrs, key)) {
props[find(config.schema, key).property] = position(
location.attrs[key]
)
}
}

node.data = {
Expand All @@ -208,7 +205,7 @@ function position(loc) {
column: loc.endCol,
offset: loc.endOffset
})
return start || end ? {start: start, end: end} : null
return start || end ? {start, end} : null
}

function point(point) {
Expand Down
54 changes: 20 additions & 34 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,44 +23,39 @@
"contributors": [
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "types/index.d.ts",
"files": [
"index.js",
"types/index.d.ts"
"types/index.d.ts",
"index.js"
],
"types": "types/index.d.ts",
"dependencies": {
"@types/parse5": "^5.0.0",
"hastscript": "^6.0.0",
"property-information": "^5.0.0",
"@types/parse5": "^6.0.0",
"hastscript": "^7.0.0",
"property-information": "^6.0.0",
"vfile": "^4.0.0",
"vfile-location": "^3.2.0",
"web-namespaces": "^1.0.0"
"web-namespaces": "^2.0.0"
},
"devDependencies": {
"browserify": "^17.0.0",
"dtslint": "^4.0.0",
"is-hidden": "^1.0.0",
"not": "^0.1.0",
"nyc": "^15.0.0",
"c8": "^7.0.0",
"is-hidden": "^2.0.0",
"parse5": "^6.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"tape": "^5.0.0",
"tinyify": "^3.0.0",
"to-vfile": "^6.0.0",
"unist-util-visit": "^2.0.0",
"xo": "^0.38.0"
"unist-util-visit": "^3.0.0",
"xo": "^0.39.0"
},
"scripts": {
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"build-bundle": "browserify index.js -s hastUtilFromParse5 -o hast-util-from-parse5.js",
"build-mangle": "browserify index.js -s hastUtilFromParse5 -o hast-util-from-parse5.min.js -p tinyify",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test",
"test-types": "dtslint types",
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
"test-api": "node test/index.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
"test": "npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -72,23 +67,14 @@
},
"xo": {
"prettier": true,
"esnext": false,
"rules": {
"unicorn/no-fn-reference-in-iterator": "off",
"unicorn/prefer-optional-catch-binding": "off",
"guard-for-in": "off"
"no-var": "off",
"prefer-arrow-callback": "off"
},
"ignores": [
"hast-util-from-parse5.js",
"**/*.ts"
"types/"
]
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
},
"remarkConfig": {
"plugins": [
"preset-wooorm"
Expand Down
14 changes: 10 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

## Install

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

[npm][]:

```sh
Expand All @@ -30,10 +33,10 @@ Say we have the following file, `example.html`:
And our script, `example.js`, looks as follows:

```js
var vfile = require('to-vfile')
var parse5 = require('parse5')
var inspect = require('unist-util-inspect')
var fromParse5 = require('hast-util-from-parse5')
import vfile from 'to-vfile'
import parse5 from 'parse5'
import {inspect} from 'unist-util-inspect'
import {fromParse5} from 'hast-util-from-parse5'

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

## API

This package exports the following identifiers: `fromParse5`.
There is no default export.

### `fromParse5(ast[, file|options])`

Transform [Parse5’s AST][ast] to a [**hast**][hast] [*tree*][tree].
Expand Down
5 changes: 1 addition & 4 deletions test/fixtures/attributes/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
"tagName": "p",
"properties": {
"id": "foo",
"className": [
"bar",
"baz"
],
"className": ["bar", "baz"],
"dataQux": "quux"
},
"children": [],
Expand Down
47 changes: 23 additions & 24 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
'use strict'

var fs = require('fs')
var path = require('path')
var assert = require('assert')
var test = require('tape')
var not = require('not')
var hidden = require('is-hidden')
var vfile = require('to-vfile')
var parse5 = require('parse5')
var visit = require('unist-util-visit')
var fromParse5 = require('..')
import fs from 'fs'
import path from 'path'
import assert from 'assert'
import test from 'tape'
import {isHidden} from 'is-hidden'
import parse5 from 'parse5'
import {visit} from 'unist-util-visit'
import vfile from 'to-vfile'
import {fromParse5} from '../index.js'

var join = path.join

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

test('fixtures', function (t) {
var base = join('test', 'fixtures')
var files = fs.readdirSync(base).filter(not(hidden))
var files = fs.readdirSync(base)
var index = -1

while (++index < files.length) {
each(files[index])
if (!isHidden(files[index])) {
each(files[index])
}
}

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

st.plan(4)

checkYesYes(st, fixture, options)
checkNoYes(st, fixture, options)
checkYesNo(st, fixture, options)
checkNoNo(st, fixture, options)
checkYesYes(st, options)
checkNoYes(st, options)
checkYesNo(st, options)
checkNoNo(st, options)
})
}

function checkYesYes(t, fixture, options) {
function checkYesYes(t, options) {
var input = parse5.parse(String(options.file), {
sourceCodeLocationInfo: true
})
Expand All @@ -382,7 +381,7 @@ test('fixtures', function (t) {

try {
expected = JSON.parse(fs.readFileSync(options.out))
} catch (_) {
} catch {
// New fixture.
fs.writeFileSync(options.out, JSON.stringify(actual, 0, 2) + '\n')
return
Expand All @@ -392,7 +391,7 @@ test('fixtures', function (t) {
t.deepEqual(actual, expected, 'p5 w/ position, hast w/ intent of position')
}

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

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

function checkNoNo(t, fixture, options) {
function checkNoNo(t, options) {
var input = parse5.parse(String(options.file))
var actual = fromParse5(input)
var expected = JSON.parse(fs.readFileSync(options.out))
Expand Down Expand Up @@ -452,7 +451,7 @@ function cleaner(node) {
function log(label, actual, expected) {
try {
assert.deepStrictEqual(actual, expected, label)
} catch (_) {
} catch {
console.log('actual:%s:', label)
console.dir(actual, {depth: null})
console.log('expected:%s:', label)
Expand Down

0 comments on commit 20e9416

Please sign in to comment.