Skip to content

Commit

Permalink
Refactor code style
Browse files Browse the repository at this point in the history
*   Use `TypeError`s instead of generic `Error`s where applicable
*   Use `Array.isArray` instead of `'length' in` check
*   Reword the complex `invoke` to the more clear `call` in error message
*   `const` and `let`
  • Loading branch information
wooorm committed Jun 25, 2021
1 parent dc46bc5 commit 32abf7c
Show file tree
Hide file tree
Showing 14 changed files with 324 additions and 342 deletions.
98 changes: 46 additions & 52 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {VFile} from 'vfile'
// Expose a frozen processor.
export const unified = base().freeze()

var slice = [].slice
var own = {}.hasOwnProperty
const slice = [].slice
const own = {}.hasOwnProperty

// Process pipeline.
var pipeline = trough()
const pipeline = trough()
.use(pipelineParse)
.use(pipelineRun)
.use(pipelineStringify)
Expand All @@ -36,7 +36,7 @@ function pipelineRun(p, ctx, next) {
}

function pipelineStringify(p, ctx) {
var result = p.stringify(ctx.tree, ctx.file)
const result = p.stringify(ctx.tree, ctx.file)

if (result === undefined || result === null) {
// Empty.
Expand All @@ -49,11 +49,11 @@ function pipelineStringify(p, ctx) {

// Function to create the first processor.
function base() {
var attachers = []
var transformers = trough()
var namespace = {}
var freezeIndex = -1
var frozen
const attachers = []
const transformers = trough()
let namespace = {}
let freezeIndex = -1
let frozen

// Data management.
processor.data = data
Expand All @@ -78,8 +78,8 @@ function base() {

// Create a new processor based on the processor in the current scope.
function processor() {
var destination = base()
var index = -1
const destination = base()
let index = -1

while (++index < attachers.length) {
destination.use.apply(null, attachers[index])
Expand All @@ -94,19 +94,16 @@ function base() {
//
// For example, take unified itself: it’s frozen.
// Plugins should not be added to it.
// Rather, it should be extended, by invoking it, before modifying it.
// Rather, it should be extended, by calling it, before modifying it.
//
// In essence, always invoke this when exporting a processor.
// In essence, always call this when exporting a processor.
function freeze() {
var values
var transformer

if (frozen) {
return processor
}

while (++freezeIndex < attachers.length) {
values = attachers[freezeIndex]
const values = attachers[freezeIndex]

if (values[1] === false) {
continue
Expand All @@ -116,15 +113,15 @@ function base() {
values[1] = undefined
}

transformer = values[0].apply(processor, values.slice(1))
const transformer = values[0].apply(processor, values.slice(1))

if (typeof transformer === 'function') {
transformers.use(transformer)
}
}

frozen = true
freezeIndex = Infinity
freezeIndex = Number.POSITIVE_INFINITY

return processor
}
Expand Down Expand Up @@ -163,7 +160,7 @@ function base() {
// * a list of presets, attachers, and arguments (list of attachers and
// options).
function use(value) {
var settings
let settings

assertUnfrozen('use', frozen)

Expand All @@ -172,13 +169,13 @@ function base() {
} else if (typeof value === 'function') {
addPlugin(...arguments)
} else if (typeof value === 'object') {
if ('length' in value) {
if (Array.isArray(value)) {
addList(value)
} else {
addPreset(value)
}
} else {
throw new Error('Expected usable value, not `' + value + '`')
throw new TypeError('Expected usable value, not `' + value + '`')
}

if (settings) {
Expand All @@ -199,32 +196,32 @@ function base() {
if (typeof value === 'function') {
addPlugin(value)
} else if (typeof value === 'object') {
if ('length' in value) {
if (Array.isArray(value)) {
addPlugin(...value)
} else {
addPreset(value)
}
} else {
throw new Error('Expected usable value, not `' + value + '`')
throw new TypeError('Expected usable value, not `' + value + '`')
}
}

function addList(plugins) {
var index = -1
let index = -1

if (plugins === null || plugins === undefined) {
// Empty.
} else if (typeof plugins === 'object' && 'length' in plugins) {
} else if (Array.isArray(plugins)) {
while (++index < plugins.length) {
add(plugins[index])
}
} else {
throw new Error('Expected a list of plugins, not `' + plugins + '`')
throw new TypeError('Expected a list of plugins, not `' + plugins + '`')
}
}

function addPlugin(plugin, value) {
var entry = find(plugin)
const entry = find(plugin)

if (entry) {
if (isPlainObj(entry[1]) && isPlainObj(value)) {
Expand All @@ -239,7 +236,7 @@ function base() {
}

function find(plugin) {
var index = -1
let index = -1

while (++index < attachers.length) {
if (attachers[index][0] === plugin) {
Expand All @@ -251,11 +248,9 @@ function base() {
// Parse a file (in string or vfile representation) into a unist node using
// the `Parser` on the processor.
function parse(doc) {
var file = vfile(doc)
var Parser

freeze()
Parser = processor.Parser
const file = vfile(doc)
const Parser = processor.Parser
assertParser('parse', Parser)

if (newable(Parser, 'parse')) {
Expand Down Expand Up @@ -301,8 +296,8 @@ function base() {
// Run transforms on a unist node representation of a file (in string or
// vfile representation), sync.
function runSync(node, file) {
var result
var complete
let result
let complete

run(node, file, done)

Expand All @@ -311,20 +306,18 @@ function base() {
return result

function done(error, tree) {
complete = true
result = tree
complete = true
bail(error)
}
}

// Stringify a unist node representation of a file (in string or vfile
// representation) into a string using the `Compiler` on the processor.
function stringify(node, doc) {
var file = vfile(doc)
var Compiler

freeze()
Compiler = processor.Compiler
const file = vfile(doc)
const Compiler = processor.Compiler
assertCompiler('stringify', Compiler)
assertNode(node)

Expand All @@ -351,7 +344,7 @@ function base() {
executor(null, cb)

function executor(resolve, reject) {
var file = vfile(doc)
const file = vfile(doc)

pipeline.run(processor, {file}, done)

Expand All @@ -369,13 +362,11 @@ function base() {

// Process the given document (in string or vfile representation), sync.
function processSync(doc) {
var file
var complete

freeze()
assertParser('processSync', processor.Parser)
assertCompiler('processSync', processor.Compiler)
file = vfile(doc)
let complete
const file = vfile(doc)

process(file, done)

Expand Down Expand Up @@ -404,9 +395,12 @@ function newable(value, name) {

// Check if `value` is an object with keys.
function keys(value) {
var key
let key

for (key in value) {
return true
if (own.call(value, key)) {
return true
}
}

return false
Expand All @@ -415,32 +409,32 @@ function keys(value) {
// Assert a parser is available.
function assertParser(name, Parser) {
if (typeof Parser !== 'function') {
throw new Error('Cannot `' + name + '` without `Parser`')
throw new TypeError('Cannot `' + name + '` without `Parser`')
}
}

// Assert a compiler is available.
function assertCompiler(name, Compiler) {
if (typeof Compiler !== 'function') {
throw new Error('Cannot `' + name + '` without `Compiler`')
throw new TypeError('Cannot `' + name + '` without `Compiler`')
}
}

// Assert the processor is not frozen.
function assertUnfrozen(name, frozen) {
if (frozen) {
throw new Error(
'Cannot invoke `' +
'Cannot call `' +
name +
'` on a frozen processor.\nCreate a new processor first, by invoking it: use `processor()` instead of `processor`.'
'` on a frozen processor.\nCreate a new processor first, by calling it: use `processor()` instead of `processor`.'
)
}
}

// Assert `node` is a unist node.
function assertNode(node) {
if (!node || typeof node.type !== 'string') {
throw new Error('Expected node, got `' + node + '`')
throw new TypeError('Expected node, got `' + node + '`')
}
}

Expand Down
10 changes: 0 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,6 @@
},
"xo": {
"prettier": true,
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off",
"guard-for-in": "off",
"no-unreachable-loop": "off",
"unicorn/prefer-number-properties": "off",
"unicorn/prefer-optional-catch-binding": "off",
"unicorn/prefer-reflect-apply": "off",
"unicorn/prefer-type-error": "off"
},
"ignores": [
"types/"
]
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ Yields:
throw new Error(
^
Error: Cannot invoke `use` on a frozen processor.
Error: Cannot call `use` on a frozen processor.
Create a new processor first, by invoking it: use `processor()` instead of `processor`.
at assertUnfrozen (~/node_modules/unified/index.js:440:11)
at Function.use (~/node_modules/unified/index.js:172:5)
Expand Down
10 changes: 5 additions & 5 deletions test/async-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import test from 'tape'
import {VFile} from 'vfile'
import {unified} from '../index.js'

test('async function transformer () {}', function (t) {
var givenFile = new VFile('alpha')
var givenNode = {type: 'bravo'}
var modifiedNode = {type: 'charlie'}
test('async function transformer () {}', (t) => {
const givenFile = new VFile('alpha')
const givenNode = {type: 'bravo'}
const modifiedNode = {type: 'charlie'}

t.plan(5)

unified()
.use(plugin)
.run(givenNode, givenFile, function (error, tree, file) {
.run(givenNode, givenFile, (error, tree, file) => {
t.error(error, 'should’t fail')
t.equal(tree, modifiedNode, 'passes given tree to `done`')
t.equal(file, givenFile, 'passes given file to `done`')
Expand Down
18 changes: 8 additions & 10 deletions test/core.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import test from 'tape'
import {unified} from '../index.js'

test('unified()', function (t) {
var count
var processor
var otherProcessor
test('unified()', (t) => {
let count

t.throws(
function () {
() => {
unified.use(Function.prototype)
},
/Cannot invoke `use` on a frozen processor/,
/Cannot call `use` on a frozen processor/,
'should be frozen'
)

processor = unified()
const processor = unified()

t.equal(typeof processor, 'function', 'should return a function')

Expand All @@ -24,18 +22,18 @@ test('unified()', function (t) {
})

count = 0
otherProcessor = processor().freeze()
const otherProcessor = processor().freeze()

t.equal(
count,
1,
'should create a new processor implementing the ancestral processor when invoked (#1)'
'should create a new processor implementing the ancestral processor when called (#1)'
)

t.equal(
otherProcessor.data('foo'),
'bar',
'should create a new processor implementing the ancestral processor when invoked (#2)'
'should create a new processor implementing the ancestral processor when called (#2)'
)

t.end()
Expand Down
4 changes: 2 additions & 2 deletions test/data.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import test from 'tape'
import {unified} from '../index.js'

test('data(key[, value])', function (t) {
var processor = unified()
test('data(key[, value])', (t) => {
const processor = unified()

t.equal(
processor.data('foo', 'bar'),
Expand Down
Loading

0 comments on commit 32abf7c

Please sign in to comment.