Skip to content

Commit f88215e

Browse files
dependabot[bot]github-actions[bot]buke
authored
deps(deps): bump microsoft/typescript-go from dedfbd9 to c9d2894 (#18)
* deps(deps): bump microsoft/typescript-go from `dedfbd9` to `c9d2894` Bumps [microsoft/typescript-go](https://github.com/microsoft/typescript-go) from `dedfbd9` to `c9d2894`. - [Commits](microsoft/typescript-go@dedfbd9...c9d2894) --- updated-dependencies: - dependency-name: microsoft/typescript-go dependency-version: c9d2894de3820f0ca814a7b445d14b551e80e549 dependency-type: direct:production ... Signed-off-by: dependabot[bot] <support@github.com> * chore(sync): mirror internal packages into pkg/ (auto) (#19) Co-authored-by: buke <1013738+buke@users.noreply.github.com> --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: buke <1013738+buke@users.noreply.github.com>
1 parent 86e5216 commit f88215e

File tree

473 files changed

+7330
-1446
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

473 files changed

+7330
-1446
lines changed

microsoft/typescript-go

Submodule typescript-go updated 479 files

pkg/api/encoder/encoder_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ func TestEncodeSourceFile(t *testing.T) {
3535
})
3636
}
3737

38+
func TestEncodeSourceFileWithUnicodeEscapes(t *testing.T) {
39+
t.Parallel()
40+
sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
41+
FileName: "/test.ts",
42+
Path: "/test.ts",
43+
}, `let a = "😃"; let b = "\ud83d\ude03"; let c = "\udc00\ud83d\ude03"; let d = "\ud83d\ud83d\ude03"`, core.ScriptKindTS)
44+
t.Run("baseline", func(t *testing.T) {
45+
t.Parallel()
46+
buf, err := encoder.EncodeSourceFile(sourceFile, "")
47+
assert.NilError(t, err)
48+
49+
str := formatEncodedSourceFile(buf)
50+
baseline.Run(t, "encodeSourceFileWithUnicodeEscapes.txt", str, baseline.Options{
51+
Subfolder: "api",
52+
})
53+
})
54+
}
55+
3856
func BenchmarkEncodeSourceFile(b *testing.B) {
3957
repo.SkipIfNoTypeScriptSubmodule(b)
4058
filePath := filepath.Join(repo.TypeScriptSubmodulePath, "src/compiler/checker.ts")

pkg/ast/ast.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,15 @@ func (n *Node) Statements() []*Node {
631631
return nil
632632
}
633633

634+
func (n *Node) CanHaveStatements() bool {
635+
switch n.Kind {
636+
case KindSourceFile, KindBlock, KindModuleBlock, KindCaseClause, KindDefaultClause:
637+
return true
638+
default:
639+
return false
640+
}
641+
}
642+
634643
func (n *Node) ModifierFlags() ModifierFlags {
635644
modifiers := n.Modifiers()
636645
if modifiers != nil {

pkg/ast/nodeflags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const (
4242
NodeFlagsInWithStatement NodeFlags = 1 << 24 // If any ancestor of node was the `statement` of a WithStatement (not the `expression`)
4343
NodeFlagsJsonFile NodeFlags = 1 << 25 // If node was parsed in a Json
4444
NodeFlagsDeprecated NodeFlags = 1 << 26 // If has '@deprecated' JSDoc tag
45+
NodeFlagsUnreachable NodeFlags = 1 << 27 // If node is unreachable according to the binder
4546

4647
NodeFlagsBlockScoped = NodeFlagsLet | NodeFlagsConst | NodeFlagsUsing
4748
NodeFlagsConstant = NodeFlagsConst | NodeFlagsUsing

pkg/ast/utilities.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2336,6 +2336,12 @@ func getModuleInstanceStateForAliasTarget(node *Node, ancestors []*Node, visited
23362336
return ModuleInstanceStateInstantiated
23372337
}
23382338

2339+
func IsInstantiatedModule(node *Node, preserveConstEnums bool) bool {
2340+
moduleState := GetModuleInstanceState(node)
2341+
return moduleState == ModuleInstanceStateInstantiated ||
2342+
(preserveConstEnums && moduleState == ModuleInstanceStateConstEnumOnly)
2343+
}
2344+
23392345
func NodeHasName(statement *Node, id *Node) bool {
23402346
name := statement.Name()
23412347
if name != nil {
@@ -3832,3 +3838,21 @@ func GetFirstConstructorWithBody(node *Node) *Node {
38323838
}
38333839
return nil
38343840
}
3841+
3842+
// Returns true for nodes that are considered executable for the purposes of unreachable code detection.
3843+
func IsPotentiallyExecutableNode(node *Node) bool {
3844+
if KindFirstStatement <= node.Kind && node.Kind <= KindLastStatement {
3845+
if IsVariableStatement(node) {
3846+
declarationList := node.AsVariableStatement().DeclarationList
3847+
if GetCombinedNodeFlags(declarationList)&NodeFlagsBlockScoped != 0 {
3848+
return true
3849+
}
3850+
declarations := declarationList.AsVariableDeclarationList().Declarations.Nodes
3851+
return core.Some(declarations, func(d *Node) bool {
3852+
return d.Initializer() != nil
3853+
})
3854+
}
3855+
return true
3856+
}
3857+
return IsClassDeclaration(node) || IsEnumDeclaration(node) || IsModuleDeclaration(node)
3858+
}

pkg/binder/binder.go

Lines changed: 19 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@ const (
4242
)
4343

4444
type Binder struct {
45-
file *ast.SourceFile
46-
bindFunc func(*ast.Node) bool
47-
unreachableFlow *ast.FlowNode
48-
reportedUnreachableFlow *ast.FlowNode
45+
file *ast.SourceFile
46+
bindFunc func(*ast.Node) bool
47+
unreachableFlow *ast.FlowNode
4948

5049
container *ast.Node
5150
thisContainer *ast.Node
@@ -122,7 +121,6 @@ func bindSourceFile(file *ast.SourceFile) {
122121
b.file = file
123122
b.inStrictMode = b.options().BindInStrictMode && !file.IsDeclarationFile || ast.IsExternalModule(file)
124123
b.unreachableFlow = b.newFlowNode(ast.FlowFlagsUnreachable)
125-
b.reportedUnreachableFlow = b.newFlowNode(ast.FlowFlagsUnreachable)
126124
b.bind(file.AsNode())
127125
file.SymbolCount = b.symbolCount
128126
file.ClassifiableNames = b.classifiableNames
@@ -1535,18 +1533,25 @@ func (b *Binder) bindChildren(node *ast.Node) {
15351533
// Most nodes aren't valid in an assignment pattern, so we clear the value here
15361534
// and set it before we descend into nodes that could actually be part of an assignment pattern.
15371535
b.inAssignmentPattern = false
1538-
if b.checkUnreachable(node) {
1536+
1537+
if b.currentFlow == b.unreachableFlow {
1538+
if flowNodeData := node.FlowNodeData(); flowNodeData != nil {
1539+
flowNodeData.FlowNode = nil
1540+
}
1541+
if ast.IsPotentiallyExecutableNode(node) {
1542+
node.Flags |= ast.NodeFlagsUnreachable
1543+
}
15391544
b.bindEachChild(node)
15401545
b.inAssignmentPattern = saveInAssignmentPattern
15411546
return
15421547
}
1543-
kind := node.Kind
1544-
if kind >= ast.KindFirstStatement && kind <= ast.KindLastStatement && (b.options().AllowUnreachableCode != core.TSTrue || kind == ast.KindReturnStatement) {
1545-
hasFlowNodeData := node.FlowNodeData()
1546-
if hasFlowNodeData != nil {
1547-
hasFlowNodeData.FlowNode = b.currentFlow
1548+
1549+
if ast.KindFirstStatement <= node.Kind && node.Kind <= ast.KindLastStatement {
1550+
if flowNodeData := node.FlowNodeData(); flowNodeData != nil {
1551+
flowNodeData.FlowNode = b.currentFlow
15481552
}
15491553
}
1554+
15501555
switch node.Kind {
15511556
case ast.KindWhileStatement:
15521557
b.bindWhileStatement(node)
@@ -1657,94 +1662,6 @@ func (b *Binder) bindEachStatementFunctionsFirst(statements *ast.NodeList) {
16571662
}
16581663
}
16591664

1660-
func (b *Binder) checkUnreachable(node *ast.Node) bool {
1661-
if b.currentFlow.Flags&ast.FlowFlagsUnreachable == 0 {
1662-
return false
1663-
}
1664-
if b.currentFlow == b.unreachableFlow {
1665-
// report errors on all statements except empty ones
1666-
// report errors on class declarations
1667-
// report errors on enums with preserved emit
1668-
// report errors on instantiated modules
1669-
reportError := ast.IsStatementButNotDeclaration(node) && !ast.IsEmptyStatement(node) ||
1670-
ast.IsClassDeclaration(node) ||
1671-
isEnumDeclarationWithPreservedEmit(node, b.options()) ||
1672-
ast.IsModuleDeclaration(node) && b.shouldReportErrorOnModuleDeclaration(node)
1673-
if reportError {
1674-
b.currentFlow = b.reportedUnreachableFlow
1675-
if b.options().AllowUnreachableCode != core.TSTrue {
1676-
// unreachable code is reported if
1677-
// - user has explicitly asked about it AND
1678-
// - statement is in not ambient context (statements in ambient context is already an error
1679-
// so we should not report extras) AND
1680-
// - node is not variable statement OR
1681-
// - node is block scoped variable statement OR
1682-
// - node is not block scoped variable statement and at least one variable declaration has initializer
1683-
// Rationale: we don't want to report errors on non-initialized var's since they are hoisted
1684-
// On the other side we do want to report errors on non-initialized 'lets' because of TDZ
1685-
isError := unreachableCodeIsError(b.options()) && node.Flags&ast.NodeFlagsAmbient == 0 && (!ast.IsVariableStatement(node) ||
1686-
ast.GetCombinedNodeFlags(node.AsVariableStatement().DeclarationList)&ast.NodeFlagsBlockScoped != 0 ||
1687-
core.Some(node.AsVariableStatement().DeclarationList.AsVariableDeclarationList().Declarations.Nodes, func(d *ast.Node) bool {
1688-
return d.Initializer() != nil
1689-
}))
1690-
b.errorOnEachUnreachableRange(node, isError)
1691-
}
1692-
}
1693-
}
1694-
return true
1695-
}
1696-
1697-
func (b *Binder) shouldReportErrorOnModuleDeclaration(node *ast.Node) bool {
1698-
instanceState := ast.GetModuleInstanceState(node)
1699-
return instanceState == ast.ModuleInstanceStateInstantiated || (instanceState == ast.ModuleInstanceStateConstEnumOnly && b.options().ShouldPreserveConstEnums)
1700-
}
1701-
1702-
func (b *Binder) errorOnEachUnreachableRange(node *ast.Node, isError bool) {
1703-
if b.isExecutableStatement(node) && ast.IsBlock(node.Parent) {
1704-
statements := node.Parent.Statements()
1705-
index := slices.Index(statements, node)
1706-
var first, last *ast.Node
1707-
for _, s := range statements[index:] {
1708-
if b.isExecutableStatement(s) {
1709-
if first == nil {
1710-
first = s
1711-
}
1712-
last = s
1713-
} else if first != nil {
1714-
b.errorOrSuggestionOnRange(isError, first, last, diagnostics.Unreachable_code_detected)
1715-
first = nil
1716-
}
1717-
}
1718-
if first != nil {
1719-
b.errorOrSuggestionOnRange(isError, first, last, diagnostics.Unreachable_code_detected)
1720-
}
1721-
} else {
1722-
b.errorOrSuggestionOnNode(isError, node, diagnostics.Unreachable_code_detected)
1723-
}
1724-
}
1725-
1726-
// As opposed to a pure declaration like an `interface`
1727-
func (b *Binder) isExecutableStatement(s *ast.Node) bool {
1728-
// Don't remove statements that can validly be used before they appear.
1729-
return !ast.IsFunctionDeclaration(s) && !b.isPurelyTypeDeclaration(s) && !(ast.IsVariableStatement(s) && ast.GetCombinedNodeFlags(s)&ast.NodeFlagsBlockScoped == 0 &&
1730-
core.Some(s.AsVariableStatement().DeclarationList.AsVariableDeclarationList().Declarations.Nodes, func(d *ast.Node) bool {
1731-
return d.Initializer() == nil
1732-
}))
1733-
}
1734-
1735-
func (b *Binder) isPurelyTypeDeclaration(s *ast.Node) bool {
1736-
switch s.Kind {
1737-
case ast.KindInterfaceDeclaration, ast.KindTypeAliasDeclaration, ast.KindJSTypeAliasDeclaration:
1738-
return true
1739-
case ast.KindModuleDeclaration:
1740-
return ast.GetModuleInstanceState(s) != ast.ModuleInstanceStateInstantiated
1741-
case ast.KindEnumDeclaration:
1742-
return !isEnumDeclarationWithPreservedEmit(s, b.options())
1743-
default:
1744-
return false
1745-
}
1746-
}
1747-
17481665
func (b *Binder) setContinueTarget(node *ast.Node, target *ast.FlowLabel) *ast.FlowLabel {
17491666
label := b.activeLabelList
17501667
for label != nil && node.Parent.Kind == ast.KindLabeledStatement {
@@ -2131,8 +2048,9 @@ func (b *Binder) bindLabeledStatement(node *ast.Node) {
21312048
}
21322049
b.bind(stmt.Label)
21332050
b.bind(stmt.Statement)
2134-
if !b.activeLabelList.referenced && b.options().AllowUnusedLabels != core.TSTrue {
2135-
b.errorOrSuggestionOnNode(unusedLabelIsError(b.options()), stmt.Label, diagnostics.Unused_label)
2051+
if !b.activeLabelList.referenced {
2052+
// Mark the label as unused; the checker will decide whether to report it
2053+
stmt.Label.Flags |= ast.NodeFlagsUnreachable
21362054
}
21372055
b.activeLabelList = b.activeLabelList.next
21382056
b.addAntecedent(postStatementLabel, b.currentFlow)
@@ -2454,10 +2372,6 @@ func (b *Binder) bindInitializer(node *ast.Node) {
24542372
b.currentFlow = b.finishFlowLabel(exitFlow)
24552373
}
24562374

2457-
func isEnumDeclarationWithPreservedEmit(node *ast.Node, options core.SourceFileAffectingCompilerOptions) bool {
2458-
return node.Kind == ast.KindEnumDeclaration && (!ast.IsEnumConst(node) || options.ShouldPreserveConstEnums)
2459-
}
2460-
24612375
func setFlowNode(node *ast.Node, flowNode *ast.FlowNode) {
24622376
data := node.FlowNodeData()
24632377
if data != nil {
@@ -2749,14 +2663,6 @@ func isFunctionSymbol(symbol *ast.Symbol) bool {
27492663
return false
27502664
}
27512665

2752-
func unreachableCodeIsError(options core.SourceFileAffectingCompilerOptions) bool {
2753-
return options.AllowUnreachableCode == core.TSFalse
2754-
}
2755-
2756-
func unusedLabelIsError(options core.SourceFileAffectingCompilerOptions) bool {
2757-
return options.AllowUnusedLabels == core.TSFalse
2758-
}
2759-
27602666
func isStatementCondition(node *ast.Node) bool {
27612667
switch node.Parent.Kind {
27622668
case ast.KindIfStatement, ast.KindWhileStatement, ast.KindDoStatement:

0 commit comments

Comments
 (0)