Skip to content

Commit 79cf2cf

Browse files
Fix GlobbyOptions TypeScript errors (#8992)
Co-authored-by: Masafumi Koba <[email protected]>
1 parent c93b573 commit 79cf2cf

8 files changed

Lines changed: 113 additions & 7 deletions

File tree

.changeset/nine-rockets-eat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"stylelint": patch
3+
---
4+
5+
Fixed: `GlobbyOptions` TypeScript errors

lib/__tests__/printConfig.test.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,15 @@ it('printConfig with globs should throw', async () => {
7272
}),
7373
).rejects.toThrow('The --print-config option does not support globs.');
7474
});
75+
76+
it('printConfig with URL cwd in globbyOptions', async () => {
77+
const result = await printConfig({
78+
files: ['a/b/foo.css'],
79+
globbyOptions: {
80+
cwd: new URL('./fixtures/getConfigForFile/', import.meta.url),
81+
},
82+
});
83+
84+
expect(result).toHaveProperty('rules');
85+
expect(result.rules).toHaveProperty('block-no-empty');
86+
});

lib/__tests__/standalone-globs.test.mjs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,51 @@ describe('standalone globbing', () => {
184184
);
185185
});
186186

187+
it('setting "cwd" as URL in globbyOptions', async () => {
188+
const cssGlob = `*.+(s|c)ss`;
189+
190+
const { results } = await standalone({
191+
files: cssGlob,
192+
config: {
193+
rules: {
194+
'block-no-empty': true,
195+
},
196+
},
197+
globbyOptions: {
198+
cwd: new URL('./fixtures/globs/got[braces] and (spaces)/', import.meta.url),
199+
},
200+
});
201+
202+
expect(results).toHaveLength(1);
203+
expect(results[0].errored).toBe(true);
204+
expect(results[0].warnings[0]).toEqual(
205+
expect.objectContaining({
206+
rule: 'block-no-empty',
207+
severity: 'error',
208+
}),
209+
);
210+
});
211+
212+
it('setting globbyOptions without cwd falls back to options cwd', async () => {
213+
const cssGlob = `*.+(s|c)ss`;
214+
215+
const { results } = await standalone({
216+
files: cssGlob,
217+
config: {
218+
rules: {
219+
'block-no-empty': true,
220+
},
221+
},
222+
cwd: `${fixturesPath}/got[braces] and (spaces)/`,
223+
globbyOptions: {
224+
dot: false,
225+
},
226+
});
227+
228+
expect(results).toHaveLength(1);
229+
expect(results[0].errored).toBe(true);
230+
});
231+
187232
it('setting "cwd" in options', async () => {
188233
const cssGlob = `*.+(s|c)ss`;
189234

lib/printConfig.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import process from 'node:process';
33
import { isDynamicPattern } from 'globby';
44

55
import resolveConfig from './resolveConfig.mjs';
6+
import toPath from './utils/toPath.mjs';
67

78
/** @import {Config as StylelintConfig} from 'stylelint' */
89

@@ -34,7 +35,7 @@ export default async function printConfig({
3435

3536
return (
3637
(await resolveConfig(filePath, {
37-
cwd: (globbyOptions && globbyOptions.cwd) || cwd,
38+
cwd: toPath(globbyOptions?.cwd) || cwd,
3839
config,
3940
configBasedir,
4041
configFile,

lib/standalone.mjs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import normalizeFixMode from './utils/normalizeFixMode.mjs';
3030
import prepareReturnValue from './prepareReturnValue.mjs';
3131
import resolveFilePath from './utils/resolveFilePath.mjs';
3232
import resolveOptionValue from './utils/resolveOptionValue.mjs';
33+
import toPath from './utils/toPath.mjs';
3334

3435
const ALWAYS_IGNORED_GLOBS = ['**/node_modules/**'];
3536

@@ -181,7 +182,7 @@ export default async function standalone({
181182
}
182183

183184
let fileList = [files].flat().map((entry) => {
184-
const globCWD = (globbyOptions && globbyOptions.cwd) || cwd;
185+
const globCWD = toPath(globbyOptions?.cwd) || cwd;
185186
const absolutePath = !isAbsolute(entry) ? join(globCWD, entry) : normalize(entry);
186187

187188
if (existsSync(absolutePath)) {
@@ -208,11 +209,9 @@ export default async function standalone({
208209
absolute: true,
209210
};
210211

211-
const globCWD = effectiveGlobbyOptions.cwd;
212+
const globCWD = toPath(effectiveGlobbyOptions.cwd);
212213

213-
let filePaths = (await globby(fileList, effectiveGlobbyOptions)).map((entry) =>
214-
typeof entry === 'string' ? entry : entry.path,
215-
);
214+
let filePaths = await globby(fileList, effectiveGlobbyOptions);
216215

217216
// Record the length of filePaths before ignore operation
218217
// Prevent prompting "No files matching the pattern 'xx' were found." when .stylelintignore ignore all input files
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { fileURLToPath } from 'node:url';
2+
3+
import { describe, expect, test } from '@jest/globals';
4+
5+
import toPath from '../toPath.mjs';
6+
7+
describe('toPath', () => {
8+
test('returns string as-is', () => {
9+
expect(toPath('/foo/bar')).toBe('/foo/bar');
10+
});
11+
12+
test('converts URL to string path', () => {
13+
const url = new URL(import.meta.url);
14+
15+
expect(toPath(url)).toBe(fileURLToPath(url));
16+
});
17+
18+
test('returns undefined for undefined input', () => {
19+
expect(toPath(undefined)).toBeUndefined();
20+
});
21+
});

lib/utils/toPath.mjs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { fileURLToPath } from 'node:url';
2+
3+
/**
4+
* Converts a URL to a file path string, or returns the string as-is.
5+
*
6+
* @overload
7+
* @param {string | URL} cwd
8+
* @returns {string}
9+
*
10+
* @overload
11+
* @param {undefined} cwd
12+
* @returns {undefined}
13+
*
14+
* @overload
15+
* @param {string | URL | undefined} cwd
16+
* @returns {string | undefined}
17+
*
18+
* @param {string | URL | undefined} cwd
19+
* @returns {string | undefined}
20+
*/
21+
export default function toPath(cwd) {
22+
return cwd instanceof URL ? fileURLToPath(cwd) : cwd;
23+
}

types/stylelint/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type * as PostCSS from 'postcss';
2-
import type { GlobbyOptions } from 'globby';
2+
import type { Options as GlobbyOptions } from 'globby';
33
import type { cosmiconfig, TransformSync as CosmiconfigTransformSync } from 'cosmiconfig';
44

55
type ConfigExtends = string | string[];

0 commit comments

Comments
 (0)