diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 833e863..11fd5cb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -53,9 +53,9 @@ jobs: echo "${ACTIONLINT_SHA256} actionlint.tar.gz" | sha256sum --check tar -xzf actionlint.tar.gz actionlint - run: '"$RUNNER_TEMP/actionlint" -color' - - uses: zizmorcore/zizmor-action@85b2b9fb8e3325e7a23d7d0a9d7ba5ae0f999e3e # v0.7.1 + - uses: zizmorcore/zizmor-action@1b4550e929643b8b26a62303c6bd64814271a4f1 # v0.9.2 with: - version: 1.31.1 + version: 1.33.1 advanced-security: false # The one check the branch ruleset requires, so the ruleset stays the same when jobs change. List every other job diff --git a/package-lock.json b/package-lock.json index 361fff8..591f751 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ }, "devDependencies": { "@types/node": "26.6.11", + "ajv": "6.15.0", "eslint": "10.15.0", "prettier": "3.10.1", "typescript-eslint": "8.74.0", diff --git a/package.json b/package.json index eda8a7c..04f7974 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ }, "devDependencies": { "@types/node": "26.6.11", + "ajv": "6.15.0", "eslint": "10.15.0", "prettier": "3.10.1", "typescript-eslint": "8.74.0", diff --git a/test/fixtures/saved-reports/0.2.1/entry.json b/test/fixtures/saved-reports/0.2.1/entry.json new file mode 100644 index 0000000..1aa908e --- /dev/null +++ b/test/fixtures/saved-reports/0.2.1/entry.json @@ -0,0 +1,110 @@ +{ + "snapshotId": "8bd2ce9c39c425ba3eaf13d8df667abf253ab6ca0694b49aa870520b98fbe804", + "entries": [ + "consumer.test.js" + ], + "completeness": "partial", + "diagnostics": [ + { + "code": "TS2345", + "message": "Argument of type 'string' is not assignable to parameter of type 'number'.", + "location": { + "file": "consumer.test.js", + "start": 106, + "end": 124, + "line": 6, + "column": 11 + } + }, + { + "code": "unresolved-call", + "message": "Call target cannot be tied to a concrete project declaration.", + "location": { + "file": "consumer.test.js", + "start": 127, + "end": 139, + "line": 7, + "column": 1 + } + } + ], + "reached": [ + { + "id": "module:consumer.test.js", + "via": null + }, + { + "id": "codec.js:0:function", + "via": { + "from": "module:consumer.test.js", + "kind": "call", + "location": { + "file": "consumer.test.js", + "start": 46, + "end": 58, + "line": 4, + "column": 1 + } + } + }, + { + "id": "module:index.js", + "via": { + "from": "module:consumer.test.js", + "kind": "import", + "location": { + "file": "consumer.test.js", + "start": 31, + "end": 43, + "line": 2, + "column": 19 + } + } + }, + { + "id": "module:codec.js", + "via": { + "from": "module:index.js", + "kind": "import", + "location": { + "file": "index.js", + "start": 23, + "end": 35, + "line": 1, + "column": 24 + } + } + } + ], + "declarations": [ + { + "id": "codec.js:0:function", + "name": "encode", + "location": { + "file": "codec.js", + "start": 0, + "end": 57, + "line": 1, + "column": 1 + }, + "status": "reachable" + }, + { + "id": "codec.js:59:function", + "name": "hidden", + "location": { + "file": "codec.js", + "start": 59, + "end": 92, + "line": 5, + "column": 1 + }, + "status": "no-known-path" + } + ], + "limitations": [ + "Paths follow resolved direct calls, construction and source module edges. Conditional code can contribute a path.", + "Callbacks, external API consumers, framework dispatch, class initialization and computed imports can hide paths.", + "No known path is relative to the selected entries and supported edges. Runtime execution is unknown." + ] +} diff --git a/test/fixtures/saved-reports/0.2.1/inventory.json b/test/fixtures/saved-reports/0.2.1/inventory.json new file mode 100644 index 0000000..5a8edd0 --- /dev/null +++ b/test/fixtures/saved-reports/0.2.1/inventory.json @@ -0,0 +1,198 @@ +{ + "schemaVersion": 1, + "analyzer": { + "name": "code-inventory", + "version": "0.2.1", + "compilerVersion": "6.0.3" + }, + "snapshotId": "8bd2ce9c39c425ba3eaf13d8df667abf253ab6ca0694b49aa870520b98fbe804", + "config": { + "path": null, + "hash": null, + "entries": [], + "projects": [], + "inputs": [] + }, + "completeness": "partial", + "files": [ + { + "path": "codec.d.ts", + "hash": "b8bec2aad318bd58250907119b1485ca1a371a0d37d5c6951a4a574f0de8f48f", + "status": "excluded", + "reason": "declaration-file" + }, + { + "path": "codec.js", + "hash": "08de306e774d7de5ec92bf8d985d7bc2029cb4c79b0ff431a646e40ab0550b8e", + "status": "analyzed", + "reason": null + }, + { + "path": "consumer.test.js", + "hash": "9e306ebb088853e687b18524bee4a94ae41d6b89151ddf8483311a2729dd6935", + "status": "analyzed", + "reason": null + }, + { + "path": "index.d.ts", + "hash": "d5aea7680ec590ead73cdf0ae4399a83543d8eb694529b3136a503923ef3d051", + "status": "excluded", + "reason": "declaration-file" + }, + { + "path": "index.js", + "hash": "d5aea7680ec590ead73cdf0ae4399a83543d8eb694529b3136a503923ef3d051", + "status": "analyzed", + "reason": null + } + ], + "symbols": [ + { + "id": "codec.js:0:function", + "name": "encode", + "kind": "function", + "location": { + "file": "codec.js", + "start": 0, + "end": 57, + "line": 1, + "column": 1 + }, + "containerId": null, + "scope": "production", + "exportedAs": [ + "codec.js#encode" + ] + }, + { + "id": "codec.js:59:function", + "name": "hidden", + "kind": "function", + "location": { + "file": "codec.js", + "start": 59, + "end": 92, + "line": 5, + "column": 1 + }, + "containerId": null, + "scope": "production", + "exportedAs": [] + } + ], + "references": [ + { + "targetId": "codec.js:0:function", + "kind": "call", + "location": { + "file": "consumer.test.js", + "start": 46, + "end": 58, + "line": 4, + "column": 1 + }, + "callerId": null, + "scope": "test" + }, + { + "targetId": "codec.js:0:function", + "kind": "read", + "location": { + "file": "consumer.test.js", + "start": 88, + "end": 94, + "line": 5, + "column": 25 + }, + "callerId": null, + "scope": "test" + }, + { + "targetId": "codec.js:0:function", + "kind": "call", + "location": { + "file": "consumer.test.js", + "start": 96, + "end": 105, + "line": 6, + "column": 1 + }, + "callerId": null, + "scope": "test" + } + ], + "modules": [ + { + "callerId": null, + "specifier": "./index.js", + "kind": "import", + "location": { + "file": "consumer.test.js", + "start": 31, + "end": 43, + "line": 2, + "column": 19 + }, + "typeOnly": false, + "resolvedFile": "index.js" + }, + { + "callerId": null, + "specifier": "./codec.js", + "kind": "import", + "location": { + "file": "index.js", + "start": 23, + "end": 35, + "line": 1, + "column": 24 + }, + "typeOnly": false, + "resolvedFile": "codec.js" + } + ], + "exports": [ + { + "file": "codec.js", + "name": "encode", + "targetId": "codec.js:0:function" + }, + { + "file": "index.js", + "name": "default", + "targetId": null + } + ], + "diagnostics": [ + { + "code": "TS2345", + "message": "Argument of type 'string' is not assignable to parameter of type 'number'.", + "location": { + "file": "consumer.test.js", + "start": 106, + "end": 124, + "line": 6, + "column": 11 + } + }, + { + "code": "unresolved-call", + "message": "Call target cannot be tied to a concrete project declaration.", + "location": { + "file": "consumer.test.js", + "start": 127, + "end": 139, + "line": 7, + "column": 1 + } + } + ], + "limitations": [ + "References describe source occurrences, not execution counts or reachability.", + "Public exports may have consumers outside the scanned root.", + "Reflection, framework conventions, mutable aliases and runtime dispatch may hide usages.", + "Default discovery omits dependency/build directories and does not follow symlinks.", + "Each source file is counted in one project context; configuration-dependent alternatives may differ.", + "Symbol IDs are snapshot-local and may change when source locations change." + ] +} diff --git a/test/fixtures/saved-reports/0.2.1/usage.json b/test/fixtures/saved-reports/0.2.1/usage.json new file mode 100644 index 0000000..81818d4 --- /dev/null +++ b/test/fixtures/saved-reports/0.2.1/usage.json @@ -0,0 +1,98 @@ +{ + "schemaVersion": 1, + "snapshotId": "8bd2ce9c39c425ba3eaf13d8df667abf253ab6ca0694b49aa870520b98fbe804", + "completeness": "partial", + "diagnostics": [ + { + "code": "TS2345", + "message": "Argument of type 'string' is not assignable to parameter of type 'number'.", + "location": { + "file": "consumer.test.js", + "start": 106, + "end": 124, + "line": 6, + "column": 11 + } + }, + { + "code": "unresolved-call", + "message": "Call target cannot be tied to a concrete project declaration.", + "location": { + "file": "consumer.test.js", + "start": 127, + "end": 139, + "line": 7, + "column": 1 + } + } + ], + "usage": { + "symbol": { + "id": "codec.js:0:function", + "name": "encode", + "kind": "function", + "location": { + "file": "codec.js", + "start": 0, + "end": 57, + "line": 1, + "column": 1 + }, + "containerId": null, + "scope": "production", + "exportedAs": [ + "codec.js#encode" + ] + }, + "references": [ + { + "targetId": "codec.js:0:function", + "kind": "call", + "location": { + "file": "consumer.test.js", + "start": 46, + "end": 58, + "line": 4, + "column": 1 + }, + "callerId": null, + "scope": "test" + }, + { + "targetId": "codec.js:0:function", + "kind": "read", + "location": { + "file": "consumer.test.js", + "start": 88, + "end": 94, + "line": 5, + "column": 25 + }, + "callerId": null, + "scope": "test" + }, + { + "targetId": "codec.js:0:function", + "kind": "call", + "location": { + "file": "consumer.test.js", + "start": 96, + "end": 105, + "line": 6, + "column": 1 + }, + "callerId": null, + "scope": "test" + } + ], + "counts": { + "references": 3, + "directCallSites": 2, + "constructorSites": 0, + "distinctCallers": 1, + "production": 0, + "test": 3, + "tooling": 0 + } + } +} diff --git a/test/fixtures/saved-reports/0.3.0/entry.json b/test/fixtures/saved-reports/0.3.0/entry.json new file mode 100644 index 0000000..aa6bb87 --- /dev/null +++ b/test/fixtures/saved-reports/0.3.0/entry.json @@ -0,0 +1,72 @@ +{ + "snapshotId": "fe7c885ad3283c3930c7a2d86b7171cfd79b6e421c726b969922e762036fcb42", + "entries": [], + "entrySymbols": [ + "app/status/route.ts:0:function" + ], + "completeness": "complete", + "diagnostics": [], + "reached": [ + { + "id": "app/status/route.ts:0:function", + "via": null + }, + { + "id": "app/status/route.ts:50:function", + "via": { + "from": "app/status/route.ts:0:function", + "kind": "call", + "location": { + "file": "app/status/route.ts", + "start": 33, + "end": 43, + "line": 2, + "column": 10 + } + } + } + ], + "declarations": [ + { + "id": "app/status/route.ts:0:function", + "name": "GET", + "location": { + "file": "app/status/route.ts", + "start": 0, + "end": 48, + "line": 1, + "column": 1 + }, + "status": "reachable" + }, + { + "id": "app/status/route.ts:50:function", + "name": "statusText", + "location": { + "file": "app/status/route.ts", + "start": 50, + "end": 93, + "line": 5, + "column": 1 + }, + "status": "reachable" + }, + { + "id": "app/status/route.ts:95:function", + "name": "POST", + "location": { + "file": "app/status/route.ts", + "start": 95, + "end": 142, + "line": 9, + "column": 1 + }, + "status": "no-known-path" + } + ], + "limitations": [ + "Paths follow resolved direct calls, construction and source module edges. Conditional code can contribute a path.", + "Callbacks, external API consumers, framework dispatch, class initialization and computed imports can hide paths.", + "No known path is relative to the selected entries and supported edges. Runtime execution is unknown." + ] +} diff --git a/test/fixtures/saved-reports/0.3.0/inventory.json b/test/fixtures/saved-reports/0.3.0/inventory.json new file mode 100644 index 0000000..ddd786b --- /dev/null +++ b/test/fixtures/saved-reports/0.3.0/inventory.json @@ -0,0 +1,113 @@ +{ + "schemaVersion": 1, + "analyzer": { + "name": "code-inventory", + "version": "0.3.0", + "compilerVersion": "6.0.3" + }, + "snapshotId": "fe7c885ad3283c3930c7a2d86b7171cfd79b6e421c726b969922e762036fcb42", + "config": { + "path": null, + "hash": null, + "entries": [], + "projects": [], + "inputs": [] + }, + "completeness": "complete", + "files": [ + { + "path": "app/status/route.ts", + "hash": "470b5028e0d99a33ed961efad911dd61ea208566e5c36ece41a0dbfda680a933", + "status": "analyzed", + "reason": null + } + ], + "symbols": [ + { + "id": "app/status/route.ts:0:function", + "name": "GET", + "kind": "function", + "location": { + "file": "app/status/route.ts", + "start": 0, + "end": 48, + "line": 1, + "column": 1 + }, + "containerId": null, + "scope": "production", + "exportedAs": [ + "app/status/route.ts#GET" + ] + }, + { + "id": "app/status/route.ts:50:function", + "name": "statusText", + "kind": "function", + "location": { + "file": "app/status/route.ts", + "start": 50, + "end": 93, + "line": 5, + "column": 1 + }, + "containerId": null, + "scope": "production", + "exportedAs": [] + }, + { + "id": "app/status/route.ts:95:function", + "name": "POST", + "kind": "function", + "location": { + "file": "app/status/route.ts", + "start": 95, + "end": 142, + "line": 9, + "column": 1 + }, + "containerId": null, + "scope": "production", + "exportedAs": [ + "app/status/route.ts#POST" + ] + } + ], + "references": [ + { + "targetId": "app/status/route.ts:50:function", + "kind": "call", + "location": { + "file": "app/status/route.ts", + "start": 33, + "end": 43, + "line": 2, + "column": 10 + }, + "callerId": "app/status/route.ts:0:function", + "scope": "production" + } + ], + "modules": [], + "exports": [ + { + "file": "app/status/route.ts", + "name": "GET", + "targetId": "app/status/route.ts:0:function" + }, + { + "file": "app/status/route.ts", + "name": "POST", + "targetId": "app/status/route.ts:95:function" + } + ], + "diagnostics": [], + "limitations": [ + "References describe source occurrences, not execution counts or reachability.", + "Public exports may have consumers outside the scanned root.", + "Reflection, framework conventions, mutable aliases and runtime dispatch may hide usages.", + "Default discovery omits dependency/build directories and does not follow symlinks.", + "Each source file is counted in one project context; configuration-dependent alternatives may differ.", + "Symbol IDs are snapshot-local and may change when source locations change." + ] +} diff --git a/test/fixtures/saved-reports/0.3.0/usage.json b/test/fixtures/saved-reports/0.3.0/usage.json new file mode 100644 index 0000000..484da73 --- /dev/null +++ b/test/fixtures/saved-reports/0.3.0/usage.json @@ -0,0 +1,47 @@ +{ + "schemaVersion": 1, + "snapshotId": "fe7c885ad3283c3930c7a2d86b7171cfd79b6e421c726b969922e762036fcb42", + "completeness": "complete", + "diagnostics": [], + "usage": { + "symbol": { + "id": "app/status/route.ts:50:function", + "name": "statusText", + "kind": "function", + "location": { + "file": "app/status/route.ts", + "start": 50, + "end": 93, + "line": 5, + "column": 1 + }, + "containerId": null, + "scope": "production", + "exportedAs": [] + }, + "references": [ + { + "targetId": "app/status/route.ts:50:function", + "kind": "call", + "location": { + "file": "app/status/route.ts", + "start": 33, + "end": 43, + "line": 2, + "column": 10 + }, + "callerId": "app/status/route.ts:0:function", + "scope": "production" + } + ], + "counts": { + "references": 1, + "directCallSites": 1, + "constructorSites": 0, + "distinctCallers": 1, + "production": 1, + "test": 0, + "tooling": 0 + } + } +} diff --git a/test/saved-reports.test.ts b/test/saved-reports.test.ts new file mode 100644 index 0000000..38a8436 --- /dev/null +++ b/test/saved-reports.test.ts @@ -0,0 +1,26 @@ +import { readFileSync } from 'node:fs'; +import Ajv from 'ajv'; +import { expect, it } from 'vitest'; +import { reachability, usages, type Inventory } from '../src/index.ts'; + +function readJson(path: string) { + return JSON.parse(readFileSync(new URL(path, import.meta.url), 'utf8')); +} + +const validate = new Ajv().compile(readJson('../docs/report-schema.json')); + +it.each([ + { version: '0.2.1', fixture: 'typed-js', symbol: 'encode', entries: ['consumer.test.js'], entrySymbols: [] }, + { version: '0.3.0', fixture: 'route', symbol: 'statusText', entries: [], entrySymbols: ['GET'] }, +])('reads and replays the $fixture reports saved by $version', ({ version, symbol, entries, entrySymbols }) => { + const directory = `./fixtures/saved-reports/${version}`; + const inventory: Inventory = readJson(`${directory}/inventory.json`); + const usage = readJson(`${directory}/usage.json`); + const entry = readJson(`${directory}/entry.json`); + for (const report of [inventory, usage, entry]) { + expect(validate(report), JSON.stringify(validate.errors)).toBe(true); + } + expect(inventory.analyzer.version).toBe(version); + expect(usages(inventory, symbol)).toEqual(usage.usage); + expect(reachability(inventory, entries, { entrySymbols })).toEqual(entry); +});