@@ -2,7 +2,7 @@ import { existsSync } from "fs";
2
2
import { isAbsolute , join , relative , resolve } from "path" ;
3
3
import ts from "typescript" ;
4
4
import type { JSONOutput , Serializer } from "../../serialization/index" ;
5
- import { readFile } from "../../utils/fs" ;
5
+ import { getCommonDirectory , readFile } from "../../utils/fs" ;
6
6
import { getQualifiedName } from "../../utils/tsutils" ;
7
7
import { optional , validate } from "../../utils/validation" ;
8
8
import { normalizePath } from "../../utils/paths" ;
@@ -61,27 +61,26 @@ export class ReflectionSymbolId {
61
61
}
62
62
63
63
toObject ( serializer : Serializer ) {
64
+ const sourceFileName = isAbsolute ( this . fileName )
65
+ ? normalizePath (
66
+ relative (
67
+ serializer . projectRoot ,
68
+ resolveDeclarationMaps ( this . fileName ) ,
69
+ ) ,
70
+ )
71
+ : this . fileName ;
72
+
64
73
return {
65
- sourceFileName : isAbsolute ( this . fileName )
66
- ? normalizePath (
67
- relative (
68
- serializer . projectRoot ,
69
- resolveDeclarationMaps ( this . fileName ) ,
70
- ) ,
71
- )
72
- : this . fileName ,
74
+ sourceFileName,
73
75
qualifiedName : this . qualifiedName ,
74
76
} ;
75
77
}
76
78
}
77
79
78
80
const declarationMapCache = new Map < string , string > ( ) ;
79
81
80
- /**
81
- * See also getTsSourceFromJsSource in package-manifest.ts.
82
- */
83
82
function resolveDeclarationMaps ( file : string ) : string {
84
- if ( ! file . endsWith ( ".d.ts" ) ) return file ;
83
+ if ( ! / \. d \. [ c m ] ? t s $ / . test ( file ) ) return file ;
85
84
if ( declarationMapCache . has ( file ) ) return declarationMapCache . get ( file ) ! ;
86
85
87
86
const mapFile = file + ".map" ;
@@ -123,3 +122,19 @@ function resolveDeclarationMaps(file: string): string {
123
122
124
123
return file ;
125
124
}
125
+
126
+ export function addInferredDeclarationMapPaths (
127
+ opts : ts . CompilerOptions ,
128
+ files : readonly string [ ] ,
129
+ ) {
130
+ const rootDir = opts . rootDir || getCommonDirectory ( files ) ;
131
+ const declDir = opts . declarationDir || opts . outDir || rootDir ;
132
+
133
+ for ( const file of files ) {
134
+ const mapFile = resolve ( declDir , relative ( rootDir , file ) ) . replace (
135
+ / \. ( [ c m ] ? [ t j ] s ) x ? $ / ,
136
+ ".d.$1" ,
137
+ ) ;
138
+ declarationMapCache . set ( mapFile , file ) ;
139
+ }
140
+ }
0 commit comments