Skip to content

Commit

Permalink
nullを型注釈に指定できるように (#831)
Browse files Browse the repository at this point in the history
  • Loading branch information
takejohn authored Oct 30, 2024
1 parent b70ffcb commit 91c7bff
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/parser/syntaxes/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,15 @@ function parseFnType(s: ITokenStream): Ast.TypeSource {
function parseNamedType(s: ITokenStream): Ast.TypeSource {
const startPos = s.getPos();

s.expect(TokenKind.Identifier);
const name = s.getTokenValue();
s.next();
let name: string;
if (s.is(TokenKind.Identifier)) {
name = s.getTokenValue();
s.next();
} else {
s.expect(TokenKind.NullKeyword);
s.next();
name = "null";
}

// inner type
let inner: Ast.TypeSource | undefined;
Expand Down
16 changes: 16 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,22 @@ describe('type declaration', () => {
`);
eq(res, ARR([NUM(1), NUM(2), NUM(3), NUM(0), NUM(5)]));
});

test.concurrent('def (null)', async () => {
const res = await exe(`
let a: null = null
<: a
`);
eq(res, NULL);
});

test.concurrent('fn def (null)', async () => {
const res = await exe(`
@f(): null {}
<: f()
`);
eq(res, NULL);
});
});

describe('Attribute', () => {
Expand Down

0 comments on commit 91c7bff

Please sign in to comment.