Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Port babel-parser changes from 2020-07-22 to 2020-11-25 #567

Merged
merged 2 commits into from
Dec 9, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 39 additions & 8 deletions src/parser/plugins/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
lookaheadTypeAndKeyword,
match,
next,
nextTemplateToken,
popTypeContext,
pushTypeContext,
} from "../tokenizer/index";
Expand Down Expand Up @@ -59,6 +60,17 @@ function tsIsIdentifier(): boolean {
return match(tt.name);
}

function isLiteralPropertyName(): boolean {
return (
match(tt.name) ||
Boolean(state.type & TokenType.IS_KEYWORD) ||
match(tt.string) ||
match(tt.num) ||
match(tt.bigint) ||
match(tt.decimal)
);
}

function tsNextTokenCanFollowModifier(): boolean {
// Note: TypeScript's implementation is much more complicated because
// more things are considered modifiers there.
Expand All @@ -68,13 +80,13 @@ function tsNextTokenCanFollowModifier(): boolean {

next();
const canFollowModifier =
!hasPrecedingLineBreak() &&
!match(tt.parenL) &&
!match(tt.parenR) &&
!match(tt.colon) &&
!match(tt.eq) &&
!match(tt.question) &&
!match(tt.bang);
(match(tt.bracketL) ||
match(tt.braceL) ||
match(tt.star) ||
match(tt.ellipsis) ||
match(tt.hash) ||
isLiteralPropertyName()) &&
!hasPrecedingLineBreak();

if (canFollowModifier) {
return true;
Expand Down Expand Up @@ -352,6 +364,9 @@ function tsParseMappedType(): void {
}
expect(tt.bracketL);
tsParseMappedTypeParameter();
if (eatContextual(ContextualKeyword._as)) {
tsParseType();
}
expect(tt.bracketR);
if (match(tt.plus) || match(tt.minus)) {
next();
Expand Down Expand Up @@ -396,6 +411,22 @@ function tsParseParenthesizedType(): void {
expect(tt.parenR);
}

function tsParseTemplateLiteralType(): void {
// Finish `, read quasi
nextTemplateToken();
// Finish quasi, read ${
nextTemplateToken();
while (!match(tt.backQuote) && !state.error) {
expect(tt.dollarBraceL);
tsParseType();
// Finish }, read quasi
nextTemplateToken();
// Finish quasi, read either ${ or `
nextTemplateToken();
}
next();
}

enum FunctionType {
TSFunctionType,
TSConstructorType,
Expand Down Expand Up @@ -456,7 +487,7 @@ function tsParseNonArrayType(): void {
tsParseParenthesizedType();
return;
case tt.backQuote:
parseTemplate();
tsParseTemplateLiteralType();
return;
default:
if (state.type & TokenType.IS_KEYWORD) {
Expand Down
10 changes: 5 additions & 5 deletions src/parser/tokenizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,11 +476,6 @@ function readToken_plus_min(code: number): void {

// '<>'
function readToken_lt_gt(code: number): void {
// Avoid right-shift for things like Array<Array<string>>.
if (code === charCodes.greaterThan && state.isType) {
finishOp(tt.greaterThan, 1);
return;
}
const nextChar = input.charCodeAt(state.pos + 1);

if (nextChar === code) {
Expand All @@ -492,6 +487,11 @@ function readToken_lt_gt(code: number): void {
finishOp(tt.assign, size + 1);
return;
}
// Avoid right-shift for things like Array<Array<string>>.
if (code === charCodes.greaterThan && state.isType) {
finishOp(tt.greaterThan, 1);
return;
}
finishOp(tt.bitShift, size);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/parser/traverser/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ export function parseExprAtom(): boolean {

case tt._do: {
next();
parseBlock(false);
parseBlock();
return false;
}

Expand Down Expand Up @@ -933,7 +933,7 @@ export function parseFunctionBody(allowExpression: boolean, funcContextId: numbe
if (isExpression) {
parseMaybeAssign();
} else {
parseBlock(true /* allowDirectives */, true /* isFunctionScope */, funcContextId);
parseBlock(true /* isFunctionScope */, funcContextId);
}
}

Expand Down
19 changes: 10 additions & 9 deletions src/parser/traverser/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,15 +481,8 @@ function parseIdentifierStatement(contextualKeyword: ContextualKeyword): void {
}
}

// Parse a semicolon-enclosed block of statements, handling `"use
// strict"` declarations when `allowStrict` is true (used for
// function bodies).

export function parseBlock(
allowDirectives: boolean = false,
isFunctionScope: boolean = false,
contextId: number = 0,
): void {
// Parse a semicolon-enclosed block of statements.
export function parseBlock(isFunctionScope: boolean = false, contextId: number = 0): void {
const startTokenIndex = state.tokens.length;
state.scopeDepth++;
expect(tt.braceL);
Expand Down Expand Up @@ -716,6 +709,14 @@ function parseClassMember(memberStart: number, classContextId: number): void {
// otherwise something static
state.tokens[state.tokens.length - 1].type = tt._static;
isStatic = true;

if (match(tt.braceL)) {
// This is a static block. Mark the word "static" with the class context ID for class element
// detection and parse as a regular block.
state.tokens[state.tokens.length - 1].contextId = classContextId;
parseBlock();
return;
}
}

parseClassMemberWithIsStatic(memberStart, isStatic, classContextId);
Expand Down
34 changes: 25 additions & 9 deletions src/util/getClassInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,24 @@ export default function getClassInfo(
}
tokens.nextToken();
}
if (isStatic && tokens.matches1(tt.braceL)) {
// This is a static block, so don't process it in any special way.
skipToNextClassElement(tokens, classContextId);
continue;
}
if (
tokens.matchesContextual(ContextualKeyword._constructor) &&
!tokens.currentToken().isType
) {
({constructorInitializerStatements, constructorInsertPos} = processConstructor(tokens));
continue;
}

const nameStartIndex = tokens.currentIndex();
skipFieldName(tokens);
if (tokens.matches1(tt.lessThan) || tokens.matches1(tt.parenL)) {
// This is a method, so just skip to the next method/field. To do that, we seek forward to
// the next start of a class name (either an open bracket or an identifier, or the closing
// curly brace), then seek backward to include any access modifiers.
while (tokens.currentToken().contextId !== classContextId) {
tokens.nextToken();
}
while (isAccessModifier(tokens.tokenAtRelativeIndex(-1))) {
tokens.previousToken();
}
// This is a method, so nothing to process.
skipToNextClassElement(tokens, classContextId);
continue;
}
// There might be a type annotation that we need to skip.
Expand Down Expand Up @@ -155,6 +154,23 @@ export default function getClassInfo(
};
}

/**
* Move the token processor to the next method/field in the class.
*
* To do that, we seek forward to the next start of a class name (either an open
* bracket or an identifier, or the closing curly brace), then seek backward to
* include any access modifiers.
*/
function skipToNextClassElement(tokens: TokenProcessor, classContextId: number): void {
tokens.nextToken();
while (tokens.currentToken().contextId !== classContextId) {
tokens.nextToken();
}
while (isAccessModifier(tokens.tokenAtRelativeIndex(-1))) {
tokens.previousToken();
}
}

function processClassHeader(tokens: TokenProcessor): ClassHeaderInfo {
const classToken = tokens.currentToken();
const contextId = classToken.contextId;
Expand Down
54 changes: 54 additions & 0 deletions test/sucrase-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1179,4 +1179,58 @@ describe("sucrase", () => {
{transforms: []},
);
});

it("parses and passes through class static blocks", () => {
assertResult(
`
class A {
static {
console.log("Initialized the class");
}
static foo() {
return 3;
}
}
`,
`
class A {
static {
console.log("Initialized the class");
}
static foo() {
return 3;
}
}
`,
{transforms: []},
);
});

it("correctly handles scope analysis within static blocks", () => {
assertResult(
`
import {x, y, z} from './foo';

class A {
static {
const x = 3;
console.log(x);
console.log(y);
}
}
`,
`
import { y,} from './foo';

class A {
static {
const x = 3;
console.log(x);
console.log(y);
}
}
`,
{transforms: ["typescript"]},
);
});
});