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 20, 2021
1 parent 106f1b9 commit c1b83c9
Show file tree
Hide file tree
Showing 43 changed files with 122 additions and 129 deletions.
6 changes: 3 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ import {one} from './tree.js'
* @returns {string}
*/
export function toHtml(node, options = {}) {
var quote = options.quote || '"'
const quote = options.quote || '"'
/** @type {Quote} */
var alternative = quote === '"' ? "'" : '"'
const alternative = quote === '"' ? "'" : '"'

if (quote !== '"' && quote !== "'") {
throw new Error('Invalid quote `' + quote + '`, expected `\'` or `"`')
}

/** @type {Context} */
var context = {
const context = {
valid: options.allowParseErrors ? 0 : 1,
safe: options.allowDangerousCharacters ? 0 : 1,
schema: options.space === 'svg' ? svg : html,
Expand Down
30 changes: 15 additions & 15 deletions lib/omission/closing.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const closing = omission({
* @type {OmitHandle}
*/
function headOrColgroupOrCaption(_, index, parent) {
var next = siblingAfter(parent, index, true)
const next = siblingAfter(parent, index, true)
return !next || (!comment(next) && !whitespaceStart(next))
}

Expand All @@ -47,7 +47,7 @@ function headOrColgroupOrCaption(_, index, parent) {
* @type {OmitHandle}
*/
function html(_, index, parent) {
var next = siblingAfter(parent, index)
const next = siblingAfter(parent, index)
return !next || !comment(next)
}

Expand All @@ -57,7 +57,7 @@ function html(_, index, parent) {
* @type {OmitHandle}
*/
function body(_, index, parent) {
var next = siblingAfter(parent, index)
const next = siblingAfter(parent, index)
return !next || !comment(next)
}

Expand All @@ -67,7 +67,7 @@ function body(_, index, parent) {
* @type {OmitHandle}
*/
function p(_, index, parent) {
var next = siblingAfter(parent, index)
const next = siblingAfter(parent, index)
return next
? isElement(next, [
'address',
Expand Down Expand Up @@ -120,7 +120,7 @@ function p(_, index, parent) {
* @type {OmitHandle}
*/
function li(_, index, parent) {
var next = siblingAfter(parent, index)
const next = siblingAfter(parent, index)
return !next || isElement(next, 'li')
}

Expand All @@ -130,7 +130,7 @@ function li(_, index, parent) {
* @type {OmitHandle}
*/
function dt(_, index, parent) {
var next = siblingAfter(parent, index)
const next = siblingAfter(parent, index)
return next && isElement(next, ['dt', 'dd'])
}

Expand All @@ -140,7 +140,7 @@ function dt(_, index, parent) {
* @type {OmitHandle}
*/
function dd(_, index, parent) {
var next = siblingAfter(parent, index)
const next = siblingAfter(parent, index)
return !next || isElement(next, ['dt', 'dd'])
}

Expand All @@ -150,7 +150,7 @@ function dd(_, index, parent) {
* @type {OmitHandle}
*/
function rubyElement(_, index, parent) {
var next = siblingAfter(parent, index)
const next = siblingAfter(parent, index)
return !next || isElement(next, ['rp', 'rt'])
}

Expand All @@ -160,7 +160,7 @@ function rubyElement(_, index, parent) {
* @type {OmitHandle}
*/
function optgroup(_, index, parent) {
var next = siblingAfter(parent, index)
const next = siblingAfter(parent, index)
return !next || isElement(next, 'optgroup')
}

Expand All @@ -170,7 +170,7 @@ function optgroup(_, index, parent) {
* @type {OmitHandle}
*/
function option(_, index, parent) {
var next = siblingAfter(parent, index)
const next = siblingAfter(parent, index)
return !next || isElement(next, ['option', 'optgroup'])
}

Expand All @@ -180,7 +180,7 @@ function option(_, index, parent) {
* @type {OmitHandle}
*/
function menuitem(_, index, parent) {
var next = siblingAfter(parent, index)
const next = siblingAfter(parent, index)
return !next || isElement(next, ['menuitem', 'hr', 'menu'])
}

Expand All @@ -190,7 +190,7 @@ function menuitem(_, index, parent) {
* @type {OmitHandle}
*/
function thead(_, index, parent) {
var next = siblingAfter(parent, index)
const next = siblingAfter(parent, index)
return next && isElement(next, ['tbody', 'tfoot'])
}

Expand All @@ -200,7 +200,7 @@ function thead(_, index, parent) {
* @type {OmitHandle}
*/
function tbody(_, index, parent) {
var next = siblingAfter(parent, index)
const next = siblingAfter(parent, index)
return !next || isElement(next, ['tbody', 'tfoot'])
}

Expand All @@ -219,7 +219,7 @@ function tfoot(_, index, parent) {
* @type {OmitHandle}
*/
function tr(_, index, parent) {
var next = siblingAfter(parent, index)
const next = siblingAfter(parent, index)
return !next || isElement(next, 'tr')
}

Expand All @@ -229,6 +229,6 @@ function tr(_, index, parent) {
* @type {OmitHandle}
*/
function cells(_, index, parent) {
var next = siblingAfter(parent, index)
const next = siblingAfter(parent, index)
return !next || isElement(next, ['td', 'th'])
}
2 changes: 1 addition & 1 deletion lib/omission/omission.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @typedef {import('../types.js').OmitHandle} OmitHandle
*/

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

/**
* Factory to check if a given node can have a tag omitted.
Expand Down
20 changes: 10 additions & 10 deletions lib/omission/opening.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const opening = omission({
* @type {OmitHandle}
*/
function html(node) {
var head = siblingAfter(node, -1)
const head = siblingAfter(node, -1)
return !head || !comment(head)
}

Expand All @@ -34,12 +34,12 @@ function html(node) {
* @type {OmitHandle}
*/
function head(node) {
var children = node.children
const children = node.children
/** @type {Array.<string>} */
var seen = []
var index = -1
const seen = []
let index = -1
/** @type {Child} */
var child
let child

while (++index < children.length) {
child = children[index]
Expand All @@ -58,7 +58,7 @@ function head(node) {
* @type {OmitHandle}
*/
function body(node) {
var head = siblingAfter(node, -1, true)
const head = siblingAfter(node, -1, true)

return (
!head ||
Expand All @@ -77,8 +77,8 @@ function body(node) {
* @type {OmitHandle}
*/
function colgroup(node, index, parent) {
var previous = siblingBefore(parent, index)
var head = siblingAfter(node, -1, true)
const previous = siblingBefore(parent, index)
const head = siblingAfter(node, -1, true)

// Previous colgroup was already omitted.
if (
Expand All @@ -97,8 +97,8 @@ function colgroup(node, index, parent) {
* @type {OmitHandle}
*/
function tbody(node, index, parent) {
var previous = siblingBefore(parent, index)
var head = siblingAfter(node, -1)
const previous = siblingBefore(parent, index)
const head = siblingAfter(node, -1)

// Previous table section was already omitted.
if (
Expand Down
6 changes: 3 additions & 3 deletions lib/omission/util/siblings.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ function siblings(increment) {
* @returns {Child}
*/
function sibling(parent, index, includeWhitespace) {
var siblings = parent && parent.children
var offset = index + increment
var next = siblings && siblings[offset]
const siblings = parent && parent.children
let offset = index + increment
let next = siblings && siblings[offset]

if (!includeWhitespace) {
while (next && whitespace(next)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/omission/util/whitespace-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {whitespace} from 'hast-util-whitespace'

/** @type {import('unist-util-is').AssertPredicate<Text>} */
// @ts-ignore
var isText = convert('text')
const isText = convert('text')

/**
* Check if `node` starts with whitespace.
Expand Down
5 changes: 4 additions & 1 deletion lib/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import {stringifyEntities} from 'stringify-entities'
*/
export function text(ctx, node, _, parent) {
// Check if content of `node` should be escaped.
return parent && (parent.tagName === 'script' || parent.tagName === 'style')
return parent &&
parent.type === 'element' &&
// @ts-expect-error: hush.
(parent.tagName === 'script' || parent.tagName === 'style')
? node.value
: stringifyEntities(
node.value,
Expand Down
56 changes: 25 additions & 31 deletions lib/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import {stringify as commas} from 'comma-separated-tokens'
import {stringifyEntities} from 'stringify-entities'
import {ccount} from 'ccount'
import {constants} from './constants.js'
import {comment} from './comment'
import {doctype} from './doctype'
import {raw} from './raw'
import {text} from './text'
import {comment} from './comment.js'
import {doctype} from './doctype.js'
import {raw} from './raw.js'
import {text} from './text.js'

/**
* @type {Object.<string, Handle>}
*/
var handlers = {
const handlers = {
comment,
doctype,
element,
Expand All @@ -32,7 +32,7 @@ var handlers = {
text
}

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

/**
* @type {Handle}
Expand All @@ -57,9 +57,9 @@ export function one(ctx, node, index, parent) {
*/
export function all(ctx, parent) {
/** @type {Array.<string>} */
var results = []
var children = (parent && parent.children) || []
var index = -1
const results = []
const children = (parent && parent.children) || []
let index = -1

while (++index < children.length) {
results[index] = one(ctx, children[index], index, parent)
Expand All @@ -74,28 +74,24 @@ export function all(ctx, parent) {
*/
// eslint-disable-next-line complexity
export function element(ctx, node, index, parent) {
var schema = ctx.schema
var omit = schema.space === 'svg' ? undefined : ctx.omit
var selfClosing =
const schema = ctx.schema
const omit = schema.space === 'svg' ? undefined : ctx.omit
let selfClosing =
schema.space === 'svg'
? ctx.closeEmpty
: ctx.voids.includes(node.tagName.toLowerCase())
/** @type {Array.<string>} */
var parts = []
const parts = []
/** @type {string} */
var attrs
/** @type {string} */
var content
/** @type {string} */
var last
let last

if (schema.space === 'html' && node.tagName === 'svg') {
ctx.schema = svg
}

attrs = serializeAttributes(ctx, node.properties)
const attrs = serializeAttributes(ctx, node.properties)

content = all(
const content = all(
ctx,
schema.space === 'html' && node.tagName === 'template' ? node.content : node
)
Expand Down Expand Up @@ -143,14 +139,14 @@ export function element(ctx, node, index, parent) {
*/
function serializeAttributes(ctx, props) {
/** @type {Array.<string>} */
var values = []
var index = -1
const values = []
let index = -1
/** @type {string} */
var key
let key
/** @type {string} */
var value
let value
/** @type {string} */
var last
let last

for (key in props) {
if (props[key] !== undefined && props[key] !== null) {
Expand Down Expand Up @@ -179,12 +175,10 @@ function serializeAttributes(ctx, props) {
*/
// eslint-disable-next-line complexity
function serializeAttribute(ctx, key, value) {
var info = find(ctx.schema, key)
var quote = ctx.quote
/** @type {string} */
var result
const info = find(ctx.schema, key)
let quote = ctx.quote
/** @type {string} */
var name
let result

if (info.overloadedBoolean && (value === info.attribute || value === '')) {
value = true
Expand All @@ -204,7 +198,7 @@ function serializeAttribute(ctx, key, value) {
return ''
}

name = stringifyEntities(
const name = stringifyEntities(
info.attribute,
Object.assign({}, ctx.entities, {
// Always encode without parse errors in non-HTML.
Expand Down
8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"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"
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -76,11 +76,7 @@
"trailingComma": "none"
},
"xo": {
"prettier": true,
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off"
}
"prettier": true
},
"remarkConfig": {
"plugins": [
Expand Down
Loading

0 comments on commit c1b83c9

Please sign in to comment.