Skip to content

Commit

Permalink
feat: Support Stylelint custom syntax object (#937)
Browse files Browse the repository at this point in the history
  • Loading branch information
TJNhxMZHmqGytuWT authored Feb 12, 2025
1 parent de89b7e commit 41a2bcc
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/knip/fixtures/plugins/stylelint2/.stylelintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('stylelint').Config} */
module.exports = {
customSyntax: require('postcss-less'),
};
18 changes: 18 additions & 0 deletions packages/knip/fixtures/plugins/stylelint2/.stylelintrc.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import less from 'postcss-less';

/** @type {import('stylelint').Config} */
const config = {
customSyntax: less,
extends: ['stylelint-config-standard'],
rules: {
'alpha-value-notation': 'number',
},
overrides: [
{
files: ['**/*.html'],
extends: ['stylelint-config-html/html.js', 'stylelint-config-standard'],
},
],
};

export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('stylelint').Config} */
const myExtendableConfig = {
plugins: ['stylelint-order'],
};

module.exports = myExtendableConfig;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions packages/knip/fixtures/plugins/stylelint2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@fixtures/stylelint2",
"version": "*",
"devDependencies": {
"postcss-less": "*",
"postcss-styl": "*",
"stylelint": "*",
"stylelint-config-recommended": "*",
"stylelint-config-standard": "*"
},
"dependencies": {
"stylelint-config-html": "*"
}
}
23 changes: 23 additions & 0 deletions packages/knip/fixtures/plugins/stylelint2/stylelint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const less = require('postcss-less');
const stylus = require('postcss-styl');

/** @type {import('stylelint').Config} */
const config = {
customSyntax: less,
extends: [require.resolve('stylelint-config-recommended'), './myExtendableConfig'],
rules: {
'alpha-value-notation': 'number',
},
overrides: [
{
files: ['**/*.html'],
extends: ['stylelint-config-html/html', 'stylelint-config-standard'],
},
{
files: ['*.styl', '**/*.styl', '*.stylus', '**/*.stylus'],
customSyntax: stylus,
},
],
};

module.exports = config;
3 changes: 2 additions & 1 deletion packages/knip/src/plugins/stylelint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const config = ['package.json', ...toCosmiconfig('stylelint')];
const resolve = (config: StyleLintConfig | BaseStyleLintConfig): Input[] => {
const extend = config.extends ?? [];
const plugins = config.plugins ?? [];
const customSyntax = config.customSyntax ? [config.customSyntax] : [];
const customSyntax: string[] = typeof config.customSyntax === 'string' ? [config.customSyntax] : [];

const overrideConfigs = 'overrides' in config ? config.overrides.flatMap(resolve) : [];
return [...[extend, plugins, customSyntax].flat().map(toDeferResolve), ...overrideConfigs];
};
Expand Down
2 changes: 1 addition & 1 deletion packages/knip/src/plugins/stylelint/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type BaseStyleLintConfig = {
customSyntax?: string;
customSyntax?: unknown;
extends?: string | string[];
plugins?: string[];
};
Expand Down
21 changes: 21 additions & 0 deletions packages/knip/test/plugins/stylelint2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { test } from 'bun:test';
import assert from 'node:assert/strict';
import { main } from '../../src/index.js';
import { resolve } from '../../src/util/path.js';
import baseArguments from '../helpers/baseArguments.js';
import baseCounters from '../helpers/baseCounters.js';

const cwd = resolve('fixtures/plugins/stylelint2');

test('Find dependencies with the stylelint plugin (2)', async () => {
const { counters } = await main({
...baseArguments,
cwd,
});

assert.deepEqual(counters, {
...baseCounters,
processed: 4,
total: 4,
});
});

0 comments on commit 41a2bcc

Please sign in to comment.