Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/node-22-support.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'code-inventory': minor
---

Support Node.js 22.12 and newer, down from Node.js 26.
11 changes: 11 additions & 0 deletions .changeset/picomatch-globs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'code-inventory': minor
---

Match glob options and npm workspace patterns with [picomatch](https://github.com/micromatch/picomatch), a new dependency, instead of Node's `path.matchesGlob`, so neither the CLI nor the library prints an experimental-feature warning on Node.js versions before 22.20 and 24.8. Most patterns select the same files as before. The differences:

- `--workspace-entries` and `workspaceEntries()` select member folders as npm does. An exclusion such as `!packages/b` applies wherever it is listed, unless a later pattern that it matches lists the folder again; `!packages/b/**` also excludes `packages/b`; exclusions also apply to dot folders; and `packages/**` also selects `packages` when it has a manifest.
- Glob options (`--exclude`, `--test`, `--tooling`, `--production`, `--entry-glob` and the matching library options) are case-sensitive on every platform. On macOS and Windows, path segments with wildcards or character classes used to ignore case.
- `--exclude`, `--test`, `--tooling` and `--production` accept a leading `./`, as `--entry-glob` already did.
- A pattern also matches a path spelled exactly like it, so `app/[id]/route.ts` selects that file as well as `app/i/route.ts`, and a pattern ending in `/**` also matches the path before it.
- A `!(…)` extglob also rejects names that begin with one of its alternatives, so `src/!(a).ts` no longer matches `src/ab.ts`, and it can match dot files.
64 changes: 63 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,68 @@ jobs:
- run: npm run check
- run: npm pack --dry-run --ignore-scripts

# The supported Node.js lines. The checks job above uses the development version in .node-version.
test:
name: Test (Node ${{ matrix.node }})
runs-on: ubuntu-latest
timeout-minutes: 10
strategy:
fail-fast: false
matrix:
node: [22, 24, 26]
env:
NODE_OPTIONS: --max-old-space-size=2048
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: ${{ matrix.node }}
package-manager-cache: false
- run: npm ci --ignore-scripts --no-audit --fund=false
- run: npm run typecheck
- run: npm run build
- run: npm test

oldest-node:
name: Tests and packed CLI on Node 22.12.0
runs-on: ubuntu-latest
timeout-minutes: 10
env:
NODE_OPTIONS: --max-old-space-size=2048
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
# ESLint needs a newer Node.js, so the package is installed, built and packed with the development version.
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version-file: .node-version
package-manager-cache: false
- run: npm ci --ignore-scripts --no-audit --fund=false
- run: npm run build
- run: npm pack --ignore-scripts --pack-destination "$RUNNER_TEMP"
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: 22.12.0
package-manager-cache: false
- run: npm test
# The package under test is installed from its tarball, as a user installs it, so no lockfile applies.
- name: Use the packed CLI and library
working-directory: ${{ runner.temp }}
run: | # zizmor: ignore[adhoc-packages]
mkdir app && cd app
npm init --yes > /dev/null
npm install --no-audit --no-fund --ignore-scripts ../code-inventory-*.tgz
printf 'export function normalize(value: string) {\n return value.trim();\n}\n' > text.ts
printf "import { normalize } from './text';\nnormalize(' order ');\n" > app.ts
npx code-inventory . --symbol normalize --format json --exclude 'node_modules/**' > usage.json 2> stderr.txt
if [ -s stderr.txt ]; then cat stderr.txt; exit 1; fi
node -e "const r = require('./usage.json'); process.exit(r.completeness === 'complete' && r.usage.counts.directCallSites === 1 ? 0 : 1)"
node --input-type=module -e "import { scan, usages } from 'code-inventory'; const n = usages(scan('.', { exclude: ['node_modules/**'], scopes: { test: ['**/*.test.ts'] } }), 'normalize').counts.directCallSites; if (n !== 1) process.exit(1); console.log(n)" 2> stderr.txt
if [ -s stderr.txt ]; then cat stderr.txt; exit 1; fi

# actionlint checks the workflow syntax and the shell in each step; zizmor checks them for security problems.
workflows:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -62,7 +124,7 @@ jobs:
# in `needs`.
ci:
if: always()
needs: [workflows, checks]
needs: [workflows, checks, test, oldest-node]
runs-on: ubuntu-latest
timeout-minutes: 5
permissions:
Expand Down
4 changes: 2 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ Notes for coding agents working in this repository. People should read CONTRIBUT
exit code 2. Don't throw for them; errors are for invalid options and inputs the scan can't use at all.
- Relative imports in `src/` end in `.ts`; the build rewrites them. Type-only imports use `import type`.
- `no-console` is on; the CLI writes with `process.stdout.write`.
- The only runtime dependency is the TypeScript 6 compiler under the `typescript` alias. Every dependency is pinned to
an exact version. Ask before adding one, and list it in `THIRD_PARTY.md`.
- The runtime dependencies are the TypeScript 6 compiler under the `typescript` alias and `picomatch`, which matches
glob options. Every dependency is pinned to an exact version. Ask before adding one, and list it in `THIRD_PARTY.md`.
- A change users can see updates `README.md` and the matching page in `docs/` in the same pull request.
- Changesets: a change that affects users carries one (`npx changeset`). Breaking changes are `minor` before 1.0;
never `major`.
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ publishes nothing.
## Dependencies

Every dependency in `package.json` is pinned to an exact version, so a version changes only in a pull request. Update
them after reading their changelogs, with `npm install --save-exact --ignore-scripts`, and run `npm audit`. The only runtime
dependency is the TypeScript 6 compiler, installed as an npm alias; ask before adding another one.
them after reading their changelogs, with `npm install --save-exact --ignore-scripts`, and run `npm audit`. The runtime
dependencies are the TypeScript 6 compiler, installed as an npm alias, and picomatch; ask before adding another one.

When a dependency is added, add it to [THIRD_PARTY.md](THIRD_PARTY.md) with its license.

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ Find where JavaScript and TypeScript declarations are used. The CLI and library

## Run your first scan

In a JavaScript or TypeScript project with Node.js 26 or newer:
In a JavaScript or TypeScript project with Node.js 22.12 or newer:

```sh
npm install --save-dev code-inventory
npx code-inventory .
```

For a checkout of this repository, use the fixture to check how calls are counted. CI uses the version in [.node-version](.node-version).
For a checkout of this repository, use the fixture to check how calls are counted. Development and CI use the version in [.node-version](.node-version); CI also tests Node.js 22.12.0 and the latest 22, 24 and 26.

```sh
npm ci --ignore-scripts --no-audit
Expand Down
6 changes: 5 additions & 1 deletion THIRD_PARTY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
| Package | License | Source |
| -------------------------------------------------- | ---------- | ---------------------------------------------------------------------------------------- |
| @typescript/typescript6; its typescript dependency | Apache-2.0 | https://github.com/microsoft/TypeScript |
| @types/node | MIT | https://github.com/DefinitelyTyped/DefinitelyTyped |
| picomatch | MIT | https://github.com/micromatch/picomatch |
| @types/node, @types/picomatch | MIT | https://github.com/DefinitelyTyped/DefinitelyTyped |
| undici-types (Node type dependency) | MIT | https://github.com/nodejs/undici |
| vitest and its workspace packages | MIT | https://github.com/vitest-dev/vitest |
| prettier | MIT | https://github.com/prettier/prettier |
Expand All @@ -13,6 +14,9 @@
The lockfile records exact versions, integrity hashes and licenses for transitive dependencies. Installation uses
`--ignore-scripts`. The optional macOS-only fsevents package declares an install script and is not installed on Linux.

The runtime dependencies are the compiler and picomatch 4.0.7, which matches glob options and has no dependencies of
its own.

The compiler dependency is an npm alias to `@typescript/typescript6@6.0.2`, as documented by Microsoft for tooling that
requires the compiler API. That package resolves `typescript@6.0.3` internally.

Expand Down
3 changes: 2 additions & 1 deletion docs/guide.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ <h1>Your first scan</h1>
<h2 id="build">Build from source</h2>
<p>
Clone the <a href="https://github.com/Huldoser/code-inventory">code-inventory repository</a> and open its root
directory. The examples use the compiled files in your checkout. Use Node.js 26 or newer. CI pins 26.13.1.
directory. The examples use the compiled files in your checkout. Use Node.js 22.12 or newer. Development and
the full set of checks use the version in <code>.node-version</code>, 26.13.1.
</p>
<pre><code>npm ci --ignore-scripts --no-audit
npm run build</code></pre>
Expand Down
12 changes: 9 additions & 3 deletions docs/reference.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ <h2 id="cli">CLI options</h2>
<pre><code>node dist/cli.js [directory] [options]</code></pre>
<p>
The directory defaults to the current working directory. Quote globs so the scanner receives the pattern
directly.
directly. Globs match slash-separated paths relative to the scan root, case-sensitively on every platform.
<code>*</code> and <code>**</code> skip dot files and folders unless the pattern names them, as in
<code>.config/**</code>. Braces such as <code>{a,b}</code>, extglobs such as <code>@(a|b)</code> and character
classes work; other parentheses are literal, so <code>'app/(marketing)/**'</code> selects a Next.js route
group.
</p>
<dl>
<dt><code>--project config</code></dt>
Expand Down Expand Up @@ -76,8 +80,10 @@ <h2 id="cli">CLI options</h2>
</dd>
<dt><code>--workspace-entries</code></dt>
<dd>
Read the root and member manifests matched by the root npm workspaces array. Each entry origin lists the
manifest, field and analyzed file. Use --project for any required compiler output mapping.
Read the root and member manifests matched by the root npm workspaces array, selecting member folders as npm
does: <code>*</code> and <code>**</code> skip dot folders unless a pattern names them, and a pattern
starting with <code>!</code> excludes the folders it matches. Each entry origin lists the manifest, field
and analyzed file. Use --project for any required compiler output mapping.
</dd>
<dt><code>--next-routes</code></dt>
<dd>
Expand Down
28 changes: 18 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"cli"
],
"engines": {
"node": ">=26"
"node": ">=22.12"
},
"exports": {
".": {
Expand Down Expand Up @@ -51,12 +51,14 @@
"changeset": "changeset"
},
"dependencies": {
"picomatch": "4.0.7",
"typescript": "npm:@typescript/typescript6@6.0.2"
},
"devDependencies": {
"@changesets/changelog-github": "2.0.0",
"@changesets/cli": "3.1.0",
"@types/node": "26.6.11",
"@types/node": "22.12.0",
"@types/picomatch": "4.0.3",
"ajv": "8.20.0",
"eslint": "10.16.0",
"prettier": "3.10.1",
Expand Down
37 changes: 37 additions & 0 deletions src/globs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { posix } from 'node:path';
import picomatch from 'picomatch/posix.js';

/**
* Rewrite a glob option for picomatch so that it keeps the syntax the options have always had: a backslash separates
* path segments like a slash, `.` and `..` segments and repeated slashes are normalized, and parentheses, `|` and
* double quotes are literal characters except in extglobs such as `@(a|b)`.
*/
function picomatchGlob(pattern: string): string {
const normalized = posix.normalize(pattern.replaceAll('\\', '/'));
const groups: boolean[] = [];
let glob = '';
for (let index = 0; index < normalized.length; index++) {
const character = normalized[index]!;
if (character === '(') {
const extglob = index > 0 && '@!+*?'.includes(normalized[index - 1]!);
groups.push(extglob);
glob += extglob ? character : '\\(';
} else if (character === ')') glob += groups.pop() ? character : '\\)';
else if (character === '|') glob += groups.at(-1) ? character : '\\|';
else if (character === '"') glob += '\\"';
else glob += character;
}
return glob;
}

/**
* Match slash-separated paths relative to the scan root against glob options. Matching is case-sensitive on every
* platform, `*` and `**` skip dot files and folders unless the pattern names them, and a leading `!` is part of the
* name rather than a negation. An empty pattern matches nothing.
*/
export function globMatcher(patterns: readonly string[]): (path: string) => boolean {
const globs = patterns.filter((pattern) => pattern !== '').map(picomatchGlob);
if (!globs.length) return () => false;
const isMatch = picomatch(globs, { nonegate: true, posix: true });
return (path) => isMatch(path);
}
Loading