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

@@ -198,13 +198,16 @@ var AST_Debugger = DEFNODE("Debugger", null, {
$documentation: "Represents a debugger statement",
}, AST_Statement);
var AST_Directive = DEFNODE("Directive", "value quote", {
var AST_Directive = DEFNODE("Directive", "quote value", {
$documentation: "Represents a directive, like \"use strict\";",
$propdoc: {
quote: "[string?] the original quote character",
value: "[string] The value of this directive as a plain string (it's not an AST_String!)",
quote: "[string] the original quote character"
},
_validate: function() {
if (this.quote != null) {
if (typeof this.quote != "string") throw new Error("quote must be string");
}
if (typeof this.value != "string") throw new Error("value must be string");
},
}, AST_Statement);
@@ -1035,6 +1038,44 @@ var AST_VarDef = DEFNODE("VarDef", "name value", {
/* -----[ OTHER ]----- */
var AST_Import = DEFNODE("Import", "all default path properties quote", {
$documentation: "An `import` statement",
$propdoc: {
all: "[AST_SymbolImport?] the imported namespace, or null if not specified",
default: "[AST_SymbolImport?] the alias for default `export`, or null if not specified",
path: "[string] the path to import module",
properties: "[(AST_SymbolImport*)?] array of aliases, or null if not specified",
quote: "[string?] the original quote character",
},
walk: function(visitor) {
var node = this;
visitor.visit(node, function() {
if (node.all) node.all.walk(visitor);
if (node.default) node.default.walk(visitor);
if (node.properties) node.properties.forEach(function(prop) {
prop.walk(visitor);
});
});
},
_validate: function() {
if (this.all != null) {
if (!(this.all instanceof AST_SymbolImport)) throw new Error("all must be AST_SymbolImport");
if (this.properties != null) throw new Error("cannot import both * and {} in the same statement");
}
if (this.default != null) {
if (!(this.default instanceof AST_SymbolImport)) throw new Error("default must be AST_SymbolImport");
if (this.default.key !== "") throw new Error("invalid default key: " + this.default.key);
}
if (typeof this.path != "string") throw new Error("path must be string");
if (this.properties != null) this.properties.forEach(function(node) {
if (!(node instanceof AST_SymbolImport)) throw new Error("properties must contain AST_SymbolImport");
});
if (this.quote != null) {
if (typeof this.quote != "string") throw new Error("quote must be string");
}
},
}, AST_Statement);
var AST_DefaultValue = DEFNODE("DefaultValue", "name value", {
$documentation: "A default value declaration",
$propdoc: {
@@ -1494,6 +1535,16 @@ var AST_SymbolFunarg = DEFNODE("SymbolFunarg", null, {
$documentation: "Symbol naming a function argument",
}, AST_SymbolVar);
var AST_SymbolImport = DEFNODE("SymbolImport", "key", {
$documentation: "Symbol defined by an `import` statement",
$propdoc: {
key: "[string] the original `export` name",
},
_validate: function() {
if (typeof this.key != "string") throw new Error("key must be string");
},
}, AST_SymbolVar);
var AST_SymbolDefun = DEFNODE("SymbolDefun", null, {
$documentation: "Symbol defining a function",
}, AST_SymbolDeclaration);
@@ -1567,13 +1618,16 @@ var AST_Constant = DEFNODE("Constant", null, {
},
});
var AST_String = DEFNODE("String", "value quote", {
var AST_String = DEFNODE("String", "quote value", {
$documentation: "A string literal",
$propdoc: {
quote: "[string?] the original quote character",
value: "[string] the contents of this string",
quote: "[string] the original quote character"
},
_validate: function() {
if (this.quote != null) {
if (typeof this.quote != "string") throw new Error("quote must be string");
}
if (typeof this.value != "string") throw new Error("value must be string");
},
}, AST_Constant);