google/traceur-compiler — pull request #785 by guybedford (2014-02-19 13:34:34+00:00)
Title: AMD local parsing

I mentioned the concept to the AMD implement list, and apparently the previous meta-argument suggestion may break some AMD implementations that read arguments in certain ways.

This current pull request does the handling inside the AMD module that would have had to be done by the AMD compatibility layer.

Basically, we need two metas - `__transpiledModule` means this module should be stored to the `default` property of the module object, and accessed from there equally.

We thus set a module with the `__transpiledModule` flag in the registry as:

```javascript
Module({
default: AMDOutput,
__defaultOnly: true
})
```

The `__defaultOnly` flag then tells us that when accessing that module from within an AMD module, we should only use the `default` property to refer to the module.

So effectively the two metas allow us to pop up and down between the two abstraction differences.

The output created by this transformer is thus:

```javascript
define(['a', 'b'], function($__0,$__1) {
"use strict";
var __moduleName = "test";
if ($__0 && $__0.__defaultOnly)$__0 = $__0['default'];
if ($__1 && $__1.__defaultOnly)$__1 = $__1['default'];
var a = ($__0).a;
var b = ($__1).b;
var p = 5;
return {
get p() {
return p;
},
__transpiledModule: true
};
});
```

The nice thing about this approach is it is a single output that caters both for backwards compatibility into AMD (in which the above doesn't do anything), and for forward compatibility of ES6 transpiled into AMD.

Currently the tests aren't passing because I can't manage to work out how to use the `parseStatement` to handle the following:

```javascript
return parseStatement `if (${local} && ${local}.__defaultOnly) ${local} = ${local}['default']`
```

The above returns quotes around the locals - `if ("$__0" && "$__0".__defaultOnly) "$__0" = "$__0"['default']`

Any tips appreciated, and let me know what you think of this as a transformation idea.