JS: parse import assertions without storing in AST

This commit is contained in:
Asger F 2023-03-02 13:10:59 +01:00
Родитель 5fdc293d82
Коммит c715de2a10
4 изменённых файлов: 1081 добавлений и 4 удалений

Просмотреть файл

@ -314,6 +314,7 @@ public class ESNextParser extends JSXParser {
this.parseExportSpecifiersMaybe(specifiers, exports);
}
Literal source = (Literal) this.parseExportFrom(specifiers, null, true);
Expression assertion = this.parseImportOrExportAssertionAndSemicolon(); // TODO: store in AST
return this.finishNode(new ExportNamedDeclaration(exportStart, null, specifiers, source));
}
@ -330,6 +331,7 @@ public class ESNextParser extends JSXParser {
List<ExportSpecifier> specifiers = CollectionUtil.makeList(nsSpec);
this.parseExportSpecifiersMaybe(specifiers, exports);
Literal source = (Literal) this.parseExportFrom(specifiers, null, true);
Expression assertion = this.parseImportOrExportAssertionAndSemicolon(); // TODO: store in AST
return this.finishNode(new ExportNamedDeclaration(exportStart, null, specifiers, source));
}
@ -435,6 +437,10 @@ public class ESNextParser extends JSXParser {
*/
private DynamicImport parseDynamicImport(Position startLoc) {
Expression source = parseMaybeAssign(false, null, null);
Expression assertion = null;
if (this.eat(TokenType.comma)) {
assertion = this.parseMaybeAssign(false, null, null); // TODO: store in AST
}
this.expect(TokenType.parenR);
DynamicImport di = this.finishNode(new DynamicImport(new SourceLocation(startLoc), source));
return di;

Просмотреть файл

@ -2783,7 +2783,7 @@ public class Parser {
boolean isBreak = keyword.equals("break");
this.next();
Identifier label = null;
if (this.eat(TokenType.semi) || this.insertSemicolon()) {
if (this.eagerlyTrySemicolon()) {
label = null;
} else if (this.type != TokenType.name) {
this.unexpected();
@ -2893,6 +2893,15 @@ public class Parser {
new IfStatement(new SourceLocation(startLoc), test, consequent, alternate));
}
/**
* Consumes or inserts a semicolon if possible, and returns true if a semicolon was consumed or inserted.
*
* Returns false if there was no semicolon and insertion was not possible.
*/
protected boolean eagerlyTrySemicolon() {
return this.eat(TokenType.semi) || this.insertSemicolon();
}
protected ReturnStatement parseReturnStatement(Position startLoc) {
if (!this.inFunction && !this.options.allowReturnOutsideFunction())
this.raise(this.start, "'return' outside of function");
@ -2902,7 +2911,7 @@ public class Parser {
// optional arguments, we eagerly look for a semicolon or the
// possibility to insert one.
Expression argument;
if (this.eat(TokenType.semi) || this.insertSemicolon()) {
if (this.eagerlyTrySemicolon()) {
argument = null;
} else {
argument = this.parseExpression(false, null);
@ -3404,6 +3413,7 @@ public class Parser {
Statement declaration;
List<ExportSpecifier> specifiers;
Expression source = null;
Expression assertion = null;
if (this.shouldParseExportStatement()) {
declaration = this.parseStatement(true, false);
if (declaration == null) return null;
@ -3419,11 +3429,13 @@ public class Parser {
declaration = null;
specifiers = this.parseExportSpecifiers(exports);
source = parseExportFrom(specifiers, source, false);
assertion = parseImportOrExportAssertionAndSemicolon(); // TODO: store in AST
}
return this.finishNode(
new ExportNamedDeclaration(loc, declaration, specifiers, (Literal) source));
}
/** Parses the 'from' clause of an export, not including the assertion or semicolon. */
protected Expression parseExportFrom(
List<ExportSpecifier> specifiers, Expression source, boolean expectFrom) {
if (this.eatContextual("from")) {
@ -3442,13 +3454,13 @@ public class Parser {
source = null;
}
this.semicolon();
return source;
}
protected ExportDeclaration parseExportAll(
SourceLocation loc, Position starLoc, Set<String> exports) {
Expression source = parseExportFrom(null, null, true);
Expression assertion = parseImportOrExportAssertionAndSemicolon(); // TODO: store in AST
return this.finishNode(new ExportAllDeclaration(loc, (Literal) source));
}
@ -3514,6 +3526,16 @@ public class Parser {
return parseImportRest(loc);
}
protected Expression parseImportOrExportAssertionAndSemicolon() {
Expression result = null;
if (!this.eagerlyTrySemicolon()) {
this.expectContextual("assert");
result = this.parseObj(false, null);
this.semicolon();
}
return result;
}
protected ImportDeclaration parseImportRest(SourceLocation loc) {
List<ImportSpecifier> specifiers;
Literal source;
@ -3527,7 +3549,7 @@ public class Parser {
if (this.type != TokenType.string) this.unexpected();
source = (Literal) this.parseExprAtom(null);
}
this.semicolon();
Expression assertion = this.parseImportOrExportAssertionAndSemicolon(); // TODO: store in AST
if (specifiers == null) return null;
return this.finishNode(new ImportDeclaration(loc, specifiers, source));
}

Просмотреть файл

@ -943,10 +943,12 @@ public class FlowParser extends ESNextParser {
// `export type { foo, bar };`
List<ExportSpecifier> specifiers = this.parseExportSpecifiers(exports);
this.parseExportFrom(specifiers, null, false);
this.parseImportOrExportAssertionAndSemicolon(); // TODO: store in AST?
return null;
} else if (this.eat(TokenType.star)) {
if (this.eatContextual("as")) this.parseIdent(true);
this.parseExportFrom(null, null, true);
this.parseImportOrExportAssertionAndSemicolon(); // TODO: store in AST?
return null;
} else {
// `export type Foo = Bar;`

Разница между файлами не показана из-за своего большого размера Загрузить разницу