From 3506854808f4d6f8f76d46ca8dcaca6a26582ef8 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Fri, 23 Jul 2021 10:02:55 +0200 Subject: [PATCH] Refactor code-style --- index.js | 27 +++++++++++++-------------- package.json | 6 +----- readme.md | 12 ++++++------ test.js | 24 ++++++++++++------------ 4 files changed, 32 insertions(+), 37 deletions(-) diff --git a/index.js b/index.js index f1f7268..c9eb1a4 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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}) } @@ -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 @@ -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] }) @@ -202,8 +201,8 @@ function element(node, parentConfig) { */ function all(origin, config) { /** @type {Array.} */ - var result = [] - var index = -1 + const result = [] + let index = -1 if ( config.schema === html && diff --git a/package.json b/package.json index 7d184ee..53cfb83 100644 --- a/package.json +++ b/package.json @@ -76,11 +76,7 @@ "trailingComma": "none" }, "xo": { - "prettier": true, - "rules": { - "no-var": "off", - "prefer-arrow-callback": "off" - } + "prettier": true }, "remarkConfig": { "plugins": [ diff --git a/readme.md b/readme.md index 3b74c3b..5afb999 100644 --- a/readme.md +++ b/readme.md @@ -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)) diff --git a/test.js b/test.js index 120ce54..cec4951 100644 --- a/test.js +++ b/test.js @@ -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() }, @@ -20,7 +20,7 @@ test('toXast', function (t) { ) t.throws( - function () { + () => { // @ts-ignore well-known. toXast({type: 'raw', value: ''}) }, @@ -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')]), @@ -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'), @@ -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'), @@ -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')), @@ -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'), '.']), @@ -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}), @@ -303,7 +303,7 @@ test('toXast', function (t) { t.end() }) - t.test('svg', function (t) { + t.test('svg', (t) => { t.deepEqual( toXast( s( @@ -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: {}}, [