Repository: webpro/knip
Pull request #79 (2023-03-12 07:38:55)

fix: improve support of commitlint configs

webpro: I'm happy to merge this, but how did you find out all of this is supported by commitlint? I can't seem to find it in their docs, do you have a link maybe?
RebeccaStevens: https://github.com/conventional-changelog/commitlint#config
webpro: > https://github.com/conventional-changelog/commitlint#config

Sorry I missed the comment/link that was already right there 🤦 

Patch:
diff --git a/src/plugins/commitlint/README.md b/src/plugins/commitlint/README.md
index bc228d2a..87a2d284 100644
--- a/src/plugins/commitlint/README.md
+++ b/src/plugins/commitlint/README.md
@@ -12,7 +12,12 @@ or `devDependencies`:
 ```json
 {
   "commitlint": {
-    "config": ["commitlint.config.{js,ts}"]
+    "config": [
+      ".commitlintrc",
+      ".commitlintrc.{json,yaml,yml,js,cjs,ts,cts}",
+      "commitlint.config.{js,cjs,ts,cts}",
+      "package.json"
+    ]
   }
 }
 ```
diff --git a/src/plugins/commitlint/index.ts b/src/plugins/commitlint/index.ts
index d4371097..91758d2a 100644
--- a/src/plugins/commitlint/index.ts
+++ b/src/plugins/commitlint/index.ts
@@ -4,6 +4,7 @@ import { hasDependency } from '../../util/plugin.js';
 import type { IsPluginEnabledCallback, GenericPluginCallback } from '../../types/plugins.js';
 
 // https://commitlint.js.org
+// https://github.com/conventional-changelog/commitlint#config
 
 type CommitLintConfig = {
   extends: string[];
@@ -16,10 +17,17 @@ export const ENABLERS = ['@commitlint/cli'];
 
 export const isEnabled: IsPluginEnabledCallback = ({ dependencies }) => hasDependency(dependencies, ENABLERS);
 
-export const CONFIG_FILE_PATTERNS = ['commitlint.config.{js,ts}'];
-
-const findCommitLintDependencies: GenericPluginCallback = async configFilePath => {
-  const config: CommitLintConfig = await _load(configFilePath);
+export const CONFIG_FILE_PATTERNS = [
+  '.commitlintrc',
+  '.commitlintrc.{json,yaml,yml,js,cjs,ts,cts}',
+  'commitlint.config.{js,cjs,ts,cts}',
+  'package.json',
+];
+
+const findCommitLintDependencies: GenericPluginCallback = async (configFilePath, { manifest }) => {
+  const config: CommitLintConfig = configFilePath.endsWith('package.json')
+    ? manifest.commitlint
+    : await _load(configFilePath);
   return config?.extends ? [config.extends].flat() : [];
 };
 


Test patch:
diff --git a/test/fixtures/plugins/commitlint/.commitlintrc.json b/test/fixtures/plugins/commitlint/.commitlintrc.json
new file mode 100644
index 00000000..d3d7f0cd
--- /dev/null
+++ b/test/fixtures/plugins/commitlint/.commitlintrc.json
@@ -0,0 +1,1 @@
+{ "extends": ["@commitlint/config-conventional"] }
diff --git a/test/fixtures/plugins/commitlint/package.json b/test/fixtures/plugins/commitlint/package.json
new file mode 100644
index 00000000..1acf59e1
--- /dev/null
+++ b/test/fixtures/plugins/commitlint/package.json
@@ -0,0 +1,7 @@
+{
+  "commitlint": {
+    "extends": [
+      "@commitlint/config-conventional"
+    ]
+  }
+}
diff --git a/test/tests/plugins/commitlint.test.ts b/test/tests/plugins/commitlint.test.ts
index 77745f2d..a0d205a1 100644
--- a/test/tests/plugins/commitlint.test.ts
+++ b/test/tests/plugins/commitlint.test.ts
@@ -2,11 +2,25 @@ import assert from 'node:assert/strict';
 import path from 'node:path';
 import test from 'node:test';
 import * as commitlint from '../../../src/plugins/commitlint/index.js';
+import { getManifest } from '../../helpers/index.js';
 
 const cwd = path.resolve('test/fixtures/plugins/commitlint');
+const manifest = getManifest(cwd);
 
-test('Find dependencies in commitlint configuration', async () => {
+test('Find dependencies in commitlint configuration (js)', async () => {
   const configFilePath = path.join(cwd, 'commitlint.config.js');
-  const dependencies = await commitlint.findDependencies(configFilePath);
+  const dependencies = await commitlint.findDependencies(configFilePath, {});
+  assert.deepEqual(dependencies, ['@commitlint/config-conventional']);
+});
+
+test('Find dependencies in commitlint configuration (json)', async () => {
+  const configFilePath = path.join(cwd, '.commitlintrc.json');
+  const dependencies = await commitlint.findDependencies(configFilePath, {});
+  assert.deepEqual(dependencies, ['@commitlint/config-conventional']);
+});
+
+test('Find dependencies in commitlint configuration (package.json)', async () => {
+  const configFilePath = path.join(cwd, 'package.json');
+  const dependencies = await commitlint.findDependencies(configFilePath, { manifest });
   assert.deepEqual(dependencies, ['@commitlint/config-conventional']);
 });