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 30, 2021
1 parent da77de4 commit 4681a82
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 120 deletions.
8 changes: 0 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@
},
"xo": {
"prettier": true,
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off",
"unicorn/no-array-callback-reference": "off",
"unicorn/prefer-number-properties": "off",
"unicorn/prefer-optional-catch-binding": "off",
"unicorn/string-content": "off"
},
"ignores": [
"**/*.ts"
]
Expand Down
41 changes: 22 additions & 19 deletions packages/rehype-parse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import fromParse5 from 'hast-util-from-parse5'
import Parser5 from 'parse5/lib/parser/index.js'
import {errors} from './errors.js'

var base = 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-'
const base = 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-'

var fatalities = {2: true, 1: false, 0: null}
const fatalities = {2: true, 1: false, 0: null}

export default function rehypeParse(options) {
var settings = Object.assign({}, options, this.data('settings'))
var position = settings.position
const settings = Object.assign({}, options, this.data('settings'))
let position = settings.position

position = typeof position === 'boolean' ? position : true

this.Parser = parser

function parser(doc, file) {
var fn = settings.fragment ? 'parseFragment' : 'parse'
var onParseError = settings.emitParseErrors ? onerror : null
var parse5 = new Parser5({
const fn = settings.fragment ? 'parseFragment' : 'parse'
const onParseError = settings.emitParseErrors ? onerror : null
const parse5 = new Parser5({
sourceCodeLocationInfo: position,
onParseError,
scriptingEnabled: false
Expand All @@ -30,23 +30,23 @@ export default function rehypeParse(options) {
})

function onerror(error) {
var code = error.code
var name = camelcase(code)
var setting = settings[name]
var config = setting === undefined || setting === null ? true : setting
var level = typeof config === 'number' ? config : config ? 1 : 0
var start = {
const code = error.code
const name = camelcase(code)
const setting = settings[name]
const config = setting === undefined || setting === null ? true : setting
const level = typeof config === 'number' ? config : config ? 1 : 0
const start = {
line: error.startLine,
column: error.startCol,
offset: error.startOffset
}
var end = {
const end = {
line: error.endLine,
column: error.endCol,
offset: error.endOffset
}
var info
var message
let info
let message

if (level) {
/* c8 ignore next */
Expand All @@ -65,13 +65,16 @@ export default function rehypeParse(options) {
}

function char($0, $1) {
var offset = $1 ? -parseInt($1, 10) : 0
var char = doc.charAt(error.startOffset + offset)
const offset = $1 ? -Number.parseInt($1, 10) : 0
const char = doc.charAt(error.startOffset + offset)
return char === '`' ? '` ` `' : char
}

function encodedChar() {
var char = doc.charCodeAt(error.startOffset).toString(16).toUpperCase()
const char = doc
.charCodeAt(error.startOffset)
.toString(16)
.toUpperCase()

return '0x' + char
}
Expand Down
2 changes: 1 addition & 1 deletion packages/rehype-stringify/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import toHTML from 'hast-util-to-html'

export default function rehypeStringify(config) {
var settings = Object.assign({}, config, this.data('settings'))
const settings = Object.assign({}, config, this.data('settings'))

this.Compiler = compiler

Expand Down
66 changes: 34 additions & 32 deletions script/parse-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import {errors} from '../packages/rehype-parse/errors.js'

const pkg = JSON.parse(fs.readFileSync('package.json'))

var repo = pkg.repository
const repo = pkg.repository

var whatwg = 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-'
var base = 'https://github.com/' + repo + '/blob/main'
const whatwg =
'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-'
const base = 'https://github.com/' + repo + '/blob/main'

var ignoreFixture = {
const ignoreFixture = {
surrogateInInputStream: true
}

Expand All @@ -27,34 +28,35 @@ function transform(tree) {
function visit(start, _, end) {
return [
start,
u('list', {ordered: false, spread: false}, Object.keys(errors).map(map)),
u(
'list',
{ordered: false, spread: false},
Object.keys(errors).map((name) => {
const info = errors[name]
const kebab = name.replace(/[A-Z]/g, ($0) => '-' + $0.toLowerCase())
const reason =
info.reason.charAt(0).toLowerCase() + info.reason.slice(1)
const head = u('inlineCode', name)
const fields = [
info.url === false ? head : u('link', {url: whatwg + kebab}, [head]),
u('text', ' — ')
].concat(unified().use(parse).parse(reason).children)

if (!ignoreFixture[name]) {
fields.push(
u('text', ' ('),
u(
'link',
{url: base + '/test/parse-error/' + kebab + '/index.html'},
[u('text', 'example')]
),
u('text', ')')
)
}

return u('listItem', [u('paragraph', fields)])
})
),
end
]

function map(name) {
var info = errors[name]
var kebab = name.replace(/[A-Z]/g, replacer)
var reason = info.reason.charAt(0).toLowerCase() + info.reason.slice(1)
var head = u('inlineCode', name)
var fields = [
info.url === false ? head : u('link', {url: whatwg + kebab}, [head]),
u('text', ' — ')
].concat(unified().use(parse).parse(reason).children)

if (!ignoreFixture[name]) {
fields.push(
u('text', ' ('),
u('link', {url: base + '/test/parse-error/' + kebab + '/index.html'}, [
u('text', 'example')
]),
u('text', ')')
)
}

return u('listItem', [u('paragraph', fields)])
}

function replacer($0) {
return '-' + $0.toLowerCase()
}
}
22 changes: 11 additions & 11 deletions script/regenerate-fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@ import path from 'path'
import bail from 'bail'
import rehype from '../packages/rehype/index.js'

var join = path.join
const join = path.join

var root = join(__dirname, '..', 'test', 'fixtures')
const root = join(__dirname, '..', 'test', 'fixtures')

fs.readdir(join(root), function (error, files) {
var index = -1
fs.readdir(join(root), (error, files) => {
let index = -1

bail(error)

while (++index < files.length) {
var base = join(root, files[index])
var config
const base = join(root, files[index])
let config

if (files[index].charAt(0) === '.') {
continue
}

try {
config = JSON.parse(fs.readFileSync(join(base, 'config.json')))
} catch (_) {
} catch {
config = {}
}

fs.readFile(join(base, 'index.html'), 'utf8', function (error, doc) {
var processor = rehype().use({settings: config})
var tree = processor.parse(doc)
var result = processor.stringify(tree)
fs.readFile(join(base, 'index.html'), 'utf8', (error, doc) => {
const processor = rehype().use({settings: config})
const tree = processor.parse(doc)
const result = processor.stringify(tree)

bail(error)

Expand Down
55 changes: 27 additions & 28 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import parse from '../packages/rehype-parse/index.js'
import stringify from '../packages/rehype-stringify/index.js'
import {rehype} from '../packages/rehype/index.js'

var fragment = {fragment: true}
const fragment = {fragment: true}

test('rehype().parse(file)', function (t) {
test('rehype().parse(file)', (t) => {
t.equal(
unified().use(parse).parse('Alfred').children.length,
1,
Expand Down Expand Up @@ -68,17 +68,17 @@ test('rehype().parse(file)', function (t) {
t.end()
})

test('rehype().stringify(ast, file, options?)', function (t) {
test('rehype().stringify(ast, file, options?)', (t) => {
t.throws(
function () {
() => {
unified().use(stringify).stringify(false)
},
/false/,
'should throw when `ast` is not a node'
)

t.throws(
function () {
() => {
unified().use(stringify).stringify({type: 'unicorn'})
},
/unicorn/,
Expand Down Expand Up @@ -136,7 +136,9 @@ test('rehype().stringify(ast, file, options?)', function (t) {
)

t.deepEqual(
rehype().processSync('<!doctypehtml>').messages.map(String),
rehype()
.processSync('<!doctypehtml>')
.messages.map((d) => String(d)),
[],
'should not emit parse errors by default'
)
Expand All @@ -145,7 +147,7 @@ test('rehype().stringify(ast, file, options?)', function (t) {
rehype()
.data('settings', {emitParseErrors: true})
.processSync('<!doctypehtml>')
.messages.map(String),
.messages.map((d) => String(d)),
['1:10-1:10: Missing whitespace before doctype name'],
'should emit parse errors when `emitParseErrors: true`'
)
Expand All @@ -157,7 +159,7 @@ test('rehype().stringify(ast, file, options?)', function (t) {
missingWhitespaceBeforeDoctypeName: false
})
.processSync('<!doctypehtml>')
.messages.map(String),
.messages.map((d) => String(d)),
[],
'should ignore parse errors when the specific rule is turned off'
)
Expand All @@ -169,7 +171,7 @@ test('rehype().stringify(ast, file, options?)', function (t) {
missingWhitespaceBeforeDoctypeName: true
})
.processSync('<!doctypehtml>')
.messages.map(String),
.messages.map((d) => String(d)),
['1:10-1:10: Missing whitespace before doctype name'],
'should emit parse errors when the specific rule is turned on'
)
Expand Down Expand Up @@ -199,15 +201,14 @@ test('rehype().stringify(ast, file, options?)', function (t) {
t.end()
})

test('fixtures', function (t) {
var index = -1
var root = path.join('test', 'fixtures')
var fixtures = fs.readdirSync(root)
test('fixtures', (t) => {
let index = -1
const root = path.join('test', 'fixtures')
const fixtures = fs.readdirSync(root)

/* Check the next fixture. */
function next() {
var fixture = fixtures[++index]
var fp
const fixture = fixtures[++index]

if (!fixture) {
t.end()
Expand All @@ -219,33 +220,31 @@ test('fixtures', function (t) {
return
}

fp = path.join(root, fixture)
const fp = path.join(root, fixture)

setImmediate(next) // Queue next.

t.test(fixture, function (st) {
var file = vfile.readSync(path.join(fp, 'index.html'))
var config = {}
var tree
var node
var out
var result
t.test(fixture, (st) => {
const file = vfile.readSync(path.join(fp, 'index.html'))
let config = {}
let tree
let result

file.dirname = ''

try {
config = JSON.parse(fs.readFileSync(path.join(fp, 'config.json')))
} catch (_) {}
} catch {}

try {
result = fs.readFileSync(path.join(fp, 'result.html'), 'utf8')
} catch (_) {}
} catch {}

node = rehype().data('settings', config).parse(file)
const node = rehype().data('settings', config).parse(file)

try {
tree = JSON.parse(fs.readFileSync(path.join(fp, 'index.json')))
} catch (_) {
} catch {
fs.writeFileSync(
path.join(fp, 'index.json'),
JSON.stringify(node, 0, 2) + '\n'
Expand All @@ -257,7 +256,7 @@ test('fixtures', function (t) {

st.deepEqual(tree, node, 'should parse `' + fixture + '`')

out = rehype().data('settings', config).stringify(node)
const out = rehype().data('settings', config).stringify(node)

if (result) {
st.equal(out, result, 'should stringify `' + fixture + '`')
Expand Down

0 comments on commit 4681a82

Please sign in to comment.