|
1 | 1 | import { ReflectionSymbolId } from "#models"; |
2 | | -import { findPackageForPath, getCommonDirectory, getQualifiedName, normalizePath, readFile } from "#node-utils"; |
3 | | -import { type NormalizedPath, Validation } from "#utils"; |
4 | | -import { existsSync } from "fs"; |
5 | | -import { join, relative, resolve } from "node:path"; |
| 2 | +import { findPackageForPath, getQualifiedName, normalizePath, resolveDeclarationMaps } from "#node-utils"; |
| 3 | +import { type NormalizedPath } from "#utils"; |
| 4 | +import { relative } from "node:path"; |
6 | 5 | import ts from "typescript"; |
7 | 6 |
|
8 | | -const declarationMapCache = new Map<string, string>(); |
9 | | - |
10 | 7 | let transientCount = 0; |
11 | 8 | const transientIds = new WeakMap<ts.Symbol, number>(); |
12 | 9 |
|
@@ -51,66 +48,3 @@ export function createSymbolIdImpl(symbol: ts.Symbol, declaration?: ts.Declarati |
51 | 48 |
|
52 | 49 | return id; |
53 | 50 | } |
54 | | - |
55 | | -function resolveDeclarationMaps(file: string): string { |
56 | | - if (!/\.d\.[cm]?ts$/.test(file)) return file; |
57 | | - if (declarationMapCache.has(file)) return declarationMapCache.get(file)!; |
58 | | - |
59 | | - const mapFile = file + ".map"; |
60 | | - if (!existsSync(mapFile)) return file; |
61 | | - |
62 | | - let sourceMap: unknown; |
63 | | - try { |
64 | | - sourceMap = JSON.parse(readFile(mapFile)) as unknown; |
65 | | - } catch { |
66 | | - return file; |
67 | | - } |
68 | | - |
69 | | - if ( |
70 | | - Validation.validate( |
71 | | - { |
72 | | - file: String, |
73 | | - sourceRoot: Validation.optional(String), |
74 | | - sources: [Array, String], |
75 | | - }, |
76 | | - sourceMap, |
77 | | - ) |
78 | | - ) { |
79 | | - // There's a pretty large assumption in here that we only have |
80 | | - // 1 source file per js file. This is a pretty standard typescript approach, |
81 | | - // but people might do interesting things with transpilation that could break this. |
82 | | - let source = sourceMap.sources[0]; |
83 | | - |
84 | | - // If we have a sourceRoot, trim any leading slash from the source, and join them |
85 | | - // Similar to how it's done at https://github.com/mozilla/source-map/blob/58819f09018d56ef84dc41ba9c93f554e0645169/lib/util.js#L412 |
86 | | - if (sourceMap.sourceRoot !== undefined) { |
87 | | - source = source.replace(/^\//, ""); |
88 | | - source = join(sourceMap.sourceRoot, source); |
89 | | - } |
90 | | - |
91 | | - const result = resolve(mapFile, "..", source); |
92 | | - declarationMapCache.set(file, result); |
93 | | - return result; |
94 | | - } |
95 | | - |
96 | | - return file; |
97 | | -} |
98 | | - |
99 | | -// See also: inferEntryPoints in entry-point.ts |
100 | | -export function addInferredDeclarationMapPaths( |
101 | | - opts: ts.CompilerOptions, |
102 | | - files: readonly string[], |
103 | | -) { |
104 | | - const rootDir = opts.rootDir || getCommonDirectory(files); |
105 | | - const declDir = opts.declarationDir || opts.outDir || rootDir; |
106 | | - |
107 | | - for (const file of files) { |
108 | | - const mapFile = normalizePath( |
109 | | - resolve(declDir, relative(rootDir, file)).replace( |
110 | | - /\.([cm]?[tj]s)x?$/, |
111 | | - ".d.$1", |
112 | | - ), |
113 | | - ); |
114 | | - declarationMapCache.set(mapFile, file); |
115 | | - } |
116 | | -} |
0 commit comments