@@ -130,59 +130,60 @@ namespace ts {
130
130
}
131
131
132
132
export function explainFiles ( program : Program , write : ( s : string ) => void ) {
133
- const configExplainInfo = getFileFromConfigExplainInfo ( program ) ;
134
133
const reasons = program . getFileIncludeReasons ( ) ;
134
+ const getCanonicalFileName = createGetCanonicalFileName ( program . useCaseSensitiveFileNames ( ) ) ;
135
+ const relativeFileName = ( fileName : string ) => convertToRelativePath ( fileName , program . getCurrentDirectory ( ) , getCanonicalFileName ) ;
135
136
for ( const file of program . getSourceFiles ( ) ) {
136
- write ( `${ toFileName ( program , file , /* relativeFileName*/ true ) } ` ) ;
137
- reasons . get ( file . path ) ?. forEach ( reason => write ( ` ${ fileIncludeReasonToDiagnostics ( program , reason , /*relativeFileNames*/ true , configExplainInfo ) . messageText } ` ) ) ;
138
- explainIfFileIsRedirect ( program , file , /*relativeFileNames*/ true ) ?. forEach ( d => write ( ` ${ d . messageText } ` ) ) ;
137
+ write ( `${ toFileName ( file , relativeFileName ) } ` ) ;
138
+ reasons . get ( file . path ) ?. forEach ( reason => write ( ` ${ fileIncludeReasonToDiagnostics ( program , reason , relativeFileName ) . messageText } ` ) ) ;
139
+ explainIfFileIsRedirect ( program , file , relativeFileName ) ?. forEach ( d => write ( ` ${ d . messageText } ` ) ) ;
139
140
}
140
141
}
141
142
142
- export function explainIfFileIsRedirect ( program : Program , file : SourceFile , relativeFileNames ?: boolean ) : DiagnosticMessageChain [ ] | undefined {
143
+ export function explainIfFileIsRedirect ( _program : Program , file : SourceFile , fileNameConvertor ?: ( fileName : string ) => string ) : DiagnosticMessageChain [ ] | undefined {
143
144
let result : DiagnosticMessageChain [ ] | undefined ;
144
145
if ( file . path !== file . resolvedPath ) {
145
146
( result ||= [ ] ) . push ( chainDiagnosticMessages (
146
147
/*details*/ undefined ,
147
148
Diagnostics . File_is_output_of_project_reference_source_0 ,
148
- toFileName ( program , file . originalFileName , relativeFileNames )
149
+ toFileName ( file . originalFileName , fileNameConvertor )
149
150
) ) ;
150
151
}
151
152
if ( file . redirectInfo ) {
152
153
( result ||= [ ] ) . push ( chainDiagnosticMessages (
153
154
/*details*/ undefined ,
154
155
Diagnostics . File_redirects_to_file_0 ,
155
- toFileName ( program , file . redirectInfo . redirectTarget , relativeFileNames )
156
+ toFileName ( file . redirectInfo . redirectTarget , fileNameConvertor )
156
157
) ) ;
157
158
}
158
159
return result ;
159
160
}
160
161
161
- export type FileFromConfigExplainInfo = ReturnType < typeof getFileFromConfigExplainInfo > ;
162
- export function getFileFromConfigExplainInfo ( program : Program ) {
163
- const options = program . getCompilerOptions ( ) ;
164
- const { validatedIncludeSpecs } = options . configFile ?. configFileSpecs || { } ;
162
+ export function getMatchedFileSpec ( program : Program , fileName : string ) {
163
+ const configFile = program . getCompilerOptions ( ) . configFile ;
164
+ if ( ! configFile ?. configFileSpecs ?. validatedFilesSpec ) return undefined ;
165
+
166
+ const getCanonicalFileName = createGetCanonicalFileName ( program . useCaseSensitiveFileNames ( ) ) ;
167
+ const filePath = getCanonicalFileName ( fileName ) ;
168
+ const basePath = getDirectoryPath ( getNormalizedAbsolutePath ( configFile . fileName , program . getCurrentDirectory ( ) ) ) ;
169
+ return find ( configFile . configFileSpecs . validatedFilesSpec , fileSpec => getCanonicalFileName ( getNormalizedAbsolutePath ( fileSpec , basePath ) ) === filePath ) ;
170
+ }
171
+
172
+ export function getMatchedIncludeSpec ( program : Program , fileName : string ) {
173
+ const configFile = program . getCompilerOptions ( ) . configFile ;
174
+ if ( ! configFile ?. configFileSpecs ?. validatedIncludeSpecs ) return undefined ;
175
+
176
+ const isJsonFile = fileExtensionIs ( fileName , Extension . Json ) ;
177
+ const basePath = getDirectoryPath ( getNormalizedAbsolutePath ( configFile . fileName , program . getCurrentDirectory ( ) ) ) ;
165
178
const useCaseSensitiveFileNames = program . useCaseSensitiveFileNames ( ) ;
166
- const basePath = options . configFile && getDirectoryPath ( getNormalizedAbsolutePath ( options . configFile . fileName , program . getCurrentDirectory ( ) ) ) ;
167
- let includeSpecs : { include : string ; regExp : RegExp ; } [ ] | undefined ;
168
- forEach ( validatedIncludeSpecs , include => {
169
- const pattern = getPatternFromSpec ( include , basePath ! , "files" ) ;
170
- if ( ! pattern ) return ;
171
- ( includeSpecs ||= [ ] ) . push ( {
172
- include,
173
- regExp : getRegexFromPattern ( `(${ pattern } )$` , useCaseSensitiveFileNames )
174
- } ) ;
179
+ return find ( configFile ?. configFileSpecs ?. validatedIncludeSpecs , includeSpec => {
180
+ if ( isJsonFile && ! endsWith ( includeSpec , Extension . Json ) ) return false ;
181
+ const pattern = getPatternFromSpec ( includeSpec , basePath , "files" ) ;
182
+ return ! ! pattern && getRegexFromPattern ( `(${ pattern } )$` , useCaseSensitiveFileNames ) . test ( fileName ) ;
175
183
} ) ;
176
- const getCanonicalFileName = createGetCanonicalFileName ( useCaseSensitiveFileNames )
177
- return { basePath, includeSpecs, getCanonicalFileName } ;
178
184
}
179
185
180
- export function fileIncludeReasonToDiagnostics (
181
- program : Program ,
182
- reason : FileIncludeReason ,
183
- relativeFileNames ?: boolean ,
184
- configFileExplainInfo ?: FileFromConfigExplainInfo
185
- ) : DiagnosticMessageChain {
186
+ export function fileIncludeReasonToDiagnostics ( program : Program , reason : FileIncludeReason , fileNameConvertor ?: ( fileName : string ) => string , ) : DiagnosticMessageChain {
186
187
const options = program . getCompilerOptions ( ) ;
187
188
if ( isReferencedFile ( reason ) ) {
188
189
const { file : referecedFromFile , pos, end, packageId } = getReferencedFileLocation ( path => program . getSourceFileByPath ( path ) , reason ) ;
@@ -214,34 +215,26 @@ namespace ts {
214
215
/*details*/ undefined ,
215
216
message ,
216
217
referenceText ,
217
- toFileName ( program , referecedFromFile , relativeFileNames ) ,
218
+ toFileName ( referecedFromFile , fileNameConvertor ) ,
218
219
packageId && packageIdToString ( packageId )
219
220
) ;
220
221
}
221
222
switch ( reason . kind ) {
222
223
case FileIncludeKind . RootFile :
223
224
if ( ! options . configFile ?. configFileSpecs ) return chainDiagnosticMessages ( /*details*/ undefined , Diagnostics . Root_file_specified_for_compilation ) ;
224
- const { basePath, includeSpecs, getCanonicalFileName } = configFileExplainInfo || getFileFromConfigExplainInfo ( program ) ;
225
- if ( includeSpecs ) {
226
- const rootName = program . getRootFileNames ( ) [ reason . index ] ;
227
- const fileName = getNormalizedAbsolutePath ( rootName , program . getCurrentDirectory ( ) ) ;
228
- const filePath = getCanonicalFileName ( fileName ) ;
229
- const matchedByFiles = forEach ( options . configFile . configFileSpecs . validatedFilesSpec , fileSpec => getCanonicalFileName ( getNormalizedAbsolutePath ( fileSpec , basePath ) ) === filePath ) ;
230
- if ( ! matchedByFiles ) {
231
- const isJsonFile = fileExtensionIs ( fileName , Extension . Json ) ;
232
- const matchedByInclude = find ( includeSpecs , spec => ( ! isJsonFile || endsWith ( spec . include , Extension . Json ) ) && spec . regExp . test ( fileName ) ) ;
233
- return matchedByInclude ?
234
- chainDiagnosticMessages (
235
- /*details*/ undefined ,
236
- Diagnostics . Matched_by_include_pattern_0_in_1 ,
237
- matchedByInclude . include ,
238
- toFileName ( program , options . configFile , relativeFileNames )
239
- ) :
240
- // Could be additional files specified as roots
241
- chainDiagnosticMessages ( /*details*/ undefined , Diagnostics . Root_file_specified_for_compilation ) ;
242
- }
243
- }
244
- return chainDiagnosticMessages ( /*details*/ undefined , Diagnostics . Part_of_files_list_in_tsconfig_json ) ;
225
+ const fileName = getNormalizedAbsolutePath ( program . getRootFileNames ( ) [ reason . index ] , program . getCurrentDirectory ( ) ) ;
226
+ const matchedByFiles = getMatchedFileSpec ( program , fileName ) ;
227
+ if ( matchedByFiles ) return chainDiagnosticMessages ( /*details*/ undefined , Diagnostics . Part_of_files_list_in_tsconfig_json ) ;
228
+ const matchedByInclude = getMatchedIncludeSpec ( program , fileName ) ;
229
+ return matchedByInclude ?
230
+ chainDiagnosticMessages (
231
+ /*details*/ undefined ,
232
+ Diagnostics . Matched_by_include_pattern_0_in_1 ,
233
+ matchedByInclude ,
234
+ toFileName ( options . configFile , fileNameConvertor )
235
+ ) :
236
+ // Could be additional files specified as roots
237
+ chainDiagnosticMessages ( /*details*/ undefined , Diagnostics . Root_file_specified_for_compilation ) ;
245
238
case FileIncludeKind . SourceFromProjectReference :
246
239
case FileIncludeKind . OutputFromProjectReference :
247
240
const isOutput = reason . kind === FileIncludeKind . OutputFromProjectReference ;
@@ -255,7 +248,7 @@ namespace ts {
255
248
isOutput ?
256
249
Diagnostics . Output_from_referenced_project_0_included_because_module_is_specified_as_none :
257
250
Diagnostics . Source_from_referenced_project_0_included_because_module_is_specified_as_none ,
258
- toFileName ( program , referencedResolvedRef . sourceFile . fileName , relativeFileNames ) ,
251
+ toFileName ( referencedResolvedRef . sourceFile . fileName , fileNameConvertor ) ,
259
252
options . outFile ? "--outFile" : "--out" ,
260
253
) ;
261
254
case FileIncludeKind . AutomaticTypeDirectiveFile :
@@ -286,11 +279,9 @@ namespace ts {
286
279
}
287
280
}
288
281
289
- function toFileName ( program : Program , file : SourceFile | string , relativeFileName : boolean | undefined ) {
282
+ function toFileName ( file : SourceFile | string , fileNameConvertor ?: ( fileName : string ) => string ) {
290
283
const fileName = isString ( file ) ? file : file . fileName ;
291
- return relativeFileName ?
292
- convertToRelativePath ( fileName , program . getCurrentDirectory ( ) , fileName => createGetCanonicalFileName ( program . useCaseSensitiveFileNames ( ) ) ( fileName ) ) :
293
- fileName ;
284
+ return fileNameConvertor ? fileNameConvertor ( fileName ) : fileName ;
294
285
}
295
286
296
287
/**
0 commit comments