Skip to content

fix: parser not support null | T #2507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,11 @@ export class NamedTypeNode extends TypeNode {
let typeArguments = this.typeArguments;
return typeArguments != null && typeArguments.length > 0;
}

/** Tests if this type is "null". */
get isNull(): bool {
return this.name.identifier.text == "null";
}
}

/** Represents a function type. */
Expand Down
57 changes: 25 additions & 32 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,45 +507,29 @@ export class Parser extends DiagnosticEmitter {
// '(' ...
if (token == Token.OpenParen) {

// '(' FunctionSignature ')' '|' 'null'?
let isNullableSignature = tn.skip(Token.OpenParen);
// '(' FunctionSignature ')'
let isInnerParenthesized = tn.skip(Token.OpenParen);
// FunctionSignature?
let signature = this.tryParseFunctionType(tn);
if (signature) {
if (isNullableSignature) {
if (isInnerParenthesized) {
if (!tn.skip(Token.CloseParen)) {
this.error(
DiagnosticCode._0_expected,
tn.range(), ")"
);
return null;
}
if (!tn.skip(Token.Bar)) {
this.error(
DiagnosticCode._0_expected,
tn.range(), "|"
);
return null;
}
if (!tn.skip(Token.Null)) {
this.error(
DiagnosticCode._0_expected,
tn.range(), "null"
);
}
signature.isNullable = true;
}
return signature;
} else if (isNullableSignature || this.tryParseSignatureIsSignature) {
type = signature;
} else if (isInnerParenthesized || this.tryParseSignatureIsSignature) {
this.error(
DiagnosticCode.Unexpected_token,
tn.range()
);
return null;
}

// Type (',' Type)* ')'
if (acceptParenthesized) {
} else if (acceptParenthesized) {
let innerType = this.parseType(tn, false, suppressErrors);
if (!innerType) return null;
if (!tn.skip(Token.CloseParen)) {
Expand Down Expand Up @@ -634,20 +618,29 @@ export class Parser extends DiagnosticEmitter {
}
return null;
}
// ... | null
// ... | type
while (tn.skip(Token.Bar)) {
if (tn.skip(Token.Null)) {
type.isNullable = true;
} else {
let notNullStart = tn.pos;
let notNull = this.parseType(tn, false, true);
let nextType = this.parseType(tn, false, true);
if (!nextType) return null;
let typeIsNull = type.kind == NodeKind.NamedType && (<NamedTypeNode>type).isNull;
let nextTypeIsNull = nextType.kind == NodeKind.NamedType && (<NamedTypeNode>nextType).isNull;
if (!typeIsNull && !nextTypeIsNull) {
if (!suppressErrors) {
this.error(
DiagnosticCode._0_expected,
notNull ? notNull.range : tn.range(notNullStart), "null"
DiagnosticCode.Not_implemented_0, nextType.range, "union types"
);
}
return null;
} else if (nextTypeIsNull) {
type.isNullable = true;
type.range.end = nextType.range.end;
} else if (typeIsNull) {
nextType.range.start = type.range.start;
nextType.isNullable = true;
type = nextType;
} else {
// `null | null` still `null`
type.range.end = nextType.range.end;
}
}
// ... [][]
Expand All @@ -672,8 +665,8 @@ export class Parser extends DiagnosticEmitter {
} else {
if (!suppressErrors) {
this.error(
DiagnosticCode._0_expected,
tn.range(), "null"
DiagnosticCode.Not_implemented_0,
tn.range(), "union types"
);
}
return null;
Expand Down
4 changes: 4 additions & 0 deletions tests/parser/type-signature.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
type foo = () => void;
type foo = (() => void) | null;
type foo = null | (() => void);
type foo = (() => void)[];
type foo = (() => void)[] | null;
type foo = null | (() => void)[];
type foo = (a: i32) => i32;
type foo = (a?: i32) => i32;
type foo = (this: AClass, a: i32) => i32;
Expand Down
4 changes: 4 additions & 0 deletions tests/parser/type-signature.ts.fixture.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
type foo = () => void;
type foo = (() => void) | null;
type foo = (() => void) | null;
type foo = Array<() => void>;
type foo = Array<() => void> | null;
type foo = Array<() => void> | null;
type foo = (a: i32) => i32;
type foo = (a?: i32) => i32;
type foo = (this: AClass, a: i32) => i32;
Expand Down
4 changes: 4 additions & 0 deletions tests/parser/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ export type T7 = Array<T7>;
export type T8 = Map<string, Array<T8>>;
export type T9 = Array<() => T9>;
export type T10 = T6<T10>;

export type T11 = T1 | null
export type T12 = null | T1

2 changes: 2 additions & 0 deletions tests/parser/type.ts.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ type int32_t = i32;
export type uint64_t = u64;
export type T1 = int32_t;
export type T2 = int32_t;
export type T11 = T1 | null;
export type T12 = T1 | null;
// ERROR 2456: "Type alias 'T3' circularly references itself." in type.ts(11,23+4)
// ERROR 100: "Not implemented: Recursion in type aliases" in type.ts(12,29+3)
// ERROR 100: "Not implemented: Recursion in type aliases" in type.ts(13,24+2)
Expand Down