importing names in the modules, not just default imports

This commit is contained in:
Fábio Santos
2016-02-26 21:12:19 +00:00
committed by Richard van Velzen
parent d35a9e7839
commit 59e1601fb8
4 changed files with 89 additions and 2 deletions

View File

@@ -702,10 +702,19 @@ var AST_Const = DEFNODE("Const", null, {
$documentation: "A `const` statement" $documentation: "A `const` statement"
}, AST_Definitions); }, AST_Definitions);
var AST_Import = DEFNODE("Import", "imported_name module_name", { var AST_NameImport = DEFNODE("NameImport", "foreign_name name", {
$documentation: "The part of the import statement that imports names from a module.",
$propdoc: {
foreign_name: "[AST_SymbolImportForeign] The name being imported (as specified in the module)",
name: "[AST_SymbolImport] The name as it becomes available to this module."
}
})
var AST_Import = DEFNODE("Import", "imported_name imported_names module_name", {
$documentation: "An `import` statement", $documentation: "An `import` statement",
$propdoc: { $propdoc: {
imported_name: "[AST_SymbolImport] The name of the variable holding the module's default export.", imported_name: "[AST_SymbolImport] The name of the variable holding the module's default export.",
imported_names: "[AST_NameImport*] The names of non-default imported variables",
module_name: "[AST_String] String literal describing where this module came from", module_name: "[AST_String] String literal describing where this module came from",
}, },
_walk: function(visitor) { _walk: function(visitor) {
@@ -1085,6 +1094,10 @@ var AST_SymbolImport = DEFNODE("SymbolImport", null, {
$documentation: "Symbol refering to an imported name", $documentation: "Symbol refering to an imported name",
}, AST_SymbolDeclaration); }, AST_SymbolDeclaration);
var AST_SymbolImportForeign = DEFNODE("SymbolImportForeign", null, {
$documentation: "A symbol imported from a module, but it is defined in the other module, and its real name is irrelevant for this module's purposes",
}, AST_Symbol);
var AST_Label = DEFNODE("Label", "references", { var AST_Label = DEFNODE("Label", "references", {
$documentation: "Symbol naming a label (declaration)", $documentation: "Symbol naming a label (declaration)",
$propdoc: { $propdoc: {

View File

@@ -1042,6 +1042,25 @@ function OutputStream(options) {
output.space(); output.space();
if (self.imported_name) { if (self.imported_name) {
self.imported_name.print(output); self.imported_name.print(output);
}
if (self.imported_name && self.imported_names) {
output.print(",");
output.space();
}
if (self.imported_names) {
output.print("{");
self.imported_names.forEach(function(name_import, i) {
output.space();
name_import.print(output);
if (i < self.imported_names.length - 1) {
output.print(",");
output.space();
}
});
output.space();
output.print("}");
}
if (self.imported_name || self.imported_names) {
output.space(); output.space();
output.print("from") output.print("from")
output.space(); output.space();
@@ -1050,6 +1069,18 @@ function OutputStream(options) {
output.semicolon(); output.semicolon();
}); });
DEFPRINT(AST_NameImport, function(self, output) {
if (self.foreign_name) {
self.foreign_name.print(output);
output.space();
output.print("as");
output.space();
self.name.print(output);
} else {
self.name.print(output);
}
});
function parenthesize_for_noin(node, output, noin) { function parenthesize_for_noin(node, output, noin) {
if (!noin) node.print(output); if (!noin) node.print(output);
else try { else try {

View File

@@ -1613,8 +1613,28 @@ function parse($TEXT, options) {
function import_() { function import_() {
var start = prev(); var start = prev();
var imported_name; var imported_name;
var imported_names;
if (is("name")) { if (is("name")) {
imported_name = as_symbol(AST_SymbolImport); imported_name = as_symbol(AST_SymbolImport);
}
if (is("punc", ",")) {
next();
}
if (is("punc", "{")) {
next();
imported_names = [];
while (!is("punc", "}")) {
imported_names.push(import_name());
if (is("punc", ",")) {
next();
}
}
next();
}
if (imported_names || imported_name) {
expect_token("name", "from"); expect_token("name", "from");
} }
var mod_str = S.token; var mod_str = S.token;
@@ -1625,6 +1645,7 @@ function parse($TEXT, options) {
return new AST_Import({ return new AST_Import({
start: start, start: start,
imported_name: imported_name, imported_name: imported_name,
imported_names: imported_names,
module_name: new AST_String({ module_name: new AST_String({
start: mod_str, start: mod_str,
value: mod_str.value, value: mod_str.value,
@@ -1635,6 +1656,25 @@ function parse($TEXT, options) {
}); });
} }
function import_name() {
var start = S.token;
var foreign_name;
var name;
if (peek().value === "as" && peek().type === "name") {
foreign_name = as_symbol(AST_SymbolImportForeign);
next(); // The "as" word
}
name = as_symbol(AST_SymbolImport);
return new AST_NameImport({
start: start,
foreign_name: foreign_name,
name: name,
end: prev(),
})
}
function as_property_name() { function as_property_name() {
var tmp = S.token; var tmp = S.token;
next(); next();

View File

@@ -309,8 +309,11 @@ import_statement: {
input: { input: {
import "mod-name"; import "mod-name";
import Foo from "bar"; import Foo from "bar";
import { Bar, Baz } from 'lel';
import Bar, { Foo } from 'lel';
import { Bar as kex, Baz as food } from 'lel';
} }
expect_exact: "import\"mod-name\";import Foo from\"bar\";" expect_exact: "import\"mod-name\";import Foo from\"bar\";import{Bar,Baz}from\"lel\";import Bar,{Foo}from\"lel\";import{Bar as kex,Baz as food}from\"lel\";"
} }
import_statement_mangling: { import_statement_mangling: {