code-inventory 0.2.0 → 0.2.1

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/dist/compiler.js CHANGED
@@ -2,7 +2,7 @@ import ts from 'typescript';
2
2
  import { isBuiltin } from 'node:module';
3
3
  import manifest from '../package.json' with { type: 'json' };
4
4
  import { realpathSync } from 'node:fs';
5
- import { resolve } from 'node:path';
5
+ import { dirname, extname, resolve } from 'node:path';
6
6
  import { loadProjects, projectOwners, fileRules } from "./projects.js";
7
7
  import { compare, discover, hash, hashFile, portablePath } from "./discovery.js";
8
8
  const limitations = [
@@ -13,6 +13,12 @@ const limitations = [
13
13
  'Each source file is counted in one project context; configuration-dependent alternatives may differ.',
14
14
  'Symbol IDs are snapshot-local and may change when source locations change.',
15
15
  ];
16
+ const javascriptDeclarations = new Map([
17
+ [ts.Extension.Js, ts.Extension.Dts],
18
+ [ts.Extension.Jsx, ts.Extension.Dts],
19
+ [ts.Extension.Mjs, ts.Extension.Dmts],
20
+ [ts.Extension.Cjs, ts.Extension.Dcts],
21
+ ]);
16
22
  /** Scan a project using the compiler API without emitting or executing project code. */
17
23
  export function scan(rootDirectory, settings = {}) {
18
24
  const root = realpathSync(rootDirectory);
@@ -116,7 +122,40 @@ function analyze(project, { root, owners, eligible, analyzed, rules, inventory,
116
122
  types: project.options.types ?? [],
117
123
  };
118
124
  const host = { ...ts.createCompilerHost(options), useSourceOfProjectReferenceRedirect: () => true };
119
- const program = ts.createProgram({ rootNames, options, host, projectReferences: project.references });
125
+ const programOptions = { rootNames, options, projectReferences: project.references };
126
+ let resolvedJavaScript = false;
127
+ const resolutionCache = ts.createModuleResolutionCache(host.getCurrentDirectory(), host.getCanonicalFileName, options);
128
+ const program = ts.createProgram({
129
+ ...programOptions,
130
+ host: {
131
+ ...host,
132
+ resolveModuleNameLiterals(literals, from, redirect, settings, source) {
133
+ return literals.map((literal) => {
134
+ const mode = ts.getModeForUsageLocation(source, literal, settings);
135
+ const result = ts.resolveModuleName(literal.text, from, settings, host, resolutionCache, redirect, mode);
136
+ const runtime = relativeJavaScript(from, literal.text, result.resolvedModule);
137
+ if (!runtime)
138
+ return result;
139
+ resolvedJavaScript = true;
140
+ return { ...result, resolvedModule: runtime };
141
+ });
142
+ },
143
+ },
144
+ });
145
+ function relativeJavaScript(from, specifier, resolved) {
146
+ if (!resolved || !/\.d\.[cm]?ts$/.test(resolved.resolvedFileName) || !/^\.{1,2}\//.test(specifier))
147
+ return;
148
+ if (!javascriptDeclarations.has(extname(from)))
149
+ return;
150
+ const extension = extname(specifier);
151
+ const declarationExtension = javascriptDeclarations.get(extension);
152
+ if (!declarationExtension)
153
+ return;
154
+ const runtime = resolve(dirname(from), specifier);
155
+ if (eligible.has(runtime) &&
156
+ resolved.resolvedFileName === runtime.slice(0, -extension.length) + declarationExtension)
157
+ return { ...resolved, resolvedFileName: runtime, extension };
158
+ }
120
159
  const checker = program.getTypeChecker();
121
160
  const sources = program
122
161
  .getSourceFiles()
@@ -138,10 +177,12 @@ function analyze(project, { root, owners, eligible, analyzed, rules, inventory,
138
177
  collectExports(source);
139
178
  visit(source, null);
140
179
  }
180
+ // Keep the project's declared types authoritative for compiler diagnostics.
181
+ const diagnosticProgram = resolvedJavaScript ? ts.createProgram({ ...programOptions, host }) : program;
141
182
  for (const diagnostic of [
142
- ...program.getSyntacticDiagnostics(),
143
- ...program.getOptionsDiagnostics(),
144
- ...program.getSemanticDiagnostics(),
183
+ ...diagnosticProgram.getSyntacticDiagnostics(),
184
+ ...diagnosticProgram.getOptionsDiagnostics(),
185
+ ...diagnosticProgram.getSemanticDiagnostics(),
145
186
  ]) {
146
187
  if (!diagnostic.file || owners.get(diagnostic.file.fileName) === project)
147
188
  addCompilerDiagnostic(diagnostic);
@@ -367,7 +408,9 @@ function analyze(project, { root, owners, eligible, analyzed, rules, inventory,
367
408
  kind,
368
409
  location: location(specifier),
369
410
  typeOnly,
370
- resolvedFile: resolved ? portablePath(root, resolved.resolvedFileName) : null,
411
+ resolvedFile: resolved
412
+ ? portablePath(root, (relativeJavaScript(node.getSourceFile().fileName, specifier.text, resolved) ?? resolved).resolvedFileName)
413
+ : null,
371
414
  });
372
415
  if (!resolved && !isBuiltin(specifier.text))
373
416
  diagnostics.push({
@@ -122,6 +122,11 @@
122
122
  <code>.next</code>, <code>.nuxt</code>, and <code>.output</code>. The compiler can still read declarations for
123
123
  resolution. Discovered files excluded from the inventory carry a reason.
124
124
  </p>
125
+ <p>
126
+ When a JavaScript file imports an explicit relative path such as <code>./codec.js</code>, the scanner traces
127
+ references into that implementation if it is in scope, even when it has an adjacent declaration file.
128
+ TypeScript diagnostics still use the project's declared types.
129
+ </p>
125
130
  <p>
126
131
  Paths containing <code>test</code>, <code>tests</code>, <code>__tests__</code>, <code>__mocks__</code>, or
127
132
  <code>fixtures</code> as directory names, and <code>.test</code> or <code>.spec</code> files, receive test
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "code-inventory",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "description": "Static code inventory for JavaScript and TypeScript. Find symbol usages and trace paths from entry points.",