@syntopica/quality-config 0.13.0 → 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.0",
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,20 +74,22 @@
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
- "typescript": "npm:@typescript/typescript6@^6.0.2"
83
+ "typescript": "npm:@typescript/typescript6@^6.0.2",
84
+ "vitest": "^5.0.3"
84
85
  },
85
86
  "scripts": {
86
87
  "fix": "pnpm run lint:fix",
87
- "lint": "eslint src bin --max-warnings 0",
88
- "lint:fix": "eslint src bin --fix",
89
- "lint:suppress": "eslint src bin --suppress-all",
90
- "lint:prune": "eslint src bin --prune-suppressions",
88
+ "lint": "eslint src bin tests --max-warnings 0",
89
+ "lint:fix": "eslint src bin tests --fix",
90
+ "lint:suppress": "eslint src bin tests --suppress-all",
91
+ "lint:prune": "eslint src bin tests --prune-suppressions",
92
+ "test": "vitest run",
91
93
  "type-check": "tsc --noEmit -p tsconfig.json",
92
94
  "publish:check": "baseline-licenses && publint --strict && attw --pack . --profile node16 --ignore-rules cjs-resolves-to-esm internal-resolution-error"
93
95
  }
@@ -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,12 @@
1
+ /**
2
+ * The binaries each runner dependency provides, keyed by package name. A
3
+ * script that names one of them directly is something knip resolves on its
4
+ * own, so the package needs no `ignoreDependencies` entry; a script that only
5
+ * names the `baseline-*` runner hides it.
6
+ */
7
+ export const RUNNER_BINARIES: Record<string, string[]> = {
8
+ jscpd: ['jscpd'],
9
+ 'type-coverage': ['type-coverage'],
10
+ 'dependency-cruiser': ['depcruise', 'dependency-cruise'],
11
+ lefthook: ['lefthook'],
12
+ }
@@ -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,19 @@
1
+ import { existsSync } from 'node:fs'
2
+ import { join } from 'node:path'
3
+
4
+ const GLOB_CHARACTERS = /[*?{}[\]!]/u
5
+
6
+ /**
7
+ * Drops the literal entry paths that do not exist in `cwd`; globs are kept,
8
+ * since whether they match is knip's call. Used for a preset's default entry
9
+ * when the caller names its own: a CLI package whose entry is
10
+ * `src/cli/main.ts` has no `src/index.ts`, and listing it is a "Refine entry
11
+ * pattern (no matches)" hint the caller cannot silence.
12
+ */
13
+ export const existingEntries = (entries: string[], cwd: string): string[] =>
14
+ entries.filter(
15
+ (entry) =>
16
+ GLOB_CHARACTERS.test(entry) ||
17
+ // eslint-disable-next-line security/detect-non-literal-fs-filename -- the entry is the preset's own literal, resolved against the project root
18
+ existsSync(join(cwd, entry)),
19
+ )
@@ -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
@@ -5,7 +5,10 @@
5
5
  // config without casting it first.
6
6
  import type { KnipConfiguration } from 'knip'
7
7
 
8
+ import { existingEntries } from './existingEntries'
8
9
  import { FRAMEWORK_ENTRIES, type KnipFramework } from './knip-framework'
10
+ import { peersOfListedDependencies } from './peersOfListedDependencies'
11
+ import { runnerDependenciesToIgnore } from './runnerDependenciesToIgnore'
9
12
 
10
13
  // Every template installs these as peer dependencies of
11
14
  // @syntopica/eslint-config: the config factories (base.ts and friends)
@@ -29,27 +32,10 @@ import { FRAMEWORK_ENTRIES, type KnipFramework } from './knip-framework'
29
32
  // `<package> knip.config.ts Remove from ignoreDependencies`. That is a hint,
30
33
  // not a gate failure. Do not filter the list consumer-side to silence it: the
31
34
  // filter would drift the moment this list changes, and the cost of being wrong
