Skip to content

Commit

Permalink
fix: bigint is a number literal
Browse files Browse the repository at this point in the history
closes #136
  • Loading branch information
3cp committed Oct 28, 2020
1 parent 9504b6a commit 2ad1a27
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4452,13 +4452,13 @@ export function parseBigIntLiteral(
? {
type: 'Literal',
value: tokenValue,
bigint: tokenRaw.substring(0, tokenRaw.length - 1), // without the ending "n"
bigint: tokenRaw.slice(0, -1), // without the ending "n"
raw: tokenRaw
}
: {
type: 'Literal',
value: tokenValue,
bigint: tokenRaw.substring(0, tokenRaw.length - 1) // without the ending "n"
bigint: tokenRaw.slice(0, -1) // without the ending "n"
}
);
}
Expand Down Expand Up @@ -4721,6 +4721,10 @@ export function parseIdentifier(parser: ParserState, context: Context, isPattern
*/
export function parseLiteral(parser: ParserState, context: Context): ESTree.Literal {
const { tokenValue, tokenRaw, tokenPos, linePos, colPos } = parser;
if (parser.token === Token.BigIntLiteral) {
return parseBigIntLiteral(parser, context, tokenPos, linePos, colPos);
}

nextToken(parser, context);
parser.assignable = AssignmentKind.CannotAssign;
return finishNode(
Expand Down
2 changes: 1 addition & 1 deletion src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export const enum Token {

// Stage #3 proposals
PrivateName = 121,
BigIntLiteral = 122,
BigIntLiteral = 122 | IsExpressionStart | IsStringOrNumber,
Coalesce = 123 | IsBinaryOp | IsCoalesc | 1 << PrecStart, // ??
QuestionMarkPeriod = 124 | IsMemberOrCallExpression, // ?.

Expand Down
45 changes: 45 additions & 0 deletions test/parser/expressions/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12634,6 +12634,51 @@ describe('Expressions - Class', () => {
sourceType: 'script',
type: 'Program'
}
],
[
`class A { [1n](){} }`,
Context.None,
{
body: [
{
body: {
body: [
{
computed: true,
key: {
bigint: '1',
type: 'Literal',
value: 1n
},
kind: 'method',
static: false,
type: 'MethodDefinition',
value: {
async: false,
body: {
body: [],
type: 'BlockStatement'
},
generator: false,
id: null,
params: [],
type: 'FunctionExpression'
}
}
],
type: 'ClassBody'
},
id: {
name: 'A',
type: 'Identifier'
},
superClass: null,
type: 'ClassDeclaration'
}
],
sourceType: 'script',
type: 'Program'
}
]
]);
});
52 changes: 52 additions & 0 deletions test/parser/expressions/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26630,6 +26630,58 @@ describe('Expressions - Object', () => {
}
]
}
],
[
'({1n:1})',
Context.OptionsRanges,
{
sourceType: 'script',
start: 0,
type: 'Program',
body: [
{
end: 8,
expression: {
end: 7,
properties: [
{
computed: false,
end: 6,
key: {
bigint: '1',
end: 4,
range: [2, 4],
start: 2,
type: 'Literal',
value: 1n
},
kind: 'init',
method: false,
range: [2, 6],
shorthand: false,
start: 2,
type: 'Property',
value: {
end: 6,
range: [5, 6],
start: 5,
type: 'Literal',
value: 1
}
}
],
range: [1, 7],
start: 1,
type: 'ObjectExpression'
},
range: [0, 8],
start: 0,
type: 'ExpressionStatement'
}
],
end: 8,
range: [0, 8]
}
]
]);
});

0 comments on commit 2ad1a27

Please sign in to comment.