@jay-framework/compiler-analyze-exported-types 0.25.4 → 0.26.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.
package/dist/index.d.cts CHANGED
@@ -1,3 +1,4 @@
1
+ import * as ts from 'typescript';
1
2
  import { JayType } from '@jay-framework/compiler-shared';
2
3
 
3
4
  interface ResolveTsConfigOptions {
@@ -6,5 +7,27 @@ interface ResolveTsConfigOptions {
6
7
  declare function resolveTsConfig(options: ResolveTsConfigOptions): string;
7
8
 
8
9
  declare function analyzeExportedTypes(filename: string, options?: ResolveTsConfigOptions): JayType[];
10
+ /**
11
+ * Extract Jay types from a source file of an already-built program. Shared by the one-shot
12
+ * {@link analyzeExportedTypes} (fresh `createProgram` per call) and the reusable {@link createTypeAnalyzer}
13
+ * (persistent `LanguageService`), so both produce identical output (DL#225).
14
+ */
15
+ declare function extractExportedTypes(program: ts.Program, filename: string): JayType[];
16
+ /**
17
+ * A reusable type analyzer (DL#225). Owns a persistent `ts.LanguageService` (one per resolved tsconfig)
18
+ * so `lib.d.ts` + sources stay parsed across calls — the cold `createProgram`-per-call cost is paid once,
19
+ * and re-analysis after an edit is incremental. Created per owner (dev-server session / build run / test),
20
+ * never module-global, so lifetimes are scoped and tests are isolated.
21
+ *
22
+ * Invalidation: `noteFileChanged` bumps the file's LanguageService version (so the service re-reads it and
23
+ * re-checks dependents — catching transitive edits the file's own mtime can't) and a global generation (so
24
+ * the result cache drops). The dev server calls it from its file watcher; the build is one-shot.
25
+ */
26
+ interface TypeAnalyzer {
27
+ analyze(filename: string, options?: ResolveTsConfigOptions): JayType[];
28
+ noteFileChanged(filename: string): void;
29
+ clear(): void;
30
+ }
31
+ declare function createTypeAnalyzer(): TypeAnalyzer;
9
32
 
10
- export { type ResolveTsConfigOptions, analyzeExportedTypes, resolveTsConfig };
33
+ export { type ResolveTsConfigOptions, type TypeAnalyzer, analyzeExportedTypes, createTypeAnalyzer, extractExportedTypes, resolveTsConfig };
package/dist/index.js CHANGED
@@ -30,6 +30,11 @@ const {
30
30
  isPropertySignature,
31
31
  convertCompilerOptionsFromJson,
32
32
  createProgram,
33
+ createLanguageService,
34
+ createDocumentRegistry,
35
+ ScriptSnapshot,
36
+ getDefaultLibFilePath,
37
+ sys,
33
38
  isImportDeclaration,
34
39
  isInterfaceDeclaration,
35
40
  isNamedImports,
@@ -184,9 +189,12 @@ function mapImportedSymbols(statements, tsTypeChecker) {
184
189
  return SYMBOLS;
185
190
  }
186
191
  function analyzeExportedTypes(filename, options = {}) {
187
- let compilerOptions = loadTSCompilerOptions(options);
192
+ const compilerOptions = loadTSCompilerOptions(options);
188
193
  filename = autoAddExtension(filename);
189
194
  const program = createProgram([filename], compilerOptions);
195
+ return extractExportedTypes(program, filename);
196
+ }
197
+ function extractExportedTypes(program, filename) {
190
198
  const sourceFile = program.getSourceFile(filename);
191
199
  const tsTypeChecker = program.getTypeChecker();
192
200
  const types = [];
@@ -242,7 +250,74 @@ function analyzeExportedTypes(filename, options = {}) {
242
250
  }
243
251
  return types;
244
252
  }
253
+ function createTypeAnalyzer() {
254
+ const services = /* @__PURE__ */ new Map();
255
+ const resultCache = /* @__PURE__ */ new Map();
256
+ let generation = 0;
257
+ function getService(options) {
258
+ const key = resolveTsConfig(options) ?? "";
259
+ let svc = services.get(key);
260
+ if (!svc) {
261
+ const compilerOptions = loadTSCompilerOptions(options);
262
+ const roots = /* @__PURE__ */ new Set();
263
+ const versions = /* @__PURE__ */ new Map();
264
+ const host = {
265
+ getScriptFileNames: () => [...roots],
266
+ getScriptVersion: (f) => String(versions.get(f) ?? 0),
267
+ getScriptSnapshot: (f) => {
268
+ try {
269
+ return ScriptSnapshot.fromString(fs.readFileSync(f, "utf8"));
270
+ } catch {
271
+ return void 0;
272
+ }
273
+ },
274
+ getCurrentDirectory: () => process.cwd(),
275
+ getCompilationSettings: () => compilerOptions,
276
+ getDefaultLibFileName: (o) => getDefaultLibFilePath(o),
277
+ fileExists: sys.fileExists,
278
+ readFile: sys.readFile,
279
+ readDirectory: sys.readDirectory,
280
+ directoryExists: sys.directoryExists,
281
+ getDirectories: sys.getDirectories
282
+ };
283
+ svc = { ls: createLanguageService(host, createDocumentRegistry()), roots, versions };
284
+ services.set(key, svc);
285
+ }
286
+ return svc;
287
+ }
288
+ return {
289
+ analyze(filename, options = {}) {
290
+ filename = autoAddExtension(filename);
291
+ const cacheKey = `${filename}::${generation}`;
292
+ const cached = resultCache.get(cacheKey);
293
+ if (cached) return cached;
294
+ const svc = getService(options);
295
+ if (!svc.roots.has(filename)) {
296
+ svc.roots.add(filename);
297
+ if (!svc.versions.has(filename)) svc.versions.set(filename, 0);
298
+ }
299
+ const program = svc.ls.getProgram();
300
+ const result = program ? extractExportedTypes(program, filename) : [];
301
+ resultCache.set(cacheKey, result);
302
+ return result;
303
+ },
304
+ noteFileChanged(filename) {
305
+ const f = autoAddExtension(filename);
306
+ generation++;
307
+ for (const svc of services.values()) {
308
+ svc.versions.set(f, (svc.versions.get(f) ?? 0) + 1);
309
+ }
310
+ },
311
+ clear() {
312
+ services.clear();
313
+ resultCache.clear();
314
+ generation++;
315
+ }
316
+ };
317
+ }
245
318
  export {
246
319
  analyzeExportedTypes,
320
+ createTypeAnalyzer,
321
+ extractExportedTypes,
247
322
  resolveTsConfig
248
323
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jay-framework/compiler-analyze-exported-types",
3
- "version": "0.25.4",
3
+ "version": "0.26.0",
4
4
  "description": "",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/index.js",
@@ -25,10 +25,10 @@
25
25
  },
26
26
  "author": "",
27
27
  "dependencies": {
28
- "@jay-framework/compiler-shared": "^0.25.4",
29
- "@jay-framework/component": "^0.25.4",
30
- "@jay-framework/runtime": "^0.25.4",
31
- "@jay-framework/secure": "^0.25.4",
28
+ "@jay-framework/compiler-shared": "^0.26.0",
29
+ "@jay-framework/component": "^0.26.0",
30
+ "@jay-framework/runtime": "^0.26.0",
31
+ "@jay-framework/secure": "^0.26.0",
32
32
  "@types/js-yaml": "^4.0.9",
33
33
  "change-case": "^4.1.2",
34
34
  "js-yaml": "^4.1.0",
@@ -38,7 +38,7 @@
38
38
  },
39
39
  "devDependencies": {
40
40
  "@caiogondim/strip-margin": "^1.0.0",
41
- "@jay-framework/dev-environment": "^0.25.4",
41
+ "@jay-framework/dev-environment": "^0.26.0",
42
42
  "@testing-library/jest-dom": "^6.2.0",
43
43
  "@types/js-beautify": "^1",
44
44
  "@types/node": "^20.11.5",
@@ -47,7 +47,7 @@
47
47
  "js-beautify": "^1.14.11",
48
48
  "rimraf": "^5.0.5",
49
49
  "tsup": "^8.0.1",
50
- "vite": "^5.0.11",
51
- "vitest": "^1.2.1"
50
+ "vite": "^6.4.4",
51
+ "vitest": "^4.1.11"
52
52
  }
53
53
  }