Skip to content

Commit

Permalink
Update unified
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 15, 2021
1 parent 30060d3 commit 16cea75
Show file tree
Hide file tree
Showing 36 changed files with 173 additions and 279 deletions.
19 changes: 10 additions & 9 deletions lib/configuration.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/**
* @typedef {import('unified').Settings} Settings
* @typedef {import('unified').Plugin} Plugin
* @typedef {import('unified').PluginTuple} PluginTuple
* @typedef {import('unified').Plugin<unknown[]>} Plugin
* @typedef {import('unified').PluginTuple<unknown[]>} PluginTuple
* @typedef {import('unified').PluggableList} PluggableList
*
* @typedef {Record<string, unknown>} Settings
*
* @typedef {Record<string, Settings|null|undefined>} PluginIdObject
* @typedef {Array<string|[string, ...unknown[]]>} PluginIdList
*
Expand Down Expand Up @@ -205,7 +206,7 @@ export class Configuration {
async function loadScriptOrModule(_, filePath) {
// C8 bug on Node@12
/* c8 ignore next 4 */
// @ts-expect-error: Should be a config.
// @ts-expect-error: Assume it matches config.
// type-coverage:ignore-next-line
return loadFromAbsolutePath(filePath, this.cwd)
}
Expand All @@ -214,7 +215,7 @@ async function loadScriptOrModule(_, filePath) {
async function loadYaml(buf, filePath) {
// C8 bug on Node@12
/* c8 ignore next 4 */
// @ts-expect-error: Should be a config.
// @ts-expect-error: Assume it matches config.
return jsYaml.load(String(buf), {filename: path.basename(filePath)})
}

Expand All @@ -225,9 +226,10 @@ async function loadJson(buf, filePath) {

// C8 bug on Node@12
/* c8 ignore next 7 */
// @ts-expect-error: Should be a config.
// @ts-expect-error: Assume it matches config.
return path.basename(filePath) === 'package.json'
? // @ts-expect-error: `this` is the configuration context.
? // @ts-expect-error: `this` is the configuration context, TS doesn’t like
// `this` on callbacks.
// type-coverage:ignore-next-line
result[this.packageField]
: result
Expand Down Expand Up @@ -282,7 +284,7 @@ async function merge(target, raw, options) {

// Keep order sequential instead of parallel.
/* eslint-disable no-await-in-loop */
// @ts-expect-error: fine.
// @ts-expect-error: Spreading is fine.
// type-coverage:ignore-next-line
await (Array.isArray(value) ? use(...value) : use(value, undefined))
/* eslint-enable no-await-in-loop */
Expand Down Expand Up @@ -376,7 +378,6 @@ async function merge(target, raw, options) {
function addPlugin(plugin, value) {
const entry = find(target.plugins, plugin)

// To do: <https://github.com/unifiedjs/unified/pull/145>.
if (value === null) {
value = undefined
}
Expand Down
1 change: 0 additions & 1 deletion lib/file-pipeline/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ export function configure(context, file, next) {
)

try {
// @ts-expect-error: unified types are wrong.
context.processor.use(plugin, options, context.fileSet)
/* Should not happen anymore! */
/* c8 ignore next 3 */
Expand Down
3 changes: 1 addition & 2 deletions lib/file-pipeline/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ export function stringify(context, file) {
if (value === undefined || value === null) {
// Empty.
} else if (typeof value === 'string' || isBuffer(value)) {
// @ts-expect-error: fine.
// @ts-expect-error: `isBuffer` checks buffer.
file.value = value
} else {
// @ts-expect-error: non-string compiler.
file.result = value
}

Expand Down
2 changes: 1 addition & 1 deletion lib/file-pipeline/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function transform(context, file, next) {
next()
} else {
debug('Transforming document `%s`', file.path)
// @ts-expect-error: `tree` is defined.
// @ts-expect-error: `tree` is defined at this point.
context.processor.run(context.tree, file, (error, node) => {
debug('Transformed document (error: %s)', error)
context.tree = node
Expand Down
2 changes: 1 addition & 1 deletion lib/find-up.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export class FindUp {
*/
function found(error, result) {
/** @type {Callback[]} */
// @ts-expect-error: always a list.
// @ts-expect-error: always a list if found.
const cbs = self.cache[directory]
self.cache[directory] = error || result
applyAll(cbs, error || result)
Expand Down
6 changes: 2 additions & 4 deletions lib/finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ export function finder(input, options, callback) {
// Glob errors are unusual.
// other errors are on the vfile results.
/* c8 ignore next 2 */
if (error) {
if (error || !result) {
callback(error)
} else {
// @ts-expect-error: either `error` or `result` is given.
callback(null, {oneFileMode: oneFileMode(result), files: result.output})
}
})
Expand Down Expand Up @@ -124,10 +123,9 @@ function expand(input, options, next) {
function done1(error, files) {
// `search` currently does not give errors.
/* c8 ignore next 2 */
if (error) {
if (error || !files) {
next(error)
} else {
// @ts-expect-error: either `error` or `files` are given.
next(null, {input: paths, output: files})
}
}
Expand Down
6 changes: 4 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* @typedef {import('vfile').VFile} VFile
* @typedef {import('unified').Processor} Processor
* @typedef {import('./file-set.js').FileSet} FileSet
* @typedef {import('./file-set.js').Completer} Completer
* @typedef {import('./ignore.js').ResolveFrom} ResolveFrom
Expand Down Expand Up @@ -58,7 +59,7 @@
*
* @typedef Options
* Options for unified engine
* @property {import('unified').Processor<>} processor
* @property {Processor} processor
* Unified processor to transform files
* @property {string} [cwd]
* Directory to search files in, load plugins from, and more.
Expand Down Expand Up @@ -181,10 +182,11 @@ import {fileSetPipeline} from './file-set-pipeline/index.js'
export function engine(options, callback) {
/** @type {Settings} */
const settings = {}
/** @type {NodeJS.ReadStream} */
// @ts-expect-error: `PassThrough` sure is readable.
let stdin = new PassThrough()

try {
// @ts-expect-error: fine.
stdin = process.stdin
// Obscure bug in Node (seen on Windows).
// See: <https://github.com/nodejs/node/blob/f856234/lib/internal/process/stdio.js#L82>,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"tape": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"unified": "^10.0.0-beta.1",
"unified": "^10.0.0",
"vfile": "^5.0.0",
"vfile-reporter-json": "^3.0.0",
"vfile-reporter-pretty": "^6.0.0",
Expand Down
11 changes: 3 additions & 8 deletions test/completers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/**
* @typedef {import('unified').Plugin<[unknown, FileSet]>} FileSetPlugin
* @typedef {import('../index.js').FileSet} FileSet
*/

Expand Down Expand Up @@ -28,10 +29,7 @@ test('completers', (t) => {
processor: noop,
streamError: stderr.stream,
plugins: [
/**
* @param {unknown} _
* @param {FileSet} set
*/
/** @type {FileSetPlugin} */
function (_, set) {
t.equal(typeof set, 'object', 'should pass a set')
t.equal(typeof set.use, 'function', 'should have a `use` method')
Expand Down Expand Up @@ -104,10 +102,7 @@ test('completers', (t) => {
processor: noop,
streamError: stderr.stream,
plugins: [
/**
* @param {unknown} _
* @param {FileSet} set
*/
/** @type {FileSetPlugin} */
function (_, set) {
set.add('bar.text')
}
Expand Down
22 changes: 6 additions & 16 deletions test/configuration-default.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ test('`defaultConfig`', (t) => {

engine(
{
// @ts-expect-error: unified types are wrong.
processor: noop().use(addTest),
processor: noop().use(function () {
Object.assign(this, {t})
}),
streamError: stderr.stream,
cwd: path.join(fixtures, 'config-default'),
files: ['.'],
Expand All @@ -38,12 +39,6 @@ test('`defaultConfig`', (t) => {
)
}
)

function addTest() {
// Used in test.
// type-coverage:ignore-next-line
this.t = t
}
})

t.test('should use found otherwise', (t) => {
Expand All @@ -53,8 +48,9 @@ test('`defaultConfig`', (t) => {

engine(
{
// @ts-expect-error: unified types are wrong.
processor: noop().use(addTest),
processor: noop().use(function () {
Object.assign(this, {t})
}),
streamError: stderr.stream,
cwd: path.join(fixtures, 'config-default'),
files: ['.'],
Expand All @@ -70,11 +66,5 @@ test('`defaultConfig`', (t) => {
)
}
)

function addTest() {
// Used in test.
// type-coverage:ignore-next-line
this.t = t
}
})
})
33 changes: 9 additions & 24 deletions test/configuration-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ test('configuration', (t) => {

engine(
{
// @ts-expect-error: unified types are wrong.
processor: noop().use(addTest),
processor: noop().use(function () {
Object.assign(this, {t})
}),
cwd: path.join(fixtures, 'config-plugins-cascade'),
streamError: stderr.stream,
files: ['.'],
Expand All @@ -34,12 +35,6 @@ test('configuration', (t) => {
)
}
)

function addTest() {
// Used in test.
// type-coverage:ignore-next-line
this.t = t
}
})

t.test('should support an ESM plugin w/ an `.mjs` extname', (t) => {
Expand All @@ -50,8 +45,9 @@ test('configuration', (t) => {

engine(
{
// @ts-expect-error: unified types are wrong.
processor: noop().use(addTest),
processor: noop().use(function () {
Object.assign(this, {t})
}),
cwd: path.join(fixtures, 'config-plugins-esm-mjs'),
streamError: stderr.stream,
files: ['one.txt'],
Expand All @@ -65,12 +61,6 @@ test('configuration', (t) => {
)
}
)

function addTest() {
// Used in test.
// type-coverage:ignore-next-line
this.t = t
}
})

t.test('should support an ESM plugin w/ a `.js` extname', (t) => {
Expand All @@ -81,8 +71,9 @@ test('configuration', (t) => {

engine(
{
// @ts-expect-error: unified types are wrong.
processor: noop().use(addTest),
processor: noop().use(function () {
Object.assign(this, {t})
}),
cwd: path.join(fixtures, 'config-plugins-esm-js'),
streamError: stderr.stream,
files: ['one.txt'],
Expand All @@ -96,12 +87,6 @@ test('configuration', (t) => {
)
}
)

function addTest() {
// Used in test.
// type-coverage:ignore-next-line
this.t = t
}
})

t.test('should handle failing plugins', (t) => {
Expand Down
Loading

0 comments on commit 16cea75

Please sign in to comment.