32
- // is a dependency that silently stops being checked.
33
- // The tools this package's own runners spawn through `pnpm exec`:
34
- // `baseline-dupes` spawns `jscpd`, `baseline-type-coverage` spawns
35
- // `type-coverage`, `baseline-deps-graph` spawns `depcruise`,
36
- // `baseline-hooks-install` spawns `lefthook`. A project that
37
- // wires the runner into its scripts never names the underlying tool anywhere
38
- // knip can see, so knip reports a real dependency as unused. Found in
39
- // a consumer the moment its `type-coverage` script became
40
- // `baseline-type-coverage`.
41
- //
42
- // `dependency-cruiser` is on the list even though a project calling
43
- // `depcruise` directly resolves it fine: there knip emits a `Remove from
44
- // ignoreDependencies` hint, which is a hint and not a gate failure, and the
45
- // cost of leaving it off is a real dependency silently reported as dead.
46
- const BASELINE_RUNNER_DEPENDENCIES = [
47
- 'jscpd',
48
- 'type-coverage',
49
- 'dependency-cruiser',
50
- 'lefthook',
51
- ]
52
-
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.
53
39
  const ESLINT_PEER_DEPENDENCIES = [
54
40
  '@eslint/js',
55
41
  'eslint-config-prettier',
@@ -104,7 +90,9 @@ export const createKnipConfig = (options: {
104
90
  /**
105
91
  * Extra entry globs, merged with the framework preset's `entry`. Same
106
92
  * reasoning as `project`, for a root nobody's plugin registers - a worker,
107
- * a CLI, a script the app spawns.
93
+ * a CLI, a script the app spawns. A preset entry that is a literal path
94
+ * missing from the project (`ts-package`'s `src/index.ts` in a CLI package)
95
+ * is dropped once this is set, so it cannot become a "no matches" hint.
108
96
  */
109
97
  entry?: string[]
110
98
  /**
@@ -122,12 +110,18 @@ export const createKnipConfig = (options: {
122
110
  * only this does.
123
111
  */
124
112
  drizzle?: false
113
+ /** The project root, for the checks that read it. Defaults to the cwd. */
114
+ cwd?: string
125
115
  }): KnipConfiguration => {
116
+ const cwd = options.cwd ?? process.cwd()
117
+ const hostedPeers = peersOfListedDependencies(cwd)
126
118
  const { entry, project, includeEntryExports } =
127
119
  FRAMEWORK_ENTRIES[options.framework]
128
120
 
129
121
  return {
130
- entry: [...entry, ...(options.entry ?? [])],
122
+ entry: options.entry
123
+ ? [...existingEntries(entry, cwd), ...options.entry]
124
+ : entry,
131
125
  project: [...project, ...(options.project ?? [])],
132
126
  ...(options.ignore ? { ignore: options.ignore } : {}),
133
127
  ...(options.drizzle === false ? { drizzle: false } : {}),
@@ -151,8 +145,10 @@ export const createKnipConfig = (options: {
151
145
  ignoreExportsUsedInFile: true,
152
146
  ignoreBinaries: ['gitleaks', ...(options.ignoreBinaries ?? [])],
153
147
  ignoreDependencies: [
154
- ...BASELINE_RUNNER_DEPENDENCIES,
155
- ...ESLINT_PEER_DEPENDENCIES,
148
+ ...runnerDependenciesToIgnore(cwd),
149
+ ...ESLINT_PEER_DEPENDENCIES.filter(
150
+ (dependency) => !hostedPeers.has(dependency),
151
+ ),
156
152
  ...(options.ignoreDependencies ?? []),
157
153
  ],
158
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
+ }
@@ -0,0 +1,36 @@
1
+ import { readFileSync } from 'node:fs'
2
+ import { join } from 'node:path'
3
+
4
+ import { isScriptInvocation } from './isScriptInvocation'
5
+
6
+ /**
7
+ * Every word of every `package.json` script in `cwd`, so a caller can ask
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
11
+ * an empty set: the caller then ignores the dependency, which is the safe side.
12
+ */
13
+ export const readScriptWords = (cwd: string): Set<string> => {
14
+ try {
15
+ const manifest: unknown = JSON.parse(
16
+ // eslint-disable-next-line security/detect-non-literal-fs-filename -- fixed file name under the project root
17
+ readFileSync(join(cwd, 'package.json'), 'utf8'),
18
+ )
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
33
+ } catch {
34
+ return new Set()
35
+ }
36
+ }
@@ -0,0 +1,28 @@
1
+ import { hasLefthookConfig } from './hasLefthookConfig'
2
+ import { readScriptWords } from './readScriptWords'
3
+ import { RUNNER_BINARIES } from './RUNNER_BINARIES'
4
+
5
+ /**
6
+ * The runner dependencies knip cannot see used, and so must be ignored.
7
+ *
8
+ * `baseline-dupes`, `baseline-type-coverage`, `baseline-deps-graph` and
9
+ * `baseline-hooks-install` spawn `jscpd`, `type-coverage`, `depcruise` and
10
+ * `lefthook` themselves, which hides a real dependency from knip. A project
11
+ * that also names the binary in a script (`"prepare": "lefthook install"`,
12
+ * `"deps:graph": "depcruise src"`) lets knip resolve it, and ignoring it then
13
+ * becomes a permanent `Remove from ignoreDependencies` hint no consumer can
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.
18
+ */
19
+ export const runnerDependenciesToIgnore = (cwd: string): string[] => {
20
+ const words = readScriptWords(cwd)
21
+ const lefthookConfigured = hasLefthookConfig(cwd)
22
+ return Object.entries(RUNNER_BINARIES)
23
+ .filter(([, binaries]) => !binaries.some((binary) => words.has(binary)))
24
+ .filter(
25
+ ([dependency]) => !(dependency === 'lefthook' && lefthookConfigured),
26
+ )
27
+ .map(([dependency]) => dependency)
28
+ }
package/tsconfig.json CHANGED
@@ -13,5 +13,10 @@
13
13
  "types": ["node"],
14
14
  "allowImportingTsExtensions": true
15
15
  },
16
- "include": ["src/**/*.ts", "eslint.config.ts"]
16
+ "include": [
17
+ "src/**/*.ts",
18
+ "tests/**/*.ts",
19
+ "eslint.config.ts",
20
+ "vitest.config.ts"
21
+ ]
17
22
  }