Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -1143,32 +1143,36 @@ func GetRootDeclaration(node *Node) *Node {
return node
}

func getCombinedFlags[T ~uint32](node *Node, getFlags func(*Node) T) T {
func GetCombinedModifierFlags(node *Node) ModifierFlags {
node = GetRootDeclaration(node)
flags := getFlags(node)
flags := node.ModifierFlags()
if node.Kind == KindVariableDeclaration {
node = node.Parent
}
if node != nil && node.Kind == KindVariableDeclarationList {
flags |= getFlags(node)
flags |= node.ModifierFlags()
node = node.Parent
}
if node != nil && node.Kind == KindVariableStatement {
flags |= getFlags(node)
flags |= node.ModifierFlags()
}
return flags
}

func GetCombinedModifierFlags(node *Node) ModifierFlags {
return getCombinedFlags(node, (*Node).ModifierFlags)
}

func GetCombinedNodeFlags(node *Node) NodeFlags {
return getCombinedFlags(node, getNodeFlags)
}

func getNodeFlags(node *Node) NodeFlags {
return node.Flags
node = GetRootDeclaration(node)
flags := node.Flags
if node.Kind == KindVariableDeclaration {
node = node.Parent
}
if node != nil && node.Kind == KindVariableDeclarationList {
flags |= node.Flags
node = node.Parent
}
if node != nil && node.Kind == KindVariableStatement {
flags |= node.Flags
}
return flags
}
Comment thread
mds-ant marked this conversation as resolved.

// Gets whether a bound `VariableDeclaration` or `VariableDeclarationList` is part of an `await using` declaration.
Expand Down
48 changes: 48 additions & 0 deletions internal/ast/utilities_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ast_test

import (
"testing"

"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/parser"
"github.com/microsoft/typescript-go/internal/testutil/fixtures"
"github.com/microsoft/typescript-go/internal/tspath"
"github.com/microsoft/typescript-go/internal/vfs/osvfs"
)

func BenchmarkGetCombinedFlags(b *testing.B) {
for _, f := range fixtures.BenchFixtures {
b.Run(f.Name(), func(b *testing.B) {
f.SkipIfNotExist(b)

fileName := tspath.GetNormalizedAbsolutePath(f.Path(), "/")
path := tspath.ToPath(fileName, "/", osvfs.FS().UseCaseSensitiveFileNames())
sourceText := f.ReadFile(b)
scriptKind := core.GetScriptKindFromFileName(fileName)

sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
FileName: fileName,
Path: path,
}, sourceText, scriptKind)

var decls []*ast.Node
var collect ast.Visitor
collect = func(n *ast.Node) bool {
if ast.IsDeclaration(n) {
decls = append(decls, n)
}
n.ForEachChild(collect)
return false
}
sourceFile.AsNode().ForEachChild(collect)

for b.Loop() {
for _, n := range decls {
_ = ast.GetCombinedNodeFlags(n)
_ = ast.GetCombinedModifierFlags(n)
}
}
})
}
}
Loading