Skip to content

Commit

Permalink
Add pos to more issue types in json reporter (resolves #930)
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Feb 10, 2025
1 parent f2efd22 commit 72cbcfa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/docs/src/content/docs/features/reporters.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ object per file structured like this:
{
"file": "package.json",
"owners": ["@org/admin"],
"dependencies": ["jquery", "moment"],
"devDependencies": [],
"dependencies": [{ "name": "jquery", "line": 5, "col": 6, "pos": 71 }],
"devDependencies": [{ "name": "lodash", "line": 9, "col": 6, "pos": 99 }],
"unlisted": [{ "name": "react" }, { "name": "@org/unresolved" }],
"exports": [],
"types": [],
Expand Down
12 changes: 6 additions & 6 deletions packages/knip/src/reporters/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ type Item = { name: string; pos?: number; line?: number; col?: number };
type Row = {
file: string;
owners?: Array<{ name: string }>;
dependencies?: Array<{ name: string }>;
devDependencies?: Array<{ name: string }>;
optionalPeerDependencies?: Array<{ name: string }>;
dependencies?: Array<Item>;
devDependencies?: Array<Item>;
optionalPeerDependencies?: Array<Item>;
unlisted?: Array<{ name: string }>;
binaries?: Array<{ name: string }>;
unresolved?: Array<{ name: string }>;
Expand Down Expand Up @@ -82,10 +82,10 @@ export default async ({ report, issues, options }: ReporterOptions) => {
item[parentSymbol].push(convert(issue));
}
} else {
if (['exports', 'nsExports', 'types', 'nsTypes', 'unresolved'].includes(type)) {
json[filePath][type]?.push(convert(issue));
} else {
if (['unlisted', 'binaries'].includes(type)) {
json[filePath][type]?.push({ name: symbol });
} else {
json[filePath][type]?.push(convert(issue));
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions packages/knip/test/cli/reporter/cli-reporter-json4.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { test } from 'bun:test';
import assert from 'node:assert/strict';
import { resolve } from '../../../src/util/path.js';
import { execFactory } from '../../helpers/exec.js';

const cwd = resolve('fixtures/dependencies');

const exec = execFactory(cwd);

test('knip --reporter json (dependencies)', () => {
const json = {
files: ['unused-module.ts'],
issues: [
{
file: 'package.json',
dependencies: [
{ name: '@tootallnate/once', line: 8, col: 6, pos: 131 },
{ name: 'fs-extra', line: 10, col: 6, pos: 190 },
],
devDependencies: [{ name: 'mocha', line: 23, col: 6, pos: 422 }],
optionalPeerDependencies: [],
unlisted: [],
binaries: [{ name: 'jest' }, { name: 'start-server' }],
unresolved: [],
exports: [],
types: [],
enumMembers: {},
duplicates: [],
},
],
};

assert.equal(exec('knip --reporter json').stdout, `${JSON.stringify(json)}\n`);
});

0 comments on commit 72cbcfa

Please sign in to comment.