diff --git a/lib/ast.js b/lib/ast.js index 358862b1..0940647c 100644 --- a/lib/ast.js +++ b/lib/ast.js @@ -702,6 +702,13 @@ var AST_Const = DEFNODE("Const", null, { $documentation: "A `const` statement" }, AST_Definitions); +var AST_Import = DEFNODE("Import", "module_name", { + $documentation: "An `import` statement", + $propdoc: { + module_name: "[AST_String] String literal describing where this module came from", + } +}); + var AST_VarDef = DEFNODE("VarDef", "name value", { $documentation: "A variable declaration; only appears in a AST_Definitions node", $propdoc: { diff --git a/lib/output.js b/lib/output.js index c0b86b96..4dc53798 100644 --- a/lib/output.js +++ b/lib/output.js @@ -1037,6 +1037,12 @@ function OutputStream(options) { DEFPRINT(AST_Const, function(self, output){ self._do_print(output, "const"); }); + DEFPRINT(AST_Import, function(self, output) { + output.print("import"); + output.space(); + self.module_name.print(output); + output.semicolon(); + }); function parenthesize_for_noin(node, output, noin) { if (!noin) node.print(output); diff --git a/lib/parse.js b/lib/parse.js index 8e608e8a..4a549fb1 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -44,9 +44,9 @@ "use strict"; -var KEYWORDS = 'break case catch class const continue debugger default delete do else extends finally for function if in instanceof new return switch throw try typeof var let void while with'; +var KEYWORDS = 'break case catch class const continue debugger default delete do else extends finally for function if in instanceof new return switch throw try typeof var let void while with import'; var KEYWORDS_ATOM = 'false null true'; -var RESERVED_WORDS = 'abstract boolean byte char double enum export final float goto implements import int interface long native package private protected public short static super synchronized this throws transient volatile yield' +var RESERVED_WORDS = 'abstract boolean byte char double enum export final float goto implements int interface long native package private protected public short static super synchronized this throws transient volatile yield' + " " + KEYWORDS_ATOM + " " + KEYWORDS; var KEYWORDS_BEFORE_EXPRESSION = 'return new delete throw else case'; @@ -909,6 +909,9 @@ function parse($TEXT, options) { body : statement() }); + case "import": + return tmp = import_(), semicolon(), tmp; + default: unexpected(); } @@ -1607,6 +1610,19 @@ function parse($TEXT, options) { } } + function import_() { + return new AST_Import({ + start: prev(), + module_name: new AST_String({ + start : S.token, + value : S.token.value, + quote : S.token.quote, + end : S.token, + }), + end: next(), + }); + } + function as_property_name() { var tmp = S.token; next(); diff --git a/test/compress/harmony.js b/test/compress/harmony.js index 622e00a0..4cb3921c 100644 --- a/test/compress/harmony.js +++ b/test/compress/harmony.js @@ -305,6 +305,14 @@ number_literals: { } } +import_statement: { + input: { + import "mod-name"; + import "module2"; + } + expect_exact: "import\"mod-name\";import\"module2\";" +} + // Fabio: My patches accidentally caused a crash whenever // there's an extraneous set of parens around an object. regression_cannot_destructure: {