Modules: allowed combinations of module, export

James Burke jrburke at gmail.com
Thu May 19 14:13:31 PDT 2011


Looking at harmony modules[1], I wanted to get some clarification on
the order of export, module and such in a module's body.

Looking at the Syntax section, this section:

ModuleElement(load) ::= Statement
                    | VariableDeclaration
                    | FunctionDeclaration
                    | ModuleDeclaration(load)
                    | ImportDeclaration(load)
                    | ExportDeclaration(load)

implies to me that the following is allowed:

//baz.js
module foo = require('foo.js');

//foo.js:
export var name = 'foo';
module bar = require('bar.js');
var barName = bar.name;

//bar.js:
export var name = 'bar';
module foo = require('foo.js');
//This works because foo.js did the export before the require?
var fooName = foo.name;

where as this would result in an error? (foo.js puts export below the
require() line):

//baz.js
module foo = require('foo.js');

//foo.js:
module bar = require('bar.js');
export var name = 'foo';
var barName = bar.name;

//bar.js:
export var name = 'bar';
module foo = require('foo.js');
var fooName = foo.name;

because I read the syntax section above as saying
"ModuleDeclaration(load) or ImportDelcaration(load) or
ExportDelcaration(load)", in other words any of those things can
occur, and they can occur in any order. Additionally, modules are
executed when the require() is reached.

However, the following would *not* work, since ModuleDeclaration(load)
can only show up as a direct child of a ProgramElement(load),
ExportDeclaration(load) or ModuleElement(load), and those items can
only have ProgramElement(load) or Program(load) as parents. so a
"module bar" statement could not appear as part of an if/else:

//foo.js
//This should error out as a syntax/compilation error
//(assume someCondition is valid here)
if (someCondition) {
   module bar = require('bar.js');
} else {
   module bar2 = require('bar2.js');
}

James

[1] http://wiki.ecmascript.org/doku.php?id=harmony:modules


More information about the es-discuss mailing list