Modules: allowed combinations of module, export

David Herman dherman at mozilla.com
Thu May 19 16:08:21 PDT 2011


All exports of all declared/required modules are computed before execution starts. So it doesn't matter what order things run in, you won't get any "no such export" errors if you import a valid export.

Dave

On May 19, 2011, at 2:13 PM, James Burke wrote:

> 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
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss



More information about the es-discuss mailing list