mo3ta-coverage 1.0.9 → 1.0.12
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/README.md +46 -58
- package/RECOMMENDED_USAGE.md +67 -0
- package/coverage/clover.xml +427 -0
- package/coverage/coverage-final.json +5 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/cli.js.html +1252 -0
- package/coverage/lcov-report/coverage/index.html +116 -0
- package/coverage/lcov-report/coverage/index.js.html +364 -0
- package/coverage/lcov-report/coverage/src/cli.js.html +652 -0
- package/coverage/lcov-report/coverage/src/fileUtils.js.html +130 -0
- package/coverage/lcov-report/coverage/src/gitUtils.js.html +145 -0
- package/coverage/lcov-report/coverage/src/index.html +146 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/fileUtils.js.html +775 -0
- package/coverage/lcov-report/gitUtils.js.html +145 -0
- package/coverage/lcov-report/importer.js.html +1051 -0
- package/coverage/lcov-report/index.html +161 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +196 -0
- package/coverage/lcov.info +799 -0
- package/index.js +43 -8
- package/package.json +3 -2
- package/src/cli.js +252 -52
- package/src/cli.test.js +113 -0
- package/src/fileUtils.js +215 -0
- package/src/fileUtils.test.js +182 -1
- package/src/importer.js +323 -0
- package/.github/workflows/publish.yml +0 -29
- package/.mo3ta-coverage.json +0 -4
package/src/importer.js
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import ts from 'typescript';
|
|
4
|
+
|
|
5
|
+
// =============================================================================
|
|
6
|
+
// AST -> import specifiers
|
|
7
|
+
// =============================================================================
|
|
8
|
+
|
|
9
|
+
function inferScriptKind(filePath) {
|
|
10
|
+
if (filePath.endsWith('.tsx')) return ts.ScriptKind.TSX;
|
|
11
|
+
if (filePath.endsWith('.jsx')) return ts.ScriptKind.JSX;
|
|
12
|
+
if (
|
|
13
|
+
filePath.endsWith('.js') ||
|
|
14
|
+
filePath.endsWith('.mjs') ||
|
|
15
|
+
filePath.endsWith('.cjs')
|
|
16
|
+
) {
|
|
17
|
+
return ts.ScriptKind.JS;
|
|
18
|
+
}
|
|
19
|
+
return ts.ScriptKind.TS;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function getImportSpecifiers(filePath, opts) {
|
|
23
|
+
let sourceCode;
|
|
24
|
+
try {
|
|
25
|
+
sourceCode = fs.readFileSync(filePath, 'utf-8');
|
|
26
|
+
} catch {
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const sourceFile = ts.createSourceFile(
|
|
31
|
+
filePath,
|
|
32
|
+
sourceCode,
|
|
33
|
+
ts.ScriptTarget.Latest,
|
|
34
|
+
/* setParentNodes */ true,
|
|
35
|
+
inferScriptKind(filePath)
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
const specifiers = [];
|
|
39
|
+
|
|
40
|
+
function visit(node) {
|
|
41
|
+
// import ... from '...'
|
|
42
|
+
if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier)) {
|
|
43
|
+
specifiers.push(node.moduleSpecifier.text);
|
|
44
|
+
}
|
|
45
|
+
// export ... from '...'
|
|
46
|
+
else if (
|
|
47
|
+
opts.includeReExports &&
|
|
48
|
+
ts.isExportDeclaration(node) &&
|
|
49
|
+
node.moduleSpecifier &&
|
|
50
|
+
ts.isStringLiteral(node.moduleSpecifier)
|
|
51
|
+
) {
|
|
52
|
+
specifiers.push(node.moduleSpecifier.text);
|
|
53
|
+
}
|
|
54
|
+
// import('...') dynamic
|
|
55
|
+
else if (
|
|
56
|
+
opts.includeDynamicImports &&
|
|
57
|
+
ts.isCallExpression(node) &&
|
|
58
|
+
node.expression.kind === ts.SyntaxKind.ImportKeyword &&
|
|
59
|
+
node.arguments[0] &&
|
|
60
|
+
ts.isStringLiteral(node.arguments[0])
|
|
61
|
+
) {
|
|
62
|
+
specifiers.push(node.arguments[0].text);
|
|
63
|
+
}
|
|
64
|
+
// require('...')
|
|
65
|
+
else if (
|
|
66
|
+
opts.includeDynamicImports &&
|
|
67
|
+
ts.isCallExpression(node) &&
|
|
68
|
+
ts.isIdentifier(node.expression) &&
|
|
69
|
+
node.expression.text === 'require' &&
|
|
70
|
+
node.arguments.length === 1 &&
|
|
71
|
+
ts.isStringLiteral(node.arguments[0])
|
|
72
|
+
) {
|
|
73
|
+
specifiers.push(node.arguments[0].text);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
ts.forEachChild(node, visit);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
visit(sourceFile);
|
|
80
|
+
return specifiers;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// =============================================================================
|
|
84
|
+
// tsconfig + module resolution
|
|
85
|
+
// =============================================================================
|
|
86
|
+
|
|
87
|
+
function loadCompilerOptions(projectDir, tsconfigPath) {
|
|
88
|
+
const configPath =
|
|
89
|
+
tsconfigPath ||
|
|
90
|
+
ts.findConfigFile(projectDir, ts.sys.fileExists, 'tsconfig.json');
|
|
91
|
+
|
|
92
|
+
if (!configPath) {
|
|
93
|
+
return {
|
|
94
|
+
options: { allowJs: true, esModuleInterop: true },
|
|
95
|
+
configDir: path.resolve(projectDir),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const configFile = ts.readConfigFile(configPath, ts.sys.readFile);
|
|
100
|
+
if (configFile.error) {
|
|
101
|
+
throw new Error(
|
|
102
|
+
`Failed to read tsconfig: ${ts.flattenDiagnosticMessageText(
|
|
103
|
+
configFile.error.messageText,
|
|
104
|
+
'\n'
|
|
105
|
+
)}`
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const configDir = path.dirname(path.resolve(configPath));
|
|
110
|
+
const parsed = ts.parseJsonConfigFileContent(configFile.config, ts.sys, configDir);
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
options: Object.assign({ allowJs: true }, parsed.options),
|
|
114
|
+
configDir,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function resolveImport(specifier, containingFile, options, host, cache) {
|
|
119
|
+
const result = ts.resolveModuleName(
|
|
120
|
+
specifier,
|
|
121
|
+
containingFile,
|
|
122
|
+
options,
|
|
123
|
+
host,
|
|
124
|
+
cache
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
const resolved = result.resolvedModule;
|
|
128
|
+
if (!resolved) return null;
|
|
129
|
+
if (resolved.isExternalLibraryImport) return null; // skip node_modules
|
|
130
|
+
if (resolved.extension === ts.Extension.Dts) return null; // skip .d.ts
|
|
131
|
+
return path.resolve(resolved.resolvedFileName);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// =============================================================================
|
|
135
|
+
// File walking
|
|
136
|
+
// =============================================================================
|
|
137
|
+
|
|
138
|
+
const DEFAULT_EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs'];
|
|
139
|
+
const DEFAULT_IGNORE = ['node_modules', '.git', 'dist', 'build', 'coverage'];
|
|
140
|
+
|
|
141
|
+
function findSourceFiles(rootDir, extensions, ignore) {
|
|
142
|
+
const out = [];
|
|
143
|
+
const absRoot = path.resolve(rootDir);
|
|
144
|
+
|
|
145
|
+
function walk(dir) {
|
|
146
|
+
let entries;
|
|
147
|
+
try {
|
|
148
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
149
|
+
} catch {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
for (const entry of entries) {
|
|
154
|
+
const full = path.join(dir, entry.name);
|
|
155
|
+
|
|
156
|
+
if (ignore.some((pat) => full.includes(pat))) continue;
|
|
157
|
+
|
|
158
|
+
if (entry.isDirectory()) {
|
|
159
|
+
if (entry.name.startsWith('.')) continue;
|
|
160
|
+
walk(full);
|
|
161
|
+
} else if (entry.isFile()) {
|
|
162
|
+
if (entry.name.endsWith('.d.ts')) continue;
|
|
163
|
+
if (extensions.some((ext) => entry.name.endsWith(ext))) {
|
|
164
|
+
out.push(full);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
walk(absRoot);
|
|
171
|
+
return out;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// =============================================================================
|
|
175
|
+
// Build the reverse-dependency graph
|
|
176
|
+
// =============================================================================
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* @typedef {Object} ImporterGraphOptions
|
|
180
|
+
* @property {string} projectDir
|
|
181
|
+
* @property {string} [tsconfigPath]
|
|
182
|
+
* @property {string[]} [extensions]
|
|
183
|
+
* @property {string[]} [ignore]
|
|
184
|
+
* @property {boolean} [includeReExports]
|
|
185
|
+
* @property {boolean} [includeDynamicImports]
|
|
186
|
+
*/
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* @typedef {Object} ReverseGraph
|
|
190
|
+
* @property {Map<string, Set<string>>} edges file -> direct importers
|
|
191
|
+
* @property {string[]} allFiles
|
|
192
|
+
*/
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* @param {ImporterGraphOptions} opts
|
|
196
|
+
* @returns {ReverseGraph}
|
|
197
|
+
*/
|
|
198
|
+
export function buildReverseGraph(opts) {
|
|
199
|
+
const {
|
|
200
|
+
projectDir,
|
|
201
|
+
tsconfigPath,
|
|
202
|
+
extensions = DEFAULT_EXTENSIONS,
|
|
203
|
+
ignore = DEFAULT_IGNORE,
|
|
204
|
+
includeReExports = true,
|
|
205
|
+
includeDynamicImports = true,
|
|
206
|
+
} = opts;
|
|
207
|
+
|
|
208
|
+
const { options, configDir } = loadCompilerOptions(projectDir, tsconfigPath);
|
|
209
|
+
const host = ts.createCompilerHost(options, /* setParentNodes */ true);
|
|
210
|
+
const cache = ts.createModuleResolutionCache(configDir, (s) => s, options);
|
|
211
|
+
|
|
212
|
+
const allFiles = findSourceFiles(projectDir, extensions, ignore);
|
|
213
|
+
const edges = new Map();
|
|
214
|
+
|
|
215
|
+
const specOpts = { includeReExports, includeDynamicImports };
|
|
216
|
+
|
|
217
|
+
for (const file of allFiles) {
|
|
218
|
+
const specifiers = getImportSpecifiers(file, specOpts);
|
|
219
|
+
for (const spec of specifiers) {
|
|
220
|
+
const resolved = resolveImport(spec, file, options, host, cache);
|
|
221
|
+
if (!resolved) continue;
|
|
222
|
+
let importers = edges.get(resolved);
|
|
223
|
+
if (!importers) {
|
|
224
|
+
importers = new Set();
|
|
225
|
+
edges.set(resolved, importers);
|
|
226
|
+
}
|
|
227
|
+
importers.add(file);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return { edges, allFiles };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// =============================================================================
|
|
235
|
+
// Main API: find importers up to a max depth (BFS)
|
|
236
|
+
// =============================================================================
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* @typedef {Object} ImporterNode
|
|
240
|
+
* @property {string} file
|
|
241
|
+
* @property {number} depth
|
|
242
|
+
* @property {string} [importedBy]
|
|
243
|
+
*/
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* @typedef {Object} FindImportersResult
|
|
247
|
+
* @property {string} target
|
|
248
|
+
* @property {ImporterNode[]} importers
|
|
249
|
+
* @property {number} maxDepthReached
|
|
250
|
+
*/
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* @param {string} targetFile
|
|
254
|
+
* @param {number} maxDepth
|
|
255
|
+
* @param {ReverseGraph} graph
|
|
256
|
+
* @returns {FindImportersResult}
|
|
257
|
+
*/
|
|
258
|
+
export function findImportersWithDepth(targetFile, maxDepth, graph) {
|
|
259
|
+
if (maxDepth < 1) {
|
|
260
|
+
throw new Error(`maxDepth must be >= 1, got ${maxDepth}`);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const target = path.resolve(targetFile);
|
|
264
|
+
const visited = new Map(); // file -> ImporterNode
|
|
265
|
+
const queue = [{ file: target, depth: 0, importedBy: undefined }];
|
|
266
|
+
let maxDepthReached = 0;
|
|
267
|
+
|
|
268
|
+
while (queue.length > 0) {
|
|
269
|
+
const { file, depth, importedBy } = queue.shift();
|
|
270
|
+
|
|
271
|
+
if (visited.has(file)) continue;
|
|
272
|
+
|
|
273
|
+
if (file !== target) {
|
|
274
|
+
visited.set(file, { file, depth, importedBy });
|
|
275
|
+
if (depth > maxDepthReached) maxDepthReached = depth;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (depth >= maxDepth) continue;
|
|
279
|
+
|
|
280
|
+
const directImporters = graph.edges.get(file);
|
|
281
|
+
if (!directImporters) continue;
|
|
282
|
+
|
|
283
|
+
for (const importer of directImporters) {
|
|
284
|
+
if (!visited.has(importer) && importer !== target) {
|
|
285
|
+
queue.push({ file: importer, depth: depth + 1, importedBy: file });
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const importers = Array.from(visited.values()).sort(
|
|
291
|
+
(a, b) => a.depth - b.depth || a.file.localeCompare(b.file)
|
|
292
|
+
);
|
|
293
|
+
|
|
294
|
+
return { target, importers, maxDepthReached };
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Convenience: build graph + query in one call.
|
|
299
|
+
* @param {string} targetFile
|
|
300
|
+
* @param {number} maxDepth
|
|
301
|
+
* @param {ImporterGraphOptions} opts
|
|
302
|
+
* @returns {FindImportersResult}
|
|
303
|
+
*/
|
|
304
|
+
export function findImporters(targetFile, maxDepth, opts) {
|
|
305
|
+
const graph = buildReverseGraph(opts);
|
|
306
|
+
return findImportersWithDepth(targetFile, maxDepth, graph);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Convenience: query many targets against the same graph.
|
|
311
|
+
* @param {string[]} targets
|
|
312
|
+
* @param {number} maxDepth
|
|
313
|
+
* @param {ImporterGraphOptions} opts
|
|
314
|
+
* @returns {Map<string, FindImportersResult>}
|
|
315
|
+
*/
|
|
316
|
+
export function findImportersForMany(targets, maxDepth, opts) {
|
|
317
|
+
const graph = buildReverseGraph(opts);
|
|
318
|
+
const results = new Map();
|
|
319
|
+
for (const t of targets) {
|
|
320
|
+
results.set(path.resolve(t), findImportersWithDepth(t, maxDepth, graph));
|
|
321
|
+
}
|
|
322
|
+
return results;
|
|
323
|
+
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
name: Publish Package
|
|
2
|
-
on:
|
|
3
|
-
push:
|
|
4
|
-
branches:
|
|
5
|
-
- main
|
|
6
|
-
|
|
7
|
-
jobs:
|
|
8
|
-
publish:
|
|
9
|
-
runs-on: ubuntu-latest
|
|
10
|
-
steps:
|
|
11
|
-
- name: Checkout code
|
|
12
|
-
uses: actions/checkout@v2
|
|
13
|
-
|
|
14
|
-
- name: Set up Node.js
|
|
15
|
-
uses: actions/setup-node@v2
|
|
16
|
-
with:
|
|
17
|
-
node-version: '16'
|
|
18
|
-
|
|
19
|
-
- name: Install dependencies
|
|
20
|
-
run: npm install
|
|
21
|
-
|
|
22
|
-
# - name: Run tests
|
|
23
|
-
# run: npm test
|
|
24
|
-
|
|
25
|
-
- name: Configure npm authentication
|
|
26
|
-
run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > ~/.npmrc
|
|
27
|
-
|
|
28
|
-
- name: Publish package
|
|
29
|
-
run: npm publish
|
package/.mo3ta-coverage.json
DELETED