Skip to content

Commit

Permalink
convert an if to a switch
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jun 18, 2022
1 parent d981064 commit 465ae2a
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions internal/js_parser/ts_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ var tsTypeIdentifierMap = map[string]tsTypeIdentifierKind{
}

func (p *parser) skipTypeScriptTypeWithOpts(level js_ast.L, opts skipTypeOpts) {
loop:
for {
switch p.lexer.Token {
case js_lexer.TNumericLiteral, js_lexer.TBigIntegerLiteral, js_lexer.TStringLiteral,
Expand Down Expand Up @@ -291,8 +292,10 @@ func (p *parser) skipTypeScriptTypeWithOpts(level js_ast.L, opts skipTypeOpts) {

case js_lexer.TIdentifier:
kind := tsTypeIdentifierMap[p.lexer.Identifier.String]
checkTypeParameters := true

if kind == tsTypeIdentifierPrefix {
switch kind {
case tsTypeIdentifierPrefix:
p.lexer.Next()

// Valid:
Expand All @@ -305,38 +308,39 @@ func (p *parser) skipTypeScriptTypeWithOpts(level js_ast.L, opts skipTypeOpts) {
if p.lexer.Token != js_lexer.TColon || (!opts.isIndexSignature && !opts.allowTupleLabels) {
p.skipTypeScriptType(js_ast.LPrefix)
}
break
}
break loop

checkTypeParameters := true

if kind == tsTypeIdentifierUnique {
case tsTypeIdentifierUnique:
p.lexer.Next()

// "let foo: unique symbol"
if p.lexer.IsContextualKeyword("symbol") {
p.lexer.Next()
break
break loop
}
} else if kind == tsTypeIdentifierAbstract {

case tsTypeIdentifierAbstract:
p.lexer.Next()

// "let foo: abstract new () => {}" added in TypeScript 4.2
if p.lexer.Token == js_lexer.TNew {
continue
}
} else if kind == tsTypeIdentifierAsserts {

case tsTypeIdentifierAsserts:
p.lexer.Next()

// "function assert(x: boolean): asserts x"
// "function assert(x: boolean): asserts x is boolean"
if opts.isReturnType && !p.lexer.HasNewlineBefore && (p.lexer.Token == js_lexer.TIdentifier || p.lexer.Token == js_lexer.TThis) {
p.lexer.Next()
}
} else if kind == tsTypeIdentifierPrimitive {

case tsTypeIdentifierPrimitive:
p.lexer.Next()
checkTypeParameters = false
} else {

default:
p.lexer.Next()
}

Expand Down

0 comments on commit 465ae2a

Please sign in to comment.