dependency-detective 0.2.0 → 0.4.0

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.
@@ -0,0 +1,32 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import path from 'node:path';
4
+ import { fileURLToPath } from 'node:url';
5
+ import { extractPackageName, parseFileImports } from '../src/ast-parser.js';
6
+
7
+ const here = path.dirname(fileURLToPath(import.meta.url));
8
+
9
+ test('extractPackageName handles bare, scoped, subpath, relative', () => {
10
+ assert.equal(extractPackageName('express'), 'express');
11
+ assert.equal(extractPackageName('lodash/get'), 'lodash');
12
+ assert.equal(extractPackageName('@babel/core'), '@babel/core');
13
+ assert.equal(extractPackageName('@babel/core/lib/x'), '@babel/core');
14
+ assert.equal(extractPackageName('./local'), './local');
15
+ });
16
+
17
+ test('parseFileImports captures import, require, dynamic import, export-from', async () => {
18
+ const { referenced, warnings } = await parseFileImports(
19
+ path.join(here, 'fixtures', 'sample-imports.js')
20
+ );
21
+ assert.ok(referenced.has('express'));
22
+ assert.ok(referenced.has('chalk'));
23
+ assert.ok(referenced.has('ora'));
24
+ assert.ok(referenced.has('lodash'));
25
+ assert.deepEqual(warnings, []);
26
+ });
27
+
28
+ test('parseFileImports returns empty result for missing file', async () => {
29
+ const { referenced, warnings } = await parseFileImports('/no/such/file.js');
30
+ assert.equal(referenced.size, 0);
31
+ assert.deepEqual(warnings, []);
32
+ });
@@ -0,0 +1,24 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { mapWithConcurrency } from '../src/util/concurrency.js';
4
+
5
+ test('maps all items preserving input order', async () => {
6
+ const out = await mapWithConcurrency([1, 2, 3, 4], 2, async (x) => x * 2);
7
+ assert.deepEqual(out, [2, 4, 6, 8]);
8
+ });
9
+
10
+ test('never runs more than `limit` concurrently', async () => {
11
+ let active = 0;
12
+ let max = 0;
13
+ await mapWithConcurrency([1, 2, 3, 4, 5, 6], 2, async () => {
14
+ active++;
15
+ max = Math.max(max, active);
16
+ await new Promise((r) => setTimeout(r, 10));
17
+ active--;
18
+ });
19
+ assert.ok(max <= 2, `observed max concurrency ${max} exceeded 2`);
20
+ });
21
+
22
+ test('empty input returns empty array', async () => {
23
+ assert.deepEqual(await mapWithConcurrency([], 4, async (x) => x), []);
24
+ });
@@ -0,0 +1,87 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import fs from 'node:fs';
4
+ import os from 'node:os';
5
+ import path from 'node:path';
6
+ import { isReferenced, eslintShortNames, isEslintReference, scanConfigFiles } from '../src/config-scanner.js';
7
+
8
+ test('matches quoted references and subpaths', () => {
9
+ assert.ok(isReferenced(`import x from 'tailwindcss'`, 'tailwindcss'));
10
+ assert.ok(isReferenced(`require('tailwindcss/plugin')`, 'tailwindcss'));
11
+ assert.ok(isReferenced(`plugins: ["@tailwindcss/forms"]`, '@tailwindcss/forms'));
12
+ });
13
+
14
+ test('does not match unrelated substrings', () => {
15
+ assert.ok(!isReferenced(`// tailwindcss-typography notes`, 'tailwindcss'));
16
+ assert.ok(!isReferenced(`const tailwindcssHelper = 1`, 'tailwindcss'));
17
+ });
18
+
19
+ test('eslintShortNames maps plugin and config packages to their short names', () => {
20
+ assert.deepEqual(eslintShortNames('eslint-plugin-react'), ['react']);
21
+ assert.deepEqual(eslintShortNames('eslint-config-airbnb'), ['airbnb']);
22
+ assert.deepEqual(eslintShortNames('@typescript-eslint/eslint-plugin'), ['@typescript-eslint']);
23
+ assert.deepEqual(eslintShortNames('@scope/eslint-plugin-foo'), ['@scope/foo']);
24
+ assert.deepEqual(eslintShortNames('@vue/eslint-config-typescript'), ['@vue/typescript']);
25
+ assert.deepEqual(eslintShortNames('@scope/eslint-config'), ['@scope']);
26
+ assert.deepEqual(eslintShortNames('react'), []);
27
+ assert.deepEqual(eslintShortNames('eslint'), []);
28
+ });
29
+
30
+ test('isEslintReference matches short names in legacy JSON and YAML configs', () => {
31
+ assert.ok(isEslintReference(`{ "plugins": ["react"] }`, 'eslint-plugin-react'));
32
+ assert.ok(isEslintReference(`{ "extends": ["plugin:react/recommended"] }`, 'eslint-plugin-react'));
33
+ assert.ok(isEslintReference(`{ "rules": { "react/jsx-key": "error" } }`, 'eslint-plugin-react'));
34
+ assert.ok(isEslintReference(`extends:\n - airbnb\n`, 'eslint-config-airbnb'));
35
+ assert.ok(isEslintReference(`extends: airbnb/hooks`, 'eslint-config-airbnb'));
36
+ assert.ok(isEslintReference(`{ "extends": ["plugin:@typescript-eslint/recommended"] }`, '@typescript-eslint/eslint-plugin'));
37
+ assert.ok(isEslintReference(`{ "plugins": ["eslint-plugin-react"] }`, 'eslint-plugin-react'));
38
+ });
39
+
40
+ test('isEslintReference does not match other packages sharing a prefix', () => {
41
+ assert.ok(!isEslintReference(`{ "plugins": ["react-hooks"] }`, 'eslint-plugin-react'));
42
+ assert.ok(!isEslintReference(`{ "extends": ["airbnb-base"] }`, 'eslint-config-airbnb'));
43
+ });
44
+
45
+ function tempProject(files) {
46
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'dd-eslint-'));
47
+ for (const [name, content] of Object.entries(files)) {
48
+ fs.writeFileSync(path.join(dir, name), content);
49
+ }
50
+ return dir;
51
+ }
52
+
53
+ test('scanConfigFiles reads eslint.config.mjs flat config', async () => {
54
+ const dir = tempProject({
55
+ 'eslint.config.mjs': `import js from '@eslint/js';\nimport react from 'eslint-plugin-react';\nexport default [js.configs.recommended, react.configs.flat.recommended];\n`,
56
+ });
57
+ const found = await scanConfigFiles(dir, ['@eslint/js', 'eslint-plugin-react', 'lodash']);
58
+ assert.deepEqual([...found].sort(), ['@eslint/js', 'eslint-plugin-react']);
59
+ });
60
+
61
+ test('scanConfigFiles resolves short names in .eslintrc.yml', async () => {
62
+ const dir = tempProject({
63
+ '.eslintrc.yml': `extends:\n - airbnb\n - plugin:react/recommended\nplugins:\n - "@typescript-eslint"\n`,
64
+ });
65
+ const found = await scanConfigFiles(dir, [
66
+ 'eslint-config-airbnb',
67
+ 'eslint-plugin-react',
68
+ '@typescript-eslint/eslint-plugin',
69
+ 'eslint-plugin-vue',
70
+ ]);
71
+ assert.deepEqual([...found].sort(), [
72
+ '@typescript-eslint/eslint-plugin',
73
+ 'eslint-config-airbnb',
74
+ 'eslint-plugin-react',
75
+ ]);
76
+ });
77
+
78
+ test('scanConfigFiles reads the eslintConfig field of package.json', async () => {
79
+ const dir = tempProject({});
80
+ const packageJson = { eslintConfig: { extends: ['prettier'], plugins: ['import'] } };
81
+ const found = await scanConfigFiles(
82
+ dir,
83
+ ['eslint-config-prettier', 'eslint-plugin-import', 'eslint-plugin-vue'],
84
+ packageJson
85
+ );
86
+ assert.deepEqual([...found].sort(), ['eslint-config-prettier', 'eslint-plugin-import']);
87
+ });
@@ -0,0 +1,6 @@
1
+ import express from 'express';
2
+ const chalk = require('chalk');
3
+ import('ora').then(() => {});
4
+ export { get } from 'lodash/get';
5
+ import fs from 'fs';
6
+ import local from './local.js';
@@ -0,0 +1,61 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import fs from 'node:fs';
4
+ import os from 'node:os';
5
+ import path from 'node:path';
6
+ import { commandNames, scriptsUse, findScriptReferences } from '../src/script-scanner.js';
7
+
8
+ function tempProject(installed = {}) {
9
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'dd-scripts-'));
10
+ for (const [name, pkg] of Object.entries(installed)) {
11
+ const pkgDir = path.join(dir, 'node_modules', name);
12
+ fs.mkdirSync(pkgDir, { recursive: true });
13
+ fs.writeFileSync(path.join(pkgDir, 'package.json'), JSON.stringify({ name, ...pkg }));
14
+ }
15
+ return dir;
16
+ }
17
+
18
+ test('commandNames reads object and string bin fields from node_modules', () => {
19
+ const dir = tempProject({
20
+ typescript: { bin: { tsc: './bin/tsc', tsserver: './bin/tsserver' } },
21
+ '@scope/tool': { bin: './cli.js' },
22
+ });
23
+ assert.deepEqual(commandNames(dir, 'typescript').sort(), ['tsc', 'tsserver', 'typescript']);
24
+ assert.deepEqual(commandNames(dir, '@scope/tool').sort(), ['@scope/tool', 'tool']);
25
+ });
26
+
27
+ test('commandNames falls back to known commands when the package is not installed', () => {
28
+ const dir = tempProject();
29
+ assert.ok(commandNames(dir, 'typescript').includes('tsc'));
30
+ assert.ok(commandNames(dir, 'npm-run-all').includes('run-s'));
31
+ assert.deepEqual(commandNames(dir, 'left-pad'), ['left-pad']);
32
+ });
33
+
34
+ test('scriptsUse matches commands chained, prefixed, or via npx and .bin paths', () => {
35
+ const uses = (script, cmd) => scriptsUse([script], cmd);
36
+ assert.ok(uses('tsc -p .', 'tsc'));
37
+ assert.ok(uses('npm run clean&&tsc', 'tsc'));
38
+ assert.ok(uses('cross-env NODE_ENV=production tsc', 'tsc'));
39
+ assert.ok(uses('npx tsc --noEmit', 'tsc'));
40
+ assert.ok(uses('node_modules/.bin/tsc', 'tsc'));
41
+ assert.ok(uses('(tsc; echo done)', 'tsc'));
42
+ assert.ok(uses('lint-staged || exit 1', 'lint-staged'));
43
+ });
44
+
45
+ test('scriptsUse does not match other words containing the command', () => {
46
+ assert.ok(!scriptsUse(['tsc-watch src'], 'tsc'));
47
+ assert.ok(!scriptsUse(['echo mytsc'], 'tsc'));
48
+ assert.ok(!scriptsUse(['run-script build'], 'run-s'));
49
+ });
50
+
51
+ test('findScriptReferences returns packages whose commands appear in scripts', () => {
52
+ const dir = tempProject({
53
+ typescript: { bin: { tsc: './bin/tsc' } },
54
+ '@biomejs/biome': { bin: { biome: './bin/biome' } },
55
+ rimraf: { bin: { rimraf: './dist/bin.js' } },
56
+ lodash: {},
57
+ });
58
+ const scripts = { build: 'rimraf dist && tsc', lint: 'biome check .' };
59
+ const found = findScriptReferences(dir, ['typescript', '@biomejs/biome', 'rimraf', 'lodash'], scripts);
60
+ assert.deepEqual([...found].sort(), ['@biomejs/biome', 'rimraf', 'typescript']);
61
+ });
@@ -0,0 +1,55 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { mapOsvVulns } from '../src/security-checker.js';
4
+
5
+ test('mapOsvVulns maps OSV entries to a short display shape', () => {
6
+ const vulns = [
7
+ {
8
+ id: 'GHSA-1',
9
+ summary: 'Prototype pollution',
10
+ details: 'A very long markdown write-up that should not be printed',
11
+ database_specific: { severity: 'HIGH' },
12
+ affected: [
13
+ {
14
+ package: { ecosystem: 'npm', name: 'lodash' },
15
+ ranges: [{ type: 'SEMVER', events: [{ introduced: '0' }, { fixed: '4.17.21' }] }],
16
+ },
17
+ ],
18
+ },
19
+ ];
20
+ assert.deepEqual(mapOsvVulns(vulns, 'lodash'), [
21
+ {
22
+ id: 'GHSA-1',
23
+ severity: 'HIGH',
24
+ title: 'Prototype pollution',
25
+ fixedIn: '4.17.21',
26
+ url: 'https://osv.dev/vulnerability/GHSA-1',
27
+ },
28
+ ]);
29
+ });
30
+
31
+ test('mapOsvVulns only reads fixed versions for the queried package', () => {
32
+ const [mapped] = mapOsvVulns(
33
+ [
34
+ {
35
+ id: 'GHSA-3',
36
+ affected: [
37
+ { package: { name: 'other' }, ranges: [{ events: [{ fixed: '9.9.9' }] }] },
38
+ ],
39
+ },
40
+ ],
41
+ 'lodash'
42
+ );
43
+ assert.equal(mapped.fixedIn, null);
44
+ });
45
+
46
+ test('mapOsvVulns falls back when fields are missing', () => {
47
+ const [mapped] = mapOsvVulns([{ id: 'GHSA-2' }], 'x');
48
+ assert.equal(mapped.severity, 'UNKNOWN');
49
+ assert.equal(mapped.title, 'GHSA-2');
50
+ assert.equal(mapped.fixedIn, null);
51
+ });
52
+
53
+ test('mapOsvVulns handles undefined input', () => {
54
+ assert.deepEqual(mapOsvVulns(), []);
55
+ });
@@ -0,0 +1,18 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { resolveTypesUsage } from '../src/unused-detector.js';
4
+
5
+ test('marks @types/* used when its base package is referenced', () => {
6
+ const referenced = new Set(['react', '@babel/core']);
7
+ const deps = ['@types/react', '@types/babel__core', '@types/lodash', '@types/node'];
8
+ const used = resolveTypesUsage(referenced, deps);
9
+ assert.ok(used.has('@types/react'), 'react referenced');
10
+ assert.ok(used.has('@types/babel__core'), 'maps to @babel/core');
11
+ assert.ok(used.has('@types/node'), 'node always used');
12
+ assert.ok(!used.has('@types/lodash'), 'lodash not referenced');
13
+ });
14
+
15
+ test('ignores non-@types deps', () => {
16
+ const used = resolveTypesUsage(new Set(['react']), ['react', 'lodash']);
17
+ assert.equal(used.size, 0);
18
+ });