Importing names from places

This commit is contained in:
Fábio Santos
2016-02-21 17:06:09 +00:00
committed by Richard van Velzen
parent 0465bd270d
commit d35a9e7839
5 changed files with 56 additions and 9 deletions

View File

@@ -702,10 +702,19 @@ var AST_Const = DEFNODE("Const", null, {
$documentation: "A `const` statement"
}, AST_Definitions);
var AST_Import = DEFNODE("Import", "module_name", {
var AST_Import = DEFNODE("Import", "imported_name module_name", {
$documentation: "An `import` statement",
$propdoc: {
imported_name: "[AST_SymbolImport] The name of the variable holding the module's default export.",
module_name: "[AST_String] String literal describing where this module came from",
},
_walk: function(visitor) {
return visitor._visit(this, function() {
if (this.imported_name) {
this.imported_name._walk(visitor);
}
this.module_name._walk(visitor);
});
}
});
@@ -1072,6 +1081,10 @@ var AST_SymbolCatch = DEFNODE("SymbolCatch", null, {
$documentation: "Symbol naming the exception in catch",
}, AST_SymbolDeclaration);
var AST_SymbolImport = DEFNODE("SymbolImport", null, {
$documentation: "Symbol refering to an imported name",
}, AST_SymbolDeclaration);
var AST_Label = DEFNODE("Label", "references", {
$documentation: "Symbol naming a label (declaration)",
$propdoc: {

View File

@@ -1775,6 +1775,10 @@ merge(Compressor.prototype, {
return self;
});
OPT(AST_Import, function(self, compressor) {
return self;
});
OPT(AST_Function, function(self, compressor){
self = AST_Lambda.prototype.optimize.call(self, compressor);
if (compressor.option("unused") && !compressor.option("keep_fnames")) {

View File

@@ -1040,6 +1040,12 @@ function OutputStream(options) {
DEFPRINT(AST_Import, function(self, output) {
output.print("import");
output.space();
if (self.imported_name) {
self.imported_name.print(output);
output.space();
output.print("from")
output.space();
}
self.module_name.print(output);
output.semicolon();
});

View File

@@ -1611,15 +1611,27 @@ function parse($TEXT, options) {
}
function import_() {
var start = prev();
var imported_name;
if (is("name")) {
imported_name = as_symbol(AST_SymbolImport);
expect_token("name", "from");
}
var mod_str = S.token;
if (mod_str.type !== 'string') {
unexpected();
}
next();
return new AST_Import({
start: prev(),
start: start,
imported_name: imported_name,
module_name: new AST_String({
start : S.token,
value : S.token.value,
quote : S.token.quote,
end : S.token,
start: mod_str,
value: mod_str.value,
quote: mod_str.quote,
end: mod_str,
}),
end: next(),
end: S.token,
});
}

View File

@@ -308,9 +308,21 @@ number_literals: {
import_statement: {
input: {
import "mod-name";
import "module2";
import Foo from "bar";
}
expect_exact: "import\"mod-name\";import Foo from\"bar\";"
}
import_statement_mangling: {
mangle = { };
input: {
import Foo from "foo";
Foo();
}
expect: {
import a from "foo";
a();
}
expect_exact: "import\"mod-name\";import\"module2\";"
}
// Fabio: My patches accidentally caused a crash whenever