Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 23, 2021
1 parent b2abdf9 commit 3506854
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 37 deletions.
27 changes: 13 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ import {position} from 'unist-util-position'
import {webNamespaces} from 'web-namespaces'
import {zwitch} from 'zwitch'

var own = {}.hasOwnProperty
const own = {}.hasOwnProperty

var one = zwitch('type', {
const one = zwitch('type', {
handlers: {root, element, text, comment, doctype},
invalid,
unknown
Expand All @@ -64,7 +64,7 @@ function unknown(value) {
* @param {Space|Options} [options]
*/
export function toXast(tree, options) {
var space = typeof options === 'string' ? options : (options || {}).space
const space = typeof options === 'string' ? options : (options || {}).space
// @ts-ignore types are wrong.
return one(tree, {schema: space === 'svg' ? svg : html, ns: null})
}
Expand Down Expand Up @@ -114,18 +114,16 @@ function doctype(node) {
*/
// eslint-disable-next-line complexity
function element(node, parentConfig) {
var props = node.properties || {}
var schema = parentConfig.schema
const props = node.properties || {}
let schema = parentConfig.schema
/** @type {XastAttributes} */
var attributes = {}
/** @type {Context} */
var config
const attributes = {}
/** @type {HastPropertyValue} */
var value
let value
/** @type {string} */
var key
let key
/** @type {Info} */
var info
let info

if (props.xmlns === webNamespaces.html) {
schema = html
Expand All @@ -137,7 +135,8 @@ function element(node, parentConfig) {
schema = svg
}

config = Object.assign({}, parentConfig, {
/** @type {Context} */
const config = Object.assign({}, parentConfig, {
schema,
ns: webNamespaces[schema.space]
})
Expand Down Expand Up @@ -202,8 +201,8 @@ function element(node, parentConfig) {
*/
function all(origin, config) {
/** @type {Array.<XastElement|XastText|XastComment|XastDoctype>} */
var result = []
var index = -1
const result = []
let index = -1

if (
config.schema === html &&
Expand Down
6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@
"trailingComma": "none"
},
"xo": {
"prettier": true,
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off"
}
"prettier": true
},
"remarkConfig": {
"plugins": [
Expand Down
12 changes: 6 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ Say we have an `example.html` file, that looks as follows:
…and our script, `example.js`, looks as follows:

```js
import fs from 'fs'
import unified from 'unified'
import parse from 'rehype-parse'
import fs from 'node:fs'
import {unified} from 'unified'
import rehypeParse from 'rehype-parse'
import {toXast} from 'hast-util-to-xast'
import {toXml} from 'xast-util-to-xml'

// Get the HTML syntax tree:
var hast = unified()
.use(parse)
const hast = unified()
.use(rehypeParse)
.parse(fs.readFileSync('example.html'))

// Turn hast to xast:
var xast = toXast(hast)
const xast = toXast(hast)

// Serialize xast:
console.log(toXml(xast))
Expand Down
24 changes: 12 additions & 12 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import s from 'hastscript/svg.js'
import x from 'xastscript'
import {toXast} from './index.js'

test('toXast', function (t) {
t.test('main', function (t) {
test('toXast', (t) => {
t.test('main', (t) => {
t.equal(typeof toXast, 'function', 'should expose a function')

t.throws(
function () {
() => {
// @ts-ignore runtime.
toXast()
},
Expand All @@ -20,7 +20,7 @@ test('toXast', function (t) {
)

t.throws(
function () {
() => {
// @ts-ignore well-known.
toXast({type: 'raw', value: '<script>alert(1)</script>'})
},
Expand Down Expand Up @@ -69,7 +69,7 @@ test('toXast', function (t) {
t.end()
})

t.test('root', function (t) {
t.test('root', (t) => {
t.deepEqual(
toXast(u('root', [h('div', 'Alpha')])),
u('root', [x('div', {xmlns: ns.html}, 'Alpha')]),
Expand All @@ -79,7 +79,7 @@ test('toXast', function (t) {
t.end()
})

t.test('text', function (t) {
t.test('text', (t) => {
t.deepEqual(
toXast(u('text', 'Alpha')),
u('text', 'Alpha'),
Expand All @@ -96,7 +96,7 @@ test('toXast', function (t) {
t.end()
})

t.test('comment', function (t) {
t.test('comment', (t) => {
t.deepEqual(
toXast(u('comment', 'Alpha')),
u('comment', 'Alpha'),
Expand All @@ -113,7 +113,7 @@ test('toXast', function (t) {
t.end()
})

t.test('doctype', function (t) {
t.test('doctype', (t) => {
t.deepEqual(
// @ts-ignore hast@next.
toXast(u('doctype')),
Expand All @@ -124,7 +124,7 @@ test('toXast', function (t) {
t.end()
})

t.test('element', function (t) {
t.test('element', (t) => {
t.deepEqual(
toXast(h('p', [h('a', 'A'), ' & ', h('b', 'B'), '.'])),
x('p', {xmlns: ns.html}, [x('a', 'A'), ' & ', x('b', 'B'), '.']),
Expand Down Expand Up @@ -167,7 +167,7 @@ test('toXast', function (t) {
t.end()
})

t.test('attributes', function (t) {
t.test('attributes', (t) => {
t.deepEqual(
toXast(u('element', {tagName: 'br'}, [])),
x('br', {xmlns: ns.html}),
Expand Down Expand Up @@ -303,7 +303,7 @@ test('toXast', function (t) {
t.end()
})

t.test('svg', function (t) {
t.test('svg', (t) => {
t.deepEqual(
toXast(
s(
Expand Down Expand Up @@ -403,7 +403,7 @@ test('toXast', function (t) {
t.end()
})

t.test('mathml', function (t) {
t.test('mathml', (t) => {
t.deepEqual(
toXast(
u('element', {tagName: 'p', properties: {}}, [
Expand Down

0 comments on commit 3506854

Please sign in to comment.