@@ -507,45 +507,29 @@ export class Parser extends DiagnosticEmitter {
507507 // '(' ...
508508 if ( token == Token . OpenParen ) {
509509
510- // '(' FunctionSignature ')' '|' 'null'?
511- let isNullableSignature = tn . skip ( Token . OpenParen ) ;
510+ // '(' FunctionSignature ')'
511+ let isInnerParenthesized = tn . skip ( Token . OpenParen ) ;
512512 // FunctionSignature?
513513 let signature = this . tryParseFunctionType ( tn ) ;
514514 if ( signature ) {
515- if ( isNullableSignature ) {
515+ if ( isInnerParenthesized ) {
516516 if ( ! tn . skip ( Token . CloseParen ) ) {
517517 this . error (
518518 DiagnosticCode . _0_expected ,
519519 tn . range ( ) , ")"
520520 ) ;
521521 return null ;
522522 }
523- if ( ! tn . skip ( Token . Bar ) ) {
524- this . error (
525- DiagnosticCode . _0_expected ,
526- tn . range ( ) , "|"
527- ) ;
528- return null ;
529- }
530- if ( ! tn . skip ( Token . Null ) ) {
531- this . error (
532- DiagnosticCode . _0_expected ,
533- tn . range ( ) , "null"
534- ) ;
535- }
536- signature . isNullable = true ;
537523 }
538- return signature ;
539- } else if ( isNullableSignature || this . tryParseSignatureIsSignature ) {
524+ type = signature ;
525+ } else if ( isInnerParenthesized || this . tryParseSignatureIsSignature ) {
540526 this . error (
541527 DiagnosticCode . Unexpected_token ,
542528 tn . range ( )
543529 ) ;
544530 return null ;
545- }
546-
547531 // Type (',' Type)* ')'
548- if ( acceptParenthesized ) {
532+ } else if ( acceptParenthesized ) {
549533 let innerType = this . parseType ( tn , false , suppressErrors ) ;
550534 if ( ! innerType ) return null ;
551535 if ( ! tn . skip ( Token . CloseParen ) ) {
@@ -634,20 +618,29 @@ export class Parser extends DiagnosticEmitter {
634618 }
635619 return null ;
636620 }
637- // ... | null
621+ // ... | type
638622 while ( tn . skip ( Token . Bar ) ) {
639- if ( tn . skip ( Token . Null ) ) {
640- type . isNullable = true ;
641- } else {
642- let notNullStart = tn . pos ;
643- let notNull = this . parseType ( tn , false , true ) ;
623+ let nextType = this . parseType ( tn , false , true ) ;
624+ if ( ! nextType ) return null ;
625+ let typeIsNull = type . kind == NodeKind . NamedType && ( < NamedTypeNode > type ) . isNull ;
626+ let nextTypeIsNull = nextType . kind == NodeKind . NamedType && ( < NamedTypeNode > nextType ) . isNull ;
627+ if ( ! typeIsNull && ! nextTypeIsNull ) {
644628 if ( ! suppressErrors ) {
645629 this . error (
646- DiagnosticCode . _0_expected ,
647- notNull ? notNull . range : tn . range ( notNullStart ) , "null"
630+ DiagnosticCode . Not_implemented_0 , nextType . range , "union types"
648631 ) ;
649632 }
650633 return null ;
634+ } else if ( nextTypeIsNull ) {
635+ type . isNullable = true ;
636+ type . range . end = nextType . range . end ;
637+ } else if ( typeIsNull ) {
638+ nextType . range . start = type . range . start ;
639+ nextType . isNullable = true ;
640+ type = nextType ;
641+ } else {
642+ // `null | null` still `null`
643+ type . range . end = nextType . range . end ;
651644 }
652645 }
653646 // ... [][]
@@ -672,8 +665,8 @@ export class Parser extends DiagnosticEmitter {
672665 } else {
673666 if ( ! suppressErrors ) {
674667 this . error (
675- DiagnosticCode . _0_expected ,
676- tn . range ( ) , "null "
668+ DiagnosticCode . Not_implemented_0 ,
669+ tn . range ( ) , "union types "
677670 ) ;
678671 }
679672 return null ;
0 commit comments