Skip to content

Commit

Permalink
Add ConfigResult type export
Browse files Browse the repository at this point in the history
Co-authored-by: JounQin <[email protected]>

Closes GH-76.
  • Loading branch information
wooorm committed Dec 5, 2023
1 parent 992aa2f commit 2aea166
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
33 changes: 16 additions & 17 deletions lib/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<PluginTuple>} plugins
* Resolved plugins.
* @property {Settings} settings
* Resolved settings.
*
* @callback ConfigTransform
* Transform arbitrary configs to our format.
* @param {any} config
Expand Down Expand Up @@ -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<PluginTuple>} plugins
* Resolved plugins.
*
*/

import assert from 'node:assert/strict'
Expand Down Expand Up @@ -184,7 +183,7 @@ export class Configuration {
this.given = {plugins: options.plugins, settings: options.settings}
this.create = this.create.bind(this)

/** @type {FindUp<Result>} */
/** @type {FindUp<ConfigResult>} */
this.findUp = new FindUp({
create: this.create,
cwd: options.cwd,
Expand Down Expand Up @@ -228,12 +227,12 @@ export class Configuration {
* File value.
* @param {string | undefined} filePath
* File path.
* @returns {Promise<Result | undefined>}
* @returns {Promise<ConfigResult | undefined>}
* 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 =
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down
20 changes: 19 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* [`Configuration`](#configuration)
* [`Completer`](#completer)
* [`Callback`](#callback)
* [`ConfigResult`](#configresult)
* [`ConfigTransform`](#configtransform)
* [`Context`](#context)
* [`FileSet`](#fileset)
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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<PluginTuple>` from `unified`)
— resolved plugins
* `settings` ([`Settings` from `unified`][unified-settings])
— resolved settings
### `ConfigTransform`
Transform arbitrary configs to our format (TypeScript type).
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -1659,6 +1675,8 @@ abide by its terms.
[api-callback]: #callback
[api-config-result]: #configresult
[api-config-transform]: #configtransform
[api-context]: #context
Expand Down

0 comments on commit 2aea166

Please sign in to comment.