Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin-commitlint): handling of parserPreset when it's not a string #632

Merged
merged 2 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/knip/fixtures/plugins/commitlint/.commitlintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"extends": ["@commitlint/config-conventional"],
"plugins": ["commitlint-plugin-tense"]
"plugins": ["commitlint-plugin-tense"],
"parserPreset": "conventional-changelog-atom"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module.exports = {
extends: ['lerna', '@commitlint/config-conventional'],
parserPreset: 'conventional-changelog-atom',
parserPreset: {
parserOpts: {
headerPattern: /^(\w*)(?:\((.*)\))?!?: (.*)$/u,
},
},
formatter: '@commitlint/format',
plugins: [
{
Expand Down
13 changes: 10 additions & 3 deletions packages/knip/src/plugins/commitlint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,23 @@ const config = [
'package.json',
];

const resolveConfig: ResolveConfig<CommitLintConfig> = config => {
const resolveConfig: ResolveConfig<CommitLintConfig> = async config => {
const extendsConfigs = config.extends
? [config.extends]
.flat()
.map(id => (id.startsWith('@') || id.startsWith('commitlint-config-') ? id : `commitlint-config-${id}`))
: [];
const plugins = config.plugins ? [config.plugins].flat().filter(s => typeof s === 'string') : [];
const formatter = config.formatter ? [config.formatter] : [];
const parserPreset = config.parserPreset ? [config.parserPreset] : [];
return [...extendsConfigs, ...plugins, ...formatter, ...parserPreset];
const parserPreset = await config.parserPreset;
const parserPresetPaths: string[] = parserPreset
? typeof parserPreset === 'string'
? [parserPreset]
: parserPreset.path
? [parserPreset.path ?? parserPreset]
: []
: [];
return [...extendsConfigs, ...plugins, ...formatter, ...parserPresetPaths];
};

export default {
Expand Down
8 changes: 7 additions & 1 deletion packages/knip/src/plugins/commitlint/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@ export type CommitLintConfig = {
extends?: string | string[];
plugins?: string[];
formatter?: string;
parserPreset?: string;
parserPreset?: string | ParserPreset | Promise<ParserPreset>;
};

type ParserPreset = {
name?: string;
path?: string;
parserOpts?: unknown;
};
2 changes: 1 addition & 1 deletion packages/knip/test/plugins/commitlint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ test('Find dependencies with the Commitizen plugin', async () => {

assert(issues.unlisted['.commitlintrc.json']['@commitlint/config-conventional']);
assert(issues.unlisted['.commitlintrc.json']['commitlint-plugin-tense']);
assert(issues.unlisted['.commitlintrc.json']['conventional-changelog-atom']);

assert(issues.unlisted['commitlint.config.js']['@commitlint/config-conventional']);
assert(issues.unlisted['commitlint.config.js']['commitlint-plugin-tense']);
assert(issues.unlisted['commitlint.config.js']['commitlint-config-lerna']);
assert(issues.unlisted['commitlint.config.js']['conventional-changelog-atom']);
assert(issues.unlisted['commitlint.config.js']['@commitlint/format']);

assert(issues.unlisted['package.json']['@commitlint/config-conventional']);
Expand Down
Loading