@@ -25,13 +25,12 @@ import (
25
25
var _ resolve.Resolver = (* typeScriptLang )(nil )
26
26
27
27
const (
28
- Resolution_Error = - 1
29
- Resolution_None = 0
30
- Resolution_NotFound = 1
31
- Resolution_Package = 2
32
- Resolution_Label = 3
33
- Resolution_NativeNode = 4
34
- Resolution_Override = 5
28
+ Resolution_Error = iota
29
+ Resolution_None
30
+ Resolution_NotFound
31
+ Resolution_Label
32
+ Resolution_NativeNode
33
+ Resolution_Override
35
34
)
36
35
37
36
type ResolutionType = int
@@ -183,12 +182,40 @@ func (ts *typeScriptLang) Embeds(r *rule.Rule, from label.Label) []label.Label {
183
182
}
184
183
185
184
return tsEmbeds
185
+ case NpmLinkAllKind :
186
+ // Do not return the embedded :node_modules/{pkg} targets.
187
+ // Instead the npm CrossResolver will resolve to specific package targets.
188
+ break
186
189
}
187
190
188
191
// TODO(jbedard): ts_proto_library() embeds
189
192
190
193
// TODO(jbedard): implement other rule kinds
191
- return make ([]label.Label , 0 )
194
+ return []label.Label {}
195
+ }
196
+
197
+ var _ resolve.CrossResolver = (* typeScriptLang )(nil )
198
+
199
+ func (ts * typeScriptLang ) CrossResolve (c * config.Config , ix * resolve.RuleIndex , imp resolve.ImportSpec , lang string ) []resolve.FindResult {
200
+ // Only resolve imports of js, can be from any language.
201
+ if imp .Lang != LanguageName {
202
+ return nil
203
+ }
204
+
205
+ fromRel := c .Exts [configRelExtension ].(string )
206
+
207
+ results := []resolve.FindResult {}
208
+
209
+ // Imports of npm packages
210
+ if impPkg , _ := node .ParseImportPath (imp .Imp ); impPkg != "" {
211
+ if pkg := ts .findPackage (fromRel , impPkg ); pkg != nil {
212
+ results = append (results , resolve.FindResult {
213
+ Label : * pkg ,
214
+ })
215
+ }
216
+ }
217
+
218
+ return results
192
219
}
193
220
194
221
// Resolve translates imported libraries for a given rule into Bazel
@@ -279,7 +306,7 @@ func (ts *typeScriptLang) addTsLib(
279
306
) {
280
307
_ , tsconfig := ts .tsconfig .FindConfig (from .Pkg )
281
308
if tsconfig != nil && tsconfig .ImportHelpers {
282
- if tslibLabel := ts .resolvePackage (from , "tslib" ); tslibLabel != nil {
309
+ if tslibLabel := ts .findPackage (from . Pkg , "tslib" ); tslibLabel != nil {
283
310
deps .Add (tslibLabel )
284
311
}
285
312
}
@@ -300,12 +327,24 @@ func (ts *typeScriptLang) resolveImports(
300
327
for it .Next () {
301
328
imp := it .Value ().(ImportStatement )
302
329
330
+ // Overrides override all
331
+ if override , ok := resolve .FindRuleWithOverride (c , imp .ImportSpec , LanguageName ); ok {
332
+ deps .Add (& override )
333
+ continue
334
+ }
335
+
336
+ // JS Overrides (js_resolve) override all
337
+ if res := cfg .GetResolution (imp .Imp ); res != nil {
338
+ deps .Add (res )
339
+ continue
340
+ }
341
+
303
342
resolutionType , dep , err := ts .resolveImport (c , ix , from , imp )
304
343
if err != nil {
305
344
return err
306
345
}
307
346
308
- types := ts .resolveImportTypes (resolutionType , from , imp )
347
+ types := ts .resolveImportTypes (c , ix , resolutionType , from , imp )
309
348
for _ , typesDep := range types {
310
349
deps .Add (typesDep )
311
350
}
@@ -363,22 +402,10 @@ func (ts *typeScriptLang) resolveImport(
363
402
from label.Label ,
364
403
impStm ImportStatement ,
365
404
) (ResolutionType , * label.Label , error ) {
366
- cfg := c .Exts [LanguageName ].(* JsGazelleConfig )
367
-
368
405
imp := impStm .ImportSpec
369
406
370
- // Overrides
371
- if override , ok := resolve .FindRuleWithOverride (c , imp , LanguageName ); ok {
372
- return Resolution_Override , & override , nil
373
- }
374
-
375
- // JS Overrides (js_resolve)
376
- if res := cfg .GetResolution (imp .Imp ); res != nil {
377
- return Resolution_Override , res , nil
378
- }
379
-
380
407
// Gazelle rule index
381
- if resolution , match , err := ts .resolveImportFromIndex (c , ix , from , impStm ); resolution != Resolution_NotFound {
408
+ if resolution , match , err := ts .resolveExplicitImportFromIndex (c , ix , from , impStm ); resolution != Resolution_NotFound {
382
409
return resolution , match , err
383
410
}
384
411
@@ -387,11 +414,6 @@ func (ts *typeScriptLang) resolveImport(
387
414
return Resolution_Label , importLabel , nil
388
415
}
389
416
390
- // References to an npm package, pnpm workspace projects etc.
391
- if pkg := ts .resolvePackageImport (from , impStm .Imp ); pkg != nil {
392
- return Resolution_Package , pkg , nil
393
- }
394
-
395
417
// References via tsconfig mappings (paths, baseUrl, rootDirs etc.)
396
418
if tsconfigPaths := ts .tsconfig .ExpandPaths (impStm .SourcePath , impStm .ImportPath ); len (tsconfigPaths ) > 0 {
397
419
for _ , p := range tsconfigPaths {
@@ -404,7 +426,7 @@ func (ts *typeScriptLang) resolveImport(
404
426
ImportPath : impStm .ImportPath ,
405
427
Optional : impStm .Optional ,
406
428
}
407
- if resolution , match , err := ts .resolveImportFromIndex (c , ix , from , pImp ); resolution != Resolution_NotFound {
429
+ if resolution , match , err := ts .resolveExplicitImportFromIndex (c , ix , from , pImp ); resolution != Resolution_NotFound {
408
430
return resolution , match , err
409
431
}
410
432
}
@@ -418,7 +440,7 @@ func (ts *typeScriptLang) resolveImport(
418
440
return Resolution_NotFound , nil , nil
419
441
}
420
442
421
- func (ts * typeScriptLang ) resolveImportFromIndex (
443
+ func (ts * typeScriptLang ) resolveExplicitImportFromIndex (
422
444
c * config.Config ,
423
445
ix * resolve.RuleIndex ,
424
446
from label.Label ,
@@ -429,82 +451,78 @@ func (ts *typeScriptLang) resolveImportFromIndex(
429
451
return Resolution_NotFound , nil , nil
430
452
}
431
453
432
- filteredMatches := make ([]label. Label , 0 , len ( matches ) )
454
+ filteredMatches := common . NewLabelSet ( from )
433
455
for _ , match := range matches {
434
456
// Prevent from adding itself as a dependency.
435
457
if ! match .IsSelfImport (from ) {
436
- filteredMatches = append ( filteredMatches , match .Label )
458
+ filteredMatches . Add ( & match .Label )
437
459
}
438
460
}
439
461
440
462
// Too many results, don't know which is correct
441
- if len ( filteredMatches ) > 1 {
463
+ if filteredMatches . Size ( ) > 1 {
442
464
return Resolution_Error , nil , fmt .Errorf (
443
465
"Import %q from %q resolved to multiple targets (%s) - this must be fixed using the \" aspect:resolve\" directive" ,
444
466
impStm .ImportPath , impStm .SourcePath , targetListFromResults (matches ))
445
467
}
446
468
447
469
// The matches were self imports, no dependency is needed
448
- if len ( filteredMatches ) == 0 {
470
+ if filteredMatches . Size ( ) == 0 {
449
471
return Resolution_None , nil , nil
450
472
}
451
473
452
- match := filteredMatches [0 ]
474
+ match := filteredMatches . Labels () [0 ]
453
475
454
476
BazelLog .Tracef ("resolve %q import %q as %q" , from , impStm .Imp , match )
455
477
456
- return Resolution_Override , & match , nil
457
- }
458
-
459
- func (ts * typeScriptLang ) resolvePackageImport (from label.Label , imp string ) * label.Label {
460
- impPkg , _ := node .ParseImportPath (imp )
461
-
462
- // Imports not in the form of a package
463
- if impPkg == "" {
464
- return nil
465
- }
466
-
467
- return ts .resolvePackage (from , impPkg )
478
+ return Resolution_Label , & match , nil
468
479
}
469
480
470
- func (ts * typeScriptLang ) resolvePackage (from label. Label , impPkg string ) * label.Label {
471
- fromProject := ts .pnpmProjects .GetProject (from . Pkg )
481
+ func (ts * typeScriptLang ) findPackage (from string , impPkg string ) * label.Label {
482
+ fromProject := ts .pnpmProjects .GetProject (from )
472
483
if fromProject == nil {
473
- BazelLog .Tracef ("resolve %q import %q project not found" , from . String () , impPkg )
484
+ BazelLog .Tracef ("resolve %q import %q project not found" , from , impPkg )
474
485
return nil
475
486
}
476
487
477
488
impPkgLabel := fromProject .Get (impPkg )
478
489
if impPkgLabel == nil {
479
- BazelLog .Tracef ("resolve %q import %q not found" , from . String () , impPkg )
490
+ BazelLog .Tracef ("resolve %q import %q not found" , from , impPkg )
480
491
return nil
481
492
}
482
493
483
- BazelLog .Tracef ("resolve %q import %q to %q" , from . String () , impPkg , impPkgLabel )
494
+ BazelLog .Tracef ("resolve %q import %q to %q" , from , impPkg , impPkgLabel )
484
495
485
496
return impPkgLabel
486
497
}
487
498
488
- func (ts * typeScriptLang ) resolveImportTypes (resolutionType ResolutionType , from label.Label , imp ImportStatement ) []* label.Label {
499
+ func (ts * typeScriptLang ) resolveImportTypes (c * config. Config , ix * resolve. RuleIndex , resolutionType ResolutionType , from label.Label , imp ImportStatement ) []* label.Label {
489
500
// Overrides are not extended with additional types
490
501
if resolutionType == Resolution_Override {
491
502
return nil
492
503
}
493
504
494
- // Types for native node imports are always resolved to @types/node
505
+ // The package the @types are for
506
+ var typesPkg string
495
507
if resolutionType == Resolution_NativeNode {
496
- if typesNode := ts .resolveAtTypes (from , "node" ); typesNode != nil {
497
- return []* label.Label {typesNode }
508
+ typesPkg = "@types/node"
509
+ } else {
510
+ typesPkg , _ = node .ParseImportPath (imp .ImportSpec .Imp )
511
+ if typesPkg == "" {
512
+ return nil
498
513
}
499
514
500
- return nil
515
+ typesPkg = toAtTypesPackage ( typesPkg )
501
516
}
502
517
503
- // Packages with specific @types/* definitions
504
- if typesPkg := ts .resolveAtTypes (from , imp .Imp ); typesPkg != nil {
518
+ typesSpec := resolve.ImportSpec {
519
+ Lang : LanguageName ,
520
+ Imp : typesPkg ,
521
+ }
522
+ if matches := ix .FindRulesByImportWithConfig (c , typesSpec , LanguageName ); len (matches ) > 0 {
505
523
// @types packages for any named imports
506
524
// The import may be a package, may be an unresolved import with only @types
507
- return []* label.Label {typesPkg }
525
+ return []* label.Label {& matches [ 0 ]. Label }
508
526
}
509
527
510
528
// If an import has not been found and has no designated package or @types package
@@ -537,18 +555,6 @@ func toAtTypesPackage(pkg string) string {
537
555
return "@types/" + pkg
538
556
}
539
557
540
- // Find and resolve any @types package for an import
541
- func (ts * typeScriptLang ) resolveAtTypes (from label.Label , imp string ) * label.Label {
542
- fromProject := ts .pnpmProjects .GetProject (from .Pkg )
543
- if fromProject == nil {
544
- return nil
545
- }
546
-
547
- typesPkg := toAtTypesPackage (imp )
548
-
549
- return fromProject .Get (typesPkg )
550
- }
551
-
552
558
// targetListFromResults returns a string with the human-readable list of
553
559
// targets contained in the given results.
554
560
func targetListFromResults (results []resolve.FindResult ) string {
0 commit comments