Skip to content

Commit

Permalink
Fix and extend commitlint plugin (resolves #628)
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed May 10, 2024
1 parent 004ae3b commit b52118e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 16 deletions.
5 changes: 4 additions & 1 deletion packages/knip/fixtures/plugins/commitlint/.commitlintrc.json
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
{ "extends": ["@commitlint/config-conventional"], "plugins": ["commitlint-plugin-tense"] }
{
"extends": ["@commitlint/config-conventional"],
"plugins": ["commitlint-plugin-tense"]
}
18 changes: 17 additions & 1 deletion packages/knip/fixtures/plugins/commitlint/commitlint.config.js
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
module.exports = { extends: ['@commitlint/config-conventional'], plugins: ['commitlint-plugin-tense'] };
module.exports = {
extends: ['lerna', '@commitlint/config-conventional'],
parserPreset: 'conventional-changelog-atom',
formatter: '@commitlint/format',
plugins: [
{
rules: {
'contains-issue': () => {},
'dollar-sign': () => {},
},
},
'commitlint-plugin-tense',
],
rules: {
'type-enum': [2, 'always', ['oh-no']],
},
};
4 changes: 1 addition & 3 deletions packages/knip/fixtures/plugins/commitlint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"@commitlint/cli": "*"
},
"commitlint": {
"extends": [
"@commitlint/config-conventional"
],
"extends": ["@commitlint/config-conventional"],
"plugins": ["commitlint-plugin-tense"]
}
}
18 changes: 10 additions & 8 deletions packages/knip/src/plugins/commitlint/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import type { IsPluginEnabled, Plugin, ResolveConfig } from '#p/types/plugins.js';
import { hasDependency } from '#p/util/plugin.js';
import type { CommitLintConfig } from './types.js';

// https://commitlint.js.org
// https://github.com/conventional-changelog/commitlint#config

type CommitLintConfig = {
extends: string[];
plugins: string[];
};

const title = 'commitlint';

const enablers = ['@commitlint/cli'];
Expand All @@ -23,9 +19,15 @@ const config = [
];

const resolveConfig: ResolveConfig<CommitLintConfig> = config => {
const extendsConfigs = config.extends ? [config.extends].flat() : [];
const plugins = config.plugins ? [config.plugins].flat() : [];
return [...extendsConfigs, ...plugins];
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];
};

export default {
Expand Down
6 changes: 6 additions & 0 deletions packages/knip/src/plugins/commitlint/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type CommitLintConfig = {
extends?: string | string[];
plugins?: string[];
formatter?: string;
parserPreset?: string;
};
11 changes: 8 additions & 3 deletions packages/knip/test/plugins/commitlint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ test('Find dependencies with the Commitizen plugin', async () => {
});

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

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']);
assert(issues.unlisted['package.json']['commitlint-plugin-tense']);

assert.deepEqual(counters, {
...baseCounters,
devDependencies: 1,
unlisted: 6,
unlisted: 9,
processed: 1,
total: 1,
});
Expand Down

0 comments on commit b52118e

Please sign in to comment.