1
1
package tsoptions
2
2
3
3
import (
4
+ "fmt"
4
5
"iter"
5
6
"slices"
7
+ "strings"
6
8
"sync"
7
9
8
10
"github.com/microsoft/typescript-go/internal/ast"
9
- "github.com/microsoft/typescript-go/internal/collections"
10
11
"github.com/microsoft/typescript-go/internal/core"
12
+ "github.com/microsoft/typescript-go/internal/glob"
11
13
"github.com/microsoft/typescript-go/internal/module"
12
14
"github.com/microsoft/typescript-go/internal/outputpaths"
13
15
"github.com/microsoft/typescript-go/internal/tspath"
14
16
"github.com/microsoft/typescript-go/internal/vfs"
15
17
)
16
18
19
+ const (
20
+ fileGlobPattern = "*.{js,jsx,mjs,cjs,ts,tsx,mts,cts,json}"
21
+ recursiveFileGlobPattern = "**/*.{js,jsx,mjs,cjs,ts,tsx,mts,cts,json}"
22
+ )
23
+
17
24
type ParsedCommandLine struct {
18
25
ParsedConfig * core.ParsedOptions `json:"parsedConfig"`
19
26
@@ -25,6 +32,8 @@ type ParsedCommandLine struct {
25
32
comparePathsOptions tspath.ComparePathsOptions
26
33
wildcardDirectoriesOnce sync.Once
27
34
wildcardDirectories map [string ]bool
35
+ includeGlobsOnce sync.Once
36
+ includeGlobs []* glob.Glob
28
37
extraFileExtensions []FileExtensionInfo
29
38
30
39
sourceAndOutputMapsOnce sync.Once
@@ -197,21 +206,40 @@ func (p *ParsedCommandLine) WildcardDirectories() map[string]bool {
197
206
return nil
198
207
}
199
208
200
- if p .wildcardDirectories != nil {
201
- return p .wildcardDirectories
202
- }
203
-
204
209
p .wildcardDirectoriesOnce .Do (func () {
205
- p .wildcardDirectories = getWildcardDirectories (
206
- p .ConfigFile .configFileSpecs .validatedIncludeSpecs ,
207
- p .ConfigFile .configFileSpecs .validatedExcludeSpecs ,
208
- p .comparePathsOptions ,
209
- )
210
+ if p .wildcardDirectories == nil {
211
+ p .wildcardDirectories = getWildcardDirectories (
212
+ p .ConfigFile .configFileSpecs .validatedIncludeSpecs ,
213
+ p .ConfigFile .configFileSpecs .validatedExcludeSpecs ,
214
+ p .comparePathsOptions ,
215
+ )
216
+ }
210
217
})
211
218
212
219
return p .wildcardDirectories
213
220
}
214
221
222
+ func (p * ParsedCommandLine ) WildcardDirectoryGlobs () []* glob.Glob {
223
+ wildcardDirectories := p .WildcardDirectories ()
224
+ if wildcardDirectories == nil {
225
+ return nil
226
+ }
227
+
228
+ p .includeGlobsOnce .Do (func () {
229
+ if p .includeGlobs == nil {
230
+ globs := make ([]* glob.Glob , 0 , len (wildcardDirectories ))
231
+ for dir , recursive := range wildcardDirectories {
232
+ if parsed , err := glob .Parse (fmt .Sprintf ("%s/%s" , tspath .NormalizePath (dir ), core .IfElse (recursive , recursiveFileGlobPattern , fileGlobPattern ))); err == nil {
233
+ globs = append (globs , parsed )
234
+ }
235
+ }
236
+ p .includeGlobs = globs
237
+ }
238
+ })
239
+
240
+ return p .includeGlobs
241
+ }
242
+
215
243
// Normalized file names explicitly specified in `files`
216
244
func (p * ParsedCommandLine ) LiteralFileNames () []string {
217
245
if p != nil && p .ConfigFile != nil {
@@ -285,48 +313,30 @@ func (p *ParsedCommandLine) GetConfigFileParsingDiagnostics() []*ast.Diagnostic
285
313
return p .Errors
286
314
}
287
315
288
- // Porting reference: ProjectService.isMatchedByConfig
289
- func (p * ParsedCommandLine ) MatchesFileName (fileName string ) bool {
316
+ // PossiblyMatchesFileName is a fast check to see if a file is currently included by a config
317
+ // or would be included if the file were to be created. It may return false positives.
318
+ func (p * ParsedCommandLine ) PossiblyMatchesFileName (fileName string ) bool {
290
319
path := tspath .ToPath (fileName , p .GetCurrentDirectory (), p .UseCaseSensitiveFileNames ())
291
- if slices .ContainsFunc (p .FileNames (), func (f string ) bool {
292
- return path == tspath .ToPath (f , p .GetCurrentDirectory (), p .UseCaseSensitiveFileNames ())
293
- }) {
320
+ if _ , ok := p .FileNamesByPath ()[path ]; ok {
294
321
return true
295
322
}
296
323
297
- if p .ConfigFile == nil {
298
- return false
299
- }
300
-
301
- if len (p .ConfigFile .configFileSpecs .validatedIncludeSpecs ) == 0 {
302
- return false
303
- }
304
-
305
- supportedExtensions := GetSupportedExtensionsWithJsonIfResolveJsonModule (
306
- p .CompilerOptions (),
307
- GetSupportedExtensions (p .CompilerOptions (), p .extraFileExtensions ),
308
- )
309
-
310
- if ! tspath .FileExtensionIsOneOf (fileName , core .Flatten (supportedExtensions )) {
311
- return false
312
- }
313
-
314
- if p .ConfigFile .configFileSpecs .matchesExclude (fileName , p .comparePathsOptions ) {
315
- return false
316
- }
317
-
318
- var allFileNames collections.Set [tspath.Path ]
319
- for _ , fileName := range p .FileNames () {
320
- allFileNames .Add (tspath .ToPath (fileName , p .GetCurrentDirectory (), p .UseCaseSensitiveFileNames ()))
324
+ for _ , include := range p .ConfigFile .configFileSpecs .validatedIncludeSpecs {
325
+ if ! strings .ContainsAny (include , "*?" ) && ! vfs .IsImplicitGlob (include ) {
326
+ includePath := tspath .ToPath (include , p .GetCurrentDirectory (), p .UseCaseSensitiveFileNames ())
327
+ if includePath == path {
328
+ return true
329
+ }
330
+ }
321
331
}
322
-
323
- if hasFileWithHigherPriorityExtension (string (path ), supportedExtensions , func (fileName string ) bool {
324
- return allFileNames .Has (tspath .Path (fileName ))
325
- }) {
326
- return false
332
+ if wildcardDirectoryGlobs := p .WildcardDirectoryGlobs (); len (wildcardDirectoryGlobs ) > 0 {
333
+ for _ , glob := range wildcardDirectoryGlobs {
334
+ if glob .Match (fileName ) {
335
+ return true
336
+ }
337
+ }
327
338
}
328
-
329
- return p .ConfigFile .configFileSpecs .getMatchedIncludeSpec (fileName , p .comparePathsOptions ) != ""
339
+ return false
330
340
}
331
341
332
342
func (p * ParsedCommandLine ) GetMatchedFileSpec (fileName string ) string {
@@ -363,6 +373,7 @@ func (p *ParsedCommandLine) ReloadFileNamesOfParsedCommandLine(fs vfs.FS) *Parse
363
373
CompileOnSave : p .CompileOnSave ,
364
374
comparePathsOptions : p .comparePathsOptions ,
365
375
wildcardDirectories : p .wildcardDirectories ,
376
+ includeGlobs : p .includeGlobs ,
366
377
extraFileExtensions : p .extraFileExtensions ,
367
378
literalFileNamesLen : literalFileNamesLen ,
368
379
}
0 commit comments