Skip to content

Commit c93a4b4

Browse files
authored
Add types baselines (microsoft#296)
1 parent 3f7ea7e commit c93a4b4

22 files changed

+364
-68
lines changed

internal/ast/ast.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,18 @@ func (n *Node) Comments() []*Node {
587587
panic("Unhandled case in Node.Comments: " + n.Kind.String())
588588
}
589589

590+
func (n *Node) Label() *Node {
591+
switch n.Kind {
592+
case KindLabeledStatement:
593+
return n.AsLabeledStatement().Label
594+
case KindBreakStatement:
595+
return n.AsBreakStatement().Label
596+
case KindContinueStatement:
597+
return n.AsContinueStatement().Label
598+
}
599+
panic("Unhandled case in Node.Label: " + n.Kind.String())
600+
}
601+
590602
// Node casts
591603

592604
func (n *Node) AsIdentifier() *Identifier {
@@ -2351,6 +2363,10 @@ func (node *LabeledStatement) VisitEachChild(v *NodeVisitor) *Node {
23512363
return v.Factory.UpdateLabeledStatement(node, v.visitNode(node.Label), v.visitNode(node.Statement))
23522364
}
23532365

2366+
func IsLabeledStatement(node *Node) bool {
2367+
return node.Kind == KindLabeledStatement
2368+
}
2369+
23542370
// ExpressionStatement
23552371

23562372
type ExpressionStatement struct {
@@ -6572,6 +6588,10 @@ func (node *JsxSpreadAttribute) VisitEachChild(v *NodeVisitor) *Node {
65726588
return v.Factory.UpdateJsxSpreadAttribute(node, v.visitNode(node.Expression))
65736589
}
65746590

6591+
func IsJsxClosingElement(node *Node) bool {
6592+
return node.Kind == KindJsxClosingElement
6593+
}
6594+
65756595
// JsxExpression
65766596

65776597
type JsxExpression struct {

internal/ast/utilities.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,6 +1859,104 @@ func IsBlockScope(node *Node, parentNode *Node) bool {
18591859
return false
18601860
}
18611861

1862+
type SemanticMeaning int32
1863+
1864+
const (
1865+
SemanticMeaningNone SemanticMeaning = 0
1866+
SemanticMeaningValue SemanticMeaning = 1 << 0
1867+
SemanticMeaningType SemanticMeaning = 1 << 1
1868+
SemanticMeaningNamespace SemanticMeaning = 1 << 2
1869+
SemanticMeaningAll SemanticMeaning = SemanticMeaningValue | SemanticMeaningType | SemanticMeaningNamespace
1870+
)
1871+
1872+
func GetMeaningFromDeclaration(node *Node) SemanticMeaning {
1873+
switch node.Kind {
1874+
case KindVariableDeclaration:
1875+
return SemanticMeaningValue
1876+
case KindParameter,
1877+
KindBindingElement,
1878+
KindPropertyDeclaration,
1879+
KindPropertySignature,
1880+
KindPropertyAssignment,
1881+
KindShorthandPropertyAssignment,
1882+
KindMethodDeclaration,
1883+
KindMethodSignature,
1884+
KindConstructor,
1885+
KindGetAccessor,
1886+
KindSetAccessor,
1887+
KindFunctionDeclaration,
1888+
KindFunctionExpression,
1889+
KindArrowFunction,
1890+
KindCatchClause,
1891+
KindJsxAttribute:
1892+
return SemanticMeaningValue
1893+
1894+
case KindTypeParameter,
1895+
KindInterfaceDeclaration,
1896+
KindTypeAliasDeclaration,
1897+
KindTypeLiteral:
1898+
return SemanticMeaningType
1899+
case KindEnumMember, KindClassDeclaration:
1900+
return SemanticMeaningValue | SemanticMeaningType
1901+
1902+
case KindModuleDeclaration:
1903+
if IsAmbientModule(node) {
1904+
return SemanticMeaningNamespace | SemanticMeaningValue
1905+
} else if GetModuleInstanceState(node) == ModuleInstanceStateInstantiated {
1906+
return SemanticMeaningNamespace | SemanticMeaningValue
1907+
} else {
1908+
return SemanticMeaningNamespace
1909+
}
1910+
1911+
case KindEnumDeclaration,
1912+
KindNamedImports,
1913+
KindImportSpecifier,
1914+
KindImportEqualsDeclaration,
1915+
KindImportDeclaration,
1916+
KindExportAssignment,
1917+
KindExportDeclaration:
1918+
return SemanticMeaningAll
1919+
1920+
// An external module can be a Value
1921+
case KindSourceFile:
1922+
return SemanticMeaningNamespace | SemanticMeaningValue
1923+
}
1924+
1925+
return SemanticMeaningAll
1926+
}
1927+
1928+
func IsPropertyAccessOrQualifiedName(node *Node) bool {
1929+
return node.Kind == KindPropertyAccessExpression || node.Kind == KindQualifiedName
1930+
}
1931+
1932+
func IsLabelName(node *Node) bool {
1933+
return IsLabelOfLabeledStatement(node) || IsJumpStatementTarget(node)
1934+
}
1935+
1936+
func IsLabelOfLabeledStatement(node *Node) bool {
1937+
if !IsIdentifier(node) {
1938+
return false
1939+
}
1940+
if !IsLabeledStatement(node.Parent) {
1941+
return false
1942+
}
1943+
return node == node.Parent.Label()
1944+
}
1945+
1946+
func IsJumpStatementTarget(node *Node) bool {
1947+
if !IsIdentifier(node) {
1948+
return false
1949+
}
1950+
if !IsBreakOrContinueStatement(node.Parent) {
1951+
return false
1952+
}
1953+
return node == node.Parent.Label()
1954+
}
1955+
1956+
func IsBreakOrContinueStatement(node *Node) bool {
1957+
return NodeKindIs(node, KindBreakStatement, KindContinueStatement)
1958+
}
1959+
18621960
// GetModuleInstanceState is used during binding as well as in transformations and tests, and therefore may be invoked
18631961
// with a node that does not yet have its `Parent` pointer set. In this case, an `ancestors` represents a stack of
18641962
// virtual `Parent` pointers that can be used to walk up the tree. Since `getModuleInstanceStateForAliasTarget` may

internal/binder/binder.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,6 @@ type Binder struct {
6969
singleDeclarationsPool core.Pool[*ast.Node]
7070
}
7171

72-
type ModuleInstanceState int32
73-
74-
const (
75-
ModuleInstanceStateUnknown ModuleInstanceState = iota
76-
ModuleInstanceStateNonInstantiated
77-
ModuleInstanceStateInstantiated
78-
ModuleInstanceStateConstEnumOnly
79-
)
80-
8172
type ActiveLabel struct {
8273
next *ActiveLabel
8374
breakTarget *ast.FlowLabel
@@ -2684,18 +2675,6 @@ func getPostfixTokenFromNode(node *ast.Node) *ast.Node {
26842675
panic("Unhandled case in getPostfixTokenFromNode")
26852676
}
26862677

2687-
func nodeHasName(statement *ast.Node, id *ast.Node) bool {
2688-
name := statement.Name()
2689-
if name != nil {
2690-
return ast.IsIdentifier(name) && name.AsIdentifier().Text == id.AsIdentifier().Text
2691-
}
2692-
if ast.IsVariableStatement(statement) {
2693-
declarations := statement.AsVariableStatement().DeclarationList.AsVariableDeclarationList().Declarations.Nodes
2694-
return core.Some(declarations, func(d *ast.Node) bool { return nodeHasName(d, id) })
2695-
}
2696-
return false
2697-
}
2698-
26992678
func isAsyncFunction(node *ast.Node) bool {
27002679
switch node.Kind {
27012680
case ast.KindFunctionDeclaration, ast.KindFunctionExpression, ast.KindArrowFunction, ast.KindMethodDeclaration:

0 commit comments

Comments
 (0)