Skip to content

Commit ddf0df9

Browse files
committed
Introduce TokenFlags enum
1 parent b8fbf88 commit ddf0df9

6 files changed

Lines changed: 46 additions & 52 deletions

File tree

src/compiler/binder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,7 +1835,7 @@ namespace ts {
18351835
}
18361836

18371837
function checkStrictModeNumericLiteral(node: NumericLiteral) {
1838-
if (inStrictMode && node.numericLiteralFlags & NumericLiteralFlags.Octal) {
1838+
if (inStrictMode && node.numericLiteralFlags & TokenFlags.Octal) {
18391839
file.bindDiagnostics.push(createDiagnosticForNode(node, Diagnostics.Octal_literals_are_not_allowed_in_strict_mode));
18401840
}
18411841
}
@@ -3319,7 +3319,7 @@ namespace ts {
33193319
break;
33203320

33213321
case SyntaxKind.NumericLiteral:
3322-
if ((<NumericLiteral>node).numericLiteralFlags & NumericLiteralFlags.BinaryOrOctalSpecifier) {
3322+
if ((<NumericLiteral>node).numericLiteralFlags & TokenFlags.BinaryOrOctalSpecifier) {
33233323
transformFlags |= TransformFlags.AssertES2015;
33243324
}
33253325
break;

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25779,7 +25779,7 @@ namespace ts {
2577925779

2578025780
function checkGrammarNumericLiteral(node: NumericLiteral): boolean {
2578125781
// Grammar checking
25782-
if (node.numericLiteralFlags & NumericLiteralFlags.Octal) {
25782+
if (node.numericLiteralFlags & TokenFlags.Octal) {
2578325783
let diagnosticMessage: DiagnosticMessage | undefined;
2578425784
if (languageVersion >= ScriptTarget.ES5) {
2578525785
diagnosticMessage = Diagnostics.Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher_Use_the_syntax_0;

src/compiler/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2111,7 +2111,7 @@ namespace ts {
21112111
// We also do not need to check for negatives because any prefix operator would be part of a
21122112
// parent unary expression.
21132113
if (node.kind === SyntaxKind.NumericLiteral) {
2114-
(<NumericLiteral>node).numericLiteralFlags = scanner.getNumericLiteralFlags();
2114+
(<NumericLiteral>node).numericLiteralFlags = scanner.getTokenFlags() & TokenFlags.NumericLiteralFlags;
21152115
}
21162116

21172117
nextToken();

src/compiler/scanner.ts

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace ts {
2727
isReservedWord(): boolean;
2828
isUnterminated(): boolean;
2929
/* @internal */
30-
getNumericLiteralFlags(): NumericLiteralFlags;
30+
getTokenFlags(): TokenFlags;
3131
reScanGreaterToken(): SyntaxKind;
3232
reScanSlashToken(): SyntaxKind;
3333
reScanTemplateToken(): SyntaxKind;
@@ -814,10 +814,7 @@ namespace ts {
814814

815815
let token: SyntaxKind;
816816
let tokenValue: string;
817-
let precedingLineBreak: boolean;
818-
let hasExtendedUnicodeEscape: boolean;
819-
let tokenIsUnterminated: boolean;
820-
let numericLiteralFlags: NumericLiteralFlags;
817+
let tokenFlags: TokenFlags;
821818

822819
setText(text, start, length);
823820

@@ -828,12 +825,12 @@ namespace ts {
828825
getTokenPos: () => tokenPos,
829826
getTokenText: () => text.substring(tokenPos, pos),
830827
getTokenValue: () => tokenValue,
831-
hasExtendedUnicodeEscape: () => hasExtendedUnicodeEscape,
832-
hasPrecedingLineBreak: () => precedingLineBreak,
828+
hasExtendedUnicodeEscape: () => (tokenFlags & TokenFlags.ExtendedUnicodeEscape) !== 0,
829+
hasPrecedingLineBreak: () => (tokenFlags & TokenFlags.PrecedingLineBreak) !== 0,
833830
isIdentifier: () => token === SyntaxKind.Identifier || token > SyntaxKind.LastReservedWord,
834831
isReservedWord: () => token >= SyntaxKind.FirstReservedWord && token <= SyntaxKind.LastReservedWord,
835-
isUnterminated: () => tokenIsUnterminated,
836-
getNumericLiteralFlags: () => numericLiteralFlags,
832+
isUnterminated: () => (tokenFlags & TokenFlags.Unterminated) !== 0,
833+
getTokenFlags: () => tokenFlags,
837834
reScanGreaterToken,
838835
reScanSlashToken,
839836
reScanTemplateToken,
@@ -870,7 +867,7 @@ namespace ts {
870867
let end = pos;
871868
if (text.charCodeAt(pos) === CharacterCodes.E || text.charCodeAt(pos) === CharacterCodes.e) {
872869
pos++;
873-
numericLiteralFlags = NumericLiteralFlags.Scientific;
870+
tokenFlags |= TokenFlags.Scientific;
874871
if (text.charCodeAt(pos) === CharacterCodes.plus || text.charCodeAt(pos) === CharacterCodes.minus) pos++;
875872
if (isDigit(text.charCodeAt(pos))) {
876873
pos++;
@@ -942,7 +939,7 @@ namespace ts {
942939
while (true) {
943940
if (pos >= end) {
944941
result += text.substring(start, pos);
945-
tokenIsUnterminated = true;
942+
tokenFlags |= TokenFlags.Unterminated;
946943
error(Diagnostics.Unterminated_string_literal);
947944
break;
948945
}
@@ -960,7 +957,7 @@ namespace ts {
960957
}
961958
if (isLineBreak(ch)) {
962959
result += text.substring(start, pos);
963-
tokenIsUnterminated = true;
960+
tokenFlags |= TokenFlags.Unterminated;
964961
error(Diagnostics.Unterminated_string_literal);
965962
break;
966963
}
@@ -984,7 +981,7 @@ namespace ts {
984981
while (true) {
985982
if (pos >= end) {
986983
contents += text.substring(start, pos);
987-
tokenIsUnterminated = true;
984+
tokenFlags |= TokenFlags.Unterminated;
988985
error(Diagnostics.Unterminated_template_literal);
989986
resultingToken = startedWithBacktick ? SyntaxKind.NoSubstitutionTemplateLiteral : SyntaxKind.TemplateTail;
990987
break;
@@ -1070,7 +1067,7 @@ namespace ts {
10701067
case CharacterCodes.u:
10711068
// '\u{DDDDDDDD}'
10721069
if (pos < end && text.charCodeAt(pos) === CharacterCodes.openBrace) {
1073-
hasExtendedUnicodeEscape = true;
1070+
tokenFlags |= TokenFlags.ExtendedUnicodeEscape;
10741071
pos++;
10751072
return scanExtendedUnicodeEscape();
10761073
}
@@ -1239,10 +1236,7 @@ namespace ts {
12391236

12401237
function scan(): SyntaxKind {
12411238
startPos = pos;
1242-
hasExtendedUnicodeEscape = false;
1243-
precedingLineBreak = false;
1244-
tokenIsUnterminated = false;
1245-
numericLiteralFlags = 0;
1239+
tokenFlags = 0;
12461240
while (true) {
12471241
tokenPos = pos;
12481242
if (pos >= end) {
@@ -1264,7 +1258,7 @@ namespace ts {
12641258
switch (ch) {
12651259
case CharacterCodes.lineFeed:
12661260
case CharacterCodes.carriageReturn:
1267-
precedingLineBreak = true;
1261+
tokenFlags |= TokenFlags.PrecedingLineBreak;
12681262
if (skipTrivia) {
12691263
pos++;
12701264
continue;
@@ -1407,7 +1401,7 @@ namespace ts {
14071401
}
14081402

14091403
if (isLineBreak(ch)) {
1410-
precedingLineBreak = true;
1404+
tokenFlags |= TokenFlags.PrecedingLineBreak;
14111405
}
14121406
pos++;
14131407
}
@@ -1420,7 +1414,9 @@ namespace ts {
14201414
continue;
14211415
}
14221416
else {
1423-
tokenIsUnterminated = !commentClosed;
1417+
if (!commentClosed) {
1418+
tokenFlags |= TokenFlags.Unterminated;
1419+
}
14241420
return token = SyntaxKind.MultiLineCommentTrivia;
14251421
}
14261422
}
@@ -1441,7 +1437,7 @@ namespace ts {
14411437
value = 0;
14421438
}
14431439
tokenValue = "" + value;
1444-
numericLiteralFlags = NumericLiteralFlags.HexSpecifier;
1440+
tokenFlags |= TokenFlags.HexSpecifier;
14451441
return token = SyntaxKind.NumericLiteral;
14461442
}
14471443
else if (pos + 2 < end && (text.charCodeAt(pos + 1) === CharacterCodes.B || text.charCodeAt(pos + 1) === CharacterCodes.b)) {
@@ -1452,7 +1448,7 @@ namespace ts {
14521448
value = 0;
14531449
}
14541450
tokenValue = "" + value;
1455-
numericLiteralFlags = NumericLiteralFlags.BinarySpecifier;
1451+
tokenFlags |= TokenFlags.BinarySpecifier;
14561452
return token = SyntaxKind.NumericLiteral;
14571453
}
14581454
else if (pos + 2 < end && (text.charCodeAt(pos + 1) === CharacterCodes.O || text.charCodeAt(pos + 1) === CharacterCodes.o)) {
@@ -1463,13 +1459,13 @@ namespace ts {
14631459
value = 0;
14641460
}
14651461
tokenValue = "" + value;
1466-
numericLiteralFlags = NumericLiteralFlags.OctalSpecifier;
1462+
tokenFlags |= TokenFlags.OctalSpecifier;
14671463
return token = SyntaxKind.NumericLiteral;
14681464
}
14691465
// Try to parse as an octal
14701466
if (pos + 1 < end && isOctalDigit(text.charCodeAt(pos + 1))) {
14711467
tokenValue = "" + scanOctalDigits();
1472-
numericLiteralFlags = NumericLiteralFlags.Octal;
1468+
tokenFlags |= TokenFlags.Octal;
14731469
return token = SyntaxKind.NumericLiteral;
14741470
}
14751471
// This fall-through is a deviation from the EcmaScript grammar. The grammar says that a leading zero
@@ -1626,7 +1622,7 @@ namespace ts {
16261622
continue;
16271623
}
16281624
else if (isLineBreak(ch)) {
1629-
precedingLineBreak = true;
1625+
tokenFlags |= TokenFlags.PrecedingLineBreak;
16301626
pos++;
16311627
continue;
16321628
}
@@ -1669,14 +1665,14 @@ namespace ts {
16691665
// If we reach the end of a file, or hit a newline, then this is an unterminated
16701666
// regex. Report error and return what we have so far.
16711667
if (p >= end) {
1672-
tokenIsUnterminated = true;
1668+
tokenFlags |= TokenFlags.Unterminated;
16731669
error(Diagnostics.Unterminated_regular_expression_literal);
16741670
break;
16751671
}
16761672

16771673
const ch = text.charCodeAt(p);
16781674
if (isLineBreak(ch)) {
1679-
tokenIsUnterminated = true;
1675+
tokenFlags |= TokenFlags.Unterminated;
16801676
error(Diagnostics.Unterminated_regular_expression_literal);
16811677
break;
16821678
}
@@ -1894,7 +1890,7 @@ namespace ts {
18941890
const saveTokenPos = tokenPos;
18951891
const saveToken = token;
18961892
const saveTokenValue = tokenValue;
1897-
const savePrecedingLineBreak = precedingLineBreak;
1893+
const saveTokenFlags = tokenFlags;
18981894
const result = callback();
18991895

19001896
// If our callback returned something 'falsy' or we're just looking ahead,
@@ -1905,7 +1901,7 @@ namespace ts {
19051901
tokenPos = saveTokenPos;
19061902
token = saveToken;
19071903
tokenValue = saveTokenValue;
1908-
precedingLineBreak = savePrecedingLineBreak;
1904+
tokenFlags = saveTokenFlags;
19091905
}
19101906
return result;
19111907
}
@@ -1916,10 +1912,8 @@ namespace ts {
19161912
const saveStartPos = startPos;
19171913
const saveTokenPos = tokenPos;
19181914
const saveToken = token;
1919-
const savePrecedingLineBreak = precedingLineBreak;
19201915
const saveTokenValue = tokenValue;
1921-
const saveHasExtendedUnicodeEscape = hasExtendedUnicodeEscape;
1922-
const saveTokenIsUnterminated = tokenIsUnterminated;
1916+
const saveTokenFlags = tokenFlags;
19231917

19241918
setText(text, start, length);
19251919
const result = callback();
@@ -1929,10 +1923,8 @@ namespace ts {
19291923
startPos = saveStartPos;
19301924
tokenPos = saveTokenPos;
19311925
token = saveToken;
1932-
precedingLineBreak = savePrecedingLineBreak;
19331926
tokenValue = saveTokenValue;
1934-
hasExtendedUnicodeEscape = saveHasExtendedUnicodeEscape;
1935-
tokenIsUnterminated = saveTokenIsUnterminated;
1927+
tokenFlags = saveTokenFlags;
19361928

19371929
return result;
19381930
}
@@ -1973,11 +1965,8 @@ namespace ts {
19731965
startPos = textPos;
19741966
tokenPos = textPos;
19751967
token = SyntaxKind.Unknown;
1976-
precedingLineBreak = false;
1977-
19781968
tokenValue = undefined;
1979-
hasExtendedUnicodeEscape = false;
1980-
tokenIsUnterminated = false;
1969+
tokenFlags = 0;
19811970
}
19821971
}
19831972
}

src/compiler/transformers/es2015.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3633,7 +3633,7 @@ namespace ts {
36333633
* @param node A string literal.
36343634
*/
36353635
function visitNumericLiteral(node: NumericLiteral) {
3636-
if (node.numericLiteralFlags & NumericLiteralFlags.BinaryOrOctalSpecifier) {
3636+
if (node.numericLiteralFlags & TokenFlags.BinaryOrOctalSpecifier) {
36373637
return setTextRange(createNumericLiteral(node.text), node);
36383638
}
36393639
return node;

src/compiler/types.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,20 +1451,25 @@ namespace ts {
14511451
}
14521452

14531453
/* @internal */
1454-
export const enum NumericLiteralFlags {
1454+
export const enum TokenFlags {
14551455
None = 0,
1456-
Scientific = 1 << 1, // e.g. `10e2`
1457-
Octal = 1 << 2, // e.g. `0777`
1458-
HexSpecifier = 1 << 3, // e.g. `0x00000000`
1459-
BinarySpecifier = 1 << 4, // e.g. `0b0110010000000000`
1460-
OctalSpecifier = 1 << 5, // e.g. `0o777`
1456+
PrecedingLineBreak = 1 << 0,
1457+
PrecedingComment = 1 << 1,
1458+
Unterminated = 1 << 2,
1459+
ExtendedUnicodeEscape = 1 << 3,
1460+
Scientific = 1 << 4, // e.g. `10e2`
1461+
Octal = 1 << 5, // e.g. `0777`
1462+
HexSpecifier = 1 << 6, // e.g. `0x00000000`
1463+
BinarySpecifier = 1 << 7, // e.g. `0b0110010000000000`
1464+
OctalSpecifier = 1 << 8, // e.g. `0o777`
14611465
BinaryOrOctalSpecifier = BinarySpecifier | OctalSpecifier,
1466+
NumericLiteralFlags = Scientific | Octal | HexSpecifier | BinarySpecifier | OctalSpecifier
14621467
}
14631468

14641469
export interface NumericLiteral extends LiteralExpression {
14651470
kind: SyntaxKind.NumericLiteral;
14661471
/* @internal */
1467-
numericLiteralFlags?: NumericLiteralFlags;
1472+
numericLiteralFlags?: TokenFlags;
14681473
}
14691474

14701475
export interface TemplateHead extends LiteralLikeNode {

0 commit comments

Comments
 (0)