Skip to content

Commit

Permalink
Port babel-parser changes from 2021-03-15 to 2021-04-28 (#636)
Browse files Browse the repository at this point in the history
Instructions: https://github.com/alangpierce/sucrase/wiki/Porting-changes-from-Babel's-parser

4b674ea031 v7.13.11
🚫 Release only.

beb7cf8b24 Sort error keys with ESLint (#13020)
🚫 Babel-internal change.

edb11baabf v7.13.12
🚫 Release only.

4f727139ec fix(ts): Allow parenthesized "assert and assign" (#12933)
🚫 Seems to work fine in Sucrase.

0067fd9e02 Disallow await before exponential (#12441)
🚫 Validation only.

2f8203f190 v7.13.13
🚫 Release only.

8efbac4a5d fix: the LHS in for-of loop should not start with let (#13049)
🚫 Validation only.

a647b9ea6b Convert `@babel/core` to TypeScript (#12929)
🚫 Babel-internal.

7fe3ebf4db fix: raise `SyntaxError` for unparenthesized assert and assign (#13099)
🚫 Validation only.

42e630e8a2 Allow trailing comma after rest parameter in TSDeclareFunction (#13101)
🚫 Seems to work fine in Sucrase.

e50f6f7eef v7.13.15
🚫 Release only.

368bf893fa [ts] raise `SyntaxError` for `declare` before getter/setter (#13143)
🚫 Validation only.

d94a8e50ed v7.13.16
🚫 Release only.

10f4d08efb refactor: avoid parsing logic on locations (#13200)
🚫 Refactor not relevant to Sucrase.

0ee98139a6 Introduce parser error codes (#13033)
🚫 Error handling not relevant to Sucrase.

eac0259ce2 Support TS 4.3 static index signature in classes (#13096)
✅ Ported and updated parser to mark all modifiers as type tokens.

f8aa32f767 Support parsing Flow's Indexed Access Types (#13053)
✅ Straightforward port.

bf14a106ad Support TS 4.3 `override` syntax in class (#13097)
✅ Ported non-validation parts and implemented the feature in the transformer as well.

2521c666f7 Add internal ESLint rule for consistent parser error messages (#13130)
🚫 Error messaging only.

7484b51e56 Support TS 4.3 `get`/`set` type members (#13089)
✅ Ported, though the implementation needed some changes to avoid depending on the AST.

c949660b34 babel-parser: Deep freeze for `makeErrorTemplates` (#13142)
🚫 Babel internal.
  • Loading branch information
alangpierce authored Jul 7, 2021
1 parent 284bda6 commit 45d20de
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 132 deletions.
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

0 comments on commit 45d20de

Please sign in to comment.