Skip to content

Commit 0c3d054

Browse files
committed
Fix up stylelint plugin
1 parent a6f2434 commit 0c3d054

File tree

5 files changed

+62
-17
lines changed

5 files changed

+62
-17
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@fixtures/stylelint3",
3+
"devDependencies": {
4+
"stylelint": "*",
5+
"stylelint-config-recommended": "*",
6+
"stylelint-plugin-logical-css": "*",
7+
"postcss-styled-syntax": "*"
8+
}
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/** @type {import('stylelint').Config} */
2+
export default {
3+
extends: [
4+
{
5+
extends: ['stylelint-config-recommended'],
6+
customSyntax: 'postcss-styled-syntax',
7+
plugins: ['stylelint-plugin-logical-css'],
8+
rules: {},
9+
},
10+
],
11+
};

packages/knip/src/plugins/stylelint/index.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { IsPluginEnabled, Plugin, ResolveConfig } from '../../types/config.
22
import { type Input, toDeferResolve } from '../../util/input.js';
33
import { toCosmiconfig } from '../../util/plugin-config.js';
44
import { hasDependency } from '../../util/plugin.js';
5-
import type { BaseStyleLintConfig, StyleLintConfig } from './types.js';
5+
import type { StyleLintConfig } from './types.js';
66

77
// https://stylelint.io/user-guide/configure/
88

@@ -14,17 +14,21 @@ const isEnabled: IsPluginEnabled = ({ dependencies }) => hasDependency(dependenc
1414

1515
const config = ['package.json', ...toCosmiconfig('stylelint')];
1616

17-
const resolve = (config: StyleLintConfig | BaseStyleLintConfig): Input[] => {
18-
const extend = config.extends ?? [];
19-
const plugins = config.plugins?.flatMap(plugin => (typeof plugin === 'string' ? [plugin] : [])) ?? [];
20-
const customSyntax: string[] = typeof config.customSyntax === 'string' ? [config.customSyntax] : [];
21-
22-
const overrideConfigs = 'overrides' in config ? config.overrides.flatMap(resolve) : [];
23-
return [...[extend, plugins, customSyntax].flat().map(toDeferResolve), ...overrideConfigs];
17+
const resolveConfig: ResolveConfig<StyleLintConfig> = async (config, options) => {
18+
const inputs: Input[] = [];
19+
const extend = Array.isArray(config.extends) ? config.extends : config.extends ? [config.extends] : [];
20+
for (const id of extend) {
21+
if (typeof id === 'string') inputs.push(toDeferResolve(id));
22+
else for (const x of await resolveConfig(id, options)) inputs.push(x);
23+
}
24+
for (const plugin of config.plugins ?? []) if (typeof plugin === 'string') inputs.push(toDeferResolve(plugin));
25+
if (typeof config.customSyntax === 'string') inputs.push(toDeferResolve(config.customSyntax));
26+
for (const override of config.overrides ?? []) {
27+
for (const input of await resolveConfig(override, options)) inputs.push(input);
28+
}
29+
return inputs;
2430
};
2531

26-
const resolveConfig: ResolveConfig<StyleLintConfig> = config => resolve(config);
27-
2832
export default {
2933
title,
3034
enablers,
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
export type BaseStyleLintConfig = {
2-
customSyntax?: unknown;
1+
interface BaseStyleLintConfig {
2+
customSyntax?: string;
33
extends?: string | string[];
4-
plugins?: unknown[];
5-
};
4+
plugins?: (string | BaseStyleLintConfig)[];
5+
}
66

7-
export type StyleLintConfig = BaseStyleLintConfig & {
8-
overrides: BaseStyleLintConfig[];
9-
};
7+
export interface StyleLintConfig extends BaseStyleLintConfig {
8+
overrides?: BaseStyleLintConfig[];
9+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { test } from 'bun:test';
2+
import assert from 'node:assert/strict';
3+
import { main } from '../../src/index.js';
4+
import { resolve } from '../../src/util/path.js';
5+
import baseArguments from '../helpers/baseArguments.js';
6+
import baseCounters from '../helpers/baseCounters.js';
7+
8+
const cwd = resolve('fixtures/plugins/stylelint3');
9+
10+
test('Find dependencies with the stylelint plugin (3)', async () => {
11+
const { counters } = await main({
12+
...baseArguments,
13+
cwd,
14+
});
15+
16+
assert.deepEqual(counters, {
17+
...baseCounters,
18+
processed: 1,
19+
total: 1,
20+
});
21+
});

0 commit comments

Comments
 (0)