support import statements (#4646)

This commit is contained in:
Alex Lam S.L
2021-02-13 20:26:43 +00:00
committed by GitHub
parent a6bb66931b
commit b7219ac489
8 changed files with 312 additions and 10 deletions

View File

@@ -47,7 +47,7 @@
var KEYWORDS = "break case catch const continue debugger default delete do else finally for function if in instanceof let new return switch throw try typeof var void while with";
var KEYWORDS_ATOM = "false null true";
var RESERVED_WORDS = [
"await abstract boolean byte char class double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized this throws transient volatile yield",
"abstract async await boolean byte char class double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized this throws transient volatile yield",
KEYWORDS_ATOM,
KEYWORDS,
].join(" ");
@@ -844,6 +844,9 @@ function parse($TEXT, options) {
case "await":
if (S.in_async) return simple_statement();
break;
case "import":
next();
return import_();
case "yield":
if (S.in_generator) return simple_statement();
break;
@@ -1272,6 +1275,51 @@ function parse($TEXT, options) {
});
}
function import_() {
var all = null;
var def = as_symbol(AST_SymbolImport, true);
var props = null;
if (def ? (def.key = "", is("punc", ",") && next()) : !is("string")) {
if (is("operator", "*")) {
next();
expect_token("name", "as");
all = as_symbol(AST_SymbolImport);
all.key = "*";
} else {
expect("{");
props = [];
while (is("name") || is_identifier_string(S.token.value)) {
var alias;
if (is_token(peek(), "name", "as")) {
var key = S.token.value;
next();
next();
alias = as_symbol(AST_SymbolImport);
alias.key = key;
} else {
alias = as_symbol(AST_SymbolImport);
alias.key = alias.name;
}
props.push(alias);
if (!is("punc", "}")) expect(",");
}
expect("}");
}
}
if (all || def || props) expect_token("name", "from");
if (!is("string")) unexpected();
var path = S.token;
next();
semicolon();
return new AST_Import({
all: all,
default: def,
path: path.value,
properties: props,
quote: path.quote,
});
}
function block_() {
expect("{");
var a = [];