Skip to content

Commit a808aba

Browse files
authored
Merge branch 'main' into fix/1374
2 parents bb2acd9 + 19ec922 commit a808aba

22 files changed

+788
-671
lines changed

internal/ast/utilities.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3886,3 +3886,7 @@ func IsExpandoInitializer(initializer *Node) bool {
38863886
}
38873887
return false
38883888
}
3889+
3890+
func GetContainingFunction(node *Node) *Node {
3891+
return FindAncestor(node.Parent, IsFunctionLike)
3892+
}

internal/checker/checker.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2464,7 +2464,7 @@ func (c *Checker) checkParameter(node *ast.Node) {
24642464
// or if its FunctionBody is strict code(11.1.5).
24652465
c.checkGrammarModifiers(node)
24662466
c.checkVariableLikeDeclaration(node)
2467-
fn := getContainingFunction(node)
2467+
fn := ast.GetContainingFunction(node)
24682468
var paramName string
24692469
if node.Name() != nil && ast.IsIdentifier(node.Name()) {
24702470
paramName = node.Name().Text()
@@ -5522,7 +5522,7 @@ func (c *Checker) checkVariableLikeDeclaration(node *ast.Node) {
55225522
}
55235523
if ast.IsBindingElement(node) {
55245524
propName := node.PropertyName()
5525-
if propName != nil && ast.IsIdentifier(node.Name()) && ast.IsPartOfParameterDeclaration(node) && ast.NodeIsMissing(getContainingFunction(node).Body()) {
5525+
if propName != nil && ast.IsIdentifier(node.Name()) && ast.IsPartOfParameterDeclaration(node) && ast.NodeIsMissing(ast.GetContainingFunction(node).Body()) {
55265526
// type F = ({a: string}) => void;
55275527
// ^^^^^^
55285528
// variable renaming in function type notation is confusing,
@@ -5557,7 +5557,7 @@ func (c *Checker) checkVariableLikeDeclaration(node *ast.Node) {
55575557
c.checkSourceElements(name.AsBindingPattern().Elements.Nodes)
55585558
}
55595559
// For a parameter declaration with an initializer, error and exit if the containing function doesn't have a body
5560-
if initializer != nil && ast.IsPartOfParameterDeclaration(node) && ast.NodeIsMissing(getContainingFunction(node).Body()) {
5560+
if initializer != nil && ast.IsPartOfParameterDeclaration(node) && ast.NodeIsMissing(ast.GetContainingFunction(node).Body()) {
55615561
c.error(node, diagnostics.A_parameter_initializer_is_only_allowed_in_a_function_or_constructor_implementation)
55625562
return
55635563
}
@@ -10508,7 +10508,7 @@ func (c *Checker) checkSpreadExpression(node *ast.Node, checkMode CheckMode) *Ty
1050810508

1050910509
func (c *Checker) checkYieldExpression(node *ast.Node) *Type {
1051010510
c.checkGrammarYieldExpression(node)
10511-
fn := getContainingFunction(node)
10511+
fn := ast.GetContainingFunction(node)
1051210512
if fn == nil {
1051310513
return c.anyType
1051410514
}
@@ -22814,7 +22814,7 @@ func (c *Checker) getTypeAliasInstantiation(symbol *ast.Symbol, typeArguments []
2281422814

2281522815
func isLocalTypeAlias(symbol *ast.Symbol) bool {
2281622816
declaration := core.Find(symbol.Declarations, isTypeAlias)
22817-
return declaration != nil && getContainingFunction(declaration) != nil
22817+
return declaration != nil && ast.GetContainingFunction(declaration) != nil
2281822818
}
2281922819

2282022820
func (c *Checker) getDeclaredTypeOfSymbol(symbol *ast.Symbol) *Type {
@@ -28318,7 +28318,7 @@ func (c *Checker) getContextualTypeForStaticPropertyDeclaration(declaration *ast
2831828318
}
2831928319

2832028320
func (c *Checker) getContextualTypeForReturnExpression(node *ast.Node, contextFlags ContextFlags) *Type {
28321-
fn := getContainingFunction(node)
28321+
fn := ast.GetContainingFunction(node)
2832228322
if fn != nil {
2832328323
contextualReturnType := c.getContextualReturnType(fn, contextFlags)
2832428324
if contextualReturnType != nil {
@@ -28413,7 +28413,7 @@ func (c *Checker) getContextualSignatureForFunctionLikeDeclaration(node *ast.Nod
2841328413
}
2841428414

2841528415
func (c *Checker) getContextualTypeForYieldOperand(node *ast.Node, contextFlags ContextFlags) *Type {
28416-
fn := getContainingFunction(node)
28416+
fn := ast.GetContainingFunction(node)
2841728417
if fn != nil {
2841828418
functionFlags := getFunctionFlags(fn)
2841928419
contextualReturnType := c.getContextualReturnType(fn, contextFlags)

internal/checker/grammarchecks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,7 @@ func (c *Checker) checkGrammarForInOrForOfStatement(forInOrOfStatement *ast.ForI
12631263
// use of 'for-await-of' in non-async function
12641264
if !c.hasParseDiagnostics(sourceFile) {
12651265
diagnostic := createDiagnosticForNode(forInOrOfStatement.AwaitModifier, diagnostics.X_for_await_loops_are_only_allowed_within_async_functions_and_at_the_top_levels_of_modules)
1266-
containingFunc := getContainingFunction(forInOrOfStatement.AsNode())
1266+
containingFunc := ast.GetContainingFunction(forInOrOfStatement.AsNode())
12671267
if containingFunc != nil && containingFunc.Kind != ast.KindConstructor {
12681268
debug.Assert((getFunctionFlags(containingFunc)&FunctionFlagsAsync) == 0, "Enclosing function should never be an async function.")
12691269
if hasAsyncModifier(containingFunc) {

internal/checker/utilities.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -907,10 +907,6 @@ func (s *orderedSet[T]) add(value T) {
907907
s.values = append(s.values, value)
908908
}
909909

910-
func getContainingFunction(node *ast.Node) *ast.Node {
911-
return ast.FindAncestor(node.Parent, ast.IsFunctionLike)
912-
}
913-
914910
func getContainingFunctionOrClassStaticBlock(node *ast.Node) *ast.Node {
915911
return ast.FindAncestor(node.Parent, ast.IsFunctionLikeOrClassStaticBlockDeclaration)
916912
}

internal/collections/ordered_map.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,11 @@ func DiffOrderedMaps[K comparable, V comparable](m1 *OrderedMap[K, V], m2 *Order
300300
}
301301

302302
func DiffOrderedMapsFunc[K comparable, V any](m1 *OrderedMap[K, V], m2 *OrderedMap[K, V], equalValues func(a, b V) bool, onAdded func(key K, value V), onRemoved func(key K, value V), onModified func(key K, oldValue V, newValue V)) {
303+
for k, v2 := range m2.Entries() {
304+
if _, ok := m1.Get(k); !ok {
305+
onAdded(k, v2)
306+
}
307+
}
303308
for k, v1 := range m1.Entries() {
304309
if v2, ok := m2.Get(k); ok {
305310
if !equalValues(v1, v2) {
@@ -309,10 +314,4 @@ func DiffOrderedMapsFunc[K comparable, V any](m1 *OrderedMap[K, V], m2 *OrderedM
309314
onRemoved(k, v1)
310315
}
311316
}
312-
313-
for k, v2 := range m2.Entries() {
314-
if _, ok := m1.Get(k); !ok {
315-
onAdded(k, v2)
316-
}
317-
}
318317
}

internal/core/core.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,11 @@ func DiffMaps[K comparable, V comparable](m1 map[K]V, m2 map[K]V, onAdded func(K
606606
}
607607

608608
func DiffMapsFunc[K comparable, V any](m1 map[K]V, m2 map[K]V, equalValues func(V, V) bool, onAdded func(K, V), onRemoved func(K, V), onChanged func(K, V, V)) {
609+
for k, v2 := range m2 {
610+
if _, ok := m1[k]; !ok {
611+
onAdded(k, v2)
612+
}
613+
}
609614
for k, v1 := range m1 {
610615
if v2, ok := m2[k]; ok {
611616
if !equalValues(v1, v2) {
@@ -615,12 +620,6 @@ func DiffMapsFunc[K comparable, V any](m1 map[K]V, m2 map[K]V, equalValues func(
615620
onRemoved(k, v1)
616621
}
617622
}
618-
619-
for k, v2 := range m2 {
620-
if _, ok := m1[k]; !ok {
621-
onAdded(k, v2)
622-
}
623-
}
624623
}
625624

626625
// CopyMapInto is maps.Copy, unless dst is nil, in which case it clones and returns src.

internal/ls/documenthighlights.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -550,14 +550,14 @@ func getLoopBreakContinueOccurrences(node *ast.Node, sourceFile *ast.SourceFile)
550550
}
551551

552552
func getAsyncAndAwaitOccurrences(node *ast.Node, sourceFile *ast.SourceFile) []*ast.Node {
553-
parent := ast.FindAncestor(node.Parent, ast.IsFunctionLike)
554-
if parent == nil {
553+
fun := ast.GetContainingFunction(node)
554+
if fun == nil {
555555
return nil
556556
}
557-
parentFunc := parent.AsFunctionDeclaration()
557+
558558
var keywords []*ast.Node
559559

560-
modifiers := parentFunc.Modifiers()
560+
modifiers := fun.Modifiers()
561561
if modifiers != nil {
562562
for _, modifier := range modifiers.Nodes {
563563
if modifier.Kind == ast.KindAsyncKeyword {
@@ -566,7 +566,7 @@ func getAsyncAndAwaitOccurrences(node *ast.Node, sourceFile *ast.SourceFile) []*
566566
}
567567
}
568568

569-
parentFunc.ForEachChild(func(child *ast.Node) bool {
569+
fun.ForEachChild(func(child *ast.Node) bool {
570570
traverseWithoutCrossingFunction(child, sourceFile, func(child *ast.Node) {
571571
if ast.IsAwaitExpression(child) {
572572
token := lsutil.GetFirstToken(child, sourceFile)

internal/lsp/server.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/microsoft/typescript-go/internal/project"
2424
"github.com/microsoft/typescript-go/internal/project/ata"
2525
"github.com/microsoft/typescript-go/internal/project/logging"
26+
"github.com/microsoft/typescript-go/internal/tspath"
2627
"github.com/microsoft/typescript-go/internal/vfs"
2728
"golang.org/x/sync/errgroup"
2829
"golang.org/x/text/language"
@@ -651,9 +652,27 @@ func (s *Server) handleInitialized(ctx context.Context, params *lsproto.Initiali
651652
s.watchEnabled = true
652653
}
653654

655+
cwd := s.cwd
656+
if s.initializeParams.Capabilities != nil &&
657+
s.initializeParams.Capabilities.Workspace != nil &&
658+
s.initializeParams.Capabilities.Workspace.WorkspaceFolders != nil &&
659+
ptrIsTrue(s.initializeParams.Capabilities.Workspace.WorkspaceFolders) &&
660+
s.initializeParams.WorkspaceFolders != nil &&
661+
s.initializeParams.WorkspaceFolders.WorkspaceFolders != nil &&
662+
len(*s.initializeParams.WorkspaceFolders.WorkspaceFolders) == 1 {
663+
cwd = lsproto.DocumentUri((*s.initializeParams.WorkspaceFolders.WorkspaceFolders)[0].Uri).FileName()
664+
} else if s.initializeParams.RootUri.DocumentUri != nil {
665+
cwd = s.initializeParams.RootUri.DocumentUri.FileName()
666+
} else if s.initializeParams.RootPath != nil && s.initializeParams.RootPath.String != nil {
667+
cwd = *s.initializeParams.RootPath.String
668+
}
669+
if !tspath.PathIsAbsolute(cwd) {
670+
cwd = s.cwd
671+
}
672+
654673
s.session = project.NewSession(&project.SessionInit{
655674
Options: &project.SessionOptions{
656-
CurrentDirectory: s.cwd,
675+
CurrentDirectory: cwd,
657676
DefaultLibraryPath: s.defaultLibraryPath,
658677
TypingsLocation: s.typingsLocation,
659678
PositionEncoding: s.positionEncoding,

internal/module/resolver.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1733,7 +1733,11 @@ func (r *resolutionState) readPackageJsonPeerDependencies(packageJsonInfo *packa
17331733
r.tracer.write(diagnostics.X_package_json_has_a_peerDependencies_field.Message())
17341734
}
17351735
packageDirectory := r.realPath(packageJsonInfo.PackageDirectory)
1736-
nodeModules := packageDirectory[:strings.LastIndex(packageDirectory, "/node_modules")+len("/node_modules")] + "/"
1736+
nodeModulesIndex := strings.LastIndex(packageDirectory, "/node_modules")
1737+
if nodeModulesIndex == -1 {
1738+
return ""
1739+
}
1740+
nodeModules := packageDirectory[:nodeModulesIndex+len("/node_modules")] + "/"
17371741
builder := strings.Builder{}
17381742
for name := range peerDependencies.Value {
17391743
peerPackageJson := r.getPackageJsonInfo(nodeModules+name /*onlyRecordFailures*/, false)

internal/project/configfileregistry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type configFileEntry struct {
4141
// when this is set, no other fields will be used.
4242
retainingConfigs map[tspath.Path]struct{}
4343
// rootFilesWatch is a watch for the root files of this config file.
44-
rootFilesWatch *WatchedFiles[[]string]
44+
rootFilesWatch *WatchedFiles[patternsAndIgnored]
4545
}
4646

4747
func newConfigFileEntry(fileName string) *configFileEntry {

0 commit comments

Comments
 (0)