@syntopica/quality-config 0.13.1 → 0.13.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@syntopica/quality-config",
3
- "version": "0.13.1",
3
+ "version": "0.13.2",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "description": "Shared configuration for cross-file quality gates: knip, dependency-cruiser, type-coverage, jscpd, lefthook",
@@ -74,11 +74,11 @@
74
74
  "devDependencies": {
75
75
  "@arethetypeswrong/cli": "^0.18.5",
76
76
  "@syntopica/eslint-config": "0.9.0",
77
- "@types/node": "^26.6.4",
77
+ "@types/node": "^26.6.5",
78
78
  "dependency-cruiser": "^18.5.0",
79
79
  "eslint": "^10.12.0",
80
80
  "eslint-plugin-regexp": "^3.3.1",
81
- "knip": "^6.40.0",
81
+ "knip": "^6.41.0",
82
82
  "publint": "^0.3.25",
83
83
  "typescript": "npm:@typescript/typescript6@^6.0.2",
84
84
  "vitest": "^5.0.3"
@@ -0,0 +1,23 @@
1
+ /**
2
+ * The configuration files knip's Lefthook plugin reads, as of knip 6.41.
3
+ * With one present, the plugin reports `lefthook` as used: under `CI` it adds
4
+ * the package outright, and elsewhere it reads the installed `.git/hooks`,
5
+ * which call the `lefthook` binary.
6
+ */
7
+ export const LEFTHOOK_CONFIG_FILES: readonly string[] = [
8
+ 'lefthook.yml',
9
+ 'lefthook.yaml',
10
+ 'lefthook.json',
11
+ 'lefthook.jsonc',
12
+ 'lefthook.toml',
13
+ '.lefthook.yml',
14
+ '.lefthook.yaml',
15
+ '.lefthook.json',
16
+ '.lefthook.jsonc',
17
+ '.lefthook.toml',
18
+ '.config/lefthook.yml',
19
+ '.config/lefthook.yaml',
20
+ '.config/lefthook.json',
21
+ '.config/lefthook.jsonc',
22
+ '.config/lefthook.toml',
23
+ ]
@@ -0,0 +1,14 @@
1
+ /**
2
+ * The words after which a script name runs that script rather than a binary:
3
+ * `pnpm type-coverage`, `pnpm run type-coverage`, `npm run type-coverage`,
4
+ * `yarn type-coverage`. `exec` and `dlx` are deliberately absent, since
5
+ * `pnpm exec type-coverage` does run the binary.
6
+ */
7
+ export const SCRIPT_RUNNERS: ReadonlySet<string> = new Set([
8
+ 'pnpm',
9
+ 'yarn',
10
+ 'bun',
11
+ 'run',
12
+ 'run-s',
13
+ 'run-p',
14
+ ])
@@ -0,0 +1,11 @@
1
+ import { existsSync } from 'node:fs'
2
+ import { join } from 'node:path'
3
+
4
+ import { LEFTHOOK_CONFIG_FILES } from './LEFTHOOK_CONFIG_FILES'
5
+
6
+ /** Whether `cwd` has a configuration file knip's Lefthook plugin reads. */
7
+ export const hasLefthookConfig = (cwd: string): boolean =>
8
+ LEFTHOOK_CONFIG_FILES.some((file) =>
9
+ // eslint-disable-next-line security/detect-non-literal-fs-filename -- fixed file names under the project root
10
+ existsSync(join(cwd, file)),
11
+ )
@@ -0,0 +1,16 @@
1
+ import { SCRIPT_RUNNERS } from './SCRIPT_RUNNERS'
2
+
3
+ /**
4
+ * Whether `word` names a `package.json` script that the previous word runs.
5
+ * `"check:quality": "pnpm type-coverage"` runs the `type-coverage` script, not
6
+ * the `type-coverage` binary, and knip resolves it that way, so reading it as
7
+ * a use of the binary would leave the package neither ignored nor used.
8
+ */
9
+ export const isScriptInvocation = (
10
+ word: string,
11
+ previous: string | undefined,
12
+ scriptNames: ReadonlySet<string>,
13
+ ): boolean =>
14
+ previous !== undefined &&
15
+ SCRIPT_RUNNERS.has(previous) &&
16
+ scriptNames.has(word)
package/src/knip.ts CHANGED
@@ -7,6 +7,7 @@ import type { KnipConfiguration } from 'knip'
7
7
 
8
8
  import { existingEntries } from './existingEntries'
9
9
  import { FRAMEWORK_ENTRIES, type KnipFramework } from './knip-framework'
10
+ import { peersOfListedDependencies } from './peersOfListedDependencies'
10
11
  import { runnerDependenciesToIgnore } from './runnerDependenciesToIgnore'
11
12
 
12
13
  // Every template installs these as peer dependencies of
@@ -31,7 +32,10 @@ import { runnerDependenciesToIgnore } from './runnerDependenciesToIgnore'
31
32
  // `<package> knip.config.ts Remove from ignoreDependencies`. That is a hint,
32
33
  // not a gate failure. Do not filter the list consumer-side to silence it: the
33
34
  // filter would drift the moment this list changes, and the cost of being wrong
34
- // is a dependency that silently stops being checked.
35
+ // is a dependency that silently stops being checked. The one filter applied
36
+ // here mirrors knip's own rule: a package another installed dependency
37
+ // declares as a peer (`eslint-plugin-astro` hosts `typescript-eslint`) is
38
+ // counted as used by knip, so it is not ignored.
35
39
  const ESLINT_PEER_DEPENDENCIES = [
36
40
  '@eslint/js',
37
41
  'eslint-config-prettier',
@@ -110,6 +114,7 @@ export const createKnipConfig = (options: {
110
114
  cwd?: string
111
115
  }): KnipConfiguration => {
112
116
  const cwd = options.cwd ?? process.cwd()
117
+ const hostedPeers = peersOfListedDependencies(cwd)
113
118
  const { entry, project, includeEntryExports } =
114
119
  FRAMEWORK_ENTRIES[options.framework]
115
120
 
@@ -141,7 +146,9 @@ export const createKnipConfig = (options: {
141
146
  ignoreBinaries: ['gitleaks', ...(options.ignoreBinaries ?? [])],
142
147
  ignoreDependencies: [
143
148
  ...runnerDependenciesToIgnore(cwd),
144
- ...ESLINT_PEER_DEPENDENCIES,
149
+ ...ESLINT_PEER_DEPENDENCIES.filter(
150
+ (dependency) => !hostedPeers.has(dependency),
151
+ ),
145
152
  ...(options.ignoreDependencies ?? []),
146
153
  ],
147
154
  rules: {
@@ -0,0 +1,15 @@
1
+ import { readListedDependencies } from './readListedDependencies'
2
+ import { readPeerDependencyNames } from './readPeerDependencyNames'
3
+
4
+ /**
5
+ * Every package some dependency of `cwd` declares as a peer. knip counts a
6
+ * peer of a used dependency as used itself (`eslint-plugin-astro` hosting
7
+ * `typescript-eslint`), so ignoring one of these is a `Remove from
8
+ * ignoreDependencies` hint the consumer cannot silence.
9
+ */
10
+ export const peersOfListedDependencies = (cwd: string): Set<string> =>
11
+ new Set(
12
+ readListedDependencies(cwd).flatMap((name) =>
13
+ readPeerDependencyNames(cwd, name),
14
+ ),
15
+ )
@@ -0,0 +1,24 @@
1
+ import { readFileSync } from 'node:fs'
2
+ import { join } from 'node:path'
3
+
4
+ /**
5
+ * The names in `dependencies` and `devDependencies` of `cwd`'s
6
+ * `package.json`; an unreadable or malformed manifest gives none.
7
+ */
8
+ export const readListedDependencies = (cwd: string): string[] => {
9
+ try {
10
+ const manifest = JSON.parse(
11
+ // eslint-disable-next-line security/detect-non-literal-fs-filename -- fixed file name under the project root
12
+ readFileSync(join(cwd, 'package.json'), 'utf8'),
13
+ ) as {
14
+ dependencies?: Record<string, string>
15
+ devDependencies?: Record<string, string>
16
+ }
17
+ return [
18
+ ...Object.keys(manifest.dependencies ?? {}),
19
+ ...Object.keys(manifest.devDependencies ?? {}),
20
+ ]
21
+ } catch {
22
+ return []
23
+ }
24
+ }
@@ -0,0 +1,21 @@
1
+ import { readFileSync } from 'node:fs'
2
+ import { join } from 'node:path'
3
+
4
+ /**
5
+ * The `peerDependencies` names of the package `name` as installed under
6
+ * `cwd`'s `node_modules`; a package that is not installed gives none.
7
+ */
8
+ export const readPeerDependencyNames = (
9
+ cwd: string,
10
+ name: string,
11
+ ): string[] => {
12
+ try {
13
+ const manifest = JSON.parse(
14
+ // eslint-disable-next-line security/detect-non-literal-fs-filename -- a listed dependency's manifest under the project's node_modules
15
+ readFileSync(join(cwd, 'node_modules', name, 'package.json'), 'utf8'),
16
+ ) as { peerDependencies?: Record<string, string> }
17
+ return Object.keys(manifest.peerDependencies ?? {})
18
+ } catch {
19
+ return []
20
+ }
21
+ }
@@ -1,9 +1,13 @@
1
1
  import { readFileSync } from 'node:fs'
2
2
  import { join } from 'node:path'
3
3
 
4
+ import { isScriptInvocation } from './isScriptInvocation'
5
+
4
6
  /**
5
7
  * Every word of every `package.json` script in `cwd`, so a caller can ask
6
- * whether a binary is named anywhere. Unreadable or malformed manifests give
8
+ * whether a binary is named anywhere. A word that only runs another script
9
+ * (`pnpm type-coverage` where a `type-coverage` script exists) is left out:
10
+ * it names the script, not the binary. Unreadable or malformed manifests give
7
11
  * an empty set: the caller then ignores the dependency, which is the safe side.
8
12
  */
9
13
  export const readScriptWords = (cwd: string): Set<string> => {
@@ -12,11 +16,20 @@ export const readScriptWords = (cwd: string): Set<string> => {
12
16
  // eslint-disable-next-line security/detect-non-literal-fs-filename -- fixed file name under the project root
13
17
  readFileSync(join(cwd, 'package.json'), 'utf8'),
14
18
  )
15
- const scripts = (manifest as { scripts?: Record<string, unknown> }).scripts
16
- const text = Object.values(scripts ?? {})
17
- .filter((script): script is string => typeof script === 'string')
18
- .join('\n')
19
- return new Set(text.split(/[\s;&|()]+/u).filter(Boolean))
19
+ const scripts =
20
+ (manifest as { scripts?: Record<string, unknown> }).scripts ?? {}
21
+ const scriptNames = new Set(Object.keys(scripts))
22
+ const words = new Set<string>()
23
+ for (const script of Object.values(scripts)) {
24
+ if (typeof script !== 'string') continue
25
+ const tokens = script.split(/[\s;&|()]+/u).filter(Boolean)
26
+ tokens.forEach((word, index) => {
27
+ if (!isScriptInvocation(word, tokens[index - 1], scriptNames)) {
28
+ words.add(word)
29
+ }
30
+ })
31
+ }
32
+ return words
20
33
  } catch {
21
34
  return new Set()
22
35
  }
@@ -1,5 +1,6 @@
1
- import { RUNNER_BINARIES } from './RUNNER_BINARIES'
1
+ import { hasLefthookConfig } from './hasLefthookConfig'
2
2
  import { readScriptWords } from './readScriptWords'
3
+ import { RUNNER_BINARIES } from './RUNNER_BINARIES'
3
4
 
4
5
  /**
5
6
  * The runner dependencies knip cannot see used, and so must be ignored.
@@ -11,10 +12,17 @@ import { readScriptWords } from './readScriptWords'
11
12
  * `"deps:graph": "depcruise src"`) lets knip resolve it, and ignoring it then
12
13
  * becomes a permanent `Remove from ignoreDependencies` hint no consumer can
13
14
  * silence. So only the packages no script names directly are ignored.
15
+ *
16
+ * `lefthook` is also resolved by knip's own Lefthook plugin whenever a
17
+ * Lefthook configuration file exists, so it is ignored only without one.
14
18
  */
15
19
  export const runnerDependenciesToIgnore = (cwd: string): string[] => {
16
20
  const words = readScriptWords(cwd)
21
+ const lefthookConfigured = hasLefthookConfig(cwd)
17
22
  return Object.entries(RUNNER_BINARIES)
18
23
  .filter(([, binaries]) => !binaries.some((binary) => words.has(binary)))
24
+ .filter(
25
+ ([dependency]) => !(dependency === 'lefthook' && lefthookConfigured),
26
+ )
19
27
  .map(([dependency]) => dependency)
20
28
  }