Skip to content
New issue

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

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

Already on GitHub? # to your account

Port babel-parser changes from 2021-03-15 to 2021-04-28 #636

Merged
merged 2 commits into from
Jul 7, 2021
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
1 change: 1 addition & 0 deletions generator/generateReadWordTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const CONTEXTUAL_KEYWORDS = [
"opaque",
"private",
"protected",
"override",
"proto",
"public",
"readonly",
Expand Down
1 change: 1 addition & 0 deletions generator/generateTokenTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ const types = {
_public: new KeywordTokenType("public"),
_private: new KeywordTokenType("private"),
_protected: new KeywordTokenType("protected"),
_override: new KeywordTokenType("override"),
_as: new KeywordTokenType("as"),
_enum: new KeywordTokenType("enum"),
_type: new KeywordTokenType("type"),
Expand Down
8 changes: 7 additions & 1 deletion src/parser/plugins/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,13 @@ function flowParsePostfixType(): void {
flowParsePrimaryType();
while (!canInsertSemicolon() && match(tt.bracketL)) {
expect(tt.bracketL);
expect(tt.bracketR);
if (eat(tt.bracketR)) {
// Array type
} else {
// Indexed access type
flowParseType();
expect(tt.bracketR);
}
}
}

Expand Down
78 changes: 39 additions & 39 deletions src/parser/plugins/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ import {
baseParseMaybeDecoratorArguments,
parseBlockBody,
parseClass,
parseClassProperty,
parseClassPropertyName,
parseFunction,
parseFunctionParams,
parsePostMemberNameModifiers,
parseStatement,
parseVarStatement,
} from "../traverser/statement";
Expand Down Expand Up @@ -96,6 +93,15 @@ function tsNextTokenCanFollowModifier(): boolean {
}
}

export function tsParseModifiers(allowedModifiers: Array<ContextualKeyword>): void {
while (true) {
const modifier = tsParseModifier(allowedModifiers);
if (modifier === null) {
break;
}
}
}

/** Parses a modifier matching one the given modifier names. */
export function tsParseModifier(
allowedModifiers: Array<ContextualKeyword>,
Expand Down Expand Up @@ -125,6 +131,9 @@ export function tsParseModifier(
case ContextualKeyword._protected:
state.tokens[state.tokens.length - 1].type = tt._protected;
break;
case ContextualKeyword._override:
state.tokens[state.tokens.length - 1].type = tt._override;
break;
case ContextualKeyword._declare:
state.tokens[state.tokens.length - 1].type = tt._declare;
break;
Expand Down Expand Up @@ -307,6 +316,13 @@ function tsParseTypeMember(): void {
if (found) {
return;
}
if (
(isContextual(ContextualKeyword._get) || isContextual(ContextualKeyword._set)) &&
tsNextTokenCanFollowModifier()
) {
// This is a getter/setter on a type. The tsNextTokenCanFollowModifier
// function already called next() for us, so continue parsing the name.
}
parsePropertyName(-1 /* Types don't need context IDs. */);
tsParsePropertyOrMethodSignature(readonly);
}
Expand Down Expand Up @@ -1279,44 +1295,28 @@ export function tsParseAccessModifier(): void {
]);
}

export function tsTryParseClassMemberWithIsStatic(
isStatic: boolean,
classContextId: number,
): boolean {
let isAbstract = false;
let isReadonly = false;

while (true) {
const mod = tsParseModifier([
ContextualKeyword._abstract,
ContextualKeyword._readonly,
ContextualKeyword._declare,
]);
if (mod == null) {
break;
}
if (mod === ContextualKeyword._readonly) {
isReadonly = true;
}
if (mod === ContextualKeyword._abstract) {
isAbstract = true;
}
}
export function tsTryParseClassMemberWithIsStatic(isStatic: boolean): boolean {
const memberStartIndexAfterStatic = state.tokens.length;
tsParseModifiers([
ContextualKeyword._abstract,
ContextualKeyword._readonly,
ContextualKeyword._declare,
ContextualKeyword._static,
ContextualKeyword._override,
]);

// We no longer check for public/private/etc, but tsTryParseIndexSignature should just return
// false in that case for valid code.
if (!isAbstract && !isStatic) {
const found = tsTryParseIndexSignature();
if (found) {
return true;
const modifiersEndIndex = state.tokens.length;
const found = tsTryParseIndexSignature();
if (found) {
// Index signatures are type declarations, so set the modifier tokens as
// type tokens. Most tokens could be assumed to be type tokens, but `static`
// is ambiguous unless we set it explicitly here.
const memberStartIndex = isStatic
? memberStartIndexAfterStatic - 1
: memberStartIndexAfterStatic;
for (let i = memberStartIndex; i < modifiersEndIndex; i++) {
state.tokens[i].isType = true;
}
}

if (isReadonly) {
// Must be a property (if not an index signature).
parseClassPropertyName(classContextId);
parsePostMemberNameModifiers();
parseClassProperty();
return true;
}
return false;
Expand Down
1 change: 1 addition & 0 deletions src/parser/tokenizer/keywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export enum ContextualKeyword {
_namespace,
_of,
_opaque,
_override,
_private,
_protected,
_proto,
Expand Down
Loading