Skip to content

Commit

Permalink
fix(parser): fixed const enum values and extended API tests to guard …
Browse files Browse the repository at this point in the history
…against TS issues
  • Loading branch information
KFlash committed Aug 6, 2019
1 parent 4ed317c commit c69ac52
Show file tree
Hide file tree
Showing 3 changed files with 519 additions and 35 deletions.
27 changes: 16 additions & 11 deletions src/lexer/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,32 @@ export function scanJSXToken(parser: ParserState): Token {
const token = TokenLookup[parser.source.charCodeAt(parser.index)];

switch (token) {
// '<'
case Token.LessThan: {
advanceChar(parser);
if (parser.currentChar === Chars.Slash) {
advanceChar(parser);
return (parser.token = Token.JSXClose);
parser.token = Token.JSXClose;
} else {
parser.token = Token.LessThan;
}

return (parser.token = Token.LessThan);
break;
}
// '{'
case Token.LeftBrace: {
advanceChar(parser);
return (parser.token = Token.LeftBrace);
parser.token = Token.LeftBrace;
break;
}
default: // ignore
}

while (parser.index < parser.end && (CharTypes[advanceChar(parser)] & CharFlags.JSXToken) === 0) {}
default:
while (parser.index < parser.end && (CharTypes[advanceChar(parser)] & CharFlags.JSXToken) === 0) {}

parser.tokenValue = parser.source.slice(parser.tokenPos, parser.index);
parser.tokenValue = parser.source.slice(parser.tokenPos, parser.index);
parser.token = Token.JSXText;
}

return (parser.token = Token.JSXText);
return parser.token;
}

/**
Expand All @@ -96,6 +101,6 @@ export function scanJSXIdentifier(parser: ParserState): Token {
}
parser.tokenValue += parser.source.slice(index, parser.index);
}

return (parser.token = Token.Identifier);
parser.token = Token.Identifier;
return parser.token;
}
49 changes: 26 additions & 23 deletions src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ export const enum Token {
Type = 0xFF,

/* Precedence for binary operators (always positive) */
PrecStart = 8,
Precedence = 15 << PrecStart, // 8-11
PrecStart = 8,
Precedence = 15 << PrecStart, // 8-11

/* Attribute names */
Keyword = 1 << 12,
Expand Down Expand Up @@ -170,28 +170,29 @@ export const enum Token {
Eval = 116 | IsEvalOrArguments,
Arguments = 117 | IsEvalOrArguments,

EscapedReserved = 118 | IsIdentifier,
EscapedFutureReserved = 119 | IsIdentifier,
ReservedIfStrict = 120 | IsIdentifier,
EscapedReserved = 118 | IsIdentifier,
EscapedFutureReserved = 119 | IsIdentifier,
ReservedIfStrict = 120 | IsIdentifier,

PrivateName = 121,
BigIntLiteral = 122,
// Stage #3 proposals
Coalesce = 123 | IsBinaryOp | IsCoalesc | 1 << PrecStart, // ??,
QuestionMarkPeriod = 124 | IsMemberOrCallExpression, // ?.,
// Misc
WhiteSpace = 125,
Illegal = 126,
CarriageReturn = 127,
PrivateField = 128,
Template = 129,
Decorator = 130,
Target = 131 | IsIdentifier,
LineFeed = 132,
EscapedIdentifier = 133,
JSXText = 134,


PrivateName = 121,
BigIntLiteral = 122,
Coalesce = 123 | IsBinaryOp | IsCoalesc | 1 << PrecStart, // ??
QuestionMarkPeriod = 124 | IsMemberOrCallExpression, // ?.

// Others
WhiteSpace = 125,
Illegal = 126,
CarriageReturn = 127,
PrivateField = 128,
Template = 129,
Decorator = 130,
Target = 131 | IsIdentifier,
LineFeed = 132,
EscapedIdentifier = 133,

// JSX
JSXText = 134,
}

export const KeywordDescTable = [
Expand Down Expand Up @@ -235,7 +236,9 @@ export const KeywordDescTable = [
/* Others */
'enum', 'eval', 'arguments', 'escaped reserved', 'escaped future reserved', 'reserved if strict', '#',

'BigIntLiteral', '??', '?.', 'WhiteSpace', 'Illegal', 'LineTerminator', 'PrivateField', 'Template', '@', 'target', 'LineFeed', 'Escaped', 'JSXText'
'BigIntLiteral', '??', '?.', 'WhiteSpace', 'Illegal', 'LineTerminator', 'PrivateField',

'Template', '@', 'target', 'LineFeed', 'Escaped', 'JSXText'
];

// Normal object is much faster than Object.create(null), even with typeof check to avoid Object.prototype interference
Expand Down
Loading

0 comments on commit c69ac52

Please sign in to comment.