Skip to content

Commit

Permalink
Refactor Cosmiconfig types (#7543)
Browse files Browse the repository at this point in the history
Allowing the `CosmiconfigResult` type to receive a type parameter can make the code a bit type-safer
because the `any` type is no longer used.
Ref https://github.com/cosmiconfig/cosmiconfig/blob/v9.0.0/src/types.ts#L11
  • Loading branch information
ybiquitous committed Apr 19, 2024
1 parent 1fa0302 commit 9afaa08
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 21 deletions.
8 changes: 2 additions & 6 deletions lib/getConfigForFile.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@ const configurationError = require('./utils/configurationError.cjs');
const IS_TEST = process.env.NODE_ENV === 'test';
const STOP_DIR = IS_TEST ? process.cwd() : undefined;

/** @typedef {import('stylelint').InternalApi} StylelintInternalApi */
/** @typedef {import('stylelint').Config} StylelintConfig */
/** @typedef {import('stylelint').CosmiconfigResult} StylelintCosmiconfigResult */

/**
* @param {StylelintInternalApi} stylelint
* @param {import('stylelint').InternalApi} stylelint
* @param {string} [searchPath]
* @param {string} [filePath]
* @returns {Promise<StylelintCosmiconfigResult>}
* @returns {Promise<import('stylelint').CosmiconfigResult>}
*/
async function getConfigForFile(
stylelint,
Expand Down
8 changes: 2 additions & 6 deletions lib/getConfigForFile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ import configurationError from './utils/configurationError.mjs';
const IS_TEST = process.env.NODE_ENV === 'test';
const STOP_DIR = IS_TEST ? process.cwd() : undefined;

/** @typedef {import('stylelint').InternalApi} StylelintInternalApi */
/** @typedef {import('stylelint').Config} StylelintConfig */
/** @typedef {import('stylelint').CosmiconfigResult} StylelintCosmiconfigResult */

/**
* @param {StylelintInternalApi} stylelint
* @param {import('stylelint').InternalApi} stylelint
* @param {string} [searchPath]
* @param {string} [filePath]
* @returns {Promise<StylelintCosmiconfigResult>}
* @returns {Promise<import('stylelint').CosmiconfigResult>}
*/
export default async function getConfigForFile(
stylelint,
Expand Down
16 changes: 14 additions & 2 deletions lib/standalone.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,20 @@ async function postProcessStylelintResult(stylelint, stylelintResult, filePath)
return;
}

for (let postprocess of config._processorFunctions.values()) {
postprocess?.(stylelintResult, stylelintResult._postcssResult?.root);
let root = stylelintResult?._postcssResult?.root;

if (root) {
if ('type' in root) {
if (root.type !== 'root') {
root = undefined;
}
} else {
root = undefined;
}
}

for (const postprocess of config._processorFunctions.values()) {
postprocess?.(stylelintResult, root);
}
}

Expand Down
16 changes: 14 additions & 2 deletions lib/standalone.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,19 @@ async function postProcessStylelintResult(stylelint, stylelintResult, filePath)
return;
}

for (let postprocess of config._processorFunctions.values()) {
postprocess?.(stylelintResult, stylelintResult._postcssResult?.root);
let root = stylelintResult?._postcssResult?.root;

if (root) {
if ('type' in root) {
if (root.type !== 'root') {
root = undefined;
}
} else {
root = undefined;
}
}

for (const postprocess of config._processorFunctions.values()) {
postprocess?.(stylelintResult, root);
}
}
15 changes: 15 additions & 0 deletions patches/cosmiconfig+9.0.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
diff --git a/node_modules/cosmiconfig/dist/types.d.ts b/node_modules/cosmiconfig/dist/types.d.ts
index a7ddb91..f59591a 100644
--- a/node_modules/cosmiconfig/dist/types.d.ts
+++ b/node_modules/cosmiconfig/dist/types.d.ts
@@ -5,8 +5,8 @@ export type Config = any;
/**
* @public
*/
-export type CosmiconfigResult = {
- config: Config;
+export type CosmiconfigResult<T = Config> = {
+ config: T;
filepath: string;
isEmpty?: boolean;
} | null;
8 changes: 3 additions & 5 deletions types/stylelint/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as PostCSS from 'postcss';
import type { GlobbyOptions } from 'globby';
import type { cosmiconfig, TransformSync as CosmiconfigTransformSync } from 'cosmiconfig';
import type * as Cosmiconfig from 'cosmiconfig';

type ConfigExtends = string | string[];

Expand Down Expand Up @@ -129,9 +129,7 @@ declare namespace stylelint {
export type DisablePropertyName = PropertyNamesOfType<Config, DisableSettings>;

/** @internal */
export type CosmiconfigResult =
| (ReturnType<CosmiconfigTransformSync> & { config: Config })
| null;
export type CosmiconfigResult = Cosmiconfig.CosmiconfigResult<Config>;

/** @internal */
export type DisabledRange = {
Expand Down Expand Up @@ -770,7 +768,7 @@ declare namespace stylelint {
*/
export type InternalApi = {
_options: LinterOptions & { cwd: string };
_extendExplorer: ReturnType<typeof cosmiconfig>;
_extendExplorer: Cosmiconfig.PublicExplorer;
_specifiedConfigCache: Map<Config, Promise<CosmiconfigResult>>;
_postcssResultCache: Map<string, PostCSS.Result>;
_fileCache: FileCache;
Expand Down

0 comments on commit 9afaa08

Please sign in to comment.