Skip to content

Commit 77d9420

Browse files
committed
refactor: use slices.Contains and slices.ContainsFunc to simplify code
Signed-off-by: tongjicoder <[email protected]>
1 parent 1b88303 commit 77d9420

File tree

5 files changed

+14
-27
lines changed

5 files changed

+14
-27
lines changed

internal/ast/ast.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ast
22

33
import (
44
"fmt"
5+
"slices"
56
"strings"
67
"sync"
78
"sync/atomic"
@@ -22,12 +23,7 @@ func visit(v Visitor, node *Node) bool {
2223
}
2324

2425
func visitNodes(v Visitor, nodes []*Node) bool {
25-
for _, node := range nodes {
26-
if v(node) {
27-
return true
28-
}
29-
}
30-
return false
26+
return slices.ContainsFunc(nodes, v)
3127
}
3228

3329
func visitNodeList(v Visitor, nodeList *NodeList) bool {

internal/binder/binder.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,10 +2666,8 @@ func isNarrowableReference(node *ast.Node) bool {
26662666

26672667
func hasNarrowableArgument(expr *ast.Node) bool {
26682668
call := expr.AsCallExpression()
2669-
for _, argument := range call.Arguments.Nodes {
2670-
if containsNarrowableReference(argument) {
2671-
return true
2672-
}
2669+
if slices.ContainsFunc(call.Arguments.Nodes, containsNarrowableReference) {
2670+
return true
26732671
}
26742672
if ast.IsPropertyAccessExpression(call.Expression) {
26752673
if containsNarrowableReference(call.Expression.AsPropertyAccessExpression().Expression) {

internal/checker/grammarchecks.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package checker
22

33
import (
44
"fmt"
5+
"slices"
56
"strings"
67

78
"github.com/microsoft/typescript-go/internal/ast"
@@ -887,12 +888,7 @@ func (c *Checker) checkGrammarHeritageClause(node *ast.HeritageClause) bool {
887888
return c.grammarErrorAtPos(node.AsNode(), types.Pos(), 0, diagnostics.X_0_list_cannot_be_empty, listType)
888889
}
889890

890-
for _, node := range types.Nodes {
891-
if c.checkGrammarExpressionWithTypeArguments(node) {
892-
return true
893-
}
894-
}
895-
return false
891+
return slices.ContainsFunc(types.Nodes, c.checkGrammarExpressionWithTypeArguments)
896892
}
897893

898894
func (c *Checker) checkGrammarExpressionWithTypeArguments(node *ast.Node /*Union[ExpressionWithTypeArguments, TypeQueryNode]*/) bool {

internal/checker/mapper.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package checker
22

3-
import "github.com/microsoft/typescript-go/internal/core"
3+
import (
4+
"slices"
5+
6+
"github.com/microsoft/typescript-go/internal/core"
7+
)
48

59
// TypeMapperKind
610

@@ -158,10 +162,8 @@ func newArrayToSingleTypeMapper(sources []*Type, target *Type) *TypeMapper {
158162
}
159163

160164
func (m *ArrayToSingleTypeMapper) Map(t *Type) *Type {
161-
for _, s := range m.sources {
162-
if t == s {
163-
return m.target
164-
}
165+
if slices.Contains(m.sources, t) {
166+
return m.target
165167
}
166168
return t
167169
}

internal/core/core.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,7 @@ func Same[T any](s1 []T, s2 []T) bool {
135135
}
136136

137137
func Some[T any](slice []T, f func(T) bool) bool {
138-
for _, value := range slice {
139-
if f(value) {
140-
return true
141-
}
142-
}
143-
return false
138+
return slices.ContainsFunc(slice, f)
144139
}
145140

146141
func Every[T any](slice []T, f func(T) bool) bool {

0 commit comments

Comments
 (0)