Compare commits
374 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
010dbe1cce | ||
|
|
75cd0bab02 | ||
|
|
3283991ed0 | ||
|
|
a66ce5b6ce | ||
|
|
9620f50f22 | ||
|
|
cc049ea851 | ||
|
|
e5a04ae117 | ||
|
|
e07e74f010 | ||
|
|
7b04c501eb | ||
|
|
70349dd4b3 | ||
|
|
2cedc843a8 | ||
|
|
cf40fe3430 | ||
|
|
09ffeed139 | ||
|
|
18f79d6253 | ||
|
|
6f5270f38f | ||
|
|
aac7aac373 | ||
|
|
9361470dc7 | ||
|
|
cf579f4b6e | ||
|
|
2e497eef98 | ||
|
|
7867daaa04 | ||
|
|
eabbcd31ad | ||
|
|
17d198f3d6 | ||
|
|
afd5376c8d | ||
|
|
02a50c9f7e | ||
|
|
df9fa32b82 | ||
|
|
831b420df3 | ||
|
|
123186003c | ||
|
|
ea627ed57c | ||
|
|
157bb48776 | ||
|
|
76a3771943 | ||
|
|
54f8889a30 | ||
|
|
bab159bc08 | ||
|
|
23950963e5 | ||
|
|
d1daac5550 | ||
|
|
d93ece5ccf | ||
|
|
c8c667bf16 | ||
|
|
49cdacbcb4 | ||
|
|
f97bd584f7 | ||
|
|
f1328aa82f | ||
|
|
e298a5d350 | ||
|
|
89a6ef0b26 | ||
|
|
c7ac9b7815 | ||
|
|
55c99b9122 | ||
|
|
545c8c3adb | ||
|
|
d093dc8231 | ||
|
|
eaf9d58b8d | ||
|
|
6131cea5b2 | ||
|
|
ca4831febe | ||
|
|
9cf8d05a19 | ||
|
|
2a4f03330b | ||
|
|
83bc614bad | ||
|
|
99ccb4111a | ||
|
|
f6c72e9ae9 | ||
|
|
a90730d4bf | ||
|
|
29503d2d37 | ||
|
|
35451ed408 | ||
|
|
7167aeb65a | ||
|
|
29d0489443 | ||
|
|
82aa2686f9 | ||
|
|
24616fa08a | ||
|
|
9f3e1eabfd | ||
|
|
286d2f84bd | ||
|
|
7fc484d82e | ||
|
|
ad6ab57fe3 | ||
|
|
348fe045d3 | ||
|
|
980b20316b | ||
|
|
5b52e507ca | ||
|
|
9853c9d78d | ||
|
|
72ec771725 | ||
|
|
75e97d9f7e | ||
|
|
295faf185c | ||
|
|
54b05f33f2 | ||
|
|
1985146760 | ||
|
|
13a52dd300 | ||
|
|
9d4bea70e6 | ||
|
|
421906bcc1 | ||
|
|
508b3531e5 | ||
|
|
c67c407cd0 | ||
|
|
aa552fea0d | ||
|
|
9bbf109dc8 | ||
|
|
393d52088a | ||
|
|
5783973734 | ||
|
|
55e01afd0d | ||
|
|
1231dc6cef | ||
|
|
b08f2061b5 | ||
|
|
1536d3de91 | ||
|
|
fddacb1612 | ||
|
|
9d1ff2b71b | ||
|
|
1c8d24f78c | ||
|
|
b619e843a9 | ||
|
|
e6a71c5849 | ||
|
|
682668c219 | ||
|
|
dd64297838 | ||
|
|
944a9d3908 | ||
|
|
d6922c9b75 | ||
|
|
fb0fcc7138 | ||
|
|
89148e6029 | ||
|
|
28d10b8eb4 | ||
|
|
3509990563 | ||
|
|
c5960fb9f7 | ||
|
|
07131576cf | ||
|
|
e75e778300 | ||
|
|
f6cb14c975 | ||
|
|
af1912ab7a | ||
|
|
8c478f29bc | ||
|
|
5b4d6d7ba9 | ||
|
|
1776b072b0 | ||
|
|
726fad1b6c | ||
|
|
23c16b0094 | ||
|
|
24f70ee4d0 | ||
|
|
fe9b0afd95 | ||
|
|
47695a8710 | ||
|
|
e1c7584280 | ||
|
|
3e63e6cd4b | ||
|
|
ba643680e5 | ||
|
|
611137c7d7 | ||
|
|
6240e5111f | ||
|
|
324a4a1b22 | ||
|
|
035f667a8f | ||
|
|
98985dd253 | ||
|
|
0a207903ce | ||
|
|
90437d262b | ||
|
|
4aba7ec192 | ||
|
|
a924c9c218 | ||
|
|
a2ed0ea9c5 | ||
|
|
c5cd729c3d | ||
|
|
b065d43a6d | ||
|
|
281003c7bd | ||
|
|
6650336c64 | ||
|
|
f904734695 | ||
|
|
0528560d81 | ||
|
|
4362ba93de | ||
|
|
ca12e87370 | ||
|
|
ebaa735adc | ||
|
|
62e406a6fe | ||
|
|
0d45a8975c | ||
|
|
8f64fe2332 | ||
|
|
a8a7587c1f | ||
|
|
7736fa11f2 | ||
|
|
d203924541 | ||
|
|
cd23e500a1 | ||
|
|
bf24a0d6b5 | ||
|
|
279d1affea | ||
|
|
2a1e012240 | ||
|
|
a307a961a6 | ||
|
|
fe5b1dc542 | ||
|
|
f057347ae9 | ||
|
|
98f2287267 | ||
|
|
e60e0e0b0e | ||
|
|
81fb8647b6 | ||
|
|
e8387c7de5 | ||
|
|
655196f3ce | ||
|
|
eef371c3df | ||
|
|
a3d21f02c6 | ||
|
|
ad4386c0ed | ||
|
|
3c82a61af1 | ||
|
|
3c154f9f6d | ||
|
|
22c972e85b | ||
|
|
6d7e14fb2b | ||
|
|
9287449372 | ||
|
|
44a35400f5 | ||
|
|
2305e2447f | ||
|
|
c420d1ffc3 | ||
|
|
fa42b076a7 | ||
|
|
0d23c1cfdb | ||
|
|
0dafdd6969 | ||
|
|
a9af8d39a2 | ||
|
|
1cc606d4d0 | ||
|
|
2a09c0a5a5 | ||
|
|
92359520cd | ||
|
|
50e5bd4680 | ||
|
|
64ef26a937 | ||
|
|
bfd6ffe471 | ||
|
|
8c1760bea2 | ||
|
|
bdb8f87480 | ||
|
|
7f6bc54920 | ||
|
|
b6af2f826d | ||
|
|
7455b6d9b9 | ||
|
|
0d2e876178 | ||
|
|
57ce1b201d | ||
|
|
5f56da457c | ||
|
|
ed988dc2bb | ||
|
|
2c471968ea | ||
|
|
fd1a91bf9c | ||
|
|
9e7de5ad2e | ||
|
|
06927288ba | ||
|
|
9342aee6bd | ||
|
|
4f5669f53c | ||
|
|
262fd995da | ||
|
|
1696f77980 | ||
|
|
dfa51954b8 | ||
|
|
216b8c2fd1 | ||
|
|
ca2cbb02f1 | ||
|
|
b72427cdd0 | ||
|
|
44e9812ad4 | ||
|
|
b7814e5e24 | ||
|
|
ec67a83326 | ||
|
|
6a560cea25 | ||
|
|
46f9ed7d74 | ||
|
|
92366dfc7b | ||
|
|
f520a46175 | ||
|
|
88bec1196b | ||
|
|
ff0098f3f3 | ||
|
|
d01d7722f4 | ||
|
|
2845c1a894 | ||
|
|
ed51728aba | ||
|
|
4c8f4121f0 | ||
|
|
b9e4340700 | ||
|
|
4c9e39afa1 | ||
|
|
7439247095 | ||
|
|
254424ced5 | ||
|
|
4fc6823ece | ||
|
|
d531c8aa4c | ||
|
|
fc59f1ff46 | ||
|
|
7c7cd6f9b8 | ||
|
|
e3174d5a39 | ||
|
|
2558a5e12e | ||
|
|
7526ae5486 | ||
|
|
811fcf7e1b | ||
|
|
62818d468e | ||
|
|
a88bd69e30 | ||
|
|
8164bb17ce | ||
|
|
3ec97fc86d | ||
|
|
b38a00d70e | ||
|
|
1b85607d7d | ||
|
|
fd6704ac25 | ||
|
|
9c527f9d9c | ||
|
|
6cf318d60f | ||
|
|
64f9f4dff5 | ||
|
|
abc90778e8 | ||
|
|
56f1683f06 | ||
|
|
520dd7e947 | ||
|
|
145413dbfd | ||
|
|
8c58e0f14c | ||
|
|
d62914e41c | ||
|
|
85db67edf4 | ||
|
|
348ad2e25c | ||
|
|
5ce8ee6c2b | ||
|
|
98ad5d42e1 | ||
|
|
b12734abd6 | ||
|
|
f34907d518 | ||
|
|
3bd6ec20c1 | ||
|
|
b8d4479333 | ||
|
|
ed833be34a | ||
|
|
16024242b0 | ||
|
|
77add8704e | ||
|
|
9fd1ec0596 | ||
|
|
90ce4e93ed | ||
|
|
cf7b9234c1 | ||
|
|
f85eeeb93c | ||
|
|
45d0eea842 | ||
|
|
b0c9d3daa4 | ||
|
|
3f152bfc3f | ||
|
|
ffbfa5281b | ||
|
|
7f87ca0d43 | ||
|
|
a8fad4b958 | ||
|
|
945ea145e7 | ||
|
|
4f10bcedda | ||
|
|
601b3d584f | ||
|
|
42878f85fe | ||
|
|
3b4c9c415e | ||
|
|
7345899343 | ||
|
|
316102ab14 | ||
|
|
4c2b85deec | ||
|
|
108784db20 | ||
|
|
a3b30bae7a | ||
|
|
6420954c53 | ||
|
|
a6fda01fa0 | ||
|
|
655cc8fcb9 | ||
|
|
97a504bbef | ||
|
|
bf3443962b | ||
|
|
a4394494ef | ||
|
|
2b333de81f | ||
|
|
b81f824b7e | ||
|
|
9bfb8c440a | ||
|
|
9a6c13a6a4 | ||
|
|
17ce21509d | ||
|
|
a4c8895dc3 | ||
|
|
af4009694a | ||
|
|
941a8cc5ea | ||
|
|
359c4e8786 | ||
|
|
7ac98c1532 | ||
|
|
61c3e0a3de | ||
|
|
ac8d77fb45 | ||
|
|
09c88238e1 | ||
|
|
103b619364 | ||
|
|
c8a404fb72 | ||
|
|
05669204dd | ||
|
|
38e6abec02 | ||
|
|
1abef2c1e2 | ||
|
|
cc6678361a | ||
|
|
2587694d44 | ||
|
|
f16079c83f | ||
|
|
b7132f3529 | ||
|
|
ed41f1652e | ||
|
|
c522bdb4b9 | ||
|
|
005362a615 | ||
|
|
88e542634f | ||
|
|
c8304138eb | ||
|
|
35608d497e | ||
|
|
b5a78355c4 | ||
|
|
223e28ba8f | ||
|
|
6a35bdb42b | ||
|
|
18813f26bb | ||
|
|
ccabb91f69 | ||
|
|
35fda899a8 | ||
|
|
e2f01d41a3 | ||
|
|
931a80f5b3 | ||
|
|
9a2fd22e91 | ||
|
|
cbcad22d81 | ||
|
|
4d5861cfdc | ||
|
|
c578db7ae8 | ||
|
|
dea73b7186 | ||
|
|
4506e39cf5 | ||
|
|
7ab6df093b | ||
|
|
22eeae1a93 | ||
|
|
a645ae0583 | ||
|
|
7f4efecb7d | ||
|
|
0c0f40d14a | ||
|
|
cb54c11d84 | ||
|
|
55123be7d7 | ||
|
|
324a0b408a | ||
|
|
8b2788e930 | ||
|
|
d945379b5b | ||
|
|
5a622ac9c7 | ||
|
|
0e9eac610b | ||
|
|
1de94a2705 | ||
|
|
71132c3538 | ||
|
|
d20ab0eb05 | ||
|
|
51f7e75d2e | ||
|
|
ab4b5ce994 | ||
|
|
f0d50ca9a2 | ||
|
|
5168355a64 | ||
|
|
2f209a9e4e | ||
|
|
20695eaba6 | ||
|
|
340a4dd1f2 | ||
|
|
ef1c7a5c69 | ||
|
|
5a01beaa1f | ||
|
|
5a5bf7b4ac | ||
|
|
88ee15bb4d | ||
|
|
3e51b65095 | ||
|
|
371df9ad09 | ||
|
|
d43d5ff409 | ||
|
|
c2c8e52430 | ||
|
|
57c0ebc5f4 | ||
|
|
339bf82481 | ||
|
|
d981c06444 | ||
|
|
8269e1488b | ||
|
|
ddda7a020f | ||
|
|
7e9340864a | ||
|
|
5ec19f23c3 | ||
|
|
ba48b66458 | ||
|
|
6e8c73f65f | ||
|
|
b34fcb0cd9 | ||
|
|
ff7129f1aa | ||
|
|
fbabd193a4 | ||
|
|
876d69798c | ||
|
|
1a963ddc06 | ||
|
|
beb5acea6b | ||
|
|
6a290f233e | ||
|
|
74563d88ed | ||
|
|
a18177026c | ||
|
|
a813341433 | ||
|
|
4968de9803 | ||
|
|
0de2d33e8d | ||
|
|
6f81ddd0d5 | ||
|
|
6b83055b12 | ||
|
|
080c23d5ed | ||
|
|
293bec58d7 | ||
|
|
93ceafc732 | ||
|
|
b2bac04b72 | ||
|
|
c7a02ca289 | ||
|
|
f4055612e5 | ||
|
|
46911bd28c |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -10,3 +10,4 @@ test/tmp
|
||||
/runtime.js
|
||||
coverage
|
||||
dist
|
||||
.package.json
|
||||
|
||||
9
.gitmodules
vendored
Normal file
9
.gitmodules
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
[submodule "vendor/traceur"]
|
||||
path = vendor/traceur
|
||||
url = https://github.com/google/traceur-compiler
|
||||
[submodule "vendor/regenerator"]
|
||||
path = vendor/regenerator
|
||||
url = https://github.com/facebook/regenerator
|
||||
[submodule "vendor/test262"]
|
||||
path = vendor/test262
|
||||
url = https://github.com/tc39/test262
|
||||
@@ -9,3 +9,4 @@ Makefile
|
||||
dist
|
||||
tests.json
|
||||
CHANGELOG.md
|
||||
.package.json
|
||||
|
||||
214
CHANGELOG.md
214
CHANGELOG.md
@@ -1,6 +1,218 @@
|
||||
# Changelog
|
||||
|
||||
Gaps between patch versions are faulty/broken releases.
|
||||
> **Tags:**
|
||||
> - [New Feature]
|
||||
> - [Bug Fix]
|
||||
> - [Spec Compliancy]
|
||||
> - [Breaking Change]
|
||||
> - [Documentation]
|
||||
> - [Internal]
|
||||
> - [Polish]
|
||||
|
||||
_Note: Gaps between patch versions are faulty/broken releases._
|
||||
|
||||
## 2.11.2
|
||||
|
||||
* **Bug Fix**
|
||||
* Support esprima-style catch clause handlers.
|
||||
* **Polish**
|
||||
* Don't print a stacktrace for syntax errors in the CLI.
|
||||
|
||||
## 2.11.1
|
||||
|
||||
* **Bug Fix**
|
||||
* Remove stray `console.log` outputting debug code.
|
||||
* Remove `Dict` from `coreAliasing`.
|
||||
|
||||
## 2.11.0
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix unnecessary IIFE in default parameters on method calls.
|
||||
* Add support for supers inside of closures.
|
||||
* **New Feature**
|
||||
* Add `--keep-module-id-extensions`/`keepModuleIdExtensions` option to keep extensions in module ids.
|
||||
* **Polish**
|
||||
* Special case single argument spread properties in `super` inside classes.
|
||||
* Don't use a variable declaration for class declaration IFFE.
|
||||
* Rename `inherits` helper parameters.
|
||||
* `coreAliasing` transformer now aliases `Promise`, `Set`, `Map` and more. Thanks [@AluisioASG](https://github.com/AluisioASG).
|
||||
|
||||
## 2.10.1
|
||||
|
||||
* **Internal**
|
||||
* Upgrade `core-js` to `0.4.4`.
|
||||
* **New Feature**
|
||||
* Added `--include-regenerator` option to CLI and `includeRegenerator` option to API that includes the regenerator runtime if necessary.
|
||||
|
||||
## 2.10.0
|
||||
|
||||
* **New Feature**
|
||||
* Add `classesFastSuper` optional transformer that doesn't support parent getters and prototype changing.
|
||||
* Add `forOfFast` transformer that speeds up `for...of` on arrays but produces more code.
|
||||
* Add `--react-compat` to `bin/6to5`.
|
||||
* **Spec Compliancy**
|
||||
* Disallow setters without a single parameter.
|
||||
* Add `configurable` and `writable` to defined properties.
|
||||
* Use define instead of assignment for class methods.
|
||||
* **Polish**
|
||||
* Fix bin name in `6to5-node --help`.
|
||||
* Fix shorthand `--whitelist` name in `bin/6to5`.
|
||||
* **Internal**
|
||||
* Hot code optimisation of traversal etc thanks to [gaearon](https://github.com/gaearon).
|
||||
|
||||
## 2.9.4
|
||||
|
||||
* **Bug Fix**
|
||||
* Support `UpdateExpression`s as `MemberExpression` objects.
|
||||
|
||||
## 2.9.3
|
||||
|
||||
* **Bug Fix**
|
||||
* Remove `exportsWildcard` helper in favor of `defaults`.
|
||||
|
||||
## 2.9.2
|
||||
|
||||
* **Bug Fix**
|
||||
* Pass `exports` to `exportWildcard` helper to allow for use inside the optional runtime.
|
||||
|
||||
## 2.9.1
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix runtime generator breaking the helper inclusion loop.
|
||||
|
||||
## 2.9.0
|
||||
|
||||
* **Internal**
|
||||
* Upgrade `acorn-6to5`.
|
||||
* Now supports destructuring shorthand properties.
|
||||
|
||||
## 2.8.1
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix computed accessors on object literals.
|
||||
|
||||
## 2.8.0
|
||||
|
||||
* **New Feature**
|
||||
* Add `commonStrict`, `amdStrict` and `umdStrict` module formatters that remove `module.exports` interop assignment.
|
||||
* Add `--indent` option to the 6to5 CLI.
|
||||
|
||||
## 2.7.4
|
||||
|
||||
* **Polish**
|
||||
* Inherit assignments from their declaration in destructuring.
|
||||
* Properly align multi-declarator variable declarations.
|
||||
|
||||
## 2.7.3
|
||||
|
||||
* **Polish**
|
||||
* Indent and add newlines to `React.createElement` calls in `react` transformer.
|
||||
* Remove `Object.assign` calls and replace it with an `extends` helper.
|
||||
|
||||
## 2.7.1
|
||||
|
||||
* **New Feature**
|
||||
* Expose `version` on browser and node API.
|
||||
* **Internal**
|
||||
* Upgrade `core-js` to 0.4.1
|
||||
|
||||
## 2.7.0
|
||||
|
||||
* **Spec Compliancy**
|
||||
* Disallow reassignments of imports.
|
||||
* **New Feature**
|
||||
* `reactCompat` option to enable pre-v0.12 react components.
|
||||
|
||||
## 2.6.3
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix 2.6.0 regression caused by faulty export default from a source handling.
|
||||
|
||||
## 2.6.2
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix rest parameter keys when on functions with params.
|
||||
|
||||
## 2.6.1
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix rest parameter arguments key.
|
||||
|
||||
## 2.6.0
|
||||
|
||||
* **Bug Fix**
|
||||
* Better handling of number literal property keys.
|
||||
* Handle `NewExpression` paren insertion edegcases better.
|
||||
* **Internal**
|
||||
* Fix incorrect AST node `identifier("this")`.
|
||||
* Better `toIdentifier` method that handles reserved words.
|
||||
* Upgrade `acorn-6to5`.
|
||||
* Fix exponentiation operator precedence.
|
||||
* Fix line terminators after async contextual keywords.
|
||||
* Add semicolons as class elements inside of a class body.
|
||||
* Upgrade to `core-js` 4.0.0.
|
||||
* Upgrade to `regenerator` 0.8.3.
|
||||
* Fix non-loop labeled statements.
|
||||
* **New Feature**
|
||||
* Basic destructuring defaults
|
||||
* Add `.es` to list of supported extensions.
|
||||
* Add optional `typeofSymbol` transformer.
|
||||
* Use a `for` loop for rest parameters instead of `Array.prototype.slice`.
|
||||
* **Polish**
|
||||
* Move `"use strict";` to inside module bodies instead of at the top of the file.
|
||||
* Better handling of dynamic imports.
|
||||
* **Spec Compliancy**
|
||||
* Class inheritance now has a `function` or `null` type check.
|
||||
* Add `interopRequireWildcard` around wildcard imports and exports to handle non-object exports.
|
||||
|
||||
## 2.5.0
|
||||
|
||||
* Remove `noDuplicateProperties` transformer.
|
||||
* Better generated UIDs based on nodes.
|
||||
* Default parameters now use `arguments[i]`, conditionals and variable declarations instead of using long-form if statements.
|
||||
|
||||
## 2.4.10
|
||||
|
||||
* Upgrade `acorn-6to5`.
|
||||
|
||||
## 2.4.9
|
||||
|
||||
* Upgrade `acorn-6to5`.
|
||||
* Add optional `protoToAssign` transformer.
|
||||
* Fix missing properties from computed property keys.
|
||||
* Make ES7 comprehensions `let` variables.
|
||||
|
||||
## 2.4.8
|
||||
|
||||
* Make `require("6to5/register")` work with browserify - [#370](https://github.com/6to5/6to5/pull/370). Thanks [@hughsk](https://github.com/hughsk)!
|
||||
|
||||
## 2.4.7
|
||||
|
||||
* Upgrade `acorn-6to5`.
|
||||
|
||||
## 2.4.6
|
||||
|
||||
* Move `coreAliasing` and `undefinedToVoid` transformers down to catch `moduleFormatter` transforms.
|
||||
|
||||
## 2.4.5
|
||||
|
||||
* Avoid printing comments if they've already been output.
|
||||
|
||||
## 2.4.4
|
||||
|
||||
* Add `module` type to browser build `<script>` handler.
|
||||
* Fix some `MemberExpression` modifying incorrectly setting `property` to a `MemberExpression`.
|
||||
|
||||
## 2.4.3
|
||||
|
||||
* Upgrade `acorn-6to5`.
|
||||
* Add support for `FunctionDeclaration`s in `bluebirdCoroutines` and `asyncToGenerators` transformers.
|
||||
|
||||
## 2.4.2
|
||||
|
||||
* Upgrade `acorn-6to5`.
|
||||
* Better uids generated for various transformers based on parent node.
|
||||
* Alias flat references in `coreAliasing` transformer.
|
||||
|
||||
## 2.4.1
|
||||
|
||||
|
||||
13
CODE_OF_CONDUCT.md
Normal file
13
CODE_OF_CONDUCT.md
Normal file
@@ -0,0 +1,13 @@
|
||||
# Contributor Code of Conduct
|
||||
|
||||
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
||||
|
||||
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
||||
|
||||
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
||||
|
||||
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
||||
@@ -1,5 +1,7 @@
|
||||
# Contributing
|
||||
|
||||
Before contributing, please read the [code of conduct](https://github.com/6to5/6to5/blob/master/CODE_OF_CONDUCT.md).
|
||||
|
||||
* **General**
|
||||
* No ES6 syntax features or methods, exclusively ES5.
|
||||
* Max of five arguments for functions
|
||||
|
||||
2
LICENSE
2
LICENSE
@@ -1,4 +1,4 @@
|
||||
Copyright (c) 2014 Sebastian McKenzie
|
||||
Copyright (c) 2014-2015 Sebastian McKenzie
|
||||
|
||||
MIT License
|
||||
|
||||
|
||||
102
Makefile
102
Makefile
@@ -6,44 +6,7 @@ MOCHA_CMD = node_modules/mocha/bin/_mocha
|
||||
|
||||
export NODE_ENV = test
|
||||
|
||||
.PHONY: clean test test-cov test-clean lint test-travis test-spec test-browser publish bench build
|
||||
|
||||
clean:
|
||||
rm -rf coverage templates.json test/tmp dist
|
||||
|
||||
bench:
|
||||
npm install es6-transpiler traceur esnext es6now jstransform
|
||||
node node_modules/matcha/bin/_matcha
|
||||
|
||||
lint:
|
||||
$(JSHINT_CMD) --reporter node_modules/jshint-stylish/stylish.js lib bin benchmark/index.js
|
||||
|
||||
test-clean:
|
||||
rm -rf test/tmp
|
||||
|
||||
test: lint
|
||||
$(MOCHA_CMD)
|
||||
make test-clean
|
||||
|
||||
test-cov:
|
||||
rm -rf coverage
|
||||
node $(ISTANBUL_CMD) $(MOCHA_CMD) --
|
||||
|
||||
test-spec:
|
||||
node $(ISTANBUL_CMD) $(MOCHA_CMD) -- --reporter spec
|
||||
|
||||
test-travis: test-spec
|
||||
if test -n "$$CODECLIMATE_REPO_TOKEN"; then codeclimate < coverage/lcov.info; fi
|
||||
|
||||
test-browser:
|
||||
mkdir -p dist
|
||||
|
||||
node bin/cache-templates
|
||||
node bin/cache-tests
|
||||
node $(BROWSERIFY_CMD) -e test/_browser.js >dist/6to5-test.js
|
||||
rm -rf templates.json tests.json
|
||||
|
||||
test -n "`which open`" && open test/browser.html
|
||||
.PHONY: clean test test-cov test-clean lint test-travis test-simple test-all test-browser publish build bootstrap publish-core
|
||||
|
||||
build:
|
||||
mkdir -p dist
|
||||
@@ -61,11 +24,56 @@ build:
|
||||
|
||||
rm -rf templates.json
|
||||
|
||||
clean:
|
||||
rm -rf coverage templates.json test/tmp dist
|
||||
|
||||
lint:
|
||||
$(JSHINT_CMD) --reporter node_modules/jshint-stylish/stylish.js lib bin
|
||||
|
||||
test-clean:
|
||||
rm -rf test/tmp
|
||||
|
||||
test: lint
|
||||
$(MOCHA_CMD)
|
||||
make test-clean
|
||||
|
||||
test-simple:
|
||||
# excludes test262
|
||||
export SIMPLE_6TO5_TESTS=1
|
||||
make test
|
||||
|
||||
test-all:
|
||||
# includes traceur, esnext, regenerator
|
||||
export ALL_6TO5_TESTS=1
|
||||
make test
|
||||
|
||||
test-cov:
|
||||
export SIMPLE_6TO5_TESTS=1
|
||||
rm -rf coverage
|
||||
node $(ISTANBUL_CMD) $(MOCHA_CMD) --
|
||||
|
||||
test-travis: bootstrap
|
||||
node $(ISTANBUL_CMD) $(MOCHA_CMD) --
|
||||
if test -n "$$CODECLIMATE_REPO_TOKEN"; then codeclimate < coverage/lcov.info; fi
|
||||
|
||||
test-browser:
|
||||
mkdir -p dist
|
||||
|
||||
node bin/cache-templates
|
||||
node bin/cache-tests
|
||||
node $(BROWSERIFY_CMD) -e test/_browser.js >dist/6to5-test.js
|
||||
rm -rf templates.json tests.json
|
||||
|
||||
test -n "`which open`" && open test/browser.html
|
||||
|
||||
publish:
|
||||
git pull --rebase
|
||||
|
||||
make test
|
||||
|
||||
read -p "Version: " version; \
|
||||
npm version $$version --message "v%s"
|
||||
|
||||
make build
|
||||
cp dist/6to5.min.js browser.js
|
||||
cp dist/polyfill.min.js browser-polyfill.js
|
||||
@@ -74,10 +82,24 @@ publish:
|
||||
node bin/cache-templates
|
||||
test -f templates.json
|
||||
|
||||
read -p "Version: " version; \
|
||||
npm version $$version --message "v%s"
|
||||
npm publish
|
||||
|
||||
git push --follow-tags
|
||||
|
||||
# generate
|
||||
bin/generate-core-package-json >package2.json
|
||||
mv package.json .package.json
|
||||
mv package2.json package.json
|
||||
|
||||
npm publish
|
||||
|
||||
# restore
|
||||
rm -rf package.json
|
||||
mv .package.json package.json
|
||||
|
||||
rm -rf templates.json browser.js runtime.js browser-polyfill.js
|
||||
|
||||
bootstrap:
|
||||
npm install
|
||||
git submodule update --init
|
||||
cd vendor/regenerator; npm install
|
||||
|
||||
13
NOTES.md
Normal file
13
NOTES.md
Normal file
@@ -0,0 +1,13 @@
|
||||
# Notes
|
||||
|
||||
* Wildcard exports/imports wont normalise if `export default` is a non-object. See [#224](https://github.com/6to5/6to5/issues/224).
|
||||
|
||||
## 3.0.0 breaking changes
|
||||
|
||||
* Remove `allowImportExportEverywhere` option from acorn.
|
||||
* Remove this shorthand from playground.
|
||||
* Remove `super()` inside non-constructors - add descriptive error message.
|
||||
* Split up ES5 getter/setter transforming and ES6 property methods into separate transformers.
|
||||
* Add autoindentation.
|
||||
* Move `super` transformation from classes into a separate transformer that also supports object expressions.
|
||||
* Prefix all fast transformers with `fast`, make them declare that they're a fast transformer similar to optional transformers and add a `--fast` flag to enable them all.
|
||||
@@ -11,10 +11,6 @@
|
||||
<img alt="Travis Status" src="http://img.shields.io/travis/6to5/6to5/master.svg?style=flat&label=travis">
|
||||
</a>
|
||||
|
||||
<a href="https://ci.appveyor.com/project/sebmck/6to5">
|
||||
<img alt="Appveyor Status" src="http://img.shields.io/appveyor/ci/sebmck/6to5.svg?style=flat&label=appveyor">
|
||||
</a>
|
||||
|
||||
<a href="https://codeclimate.com/github/6to5/6to5">
|
||||
<img alt="Code Climate Score" src="http://img.shields.io/codeclimate/github/6to5/6to5.svg?style=flat">
|
||||
</a>
|
||||
|
||||
@@ -1,99 +0,0 @@
|
||||
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||
var obj = {};
|
||||
var foo = "foo";
|
||||
var bar = "bar";
|
||||
|
||||
// constants
|
||||
const MULTIPLIER = 5;
|
||||
|
||||
// classes
|
||||
class Foo {
|
||||
constructor() {
|
||||
this.foo = "bar";
|
||||
}
|
||||
}
|
||||
|
||||
class Bar extends Foo {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
// default parameters
|
||||
go(foo = "bar", bar = "foo") {
|
||||
|
||||
}
|
||||
|
||||
// not supported by jstransform
|
||||
//get foo() {
|
||||
// return this._foo;
|
||||
//}
|
||||
//set foo(val) {
|
||||
// this._foo = val + " foo!";
|
||||
//}
|
||||
}
|
||||
|
||||
// arrow functions
|
||||
arr.map(x => x * x);
|
||||
|
||||
// block binding
|
||||
for (let key in arr) {
|
||||
let val = arr[key];
|
||||
console.log(key, val);
|
||||
}
|
||||
|
||||
// computed property names
|
||||
obj = {
|
||||
["foo" + bar]: "foobar"
|
||||
};
|
||||
|
||||
// destructuring
|
||||
var [a, [b], c, d] = ["hello", [", ", "junk"], ["world"]];
|
||||
console.log(a + b + c);
|
||||
|
||||
// array comprehension
|
||||
// [for (i of [1, 2, 3]) i * i]; // not supported by es6now
|
||||
|
||||
// for-of
|
||||
for (var i of [1, 2, 3]) {
|
||||
console.log(i * i);
|
||||
}
|
||||
|
||||
// property method assignment
|
||||
obj = {
|
||||
foo() {
|
||||
return "foobar";
|
||||
},
|
||||
|
||||
get bar() {
|
||||
return this._bar;
|
||||
},
|
||||
|
||||
set bar(val) {
|
||||
this._bar = val;
|
||||
}
|
||||
};
|
||||
|
||||
// property name shorthand
|
||||
function f(x, y) {
|
||||
return { x, y };
|
||||
}
|
||||
|
||||
// rest parameters
|
||||
function printList(name, ...items) {
|
||||
console.log("list %s has the following items", name);
|
||||
items.forEach(function (item) {
|
||||
console.log(item);
|
||||
});
|
||||
}
|
||||
|
||||
// spread
|
||||
function add(x, y) {
|
||||
return x + y;
|
||||
}
|
||||
var numbers = [5, 10];
|
||||
add(...numbers);
|
||||
|
||||
// template literals
|
||||
var x = 5;
|
||||
var y = 10;
|
||||
console.log(`${x} + ${y} = ${x + y}`);
|
||||
@@ -1,168 +0,0 @@
|
||||
Error.stackTraceLimit = Infinity;
|
||||
|
||||
var jsTrans = require("jstransform");
|
||||
var traceur = require("traceur");
|
||||
//var es6tr = require("es6-transpiler");
|
||||
var es6now = require("es6now");
|
||||
//var esnext = require("esnext");
|
||||
var to5 = require("../lib/6to5");
|
||||
|
||||
//var uglify = require("uglify-js");
|
||||
var matcha = require("matcha");
|
||||
var path = require("path");
|
||||
var fs = require("fs");
|
||||
var _ = require("lodash");
|
||||
|
||||
var readResolve = function (filename) {
|
||||
try {
|
||||
filename = require.resolve(filename);
|
||||
} catch (err) {
|
||||
return null;
|
||||
}
|
||||
return fs.readFileSync(filename, "utf8");
|
||||
};
|
||||
|
||||
var getVersion = function (name) {
|
||||
return require(name + "/package.json").version;
|
||||
};
|
||||
|
||||
var jsTransVisitors = [];
|
||||
|
||||
_.each([
|
||||
"arrow-function-visitors", "class-visitors", "destructuring-visitors",
|
||||
"object-concise-method-visitors", "object-short-notation-visitors",
|
||||
"rest-param-visitors", "template-visitors"
|
||||
], function (name) {
|
||||
var mod = require("jstransform/visitors/es6-" + name);
|
||||
jsTransVisitors = jsTransVisitors.concat(mod.visitorList);
|
||||
});
|
||||
|
||||
|
||||
var compilers = {
|
||||
"6to5": {
|
||||
version: getVersion(".."),
|
||||
compile: function (code, filename) {
|
||||
return to5.transform(code, { filename: filename }).code;
|
||||
}
|
||||
},
|
||||
|
||||
traceur: {
|
||||
runtime: readResolve("traceur/bin/traceur-runtime.js"),
|
||||
compile: function (code) {
|
||||
return traceur.compile(code, {
|
||||
modules: "commonjs",
|
||||
experimental: true
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
/*esnext: {
|
||||
runtime: readResolve("esnext/node_modules/regenerator/runtime.js") || readResolve("regenerator/runtime.js"),
|
||||
compile: function (code) {
|
||||
return esnext.compile(code).code;
|
||||
}
|
||||
},*/
|
||||
|
||||
es6now: {
|
||||
runtime: readResolve("es6now/runtime/ES6.js"),
|
||||
compile: function (code) {
|
||||
return es6now.translate(code);
|
||||
}
|
||||
},
|
||||
|
||||
/*"es6-transpiler": {
|
||||
compile: function (code) {
|
||||
var result = es6tr.run({ src: code });
|
||||
if (result.errors.length) throw new Error(result.join("; "));
|
||||
return result.src;
|
||||
}
|
||||
},*/
|
||||
|
||||
jstransform: {
|
||||
compile: function (code) {
|
||||
return jsTrans.transform(jsTransVisitors, code).code;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// versions
|
||||
|
||||
//var uglifyTitle = "uglify v" + getVersion("uglify-js");
|
||||
|
||||
_.each(compilers, function (compiler, name) {
|
||||
compiler.title = name + " v" + (compiler.version || getVersion(name));
|
||||
});
|
||||
|
||||
//
|
||||
|
||||
var sizeBenchmark = function (code, loc, name, compiler) {
|
||||
var log = function (output, title) {
|
||||
title = [compiler.title].concat(title || []).join(" + ");
|
||||
|
||||
var text;
|
||||
var color;
|
||||
if (output.stack) {
|
||||
text = "error";
|
||||
color = "red";
|
||||
} else {
|
||||
var kilo = (output.length / 1024).toFixed(2);
|
||||
text = kilo + "KB";
|
||||
color = "cyan";
|
||||
}
|
||||
|
||||
text = matcha.utils.color(matcha.utils.padBefore(text, 22), color);
|
||||
|
||||
console.log(text, matcha.utils.color("» " + title, "gray"));
|
||||
|
||||
if (output.stack) {
|
||||
console.error(output.stack);
|
||||
}
|
||||
};
|
||||
|
||||
var go = function (getOutput, title) {
|
||||
var code;
|
||||
try {
|
||||
code = getOutput();
|
||||
} catch (err) {
|
||||
log(err, title);
|
||||
return;
|
||||
}
|
||||
|
||||
log(code, title);
|
||||
};
|
||||
|
||||
var output;
|
||||
go(function () {
|
||||
return output = compiler.compile(code, loc);
|
||||
});
|
||||
if (!output) return;
|
||||
|
||||
//go(function () {
|
||||
// return uglify.minify(output, { fromString: true }).code;
|
||||
//}, uglifyTitle);
|
||||
};
|
||||
|
||||
//
|
||||
|
||||
_.each(fs.readdirSync(__dirname + "/fixtures"), function (name) {
|
||||
var alias = path.basename(name, path.extname(name));
|
||||
|
||||
suite(alias, function () {
|
||||
set("delay", 0);
|
||||
|
||||
var loc = __dirname + "/fixtures/" + name;
|
||||
var code = fs.readFileSync(loc, "utf8");
|
||||
|
||||
before(function () {
|
||||
_.each(compilers, function (compiler, name) {
|
||||
sizeBenchmark(code, loc, name, compiler);
|
||||
});
|
||||
});
|
||||
|
||||
_.each(compilers, function (compiler) {
|
||||
bench(compiler.title, function () {
|
||||
compiler.compile(code, loc);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -15,14 +15,18 @@ commander.option("-e, --experimental", "Enable experimental support for proposed
|
||||
commander.option("-p, --playground", "Enable playground support");
|
||||
|
||||
commander.option("-m, --modules [modules]", "Module formatter type to use [common]", "common");
|
||||
commander.option("-w, --whitelist [whitelist]", "Whitelist of transformers to ONLY use", util.list);
|
||||
commander.option("-l, --whitelist [whitelist]", "Whitelist of transformers to ONLY use", util.list);
|
||||
commander.option("-b, --blacklist [blacklist]", "Blacklist of transformers to NOT use", util.list);
|
||||
commander.option("-i, --optional [list]", "List of optional transformers to enable", util.list);
|
||||
commander.option("-o, --out-file [out]", "Compile all input files into a single file");
|
||||
commander.option("-d, --out-dir [out]", "Compile an input directory of modules into an output directory");
|
||||
commander.option("-c, --remove-comments", "Remove comments from the compiled code", false);
|
||||
commander.option("-I, --indent [width]", "Indent width [2]", 2);
|
||||
commander.option("-a, --amd-module-ids", "Insert module id in AMD modules", false); // todo: remove in 3.0.0
|
||||
commander.option("-m, --module-ids", "Insert module id in modules", false);
|
||||
commander.option("-R, --react-compat", "Makes the react transformer produce pre-v0.12 code");
|
||||
commander.option("-E, --include-regenerator", "Include the regenerator runtime if necessary", false);
|
||||
commander.option("--keep-module-id-extensions", "Keep extensions when generating module ids", false);
|
||||
|
||||
commander.on("--help", function(){
|
||||
var outKeys = function (title, obj) {
|
||||
@@ -98,17 +102,25 @@ if (errors.length) {
|
||||
//
|
||||
|
||||
exports.opts = {
|
||||
sourceMapName: commander.outFile,
|
||||
experimental: commander.experimental,
|
||||
playground: commander.playground,
|
||||
moduleIds: commander.amdModuleIds || commander.moduleIds,
|
||||
blacklist: commander.blacklist,
|
||||
whitelist: commander.whitelist,
|
||||
sourceMap: commander.sourceMaps || commander.sourceMapsInline,
|
||||
optional: commander.optional,
|
||||
comments: !commander.removeComments,
|
||||
runtime: commander.runtime,
|
||||
modules: commander.modules
|
||||
keepModuleIdExtensions: commander.keepModuleIdExtensions,
|
||||
includeRegenerator: commander.includeRegenerator,
|
||||
sourceMapName: commander.outFile,
|
||||
experimental: commander.experimental,
|
||||
reactCompat: commander.reactCompat,
|
||||
playground: commander.playground,
|
||||
moduleIds: commander.amdModuleIds || commander.moduleIds,
|
||||
blacklist: commander.blacklist,
|
||||
whitelist: commander.whitelist,
|
||||
sourceMap: commander.sourceMaps || commander.sourceMapsInline,
|
||||
optional: commander.optional,
|
||||
comments: !commander.removeComments,
|
||||
runtime: commander.runtime,
|
||||
modules: commander.modules,
|
||||
format: {
|
||||
indent: {
|
||||
style: util.repeat(parseInt(commander.indent))
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var fn;
|
||||
|
||||
@@ -20,7 +20,17 @@ exports.transform = function (filename, code, opts) {
|
||||
opts = _.extend(opts || {}, index.opts);
|
||||
opts.filename = filename;
|
||||
|
||||
var result = to5.transform(code, opts);
|
||||
var result;
|
||||
try {
|
||||
result = to5.transform(code, opts);
|
||||
} catch (e) {
|
||||
if (e instanceof SyntaxError) {
|
||||
console.error("SyntaxError:", e.message);
|
||||
process.exit(1);
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
result.filename = filename;
|
||||
result.actual = code;
|
||||
return result;
|
||||
|
||||
@@ -10,29 +10,31 @@ var util = require("../lib/6to5/util");
|
||||
var vm = require("vm");
|
||||
var _ = require("lodash");
|
||||
|
||||
commander.option("-e, --eval [script]", "Evaluate script");
|
||||
commander.option("-p, --print", "Evaluate script and print result");
|
||||
commander.option("-i, --ignore [regex]", "Ignore all files that match this regex when using the require hook");
|
||||
commander.option("-x, --extensions [extensions]", "List of extensions to hook into [.es6,.js]");
|
||||
commander.option("-r, --experimental", "Enable experimental support for proposed ES7 features");
|
||||
commander.option("-g, --playground", "Enable playground support");
|
||||
commander.option("-c, --cache", "Cache compiled files and require paths");
|
||||
var program = new commander.Command("6to5-node");
|
||||
|
||||
program.option("-e, --eval [script]", "Evaluate script");
|
||||
program.option("-p, --print", "Evaluate script and print result");
|
||||
program.option("-i, --ignore [regex]", "Ignore all files that match this regex when using the require hook");
|
||||
program.option("-x, --extensions [extensions]", "List of extensions to hook into [.es6,.js]");
|
||||
program.option("-r, --experimental", "Enable experimental support for proposed ES7 features");
|
||||
program.option("-g, --playground", "Enable playground support");
|
||||
program.option("-c, --cache", "Cache compiled files and require paths");
|
||||
|
||||
var pkg = require("../package.json");
|
||||
commander.version(pkg.version);
|
||||
commander.usage("[options] [ -e script | script.js ] [arguments]");
|
||||
commander.parse(process.argv);
|
||||
program.version(pkg.version);
|
||||
program.usage("[options] [ -e script | script.js ] [arguments]");
|
||||
program.parse(process.argv);
|
||||
|
||||
if (commander.cache) roadrunner.load();
|
||||
if (program.cache) roadrunner.load();
|
||||
|
||||
//
|
||||
|
||||
to5.register({
|
||||
experimental: commander.experimental,
|
||||
extensions: commander.extensions,
|
||||
playground: commander.playground,
|
||||
ignore: commander.ignore,
|
||||
cache: commander.cache && roadrunner.get("6to5")
|
||||
experimental: program.experimental,
|
||||
extensions: program.extensions,
|
||||
playground: program.playground,
|
||||
ignore: program.ignore,
|
||||
cache: program.cache && roadrunner.get("6to5")
|
||||
});
|
||||
|
||||
//
|
||||
@@ -41,17 +43,17 @@ var _eval = function (code, filename) {
|
||||
code = to5.transform(code, {
|
||||
filename: filename,
|
||||
blacklist: ["useStrict"],
|
||||
experimental: commander.experimental,
|
||||
playground: commander.playground
|
||||
experimental: program.experimental,
|
||||
playground: program.playground
|
||||
}).code;
|
||||
return vm.runInThisContext(code, filename);
|
||||
};
|
||||
|
||||
if (commander.eval) {
|
||||
var result = _eval(commander.eval, "eval");
|
||||
if (commander.print) console.log(result);
|
||||
if (program.eval) {
|
||||
var result = _eval(program.eval, "eval");
|
||||
if (program.print) console.log(result);
|
||||
} else {
|
||||
if (commander.args.length) {
|
||||
if (program.args.length) {
|
||||
// slice all arguments up to the first filename since they're 6to5 args that we handle
|
||||
var args = process.argv.slice(2);
|
||||
|
||||
@@ -77,7 +79,7 @@ if (commander.eval) {
|
||||
}
|
||||
}
|
||||
|
||||
if (commander.cache) roadrunner.save();
|
||||
if (program.cache) roadrunner.save();
|
||||
|
||||
function replStart() {
|
||||
repl.start({
|
||||
|
||||
11
bin/generate-core-package-json
Executable file
11
bin/generate-core-package-json
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
var pkg = require("../package.json");
|
||||
pkg.name += "-core";
|
||||
delete pkg.dependencies.chokidar;
|
||||
delete pkg.optionalDependencies;
|
||||
delete pkg.devDependencies;
|
||||
delete pkg.preferGlobal;
|
||||
delete pkg.scripts;
|
||||
delete pkg.bin;
|
||||
console.log(JSON.stringify(pkg, null, " "));
|
||||
@@ -1,65 +0,0 @@
|
||||
# Browser
|
||||
|
||||
A browser version of 6to5 is available from `browser.js` inside the 6to5
|
||||
directory in an npm release.
|
||||
|
||||
## Scripts
|
||||
|
||||
While it's not recommended for serious use, when the browser version is included
|
||||
all scripts with the type `text/ecmascript-6` and `text/6to5` are automatically
|
||||
compiled and ran.
|
||||
|
||||
For example:
|
||||
|
||||
```html
|
||||
<script src="node_modules/6to5/browser.js"></script>
|
||||
<script type="text/6to5">
|
||||
class Test {
|
||||
test() {
|
||||
return "test";
|
||||
}
|
||||
}
|
||||
|
||||
var test = new Test;
|
||||
test.test();
|
||||
</script>
|
||||
```
|
||||
|
||||
## Build
|
||||
|
||||
You can build a browser version of the compiler by running the following in the
|
||||
6to5 directory:
|
||||
|
||||
```sh
|
||||
$ make build
|
||||
```
|
||||
|
||||
This will output the files `dist/6to5.js` and `dist/6to5.min.js`.
|
||||
|
||||
## Test
|
||||
|
||||
To test 6to5 in your browser run:
|
||||
|
||||
```sh
|
||||
$ make test-browser
|
||||
```
|
||||
|
||||
And open `test/browser.html` in your browser if it doesn't open automatically.
|
||||
|
||||
## API
|
||||
|
||||
### to5.transform(code, [opts])
|
||||
|
||||
See [options](usage.md#options) for additional documentation.
|
||||
|
||||
```javascript
|
||||
to5.transform("class Test {}").code;
|
||||
```
|
||||
|
||||
### to5.run(code, [opts])
|
||||
|
||||
See [options](usage.md#options) for additional documentation.
|
||||
|
||||
```javascript
|
||||
to5.run("class Test {}");
|
||||
```
|
||||
@@ -1,59 +0,0 @@
|
||||
# Caveats
|
||||
|
||||
In order for certain features to work they require certain polyfills. You can
|
||||
satisfy **all** 6to5 feature requirements by using the included
|
||||
[polyfill](polyfill.md).
|
||||
|
||||
You may alternatively selectively include what you need:
|
||||
|
||||
| Feature | Requirements |
|
||||
| --------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
|
||||
| Abstract References | [experimental](experimental.md), `Symbol` |
|
||||
| Array destructuring | `Array.isArray`, `Array.from` |
|
||||
| Async functions, Generators | [experimental](experimental.md), [regenerator runtime](https://github.com/facebook/regenerator/blob/master/runtime.js) |
|
||||
| Comprehensions | [experimental](experimental.md), `Array.isArray`, `Array.from` |
|
||||
| For Of | `Symbol`, `prototype[Symbol.iterator]` |
|
||||
| Object spread/rest | [experimental](experimental.md), `Object.assign` |
|
||||
| Spread | `Array.isArray`, `Array.from` |
|
||||
|
||||
## ES5
|
||||
|
||||
Since 6to5 assumes that your code will be ran in an ES5 environment it uses ES5
|
||||
functions. So if you're using an environment that has limited or no support for
|
||||
ES5 such as lower versions of IE then using the
|
||||
[es5-shim](https://github.com/es-shims/es5-shim) along with the
|
||||
[6to5 polyfill](polyfill.md) will add support for these methods.
|
||||
|
||||
## Internet Explorer
|
||||
|
||||
### Classes (9 and below)
|
||||
|
||||
If you're inheriting from a class then static properties are inherited from it
|
||||
via [\_\_proto\_\_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto),
|
||||
this is widely supported but you may run into problems with much older browsers.
|
||||
|
||||
**NOTE:** `__proto__` is not supported on IE <= 9 so static properties
|
||||
**will not** be inherited. A possible workaround is to use `super();`:
|
||||
|
||||
```javascript
|
||||
class Foo {
|
||||
static foo() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class Bar extends Foo {
|
||||
static foo() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Getters/setters (8 and below)
|
||||
|
||||
In IE8 `Object.defineProperty` can only be used on DOM objects. This is
|
||||
unfortunate as it's required to set getters and setters. Due to this if
|
||||
you plan on supporting IE8 or below then the user of getters and setters
|
||||
isn't recommended.
|
||||
|
||||
Reference: [MDN](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty#Internet_Explorer_8_specific_notes).
|
||||
@@ -1,138 +0,0 @@
|
||||
# Differences
|
||||
|
||||
There are three main points that separate 6to5 from all other transpilers.
|
||||
|
||||
### Readable code
|
||||
|
||||
The fundamental concept behind 6to5 is that the generated code must be close as
|
||||
possible to the original, retaining all the same formatting and readability.
|
||||
|
||||
Many other transpilers are just concerned with making the code work while 6to5
|
||||
is concerned with making sure it works **and** is readable at the same time.
|
||||
|
||||
For example, given the following array comprehension:
|
||||
|
||||
```javascript
|
||||
var seattlers = [for (c of customers) if (c.city == "Seattle") { name: c.name, age: c.age }];
|
||||
```
|
||||
|
||||
is generated to the following with 6to5:
|
||||
|
||||
```javascript
|
||||
var seattlers = Array.from(customers).filter(function (c) {
|
||||
return c.city == "Seattle";
|
||||
}).map(function (c) {
|
||||
return {
|
||||
name: c.name,
|
||||
age: c.age
|
||||
};
|
||||
});
|
||||
```
|
||||
|
||||
The following is what Traceur generates:
|
||||
|
||||
```javascript
|
||||
var seattlers = (function() {
|
||||
var c;
|
||||
var $__20 = 0,
|
||||
$__21 = [];
|
||||
for (var $__22 = customers[$traceurRuntime.toProperty(Symbol.iterator)](),
|
||||
$__23; !($__23 = $__22.next()).done; ) {
|
||||
c = $__23.value;
|
||||
if (c.city == "Seattle")
|
||||
$traceurRuntime.setProperty($__21, $__20++, {
|
||||
name: c.name,
|
||||
age: c.age
|
||||
});
|
||||
}
|
||||
return $__21;
|
||||
}());
|
||||
```
|
||||
|
||||
As you can tell, it's not very pretty, unreadable even. Instead of mapping
|
||||
directly to a runtime, like other transpilers, 6to5 maps directly to the
|
||||
equivalent ES5.
|
||||
|
||||
Sometimes there are little inline functions that 6to5 needs. These are
|
||||
placed at the top of your file much like coffee-script does. If these
|
||||
are bother you then you can use the [optional runtime](optional-runtime.md).
|
||||
We promise that these inline functions will never be significant and will
|
||||
always be used as little as possible.
|
||||
|
||||
### Static analysis
|
||||
|
||||
6to5 uses a lot of static analysis to simplify code as much as possible.
|
||||
Not many other transpilers do this, and those that do don't do it nearly
|
||||
as much as 6to5. This process is pretty intensive but it leads to higher
|
||||
quality code.
|
||||
|
||||
### Spec compliancy
|
||||
|
||||
6to5 prides itself on
|
||||
[spec compliancy](https://kangax.github.io/compat-table/es6/). We have
|
||||
excellent support for edgecases, something that many other transpilers
|
||||
suffer from, including Traceur. When you use 6to5 you can be confident
|
||||
that when you turn it off and use your code in a full ES6 environment
|
||||
**it'll just work**.
|
||||
|
||||
## Comparison to other transpilers
|
||||
|
||||
### Features
|
||||
|
||||
| | 6to5 | Traceur | es6-transpiler | esnext | es6now | jstransform |
|
||||
| ---------------------------- | ---- | ------- | -------------- | ------ | ------ | ----------- |
|
||||
| Source maps | ✓ | ✓ | ✓ | ✓ | | ✓ |
|
||||
| No compiler global pollution | ✓ | | ✓ | ✓ | | ✓ |
|
||||
| Optional runtime | ✓ | | ✓ | | | ✓ |
|
||||
| Browser compiler | ✓ | ✓ | | ✓ | | |
|
||||
|
||||
### Language Support
|
||||
|
||||
| | 6to5 | Traceur | es6-transpiler | esnext | es6now | jstransform |
|
||||
| ---------------------------- | ---- | ------- | -------------- | ------ | ------ | ----------- |
|
||||
| Abstract references | ✓ | | | | | |
|
||||
| Array comprehension | ✓ | ✓ | ✓ | | | |
|
||||
| Arrow functions | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
||||
| Async functions | ✓ | ✓ | | ✓ | | |
|
||||
| Async generator functions | ✓ | ✓ | | | | |
|
||||
| Classes | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
||||
| Computed property names | ✓ | ✓ | ✓ | ✓ | ✓ | |
|
||||
| Constants | ✓ | ✓ | ✓ | | | |
|
||||
| Default parameters | ✓ | ✓ | ✓ | ✓ | ✓ | |
|
||||
| Destructuring | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
||||
| Exponentiation operator | ✓ | ✓ | | | | |
|
||||
| Flow types | ✓ | | | | | ✓ |
|
||||
| For-of | ✓ | ✓ | ✓ | ✓ | ✓ | |
|
||||
| Generators | ✓ | ✓ | | ✓ | | |
|
||||
| Generator comprehension | ✓ | ✓ | | | | |
|
||||
| JSX | ✓ | | | | | |
|
||||
| Let scoping | ✓ | ✓ | ✓ | | | |
|
||||
| Modules | ✓ | ✓ | | | ✓ | |
|
||||
| Object rest/spread | ✓ | | | | | ✓ |
|
||||
| Property method assignment | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
||||
| Property name shorthand | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
||||
| Rest parameters | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
||||
| React | ✓ | | | | | |
|
||||
| Spread | ✓ | ✓ | ✓ | ✓ | ✓ | |
|
||||
| Template literals | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
||||
| Unicode regex | ✓ | ✓ | ✓ | | | |
|
||||
|
||||
### [Traceur](https://github.com/google/traceur-compiler)
|
||||
|
||||
Traceur requires quite a bulky runtime (~75KB) and produces quite verbose code.
|
||||
While this can be trimmed down by selectively building the runtime, it's an
|
||||
unnecessary step when a large runtime can be eliminated entirely.
|
||||
|
||||
### [es6now](https://github.com/zenparsing/es6now)
|
||||
|
||||
es6now doesn't output sourcemaps. This is cited as a positive as line-to-line
|
||||
mapping is the goal. This however obviously doesn't retain column mapping
|
||||
resulting in the output code not being very pleasant.
|
||||
|
||||
### [es6-transpiler](https://github.com/termi/es6-transpiler)
|
||||
|
||||
The es6-transpiler compiler requires shims to operate which pollutes the global
|
||||
scope resulting in possible collisions.
|
||||
|
||||
es6-transpiler maps line-by-line, just like es6now, this results in the same
|
||||
issues such as lack of column information and unpleasant code output.
|
||||
@@ -1,8 +0,0 @@
|
||||
## Experimental
|
||||
|
||||
6to5 also has experimental support for ES7 proposals. You can enable this with
|
||||
the `experimental: true` option when using the [Node API](#node) or
|
||||
`--experimental` when using the [CLI](#cli).
|
||||
|
||||
**WARNING:** These proposals are subject to change so use with
|
||||
**extreme caution**.
|
||||
283
doc/features.md
283
doc/features.md
@@ -1,283 +0,0 @@
|
||||
# Features
|
||||
|
||||
## Abstract references ([experimental](experimental.md)) ([spec](https://github.com/zenparsing/es-abstract-refs))
|
||||
|
||||
```javascript
|
||||
foo::bar;
|
||||
foo::bar = baz;
|
||||
delete foo::bar;
|
||||
```
|
||||
|
||||
## Array comprehensions ([experimental](experimental.md))
|
||||
|
||||
```javascript
|
||||
var results = [for (c of customers) if (c.city == "Seattle") { name: c.name, age: c.age }]
|
||||
```
|
||||
|
||||
## Arrow functions
|
||||
|
||||
```javascript
|
||||
// Expression bodies
|
||||
var odds = evens.map(v => v + 1);
|
||||
var nums = evens.map((v, i) => v + i);
|
||||
|
||||
// Statement bodies
|
||||
nums.forEach(v => {
|
||||
if (v % 5 === 0)
|
||||
fives.push(v);
|
||||
});
|
||||
|
||||
// Lexical this
|
||||
var bob = {
|
||||
_name: "Bob",
|
||||
_friends: [],
|
||||
printFriends() {
|
||||
this._friends.forEach(f => {
|
||||
console.log(this._name + " knows " + f);
|
||||
});
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
## Async functions ([experimental](experimental.md)) ([spec](https://github.com/lukehoban/ecmascript-asyncawait))
|
||||
|
||||
```javascript
|
||||
async function chainAnimationsAsync(elem, animations) {
|
||||
for (var anim of animations) {
|
||||
await anim(elem);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Async generator functions ([experimental](experimental.md)) ([spec](https://github.com/jhusain/asyncgenerator))
|
||||
|
||||
```javascript
|
||||
|
||||
```
|
||||
|
||||
## Classes
|
||||
|
||||
```javascript
|
||||
class SkinnedMesh extends THREE.Mesh {
|
||||
constructor(geometry, materials) {
|
||||
super(geometry, materials);
|
||||
|
||||
this.idMatrix = SkinnedMesh.defaultMatrix();
|
||||
this.bones = [];
|
||||
this.boneMatrices = [];
|
||||
//...
|
||||
}
|
||||
|
||||
update(camera) {
|
||||
//...
|
||||
super.update();
|
||||
}
|
||||
|
||||
static defaultMatrix() {
|
||||
return new THREE.Matrix4();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Computed property names
|
||||
|
||||
```javascript
|
||||
var foo = "foo";
|
||||
var bar = "bar";
|
||||
var obj = {
|
||||
["foo" + bar]: "heh",
|
||||
["bar" + foo]: "noo",
|
||||
foo: "foo",
|
||||
bar: "bar"
|
||||
};
|
||||
```
|
||||
|
||||
## Constants
|
||||
|
||||
```javascript
|
||||
const MULTIPLIER = 5;
|
||||
console.log(2 * MULTIPLIER);
|
||||
|
||||
MULTIPLIER = 6; // error
|
||||
var MULTIPLIER; // error
|
||||
```
|
||||
|
||||
## Default parameters
|
||||
|
||||
```javascript
|
||||
function f(x, y = 12) {
|
||||
// y is 12 if not passed (or passed as undefined)
|
||||
return x + y;
|
||||
}
|
||||
f(3) == 15
|
||||
```
|
||||
|
||||
## Destructuring
|
||||
|
||||
```javascript
|
||||
// list matching
|
||||
var [a, , b] = [1,2,3];
|
||||
|
||||
// object matching
|
||||
var { op: a, lhs: { op: b }, rhs: c } = getASTNode();
|
||||
|
||||
// object matching shorthand
|
||||
// binds `op`, `lhs` and `rhs` in scope
|
||||
var { op, lhs, rhs } = getASTNode();
|
||||
|
||||
// Can be used in parameter position
|
||||
function g({ name: x }) {
|
||||
console.log(x);
|
||||
}
|
||||
g({ name: 5 });
|
||||
|
||||
// Fail-soft destructuring
|
||||
var [a] = [];
|
||||
a === undefined;
|
||||
```
|
||||
|
||||
## Exponentiation operator ([experimental](experimental.md)) ([spec](https://github.com/rwaldron/exponentiation-operator))
|
||||
|
||||
```javascript
|
||||
var a = 2;
|
||||
a **= 2;
|
||||
|
||||
var squared = 2 ** 2;
|
||||
```
|
||||
|
||||
## For-of
|
||||
|
||||
```javascript
|
||||
for (var i of [1, 2, 3]) {
|
||||
console.log(i * i);
|
||||
}
|
||||
```
|
||||
|
||||
## Generators
|
||||
|
||||
```javascript
|
||||
function* fibonacci() {
|
||||
var pre = 0, cur = 1;
|
||||
for (;;) {
|
||||
var temp = pre;
|
||||
pre = cur;
|
||||
cur += temp;
|
||||
yield cur;
|
||||
}
|
||||
}
|
||||
|
||||
for (var n of fibonacci()) {
|
||||
// truncate the sequence at 1000
|
||||
if (n > 1000) break;
|
||||
console.log(n);
|
||||
}
|
||||
```
|
||||
|
||||
## Generator comprehensions ([experimental](experimental.md))
|
||||
|
||||
```javascript
|
||||
var nums = [1, 2, 3, 4, 5, 6];
|
||||
var multiples = (for (i of nums) if (i % 2) i * i);
|
||||
assert.equal(multiples.next().value, 1);
|
||||
assert.equal(multiples.next().value, 9);
|
||||
assert.equal(multiples.next().value, 25);
|
||||
```
|
||||
|
||||
## Let scoping
|
||||
|
||||
```javascript
|
||||
for (let i in arr) {
|
||||
let v = arr[i];
|
||||
}
|
||||
```
|
||||
|
||||
## Modules
|
||||
|
||||
```javascript
|
||||
import "foo";
|
||||
import foo from "foo";
|
||||
import * as foo from "foo";
|
||||
import {bar} from "foo";
|
||||
import {foo as bar} from "foo";
|
||||
|
||||
export { test };
|
||||
export var test = 5;
|
||||
export function test() {}
|
||||
|
||||
export default test;
|
||||
```
|
||||
|
||||
## Numeric literals
|
||||
|
||||
```javascript
|
||||
0b111110111 === 503; // true
|
||||
0o767 === 503; // true
|
||||
```
|
||||
|
||||
## Object spread/rest ([experimental](experimental.md)) ([spec](https://github.com/sebmarkbage/ecmascript-rest-spread))
|
||||
|
||||
```javascript
|
||||
var n = { x, y, ...z };
|
||||
var { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
|
||||
```
|
||||
|
||||
## Property method assignment
|
||||
|
||||
```javascript
|
||||
var obj = {
|
||||
foo() {
|
||||
return "foobar";
|
||||
},
|
||||
|
||||
get bar() {
|
||||
return this._bar;
|
||||
},
|
||||
|
||||
set bar(val) {
|
||||
this._bar = val;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
## Property name shorthand
|
||||
|
||||
```javascript
|
||||
function f(x, y) {
|
||||
return { x, y };
|
||||
}
|
||||
```
|
||||
|
||||
## Rest parameters
|
||||
|
||||
```javascript
|
||||
function f(x, ...y) {
|
||||
// y is an Array
|
||||
return x * y.length;
|
||||
}
|
||||
f(3, "hello", true) == 6
|
||||
```
|
||||
|
||||
## Spread
|
||||
|
||||
```javascript
|
||||
function f(x, y, z) {
|
||||
return x + y + z;
|
||||
}
|
||||
// Pass each elem of array as argument
|
||||
f(...[1,2,3]) == 6
|
||||
```
|
||||
|
||||
## Template literals
|
||||
|
||||
```javascript
|
||||
var x = 5;
|
||||
var y = 10;
|
||||
console.log(`${x} + ${y} = ${x + y}`); // "5 + 10 = 15"
|
||||
```
|
||||
## Unicode regex
|
||||
|
||||
```javascript
|
||||
var string = 'foo💩bar';
|
||||
var match = string.match(/foo(.)bar/u);
|
||||
console.log(match[1]);
|
||||
```
|
||||
63
doc/index.md
63
doc/index.md
@@ -1,62 +1 @@
|
||||
**6to5** turns ES6+ code into vanilla ES5, so you can use next generation features **today.**
|
||||
|
||||
- **Readable** - formatting is retained if possible so your generated code is as similar as possible.
|
||||
- **Extensible** - with a large range of [plugins](plugins.md) and **browser support**.
|
||||
- **Lossless** - **source map support** so you can debug your compiled code with ease.
|
||||
- **Compact** - maps directly to the equivalent ES5 with **no runtime**[\*](caveats.md).
|
||||
|
||||
## Installation
|
||||
|
||||
It's as easy as:
|
||||
|
||||
$ npm install -g 6to5
|
||||
|
||||
## Usage
|
||||
|
||||
Once you've installed 6to5, there are multiple paths you can take depending on
|
||||
how you want to use it.
|
||||
|
||||
6to5 will simply compile your ES6+ script to ES5 if you pass it as an argument
|
||||
to the command-line tool `6to5`:
|
||||
|
||||
```sh
|
||||
$ 6to5 script.js
|
||||
```
|
||||
|
||||
If you have a file written using ES6+ and you just want to run it, `6to5-node`
|
||||
has you covered:
|
||||
|
||||
```sh
|
||||
$ 6to5-node script.js
|
||||
```
|
||||
|
||||
And it doesn't end here! To see all the ways you can use 6to5, check out the
|
||||
[Usage](http://6to5.github.io/usage.html) page.
|
||||
|
||||
## [Features](features.md)
|
||||
|
||||
- [Abstract references](features.md#abstract-references) ([experimental](experimental.md))
|
||||
- [Array comprehension](features.md#array-comprehension) ([experimental](experimental.md))
|
||||
- [Async functions](features.md#async-functions) ([experimental](experimental.md))
|
||||
- [Async generator functions](features.md#async-generator-functions) ([experimental](experimental.md))
|
||||
- [Arrow functions](features.md#arrow-functions)
|
||||
- [Classes](features.md#classes)
|
||||
- [Computed property names](features.md#computed-property-names)
|
||||
- [Constants](features.md#constants)
|
||||
- [Default parameters](features.md#default-parameters)
|
||||
- [Destructuring](features.md#destructuring)
|
||||
- [Exponentiation operator](features.md#exponentiation-operator) ([experimental](experimental.md))
|
||||
- [For-of](features.md#for-of)
|
||||
- [Generators](features.md#generators)
|
||||
- [Generator comprehension](features.md#generator-comprehension) ([experimental](experimental.md))
|
||||
- [Let scoping](features.md#let-scoping)
|
||||
- [Modules](features.md#modules)
|
||||
- [Numeric literals](features.md#numeric-literals)
|
||||
- [Object Rest/Spread](features.md#object-rest-spread) ([experimental](experimental.md))
|
||||
- [Property method assignment](features.md#property-method-assignment)
|
||||
- [Property name shorthand](features.md#property-name-shorthand)
|
||||
- [React/JSX](react.md)
|
||||
- [Rest parameters](features.md#rest-parameters)
|
||||
- [Spread](features.md#spread)
|
||||
- [Template literals](features.md#template-literals)
|
||||
- [Unicode regex](features.md#unicode-regex)
|
||||
See [6to5.github.io/docs](https://github.com/6to5/6to5.github.io/tree/master/docs).
|
||||
|
||||
257
doc/modules.md
257
doc/modules.md
@@ -1,257 +0,0 @@
|
||||
# Modules
|
||||
|
||||
## Usage
|
||||
|
||||
### CLI
|
||||
|
||||
```sh
|
||||
$ 6to5 --modules common script.js
|
||||
```
|
||||
|
||||
### Node
|
||||
|
||||
```javascript
|
||||
var to5 = require("6to5");
|
||||
to5.transform('import "foo";', { modules: "common" });
|
||||
```
|
||||
|
||||
## Formats
|
||||
|
||||
* [AMD](#amd)
|
||||
* [Common (Default)](#common-default)
|
||||
* [Ignore](#ignore)
|
||||
* [System](#system)
|
||||
* [UMD](#umd)
|
||||
|
||||
### Common
|
||||
|
||||
```sh
|
||||
$ 6to5 --modules common
|
||||
```
|
||||
|
||||
**In**
|
||||
|
||||
```javascript
|
||||
export default test;
|
||||
|
||||
export {test};
|
||||
export var test = 5;
|
||||
|
||||
import "foo";
|
||||
|
||||
import foo from "foo";
|
||||
import * as foo from "foo";
|
||||
|
||||
import {bar} from "foo";
|
||||
import {foo as bar} from "foo";
|
||||
```
|
||||
|
||||
**Out**
|
||||
|
||||
```javascript
|
||||
"use strict";
|
||||
|
||||
var _interopRequire = function (obj) {
|
||||
return obj && (obj["default"] || obj);
|
||||
};
|
||||
|
||||
exports = module.exports = test;
|
||||
|
||||
exports.test = test;
|
||||
var test = exports.test = 5;
|
||||
|
||||
require("foo");
|
||||
|
||||
var foo = _interopRequire(require("foo"));
|
||||
|
||||
var foo = require("foo");
|
||||
|
||||
var bar = require("foo").bar;
|
||||
var bar = require("foo").foo;
|
||||
```
|
||||
|
||||
### AMD
|
||||
|
||||
```sh
|
||||
$ 6to5 --modules amd
|
||||
```
|
||||
|
||||
**In**
|
||||
|
||||
```javascript
|
||||
import foo from "foo";
|
||||
|
||||
export function bar() {
|
||||
return foo("foobar");
|
||||
}
|
||||
```
|
||||
|
||||
**Out**
|
||||
|
||||
```javascript
|
||||
define(["exports", "foo"], function (exports, _foo) {
|
||||
"use strict";
|
||||
|
||||
var _interopRequire = function (obj) {
|
||||
return obj && (obj["default"] || obj);
|
||||
};
|
||||
|
||||
exports.bar = bar;
|
||||
var foo = _interopRequire(_foo);
|
||||
|
||||
function bar() {
|
||||
return foo("foobar");
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
You can optionally specify to include the module id (using the `--amd-module-id` argument):
|
||||
|
||||
```javascript
|
||||
define("filename", ["exports", "foo"], function (exports, _foo) {})
|
||||
```
|
||||
|
||||
### UMD
|
||||
|
||||
```sh
|
||||
$ 6to5 --modules umd
|
||||
```
|
||||
|
||||
**In**
|
||||
|
||||
```javascript
|
||||
import foo from "foo";
|
||||
|
||||
export function bar() {
|
||||
return foo("foobar");
|
||||
}
|
||||
```
|
||||
|
||||
**Out**
|
||||
|
||||
```javascript
|
||||
(function (factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["exports", "foo"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, require("foo"));
|
||||
}
|
||||
})(function (exports, _foo) {
|
||||
"use strict";
|
||||
|
||||
var _interopRequire = function (obj) {
|
||||
return obj && (obj["default"] || obj);
|
||||
};
|
||||
|
||||
exports.bar = bar;
|
||||
var foo = _interopRequire(_foo);
|
||||
|
||||
function bar() {
|
||||
return foo("foobar");
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
### Ignore
|
||||
|
||||
```sh
|
||||
$ 6to5 --modules ignore
|
||||
```
|
||||
|
||||
**In**
|
||||
|
||||
```javascript
|
||||
import foo from "foo";
|
||||
|
||||
export function bar() {
|
||||
return foo("foobar");
|
||||
}
|
||||
```
|
||||
|
||||
**Out**
|
||||
|
||||
```javascript
|
||||
function bar() {
|
||||
return foo("foobar");
|
||||
}
|
||||
```
|
||||
|
||||
### System
|
||||
|
||||
```sh
|
||||
$ 6to5 --modules system
|
||||
```
|
||||
|
||||
**In**
|
||||
|
||||
```javascript
|
||||
import foo from "foo";
|
||||
|
||||
export function bar() {
|
||||
return foo("foobar");
|
||||
}
|
||||
```
|
||||
|
||||
**Out**
|
||||
|
||||
```javascript
|
||||
System.register("bar", ["foo"], function (_export) {
|
||||
"use strict";
|
||||
|
||||
var __moduleName = "bar";
|
||||
|
||||
var foo;
|
||||
function bar() {
|
||||
return foo("foobar");
|
||||
}
|
||||
return {
|
||||
setters: [function (m) {
|
||||
foo = m.default;
|
||||
}],
|
||||
execute: function () {
|
||||
_export("bar", bar);
|
||||
}
|
||||
};
|
||||
});
|
||||
```
|
||||
|
||||
## Custom
|
||||
|
||||
You can alternatively specify module names instead of one of the built-in types.
|
||||
|
||||
```sh
|
||||
$ 6to5 --modules custom-module-formatter
|
||||
```
|
||||
|
||||
**node_modules/custom-module-formatter/index.js**
|
||||
|
||||
```javascript
|
||||
module.exports = ModuleFormatter;
|
||||
|
||||
function ModuleFormatter() {
|
||||
|
||||
}
|
||||
|
||||
ModuleFormatter.prototype.transform = function (ast) {
|
||||
// this is ran after all transformers have had their turn at modifying the ast
|
||||
// feel free to modify this however
|
||||
};
|
||||
|
||||
ModuleFormatter.prototype.importDeclaration = function (node, nodes) {
|
||||
// node is an ImportDeclaration
|
||||
};
|
||||
|
||||
ModuleFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
||||
// specifier is an ImportSpecifier
|
||||
// node is an ImportDeclaration
|
||||
};
|
||||
|
||||
ModuleFormatter.prototype.exportDeclaration = function (node, nodes) {
|
||||
// node is an ExportDeclaration
|
||||
};
|
||||
|
||||
ModuleFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
|
||||
// specifier is an ExportSpecifier
|
||||
// node is an ExportDeclaration
|
||||
};
|
||||
```
|
||||
@@ -1,57 +0,0 @@
|
||||
# Optional runtime
|
||||
|
||||
6to5 has a few helper functions that'll be placed at the top of the generated
|
||||
code if needed so it's not inlined multiple times throughout that file. This may
|
||||
become an issue if you have multiple files, especially when you're sending them
|
||||
to the browser. gzip alleviates most of this concern but it's still not ideal.
|
||||
|
||||
You can tell 6to5 to not place any declarations at the top of your files and
|
||||
instead just point them to a reference contained within the runtime.
|
||||
|
||||
Simply use the following option if you're using the [Node API](usage.md#node):
|
||||
|
||||
```javascript
|
||||
{
|
||||
runtime: true
|
||||
}
|
||||
```
|
||||
|
||||
or the following flag if you're using the [CLI](usage.md#cli):
|
||||
|
||||
```ssh
|
||||
$ 6to5 --runtime
|
||||
```
|
||||
|
||||
Then just include the runtime before your generated code.
|
||||
|
||||
## Getting the runtime
|
||||
|
||||
You can get the runtime via either:
|
||||
|
||||
```sh
|
||||
$ 6to5-runtime
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```javascript
|
||||
require("6to5").runtime();
|
||||
```
|
||||
|
||||
or from an npm release in `runtime.js` from the 6to5 directory.
|
||||
|
||||
## Customising namespace
|
||||
|
||||
You can also customise the runtime namespace by passing an optional namespace
|
||||
argument:
|
||||
|
||||
```javascript
|
||||
require("6to5").runtime("myCustomNamespace");
|
||||
```
|
||||
|
||||
```sh
|
||||
$ 6to5-runtime myCustomNamespace
|
||||
```
|
||||
|
||||
See [Options - runtime](usage.md#options) for documentation on changing the
|
||||
reference in generated code.
|
||||
@@ -1,120 +0,0 @@
|
||||
# Playground
|
||||
|
||||
Playground is a proving ground for **possible** ES7 proposals.
|
||||
|
||||
**NOTE: These features are in no way endorsed by Ecma International and are not a part of ES6. They might become a part of ECMAScript in the future.**
|
||||
|
||||
## Usage
|
||||
|
||||
$ 6to5 --playground
|
||||
|
||||
```javascript
|
||||
to5.transform("code", { playground: true });
|
||||
```
|
||||
|
||||
**NOTE:** Enabling `playground` also enables [experimental support](experimental.md).
|
||||
|
||||
## Features
|
||||
|
||||
* [Memoization operator](#memoization-operator)
|
||||
* [Method binding](#method-binding)
|
||||
* [Object getter memoization](#object-getter-memoization)
|
||||
* [This shorthand](#this-shorthand)
|
||||
|
||||
### Memoization assignment operator
|
||||
|
||||
```javascript
|
||||
var obj = {};
|
||||
obj.x ?= 2;
|
||||
obj.x; // 2
|
||||
|
||||
obj = { x: 1 };
|
||||
obj.x ?= 2;
|
||||
obj.x; // 1
|
||||
|
||||
obj = { x: undefined }
|
||||
obj.x ?= 2;
|
||||
obj.x; // undefined
|
||||
```
|
||||
|
||||
```javascript
|
||||
var obj = {};
|
||||
obj.x ?= 2;
|
||||
```
|
||||
|
||||
equivalent to:
|
||||
|
||||
```javascript
|
||||
var obj = {};
|
||||
if (!Object.prototype.hasOwnProperty.call(obj, "x")) obj.x = 2;
|
||||
```
|
||||
|
||||
### Method binding
|
||||
|
||||
```javascript
|
||||
var fn = obj#method;
|
||||
var fn = obj#method("foob");
|
||||
|
||||
["foo", "bar"].map(#toUpperCase); // ["FOO", "BAR"]
|
||||
[1.1234, 23.53245, 3].map(#toFixed(2)); // ["1.12", "23.53", "3.00"]
|
||||
```
|
||||
|
||||
equivalent to:
|
||||
|
||||
```javascript
|
||||
var fn = obj.method.bind(obj);
|
||||
var fn = obj.method.bind(obj, "foob");
|
||||
|
||||
["foo", "bar"].map(function (val) { return val.toUpperCase(); });
|
||||
[1.1234, 23.53245, 3].map(function (val) { return val.toFixed(2); });
|
||||
```
|
||||
|
||||
### Object getter memoization
|
||||
|
||||
```javascript
|
||||
var foo = {
|
||||
memo bar() {
|
||||
return complex();
|
||||
}
|
||||
};
|
||||
|
||||
class Foo {
|
||||
memo bar() {
|
||||
return complex();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
equivalent to
|
||||
|
||||
```javascript
|
||||
var foo = {
|
||||
get bar() {
|
||||
if (this._barRan) return this._bar;
|
||||
this._barRan = true;
|
||||
return this._bar = complex();
|
||||
}
|
||||
};
|
||||
|
||||
class Foo {
|
||||
get bar() {
|
||||
if (this._barRan) return this._bar;
|
||||
this._barRan = true;
|
||||
return this._bar = complex();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**NOTE:** Memoised functions will return the result of the **first** execution, regardless of arguments.
|
||||
|
||||
### This shorthand
|
||||
|
||||
```javascript
|
||||
@foo
|
||||
```
|
||||
|
||||
equivalent to
|
||||
|
||||
```javascirpt
|
||||
this.foo
|
||||
```
|
||||
@@ -1,33 +0,0 @@
|
||||
# Plugins
|
||||
|
||||
## Build systems
|
||||
|
||||
- [Broccoli](https://github.com/6to5/broccoli-6to5-transpiler)
|
||||
- [Browserify](https://github.com/6to5/6to5ify)
|
||||
- [Brunch](https://github.com/6to5/6to5-brunch)
|
||||
- [Duo](https://github.com/6to5/duo6to5)
|
||||
- [Gobble](https://github.com/6to5/gobble-6to5)
|
||||
- [Gulp](https://github.com/6to5/gulp-6to5)
|
||||
- [Grunt](https://github.com/6to5/grunt-6to5)
|
||||
- [Sprockets](https://github.com/josh/sprockets-es6) or via [Browserify](https://github.com/6to5/6to5-rails)
|
||||
- [webpack](https://github.com/6to5/6to5-loader)
|
||||
|
||||
## Integrations
|
||||
|
||||
- [Isparta](https://github.com/douglasduteil/isparta) - Code coverage with `karma` and `instanbul` using 6to5
|
||||
- [JSXHint](https://github.com/STRML/JSXHint) - A wrapper around JSHint to allow linting of JSX files
|
||||
|
||||
## Bridges
|
||||
|
||||
- [Ruby](https://github.com/6to5/6to5-ruby)
|
||||
|
||||
## Testing
|
||||
|
||||
- [Jest](https://github.com/6to5/6to5-jest)
|
||||
- [Karma](https://github.com/6to5/karma-6to5-preprocessor)
|
||||
- [Mocha](https://github.com/6to5/6to5-mocha)
|
||||
|
||||
## Misc
|
||||
|
||||
- [Connect](https://github.com/6to5/6to5-connect)
|
||||
- [Jade](https://github.com/6to5/jade-6to5)
|
||||
@@ -1,34 +0,0 @@
|
||||
# Polyfill
|
||||
|
||||
6to5 includes a polyfill that includes a custom
|
||||
[regenerator runtime](https://github.com/facebook/regenerator/blob/master/runtime.js) and
|
||||
[core.js](https://github.com/zloirock/core-js).
|
||||
|
||||
This will emulate a full ES6 environment. This polyfill is automatically loaded
|
||||
when using [6to5-node](usage.md#node) and [6to5/register](usage.md#register-hook).
|
||||
|
||||
## Usage
|
||||
|
||||
### Node/Browserify
|
||||
|
||||
You need to include the polyfill require at the top the **entry point** to your
|
||||
application.
|
||||
|
||||
```javascript
|
||||
require("6to5/polyfill");
|
||||
```
|
||||
|
||||
Fortunately, this is automatically loaded when using:
|
||||
|
||||
```javascript
|
||||
require("6to5/register");
|
||||
```
|
||||
|
||||
### Browser
|
||||
|
||||
Available from the `browser-polyfill.js` file within the 6to5 directory of an
|
||||
npm release. This needs to be included **before** all your compiled 6to5 code.
|
||||
You can either prepend it to your compiled code or include it in a `<script>`
|
||||
before it.
|
||||
|
||||
**NOTE:** Do not `require` this via browserify etc, use `6to5/polyfill`.
|
||||
17
doc/react.md
17
doc/react.md
@@ -1,17 +0,0 @@
|
||||
# React/JSX
|
||||
|
||||
6to5 has built-in support for React v0.12. Tags are automatically transformed to
|
||||
their equivalent `React.createElement(...)` and `displayName` is automatically
|
||||
inferred and added to all `React.createClass` calls.
|
||||
|
||||
## Blacklist
|
||||
|
||||
To disable this behaviour add `react` to your blacklist:
|
||||
|
||||
```javascript
|
||||
to5.transform("code", { blacklist: ["react"] });
|
||||
```
|
||||
|
||||
```sh
|
||||
$ 6to5 --blacklist react
|
||||
```
|
||||
259
doc/usage.md
259
doc/usage.md
@@ -1,259 +0,0 @@
|
||||
# Usage
|
||||
|
||||
## CLI
|
||||
|
||||
Compile the file `script.js` and output it to stdout.
|
||||
|
||||
```sh
|
||||
$ 6to5 script.js
|
||||
```
|
||||
|
||||
Compile the file `script.js` and output it to `script-compiled.js`.
|
||||
|
||||
```sh
|
||||
$ 6to5 script.js --out-file script-compiled.js
|
||||
```
|
||||
|
||||
Compile the file `script.js` and output it to `script-compiled.js` and save a
|
||||
source map to `script-compiled.js.map`.
|
||||
|
||||
```sh
|
||||
$ 6to5 script.js --source-maps --out-file script-compiled.js
|
||||
```
|
||||
|
||||
Compile the file `script.js` and output it to `script-compiled.js` with a source
|
||||
map embedded in a comment at the bottom.
|
||||
|
||||
```sh
|
||||
$ 6to5 script.js --source-maps-inline --out-file script-compiled.js
|
||||
```
|
||||
|
||||
Compile the entire `src` directory and output it to the `lib` directory.
|
||||
|
||||
```sh
|
||||
$ 6to5 src --out-dir lib
|
||||
```
|
||||
|
||||
Compile the entire `src` directory and output it to the one concatenated file.
|
||||
|
||||
```sh
|
||||
$ 6to5 src --out-file script-compiled.js
|
||||
```
|
||||
|
||||
Pipe a file in via stdin and output it to `script-compiled.js`
|
||||
|
||||
```sh
|
||||
$ 6to5 --out-file script-compiled.js < script.js
|
||||
```
|
||||
|
||||
### Node
|
||||
|
||||
Launch a repl.
|
||||
|
||||
```sh
|
||||
$ 6to5-node
|
||||
```
|
||||
|
||||
Evaluate code.
|
||||
|
||||
```sh
|
||||
$ 6to5-node -e "class Test { }"
|
||||
```
|
||||
|
||||
Compile and run `test.js`.
|
||||
|
||||
```sh
|
||||
$ 6to5-node test
|
||||
```
|
||||
|
||||
## Node
|
||||
|
||||
```javascript
|
||||
var to5 = require("6to5");
|
||||
```
|
||||
|
||||
### to5.transform(code, [opts]);
|
||||
|
||||
```javascript
|
||||
var result = to5.transform("code();", options);
|
||||
result.code;
|
||||
result.map;
|
||||
result.ast;
|
||||
```
|
||||
|
||||
### to5.transformFileSync(filename, [opts])
|
||||
|
||||
```javascript
|
||||
to5.transformFileSync("filename.js", options).code;
|
||||
```
|
||||
|
||||
### to5.transformFile(filename, [opts], callback)
|
||||
|
||||
```javascript
|
||||
to5.transformFile("filename.js", options, function (err, result) {
|
||||
result.code;
|
||||
});
|
||||
```
|
||||
|
||||
### to5.transform.fromAst(ast, [code], [opts])
|
||||
|
||||
```javascript
|
||||
var result = to5.transform.fromAst(ast, "var a = 2;", opts);
|
||||
result.code;
|
||||
result.map;
|
||||
result.ast;
|
||||
```
|
||||
|
||||
#### Options
|
||||
|
||||
```javascript
|
||||
{
|
||||
// Filename for use in errors etc.
|
||||
// Default: "unknown"
|
||||
filename: "filename",
|
||||
|
||||
// Filename relative to `sourceRoot`
|
||||
// Default: `filename` option.
|
||||
filenameRelative: "",
|
||||
|
||||
// List of transformers to EXCLUDE.
|
||||
// Run `6to5 --help` to see a full list of transformers.
|
||||
blacklist: [],
|
||||
|
||||
// List of transformers to ONLY use.
|
||||
// Run `6to5 --help` to see a full list of transformers.
|
||||
whitelist: [],
|
||||
|
||||
// Module formatter to use
|
||||
// Run `6to5 --help` to see a full list of module formatters.
|
||||
// Default: "common"
|
||||
modules: "common",
|
||||
|
||||
// If truthy, adds a `map` property to returned output.
|
||||
// If set to "inline", a comment with a sourceMappingURL directive is added to
|
||||
// the bottom of the returned code.
|
||||
// Default: false
|
||||
sourceMap: true,
|
||||
|
||||
// Set `file` on returned source map.
|
||||
// Default: `filenameRelative` option.
|
||||
sourceMapName: "filename",
|
||||
|
||||
// Set `sources[0]` on returned source map.
|
||||
// Default: `filenameRelative` option.
|
||||
sourceFileName: "filename",
|
||||
|
||||
// The root from which all sources are relative
|
||||
// Default: `moduleRoot` option.
|
||||
sourceRoot: "assets/scripts",
|
||||
|
||||
// Optional prefix for the AMD module formatter that will be prepend to the
|
||||
// filename on module definitions
|
||||
// Default: `sourceRoot` option.
|
||||
moduleRoot: "my-app",
|
||||
|
||||
// If truthy, insert an explicit id for each defined AMD/System module.
|
||||
// By default, AMD/System modules are anonymous.
|
||||
// Default: false
|
||||
moduleIds: true,
|
||||
|
||||
// Optionally replace all 6to5 helper declarations with a referenece to this
|
||||
// variable. If set to `true` then the default namespace is used "to5Runtime".
|
||||
// Default: false
|
||||
runtime: true,
|
||||
|
||||
// Enable support for experimental ES7 features
|
||||
// Default: false
|
||||
experimental: true,
|
||||
|
||||
// Set this to `false` if you don't want the transformed AST in the returned
|
||||
// result
|
||||
// Default: true
|
||||
ast: true,
|
||||
|
||||
// Set this to `false` if you don't want the transformed code in the returned
|
||||
// result
|
||||
// Default: true
|
||||
code: true,
|
||||
|
||||
format: {
|
||||
// Output comments in generated output
|
||||
// Default: true
|
||||
comments: true,
|
||||
|
||||
// Do not include superfluous whitespace characters and line terminators
|
||||
// Default: false
|
||||
compact: false,
|
||||
|
||||
indent: {
|
||||
// Preserve parentheses in new expressions that have no arguments
|
||||
// Default: true
|
||||
parentheses: true,
|
||||
|
||||
// Adjust the indentation of multiline comments to keep asterisks vertically aligned
|
||||
// Default: true
|
||||
adjustMultilineComment: true,
|
||||
|
||||
// Indent string
|
||||
// Default: " "
|
||||
style: " ",
|
||||
|
||||
// Base indent level
|
||||
// Default: 0
|
||||
base: 0
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Require hook
|
||||
|
||||
All subsequent files required by node with the extensions `.es6` and `.js` will
|
||||
be transformed by 6to5. The polyfill specified in [Polyfill](polyfill.md) is
|
||||
also required; but this is automatically loaded when using:
|
||||
|
||||
Source maps are automatically configured so if any errors a thrown then line
|
||||
number info is mapped and you'll get the correct source location.
|
||||
|
||||
```javascript
|
||||
require("6to5/register");
|
||||
```
|
||||
|
||||
**NOTE:** By default all requires to `node_modules` will be ignored. You can
|
||||
override this by passing an ignore regex via:
|
||||
|
||||
```javascript
|
||||
require("6to5/register")({
|
||||
// This will override `node_modules` ignoring - you can alternatively pass
|
||||
// a regex
|
||||
ignore: false
|
||||
});
|
||||
```
|
||||
|
||||
**NOTE:** Using 6to5-node or `require("6to5/register")`, 6to5 will check what features are available in the current environment and blacklists them if they're supported.
|
||||
|
||||
#### Options
|
||||
|
||||
```javascript
|
||||
require("6to5/register")({
|
||||
// Optional ignore regex - if any filenames **do** match this regex then they
|
||||
// aren't compiled
|
||||
ignore: /regex/,
|
||||
|
||||
// Optional only regex - if any filenames **don't** match this regex then they
|
||||
// aren't compiled
|
||||
only: /my_es6_folder/,
|
||||
|
||||
// See options above for usage
|
||||
whitelist: [],
|
||||
blacklist: [],
|
||||
|
||||
// This will remove the currently hooked extensions of .es6 and .js so you'll
|
||||
// have to add them back if you want them to be used again.
|
||||
extensions: [".js", ".es6"],
|
||||
|
||||
// Enables `roadrunner` cache that will save to a `.roadrunner.json` file in your cwd
|
||||
// Do not check this into git as it's user-specific
|
||||
cache: true
|
||||
});
|
||||
```
|
||||
@@ -1,5 +1,7 @@
|
||||
var transform = module.exports = require("./transformation/transform");
|
||||
|
||||
transform.version = require("../../package").version;
|
||||
|
||||
transform.transform = transform;
|
||||
|
||||
transform.run = function (code, opts) {
|
||||
@@ -34,7 +36,7 @@ transform.load = function (url, callback, opts, hold) {
|
||||
|
||||
var runScripts = function () {
|
||||
var scripts = [];
|
||||
var types = ["text/ecmascript-6", "text/6to5"];
|
||||
var types = ["text/ecmascript-6", "text/6to5", "module"];
|
||||
var index = 0;
|
||||
|
||||
var exec = function () {
|
||||
@@ -60,8 +62,9 @@ var runScripts = function () {
|
||||
}
|
||||
};
|
||||
|
||||
var _scripts = global.document.getElementsByTagName("script");
|
||||
for (var i in _scripts) {
|
||||
var _scripts = global.document .getElementsByTagName("script");
|
||||
|
||||
for (var i = 0; i < _scripts.length; ++i) {
|
||||
var _script = _scripts[i];
|
||||
if (types.indexOf(_script.type) >= 0) scripts.push(_script);
|
||||
}
|
||||
|
||||
@@ -10,14 +10,17 @@ var t = require("./types");
|
||||
var _ = require("lodash");
|
||||
|
||||
function File(opts) {
|
||||
this.opts = File.normaliseOptions(opts);
|
||||
this.transformers = this.getTransformers();
|
||||
this.uids = {};
|
||||
this.ast = {};
|
||||
this.dynamicImports = [];
|
||||
this.dynamicImportIds = {};
|
||||
this.opts = File.normaliseOptions(opts);
|
||||
this.transformers = this.getTransformers();
|
||||
this.uids = {};
|
||||
this.ast = {};
|
||||
}
|
||||
|
||||
File.declarations = [
|
||||
File.helpers = [
|
||||
"inherits",
|
||||
"defaults",
|
||||
"prototype-properties",
|
||||
"apply-constructor",
|
||||
"tagged-template-literal",
|
||||
@@ -27,32 +30,41 @@ File.declarations = [
|
||||
"object-without-properties",
|
||||
"has-own",
|
||||
"slice",
|
||||
"bind",
|
||||
"define-property",
|
||||
"async-to-generator"
|
||||
"async-to-generator",
|
||||
"interop-require-wildcard",
|
||||
"typeof",
|
||||
"extends",
|
||||
"get"
|
||||
];
|
||||
|
||||
File.excludeDeclarationsFromRuntime = [
|
||||
"async-to-generator"
|
||||
File.excludeHelpersFromRuntime = [
|
||||
"async-to-generator",
|
||||
"typeof"
|
||||
];
|
||||
|
||||
File.normaliseOptions = function (opts) {
|
||||
opts = _.cloneDeep(opts || {});
|
||||
|
||||
_.defaults(opts, {
|
||||
experimental: false,
|
||||
playground: false,
|
||||
whitespace: true,
|
||||
moduleIds: opts.amdModuleIds || false,
|
||||
blacklist: [],
|
||||
whitelist: [],
|
||||
sourceMap: false,
|
||||
optional: [],
|
||||
comments: true,
|
||||
filename: "unknown",
|
||||
modules: "common",
|
||||
runtime: false,
|
||||
code: true,
|
||||
ast: true
|
||||
keepModuleIdExtensions: false,
|
||||
includeRegenerator: false,
|
||||
experimental: false,
|
||||
reactCompat: false,
|
||||
playground: false,
|
||||
whitespace: true,
|
||||
moduleIds: opts.amdModuleIds || false,
|
||||
blacklist: [],
|
||||
whitelist: [],
|
||||
sourceMap: false,
|
||||
optional: [],
|
||||
comments: true,
|
||||
filename: "unknown",
|
||||
modules: "common",
|
||||
runtime: false,
|
||||
code: true,
|
||||
ast: true
|
||||
});
|
||||
|
||||
// normalise windows path separators to unix
|
||||
@@ -96,25 +108,30 @@ File.normaliseOptions = function (opts) {
|
||||
File.prototype.getTransformers = function () {
|
||||
var file = this;
|
||||
var transformers = [];
|
||||
var secondPassTransformers = [];
|
||||
|
||||
_.each(transform.transformers, function (transformer) {
|
||||
if (transformer.canRun(file)) {
|
||||
transformers.push(transformer);
|
||||
|
||||
if (transformer.secondPass) {
|
||||
secondPassTransformers.push(transformer);
|
||||
}
|
||||
|
||||
if (transformer.manipulateOptions) {
|
||||
transformer.manipulateOptions(file.opts, file);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return transformers;
|
||||
return transformers.concat(secondPassTransformers);
|
||||
};
|
||||
|
||||
File.prototype.toArray = function (node, i) {
|
||||
if (t.isArrayExpression(node)) {
|
||||
return node;
|
||||
} else if (t.isIdentifier(node) && node.name === "arguments") {
|
||||
return t.callExpression(t.memberExpression(this.addDeclaration("slice"), t.identifier("call")), [node]);
|
||||
return t.callExpression(t.memberExpression(this.addHelper("slice"), t.identifier("call")), [node]);
|
||||
} else {
|
||||
var declarationName = "to-array";
|
||||
var args = [node];
|
||||
@@ -122,7 +139,7 @@ File.prototype.toArray = function (node, i) {
|
||||
args.push(t.literal(i));
|
||||
declarationName = "sliced-to-array";
|
||||
}
|
||||
return t.callExpression(this.addDeclaration(declarationName), args);
|
||||
return t.callExpression(this.addHelper(declarationName), args);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -153,14 +170,24 @@ File.prototype.parseShebang = function (code) {
|
||||
return code;
|
||||
};
|
||||
|
||||
File.prototype.addImport = function (id, source) {
|
||||
var specifiers = [t.importSpecifier(t.identifier("default"), id)];
|
||||
var declar = t.importDeclaration(specifiers, t.literal(source));
|
||||
this.ast.program.body.unshift(declar);
|
||||
File.prototype.addImport = function (source, name) {
|
||||
name = name || source;
|
||||
var id = this.dynamicImportIds[name];
|
||||
|
||||
if (!id) {
|
||||
id = this.dynamicImportIds[name] = this.generateUidIdentifier(name);
|
||||
|
||||
var specifiers = [t.importSpecifier(t.identifier("default"), id)];
|
||||
var declar = t.importDeclaration(specifiers, t.literal(source));
|
||||
declar._blockHoist = 3;
|
||||
this.dynamicImports.push(declar);
|
||||
}
|
||||
|
||||
return id;
|
||||
};
|
||||
|
||||
File.prototype.addDeclaration = function (name) {
|
||||
if (!_.contains(File.declarations, name)) {
|
||||
File.prototype.addHelper = function (name) {
|
||||
if (!_.contains(File.helpers, name)) {
|
||||
throw new ReferenceError("unknown declaration " + name);
|
||||
}
|
||||
|
||||
@@ -171,7 +198,7 @@ File.prototype.addDeclaration = function (name) {
|
||||
|
||||
var ref;
|
||||
var runtimeNamespace = this.opts.runtime;
|
||||
if (runtimeNamespace && !_.contains(File.excludeDeclarationsFromRuntime, name)) {
|
||||
if (runtimeNamespace && !_.contains(File.excludeHelpersFromRuntime, name)) {
|
||||
name = t.identifier(t.toIdentifier(name));
|
||||
return t.memberExpression(t.identifier(runtimeNamespace), name);
|
||||
} else {
|
||||
@@ -265,7 +292,7 @@ File.prototype.generate = function () {
|
||||
};
|
||||
|
||||
File.prototype.generateUid = function (name, scope) {
|
||||
name = t.toIdentifier(name);
|
||||
name = t.toIdentifier(name).replace(/^_+/, "");
|
||||
|
||||
scope = scope || this.scope;
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ Buffer.prototype.space = function () {
|
||||
Buffer.prototype.removeLast = function (cha) {
|
||||
if (!this.isLast(cha)) return;
|
||||
|
||||
this.buf = this.buf.slice(0, -1);
|
||||
this.buf = this.buf.substr(0, this.buf.length - 1);
|
||||
this.position.unshift(cha);
|
||||
};
|
||||
|
||||
@@ -77,10 +77,9 @@ Buffer.prototype.newline = function (i, removeLast) {
|
||||
if (this.endsWith("{\n")) i--;
|
||||
if (this.endsWith(util.repeat(i, "\n"))) return;
|
||||
|
||||
var self = this;
|
||||
_.times(i, function () {
|
||||
self.newline(null, removeLast);
|
||||
});
|
||||
for (var j = 0; j < i; j++) {
|
||||
this.newline(null, removeLast);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -115,13 +114,18 @@ Buffer.prototype._push = function (str) {
|
||||
};
|
||||
|
||||
Buffer.prototype.endsWith = function (str) {
|
||||
return this.buf.slice(-str.length) === str;
|
||||
var d = this.buf.length - str.length;
|
||||
return d >= 0 && this.buf.lastIndexOf(str) === d;
|
||||
};
|
||||
|
||||
Buffer.prototype.isLast = function (cha, trimRight) {
|
||||
var buf = this.buf;
|
||||
if (trimRight) buf = util.trimRight(buf);
|
||||
var last = buf[buf.length - 1];
|
||||
|
||||
var chars = [].concat(cha);
|
||||
return _.contains(chars, _.last(buf));
|
||||
if (Array.isArray(cha)) {
|
||||
return _.contains(cha, last);
|
||||
} else {
|
||||
return cha === last;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -125,7 +125,7 @@ CodeGenerator.prototype.print = function (node, parent, opts) {
|
||||
|
||||
var lines = 0;
|
||||
|
||||
if (node.start != null) {
|
||||
if (node.start != null && !node._ignoreUserWhitespace) {
|
||||
// user node
|
||||
if (leading) {
|
||||
lines = self.whitespace.getNewlinesBefore(node);
|
||||
@@ -148,11 +148,11 @@ CodeGenerator.prototype.print = function (node, parent, opts) {
|
||||
};
|
||||
|
||||
if (this[node.type]) {
|
||||
var needsCommentParens = t.isExpression(node) && node.leadingComments && node.leadingComments.length;
|
||||
var needsParens = needsCommentParens || n.needsParens(node, parent);
|
||||
var needsNoLineTermParens = n.needsParensNoLineTerminator(node, parent);
|
||||
var needsParens = needsNoLineTermParens || n.needsParens(node, parent);
|
||||
|
||||
if (needsParens) this.push("(");
|
||||
if (needsCommentParens) this.indent();
|
||||
if (needsNoLineTermParens) this.indent();
|
||||
|
||||
this.printLeadingComments(node, parent);
|
||||
|
||||
@@ -163,7 +163,7 @@ CodeGenerator.prototype.print = function (node, parent, opts) {
|
||||
|
||||
this[node.type](node, this.buildPrint(node), parent);
|
||||
|
||||
if (needsCommentParens) {
|
||||
if (needsNoLineTermParens) {
|
||||
this.newline();
|
||||
this.dedent();
|
||||
}
|
||||
@@ -274,14 +274,21 @@ CodeGenerator.prototype._printComments = function (comments) {
|
||||
var self = this;
|
||||
|
||||
_.each(comments, function (comment) {
|
||||
var skip = false;
|
||||
|
||||
// find the original comment in the ast and set it as displayed
|
||||
_.each(self.ast.comments, function (origComment) {
|
||||
if (origComment.start === comment.start) {
|
||||
// comment has already been output
|
||||
if (origComment._displayed) skip = true;
|
||||
|
||||
origComment._displayed = true;
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
if (skip) return;
|
||||
|
||||
// whitespace before
|
||||
self.newline(self.whitespace.getNewlinesBefore(comment));
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.UnaryExpression = function (node, print) {
|
||||
var hasSpace = /[a-z]$/.test(node.operator);
|
||||
@@ -56,8 +57,28 @@ exports.ThisExpression = function () {
|
||||
|
||||
exports.CallExpression = function (node, print) {
|
||||
print(node.callee);
|
||||
|
||||
this.push("(");
|
||||
print.join(node.arguments, { separator: ", " });
|
||||
|
||||
var separator = ",";
|
||||
|
||||
if (node._prettyCall) {
|
||||
separator += "\n";
|
||||
this.newline();
|
||||
this.indent();
|
||||
} else {
|
||||
separator += " ";
|
||||
}
|
||||
|
||||
print.join(node.arguments, {
|
||||
separator: separator
|
||||
});
|
||||
|
||||
if (node._prettyCall) {
|
||||
this.newline();
|
||||
this.dedent();
|
||||
}
|
||||
|
||||
this.push(")");
|
||||
};
|
||||
|
||||
@@ -86,6 +107,7 @@ exports.ExpressionStatement = function (node, print) {
|
||||
|
||||
exports.BinaryExpression =
|
||||
exports.LogicalExpression =
|
||||
exports.AssignmentPattern =
|
||||
exports.AssignmentExpression = function (node, print) {
|
||||
print(node.left);
|
||||
this.push(" " + node.operator + " ");
|
||||
@@ -98,7 +120,16 @@ exports.MemberExpression = function (node, print) {
|
||||
var obj = node.object;
|
||||
print(obj);
|
||||
|
||||
if (node.computed) {
|
||||
if (!node.computed && t.isMemberExpression(node.property)) {
|
||||
throw new TypeError("Got a MemberExpression for MemberExpression property");
|
||||
}
|
||||
|
||||
var computed = node.computed;
|
||||
if (t.isLiteral(node.property) && _.isNumber(node.property.value)) {
|
||||
computed = true;
|
||||
}
|
||||
|
||||
if (computed) {
|
||||
this.push("[");
|
||||
print(node.property);
|
||||
this.push("]");
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
var t = require("../../types");
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
|
||||
exports.WithStatement = function (node, print) {
|
||||
this.keyword("with");
|
||||
@@ -106,7 +107,16 @@ exports.TryStatement = function (node, print) {
|
||||
this.keyword("try");
|
||||
print(node.block);
|
||||
this.space();
|
||||
print(node.handler);
|
||||
|
||||
// Esprima bug puts the catch clause in a `handlers` array.
|
||||
// see https://code.google.com/p/esprima/issues/detail?id=433
|
||||
// We run into this from regenerator generated ast.
|
||||
if (node.handlers) {
|
||||
print(node.handlers[0]);
|
||||
} else {
|
||||
print(node.handler);
|
||||
}
|
||||
|
||||
if (node.finalizer) {
|
||||
this.space();
|
||||
this.push("finally ");
|
||||
@@ -157,7 +167,24 @@ exports.DebuggerStatement = function () {
|
||||
exports.VariableDeclaration = function (node, print, parent) {
|
||||
this.push(node.kind + " ");
|
||||
|
||||
print.join(node.declarations, { separator: ", " });
|
||||
var inits = 0;
|
||||
var noInits = 0;
|
||||
for (var i in node.declarations) {
|
||||
if (node.declarations[i].init) {
|
||||
inits++;
|
||||
} else {
|
||||
noInits++;
|
||||
}
|
||||
}
|
||||
|
||||
var sep = ",";
|
||||
if (inits > noInits) { // more inits than noinits
|
||||
sep += "\n" + util.repeat(node.kind.length + 1);
|
||||
} else {
|
||||
sep += " ";
|
||||
}
|
||||
|
||||
print.join(node.declarations, { separator: sep });
|
||||
|
||||
if (!t.isFor(parent)) {
|
||||
this.semicolon();
|
||||
|
||||
@@ -6,14 +6,19 @@ var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
var find = function (obj, node, parent) {
|
||||
if (!obj) return;
|
||||
var result;
|
||||
|
||||
_.each(obj, function (fn, type) {
|
||||
var types = Object.keys(obj);
|
||||
for (var i = 0; i < types.length; i++) {
|
||||
var type = types[i];
|
||||
|
||||
if (t["is" + type](node)) {
|
||||
var fn = obj[type];
|
||||
result = fn(node, parent);
|
||||
if (result != null) return false;
|
||||
if (result != null) break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
@@ -61,19 +66,51 @@ Node.prototype.needsParens = function () {
|
||||
if (!parent) return false;
|
||||
|
||||
if (t.isNewExpression(parent) && parent.callee === node) {
|
||||
return t.isCallExpression(node) || _.some(node, function (val) {
|
||||
if (t.isCallExpression(node)) return true;
|
||||
|
||||
var hasCall = _.some(node, function (val) {
|
||||
return t.isCallExpression(val);
|
||||
});
|
||||
if (hasCall) return true;
|
||||
}
|
||||
|
||||
return find(parens, node, parent);
|
||||
};
|
||||
|
||||
Node.prototype.needsParensNoLineTerminator = function () {
|
||||
var parent = this.parent;
|
||||
var node = this.node;
|
||||
|
||||
if (!parent) return false;
|
||||
|
||||
// no comments
|
||||
if (!node.leadingComments || !node.leadingComments.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (t.isYieldExpression(parent) || t.isAwaitExpression(parent)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (t.isContinueStatement(parent) || t.isBreakStatement(parent) ||
|
||||
t.isReturnStatement(parent) || t.isThrowStatement(parent)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
_.each(Node.prototype, function (fn, key) {
|
||||
Node[key] = function (node, parent) {
|
||||
var n = new Node(node, parent);
|
||||
|
||||
var args = _.toArray(arguments).slice(2);
|
||||
// Avoid leaking arguments to prevent deoptimization
|
||||
var skipCount = 2;
|
||||
var args = new Array(arguments.length - skipCount);
|
||||
for (var i = 0; i < args.length; i++) {
|
||||
args[i] = arguments[i + 2];
|
||||
}
|
||||
|
||||
return n[key].apply(n, args);
|
||||
};
|
||||
});
|
||||
|
||||
@@ -13,15 +13,37 @@ _.each([
|
||||
["<", ">", "<=", ">=", "in", "instanceof"],
|
||||
[">>", "<<", ">>>"],
|
||||
["+", "-"],
|
||||
["*", "/", "%"]
|
||||
["*", "/", "%"],
|
||||
["**"]
|
||||
], function (tier, i) {
|
||||
_.each(tier, function (op) {
|
||||
PRECEDENCE[op] = i;
|
||||
});
|
||||
});
|
||||
|
||||
exports.UpdateExpression = function (node, parent) {
|
||||
if (t.isMemberExpression(parent) && parent.object === node) {
|
||||
// (foo++).test()
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
exports.ObjectExpression = function (node, parent) {
|
||||
if (t.isExpressionStatement(parent)) {
|
||||
// ({ foo: "bar" });
|
||||
return true;
|
||||
}
|
||||
|
||||
if (t.isMemberExpression(parent) && parent.object === node) {
|
||||
// ({ foo: "bar" }).foo
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
exports.Binary = function (node, parent) {
|
||||
if (t.isCallExpression(parent) && parent.callee === node) {
|
||||
if ((t.isCallExpression(parent) || t.isNewExpression(parent)) && parent.callee === node) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -62,11 +62,15 @@ _.each({
|
||||
CallExpression: { after: 1 },
|
||||
Literal: { after: 1 }
|
||||
}, function (amounts, type) {
|
||||
if (_.isNumber(amounts)) amounts = { after: amounts, before: amounts };
|
||||
if (_.isNumber(amounts)) {
|
||||
amounts = { after: amounts, before: amounts };
|
||||
}
|
||||
|
||||
_.each(amounts, function (amount, key) {
|
||||
exports[key].nodes[type] = function () {
|
||||
return amount;
|
||||
};
|
||||
_.each([type].concat(t.FLIPPED_ALIAS_KEYS[type] || []), function (type) {
|
||||
_.each(amounts, function (amount, key) {
|
||||
exports[key].nodes[type] = function () {
|
||||
return amount;
|
||||
};
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,33 +1,27 @@
|
||||
module.exports = Position;
|
||||
|
||||
var _ = require("lodash");
|
||||
|
||||
function Position() {
|
||||
this.line = 1;
|
||||
this.column = 0;
|
||||
}
|
||||
|
||||
Position.prototype.push = function (str) {
|
||||
var self = this;
|
||||
|
||||
_.each(str, function (cha) {
|
||||
if (cha === "\n") {
|
||||
self.line++;
|
||||
self.column = 0;
|
||||
for (var i = 0; i < str.length; i++) {
|
||||
if (str[i] === "\n") {
|
||||
this.line++;
|
||||
this.column = 0;
|
||||
} else {
|
||||
self.column++;
|
||||
this.column++;
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Position.prototype.unshift = function (str) {
|
||||
var self = this;
|
||||
|
||||
_.each(str, function (cha) {
|
||||
if (cha === "\n") {
|
||||
self.line--;
|
||||
for (var i = 0; i < str.length; i++) {
|
||||
if (str[i] === "\n") {
|
||||
this.line--;
|
||||
} else {
|
||||
self.column--;
|
||||
this.column--;
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -11,15 +11,18 @@ Whitespace.prototype.getNewlinesBefore = function (node) {
|
||||
var startToken;
|
||||
var endToken;
|
||||
var tokens = this.tokens;
|
||||
var token;
|
||||
|
||||
for (var i = 0; i < tokens.length; i++) {
|
||||
token = tokens[i];
|
||||
|
||||
_.each(tokens, function (token, i) {
|
||||
// this is the token this node starts with
|
||||
if (node.start === token.start) {
|
||||
startToken = tokens[i - 1];
|
||||
endToken = token;
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return this.getNewlinesBetween(startToken, endToken);
|
||||
};
|
||||
@@ -28,15 +31,18 @@ Whitespace.prototype.getNewlinesAfter = function (node) {
|
||||
var startToken;
|
||||
var endToken;
|
||||
var tokens = this.tokens;
|
||||
var token;
|
||||
|
||||
for (var i = 0; i < tokens.length; i++) {
|
||||
token = tokens[i];
|
||||
|
||||
_.each(tokens, function (token, i) {
|
||||
// this is the token this node ends with
|
||||
if (node.end === token.end) {
|
||||
startToken = token;
|
||||
endToken = tokens[i + 1];
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (endToken.type.type === "eof") {
|
||||
return 1;
|
||||
|
||||
@@ -3,6 +3,8 @@ var util = require("./util");
|
||||
var fs = require("fs");
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.version = require("../../package").version;
|
||||
|
||||
exports.types = require("./types");
|
||||
|
||||
exports.runtime = require("./runtime-generator");
|
||||
|
||||
@@ -12,6 +12,12 @@ var types = require("ast-types");
|
||||
var def = types.Type.def;
|
||||
var or = types.Type.or;
|
||||
|
||||
def("AssignmentPattern")
|
||||
.bases("Pattern")
|
||||
.build("left", "right")
|
||||
.field("left", def("Pattern"))
|
||||
.field("right", def("Expression"));
|
||||
|
||||
// Acorn - Same as ImportNamespaceSpecifier but `id` is `name`
|
||||
def("ImportBatchSpecifier")
|
||||
.bases("Specifier")
|
||||
|
||||
4
lib/6to5/register-browser.js
Normal file
4
lib/6to5/register-browser.js
Normal file
@@ -0,0 +1,4 @@
|
||||
// Required to safely use 6to5/register within a browserify codebase.
|
||||
module.exports = function () {};
|
||||
|
||||
require("./polyfill");
|
||||
@@ -123,7 +123,7 @@ var hookExtensions = function (_exts) {
|
||||
});
|
||||
};
|
||||
|
||||
hookExtensions([".es6", ".js"]);
|
||||
hookExtensions([".es6", ".es", ".js"]);
|
||||
|
||||
module.exports = function (opts) {
|
||||
// normalise options
|
||||
|
||||
@@ -18,9 +18,9 @@ module.exports = function (namespace) {
|
||||
)
|
||||
]));
|
||||
|
||||
_.each(File.declarations, function (name) {
|
||||
if (_.contains(File.excludeDeclarationsFromRuntime, name)) {
|
||||
return false;
|
||||
_.each(File.helpers, function (name) {
|
||||
if (_.contains(File.excludeHelpersFromRuntime, name)) {
|
||||
return;
|
||||
}
|
||||
|
||||
var key = t.identifier(t.toIdentifier(name));
|
||||
|
||||
@@ -9,7 +9,11 @@ function DefaultFormatter(file) {
|
||||
this.file = file;
|
||||
|
||||
this.localExports = this.getLocalExports();
|
||||
this.localImports = this.getLocalImports();
|
||||
|
||||
this.remapAssignments();
|
||||
|
||||
//this.checkCollisions();
|
||||
}
|
||||
|
||||
DefaultFormatter.prototype.getLocalExports = function () {
|
||||
@@ -27,6 +31,53 @@ DefaultFormatter.prototype.getLocalExports = function () {
|
||||
return localExports;
|
||||
};
|
||||
|
||||
DefaultFormatter.prototype.getLocalImports = function () {
|
||||
var localImports = {};
|
||||
|
||||
traverse(this.file.ast, {
|
||||
enter: function (node) {
|
||||
if (t.isImportDeclaration(node)) {
|
||||
_.extend(localImports, t.getIds(node, true));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return localImports;
|
||||
};
|
||||
|
||||
DefaultFormatter.prototype.checkCollisions = function () {
|
||||
// todo: all check export collissions
|
||||
|
||||
var localImports = this.localImports;
|
||||
var file = this.file;
|
||||
|
||||
var isLocalReference = function (node) {
|
||||
return t.isIdentifier(node) && _.has(localImports, node.name) && localImports[node.name] !== node;
|
||||
};
|
||||
|
||||
var check = function (node) {
|
||||
if (isLocalReference(node)) {
|
||||
throw file.errorWithNode(node, "Illegal assignment of module import");
|
||||
}
|
||||
};
|
||||
|
||||
traverse(file.ast, {
|
||||
enter: function (node) {
|
||||
if (t.isAssignmentExpression(node)) {
|
||||
|
||||
var left = node.left;
|
||||
if (t.isMemberExpression(left)) {
|
||||
while (left.object) left = left.object;
|
||||
}
|
||||
|
||||
check(left);
|
||||
} else if (t.isDeclaration(node)) {
|
||||
_.each(t.getIds(node, true), check);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
DefaultFormatter.prototype.remapExportAssignment = function (node) {
|
||||
return t.assignmentExpression(
|
||||
"=",
|
||||
@@ -51,7 +102,7 @@ DefaultFormatter.prototype.remapAssignments = function () {
|
||||
traverse(this.file.ast, {
|
||||
enter: function (node, parent, scope) {
|
||||
if (t.isUpdateExpression(node) && isLocalReference(node.argument, scope)) {
|
||||
this.stop();
|
||||
this.skip();
|
||||
|
||||
// expand to long file assignment expression
|
||||
var assign = t.assignmentExpression(node.operator[0] + "=", node.argument, t.literal(1));
|
||||
@@ -79,7 +130,7 @@ DefaultFormatter.prototype.remapAssignments = function () {
|
||||
}
|
||||
|
||||
if (t.isAssignmentExpression(node) && isLocalReference(node.left, scope)) {
|
||||
this.stop();
|
||||
this.skip();
|
||||
return self.remapExportAssignment(node);
|
||||
}
|
||||
}
|
||||
@@ -105,11 +156,16 @@ DefaultFormatter.prototype.getModuleName = function () {
|
||||
filenameRelative = filenameRelative.replace(sourceRootRegEx, "");
|
||||
}
|
||||
|
||||
// remove extension
|
||||
filenameRelative = filenameRelative.replace(/\.(.*?)$/, "");
|
||||
if (!opts.keepModuleIdExtensions) {
|
||||
// remove extension
|
||||
filenameRelative = filenameRelative.replace(/\.(.*?)$/, "");
|
||||
}
|
||||
|
||||
moduleName += filenameRelative;
|
||||
|
||||
// normalise path separators
|
||||
moduleName = moduleName.replace(/\\/g, "/");
|
||||
|
||||
return moduleName;
|
||||
};
|
||||
|
||||
@@ -141,10 +197,18 @@ DefaultFormatter.prototype._exportSpecifier = function (getRef, specifier, node,
|
||||
// export * from "foo";
|
||||
nodes.push(this._exportsWildcard(getRef(), node));
|
||||
} else {
|
||||
var ref;
|
||||
if (t.isSpecifierDefault(specifier) && !this.noInteropRequire) {
|
||||
// importing a default so we need to normalise it
|
||||
ref = t.callExpression(this.file.addHelper("interop-require"), [getRef()]);
|
||||
} else {
|
||||
ref = t.memberExpression(getRef(), specifier.id);
|
||||
}
|
||||
|
||||
// export { foo } from "test";
|
||||
nodes.push(this._exportsAssign(
|
||||
t.getSpecifierName(specifier),
|
||||
t.memberExpression(getRef(), specifier.id),
|
||||
ref,
|
||||
node
|
||||
));
|
||||
}
|
||||
@@ -155,9 +219,10 @@ DefaultFormatter.prototype._exportSpecifier = function (getRef, specifier, node,
|
||||
};
|
||||
|
||||
DefaultFormatter.prototype._exportsWildcard = function (objectIdentifier) {
|
||||
return util.template("exports-wildcard", {
|
||||
OBJECT: objectIdentifier
|
||||
}, true);
|
||||
return t.expressionStatement(t.callExpression(this.file.addHelper("defaults"), [
|
||||
t.identifier("exports"),
|
||||
t.callExpression(this.file.addHelper("interop-require-wildcard"), [objectIdentifier])
|
||||
]));
|
||||
};
|
||||
|
||||
DefaultFormatter.prototype._exportsAssign = function (id, init) {
|
||||
|
||||
12
lib/6to5/transformation/modules/_strict.js
Normal file
12
lib/6to5/transformation/modules/_strict.js
Normal file
@@ -0,0 +1,12 @@
|
||||
var util = require("../../util");
|
||||
|
||||
module.exports = function (Parent) {
|
||||
var Constructor = function () {
|
||||
this.noInteropExport = true;
|
||||
Parent.apply(this, arguments);
|
||||
};
|
||||
|
||||
util.inherits(Constructor, Parent);
|
||||
|
||||
return Constructor;
|
||||
};
|
||||
1
lib/6to5/transformation/modules/amd-strict.js
Normal file
1
lib/6to5/transformation/modules/amd-strict.js
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = require("./_strict")(require("./amd"));
|
||||
@@ -1,13 +1,14 @@
|
||||
module.exports = AMDFormatter;
|
||||
|
||||
var DefaultFormatter = require("./_default");
|
||||
var CommonFormatter = require("./common");
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
function AMDFormatter() {
|
||||
DefaultFormatter.apply(this, arguments);
|
||||
this.ids = {};
|
||||
CommonFormatter.apply(this, arguments);
|
||||
this.ids = {};
|
||||
}
|
||||
|
||||
util.inherits(AMDFormatter, DefaultFormatter);
|
||||
@@ -30,12 +31,15 @@ AMDFormatter.prototype.transform = function (ast) {
|
||||
|
||||
// build an array of module names
|
||||
|
||||
var names = [t.literal("exports")].concat(this.buildDependencyLiterals());
|
||||
var names = [t.literal("exports")];
|
||||
if (this.passModuleArg) names.push(t.literal("module"));
|
||||
names = names.concat(this.buildDependencyLiterals());
|
||||
names = t.arrayExpression(names);
|
||||
|
||||
// build up define container
|
||||
|
||||
var params = _.values(this.ids);
|
||||
if (this.passModuleArg) params.unshift(t.identifier("module"));
|
||||
params.unshift(t.identifier("exports"));
|
||||
|
||||
var container = t.functionExpression(null, params, t.blockStatement(body));
|
||||
@@ -85,7 +89,7 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
||||
// import * as bar from "foo";
|
||||
} else if (t.isSpecifierDefault(specifier) && !this.noInteropRequire) {
|
||||
// import foo from "foo";
|
||||
ref = t.callExpression(this.file.addDeclaration("interop-require"), [ref]);
|
||||
ref = t.callExpression(this.file.addHelper("interop-require"), [ref]);
|
||||
} else {
|
||||
// import {foo} from "foo";
|
||||
ref = t.memberExpression(ref, specifier.id, false);
|
||||
@@ -96,6 +100,14 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
||||
]));
|
||||
};
|
||||
|
||||
AMDFormatter.prototype.exportDeclaration = function (node) {
|
||||
if (node.default && !this.noInteropExport) {
|
||||
this.passModuleArg = true;
|
||||
}
|
||||
|
||||
CommonFormatter.prototype.exportDeclaration.apply(this, arguments);
|
||||
};
|
||||
|
||||
AMDFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
|
||||
var self = this;
|
||||
return this._exportSpecifier(function () {
|
||||
|
||||
1
lib/6to5/transformation/modules/common-strict.js
Normal file
1
lib/6to5/transformation/modules/common-strict.js
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = require("./_strict")(require("./common"));
|
||||
@@ -26,24 +26,30 @@ CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes)
|
||||
if (t.isSpecifierDefault(specifier)) {
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(variableName,
|
||||
t.callExpression(this.file.addDeclaration("interop-require"), [util.template("require", {
|
||||
t.callExpression(this.file.addHelper("interop-require"), [util.template("require", {
|
||||
MODULE_NAME: node.source
|
||||
})])
|
||||
)
|
||||
]));
|
||||
} else {
|
||||
// import foo from "foo";
|
||||
|
||||
var templateName = "require-assign";
|
||||
|
||||
// import * as bar from "foo";
|
||||
if (specifier.type !== "ImportBatchSpecifier") templateName += "-key";
|
||||
|
||||
nodes.push(util.template(templateName, {
|
||||
VARIABLE_NAME: variableName,
|
||||
MODULE_NAME: node.source,
|
||||
KEY: specifier.id
|
||||
}));
|
||||
if (specifier.type === "ImportBatchSpecifier") {
|
||||
// import * as bar from "foo";
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(
|
||||
variableName,
|
||||
t.callExpression(this.file.addHelper("interop-require-wildcard"), [
|
||||
t.callExpression(t.identifier("require"), [node.source])
|
||||
])
|
||||
)
|
||||
]));
|
||||
} else {
|
||||
// import foo from "foo";
|
||||
nodes.push(util.template("require-assign-key", {
|
||||
VARIABLE_NAME: variableName,
|
||||
MODULE_NAME: node.source,
|
||||
KEY: specifier.id
|
||||
}));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -55,7 +61,7 @@ CommonJSFormatter.prototype.importDeclaration = function (node, nodes) {
|
||||
};
|
||||
|
||||
CommonJSFormatter.prototype.exportDeclaration = function (node, nodes) {
|
||||
if (node.default) {
|
||||
if (node.default && !this.noInteropRequire && !this.noInteropExport) {
|
||||
var declar = node.declaration;
|
||||
var assign;
|
||||
|
||||
@@ -65,7 +71,7 @@ CommonJSFormatter.prototype.exportDeclaration = function (node, nodes) {
|
||||
// exports = module.exports = VALUE;
|
||||
if (this.hasNonDefaultExports) templateName = "exports-default-module-override";
|
||||
|
||||
if (t.isFunction(declar) || !this.hasNonDefaultExports) {
|
||||
if (t.isFunctionDeclaration(declar) || !this.hasNonDefaultExports) {
|
||||
assign = util.template(templateName, {
|
||||
VALUE: this._pushStatement(declar, nodes)
|
||||
}, true);
|
||||
@@ -77,10 +83,12 @@ CommonJSFormatter.prototype.exportDeclaration = function (node, nodes) {
|
||||
// this export isn't a function so we can't hoist it to the top so we need to set it
|
||||
// at the very end of the file with something like:
|
||||
//
|
||||
// module.exports = Object.assign(exports["default"], exports)
|
||||
// module.exports = _extends(exports["default"], exports)
|
||||
//
|
||||
|
||||
assign = util.template("common-export-default-assign", true);
|
||||
assign = util.template("common-export-default-assign", {
|
||||
EXTENDS_HELPER: this.file.addHelper("extends")
|
||||
}, true);
|
||||
assign._blockHoist = 0;
|
||||
|
||||
nodes.push(assign);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
module.exports = SystemFormatter;
|
||||
|
||||
var AMDFormatter = require("./amd");
|
||||
var useStrict = require("../transformers/use-strict");
|
||||
var traverse = require("../../traverse");
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
@@ -31,7 +32,7 @@ SystemFormatter.prototype._exportsWildcard = function (objectIdentifier, node) {
|
||||
var right = objectIdentifier;
|
||||
|
||||
var block = t.blockStatement([
|
||||
this.buildExportCall(leftIdentifier, valIdentifier)
|
||||
t.expressionStatement(this.buildExportCall(leftIdentifier, valIdentifier))
|
||||
]);
|
||||
|
||||
return this._addImportSource(t.forInStatement(left, right, block), node);
|
||||
@@ -114,7 +115,7 @@ SystemFormatter.prototype.transform = function (ast) {
|
||||
enter: function (node, parent, scope) {
|
||||
if (t.isFunction(node)) {
|
||||
// nothing inside is accessible
|
||||
return this.stop();
|
||||
return this.skip();
|
||||
}
|
||||
|
||||
if (t.isVariableDeclaration(node)) {
|
||||
@@ -159,7 +160,7 @@ SystemFormatter.prototype.transform = function (ast) {
|
||||
// hoist up function declarations for circular references
|
||||
traverse(block, {
|
||||
enter: function (node) {
|
||||
if (t.isFunction(node)) this.stop();
|
||||
if (t.isFunction(node)) this.skip();
|
||||
|
||||
if (t.isFunctionDeclaration(node) || node._blockHoist) {
|
||||
handlerBody.push(node);
|
||||
@@ -170,5 +171,9 @@ SystemFormatter.prototype.transform = function (ast) {
|
||||
|
||||
handlerBody.push(returnStatement);
|
||||
|
||||
if (useStrict._has(block)) {
|
||||
handlerBody.unshift(block.body.shift());
|
||||
}
|
||||
|
||||
program.body = [runner];
|
||||
};
|
||||
|
||||
1
lib/6to5/transformation/modules/umd-strict.js
Normal file
1
lib/6to5/transformation/modules/umd-strict.js
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = require("./_strict")(require("./umd"));
|
||||
@@ -25,22 +25,44 @@ UMDFormatter.prototype.transform = function (ast) {
|
||||
// factory
|
||||
|
||||
var ids = _.values(this.ids);
|
||||
var args = [t.identifier("exports")].concat(ids);
|
||||
var args = [t.identifier("exports")];
|
||||
if (this.passModuleArg) args.push(t.identifier("module"));
|
||||
args = args.concat(ids);
|
||||
|
||||
var factory = t.functionExpression(null, args, t.blockStatement(body));
|
||||
|
||||
// runner
|
||||
// amd
|
||||
|
||||
var defineArgs = [t.literal("exports")];
|
||||
if (this.passModuleArg) defineArgs.push(t.literal("module"));
|
||||
defineArgs = defineArgs.concat(names);
|
||||
defineArgs = [t.arrayExpression(defineArgs)];
|
||||
|
||||
// common
|
||||
|
||||
var testExports = util.template("test-exports");
|
||||
var testModule = util.template("test-module");
|
||||
var commonTests = this.passModuleArg ? t.logicalExpression("&&", testExports, testModule) : testExports;
|
||||
|
||||
var commonArgs = [t.identifier("exports")];
|
||||
if (this.passModuleArg) commonArgs.push(t.identifier("module"));
|
||||
commonArgs = commonArgs.concat(names.map(function (name) {
|
||||
return t.callExpression(t.identifier("require"), [name]);
|
||||
}));
|
||||
|
||||
// globals
|
||||
|
||||
//var umdArgs = [];
|
||||
|
||||
//
|
||||
|
||||
var defineArgs = [t.arrayExpression([t.literal("exports")].concat(names))];
|
||||
var moduleName = this.getModuleName();
|
||||
if (moduleName) defineArgs.unshift(t.literal(moduleName));
|
||||
|
||||
var runner = util.template("umd-runner-body", {
|
||||
AMD_ARGUMENTS: defineArgs,
|
||||
|
||||
COMMON_ARGUMENTS: names.map(function (name) {
|
||||
return t.callExpression(t.identifier("require"), [name]);
|
||||
})
|
||||
COMMON_TEST: commonTests,
|
||||
COMMON_ARGUMENTS: commonArgs
|
||||
});
|
||||
|
||||
//
|
||||
|
||||
1
lib/6to5/transformation/templates/bind.js
Normal file
1
lib/6to5/transformation/templates/bind.js
Normal file
@@ -0,0 +1 @@
|
||||
Function.prototype.bind
|
||||
@@ -0,0 +1,5 @@
|
||||
(function () {
|
||||
if (SUPER_NAME != null) {
|
||||
SUPER_NAME.apply(this, arguments);
|
||||
}
|
||||
});
|
||||
@@ -1,3 +1,3 @@
|
||||
if (SUPER_NAME) {
|
||||
SUPER_NAME.apply(this, arguments);
|
||||
if (Object.getPrototypeOf(CLASS_NAME) !== null) {
|
||||
Object.getPrototypeOf(CLASS_NAME).apply(this, arguments);
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
module.exports = Object.assign(exports["default"], exports);
|
||||
module.exports = EXTENDS_HELPER(exports["default"], exports);
|
||||
|
||||
1
lib/6to5/transformation/templates/default-parameter.js
Normal file
1
lib/6to5/transformation/templates/default-parameter.js
Normal file
@@ -0,0 +1 @@
|
||||
var VARIABLE_NAME = ARGUMENTS[ARGUMENT_KEY] === undefined ? DEFAULT_VALUE : ARGUMENTS[ARGUMENT_KEY];
|
||||
8
lib/6to5/transformation/templates/defaults.js
Normal file
8
lib/6to5/transformation/templates/defaults.js
Normal file
@@ -0,0 +1,8 @@
|
||||
(function (obj, defaults) {
|
||||
for (var key in defaults) {
|
||||
if (obj[key] === undefined) {
|
||||
obj[key] = defaults[key];
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
})
|
||||
@@ -1,5 +0,0 @@
|
||||
(function (obj) {
|
||||
for (var i in obj) {
|
||||
exports[i] = obj[i];
|
||||
}
|
||||
})(OBJECT);
|
||||
9
lib/6to5/transformation/templates/extends.js
Normal file
9
lib/6to5/transformation/templates/extends.js
Normal file
@@ -0,0 +1,9 @@
|
||||
(function (target) {
|
||||
for (var i = 1; i < arguments.length; i++) {
|
||||
var source = arguments[i];
|
||||
for (var key in source) {
|
||||
target[key] = source[key];
|
||||
}
|
||||
}
|
||||
return target;
|
||||
})
|
||||
13
lib/6to5/transformation/templates/for-of-fast.js
Normal file
13
lib/6to5/transformation/templates/for-of-fast.js
Normal file
@@ -0,0 +1,13 @@
|
||||
for (var LOOP_OBJECT = OBJECT,
|
||||
IS_ARRAY = Array.isArray(LOOP_OBJECT),
|
||||
INDEX = 0,
|
||||
LOOP_OBJECT = IS_ARRAY ? LOOP_OBJECT : LOOP_OBJECT[Symbol.iterator]();;) {
|
||||
if (IS_ARRAY) {
|
||||
if (INDEX >= LOOP_OBJECT.length) break;
|
||||
ID = LOOP_OBJECT[INDEX++];
|
||||
} else {
|
||||
INDEX = LOOP_OBJECT.next();
|
||||
if (INDEX.done) break;
|
||||
ID = INDEX.value;
|
||||
}
|
||||
}
|
||||
23
lib/6to5/transformation/templates/get.js
Normal file
23
lib/6to5/transformation/templates/get.js
Normal file
@@ -0,0 +1,23 @@
|
||||
(function get(object, property, receiver) {
|
||||
var desc = Object.getOwnPropertyDescriptor(object, property);
|
||||
|
||||
if (desc === undefined) {
|
||||
var parent = Object.getPrototypeOf(object);
|
||||
|
||||
if (parent === null) {
|
||||
return undefined;
|
||||
} else {
|
||||
return get(parent, property, receiver);
|
||||
}
|
||||
} else if ("value" in desc && desc.writable) {
|
||||
return desc.value;
|
||||
} else {
|
||||
var getter = desc.get;
|
||||
|
||||
if (getter === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return getter.call(receiver);
|
||||
}
|
||||
});
|
||||
@@ -1 +0,0 @@
|
||||
if (VARIABLE === undefined) VARIABLE = DEFAULT;
|
||||
@@ -1,11 +1,14 @@
|
||||
(function (child, parent) {
|
||||
child.prototype = Object.create(parent && parent.prototype, {
|
||||
(function (subClass, superClass) {
|
||||
if (typeof superClass !== "function" && superClass !== null) {
|
||||
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
|
||||
}
|
||||
subClass.prototype = Object.create(superClass && superClass.prototype, {
|
||||
constructor: {
|
||||
value: child,
|
||||
value: subClass,
|
||||
enumerable: false,
|
||||
writable: true,
|
||||
configurable: true
|
||||
}
|
||||
});
|
||||
if (parent) child.__proto__ = parent;
|
||||
if (superClass) subClass.__proto__ = superClass;
|
||||
})
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
(function (obj) {
|
||||
return obj && obj.constructor === Object ? obj : { default: obj };
|
||||
})
|
||||
@@ -1 +0,0 @@
|
||||
Object.defineProperties(OBJECT, PROPS);
|
||||
@@ -0,0 +1,11 @@
|
||||
(function (FUNCTION_KEY) {
|
||||
var WRAPPER_KEY = function FUNCTION_ID() {
|
||||
return FUNCTION_KEY.apply(this, arguments);
|
||||
};
|
||||
|
||||
WRAPPER_KEY.toString = function () {
|
||||
return FUNCTION_KEY.toString();
|
||||
};
|
||||
|
||||
return WRAPPER_KEY;
|
||||
})(FUNCTION)
|
||||
@@ -1 +0,0 @@
|
||||
var VARIABLE_NAME = require(MODULE_NAME);
|
||||
3
lib/6to5/transformation/templates/rest.js
Normal file
3
lib/6to5/transformation/templates/rest.js
Normal file
@@ -0,0 +1,3 @@
|
||||
for (var KEY = START; KEY < ARGUMENTS.length; KEY++) {
|
||||
ARRAY[ARRAY_KEY] = ARGUMENTS[KEY];
|
||||
}
|
||||
1
lib/6to5/transformation/templates/test-exports.js
Normal file
1
lib/6to5/transformation/templates/test-exports.js
Normal file
@@ -0,0 +1 @@
|
||||
typeof exports !== "undefined"
|
||||
1
lib/6to5/transformation/templates/test-module.js
Normal file
1
lib/6to5/transformation/templates/test-module.js
Normal file
@@ -0,0 +1 @@
|
||||
typeof module !== "undefined"
|
||||
3
lib/6to5/transformation/templates/typeof.js
Normal file
3
lib/6to5/transformation/templates/typeof.js
Normal file
@@ -0,0 +1,3 @@
|
||||
(function (obj) {
|
||||
return obj && obj.constructor === Symbol ? "symbol" : typeof obj;
|
||||
});
|
||||
@@ -1,7 +1,7 @@
|
||||
(function (factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(AMD_ARGUMENTS, factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, COMMON_ARGUMENTS);
|
||||
} else if (COMMON_TEST) {
|
||||
factory(COMMON_ARGUMENTS);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -31,18 +31,19 @@ transform._ensureTransformerNames = function (type, keys) {
|
||||
transform.transformers = {};
|
||||
|
||||
transform.moduleFormatters = {
|
||||
common: require("./modules/common"),
|
||||
system: require("./modules/system"),
|
||||
ignore: require("./modules/ignore"),
|
||||
amd: require("./modules/amd"),
|
||||
umd: require("./modules/umd")
|
||||
commonStrict: require("./modules/common-strict"),
|
||||
umdStrict: require("./modules/umd-strict"),
|
||||
amdStrict: require("./modules/amd-strict"),
|
||||
common: require("./modules/common"),
|
||||
system: require("./modules/system"),
|
||||
ignore: require("./modules/ignore"),
|
||||
amd: require("./modules/amd"),
|
||||
umd: require("./modules/umd")
|
||||
};
|
||||
|
||||
_.each({
|
||||
// spec
|
||||
specBlockHoistFunctions: require("./transformers/spec-block-hoist-functions"),
|
||||
specNoForInOfAssignment: require("./transformers/spec-no-for-in-of-assignment"),
|
||||
specNoDuplicateProperties: require("./transformers/spec-no-duplicate-properties"),
|
||||
specSetters: require("./transformers/spec-setters"),
|
||||
|
||||
// playground
|
||||
methodBinding: require("./transformers/playground-method-binding"),
|
||||
@@ -58,17 +59,18 @@ _.each({
|
||||
arrayComprehension: require("./transformers/es7-array-comprehension"),
|
||||
generatorComprehension: require("./transformers/es7-generator-comprehension"),
|
||||
arrowFunctions: require("./transformers/es6-arrow-functions"),
|
||||
classesFastSuper: require("./transformers/optional-classes-fast-super"),
|
||||
classes: require("./transformers/es6-classes"),
|
||||
|
||||
computedPropertyNames: require("./transformers/es6-computed-property-names"),
|
||||
|
||||
objectSpread: require("./transformers/es7-object-spread"),
|
||||
exponentiationOperator: require("./transformers/es7-exponentiation-operator"),
|
||||
spread: require("./transformers/es6-spread"),
|
||||
templateLiterals: require("./transformers/es6-template-literals"),
|
||||
propertyMethodAssignment: require("./transformers/es5-property-method-assignment"),
|
||||
propertyMethodAssignment: require("./transformers/es6-property-method-assignment"),
|
||||
computedPropertyNames: require("./transformers/es6-computed-property-names"),
|
||||
destructuring: require("./transformers/es6-destructuring"),
|
||||
defaultParameters: require("./transformers/es6-default-parameters"),
|
||||
forOfFast: require("./transformers/optional-for-of-fast"),
|
||||
forOf: require("./transformers/es6-for-of"),
|
||||
unicodeRegex: require("./transformers/es6-unicode-regex"),
|
||||
abstractReferences: require("./transformers/es7-abstract-references"),
|
||||
@@ -81,15 +83,18 @@ _.each({
|
||||
generators: require("./transformers/es6-generators"),
|
||||
restParameters: require("./transformers/es6-rest-parameters"),
|
||||
|
||||
protoToAssign: require("./transformers/optional-proto-to-assign"),
|
||||
|
||||
_declarations: require("./transformers/_declarations"),
|
||||
|
||||
coreAliasing: require("./transformers/optional-core-aliasing"),
|
||||
undefinedToVoid: require("./transformers/optional-undefined-to-void"),
|
||||
|
||||
// wrap up
|
||||
useStrict: require("./transformers/use-strict"),
|
||||
_aliasFunctions: require("./transformers/_alias-functions"),
|
||||
_moduleFormatter: require("./transformers/_module-formatter"),
|
||||
useStrict: require("./transformers/use-strict"),
|
||||
|
||||
typeofSymbol: require("./transformers/optional-typeof-symbol"),
|
||||
coreAliasing: require("./transformers/optional-core-aliasing"),
|
||||
undefinedToVoid: require("./transformers/optional-undefined-to-void"),
|
||||
|
||||
// spec
|
||||
specPropertyLiterals: require("./transformers/spec-property-literals"),
|
||||
|
||||
@@ -7,20 +7,26 @@ var _ = require("lodash");
|
||||
function Transformer(key, transformer, opts) {
|
||||
this.manipulateOptions = transformer.manipulateOptions;
|
||||
this.experimental = !!transformer.experimental;
|
||||
this.transformer = Transformer.normalise(transformer);
|
||||
this.secondPass = !!transformer.secondPass;
|
||||
this.transformer = this.normalise(transformer);
|
||||
this.optional = !!transformer.optional;
|
||||
this.opts = opts || {};
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
Transformer.normalise = function (transformer) {
|
||||
Transformer.prototype.normalise = function (transformer) {
|
||||
var self = this;
|
||||
|
||||
if (_.isFunction(transformer)) {
|
||||
transformer = { ast: transformer };
|
||||
}
|
||||
|
||||
_.each(transformer, function (fns, type) {
|
||||
// hidden property
|
||||
if (type[0] === "_") return;
|
||||
if (type[0] === "_") {
|
||||
self[type] = fns;
|
||||
return;
|
||||
}
|
||||
|
||||
if (_.isFunction(fns)) fns = { enter: fns };
|
||||
|
||||
@@ -41,16 +47,14 @@ Transformer.normalise = function (transformer) {
|
||||
|
||||
Transformer.prototype.astRun = function (file, key) {
|
||||
var transformer = this.transformer;
|
||||
var ast = file.ast;
|
||||
|
||||
if (transformer.ast && transformer.ast[key]) {
|
||||
transformer.ast[key](ast, file);
|
||||
transformer.ast[key](file.ast, file);
|
||||
}
|
||||
};
|
||||
|
||||
Transformer.prototype.transform = function (file) {
|
||||
var transformer = this.transformer;
|
||||
var ast = file.ast;
|
||||
|
||||
var build = function (exit) {
|
||||
return function (node, parent, scope) {
|
||||
@@ -67,7 +71,7 @@ Transformer.prototype.transform = function (file) {
|
||||
|
||||
this.astRun(file, "before");
|
||||
|
||||
traverse(ast, {
|
||||
traverse(file.ast, {
|
||||
enter: build(),
|
||||
exit: build(true)
|
||||
});
|
||||
|
||||
@@ -20,7 +20,7 @@ var go = function (getBody, node, file, scope) {
|
||||
if (!node._aliasFunction) {
|
||||
if (t.isFunction(node)) {
|
||||
// stop traversal of this node as it'll be hit again by this transformer
|
||||
return this.stop();
|
||||
return this.skip();
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
@@ -30,10 +30,10 @@ var go = function (getBody, node, file, scope) {
|
||||
traverse(node, {
|
||||
enter: function (node, parent) {
|
||||
if (t.isFunction(node) && !node._aliasFunction) {
|
||||
return this.stop();
|
||||
return this.skip();
|
||||
}
|
||||
|
||||
if (node._ignoreAliasFunctions) return this.stop();
|
||||
if (node._ignoreAliasFunctions) return this.skip();
|
||||
|
||||
var getId;
|
||||
|
||||
@@ -49,7 +49,7 @@ var go = function (getBody, node, file, scope) {
|
||||
}
|
||||
});
|
||||
|
||||
return this.stop();
|
||||
return this.skip();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -67,7 +67,7 @@ var go = function (getBody, node, file, scope) {
|
||||
}
|
||||
|
||||
if (thisId) {
|
||||
pushDeclaration(thisId, t.identifier("this"));
|
||||
pushDeclaration(thisId, t.thisExpression());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
var _ = require("lodash");
|
||||
var useStrict = require("./use-strict");
|
||||
var _ = require("lodash");
|
||||
|
||||
// Priority:
|
||||
//
|
||||
@@ -17,13 +18,15 @@ exports.Program = {
|
||||
}
|
||||
if (!hasChange) return;
|
||||
|
||||
var nodePriorities = _.groupBy(node.body, function (bodyNode) {
|
||||
var priority = bodyNode._blockHoist;
|
||||
if (priority == null) priority = 1;
|
||||
if (priority === true) priority = 2;
|
||||
return priority;
|
||||
});
|
||||
useStrict._wrap(node, function () {
|
||||
var nodePriorities = _.groupBy(node.body, function (bodyNode) {
|
||||
var priority = bodyNode._blockHoist;
|
||||
if (priority == null) priority = 1;
|
||||
if (priority === true) priority = 2;
|
||||
return priority;
|
||||
});
|
||||
|
||||
node.body = _.flatten(_.values(nodePriorities).reverse());
|
||||
node.body = _.flatten(_.values(nodePriorities).reverse());
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,25 +1,32 @@
|
||||
var t = require("../../types");
|
||||
var useStrict = require("./use-strict");
|
||||
var t = require("../../types");
|
||||
|
||||
exports.secondPass = true;
|
||||
|
||||
exports.BlockStatement =
|
||||
exports.Program = function (node) {
|
||||
var kinds = {};
|
||||
var kind;
|
||||
|
||||
for (var i in node._declarations) {
|
||||
var declar = node._declarations[i];
|
||||
useStrict._wrap(node, function () {
|
||||
for (var i in node._declarations) {
|
||||
var declar = node._declarations[i];
|
||||
|
||||
kind = declar.kind || "var";
|
||||
var declarNode = t.variableDeclarator(declar.id, declar.init);
|
||||
kind = declar.kind || "var";
|
||||
var declarNode = t.variableDeclarator(declar.id, declar.init);
|
||||
|
||||
if (!declar.init) {
|
||||
kinds[kind] = kinds[kind] || [];
|
||||
kinds[kind].push(declarNode);
|
||||
} else {
|
||||
node.body.unshift(t.variableDeclaration(kind, [declarNode]));
|
||||
if (!declar.init) {
|
||||
kinds[kind] = kinds[kind] || [];
|
||||
kinds[kind].push(declarNode);
|
||||
} else {
|
||||
node.body.unshift(t.variableDeclaration(kind, [declarNode]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (kind in kinds) {
|
||||
node.body.unshift(t.variableDeclaration(kind, kinds[kind]));
|
||||
}
|
||||
for (kind in kinds) {
|
||||
node.body.unshift(t.variableDeclaration(kind, kinds[kind]));
|
||||
}
|
||||
});
|
||||
|
||||
node._declarations = null;
|
||||
};
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
var util = require("../../util");
|
||||
|
||||
exports.Property = function (node) {
|
||||
if (node.method) node.method = false;
|
||||
};
|
||||
|
||||
exports.ObjectExpression = function (node, parent, file) {
|
||||
var mutatorMap = {};
|
||||
var hasAny = false;
|
||||
|
||||
node.properties = node.properties.filter(function (prop) {
|
||||
if (prop.kind === "get" || prop.kind === "set") {
|
||||
hasAny = true;
|
||||
util.pushMutatorMap(mutatorMap, prop.key, prop.kind, prop.value);
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
if (!hasAny) return;
|
||||
|
||||
if (node.properties.length) {
|
||||
var objId = util.getUid(parent, file);
|
||||
|
||||
return util.template("object-define-properties-closure", {
|
||||
KEY: objId,
|
||||
OBJECT: node,
|
||||
CONTENT: util.template("object-define-properties", {
|
||||
OBJECT: objId,
|
||||
PROPS: util.buildDefineProperties(mutatorMap)
|
||||
})
|
||||
});
|
||||
} else {
|
||||
return util.template("object-define-properties", {
|
||||
OBJECT: node,
|
||||
PROPS: util.buildDefineProperties(mutatorMap)
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -3,36 +3,11 @@ var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
|
||||
exports.ClassDeclaration = function (node, parent, file, scope) {
|
||||
var closure = true;
|
||||
if (t.isProgram(parent) || t.isBlockStatement(parent)) {
|
||||
closure = false;
|
||||
}
|
||||
|
||||
var factory = new Class(node, file, scope, closure);
|
||||
var newNode = factory.run();
|
||||
if (factory.closure) {
|
||||
if (closure) {
|
||||
// declaration in an expression context...
|
||||
// export default class Foo {}
|
||||
scope.push({
|
||||
kind: "var",
|
||||
key: node.id.key,
|
||||
id: node.id
|
||||
});
|
||||
return t.assignmentExpression("=", node.id, newNode);
|
||||
} else {
|
||||
// has a super class or PrivateDeclaration etc
|
||||
return t.variableDeclaration("let", [
|
||||
t.variableDeclarator(node.id, newNode)
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
return newNode;
|
||||
}
|
||||
return new Class(node, file, scope, true).run();
|
||||
};
|
||||
|
||||
exports.ClassExpression = function (node, parent, file, scope) {
|
||||
return new Class(node, file, scope, true).run();
|
||||
return new Class(node, file, scope, false).run();
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -44,11 +19,11 @@ exports.ClassExpression = function (node, parent, file, scope) {
|
||||
* @param {Boolean} closure
|
||||
*/
|
||||
|
||||
function Class(node, file, scope, closure) {
|
||||
this.closure = closure;
|
||||
this.scope = scope;
|
||||
this.node = node;
|
||||
this.file = file;
|
||||
function Class(node, file, scope, isStatement) {
|
||||
this.isStatement = isStatement;
|
||||
this.scope = scope;
|
||||
this.node = node;
|
||||
this.file = file;
|
||||
|
||||
this.hasInstanceMutators = false;
|
||||
this.hasStaticMutators = false;
|
||||
@@ -75,49 +50,60 @@ Class.prototype.run = function () {
|
||||
|
||||
var body = this.body = [];
|
||||
|
||||
var constructor = t.functionExpression(null, [], t.blockStatement([]));
|
||||
if (this.node.id) constructor.id = className;
|
||||
var constructor;
|
||||
if (this.node.id) {
|
||||
constructor = t.functionDeclaration(className, [], t.blockStatement([]));
|
||||
body.push(constructor);
|
||||
} else {
|
||||
constructor = t.functionExpression(null, [], t.blockStatement([]));
|
||||
body.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(className, constructor)
|
||||
]));
|
||||
}
|
||||
this.constructor = constructor;
|
||||
|
||||
body.push(t.variableDeclaration("let", [
|
||||
t.variableDeclarator(className, constructor)
|
||||
]));
|
||||
var closureParams = [];
|
||||
var closureArgs = [];
|
||||
|
||||
//
|
||||
|
||||
if (superName) {
|
||||
this.closure = true;
|
||||
// so we're only evaluating it once
|
||||
var superRefName = "super";
|
||||
if (className) superRefName = className.name + "Super";
|
||||
closureArgs.push(superName);
|
||||
|
||||
var superRef = file.generateUidIdentifier(superRefName, this.scope);
|
||||
body.unshift(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(superRef, superName)
|
||||
]));
|
||||
superName = superRef;
|
||||
if (!t.isIdentifier(superName)) {
|
||||
var superRef = this.scope.generateUidBasedOnNode(superName, this.file);
|
||||
superName = superRef;
|
||||
}
|
||||
|
||||
closureParams.push(superName);
|
||||
|
||||
this.superName = superName;
|
||||
body.push(t.expressionStatement(t.callExpression(file.addDeclaration("inherits"), [className, superName])));
|
||||
body.push(t.expressionStatement(t.callExpression(file.addHelper("inherits"), [className, superName])));
|
||||
}
|
||||
|
||||
this.buildBody();
|
||||
|
||||
t.inheritsComments(body[0], this.node);
|
||||
|
||||
if (this.closure) {
|
||||
if (body.length === 1) {
|
||||
// only a constructor so no need for a closure container
|
||||
return constructor;
|
||||
} else {
|
||||
body.push(t.returnStatement(className));
|
||||
return t.callExpression(
|
||||
t.functionExpression(null, [], t.blockStatement(body)),
|
||||
[]
|
||||
);
|
||||
}
|
||||
var init;
|
||||
|
||||
if (body.length === 1) {
|
||||
// only a constructor so no need for a closure container
|
||||
init = t.toExpression(constructor);
|
||||
} else {
|
||||
return body;
|
||||
body.push(t.returnStatement(className));
|
||||
init = t.callExpression(
|
||||
t.functionExpression(null, closureParams, t.blockStatement(body)),
|
||||
closureArgs
|
||||
);
|
||||
}
|
||||
|
||||
if (this.isStatement) {
|
||||
return t.variableDeclaration("let", [
|
||||
t.variableDeclarator(className, init)
|
||||
]);
|
||||
} else {
|
||||
return init;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -151,7 +137,7 @@ Class.prototype.buildBody = function () {
|
||||
|
||||
if (!this.hasConstructor && superName && !t.isFalsyExpression(superName)) {
|
||||
constructor.body.body.push(util.template("class-super-constructor-call", {
|
||||
SUPER_NAME: superName
|
||||
CLASS_NAME: className
|
||||
}, true));
|
||||
}
|
||||
|
||||
@@ -159,15 +145,11 @@ Class.prototype.buildBody = function () {
|
||||
var staticProps;
|
||||
|
||||
if (this.hasInstanceMutators) {
|
||||
var protoId = util.template("prototype-identifier", {
|
||||
CLASS_NAME: className
|
||||
});
|
||||
|
||||
instanceProps = util.buildDefineProperties(this.instanceMutatorMap, protoId);
|
||||
instanceProps = util.buildDefineProperties(this.instanceMutatorMap);
|
||||
}
|
||||
|
||||
if (this.hasStaticMutators) {
|
||||
staticProps = util.buildDefineProperties(this.staticMutatorMap, className);
|
||||
staticProps = util.buildDefineProperties(this.staticMutatorMap);
|
||||
}
|
||||
|
||||
if (instanceProps || staticProps) {
|
||||
@@ -177,7 +159,7 @@ Class.prototype.buildBody = function () {
|
||||
if (instanceProps) args.push(instanceProps);
|
||||
|
||||
body.push(t.expressionStatement(
|
||||
t.callExpression(this.file.addDeclaration("prototype-properties"), args)
|
||||
t.callExpression(this.file.addHelper("prototype-properties"), args)
|
||||
));
|
||||
}
|
||||
};
|
||||
@@ -192,70 +174,49 @@ Class.prototype.pushMethod = function (node) {
|
||||
var methodName = node.key;
|
||||
|
||||
var kind = node.kind;
|
||||
var mutatorMap = this.instanceMutatorMap;
|
||||
if (node.static) {
|
||||
this.hasStaticMutators = true;
|
||||
mutatorMap = this.staticMutatorMap;
|
||||
} else {
|
||||
this.hasInstanceMutators = true;
|
||||
}
|
||||
|
||||
if (kind === "") {
|
||||
// method
|
||||
|
||||
var className = this.className;
|
||||
if (!node.static) className = t.memberExpression(className, t.identifier("prototype"));
|
||||
methodName = t.memberExpression(className, methodName, node.computed);
|
||||
|
||||
var expr = t.expressionStatement(t.assignmentExpression("=", methodName, node.value));
|
||||
t.inheritsComments(expr, node);
|
||||
this.body.push(expr);
|
||||
} else {
|
||||
// mutator
|
||||
var mutatorMap = this.instanceMutatorMap;
|
||||
if (node.static) {
|
||||
this.hasStaticMutators = true;
|
||||
mutatorMap = this.staticMutatorMap;
|
||||
} else {
|
||||
this.hasInstanceMutators = true;
|
||||
}
|
||||
util.pushMutatorMap(mutatorMap, methodName, kind, node);
|
||||
kind = "value";
|
||||
}
|
||||
|
||||
util.pushMutatorMap(mutatorMap, methodName, kind, node.computed, node);
|
||||
};
|
||||
|
||||
/**
|
||||
* Given a `methodNode`, produce a `MemberExpression` super class reference.
|
||||
* Gets a node representing the super class value of the named property.
|
||||
*
|
||||
* @param {Node} methodNode MethodDefinition
|
||||
* @param {Node} node Identifier
|
||||
* @param {Node} parent
|
||||
* @example
|
||||
*
|
||||
* _get(Object.getPrototypeOf(CLASS.prototype), "METHOD", this)
|
||||
*
|
||||
* @param {Node} property
|
||||
* @param {boolean} isStatic
|
||||
* @param {boolean} isComputed
|
||||
*
|
||||
* @returns {Node}
|
||||
*/
|
||||
|
||||
Class.prototype.superIdentifier = function (methodNode, id, parent) {
|
||||
var methodName = methodNode.key;
|
||||
var superName = this.superName || t.identifier("Function");
|
||||
|
||||
if (parent.property === id) {
|
||||
return;
|
||||
} else if (t.isCallExpression(parent, { callee: id })) {
|
||||
// super(); -> ClassName.prototype.MethodName.call(this);
|
||||
parent.arguments.unshift(t.thisExpression());
|
||||
|
||||
if (methodName.name === "constructor") {
|
||||
// constructor() { super(); }
|
||||
return t.memberExpression(superName, t.identifier("call"));
|
||||
} else {
|
||||
id = superName;
|
||||
|
||||
// foo() { super(); }
|
||||
if (!methodNode.static) {
|
||||
id = t.memberExpression(id, t.identifier("prototype"));
|
||||
}
|
||||
|
||||
id = t.memberExpression(id, methodName, methodNode.computed);
|
||||
return t.memberExpression(id, t.identifier("call"));
|
||||
}
|
||||
} else if (t.isMemberExpression(parent) && !methodNode.static) {
|
||||
// super.test -> ClassName.prototype.test
|
||||
return t.memberExpression(superName, t.identifier("prototype"));
|
||||
} else {
|
||||
return superName;
|
||||
}
|
||||
Class.prototype.superProperty = function (property, isStatic, isComputed, thisExpression) {
|
||||
return t.callExpression(
|
||||
this.file.addHelper("get"),
|
||||
[
|
||||
t.callExpression(
|
||||
t.memberExpression(t.identifier("Object"), t.identifier("getPrototypeOf")),
|
||||
[
|
||||
isStatic ? this.className : t.memberExpression(this.className, t.identifier("prototype"))
|
||||
]
|
||||
),
|
||||
isComputed ? property : t.literal(property.name),
|
||||
thisExpression
|
||||
]
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -266,23 +227,87 @@ Class.prototype.superIdentifier = function (methodNode, id, parent) {
|
||||
|
||||
Class.prototype.replaceInstanceSuperReferences = function (methodNode) {
|
||||
var method = methodNode.value;
|
||||
var self = this;
|
||||
var self = this;
|
||||
|
||||
traverse(method, {
|
||||
enter: function (node, parent) {
|
||||
if (t.isIdentifier(node, { name: "super" })) {
|
||||
return self.superIdentifier(methodNode, node, parent);
|
||||
} else if (t.isCallExpression(node)) {
|
||||
var callee = node.callee;
|
||||
if (!t.isMemberExpression(callee)) return;
|
||||
if (callee.object.name !== "super") return;
|
||||
var topLevelThisReference;
|
||||
|
||||
// super.test(); -> ClassName.prototype.MethodName.call(this);
|
||||
callee.property = t.memberExpression(callee.property, t.identifier("call"));
|
||||
node.arguments.unshift(t.thisExpression());
|
||||
traverse2(method, true);
|
||||
|
||||
if (topLevelThisReference) {
|
||||
method.body.body.unshift(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(topLevelThisReference, t.thisExpression())
|
||||
]));
|
||||
}
|
||||
|
||||
function traverse2(node, topLevel) {
|
||||
traverse(node, {
|
||||
enter: function (node, parent) {
|
||||
if (t.isFunction(node) && !t.isArrowFunctionExpression(node)) {
|
||||
traverse2(node, false);
|
||||
return this.skip();
|
||||
}
|
||||
|
||||
var property;
|
||||
var computed;
|
||||
var args;
|
||||
|
||||
if (t.isIdentifier(node, { name: "super" })) {
|
||||
if (!(t.isMemberExpression(parent) && !parent.computed && parent.property === node)) {
|
||||
throw self.file.errorWithNode(node, "illegal use of bare super");
|
||||
}
|
||||
} else if (t.isCallExpression(node)) {
|
||||
var callee = node.callee;
|
||||
if (t.isIdentifier(callee, { name: "super" })) {
|
||||
// super(); -> _get(Object.getPrototypeOf(ClassName), "MethodName", this).call(this);
|
||||
property = methodNode.key;
|
||||
computed = methodNode.computed;
|
||||
args = node.arguments;
|
||||
} else {
|
||||
if (!t.isMemberExpression(callee)) return;
|
||||
if (callee.object.name !== "super") return;
|
||||
|
||||
// super.test(); -> _get(Object.getPrototypeOf(ClassName.prototype), "test", this).call(this);
|
||||
property = callee.property;
|
||||
computed = callee.computed;
|
||||
args = node.arguments;
|
||||
}
|
||||
} else if (t.isMemberExpression(node)) {
|
||||
if (!t.isIdentifier(node.object, { name: "super" })) return;
|
||||
|
||||
// super.name; -> _get(Object.getPrototypeOf(ClassName.prototype), "name", this);
|
||||
property = node.property;
|
||||
computed = node.computed;
|
||||
}
|
||||
|
||||
if (property) {
|
||||
var thisReference;
|
||||
if (topLevel) {
|
||||
thisReference = t.thisExpression();
|
||||
} else {
|
||||
topLevelThisReference = thisReference = topLevelThisReference || self.file.generateUidIdentifier("this");
|
||||
}
|
||||
|
||||
var superProperty = self.superProperty(property, methodNode.static, computed, thisReference);
|
||||
if (args) {
|
||||
if (args.length === 1 && t.isSpreadElement(args[0])) {
|
||||
// super(...arguments);
|
||||
return t.callExpression(
|
||||
t.memberExpression(superProperty, t.identifier("apply"), false),
|
||||
[thisReference, args[0].argument]
|
||||
);
|
||||
} else {
|
||||
return t.callExpression(
|
||||
t.memberExpression(superProperty, t.identifier("call"), false),
|
||||
[thisReference].concat(args)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
return superProperty;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -292,7 +317,7 @@ Class.prototype.replaceInstanceSuperReferences = function (methodNode) {
|
||||
*/
|
||||
|
||||
Class.prototype.pushConstructor = function (method) {
|
||||
if (method.kind !== "") {
|
||||
if (method.kind) {
|
||||
throw this.file.errorWithNode(method, "illegal kind for constructor method");
|
||||
}
|
||||
|
||||
@@ -304,8 +329,9 @@ Class.prototype.pushConstructor = function (method) {
|
||||
t.inherits(construct, fn);
|
||||
t.inheritsComments(construct, method);
|
||||
|
||||
construct.defaults = fn.defaults;
|
||||
construct.params = fn.params;
|
||||
construct.body = fn.body;
|
||||
construct.rest = fn.rest;
|
||||
construct._ignoreUserWhitespace = true;
|
||||
construct.defaults = fn.defaults;
|
||||
construct.params = fn.params;
|
||||
construct.body = fn.body;
|
||||
construct.rest = fn.rest;
|
||||
};
|
||||
|
||||
@@ -1,20 +1,19 @@
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var t = require("../../types");
|
||||
|
||||
exports.ObjectExpression = function (node, parent, file) {
|
||||
exports.ObjectExpression = function (node, parent, file, scope) {
|
||||
var hasComputed = false;
|
||||
var prop;
|
||||
var key;
|
||||
var i;
|
||||
|
||||
for (i in node.properties) {
|
||||
hasComputed = t.isProperty(node.properties[i], { computed: true });
|
||||
hasComputed = t.isProperty(node.properties[i], { computed: true, kind: "init" });
|
||||
if (hasComputed) break;
|
||||
}
|
||||
|
||||
if (!hasComputed) return;
|
||||
|
||||
var objId = util.getUid(parent, file);
|
||||
var objId = scope.generateUidBasedOnNode(parent, file);
|
||||
|
||||
var body = [];
|
||||
var container = t.functionExpression(null, [], t.blockStatement(body));
|
||||
@@ -26,6 +25,8 @@ exports.ObjectExpression = function (node, parent, file) {
|
||||
|
||||
for (i in props) {
|
||||
prop = props[i];
|
||||
if (prop.kind !== "init") continue;
|
||||
|
||||
key = prop.key;
|
||||
|
||||
if (!prop.computed && t.isIdentifier(key)) {
|
||||
@@ -45,7 +46,7 @@ exports.ObjectExpression = function (node, parent, file) {
|
||||
broken = true;
|
||||
}
|
||||
|
||||
if (!broken || t.isLiteral(prop.key, { value: "__proto__" })) {
|
||||
if (prop.kind !== "init" || !broken || t.isLiteral(t.toComputedKey(prop, prop.key), { value: "__proto__" })) {
|
||||
initProps.push(prop);
|
||||
props[i] = null;
|
||||
}
|
||||
@@ -62,13 +63,14 @@ exports.ObjectExpression = function (node, parent, file) {
|
||||
var bodyNode;
|
||||
|
||||
if (prop.computed && t.isMemberExpression(key) && t.isIdentifier(key.object, { name: "Symbol" })) {
|
||||
// { [Symbol.iterator]: "foo" }
|
||||
bodyNode = t.assignmentExpression(
|
||||
"=",
|
||||
t.memberExpression(objId, key, true),
|
||||
prop.value
|
||||
);
|
||||
} else {
|
||||
bodyNode = t.callExpression(file.addDeclaration("define-property"), [objId, key, prop.value]);
|
||||
bodyNode = t.callExpression(file.addHelper("define-property"), [objId, key, prop.value]);
|
||||
}
|
||||
|
||||
body.push(t.expressionStatement(bodyNode));
|
||||
@@ -80,7 +82,7 @@ exports.ObjectExpression = function (node, parent, file) {
|
||||
var first = body[0].expression;
|
||||
|
||||
if (t.isCallExpression(first)) {
|
||||
first.arguments[0] = t.objectExpression([]);
|
||||
first.arguments[0] = t.objectExpression(initProps);
|
||||
return first;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ exports.Function = function (node, parent, file, scope) {
|
||||
throw file.errorWithNode(node, "Temporal dead zone - accessing a variable before it's initialized");
|
||||
}
|
||||
|
||||
if (scope.has(node.name)) {
|
||||
if (scope.has(node.name, true)) {
|
||||
iife = true;
|
||||
}
|
||||
};
|
||||
@@ -49,16 +49,29 @@ exports.Function = function (node, parent, file, scope) {
|
||||
|
||||
var body = [];
|
||||
|
||||
var argsIdentifier = t.identifier("arguments");
|
||||
argsIdentifier._ignoreAliasFunctions = true;
|
||||
|
||||
var lastNonDefaultParam = 0;
|
||||
|
||||
for (i in node.defaults) {
|
||||
def = node.defaults[i];
|
||||
if (!def) continue;
|
||||
if (!def) {
|
||||
lastNonDefaultParam = +i + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
body.push(util.template("if-undefined-set-to", {
|
||||
VARIABLE: node.params[i],
|
||||
DEFAULT: def
|
||||
body.push(util.template("default-parameter", {
|
||||
VARIABLE_NAME: node.params[i],
|
||||
DEFAULT_VALUE: def,
|
||||
ARGUMENT_KEY: t.literal(+i),
|
||||
ARGUMENTS: argsIdentifier
|
||||
}, true));
|
||||
}
|
||||
|
||||
// we need to cut off all trailing default parameters
|
||||
node.params = node.params.slice(0, lastNonDefaultParam);
|
||||
|
||||
if (iife) {
|
||||
var container = t.functionExpression(null, [], node.body, node.generator);
|
||||
container._aliasFunction = true;
|
||||
|
||||
@@ -20,11 +20,31 @@ var push = function (opts, nodes, elem, parentId) {
|
||||
pushObjectPattern(opts, nodes, elem, parentId);
|
||||
} else if (t.isArrayPattern(elem)) {
|
||||
pushArrayPattern(opts, nodes, elem, parentId);
|
||||
} else if (t.isAssignmentPattern(elem)) {
|
||||
pushAssignmentPattern(opts, nodes, elem, parentId);
|
||||
} else {
|
||||
nodes.push(buildVariableAssign(opts, elem, parentId));
|
||||
}
|
||||
};
|
||||
|
||||
var pushAssignmentPattern = function (opts, nodes, pattern, parentId) {
|
||||
var tempParentId = opts.scope.generateUidBasedOnNode(parentId, opts.file);
|
||||
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(tempParentId, parentId)
|
||||
]));
|
||||
|
||||
nodes.push(buildVariableAssign(
|
||||
opts,
|
||||
pattern.left,
|
||||
t.conditionalExpression(
|
||||
t.binaryExpression("===", tempParentId, t.identifier("undefined")),
|
||||
pattern.right,
|
||||
tempParentId
|
||||
)
|
||||
));
|
||||
};
|
||||
|
||||
var pushObjectPattern = function (opts, nodes, pattern, parentId) {
|
||||
for (var i in pattern.properties) {
|
||||
var prop = pattern.properties[i];
|
||||
@@ -45,9 +65,11 @@ var pushObjectPattern = function (opts, nodes, pattern, parentId) {
|
||||
}
|
||||
keys = t.arrayExpression(keys);
|
||||
|
||||
var value = t.callExpression(opts.file.addDeclaration("object-without-properties"), [parentId, keys]);
|
||||
var value = t.callExpression(opts.file.addHelper("object-without-properties"), [parentId, keys]);
|
||||
nodes.push(buildVariableAssign(opts, prop.argument, value));
|
||||
} else {
|
||||
if (t.isLiteral(prop.key)) prop.computed = true;
|
||||
|
||||
var pattern2 = prop.value;
|
||||
var patternId2 = t.memberExpression(parentId, prop.key, prop.computed);
|
||||
|
||||
@@ -75,7 +97,7 @@ var pushArrayPattern = function (opts, nodes, pattern, parentId) {
|
||||
|
||||
var toArray = opts.file.toArray(parentId, !hasSpreadElement && pattern.elements.length);
|
||||
|
||||
var _parentId = opts.file.generateUidIdentifier("ref", opts.scope);
|
||||
var _parentId = opts.scope.generateUidBasedOnNode(parentId, opts.file);
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(_parentId, toArray)
|
||||
]));
|
||||
@@ -112,8 +134,8 @@ var pushPattern = function (opts) {
|
||||
var file = opts.file;
|
||||
var scope = opts.scope;
|
||||
|
||||
if (!t.isMemberExpression(parentId) && !t.isIdentifier(parentId)) {
|
||||
var key = file.generateUidIdentifier("ref", scope);
|
||||
if (!t.isArrayExpression(parentId) && !t.isMemberExpression(parentId) && !t.isIdentifier(parentId)) {
|
||||
var key = scope.generateUidBasedOnNode(parentId, file);
|
||||
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(key, parentId)
|
||||
@@ -183,6 +205,24 @@ exports.Function = function (node, parent, file, scope) {
|
||||
block.body = nodes.concat(block.body);
|
||||
};
|
||||
|
||||
exports.CatchClause = function (node, parent, file, scope) {
|
||||
var pattern = node.param;
|
||||
if (!t.isPattern(pattern)) return;
|
||||
|
||||
var ref = file.generateUidIdentifier("ref", scope);
|
||||
node.param = ref;
|
||||
|
||||
var nodes = [];
|
||||
|
||||
push({
|
||||
kind: "var",
|
||||
file: file,
|
||||
scope: scope
|
||||
}, nodes, pattern, ref);
|
||||
|
||||
node.body.body = nodes.concat(node.body.body);
|
||||
};
|
||||
|
||||
exports.ExpressionStatement = function (node, parent, file, scope) {
|
||||
var expr = node.expression;
|
||||
if (expr.type !== "AssignmentExpression") return;
|
||||
@@ -260,10 +300,17 @@ exports.VariableDeclaration = function (node, parent, file, scope) {
|
||||
file: file,
|
||||
scope: scope
|
||||
};
|
||||
|
||||
if (t.isPattern(pattern) && patternId) {
|
||||
pushPattern(opts);
|
||||
|
||||
if (+i !== node.declarations.length - 1) {
|
||||
// we aren't the last declarator so let's just make the
|
||||
// last transformed node inherit from us
|
||||
t.inherits(nodes[nodes.length - 1], declar);
|
||||
}
|
||||
} else {
|
||||
nodes.push(buildVariableAssign(opts, declar.id, declar.init));
|
||||
nodes.push(t.inherits(buildVariableAssign(opts, declar.id, declar.init), declar));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ exports.ForOfStatement = function (node, parent, file, scope) {
|
||||
var stepKey = file.generateUidIdentifier("step", scope);
|
||||
var stepValue = t.memberExpression(stepKey, t.identifier("value"));
|
||||
|
||||
if (t.isIdentifier(left)) {
|
||||
if (t.isIdentifier(left) || t.isPattern(left)) {
|
||||
declar = t.expressionStatement(t.assignmentExpression("=", left, stepValue));
|
||||
} else if (t.isVariableDeclaration(left)) {
|
||||
declar = t.variableDeclaration(left.kind, [
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
var regenerator = require("regenerator");
|
||||
|
||||
exports.ast = {
|
||||
before: require("regenerator").transform
|
||||
before: function (ast, file) {
|
||||
regenerator.transform(ast, {
|
||||
includeRuntime: file.opts.includeRegenerator && "if used"
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -280,7 +280,7 @@ LetScoping.prototype.checkLoop = function () {
|
||||
var replace;
|
||||
|
||||
if (t.isFunction(node) || t.isLoop(node)) {
|
||||
return this.stop();
|
||||
return this.skip();
|
||||
}
|
||||
|
||||
if (node && !node.label) {
|
||||
@@ -329,7 +329,7 @@ LetScoping.prototype.hoistVarDeclarations = function () {
|
||||
} else if (isVar(node, parent)) {
|
||||
return self.pushDeclar(node).map(t.expressionStatement);
|
||||
} else if (t.isFunction(node)) {
|
||||
return this.stop();
|
||||
return this.skip();
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -376,7 +376,7 @@ LetScoping.prototype.getLetReferences = function () {
|
||||
|
||||
// this scope has a variable with the same name so it couldn't belong
|
||||
// to our let scope
|
||||
if (scope.hasOwn(node.name)) return;
|
||||
if (scope.hasOwn(node.name, true)) return;
|
||||
|
||||
closurify = true;
|
||||
|
||||
@@ -388,9 +388,7 @@ LetScoping.prototype.getLetReferences = function () {
|
||||
}
|
||||
});
|
||||
|
||||
return this.stop();
|
||||
} else if (t.isLoop(node)) {
|
||||
return this.stop();
|
||||
return this.skip();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
var t = require("../../types");
|
||||
|
||||
exports.ast = {
|
||||
before: function (ast, file) {
|
||||
ast.program.body = file.dynamicImports.concat(ast.program.body);
|
||||
}
|
||||
};
|
||||
|
||||
exports.ImportDeclaration = function (node, parent, file) {
|
||||
var nodes = [];
|
||||
|
||||
@@ -11,6 +17,12 @@ exports.ImportDeclaration = function (node, parent, file) {
|
||||
file.moduleFormatter.importDeclaration(node, nodes, parent);
|
||||
}
|
||||
|
||||
if (nodes.length === 1) {
|
||||
// inherit `_blockHoist`
|
||||
// this for `_blockHoist` in File.prototype.addImport
|
||||
nodes[0]._blockHoist = node._blockHoist;
|
||||
}
|
||||
|
||||
return nodes;
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
var traverse = require("../../traverse");
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
|
||||
exports.Property = function (node, parent, file, scope) {
|
||||
if (!node.method) return;
|
||||
|
||||
node.method = false;
|
||||
|
||||
var key = t.toComputedKey(node, node.key);
|
||||
if (!t.isLiteral(key)) return; // we can't set a function id with this
|
||||
|
||||
var id = t.toIdentifier(key.value);
|
||||
key = t.identifier(id);
|
||||
|
||||
var selfReference = false;
|
||||
var outerDeclar = scope.get(id, true);
|
||||
|
||||
traverse(node, {
|
||||
enter: function (node, parent, scope) {
|
||||
// check if this node is an identifier that matches the same as our function id
|
||||
if (!t.isIdentifier(node, { name: id })) return;
|
||||
|
||||
// check if this node is the one referenced
|
||||
if (!t.isReferenced(node, parent)) return;
|
||||
|
||||
// check that we don't have a local variable declared as that removes the need
|
||||
// for the wrapper
|
||||
var localDeclar = scope.get(id, true);
|
||||
if (localDeclar !== outerDeclar) return;
|
||||
|
||||
selfReference = true;
|
||||
this.stop();
|
||||
}
|
||||
}, scope);
|
||||
|
||||
if (selfReference) {
|
||||
node.value = util.template("property-method-assignment-wrapper", {
|
||||
FUNCTION: node.value,
|
||||
FUNCTION_ID: key,
|
||||
FUNCTION_KEY: file.generateUidIdentifier(id, scope),
|
||||
WRAPPER_KEY: file.generateUidIdentifier(id + "Wrapper", scope)
|
||||
});
|
||||
} else {
|
||||
node.value.id = key;
|
||||
}
|
||||
};
|
||||
|
||||
exports.ObjectExpression = function (node) {
|
||||
var mutatorMap = {};
|
||||
var hasAny = false;
|
||||
|
||||
node.properties = node.properties.filter(function (prop) {
|
||||
if (prop.kind === "get" || prop.kind === "set") {
|
||||
hasAny = true;
|
||||
util.pushMutatorMap(mutatorMap, prop.key, prop.kind, prop.computed, prop.value);
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
if (!hasAny) return;
|
||||
|
||||
return t.callExpression(
|
||||
t.memberExpression(t.identifier("Object"), t.identifier("defineProperties")),
|
||||
[node, util.buildDefineProperties(mutatorMap)]
|
||||
);
|
||||
};
|
||||
@@ -1,4 +1,5 @@
|
||||
var t = require("../../types");
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
|
||||
exports.Function = function (node, parent, file) {
|
||||
if (!node.rest) return;
|
||||
@@ -8,15 +9,28 @@ exports.Function = function (node, parent, file) {
|
||||
|
||||
t.ensureBlock(node);
|
||||
|
||||
var call = file.toArray(t.identifier("arguments"));
|
||||
var argsId = t.identifier("arguments");
|
||||
argsId._ignoreAliasFunctions = true;
|
||||
|
||||
var start = t.literal(node.params.length);
|
||||
var key = file.generateUidIdentifier("key");
|
||||
|
||||
var arrKey = key;
|
||||
if (node.params.length) {
|
||||
call.arguments.push(t.literal(node.params.length));
|
||||
arrKey = t.binaryExpression("-", arrKey, start);
|
||||
}
|
||||
|
||||
call._ignoreAliasFunctions = true;
|
||||
node.body.body.unshift(
|
||||
t.variableDeclaration("var", [
|
||||
t.variableDeclarator(rest, t.arrayExpression([]))
|
||||
]),
|
||||
|
||||
node.body.body.unshift(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(rest, call)
|
||||
]));
|
||||
util.template("rest", {
|
||||
ARGUMENTS: argsId,
|
||||
ARRAY_KEY: arrKey,
|
||||
START: start,
|
||||
ARRAY: rest,
|
||||
KEY: key
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
var getSpreadLiteral = function (spread, file) {
|
||||
return file.toArray(spread.argument);
|
||||
@@ -58,7 +59,7 @@ exports.CallExpression = function (node, parent, file, scope) {
|
||||
var args = node.arguments;
|
||||
if (!hasSpread(args)) return;
|
||||
|
||||
var contextLiteral = t.literal(null);
|
||||
var contextLiteral = t.identifier("undefined");
|
||||
|
||||
node.arguments = [];
|
||||
|
||||
@@ -77,23 +78,16 @@ exports.CallExpression = function (node, parent, file, scope) {
|
||||
}
|
||||
|
||||
var callee = node.callee;
|
||||
var temp;
|
||||
|
||||
if (t.isMemberExpression(callee)) {
|
||||
contextLiteral = callee.object;
|
||||
|
||||
if (t.isDynamic(contextLiteral)) {
|
||||
temp = contextLiteral = scope.generateTemp(file);
|
||||
var temp = scope.generateTempBasedOnNode(callee.object, file);
|
||||
if (temp) {
|
||||
callee.object = t.assignmentExpression("=", temp, callee.object);
|
||||
}
|
||||
|
||||
if (callee.computed) {
|
||||
callee.object = t.memberExpression(callee.object, callee.property, true);
|
||||
callee.property = t.identifier("apply");
|
||||
callee.computed = false;
|
||||
contextLiteral = temp;
|
||||
} else {
|
||||
callee.property = t.memberExpression(callee.property, t.identifier("apply"));
|
||||
contextLiteral = callee.object;
|
||||
}
|
||||
t.appendToMemberExpression(callee, t.identifier("apply"));
|
||||
} else {
|
||||
node.callee = t.memberExpression(node.callee, t.identifier("apply"));
|
||||
}
|
||||
@@ -105,7 +99,14 @@ exports.NewExpression = function (node, parent, file) {
|
||||
var args = node.arguments;
|
||||
if (!hasSpread(args)) return;
|
||||
|
||||
var nativeType = t.isIdentifier(node.callee) && _.contains(t.NATIVE_TYPE_NAMES, node.callee.name);
|
||||
|
||||
var nodes = build(args, file);
|
||||
|
||||
if (nativeType) {
|
||||
nodes.unshift(t.arrayExpression([t.literal(null)]));
|
||||
}
|
||||
|
||||
var first = nodes.shift();
|
||||
|
||||
if (nodes.length) {
|
||||
@@ -114,5 +115,15 @@ exports.NewExpression = function (node, parent, file) {
|
||||
args = first;
|
||||
}
|
||||
|
||||
return t.callExpression(file.addDeclaration("apply-constructor"), [node.callee, args]);
|
||||
if (nativeType) {
|
||||
return t.newExpression(
|
||||
t.callExpression(
|
||||
t.memberExpression(file.addHelper("bind"), t.identifier("apply")),
|
||||
[node.callee, args]
|
||||
),
|
||||
[]
|
||||
);
|
||||
} else {
|
||||
return t.callExpression(file.addHelper("apply-constructor"), [node.callee, args]);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -17,7 +17,7 @@ exports.TaggedTemplateExpression = function (node, parent, file) {
|
||||
raw.push(t.literal(elem.value.raw));
|
||||
}
|
||||
|
||||
args.push(t.callExpression(file.addDeclaration("tagged-template-literal"), [
|
||||
args.push(t.callExpression(file.addHelper("tagged-template-literal"), [
|
||||
t.arrayExpression(strings),
|
||||
t.arrayExpression(raw)
|
||||
]));
|
||||
|
||||
@@ -30,10 +30,8 @@ exports.AssignmentExpression = function (node, parent, file, scope) {
|
||||
|
||||
// we need to return `node.right`
|
||||
if (!t.isExpressionStatement(parent)) {
|
||||
// `node.right` isn't a simple identifier so we need to reference it
|
||||
if (t.isDynamic(value)) {
|
||||
temp = value = scope.generateTemp(file);
|
||||
}
|
||||
temp = scope.generateTempBasedOnNode(node.right, file);
|
||||
if (temp) value = temp;
|
||||
}
|
||||
|
||||
if (node.operator !== "=") {
|
||||
@@ -80,11 +78,7 @@ exports.CallExpression = function (node, parent, file, scope) {
|
||||
var callee = node.callee;
|
||||
if (!t.isVirtualPropertyExpression(callee)) return;
|
||||
|
||||
var temp;
|
||||
if (t.isDynamic(callee.object)) {
|
||||
// we need to save `callee.object` so we can call it again
|
||||
temp = scope.generateTemp(file);
|
||||
}
|
||||
var temp = scope.generateTempBasedOnNode(callee.object, file);
|
||||
|
||||
var call = util.template("abstract-expression-call", {
|
||||
PROPERTY: callee.property,
|
||||
|
||||
@@ -4,8 +4,8 @@ var t = require("../../types");
|
||||
|
||||
exports.experimental = true;
|
||||
|
||||
var build = function (node, file) {
|
||||
var uid = file.generateUidIdentifier("arr");
|
||||
var build = function (node, parent, file, scope) {
|
||||
var uid = scope.generateUidBasedOnNode(parent, file);
|
||||
|
||||
var container = util.template("array-comprehension-container", {
|
||||
KEY: uid
|
||||
@@ -49,14 +49,14 @@ exports._build = function (node, buildBody) {
|
||||
}
|
||||
|
||||
return t.forOfStatement(
|
||||
t.variableDeclaration("var", [t.variableDeclarator(self.left)]),
|
||||
t.variableDeclaration("let", [t.variableDeclarator(self.left)]),
|
||||
self.right,
|
||||
t.blockStatement([child])
|
||||
);
|
||||
};
|
||||
|
||||
exports.ComprehensionExpression = function (node, parent, file) {
|
||||
exports.ComprehensionExpression = function (node, parent, file, scope) {
|
||||
if (node.generator) return;
|
||||
|
||||
return build(node, file);
|
||||
return build(node, parent, file, scope);
|
||||
};
|
||||
|
||||
@@ -4,7 +4,7 @@ var t = require("../../types");
|
||||
|
||||
exports.experimental = true;
|
||||
|
||||
exports.ObjectExpression = function (node) {
|
||||
exports.ObjectExpression = function (node, parent, file) {
|
||||
var hasSpread = false;
|
||||
var i;
|
||||
var prop;
|
||||
@@ -42,5 +42,5 @@ exports.ObjectExpression = function (node) {
|
||||
args.unshift(t.objectExpression([]));
|
||||
}
|
||||
|
||||
return t.callExpression(t.memberExpression(t.identifier("Object"), t.identifier("assign")), args);
|
||||
return t.callExpression(file.addHelper("extends"), args);
|
||||
};
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
var bluebirdCoroutines = require("./optional-bluebird-coroutines");
|
||||
var t = require("../../types");
|
||||
|
||||
exports.optional = true;
|
||||
|
||||
@@ -8,7 +7,5 @@ exports.manipulateOptions = bluebirdCoroutines.manipulateOptions;
|
||||
exports.Function = function (node, parent, file) {
|
||||
if (!node.async || node.generator) return;
|
||||
|
||||
bluebirdCoroutines._Function(node);
|
||||
|
||||
return t.callExpression(file.addDeclaration("async-to-generator"), [node]);
|
||||
return bluebirdCoroutines._Function(node, file.addHelper("async-to-generator"));
|
||||
};
|
||||
|
||||
@@ -8,27 +8,36 @@ exports.manipulateOptions = function (opts) {
|
||||
|
||||
exports.optional = true;
|
||||
|
||||
exports._Function = function (node) {
|
||||
exports._Function = function (node, callId) {
|
||||
node.async = false;
|
||||
node.generator = true;
|
||||
|
||||
traverse(node, {
|
||||
enter: function (node) {
|
||||
if (t.isFunction(node)) this.stop();
|
||||
if (t.isFunction(node)) this.skip();
|
||||
|
||||
if (t.isAwaitExpression(node)) {
|
||||
node.type = "YieldExpression";
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var call = t.callExpression(callId, [node]);
|
||||
|
||||
if (t.isFunctionDeclaration(node)) {
|
||||
var declar = t.variableDeclaration("var", [
|
||||
t.variableDeclarator(node.id, call)
|
||||
]);
|
||||
declar._blockHoist = true;
|
||||
return declar;
|
||||
} else {
|
||||
return call;
|
||||
}
|
||||
};
|
||||
|
||||
exports.Function = function (node, parent, file) {
|
||||
if (!node.async || node.generator) return;
|
||||
|
||||
exports._Function(node);
|
||||
|
||||
var id = t.identifier("Bluebird");
|
||||
file.addImport(id, "bluebird");
|
||||
return t.callExpression(t.memberExpression(id, t.identifier("coroutine")), [node]);
|
||||
var id = file.addImport("bluebird");
|
||||
return exports._Function(node, t.memberExpression(id, t.identifier("coroutine")));
|
||||
};
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
var traverse = require("../../traverse");
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
|
||||
exports.optional = true;
|
||||
|
||||
exports.Class = function (node) {
|
||||
var superClass = node.superClass || t.identifier("Function");
|
||||
|
||||
var hasConstructor = false;
|
||||
var body = node.body.body;
|
||||
|
||||
for (var i in body) {
|
||||
var methodNode = body[i];
|
||||
|
||||
hasConstructor = hasConstructor || methodNode.key.name === "constructor";
|
||||
|
||||
traverse(methodNode, {
|
||||
enter: function (node, parent) {
|
||||
if (t.isIdentifier(node, { name: "super" })) {
|
||||
return superIdentifier(superClass, methodNode, node, parent);
|
||||
} else if (t.isCallExpression(node)) {
|
||||
var callee = node.callee;
|
||||
if (!t.isMemberExpression(callee)) return;
|
||||
if (callee.object.name !== "super") return;
|
||||
|
||||
// super.test(); -> ClassName.prototype.MethodName.call(this);
|
||||
t.appendToMemberExpression(callee, t.identifier("call"));
|
||||
node.arguments.unshift(t.thisExpression());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (node.superClass && !hasConstructor) {
|
||||
body.unshift(t.methodDefinition(
|
||||
t.identifier("constructor"),
|
||||
util.template("class-super-constructor-call-fast", {
|
||||
SUPER_NAME: superClass
|
||||
})
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
var superIdentifier = function (superClass, methodNode, id, parent) {
|
||||
var methodName = methodNode.key;
|
||||
|
||||
if (parent.property === id) {
|
||||
return;
|
||||
} else if (t.isCallExpression(parent, { callee: id })) {
|
||||
// super(); -> ClassName.prototype.MethodName.call(this);
|
||||
parent.arguments.unshift(t.thisExpression());
|
||||
|
||||
if (methodName.name === "constructor") {
|
||||
// constructor() { super(); }
|
||||
return t.memberExpression(superClass, t.identifier("call"));
|
||||
} else {
|
||||
id = superClass;
|
||||
|
||||
// foo() { super(); }
|
||||
if (!methodNode.static) {
|
||||
id = t.memberExpression(id, t.identifier("prototype"));
|
||||
}
|
||||
|
||||
id = t.memberExpression(id, methodName, methodNode.computed);
|
||||
return t.memberExpression(id, t.identifier("call"));
|
||||
}
|
||||
} else if (t.isMemberExpression(parent) && !methodNode.static) {
|
||||
// super.test -> ClassName.prototype.test
|
||||
return t.memberExpression(superClass, t.identifier("prototype"));
|
||||
} else {
|
||||
return superClass;
|
||||
}
|
||||
};
|
||||
@@ -4,12 +4,24 @@ var core = require("core-js/library");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
var coreHas = function (node) {
|
||||
return node.name !== "_" && _.has(core, node.name);
|
||||
};
|
||||
|
||||
var ALIASABLE_CONSTRUCTORS = [
|
||||
"Symbol",
|
||||
"Promise",
|
||||
"Map",
|
||||
"WeakMap",
|
||||
"Set",
|
||||
"WeakSet"
|
||||
];
|
||||
|
||||
exports.optional = true;
|
||||
|
||||
exports.ast = {
|
||||
enter: function (ast, file) {
|
||||
file._coreId = file.generateUidIdentifier("core");
|
||||
file.addImport(file._coreId, "core-js/library");
|
||||
file._coreId = file.addImport("core-js/library", "core");
|
||||
},
|
||||
|
||||
exit: function (ast, file) {
|
||||
@@ -24,11 +36,13 @@ exports.ast = {
|
||||
|
||||
if (!t.isReferenced(obj, node)) return;
|
||||
|
||||
var coreHasObject = obj.name !== "_" && _.has(core, obj.name);
|
||||
if (coreHasObject && _.has(core[obj.name], prop.name)) {
|
||||
this.stop();
|
||||
return t.memberExpression(file._coreId, node);
|
||||
if (!node.computed && coreHas(obj) && _.has(core[obj.name], prop.name)) {
|
||||
this.skip();
|
||||
return t.prependToMemberExpression(node, file._coreId);
|
||||
}
|
||||
} else if (t.isIdentifier(node) && !t.isMemberExpression(parent) && t.isReferenced(node, parent) && _.contains(ALIASABLE_CONSTRUCTORS, node.name)) {
|
||||
// Symbol() -> _core.Symbol(); new Promise -> new _core.Promise
|
||||
return t.memberExpression(file._coreId, node);
|
||||
} else if (t.isCallExpression(node)) {
|
||||
// arr[Symbol.iterator]() -> _core.$for.getIterator(arr)
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user