From 2aea166efe88a3836cce255ba18e58ec3cd9ba38 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Tue, 5 Dec 2023 16:10:38 +0100 Subject: [PATCH] Add `ConfigResult` type export Co-authored-by: JounQin Closes GH-76. --- .prettierignore | 2 ++ index.js | 1 + lib/configuration.js | 33 ++++++++++++++++----------------- readme.md | 20 +++++++++++++++++++- 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/.prettierignore b/.prettierignore index 9a1f0a1..b32054f 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,3 +1,5 @@ coverage/ test/fixtures/malformed-rc-yaml/.foorc.yaml +test/fixtures/malformed-package-file/one.txt +test/fixtures/malformed-package-file/package.json *.md diff --git a/index.js b/index.js index 6d9d14b..035209b 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ /** + * @typedef {import('./lib/configuration.js').ConfigResult} ConfigResult * @typedef {import('./lib/file-set.js').Completer} Completer * @typedef {import('./lib/index.js').Callback} Callback * @typedef {import('./lib/index.js').ConfigTransform} ConfigTransform diff --git a/lib/configuration.js b/lib/configuration.js index e40c711..6c800b0 100644 --- a/lib/configuration.js +++ b/lib/configuration.js @@ -10,11 +10,20 @@ * Callback called when loading a config. * @param {Error | undefined} error * Error if something happened. - * @param {Result | undefined} [result] + * @param {ConfigResult | undefined} [result] * Result. * @returns {undefined} * Nothing. * + * @typedef ConfigResult + * Resolved configuration. + * @property {string | undefined} filePath + * File path of found configuration. + * @property {Array} plugins + * Resolved plugins. + * @property {Settings} settings + * Resolved settings. + * * @callback ConfigTransform * Transform arbitrary configs to our format. * @param {any} config @@ -70,16 +79,6 @@ * List of plugins and presets (optional). * @property {Settings | undefined} [settings] * Shared settings for parsers and compilers (optional). - * - * @typedef Result - * Resolved configuration. - * @property {string | undefined} filePath - * File path of found configuration. - * @property {Settings} settings - * Resolved settings. - * @property {Array} plugins - * Resolved plugins. - * */ import assert from 'node:assert/strict' @@ -184,7 +183,7 @@ export class Configuration { this.given = {plugins: options.plugins, settings: options.settings} this.create = this.create.bind(this) - /** @type {FindUp} */ + /** @type {FindUp} */ this.findUp = new FindUp({ create: this.create, cwd: options.cwd, @@ -228,12 +227,12 @@ export class Configuration { * File value. * @param {string | undefined} filePath * File path. - * @returns {Promise} + * @returns {Promise} * Result. */ async create(buf, filePath) { const options = {cwd: this.cwd, prefix: this.pluginPrefix} - /** @type {Result} */ + /** @type {ConfigResult} */ const result = {filePath: undefined, plugins: [], settings: {}} const extname = filePath ? path.extname(filePath) : undefined const loader = @@ -283,7 +282,7 @@ export class Configuration { */ async function loadScriptOrModule(_, filePath) { // Assume it’s a config. - const result = /** @type {Result} */ ( + const result = /** @type {ConfigResult} */ ( await loadFromAbsolutePath(pathToFileURL(filePath).href, this.cwd) ) return result @@ -305,7 +304,7 @@ async function loadJson(buf, filePath) { const data = parseJson(String(buf), filePath) // Assume it’s a config. - const result = /** @type {Result} */ ( + const result = /** @type {ConfigResult} */ ( this.packageField && path.basename(filePath) === 'package.json' ? data[this.packageField] : data @@ -315,7 +314,7 @@ async function loadJson(buf, filePath) { } /** - * @param {Result} target + * @param {ConfigResult} target * Result to merge into. * @param {PresetSupportingSpecifiers} raw * Raw found config. diff --git a/readme.md b/readme.md index 1ff8025..5215d29 100644 --- a/readme.md +++ b/readme.md @@ -21,6 +21,7 @@ * [`Configuration`](#configuration) * [`Completer`](#completer) * [`Callback`](#callback) + * [`ConfigResult`](#configresult) * [`ConfigTransform`](#configtransform) * [`Context`](#context) * [`FileSet`](#fileset) @@ -181,7 +182,7 @@ Exposed to build more complex integrations. ###### Fields -* `load(string, (Error?[, Result?]): undefined): undefined` +* `load(string, (Error?[, ConfigResult?]): undefined): undefined` — get the config for a file ### `Completer` @@ -220,6 +221,20 @@ incorrect configuration), or a status code and the processing context. Nothing (`undefined`). +### `ConfigResult` + +Resolved configuration from [`Configuration`][api-configuration] (TypeScript +type). + +###### Fields + +* `filePath` (`string`) + — file path of found configuration +* `plugins` (`Array` from `unified`) + — resolved plugins +* `settings` ([`Settings` from `unified`][unified-settings]) + — resolved settings + ### `ConfigTransform` Transform arbitrary configs to our format (TypeScript type). @@ -1521,6 +1536,7 @@ This package is fully typed with [TypeScript][]. It exports the additional types [`Completer`][api-completer], [`Callback`][api-callback], +[`ConfigResult`][api-config-result], [`ConfigTransform`][api-config-transform], [`Context`][api-context], [`FileSet`][api-file-set], @@ -1659,6 +1675,8 @@ abide by its terms. [api-callback]: #callback +[api-config-result]: #configresult + [api-config-transform]: #configtransform [api-context]: #context