Compare commits
381 Commits
v5.0.0-bet
...
v5.1.11
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7af4302d07 | ||
|
|
543770960c | ||
|
|
a2975a72cc | ||
|
|
beb3fd51d8 | ||
|
|
9360860601 | ||
|
|
7d4ecea8fb | ||
|
|
a44831b3fa | ||
|
|
8357f3900f | ||
|
|
6b1c5bc358 | ||
|
|
3564309a77 | ||
|
|
c9518bb60d | ||
|
|
0f1215e33e | ||
|
|
d7b0b5bec0 | ||
|
|
3e5af404ab | ||
|
|
1818094577 | ||
|
|
6d712b3d14 | ||
|
|
11389fe165 | ||
|
|
0ee9531db3 | ||
|
|
1622d2498d | ||
|
|
b76e6f8e5b | ||
|
|
beac006832 | ||
|
|
20d7603c0b | ||
|
|
24b13c5e49 | ||
|
|
9e21074e88 | ||
|
|
9a825bced7 | ||
|
|
fcb78436c6 | ||
|
|
bb30a64f8d | ||
|
|
b483c16fe2 | ||
|
|
569f046045 | ||
|
|
4e969d3d92 | ||
|
|
0bca1de285 | ||
|
|
3a5bd06a7e | ||
|
|
66a908aaa7 | ||
|
|
cd0ed08237 | ||
|
|
dd84b554e6 | ||
|
|
b51411f090 | ||
|
|
4cc843ded3 | ||
|
|
47d253c732 | ||
|
|
09901274ab | ||
|
|
35196e20c7 | ||
|
|
35af771a29 | ||
|
|
dee80caa09 | ||
|
|
46b7cc0a72 | ||
|
|
3b189e22b7 | ||
|
|
6ca565d7fb | ||
|
|
64f5480f96 | ||
|
|
828fdc8769 | ||
|
|
6763c4415e | ||
|
|
fb30e79e03 | ||
|
|
85fb4304b0 | ||
|
|
c2da77d7ec | ||
|
|
c54c3d3c15 | ||
|
|
2a7c954155 | ||
|
|
3fb7f7c7cc | ||
|
|
c4b2818336 | ||
|
|
061561bfd8 | ||
|
|
d4944d606b | ||
|
|
736b689c3b | ||
|
|
e12b377014 | ||
|
|
5fd5b815ab | ||
|
|
92db8312f6 | ||
|
|
e080fe547d | ||
|
|
b967ecf063 | ||
|
|
365b7285d2 | ||
|
|
a40d532b0e | ||
|
|
0487bf911d | ||
|
|
57c72d1cd0 | ||
|
|
335bdffec2 | ||
|
|
e1d76a1dfe | ||
|
|
a4845323a2 | ||
|
|
792951b489 | ||
|
|
5adda836af | ||
|
|
4c2c3098c3 | ||
|
|
087c714098 | ||
|
|
5a33e4fe05 | ||
|
|
5f8667eaa7 | ||
|
|
31b4468514 | ||
|
|
2be9678bbe | ||
|
|
b5210877cf | ||
|
|
658dde768e | ||
|
|
8df1d81ca4 | ||
|
|
a505e4a121 | ||
|
|
71eafdcac7 | ||
|
|
65060b0c97 | ||
|
|
09883962f1 | ||
|
|
1583262807 | ||
|
|
04c7cd5c0c | ||
|
|
3f6969f4f8 | ||
|
|
23db842cb8 | ||
|
|
41c82e00f9 | ||
|
|
766099e783 | ||
|
|
53bc54b3b2 | ||
|
|
bb70f37123 | ||
|
|
8a0317132e | ||
|
|
f598c70841 | ||
|
|
61b57ea055 | ||
|
|
a971ad7fd2 | ||
|
|
27b6f804ce | ||
|
|
8ab79f3a72 | ||
|
|
0c2bf2f5a2 | ||
|
|
7e1a4be085 | ||
|
|
bbc951dfe1 | ||
|
|
b3e4aefe12 | ||
|
|
26b5174dfa | ||
|
|
652d3c7320 | ||
|
|
edc2cd320c | ||
|
|
274a6e01dc | ||
|
|
3561efdb86 | ||
|
|
1a30f1aafc | ||
|
|
e362512af3 | ||
|
|
cf5d2429b4 | ||
|
|
72098036b2 | ||
|
|
7905f48280 | ||
|
|
aba2d1c23c | ||
|
|
5326e0543d | ||
|
|
1360c93e4b | ||
|
|
235cbc18cf | ||
|
|
98c5255b91 | ||
|
|
2a9777cc20 | ||
|
|
a8702be756 | ||
|
|
0995cc3f4e | ||
|
|
1cfca745a4 | ||
|
|
a3459deecf | ||
|
|
070152f6c1 | ||
|
|
719fdf5ca1 | ||
|
|
f2460c6173 | ||
|
|
155c640409 | ||
|
|
7de4718b27 | ||
|
|
4e6aed0408 | ||
|
|
3be8ccf68d | ||
|
|
704b31f44f | ||
|
|
1a9f193841 | ||
|
|
7ff67589a7 | ||
|
|
80f109efeb | ||
|
|
77c72bb5a6 | ||
|
|
6c5e0e6590 | ||
|
|
83324b977d | ||
|
|
5feab11d3f | ||
|
|
7b4172ce7b | ||
|
|
84439384c0 | ||
|
|
c9e7c306cc | ||
|
|
1e398e45ff | ||
|
|
475324c4b5 | ||
|
|
407daf4bac | ||
|
|
4155590cd8 | ||
|
|
0be6fd7abe | ||
|
|
cf51bf1395 | ||
|
|
06a31c419a | ||
|
|
da566110c0 | ||
|
|
c12c4a5c39 | ||
|
|
d114349890 | ||
|
|
8d6ae0e1eb | ||
|
|
cb8b47ee2f | ||
|
|
daf24c5c59 | ||
|
|
cdb2784e75 | ||
|
|
93feabb82e | ||
|
|
982c142bf6 | ||
|
|
de5520a94f | ||
|
|
c239d06f10 | ||
|
|
a5fed376d8 | ||
|
|
e99fd77d89 | ||
|
|
1374863b9c | ||
|
|
e88c28f88b | ||
|
|
7a0fd26f56 | ||
|
|
17583e4807 | ||
|
|
37dd5137ff | ||
|
|
24fced406e | ||
|
|
0ab1362893 | ||
|
|
96506f4249 | ||
|
|
ed747f88bd | ||
|
|
3987545b4f | ||
|
|
148aa3f96d | ||
|
|
0cb5a7c91e | ||
|
|
fc34d5a9b0 | ||
|
|
f8d56d9612 | ||
|
|
737be0e95e | ||
|
|
26e2b392e8 | ||
|
|
708cdfb993 | ||
|
|
8cb3aabefa | ||
|
|
4a87b35d20 | ||
|
|
543554b258 | ||
|
|
afd95cf663 | ||
|
|
87ce4b9cd8 | ||
|
|
6b76f26ed8 | ||
|
|
c2776e63ae | ||
|
|
3f2fe363d1 | ||
|
|
8de28098f4 | ||
|
|
584532cc2c | ||
|
|
9a28f3fdb1 | ||
|
|
88941b3270 | ||
|
|
3a768db2bf | ||
|
|
320a39f4c4 | ||
|
|
dc98ac7c93 | ||
|
|
6e456f0ec1 | ||
|
|
793090628d | ||
|
|
9ed251cb08 | ||
|
|
480fa7f4e0 | ||
|
|
3e642dfa1b | ||
|
|
d9cbce1862 | ||
|
|
4bd19da3c2 | ||
|
|
7c710a0378 | ||
|
|
56335409d3 | ||
|
|
91d78afc67 | ||
|
|
54c6339f20 | ||
|
|
fdcf64265e | ||
|
|
ffdfb491eb | ||
|
|
eedd431f2b | ||
|
|
db9ed0235f | ||
|
|
6c98d39937 | ||
|
|
20651df3ce | ||
|
|
f3155919fe | ||
|
|
0ac8330899 | ||
|
|
67201e9698 | ||
|
|
c715d96e46 | ||
|
|
b7a08100a6 | ||
|
|
5f91ee8a1a | ||
|
|
bc1abb5103 | ||
|
|
4b9207e5df | ||
|
|
e847f3685f | ||
|
|
d64c2c0c45 | ||
|
|
76d0fb4ba6 | ||
|
|
0f33b7bfbc | ||
|
|
630224e504 | ||
|
|
62980ab6b4 | ||
|
|
d34480b42b | ||
|
|
306de2edbf | ||
|
|
c33e84730d | ||
|
|
20f28aba64 | ||
|
|
9c312607d1 | ||
|
|
33659711c3 | ||
|
|
b154af48a7 | ||
|
|
bad877946f | ||
|
|
dff3751e7f | ||
|
|
23038dcfff | ||
|
|
7c7a7ee17f | ||
|
|
9f467ef7c0 | ||
|
|
a52f9ab346 | ||
|
|
5ccbed585b | ||
|
|
f7c7da7526 | ||
|
|
ed58c80c64 | ||
|
|
d7fb9f67df | ||
|
|
eef6002324 | ||
|
|
32bcf4c3a5 | ||
|
|
86060cb0ce | ||
|
|
66a836d435 | ||
|
|
ecc14c2a31 | ||
|
|
7e9b60fba5 | ||
|
|
313f1e755a | ||
|
|
8c7ef0a2c9 | ||
|
|
8f79010b16 | ||
|
|
022e6f26aa | ||
|
|
138fa6c9f8 | ||
|
|
c07704cc80 | ||
|
|
bf811ea34a | ||
|
|
8af3ea1c43 | ||
|
|
5fbe147a5e | ||
|
|
a9e214b1db | ||
|
|
6812fca7a9 | ||
|
|
ebc42f5ce0 | ||
|
|
b12a97ddd6 | ||
|
|
e8daad38a7 | ||
|
|
c6c0bc07e2 | ||
|
|
c60f3f0e34 | ||
|
|
07078138f3 | ||
|
|
cb0026edfe | ||
|
|
ab55ec4ea2 | ||
|
|
e164123ce9 | ||
|
|
f42bacf4f6 | ||
|
|
46adb07d41 | ||
|
|
e10bdf7c5b | ||
|
|
83eea2acc7 | ||
|
|
8bdd4cc559 | ||
|
|
15d31b5608 | ||
|
|
9ed6a7b650 | ||
|
|
01a2aa7dd1 | ||
|
|
33fde5f143 | ||
|
|
682c775a97 | ||
|
|
4846b89931 | ||
|
|
25312396a5 | ||
|
|
cfff7aa6fb | ||
|
|
f88a4147a6 | ||
|
|
3c343adf33 | ||
|
|
269587cde5 | ||
|
|
121ba8c381 | ||
|
|
563a427b69 | ||
|
|
658164a0d2 | ||
|
|
135ba84abb | ||
|
|
76573093bd | ||
|
|
22b88487ee | ||
|
|
3686951dc1 | ||
|
|
f43ca32ff5 | ||
|
|
424a59a856 | ||
|
|
5f159853d5 | ||
|
|
f9c56c6dd7 | ||
|
|
613e77cd50 | ||
|
|
27c97aab34 | ||
|
|
ad857dddd1 | ||
|
|
d9762e9049 | ||
|
|
088846a347 | ||
|
|
7fc28cb63f | ||
|
|
3323e0f3a5 | ||
|
|
8c09a2198a | ||
|
|
eeb5b8d0c3 | ||
|
|
19e8274e79 | ||
|
|
6353abfd1b | ||
|
|
ee14770a5c | ||
|
|
d3b9b7251e | ||
|
|
3f7f89a6b9 | ||
|
|
7476b8c6a3 | ||
|
|
1ef41b6f32 | ||
|
|
6bc2bfce7d | ||
|
|
153e81261c | ||
|
|
b8f2a27e90 | ||
|
|
2f25c3bd17 | ||
|
|
59e07b65dd | ||
|
|
769f7fb81d | ||
|
|
7895857624 | ||
|
|
80fed5f395 | ||
|
|
1c7fabdd6d | ||
|
|
12f8fa229d | ||
|
|
c6546264d2 | ||
|
|
86cc5875cf | ||
|
|
9ea999b261 | ||
|
|
f81e841283 | ||
|
|
c14ffb8319 | ||
|
|
b9f4fe86f1 | ||
|
|
56ada634b4 | ||
|
|
c4d68897b3 | ||
|
|
1641caedc4 | ||
|
|
3d6e722fc7 | ||
|
|
253493a43f | ||
|
|
66471f044a | ||
|
|
5fa227eb73 | ||
|
|
f3b50f0bb9 | ||
|
|
92bba76bd5 | ||
|
|
46462e3e69 | ||
|
|
bc155f956c | ||
|
|
dc1ba58705 | ||
|
|
99e274921f | ||
|
|
862bd6d92a | ||
|
|
46d991e3e9 | ||
|
|
8e1b6df970 | ||
|
|
f9c8d7d1fd | ||
|
|
eb2f61e43f | ||
|
|
afe8792ba4 | ||
|
|
42a2430d2c | ||
|
|
c4d260231b | ||
|
|
ca5daca5dd | ||
|
|
3952eefd01 | ||
|
|
52c6fe2bc1 | ||
|
|
6a698f7ae4 | ||
|
|
2b0c07c2e7 | ||
|
|
e27e7facac | ||
|
|
0dd32e7d9c | ||
|
|
286ae68da2 | ||
|
|
e26f994075 | ||
|
|
94f005e324 | ||
|
|
93e37aaaf1 | ||
|
|
e4db93f6a5 | ||
|
|
c9243e8e97 | ||
|
|
bbba374783 | ||
|
|
7d95fddbed | ||
|
|
617ed09227 | ||
|
|
6d7887cfe2 | ||
|
|
b8e9171aaa | ||
|
|
22118c0be0 | ||
|
|
3754f7615f | ||
|
|
b54901018b | ||
|
|
121b9ca063 | ||
|
|
4988a27b6c | ||
|
|
e0297e08b8 | ||
|
|
79005d2f03 | ||
|
|
5cce8c32a4 | ||
|
|
95b1accddc | ||
|
|
6149e6325f | ||
|
|
4c9d4d0378 | ||
|
|
2e599bef4f | ||
|
|
885da177f9 | ||
|
|
463112517f | ||
|
|
048f9acc55 | ||
|
|
ed6421876a |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -11,7 +11,7 @@ test/core/tmp
|
||||
coverage
|
||||
dist
|
||||
.package.json
|
||||
packages/babel-runtime/*.js
|
||||
packages/babel-runtime/core-js
|
||||
packages/babel-runtime/helpers/*.js
|
||||
packages/babel-runtime/regenerator/*.js
|
||||
lib
|
||||
|
||||
25
.npmignore
25
.npmignore
@@ -1,16 +1,15 @@
|
||||
node_modules
|
||||
*.log
|
||||
*.cache
|
||||
lib/babel/transformation/templates
|
||||
test
|
||||
benchmark
|
||||
Makefile
|
||||
.*
|
||||
dist
|
||||
tests.json
|
||||
CHANGELOG.md
|
||||
.package.json
|
||||
coverage
|
||||
vendor
|
||||
packages
|
||||
src
|
||||
/lib/babel/transformation/templates
|
||||
/test
|
||||
/benchmark
|
||||
/Makefile
|
||||
/dist
|
||||
/tests.json
|
||||
/CHANGELOG.md
|
||||
/.package.json
|
||||
/coverage
|
||||
/vendor
|
||||
/packages
|
||||
/src
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
sudo: false
|
||||
language: node_js
|
||||
cache:
|
||||
directories:
|
||||
- node_modules
|
||||
node_js:
|
||||
- "0.12"
|
||||
- "iojs"
|
||||
|
||||
branches:
|
||||
except:
|
||||
- experimental
|
||||
|
||||
before_script: "npm install -g codeclimate-test-reporter"
|
||||
script: "make test-travis"
|
||||
|
||||
|
||||
217
CHANGELOG.md
217
CHANGELOG.md
@@ -13,6 +13,223 @@ _Note: Gaps between patch versions are faulty/broken releases._
|
||||
|
||||
See [CHANGELOG - 6to5](CHANGELOG-6to5.md) for the pre-4.0.0 version changelog.
|
||||
|
||||
## 5.1.11
|
||||
|
||||
* **Bug Fix**
|
||||
* Memoise and bind member expression decorators.
|
||||
* Move JSX children cleaning to opening element visitor. Fixes elements not being cleaned in certain scenarios.
|
||||
* Consider `SwitchStatement`s to be `Scopable`.
|
||||
* Fix `bluebirdCoroutines` calling `interopRequireWildcard` before it's defined.
|
||||
* Add space to `do...while` code generation.
|
||||
* Validate `super` use before `this` on `super` exit rather than entrance.
|
||||
* **Polish**
|
||||
* Add Babel name to logger.
|
||||
|
||||
## 5.1.10
|
||||
|
||||
* **Bug Fix**
|
||||
* Remove `makePredicate` from acorn in favor of an `indexOf`.
|
||||
* Remove statements to expression explosion when inserting a block statement.
|
||||
* **Internal**
|
||||
* Remove runtime compatibility check.
|
||||
|
||||
## 5.1.9
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix class property initializers with `undefined` values not being correctly writable.
|
||||
* Fix self inferring generators incorrectly causing a stack error.
|
||||
* Fix default export specifiers not triggering AMD `module` argument inclusion.
|
||||
* Fix assignments not having their module references properly remapped.
|
||||
* **Internal**
|
||||
* Upgrade to latest `acorn`.
|
||||
* **Polish**
|
||||
* Make invalid LHS pattern error messages nicer.
|
||||
|
||||
## 5.1.8
|
||||
|
||||
* **Bug Fix**
|
||||
* Only make parenthesized object pattern LHS illegal.
|
||||
|
||||
## 5.1.7
|
||||
|
||||
* **Internal**
|
||||
* Add `parse` node API.
|
||||
|
||||
## 5.1.6
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix `runtime` built-in catchall not properly checking for local variables.
|
||||
|
||||
## 5.1.5
|
||||
|
||||
* **Internal**
|
||||
* Bump `core-js` version.
|
||||
|
||||
## 5.1.4
|
||||
|
||||
* **Polish**
|
||||
* Add missing `Reflect` methods to runtime transformer.
|
||||
|
||||
## 5.1.3
|
||||
|
||||
* **Internal**
|
||||
* Switch entirely to vanilla regenerator.
|
||||
* Clean up and make the parsing of decorators stateless.
|
||||
* **Bug Fix**
|
||||
* Don't do TCO on generators and async functions.
|
||||
* Add missing `core-js` runtime definitions.
|
||||
|
||||
## 5.1.2
|
||||
|
||||
* **Bug Fix**
|
||||
* Add `getIterator` and `isIterable` to `babel-runtime` build script.
|
||||
|
||||
## 5.1.1
|
||||
|
||||
* **Bug Fix**
|
||||
* Add missing runtime symbol definitions.
|
||||
|
||||
## 5.1.0
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix super reference when using decorators.
|
||||
* Don't do array unpack optimisation when member expressions are present.
|
||||
* Add missing descriptors for undecorated class properties.
|
||||
* Don't consider `arguments` and `eval` valid function names when doing function name inferrence.
|
||||
* Fix scope tracking of constants in loop heads.
|
||||
* Parse `AwaitExpression` as a unary instead of an assignment.
|
||||
* Fix regex evaluation when attempting static evaluation.
|
||||
* Don't emit tokens when doing a lookahead.
|
||||
* Add missing `test` declaration to `utility.deadCodeElimination` transformer.
|
||||
* **Internal**
|
||||
* Upgrade `regenerator` to the latest and use my branch with the hope of eventually switching to vanilla regenerator.
|
||||
* Add support for the replacement of for loop `init`s with statements.
|
||||
* Upgrade dependencies.
|
||||
* **Polish**
|
||||
* When adding the scope IIFE when using default parameters, don't shadow the function expression, just `apply` `this` and `arguments` if necessary.
|
||||
* Use path basename as non-default import fallback.
|
||||
* **New Feature**
|
||||
* Add [trailing function comma proposal](https://github.com/jeffmo/es-trailing-function-commas). Thanks [@AluisioASG](https://github.com/AluisioASG)!
|
||||
* Add support for object literal decorators.
|
||||
* Make core-js modular when using the `runtime` transformer.
|
||||
|
||||
## 5.0.12
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix incorrect remapping of module references inside of a function id redirection container.
|
||||
|
||||
## 5.0.11
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix new `for...of` loops not properly inheriting their original loop.
|
||||
* **Internal**
|
||||
* Disable scope instance cache.
|
||||
* **Polish**
|
||||
* Allow comments in `.babelrc` JSON.
|
||||
|
||||
## 5.0.9
|
||||
|
||||
* **Polish**
|
||||
* Use `moduleId` for UMD global name if available.
|
||||
* **Bug Fix**
|
||||
* Fix UMD global `module` variable shadowing the `amd`/`common` `module` variable.
|
||||
* Fix Flow param type annotation regression.
|
||||
* Fix function name collision `toString` wrapper. Thanks [@alawatthe](https://github.com/alawatthe)!
|
||||
|
||||
## 5.0.8
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix falsy static class properties not being writable.
|
||||
* Fix block scoping collisions not properly detecting modules and function clashes.
|
||||
* Skip `this` before `super` for derived constructors on functions.
|
||||
|
||||
## 5.0.7
|
||||
|
||||
* **New Feature**
|
||||
* Add `--ignore` and `--only` support to the CLI.
|
||||
* **Bug Fix**
|
||||
* Remove `HOMEPATH` environment variable from home resolution in `babel/register` cache.
|
||||
* **Internal**
|
||||
* Disable WIP path resolution introducing infinite recursion in some code examples.
|
||||
* **Polish**
|
||||
* Add live binding to CommonJS default imports.
|
||||
|
||||
## 5.0.6
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix mangling of import references that collide with properties on `Object.prototype`.
|
||||
* Fix duplicate declarations incorrectly being reported for `var`.
|
||||
|
||||
## 5.0.5
|
||||
|
||||
* **Internal**
|
||||
* Upgrade `core-js`.
|
||||
* **Bug Fix**
|
||||
* Fix arrays not being supported in `util.list`.
|
||||
|
||||
## 5.0.4
|
||||
|
||||
* **Polish**
|
||||
* Check for top level `breakConfig` in `resolveRc`.
|
||||
|
||||
## 5.0.3
|
||||
|
||||
* **Bug Fix**
|
||||
* Make relative location absolute before calling `resolveRc`.
|
||||
* **Internal**
|
||||
* Switch to global UID registry.
|
||||
* Add `breakConfig` option to prevent Babel from erroring when hitting that option.
|
||||
|
||||
## 5.0.1
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix duplicate declaration regression.
|
||||
* Fix not being able to call non-writable methods.
|
||||
|
||||
## 5.0.0
|
||||
|
||||
* **New Feature**
|
||||
* Decorators based on [@wycat's](https://github.com/wycats) [stage 1 proposal](https://github.com/wycats/javascript-decorators).
|
||||
* Class property initializers based on [@jeffmo's](https://github.com/jeffmo) [stage 0 proposal](https://gist.github.com/jeffmo/054df782c05639da2adb).
|
||||
* Export extensions based on [@leebyron's](https://github.com/leebyron) [stage 1 proposal](https://github.com/leebyron/ecmascript-more-export-from).
|
||||
* UMD module formatter now supports globals.
|
||||
* Add `es3.runtime`, `optimisation.react.inlineElements` and `optimisation.react.constantElements` transformers.
|
||||
* Add stage option that replaces the experimental one.
|
||||
* Allow ES7 transformer to be enabled via `optional` instead of only via `stage`.
|
||||
* Infer string quotes to use in the code generator.
|
||||
* Consider `export { foo as default };` to be the same as `export default foo;`.
|
||||
* Add `nonStandard` option that can be set to `false` to remove parser support for JSX and Flow.
|
||||
* Add `jsxPragma` option.
|
||||
* Automatically generate CLI options based on internal API options.
|
||||
* Add support for `.babelrc` on absolute paths.
|
||||
* Plugin API!
|
||||
* **Internal**
|
||||
* Export `options` in browser API.
|
||||
* Rewritten parser.
|
||||
* Don't block hoist when runtime transformer is enabled in system module formatter.
|
||||
* Rewritten the internal traversal and node replacement API to use "paths" that abstracts out node relationships.
|
||||
* **Polish**
|
||||
* JSX output is now more inline with the official JSX transformer.
|
||||
* Hoist block scoping IIFE - this improves memory usage and performance.
|
||||
* Better IIFE detection - references are now checked to see if they're referencing the binding we're searching for.
|
||||
* Check for import reassignments in constants transformer.
|
||||
* Make method definitions with expression bodies illegal.
|
||||
* Save register cache on tick instead of `SIGINT`.
|
||||
* Enable strict mode on babel-node eval flag.
|
||||
* **Bug Fixes**
|
||||
* Add support for live bindings. This change also increases the reliablity of export specifier renaming.
|
||||
* Add support for super update and non equals assignment expressions.
|
||||
* Rename shadow constructor binding in classes.
|
||||
* Seed next iteration bindings with previous fresh bindings when reassinging loop block scoped variables.
|
||||
* Fix new expression spread referencing the wrong constructor.
|
||||
* Call `resolveModuleSource` on dynamic imports.
|
||||
* Added `param` to list of duplicate declaration kinds.
|
||||
* **Breaking Changes**
|
||||
* The Babel playground has been removed.
|
||||
* ES7 Abstract References have been removed.
|
||||
* Experimental option has been removed in favor of a stage option.
|
||||
* Rename `returnUsedHelpers` to `metadataUsedHelpers`.
|
||||
|
||||
## 4.7.16
|
||||
|
||||
* **Bug Fix**
|
||||
|
||||
@@ -1,9 +1,24 @@
|
||||
<p align="center">
|
||||
<strong><a href="#setup">Setup</a></strong>
|
||||
|
|
||||
<strong><a href="#running-tests">Running tests</a></strong>
|
||||
|
|
||||
<strong><a href="#workflow">Workflow</a></strong>
|
||||
|
|
||||
<strong><a href="#dependencies">Dependencies</a></strong>
|
||||
|
|
||||
<strong><a href="#code-standards">Code Standards</a></strong>
|
||||
</p>
|
||||
|
||||
----
|
||||
|
||||
# Contributing
|
||||
|
||||
Contributions are always welcome, no matter how large or small. Before
|
||||
contributing, please read the
|
||||
[code of conduct](https://github.com/babel/babel/blob/master/CODE_OF_CONDUCT.md).
|
||||
|
||||
|
||||
## Developing
|
||||
|
||||
#### Setup
|
||||
@@ -20,8 +35,10 @@ Then you need to run:
|
||||
$ make watch-core
|
||||
```
|
||||
|
||||
This will compile babel and then sit in the background and on file modification
|
||||
recompile the necessary files.
|
||||
This will compile Babel and then sit in the background and on file modification
|
||||
recompile the necessary files. Babel itself is written in ES6. The source files
|
||||
reside in `src/` and transpile to `lib/`
|
||||
|
||||
|
||||
#### Running tests
|
||||
|
||||
@@ -31,21 +48,24 @@ You can run tests via:
|
||||
$ make test
|
||||
```
|
||||
|
||||
This will usually take around two minutes as it's compiling the entire
|
||||
[test262](https://github.com/tc39/test262) test suite and validating its AST.
|
||||
This is mostly overkill and you can limit the tests to a select few by directly
|
||||
running them with `mocha`:
|
||||
|
||||
```sh
|
||||
$ mocha test/transformation.js
|
||||
$ mocha test/core/transformation.js
|
||||
```
|
||||
|
||||
Use mocha's `--grep` option to run a subset of tests by name:
|
||||
|
||||
```sh
|
||||
$ mocha test/transformation.js --grep es7
|
||||
$ mocha test/core/transformation.js --grep es7
|
||||
```
|
||||
|
||||
If you don't have `mocha` installed globally, you can still use it from Babel's
|
||||
dependencies in `node_modules`, but make sure `node_modules/.bin` is added to
|
||||
your [`$PATH`](http://unix.stackexchange.com/questions/26047/how-to-correctly-add-a-path-to-path) environment variable.
|
||||
|
||||
|
||||
#### Workflow
|
||||
|
||||
* Fork the repository
|
||||
@@ -56,24 +76,91 @@ $ mocha test/transformation.js --grep es7
|
||||
* Ensure the test are passing (`make test`)
|
||||
* Create new pull request explaining your proposed change or reference an issue in your commit message
|
||||
|
||||
|
||||
#### Dependencies
|
||||
|
||||
+ [ast-types](http://ghub.io/ast-types) This is required to monkeypatch regenerators AST definitions. Could be improved in the future.
|
||||
|
||||
+ [chalk](http://ghub.io/chalk) This is used for terminal color highlighting for syntax errors.
|
||||
|
||||
+ [convert-source-map](http://ghub.io/convert-source-map) Turns a source map object into a comment etc.
|
||||
|
||||
+ [core-js](http://ghub.io/core-js) Used for the polyfill.
|
||||
|
||||
+ [debug](http://ghub.io/debug) Used to output debugging information when NODE_DEBUG is set to babel.
|
||||
|
||||
+ [detect-indent](http://ghub.io/detect-indent) This is used in the code generator so it can infer indentation.
|
||||
|
||||
+ [estraverse](http://ghub.io/estraverse) The only method on this is attachComments. I'd like to implement our own comment attachment algorithm eventually though.
|
||||
|
||||
+ [esutils](http://ghub.io/esutils) Various ES related utilities. Check whether something is a keyword etc.
|
||||
|
||||
+ [fs-readdir-recursive](http://ghub.io/fs-readdir-recursive) Recursively search a directory for.
|
||||
|
||||
+ [globals](http://ghub.io/globals) A list of JavaScript global variables. This is used by the scope tracking to check for colliding variables.
|
||||
|
||||
+ [is-integer](http://ghub.io/is-integer) Checks if something is an integer.
|
||||
|
||||
+ [js-tokens](http://ghub.io/js-tokens) This is used to get tokens for syntax error highlighting.
|
||||
|
||||
+ [leven](http://ghub.io/leven) A levenstein algorithm to determine how close a word is to another. This is used to offer suggestions when using the utility.undeclaredVariableCheck transformer.
|
||||
|
||||
+ [line-numbers](http://ghub.io/line-numbers) Used to produce the code frames in syntax errors.
|
||||
|
||||
+ [lodash](http://ghub.io/lodash) Used for various utilities.
|
||||
|
||||
+ [minimatch](http://ghub.io/minimatch) This is used to match glob-style ignore/only filters.
|
||||
|
||||
+ [output-file-sync](http://ghub.io/output-file-sync) Synchronously writes a file and create its ancestor directories if needed.
|
||||
|
||||
+ [path-is-absolute](http://ghub.io/path-is-absolute) Checks if a path is absolute. C:\foo and \foo are considered absolute.
|
||||
|
||||
+ [regenerator](http://ghub.io/regenerator) This is used to transform generators/async functions.
|
||||
|
||||
+ [regexpu](http://ghub.io/regexpu) Used to transform unicode regex patterns.
|
||||
|
||||
+ [repeating](http://ghub.io/repeating) Repeats a string.
|
||||
|
||||
+ [shebang-regex](http://ghub.io/shebang-regex) Literally just a regex that matches shebangs.
|
||||
|
||||
+ [slash](http://ghub.io/slash) Normalises path separators.
|
||||
|
||||
+ [source-map](http://ghub.io/source-map) Generates sourcemaps.
|
||||
|
||||
+ [source-map-support](http://ghub.io/source-map-support) Adds source map support to babel-node/babel/register.
|
||||
|
||||
+ [strip-json-comments](http://ghub.io/strip-json-comments) Remove comments from a JSON string. This is used for .babelrc files.
|
||||
|
||||
+ [to-fast-properties](http://ghub.io/to-fast-properties) A V8 trick to put an object into fast properties mode.
|
||||
|
||||
+ [trim-right](http://ghub.io/trim-right) Trims the rightside whitespace.
|
||||
|
||||
+ [user-home](http://ghub.io/user-home) Gets the users home directory. This is used to resolve the babel-node/babel/register cache.
|
||||
|
||||
|
||||
#### Code Standards
|
||||
|
||||
* **General**
|
||||
* Max of five arguments for functions
|
||||
* Max depth of four nested blocks
|
||||
* 2-spaced soft tabs
|
||||
|
||||
* **Naming**
|
||||
* CamelCase all class names
|
||||
* camelBack all variable names
|
||||
|
||||
* **Spacing**
|
||||
* Spaces after all keywords
|
||||
* Spaces before all left curly braces
|
||||
|
||||
* **Comments**
|
||||
* Use JSDoc-style comments for methods
|
||||
* Single-line comments for ambiguous code
|
||||
|
||||
* **Quotes**
|
||||
* Always use double quotes
|
||||
* Only use single quotes when the string contains a double quote
|
||||
|
||||
* **Declaration**
|
||||
* No unused variables
|
||||
* No pollution of global variables and prototypes
|
||||
|
||||
29
Makefile
29
Makefile
@@ -1,3 +1,4 @@
|
||||
MAKEFLAGS = -j1
|
||||
BROWSERIFY_CMD = node_modules/browserify/bin/cmd.js
|
||||
ISTANBUL_CMD = node_modules/istanbul/lib/cli.js cover
|
||||
UGLIFY_CMD = node_modules/uglify-js/bin/uglifyjs
|
||||
@@ -31,13 +32,13 @@ build:
|
||||
node $(BROWSERIFY_CMD) lib/babel/api/browser.js -s babel >dist/babel.js
|
||||
node $(UGLIFY_CMD) dist/babel.js >dist/babel.min.js
|
||||
|
||||
node bin/babel-external-helpers >dist/external-helpers.js
|
||||
node packages/babel-cli/bin/babel-external-helpers >dist/external-helpers.js
|
||||
node $(UGLIFY_CMD) dist/external-helpers.js >dist/external-helpers.min.js
|
||||
|
||||
rm -rf templates.json
|
||||
|
||||
clean:
|
||||
rm -rf coverage templates.json test/tmp dist
|
||||
rm -rf coverage templates.json test/tmp dist lib
|
||||
|
||||
test-clean:
|
||||
rm -rf test/tmp
|
||||
@@ -86,11 +87,11 @@ publish:
|
||||
node tools/cache-templates
|
||||
test -f templates.json
|
||||
|
||||
npm publish --tag beta
|
||||
npm publish
|
||||
|
||||
git push --follow-tags
|
||||
|
||||
make publish-core
|
||||
make publish-cli
|
||||
make publish-runtime
|
||||
|
||||
rm -rf templates.json browser.js browser-polyfill.js external-helpers.js
|
||||
@@ -99,20 +100,18 @@ publish-runtime:
|
||||
cd packages; \
|
||||
node build-runtime.js; \
|
||||
cd babel-runtime; \
|
||||
npm publish --tag beta
|
||||
npm publish
|
||||
|
||||
publish-core:
|
||||
tools/generate-core-package-json >package2.json
|
||||
mv package.json .package.json
|
||||
mv package2.json package.json
|
||||
|
||||
npm publish --tag beta
|
||||
|
||||
rm -rf package.json
|
||||
mv .package.json package.json
|
||||
publish-cli:
|
||||
cd packages; \
|
||||
node build-cli.js; \
|
||||
cd babel-cli; \
|
||||
npm publish
|
||||
|
||||
bootstrap:
|
||||
npm list --global --depth 1 babel >/dev/null 2>&1 && npm uninstall -g babel || true
|
||||
npm install
|
||||
npm link
|
||||
cd packages/babel-cli && npm install && npm link && npm link babel-core
|
||||
git submodule update --init
|
||||
cd vendor/regenerator && npm install
|
||||
cd vendor/compat-table && npm install object-assign
|
||||
|
||||
@@ -5,11 +5,11 @@
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<strong>Babel</strong> is a transpiler for writing next generation JavaScript.
|
||||
<strong>Babel</strong> is a compiler for writing next generation JavaScript.
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
For questions and support please visit the <a href="https://gitter.im/babel/babel">gitter room</a> before creating an issue.
|
||||
For questions and support please visit the <a href="https://gitter.im/babel/babel">gitter room</a> or <a href="http://stackoverflow.com/questions/tagged/babeljs">StackOverflow</a>. The Babel issue tracker is <strong>exclusively</strong> for bug reports and feature requests.
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
console.error("`6to5` has been renamed to `babel`");
|
||||
require("../babel");
|
||||
@@ -1,3 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
console.error("`6to5-node` has been renamed to `babel-node`");
|
||||
require("../_babel-node");
|
||||
@@ -1,3 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
console.error("`6to5-runtime` has been renamed to `babel-external-helpers`");
|
||||
require("../babel-external-helpers");
|
||||
47
package.json
47
package.json
@@ -1,23 +1,14 @@
|
||||
{
|
||||
"name": "babel",
|
||||
"name": "babel-core",
|
||||
"description": "Turn ES6 code into readable vanilla ES5 with source maps",
|
||||
"version": "5.0.0-beta2",
|
||||
"version": "5.1.11",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>",
|
||||
"homepage": "https://babeljs.io/",
|
||||
"repository": "babel/babel",
|
||||
"preferGlobal": true,
|
||||
"main": "lib/babel/api/node.js",
|
||||
"browser": {
|
||||
"./lib/babel/api/register/node.js": "./lib/babel/api/register/browser.js"
|
||||
},
|
||||
"bin": {
|
||||
"6to5": "./bin/deprecated/6to5",
|
||||
"6to5-node": "./bin/deprecated/6to5-node",
|
||||
"6to5-runtime": "./bin/deprecated/6to5-runtime",
|
||||
"babel": "./bin/babel/index.js",
|
||||
"babel-node": "./bin/babel-node",
|
||||
"babel-external-helpers": "./bin/babel-external-helpers"
|
||||
},
|
||||
"keywords": [
|
||||
"harmony",
|
||||
"classes",
|
||||
@@ -38,46 +29,46 @@
|
||||
"dependencies": {
|
||||
"ast-types": "~0.7.0",
|
||||
"chalk": "^1.0.0",
|
||||
"chokidar": "^0.12.6",
|
||||
"commander": "^2.6.0",
|
||||
"convert-source-map": "^0.5.0",
|
||||
"core-js": "^0.6.1",
|
||||
"convert-source-map": "^1.0.0",
|
||||
"core-js": "^0.8.3",
|
||||
"debug": "^2.1.1",
|
||||
"detect-indent": "^3.0.0",
|
||||
"estraverse": "^1.9.1",
|
||||
"esutils": "^1.1.6",
|
||||
"estraverse": "^3.0.0",
|
||||
"esutils": "^2.0.0",
|
||||
"fs-readdir-recursive": "^0.1.0",
|
||||
"globals": "^6.2.0",
|
||||
"globals": "^6.4.0",
|
||||
"is-integer": "^1.0.4",
|
||||
"js-tokens": "1.0.0",
|
||||
"leven": "^1.0.1",
|
||||
"line-numbers": "0.2.0",
|
||||
"lodash": "^3.2.0",
|
||||
"lodash": "^3.6.0",
|
||||
"minimatch": "^2.0.3",
|
||||
"output-file-sync": "^1.1.0",
|
||||
"path-is-absolute": "^1.0.0",
|
||||
"private": "^0.1.6",
|
||||
"regenerator-babel": "0.8.13-2",
|
||||
"regenerator": "^0.8.20",
|
||||
"regexpu": "^1.1.2",
|
||||
"repeating": "^1.1.2",
|
||||
"shebang-regex": "^1.0.0",
|
||||
"slash": "^1.0.0",
|
||||
"source-map": "^0.4.0",
|
||||
"source-map-support": "^0.2.9",
|
||||
"source-map-support": "^0.2.10",
|
||||
"strip-json-comments": "^1.0.2",
|
||||
"to-fast-properties": "^1.0.0",
|
||||
"trim-right": "^1.0.0"
|
||||
"trim-right": "^1.0.0",
|
||||
"user-home": "^1.1.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel": "4.7.13",
|
||||
"browserify": "^9.0.3",
|
||||
"chai": "^2.0.0",
|
||||
"eslint": "^0.15.1",
|
||||
"babel-eslint": "^1.0.1",
|
||||
"browserify": "^9.0.8",
|
||||
"chai": "^2.2.0",
|
||||
"eslint": "^0.18.0",
|
||||
"babel-eslint": "^2.0.0",
|
||||
"esvalid": "^1.1.0",
|
||||
"istanbul": "^0.3.5",
|
||||
"matcha": "^0.6.0",
|
||||
"mocha": "^2.1.0",
|
||||
"rimraf": "^2.2.8",
|
||||
"mocha": "2.2.0",
|
||||
"rimraf": "^2.3.2",
|
||||
"uglify-js": "^2.4.16"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
# What is this?
|
||||
|
||||
This is a folder containing additional packages that are built whenever
|
||||
a new version of babel is released.
|
||||
|
||||
**TODO:** Move `babel-core` into here.
|
||||
5
packages/babel-cli/README.md
Normal file
5
packages/babel-cli/README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# babel-cli
|
||||
|
||||
Babel CLI
|
||||
|
||||
For more information please look at [babel](https://github.com/babel/babel).
|
||||
@@ -3,10 +3,11 @@
|
||||
var pathIsAbsolute = require("path-is-absolute");
|
||||
var commander = require("commander");
|
||||
var Module = require("module");
|
||||
var babel = require("../lib/babel/api/node");
|
||||
var babel = require("babel-core");
|
||||
var inspect = require("util").inspect;
|
||||
var path = require("path");
|
||||
var repl = require("repl");
|
||||
var util = require("../lib/babel/util");
|
||||
var util = require("babel-core").util;
|
||||
var vm = require("vm");
|
||||
var _ = require("lodash");
|
||||
|
||||
@@ -16,7 +17,7 @@ program.option("-e, --eval [script]", "Evaluate script");
|
||||
program.option("-p, --print [code]", "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,.es,.jsx]");
|
||||
program.option("-r, --experimental", "Enable experimental support for proposed ES7 features");
|
||||
program.option("-r, --stage [stage]", "Enable support for specific ECMAScript stages");
|
||||
program.option("-w, --whitelist [whitelist]", "Whitelist of transformers to ONLY use", util.list);
|
||||
program.option("-b, --blacklist [blacklist]", "Blacklist of transformers to NOT use", util.list);
|
||||
program.option("-o, --optional [optional]", "List of optional transformers to enable", util.list);
|
||||
@@ -29,12 +30,12 @@ program.parse(process.argv);
|
||||
//
|
||||
|
||||
babel.register({
|
||||
experimental: program.experimental,
|
||||
extensions: program.extensions,
|
||||
blacklist: program.blacklist,
|
||||
whitelist: program.whitelist,
|
||||
optional: program.optional,
|
||||
ignore: program.ignore
|
||||
ignore: program.ignore,
|
||||
stage: program.stage,
|
||||
});
|
||||
|
||||
//
|
||||
@@ -47,7 +48,7 @@ var _eval = function (code, filename) {
|
||||
blacklist: program.blacklist,
|
||||
whitelist: program.whitelist,
|
||||
optional: program.optional,
|
||||
experimental: program.experimental
|
||||
stage: program.stage
|
||||
}).code;
|
||||
|
||||
return vm.runInThisContext(code, {
|
||||
@@ -71,7 +72,10 @@ if (program.eval || program.print) {
|
||||
global.require = module.require.bind(module);
|
||||
|
||||
var result = _eval(code, global.__filename);
|
||||
if (program.print) console.log(result);
|
||||
if (program.print) {
|
||||
var output = _.isString(result) ? result : inspect(result);
|
||||
process.stdout.write(output + "\n");
|
||||
}
|
||||
} else {
|
||||
if (program.args.length) {
|
||||
// slice all arguments up to the first filename since they're babel args that we handle
|
||||
@@ -1,8 +1,8 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
var commander = require("commander");
|
||||
var util = require("../lib/babel/util");
|
||||
var runtime = require("../lib/babel/tools/build-external-helpers");
|
||||
var util = require("babel-core").util;
|
||||
var runtime = require("babel-core").buildExternalHelpers;
|
||||
|
||||
commander.option("-l, --whitelist [whitelist]", "Whitelist of helpers to ONLY include", util.list);
|
||||
commander.option("-t, --output-type [type]", "Type of output (global|umd|var)", "global");
|
||||
@@ -28,6 +28,8 @@ module.exports = function (commander, filenames, opts) {
|
||||
};
|
||||
|
||||
var handleFile = function (src, filename) {
|
||||
if (util.shouldIgnore(src)) return;
|
||||
|
||||
if (util.canCompile(filename)) {
|
||||
write(src, filename);
|
||||
} else if (commander.copyFiles) {
|
||||
@@ -107,6 +107,8 @@ module.exports = function (commander, filenames, opts) {
|
||||
});
|
||||
|
||||
_.each(_filenames, function (filename) {
|
||||
if (util.shouldIgnore(filename)) return;
|
||||
|
||||
results.push(util.compile(filename));
|
||||
});
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
var commander = require("commander");
|
||||
var transform = require("../../lib/babel/transformation");
|
||||
var transform = require("babel-core").transform;
|
||||
var kebabCase = require("lodash/string/kebabCase");
|
||||
var File = require("../../lib/babel/transformation/file");
|
||||
var util = require("../../lib/babel/util");
|
||||
var options = require("babel-core").options;
|
||||
var util = require("babel-core").util;
|
||||
var each = require("lodash/collection/each");
|
||||
var keys = require("lodash/object/keys");
|
||||
var fs = require("fs");
|
||||
|
||||
each(File.options, function (option, key) {
|
||||
each(options, function (option, key) {
|
||||
if (option.hidden) return;
|
||||
|
||||
var arg = kebabCase(key);
|
||||
@@ -19,7 +19,7 @@ each(File.options, function (option, key) {
|
||||
}
|
||||
|
||||
if (option.type === "boolean" && option.default === true) {
|
||||
arg = "no-" + key;
|
||||
arg = "no-" + arg;
|
||||
}
|
||||
|
||||
arg = "--" + arg;
|
||||
@@ -28,7 +28,11 @@ each(File.options, function (option, key) {
|
||||
arg = "-" + option.shorthand + ", " + arg;
|
||||
}
|
||||
|
||||
commander.option(arg, option.description);
|
||||
var desc = [];
|
||||
if (option.deprecated) desc.push("[DEPRECATED] " + option.deprecated);
|
||||
if (option.description) desc.push(option.description);
|
||||
|
||||
commander.option(arg, desc.join(" "));
|
||||
})
|
||||
|
||||
commander.option("-w, --watch", "Recompile files on changes");
|
||||
@@ -98,12 +102,15 @@ if (errors.length) {
|
||||
|
||||
//
|
||||
|
||||
exports.opts = {};
|
||||
var opts = exports.opts = {};
|
||||
|
||||
each(File.options, function (opt, key) {
|
||||
exports.opts[key] = commander[key];
|
||||
each(options, function (opt, key) {
|
||||
opts[key] = commander[key];
|
||||
});
|
||||
|
||||
opts.ignore = util.arrayify(opts.ignore, util.regexify);
|
||||
opts.only = util.arrayify(opts.only, util.regexify);
|
||||
|
||||
var fn;
|
||||
|
||||
if (commander.outDir) {
|
||||
@@ -1,7 +1,7 @@
|
||||
var readdir = require("fs-readdir-recursive");
|
||||
var index = require("./index");
|
||||
var babel = require("../../lib/babel/api/node");
|
||||
var util = require("../../lib/babel/util");
|
||||
var babel = require("babel-core");
|
||||
var util = require("babel-core").util;
|
||||
var path = require("path");
|
||||
var fs = require("fs");
|
||||
var _ = require("lodash");
|
||||
@@ -16,6 +16,10 @@ exports.readdir = readdir;
|
||||
|
||||
exports.canCompile = util.canCompile;
|
||||
|
||||
exports.shouldIgnore = function (loc) {
|
||||
return util.shouldIgnore(loc, index.opts.ignore, index.opts.only);
|
||||
};
|
||||
|
||||
exports.addSourceMappingUrl = function (code, loc) {
|
||||
return code + "\n//# sourceMappingURL=" + path.basename(loc);
|
||||
};
|
||||
@@ -23,6 +27,8 @@ exports.addSourceMappingUrl = function (code, loc) {
|
||||
exports.transform = function (filename, code, opts) {
|
||||
opts = _.defaults(opts || {}, index.opts);
|
||||
opts.filename = filename;
|
||||
opts.ignore = null;
|
||||
opts.only = null;
|
||||
|
||||
var result = babel.transform(code, opts);
|
||||
result.filename = filename;
|
||||
1
packages/babel-cli/index.js
Normal file
1
packages/babel-cli/index.js
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = require("babel-core");
|
||||
25
packages/babel-cli/package.json
Normal file
25
packages/babel-cli/package.json
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "babel",
|
||||
"description": "Turn ES6 code into readable vanilla ES5 with source maps",
|
||||
"version": "5.1.10",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>",
|
||||
"homepage": "https://babeljs.io/",
|
||||
"repository": "babel/babel",
|
||||
"preferGlobal": true,
|
||||
"dependencies": {
|
||||
"babel-core": "^5.1.10",
|
||||
"chokidar": "^1.0.0",
|
||||
"commander": "^2.6.0",
|
||||
"fs-readdir-recursive": "^0.1.0",
|
||||
"output-file-sync": "^1.1.0",
|
||||
"lodash": "^3.2.0",
|
||||
"convert-source-map": "^0.5.0",
|
||||
"source-map": "^0.4.0",
|
||||
"path-is-absolute": "^1.0.0"
|
||||
},
|
||||
"bin": {
|
||||
"babel": "./bin/babel/index.js",
|
||||
"babel-node": "./bin/babel-node",
|
||||
"babel-external-helpers": "./bin/babel-external-helpers"
|
||||
}
|
||||
}
|
||||
1
packages/babel-cli/polyfill.js
Normal file
1
packages/babel-cli/polyfill.js
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = require("babel-core/polyfill");
|
||||
1
packages/babel-cli/register.js
Normal file
1
packages/babel-cli/register.js
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = require("babel-core/register");
|
||||
4
packages/babel-runtime/core-js.js
Normal file
4
packages/babel-runtime/core-js.js
Normal file
@@ -0,0 +1,4 @@
|
||||
module.exports = {
|
||||
"default": require("core-js/library"),
|
||||
__esModule: true
|
||||
};
|
||||
@@ -1,10 +1,10 @@
|
||||
{
|
||||
"name": "babel-runtime",
|
||||
"description": "babel selfContained runtime",
|
||||
"version": "5.0.0-beta1",
|
||||
"version": "5.1.10",
|
||||
"repository": "babel/babel",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>",
|
||||
"dependencies": {
|
||||
"core-js": "^0.6.1"
|
||||
"core-js": "^0.8.0"
|
||||
}
|
||||
}
|
||||
11
packages/build-cli.js
Normal file
11
packages/build-cli.js
Normal file
@@ -0,0 +1,11 @@
|
||||
var outputFile = require("output-file-sync");
|
||||
|
||||
var pkgLoc = __dirname + "/babel-cli/package.json";
|
||||
var pkg = require(pkgLoc);
|
||||
|
||||
var mainPkg = require("../package.json");
|
||||
|
||||
pkg.version = mainPkg.version;
|
||||
pkg.dependencies["babel-core"] = "^" + mainPkg.version;
|
||||
|
||||
outputFile(pkgLoc, JSON.stringify(pkg, null, 2));
|
||||
@@ -1,50 +1,60 @@
|
||||
"use strict";
|
||||
|
||||
var transform = require("../lib/babel/transformation");
|
||||
var each = require("lodash/collection/each");
|
||||
var File = require("../lib/babel/transformation/file");
|
||||
var util = require("../lib/babel/util");
|
||||
var fs = require("fs");
|
||||
var t = require("../lib/babel/types");
|
||||
var _ = require("lodash");
|
||||
var outputFile = require("output-file-sync");
|
||||
var transform = require("../lib/babel/transformation");
|
||||
var each = require("lodash/collection/each");
|
||||
var File = require("../lib/babel/transformation/file");
|
||||
var util = require("../lib/babel/util");
|
||||
var fs = require("fs");
|
||||
var t = require("../lib/babel/types");
|
||||
var _ = require("lodash");
|
||||
|
||||
var relative = function (filename) {
|
||||
function relative(filename) {
|
||||
return __dirname + "/babel-runtime/" + filename;
|
||||
};
|
||||
}
|
||||
|
||||
var writeFile = function (filename, content) {
|
||||
filename = relative(filename);
|
||||
console.log(filename);
|
||||
fs.writeFileSync(filename, content);
|
||||
};
|
||||
|
||||
var readFile = function (filename, defaultify) {
|
||||
function readFile(filename, shouldDefaultify) {
|
||||
var file = fs.readFileSync(require.resolve(filename), "utf8");
|
||||
|
||||
if (defaultify) {
|
||||
file += '\nmodule.exports = { "default": module.exports, __esModule: true };\n';
|
||||
if (shouldDefaultify) {
|
||||
file += "\n" + defaultify("module.exports") + "\n";
|
||||
}
|
||||
|
||||
return file;
|
||||
};
|
||||
}
|
||||
|
||||
var updatePackage = function () {
|
||||
function defaultify(name) {
|
||||
return 'module.exports = { "default": ' + name + ', __esModule: true };';
|
||||
}
|
||||
|
||||
function updatePackage() {
|
||||
var pkgLoc = relative("package.json");
|
||||
var pkg = require(pkgLoc);
|
||||
|
||||
var mainPkg = require("../package.json");
|
||||
pkg.version = mainPkg.version;
|
||||
|
||||
writeFile("package.json", JSON.stringify(pkg, null, 2));
|
||||
};
|
||||
writeRootFile("package.json", JSON.stringify(pkg, null, 2));
|
||||
}
|
||||
|
||||
var selfContainify = function (code) {
|
||||
function writeRootFile(filename, content) {
|
||||
filename = relative(filename);
|
||||
console.log(filename);
|
||||
outputFile(filename, content);
|
||||
}
|
||||
|
||||
|
||||
function writeFile(filename, content) {
|
||||
return writeRootFile(filename, content);
|
||||
}
|
||||
|
||||
function selfContainify(code) {
|
||||
return transform(code, {
|
||||
optional: ["runtime"]
|
||||
}).code;
|
||||
};
|
||||
}
|
||||
|
||||
var buildHelper = function (helperName) {
|
||||
function buildHelper(helperName) {
|
||||
var tree = t.program(
|
||||
util.template("self-contained-helpers-head", {
|
||||
HELPER: util.template("helper-" + helperName)
|
||||
@@ -54,13 +64,35 @@ var buildHelper = function (helperName) {
|
||||
return transform.fromAst(tree, null, {
|
||||
optional: ["runtime"]
|
||||
}).code;
|
||||
};
|
||||
}
|
||||
|
||||
each(File.helpers, function (helperName) {
|
||||
writeFile("helpers/" + helperName + ".js", buildHelper(helperName));
|
||||
});
|
||||
|
||||
writeFile("core-js.js", readFile("core-js/library", true));
|
||||
writeFile("regenerator/index.js", readFile("regenerator-babel/runtime-module", true));
|
||||
writeFile("regenerator/runtime.js", selfContainify(readFile("regenerator-babel/runtime")));
|
||||
writeFile("regenerator/index.js", readFile("regenerator/runtime-module", true));
|
||||
writeFile("regenerator/runtime.js", selfContainify(readFile("regenerator/runtime")));
|
||||
|
||||
//
|
||||
|
||||
var coreDefinitions = require("../lib/babel/transformation/transformers/other/runtime/definitions");
|
||||
|
||||
var paths = ["is-iterable", "get-iterator"];
|
||||
|
||||
each(coreDefinitions.builtins, function (path) {
|
||||
paths.push(path);
|
||||
});
|
||||
|
||||
each(coreDefinitions.methods, function (props) {
|
||||
each(props, function (path) {
|
||||
paths.push(path);
|
||||
});
|
||||
});
|
||||
|
||||
each(paths, function (path) {
|
||||
writeFile("core-js/" + path + ".js", defaultify('require("core-js/library/fn/' + path + '")'));
|
||||
});
|
||||
|
||||
//
|
||||
|
||||
updatePackage();
|
||||
|
||||
@@ -215,6 +215,7 @@ pp.flow_parseObjectTypeIndexer = function (node, isStatic) {
|
||||
this.expect(tt.colon)
|
||||
node.value = this.flow_parseType()
|
||||
|
||||
this.flow_objectTypeSemicolon()
|
||||
return this.finishNode(node, "ObjectTypeIndexer")
|
||||
}
|
||||
|
||||
@@ -251,6 +252,7 @@ pp.flow_parseObjectTypeMethod = function (start, isStatic, key) {
|
||||
node.static = isStatic
|
||||
node.key = key
|
||||
node.optional = false
|
||||
this.flow_objectTypeSemicolon()
|
||||
return this.finishNode(node, "ObjectTypeProperty")
|
||||
}
|
||||
|
||||
@@ -258,6 +260,7 @@ pp.flow_parseObjectTypeCallProperty = function (node, isStatic) {
|
||||
var valueNode = this.startNode()
|
||||
node.static = isStatic
|
||||
node.value = this.flow_parseObjectTypeMethodish(valueNode)
|
||||
this.flow_objectTypeSemicolon()
|
||||
return this.finishNode(node, "ObjectTypeCallProperty")
|
||||
}
|
||||
|
||||
@@ -307,13 +310,10 @@ pp.flow_parseObjectType = function (allowStatic) {
|
||||
node.value = this.flow_parseType()
|
||||
node.optional = optional
|
||||
node.static = isStatic
|
||||
this.flow_objectTypeSemicolon()
|
||||
nodeStart.properties.push(this.finishNode(node, "ObjectTypeProperty"))
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.eat(tt.semi) && this.type !== tt.braceR) {
|
||||
this.unexpected()
|
||||
}
|
||||
}
|
||||
|
||||
this.expect(tt.braceR)
|
||||
@@ -321,6 +321,12 @@ pp.flow_parseObjectType = function (allowStatic) {
|
||||
return this.finishNode(nodeStart, "ObjectTypeAnnotation")
|
||||
}
|
||||
|
||||
pp.flow_objectTypeSemicolon = function () {
|
||||
if (!this.eat(tt.semi) && this.type !== tt.braceR) {
|
||||
this.unexpected()
|
||||
}
|
||||
}
|
||||
|
||||
pp.flow_parseGenericType = function (start, id) {
|
||||
var node = this.startNodeAt(start)
|
||||
|
||||
|
||||
@@ -93,19 +93,25 @@ pp.parseMaybeAssign = function(noIn, refShorthandDefaultPos, afterLeftParse) {
|
||||
failOnShorthandAssign = false
|
||||
}
|
||||
let start = this.markPosition()
|
||||
if (this.type == tt.parenL || this.type == tt.name)
|
||||
this.potentialArrowAt = this.start
|
||||
let left = this.parseMaybeConditional(noIn, refShorthandDefaultPos)
|
||||
if (afterLeftParse) left = afterLeftParse.call(this, left, start)
|
||||
if (this.type.isAssign) {
|
||||
let node = this.startNodeAt(start)
|
||||
node.operator = this.value
|
||||
node.left = this.type === tt.eq ? this.toAssignable(left) : left
|
||||
refShorthandDefaultPos.start = 0; // reset because shorthand default was used correctly
|
||||
refShorthandDefaultPos.start = 0 // reset because shorthand default was used correctly
|
||||
this.checkLVal(left)
|
||||
if (left.parenthesizedExpression) {
|
||||
let errorMsg
|
||||
if (left.type === "ObjectPattern") {
|
||||
this.raise(left.start, "You're trying to assign to a parenthesized expression, instead of `({ foo }) = {}` use `({ foo } = {})`");
|
||||
} else {
|
||||
this.raise(left.start, "Parenthesized left hand expressions are illegal");
|
||||
errorMsg = "`({a}) = 0` use `({a} = 0)`"
|
||||
} else if (left.type === "ArrayPattern") {
|
||||
errorMsg = "`([a]) = 0` use `([a] = 0)`"
|
||||
}
|
||||
if (errorMsg) {
|
||||
this.raise(left.start, `You're trying to assign to a parenthesized expression, eg. instead of ${errorMsg}`)
|
||||
}
|
||||
}
|
||||
this.next()
|
||||
@@ -224,7 +230,7 @@ pp.parseSubscripts = function(base, start, noCalls) {
|
||||
} else if (!noCalls && this.eat(tt.parenL)) {
|
||||
let node = this.startNodeAt(start)
|
||||
node.callee = base
|
||||
node.arguments = this.parseExprList(tt.parenR, false)
|
||||
node.arguments = this.parseExprList(tt.parenR, this.options.features["es7.trailingFunctionCommas"])
|
||||
return this.parseSubscripts(this.finishNode(node, "CallExpression"), start, noCalls)
|
||||
} else if (this.type === tt.backQuote) {
|
||||
let node = this.startNodeAt(start)
|
||||
@@ -240,7 +246,7 @@ pp.parseSubscripts = function(base, start, noCalls) {
|
||||
// or `{}`.
|
||||
|
||||
pp.parseExprAtom = function(refShorthandDefaultPos) {
|
||||
let node
|
||||
let node, canBeArrow = this.potentialArrowAt == this.start
|
||||
switch (this.type) {
|
||||
case tt._this:
|
||||
case tt._super:
|
||||
@@ -250,7 +256,7 @@ pp.parseExprAtom = function(refShorthandDefaultPos) {
|
||||
return this.finishNode(node, type)
|
||||
|
||||
case tt._yield:
|
||||
if (this.inGenerator) unexpected()
|
||||
if (this.inGenerator) this.unexpected()
|
||||
|
||||
case tt._do:
|
||||
if (this.options.features["es7.doExpressions"]) {
|
||||
@@ -271,12 +277,14 @@ pp.parseExprAtom = function(refShorthandDefaultPos) {
|
||||
if (id.name === "async") {
|
||||
// arrow functions
|
||||
if (this.type === tt.parenL) {
|
||||
let expr = this.parseParenAndDistinguishExpression(start, true)
|
||||
if (expr.type === "ArrowFunctionExpression") {
|
||||
let expr = this.parseParenAndDistinguishExpression(start, true, true)
|
||||
if (expr && expr.type === "ArrowFunctionExpression") {
|
||||
return expr
|
||||
} else {
|
||||
node.callee = id
|
||||
if (expr.type === "SequenceExpression") {
|
||||
if (!expr) {
|
||||
node.arguments = []
|
||||
} else if (expr.type === "SequenceExpression") {
|
||||
node.arguments = expr.expressions
|
||||
} else {
|
||||
node.arguments = [expr]
|
||||
@@ -300,9 +308,8 @@ pp.parseExprAtom = function(refShorthandDefaultPos) {
|
||||
}
|
||||
//
|
||||
|
||||
if (!this.canInsertSemicolon() && this.eat(tt.arrow)) {
|
||||
if (canBeArrow && !this.canInsertSemicolon() && this.eat(tt.arrow))
|
||||
return this.parseArrowExpression(this.startNodeAt(start), [id])
|
||||
}
|
||||
return id
|
||||
|
||||
case tt.regexp:
|
||||
@@ -322,7 +329,7 @@ pp.parseExprAtom = function(refShorthandDefaultPos) {
|
||||
return this.finishNode(node, "Literal")
|
||||
|
||||
case tt.parenL:
|
||||
return this.parseParenAndDistinguishExpression()
|
||||
return this.parseParenAndDistinguishExpression(null, null, canBeArrow)
|
||||
|
||||
case tt.bracketL:
|
||||
node = this.startNode()
|
||||
@@ -376,7 +383,7 @@ pp.parseParenExpression = function() {
|
||||
return val
|
||||
}
|
||||
|
||||
pp.parseParenAndDistinguishExpression = function(start, isAsync) {
|
||||
pp.parseParenAndDistinguishExpression = function(start, isAsync, canBeArrow) {
|
||||
start = start || this.markPosition()
|
||||
let val
|
||||
if (this.options.ecmaVersion >= 6) {
|
||||
@@ -405,12 +412,18 @@ pp.parseParenAndDistinguishExpression = function(start, isAsync) {
|
||||
let innerEnd = this.markPosition()
|
||||
this.expect(tt.parenR)
|
||||
|
||||
if (!this.canInsertSemicolon() && this.eat(tt.arrow)) {
|
||||
if (canBeArrow && !this.canInsertSemicolon() && this.eat(tt.arrow)) {
|
||||
if (innerParenStart) this.unexpected(innerParenStart)
|
||||
return this.parseParenArrowList(start, exprList, isAsync)
|
||||
}
|
||||
|
||||
if (!exprList.length) this.unexpected(this.lastTokStart)
|
||||
if (!exprList.length) {
|
||||
if (isAsync) {
|
||||
return
|
||||
} else {
|
||||
this.unexpected(this.lastTokStart)
|
||||
}
|
||||
}
|
||||
if (spreadStart) this.unexpected(spreadStart)
|
||||
if (refShorthandDefaultPos.start) this.unexpected(refShorthandDefaultPos.start)
|
||||
|
||||
@@ -500,14 +513,21 @@ pp.parseTemplate = function() {
|
||||
pp.parseObj = function(isPattern, refShorthandDefaultPos) {
|
||||
let node = this.startNode(), first = true, propHash = {}
|
||||
node.properties = []
|
||||
let decorators = []
|
||||
this.next()
|
||||
while (!this.eat(tt.braceR)) {
|
||||
if (!first) {
|
||||
this.expect(tt.comma)
|
||||
if (this.afterTrailingComma(tt.braceR)) break
|
||||
} else first = false
|
||||
|
||||
while (this.type === tt.at) {
|
||||
decorators.push(this.parseDecorator())
|
||||
}
|
||||
let prop = this.startNode(), isGenerator = false, isAsync = false, start
|
||||
if (decorators.length) {
|
||||
prop.decorators = decorators
|
||||
decorators = []
|
||||
}
|
||||
if (this.options.features["es7.objectRestSpread"] && this.type === tt.ellipsis) {
|
||||
prop = this.parseSpread()
|
||||
prop.type = "SpreadProperty"
|
||||
@@ -538,6 +558,9 @@ pp.parseObj = function(isPattern, refShorthandDefaultPos) {
|
||||
this.checkPropClash(prop, propHash)
|
||||
node.properties.push(this.finishNode(prop, "Property"))
|
||||
}
|
||||
if (decorators.length) {
|
||||
this.raise(this.start, "You have trailing decorators with no property");
|
||||
}
|
||||
return this.finishNode(node, isPattern ? "ObjectPattern" : "ObjectExpression")
|
||||
}
|
||||
|
||||
@@ -718,7 +741,7 @@ pp.parseAwait = function (node) {
|
||||
this.unexpected()
|
||||
}
|
||||
node.all = this.eat(tt.star)
|
||||
node.argument = this.parseMaybeAssign(true)
|
||||
node.argument = this.parseMaybeUnary()
|
||||
return this.finishNode(node, "AwaitExpression")
|
||||
};
|
||||
|
||||
|
||||
@@ -9,41 +9,9 @@
|
||||
|
||||
function makePredicate(words) {
|
||||
words = words.split(" ")
|
||||
let f = "", cats = []
|
||||
out: for (let i = 0; i < words.length; ++i) {
|
||||
for (let j = 0; j < cats.length; ++j)
|
||||
if (cats[j][0].length == words[i].length) {
|
||||
cats[j].push(words[i])
|
||||
continue out
|
||||
}
|
||||
cats.push([words[i]])
|
||||
return function(str) {
|
||||
return words.indexOf(str) >= 0
|
||||
}
|
||||
function compareTo(arr) {
|
||||
if (arr.length == 1) return f += "return str === " + JSON.stringify(arr[0]) + ";"
|
||||
f += "switch(str){"
|
||||
for (let i = 0; i < arr.length; ++i) f += "case " + JSON.stringify(arr[i]) + ":"
|
||||
f += "return true}return false;"
|
||||
}
|
||||
|
||||
// When there are more than three length categories, an outer
|
||||
// switch first dispatches on the lengths, to save on comparisons.
|
||||
|
||||
if (cats.length > 3) {
|
||||
cats.sort((a, b) => b.length - a.length)
|
||||
f += "switch(str.length){"
|
||||
for (let i = 0; i < cats.length; ++i) {
|
||||
let cat = cats[i]
|
||||
f += "case " + cat[0].length + ":"
|
||||
compareTo(cat)
|
||||
}
|
||||
f += "}"
|
||||
|
||||
// Otherwise, simply generate a flat `switch` statement.
|
||||
|
||||
} else {
|
||||
compareTo(words)
|
||||
}
|
||||
return new Function("str", f)
|
||||
}
|
||||
|
||||
// Reserved word lists for various dialects of the language
|
||||
|
||||
@@ -18,17 +18,19 @@ var STATE_KEYS = [
|
||||
];
|
||||
|
||||
pp.getState = function () {
|
||||
var state = {};
|
||||
var state = {}
|
||||
for (var i = 0; i < STATE_KEYS.length; i++) {
|
||||
var key = STATE_KEYS[i];
|
||||
state[key] = this[key];
|
||||
var key = STATE_KEYS[i]
|
||||
state[key] = this[key]
|
||||
}
|
||||
return state;
|
||||
return state
|
||||
};
|
||||
|
||||
pp.lookahead = function() {
|
||||
var old = this.getState();
|
||||
this.next();
|
||||
this.isLookahead = true
|
||||
this.next()
|
||||
this.isLookahead = false
|
||||
var curr = this.getState();
|
||||
for (var key in old) this[key] = old[key];
|
||||
return curr;
|
||||
|
||||
@@ -125,7 +125,9 @@ pp.parseBindingList = function(close, allowEmpty, allowTrailingComma) {
|
||||
this.expect(close)
|
||||
break
|
||||
} else {
|
||||
elts.push(this.parseAssignableListItemTypes(this.parseMaybeDefault()))
|
||||
var left = this.parseMaybeDefault()
|
||||
this.parseAssignableListItemTypes(left)
|
||||
elts.push(this.parseMaybeDefault(null, left))
|
||||
}
|
||||
}
|
||||
return elts
|
||||
|
||||
@@ -46,6 +46,9 @@ export function Parser(options, input, startPos) {
|
||||
this.inModule = this.options.sourceType === "module"
|
||||
this.strict = this.options.strictMode === false ? false : this.inModule
|
||||
|
||||
// Used to signify the start of a potential arrow function
|
||||
this.potentialArrowAt = -1
|
||||
|
||||
// Flags to track whether we are in a function, a generator.
|
||||
this.inFunction = this.inGenerator = false
|
||||
// Labels in scope.
|
||||
|
||||
@@ -458,7 +458,7 @@ pp.parseFunction = function(node, isStatement, allowExpressionBody, isAsync) {
|
||||
|
||||
pp.parseFunctionParams = function(node) {
|
||||
this.expect(tt.parenL)
|
||||
node.params = this.parseBindingList(tt.parenR, false, false)
|
||||
node.params = this.parseBindingList(tt.parenR, false, this.options.features["es7.trailingFunctionCommas"])
|
||||
}
|
||||
|
||||
// Parse a class declaration or literal (depending on the
|
||||
@@ -471,14 +471,18 @@ pp.parseClass = function(node, isStatement) {
|
||||
var classBody = this.startNode()
|
||||
classBody.body = []
|
||||
this.expect(tt.braceL)
|
||||
let decorators = []
|
||||
while (!this.eat(tt.braceR)) {
|
||||
if (this.eat(tt.semi)) continue
|
||||
if (this.type === tt.at) {
|
||||
this.parseDecorator()
|
||||
continue;
|
||||
decorators.push(this.parseDecorator())
|
||||
continue
|
||||
}
|
||||
var method = this.startNode()
|
||||
this.takeDecorators(method)
|
||||
if (decorators.length) {
|
||||
method.decorators = decorators
|
||||
decorators = []
|
||||
}
|
||||
var isGenerator = this.eat(tt.star), isAsync = false
|
||||
this.parsePropertyName(method)
|
||||
if (this.type !== tt.parenL && !method.computed && method.key.type === "Identifier" &&
|
||||
@@ -517,7 +521,7 @@ pp.parseClass = function(node, isStatement) {
|
||||
}
|
||||
this.parseClassMethod(classBody, method, isGenerator, isAsync)
|
||||
}
|
||||
if (this.decorators.length) {
|
||||
if (decorators.length) {
|
||||
this.raise(this.start, "You have trailing decorators with no method");
|
||||
}
|
||||
node.body = this.finishNode(classBody, "ClassBody")
|
||||
@@ -558,14 +562,34 @@ pp.parseClassSuper = function(node) {
|
||||
pp.parseExport = function(node) {
|
||||
this.next()
|
||||
// export * from '...'
|
||||
if (this.eat(tt.star)) {
|
||||
this.expectContextual("from")
|
||||
node.source = this.type === tt.string ? this.parseExprAtom() : this.unexpected()
|
||||
this.semicolon()
|
||||
this.checkExport(node)
|
||||
return this.finishNode(node, "ExportAllDeclaration")
|
||||
}
|
||||
if (this.eat(tt._default)) { // export default ...
|
||||
if (this.type === tt.star) {
|
||||
let specifier = this.startNode()
|
||||
this.next()
|
||||
if (this.options.features["es7.exportExtensions"] && this.eatContextual("as")) {
|
||||
specifier.exported = this.parseIdent()
|
||||
node.specifiers = [this.finishNode(specifier, "ExportNamespaceSpecifier")]
|
||||
this.parseExportSpecifiersMaybe(node)
|
||||
this.parseExportFrom(node)
|
||||
} else {
|
||||
this.parseExportFrom(node)
|
||||
return this.finishNode(node, "ExportAllDeclaration")
|
||||
}
|
||||
} else if (this.isExportDefaultSpecifier()) {
|
||||
let specifier = this.startNode()
|
||||
specifier.exported = this.parseIdent(true)
|
||||
node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")]
|
||||
if (this.type === tt.comma && this.lookahead().type === tt.star) {
|
||||
this.expect(tt.comma)
|
||||
let specifier = this.startNode()
|
||||
this.expect(tt.star)
|
||||
this.expectContextual("as")
|
||||
specifier.exported = this.parseIdent()
|
||||
node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"))
|
||||
} else {
|
||||
this.parseExportSpecifiersMaybe(node)
|
||||
}
|
||||
this.parseExportFrom(node)
|
||||
} else if (this.eat(tt._default)) { // export default ...
|
||||
let expr = this.parseMaybeAssign()
|
||||
let needsSemi = true
|
||||
if (expr.type == "FunctionExpression" ||
|
||||
@@ -581,9 +605,7 @@ pp.parseExport = function(node) {
|
||||
if (needsSemi) this.semicolon()
|
||||
this.checkExport(node)
|
||||
return this.finishNode(node, "ExportDefaultDeclaration")
|
||||
}
|
||||
// export var|const|let|function|class ...
|
||||
if (this.type.keyword || this.shouldParseExportDeclaration()) {
|
||||
} else if (this.type.keyword || this.shouldParseExportDeclaration()) {
|
||||
node.declaration = this.parseStatement(true)
|
||||
node.specifiers = []
|
||||
node.source = null
|
||||
@@ -601,6 +623,32 @@ pp.parseExport = function(node) {
|
||||
return this.finishNode(node, "ExportNamedDeclaration")
|
||||
}
|
||||
|
||||
pp.isExportDefaultSpecifier = function () {
|
||||
if (this.type === tt.name) {
|
||||
return this.value !== "type" && this.value !== "async"
|
||||
}
|
||||
|
||||
if (this.type !== tt._default) {
|
||||
return false
|
||||
}
|
||||
|
||||
var lookahead = this.lookahead()
|
||||
return lookahead.type === tt.comma || (lookahead.type === tt.name && lookahead.value === "from")
|
||||
}
|
||||
|
||||
pp.parseExportSpecifiersMaybe = function (node) {
|
||||
if (this.eat(tt.comma)) {
|
||||
node.specifiers = node.specifiers.concat(this.parseExportSpecifiers())
|
||||
}
|
||||
}
|
||||
|
||||
pp.parseExportFrom = function(node) {
|
||||
this.expectContextual("from")
|
||||
node.source = this.type === tt.string ? this.parseExprAtom() : this.unexpected()
|
||||
this.semicolon()
|
||||
this.checkExport(node)
|
||||
}
|
||||
|
||||
pp.shouldParseExportDeclaration = function() {
|
||||
return this.options.features["es7.asyncFunctions"] && this.isContextual("async")
|
||||
}
|
||||
@@ -643,7 +691,6 @@ pp.parseImport = function(node) {
|
||||
if (this.type === tt.string) {
|
||||
node.specifiers = empty
|
||||
node.source = this.parseExprAtom()
|
||||
node.kind = ""
|
||||
} else {
|
||||
node.specifiers = []
|
||||
this.parseImportSpecifiers(node)
|
||||
|
||||
@@ -28,7 +28,7 @@ const pp = Parser.prototype
|
||||
// Move to the next token
|
||||
|
||||
pp.next = function() {
|
||||
if (this.options.onToken)
|
||||
if (this.options.onToken && !this.isLookahead)
|
||||
this.options.onToken(new Token(this))
|
||||
|
||||
this.lastTokEnd = this.end
|
||||
|
||||
@@ -116,7 +116,7 @@ kw("do", {isLoop: true})
|
||||
kw("else", beforeExpr)
|
||||
kw("finally")
|
||||
kw("for", {isLoop: true})
|
||||
kw("function")
|
||||
kw("function", startsExpr)
|
||||
kw("if")
|
||||
kw("return", beforeExpr)
|
||||
kw("switch")
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
var transform = module.exports = require("../transformation");
|
||||
|
||||
transform.options = require("../transformation/file/options");
|
||||
transform.version = require("../../../package").version;
|
||||
|
||||
transform.transform = transform;
|
||||
|
||||
@@ -4,12 +4,11 @@ import * as acorn from "../../acorn";
|
||||
import * as util from "../util";
|
||||
import fs from "fs";
|
||||
|
||||
export { util };
|
||||
export { util, acorn, transform };
|
||||
export { canCompile } from "../util";
|
||||
|
||||
export { acorn } from "../../acorn";
|
||||
export { default as options } from "../transformation/file/options";
|
||||
export { default as Transformer } from "../transformation/transformer";
|
||||
export { default as transform } from "../transformation";
|
||||
export { default as traverse } from "../traversal";
|
||||
export { default as buildExternalHelpers } from "../tools/build-external-helpers";
|
||||
export { version } from "../../../package";
|
||||
@@ -54,3 +53,19 @@ export function transformFileSync(filename: string, opts?: Object = {}) {
|
||||
opts.filename = filename;
|
||||
return transform(fs.readFileSync(filename), opts);
|
||||
}
|
||||
|
||||
export function parse(code, opts = {}) {
|
||||
opts.sourceType = "module";
|
||||
opts.ecmaVersion = Infinity;
|
||||
opts.plugins = {
|
||||
flow: true,
|
||||
jsx: true
|
||||
};
|
||||
opts.features = {};
|
||||
|
||||
for (var key in transform.transformers) {
|
||||
opts.features[key] = true;
|
||||
}
|
||||
|
||||
return acorn.parse(code, opts);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import path from "path";
|
||||
import os from "os";
|
||||
import fs from "fs";
|
||||
import userHome from "user-home";
|
||||
|
||||
const FILENAME = process.env.BABEL_CACHE_PATH || path.join(os.tmpdir(), "babel.json");
|
||||
const FILENAME = process.env.BABEL_CACHE_PATH || path.join(userHome || os.tmpdir(), ".babel.json");
|
||||
var data = {};
|
||||
|
||||
export function save() {
|
||||
|
||||
@@ -156,16 +156,3 @@ export default function (opts = {}) {
|
||||
|
||||
extend(transformOpts, opts);
|
||||
};
|
||||
|
||||
//
|
||||
|
||||
try {
|
||||
var runtimePackage = require("babel-runtime/package");
|
||||
var version = require("../../package").version;
|
||||
if (runtimePackage.version !== version) {
|
||||
throw new ReferenceError(`The verison of babel-runtime of ${runtimePackage.runtime} that you have installed does not match the babel verison of ${version}`);
|
||||
}
|
||||
} catch (err) {
|
||||
if (err.code !== "MODULE_NOT_FOUND") throw err;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
export function ClassDeclaration(node, print) {
|
||||
if (node.decorators && node.decorators.length) {
|
||||
print.list(node.decorators);
|
||||
}
|
||||
|
||||
print.list(node.decorators);
|
||||
this.push("class");
|
||||
|
||||
if (node.id) {
|
||||
@@ -44,10 +41,24 @@ export function ClassBody(node, print) {
|
||||
}
|
||||
}
|
||||
|
||||
export function MethodDefinition(node, print) {
|
||||
if (node.decorators && node.decorators.length) {
|
||||
print.list(node.decorators);
|
||||
|
||||
export function ClassProperty(node, print) {
|
||||
print.list(node.decorators);
|
||||
|
||||
if (node.static) this.push("static ");
|
||||
print(node.key);
|
||||
print(node.typeAnnotation);
|
||||
if (node.value) {
|
||||
this.space();
|
||||
this.push("=");
|
||||
this.space();
|
||||
print(node.value);
|
||||
}
|
||||
this.semicolon();
|
||||
}
|
||||
|
||||
export function MethodDefinition(node, print) {
|
||||
print.list(node.decorators);
|
||||
|
||||
if (node.static) {
|
||||
this.push("static ");
|
||||
|
||||
@@ -169,4 +169,8 @@ export function MemberExpression(node, print) {
|
||||
}
|
||||
}
|
||||
|
||||
export { MemberExpression as MetaProperty };
|
||||
export function MetaProperty(node, print) {
|
||||
print(node.meta);
|
||||
this.push(".");
|
||||
print(node.property);
|
||||
}
|
||||
|
||||
@@ -14,13 +14,6 @@ export function BooleanTypeAnnotation(node) {
|
||||
this.push("bool");
|
||||
}
|
||||
|
||||
export function ClassProperty(node, print) {
|
||||
if (node.static) this.push("static ");
|
||||
print(node.key);
|
||||
print(node.typeAnnotation);
|
||||
this.semicolon();
|
||||
}
|
||||
|
||||
export function DeclareClass(node, print) {
|
||||
this.push("declare class ");
|
||||
this._interfaceish(node, print);
|
||||
@@ -167,11 +160,24 @@ export { TypeParameterInstantiation as TypeParameterDeclaration };
|
||||
export function ObjectTypeAnnotation(node, print) {
|
||||
this.push("{");
|
||||
var props = node.properties.concat(node.callProperties, node.indexers);
|
||||
|
||||
if (props.length) {
|
||||
this.space();
|
||||
print.list(props, { indent: true, separator: "; " });
|
||||
|
||||
print.list(props, {
|
||||
separator: false,
|
||||
indent: true,
|
||||
iterator: () => {
|
||||
if (props.length !== 1) {
|
||||
this.semicolon();
|
||||
this.space();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.space();
|
||||
}
|
||||
|
||||
this.push("}");
|
||||
}
|
||||
|
||||
|
||||
@@ -75,5 +75,16 @@ export function ArrowFunctionExpression(node, print) {
|
||||
}
|
||||
|
||||
this.push(" => ");
|
||||
|
||||
const bodyNeedsParens = t.isObjectExpression(node.body);
|
||||
|
||||
if (bodyNeedsParens) {
|
||||
this.push("(");
|
||||
}
|
||||
|
||||
print(node.body);
|
||||
|
||||
if (bodyNeedsParens) {
|
||||
this.push(")");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,10 @@ export function ImportDefaultSpecifier(node, print) {
|
||||
print(node.local);
|
||||
}
|
||||
|
||||
export function ExportDefaultSpecifier(node, print) {
|
||||
print(node.exported);
|
||||
}
|
||||
|
||||
export function ExportSpecifier(node, print) {
|
||||
print(node.local);
|
||||
if (node.exported && node.local !== node.exported) {
|
||||
@@ -21,8 +25,18 @@ export function ExportSpecifier(node, print) {
|
||||
}
|
||||
}
|
||||
|
||||
export function ExportNamespaceSpecifier(node, print) {
|
||||
this.push("* as ");
|
||||
print(node.exported);
|
||||
}
|
||||
|
||||
export function ExportAllDeclaration(node, print) {
|
||||
this.push("export * from ");
|
||||
this.push("export *");
|
||||
if (node.exported) {
|
||||
this.push(" as ");
|
||||
print(node.exported);
|
||||
}
|
||||
this.push(" from ");
|
||||
print(node.source);
|
||||
this.semicolon();
|
||||
}
|
||||
@@ -41,16 +55,29 @@ function ExportDeclaration(node, print) {
|
||||
var specifiers = node.specifiers;
|
||||
|
||||
if (node.declaration) {
|
||||
print(node.declaration);
|
||||
if (t.isStatement(node.declaration)) return;
|
||||
var declar = node.declaration;
|
||||
print(declar);
|
||||
if (t.isStatement(declar) || t.isFunction(declar) || t.isClass(declar)) return;
|
||||
} else {
|
||||
this.push("{");
|
||||
if (specifiers.length) {
|
||||
this.space();
|
||||
print.join(specifiers, { separator: ", " });
|
||||
this.space();
|
||||
var first = specifiers[0];
|
||||
var hasSpecial = false;
|
||||
if (t.isExportDefaultSpecifier(first) || t.isExportNamespaceSpecifier(first)) {
|
||||
hasSpecial = true;
|
||||
print(specifiers.shift());
|
||||
if (specifiers.length) {
|
||||
this.push(", ");
|
||||
}
|
||||
}
|
||||
|
||||
if (specifiers.length || (!specifiers.length && !hasSpecial)) {
|
||||
this.push("{");
|
||||
if (specifiers.length) {
|
||||
this.space();
|
||||
print.join(specifiers, { separator: ", " });
|
||||
this.space();
|
||||
}
|
||||
this.push("}");
|
||||
}
|
||||
this.push("}");
|
||||
|
||||
if (node.source) {
|
||||
this.push(" from ");
|
||||
@@ -70,25 +97,20 @@ export function ImportDeclaration(node, print) {
|
||||
|
||||
var specfiers = node.specifiers;
|
||||
if (specfiers && specfiers.length) {
|
||||
var foundImportSpecifier = false;
|
||||
|
||||
for (var i = 0; i < node.specifiers.length; i++) {
|
||||
var spec = node.specifiers[i];
|
||||
|
||||
if (i > 0) {
|
||||
var first = node.specifiers[0];
|
||||
if (t.isImportDefaultSpecifier(first) || t.isImportNamespaceSpecifier(first)) {
|
||||
print(node.specifiers.shift());
|
||||
if (node.specifiers.length) {
|
||||
this.push(", ");
|
||||
}
|
||||
|
||||
if (!t.isImportDefaultSpecifier(spec) && !t.isImportNamespaceSpecifier(spec) && !foundImportSpecifier) {
|
||||
foundImportSpecifier = true;
|
||||
this.push("{ ");
|
||||
}
|
||||
|
||||
print(spec);
|
||||
}
|
||||
|
||||
if (foundImportSpecifier) {
|
||||
this.push(" }");
|
||||
if (node.specifiers.length) {
|
||||
this.push("{");
|
||||
this.space()
|
||||
print.join(node.specifiers, { separator: ", " });
|
||||
this.space()
|
||||
this.push("}");
|
||||
}
|
||||
|
||||
this.push(" from ");
|
||||
|
||||
@@ -71,7 +71,7 @@ export var ForInStatement = buildForXStatement("in");
|
||||
export var ForOfStatement = buildForXStatement("of");
|
||||
|
||||
export function DoWhileStatement(node, print) {
|
||||
this.keyword("do");
|
||||
this.push("do ");
|
||||
print(node.body);
|
||||
this.space();
|
||||
this.keyword("while");
|
||||
@@ -199,9 +199,11 @@ export function VariableDeclaration(node, print, parent) {
|
||||
|
||||
print.list(node.declarations, { separator: sep });
|
||||
|
||||
if (!t.isFor(parent)) {
|
||||
this.semicolon();
|
||||
if (t.isFor(parent)) {
|
||||
if (parent.left === node || parent.init === node) return;
|
||||
}
|
||||
|
||||
this.semicolon();
|
||||
}
|
||||
|
||||
export function VariableDeclarator(node, print) {
|
||||
|
||||
@@ -99,5 +99,12 @@ export function _stringLiteral(val) {
|
||||
return "\\u" + ("0000" + c.charCodeAt(0).toString(16)).slice(-4);
|
||||
});
|
||||
|
||||
if (this.format.quotes === "single") {
|
||||
val = val.slice(1, -1);
|
||||
val = val.replace(/\\"/g, '"');
|
||||
val = val.replace(/'/g, "\\'");
|
||||
val = `'${val}'`;
|
||||
}
|
||||
|
||||
this.push(val);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ class CodeGenerator {
|
||||
|
||||
this.comments = ast.comments || [];
|
||||
this.tokens = ast.tokens || [];
|
||||
this.format = CodeGenerator.normalizeOptions(code, opts);
|
||||
this.format = CodeGenerator.normalizeOptions(code, opts, this.tokens);
|
||||
this.opts = opts;
|
||||
this.ast = ast;
|
||||
|
||||
@@ -26,7 +26,7 @@ class CodeGenerator {
|
||||
this.buffer = new Buffer(this.position, this.format);
|
||||
}
|
||||
|
||||
static normalizeOptions(code, opts) {
|
||||
static normalizeOptions(code, opts, tokens) {
|
||||
var style = " ";
|
||||
if (code) {
|
||||
var indent = detectIndent(code).indent;
|
||||
@@ -36,6 +36,7 @@ class CodeGenerator {
|
||||
var format = {
|
||||
comments: opts.comments == null || opts.comments,
|
||||
compact: opts.compact,
|
||||
quotes: CodeGenerator.findCommonStringDelimeter(code, tokens),
|
||||
indent: {
|
||||
adjustMultilineComment: true,
|
||||
style: style,
|
||||
@@ -54,6 +55,36 @@ class CodeGenerator {
|
||||
return format;
|
||||
}
|
||||
|
||||
static findCommonStringDelimeter(code, tokens) {
|
||||
var occurences = {
|
||||
single: 0,
|
||||
double: 0
|
||||
};
|
||||
|
||||
var checked = 0;
|
||||
|
||||
for (var i = 0; i < tokens.length; i++) {
|
||||
var token = tokens[i];
|
||||
if (token.type.label !== "string") continue;
|
||||
if (checked >= 3) continue;
|
||||
|
||||
var raw = code.slice(token.start, token.end);
|
||||
if (raw[0] === "'") {
|
||||
occurences.single++;
|
||||
} else {
|
||||
occurences.double++;
|
||||
}
|
||||
|
||||
checked++;
|
||||
}
|
||||
|
||||
if (occurences.single > occurences.double) {
|
||||
return "single";
|
||||
} else {
|
||||
return "double";
|
||||
}
|
||||
}
|
||||
|
||||
static generators = {
|
||||
templateLiterals: require("./generators/template-literals"),
|
||||
comprehensions: require("./generators/comprehensions"),
|
||||
@@ -100,7 +131,7 @@ class CodeGenerator {
|
||||
};
|
||||
|
||||
print.list = function (items, opts = {}) {
|
||||
opts.separator ||= ", ";
|
||||
if (opts.separator == null) opts.separator = ", ";
|
||||
print.join(items, opts);
|
||||
};
|
||||
|
||||
|
||||
@@ -22,8 +22,10 @@ export default function (opts, code, callback) {
|
||||
ranges: true
|
||||
};
|
||||
|
||||
parseOpts.plugins.jsx = true;
|
||||
parseOpts.plugins.flow = true;
|
||||
if (opts.nonStandard) {
|
||||
parseOpts.plugins.jsx = true;
|
||||
parseOpts.plugins.flow = true;
|
||||
}
|
||||
|
||||
var ast = acorn.parse(code, parseOpts);
|
||||
|
||||
|
||||
@@ -21,7 +21,8 @@ export const MESSAGES = {
|
||||
evalInStrictMode: "eval is not allowed in strict mode",
|
||||
codeGeneratorDeopt: "Note: The code generator has deoptimised the styling of $1 as it exceeds the max of $2.",
|
||||
missingTemplatesDirectory: "no templates directory - this is most likely the result of a broken `npm publish`. Please report to https://github.com/babel/babel/issues",
|
||||
unsupportedOutputType: "Unsupported output type $1"
|
||||
unsupportedOutputType: "Unsupported output type $1",
|
||||
illegalMethodName: "Illegal method name $1"
|
||||
};
|
||||
|
||||
export function get(key: String, ...args) {
|
||||
|
||||
@@ -7,15 +7,15 @@ import * as t from "./types";
|
||||
|
||||
extend(estraverse.VisitorKeys, t.VISITOR_KEYS);
|
||||
|
||||
// regenerator-babel/ast-types
|
||||
// regenerator/ast-types
|
||||
|
||||
var def = types.Type.def;
|
||||
var or = types.Type.or;
|
||||
|
||||
def("File")
|
||||
.bases("Node")
|
||||
.build("program")
|
||||
.field("program", def("Program"));
|
||||
//def("File")
|
||||
// .bases("Node")
|
||||
// .build("program")
|
||||
// .field("program", def("Program"));
|
||||
|
||||
def("AssignmentPattern")
|
||||
.bases("Pattern")
|
||||
@@ -28,6 +28,11 @@ def("RestElement")
|
||||
.build("argument")
|
||||
.field("argument", def("expression"));
|
||||
|
||||
def("DoExpression")
|
||||
.bases("Expression")
|
||||
.build("body")
|
||||
.field("body", [def("Statement")]);
|
||||
|
||||
def("ExportDefaultDeclaration")
|
||||
.bases("Declaration")
|
||||
.build("declaration")
|
||||
|
||||
@@ -4,4 +4,4 @@ if (global._babelPolyfill) {
|
||||
global._babelPolyfill = true;
|
||||
|
||||
import "core-js/shim";
|
||||
import "regenerator-babel/runtime";
|
||||
import "regenerator/runtime";
|
||||
|
||||
@@ -50,9 +50,9 @@ function buildVar(namespace, builder) {
|
||||
return t.program(body);
|
||||
}
|
||||
|
||||
function buildHelpers(body, namespace, whitelist = []) {
|
||||
function buildHelpers(body, namespace, whitelist) {
|
||||
each(File.helpers, function (name) {
|
||||
if (whitelist.length && whitelist.indexOf(name) === -1) return;
|
||||
if (whitelist && whitelist.indexOf(name) === -1) return;
|
||||
|
||||
var key = t.identifier(t.toIdentifier(name));
|
||||
body.push(t.expressionStatement(
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import stripJsonComments from "strip-json-comments";
|
||||
import merge from "lodash/object/merge";
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
@@ -24,7 +25,7 @@ export default function (loc, opts = {}) {
|
||||
var json;
|
||||
|
||||
try {
|
||||
json = jsons[content] ||= JSON.parse(content);
|
||||
json = jsons[content] ||= JSON.parse(stripJsonComments(content));
|
||||
} catch (err) {
|
||||
err.message = `${file}: ${err.message}`;
|
||||
throw err;
|
||||
@@ -44,7 +45,9 @@ export default function (loc, opts = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
find(loc, rel);
|
||||
if (opts.breakConfig !== true) {
|
||||
find(loc, rel);
|
||||
}
|
||||
|
||||
return opts;
|
||||
};
|
||||
|
||||
@@ -16,6 +16,7 @@ import Logger from "./logger";
|
||||
import parse from "../../helpers/parse";
|
||||
import Scope from "../../traversal/scope";
|
||||
import slash from "slash";
|
||||
import clone from "lodash/lang/clone";
|
||||
import * as util from "../../util";
|
||||
import path from "path";
|
||||
import each from "lodash/collection/each";
|
||||
@@ -36,14 +37,14 @@ function checkNode(stack, node, scope) {
|
||||
|
||||
export default class File {
|
||||
constructor(opts = {}) {
|
||||
this.dynamicImportedNoDefault = [];
|
||||
this.dynamicImportIds = {};
|
||||
this.dynamicImported = [];
|
||||
this.dynamicImports = [];
|
||||
this.dynamicImportTypes = {};
|
||||
this.dynamicImportIds = {};
|
||||
this.dynamicImports = [];
|
||||
|
||||
this.usedHelpers = {};
|
||||
this.dynamicData = {};
|
||||
this.data = {};
|
||||
this.uids = {};
|
||||
|
||||
this.lastStatements = [];
|
||||
this.log = new Logger(this, opts.filename || "unknown");
|
||||
@@ -58,6 +59,7 @@ export default class File {
|
||||
"defaults",
|
||||
"create-class",
|
||||
"create-decorated-class",
|
||||
"create-decorated-object",
|
||||
"tagged-template-literal",
|
||||
"tagged-template-literal-loose",
|
||||
"interop-require",
|
||||
@@ -80,7 +82,13 @@ export default class File {
|
||||
"object-destructuring-empty",
|
||||
"temporal-undefined",
|
||||
"temporal-assert-defined",
|
||||
"self-global"
|
||||
"self-global",
|
||||
"default-props"
|
||||
];
|
||||
|
||||
static soloHelpers = [
|
||||
"ludicrous-proxy-create",
|
||||
"ludicrous-proxy-directory"
|
||||
];
|
||||
|
||||
static options = require("./options");
|
||||
@@ -88,8 +96,10 @@ export default class File {
|
||||
normalizeOptions(opts: Object) {
|
||||
opts = assign({}, opts);
|
||||
|
||||
if (opts.filename && isAbsolute(opts.filename)) {
|
||||
opts = resolveRc(opts.filename, opts);
|
||||
if (opts.filename) {
|
||||
var rcFilename = opts.filename;
|
||||
if (!isAbsolute(rcFilename)) rcFilename = path.join(process.cwd(), rcFilename);
|
||||
opts = resolveRc(rcFilename, opts);
|
||||
}
|
||||
|
||||
//
|
||||
@@ -106,7 +116,14 @@ export default class File {
|
||||
|
||||
var val = opts[key];
|
||||
if (!val && option.optional) continue;
|
||||
if (val == null) val = option.default || val;
|
||||
|
||||
if (val && option.deprecated) {
|
||||
throw new Error("Deprecated option " + key + ": " + option.deprecated);
|
||||
}
|
||||
|
||||
if (val == null) {
|
||||
val = clone(option.default);
|
||||
}
|
||||
|
||||
var optionParser = optionParsers[option.type];
|
||||
if (optionParser) val = optionParser(key, val);
|
||||
@@ -167,20 +184,15 @@ export default class File {
|
||||
return includes(this.opts.loose, key);
|
||||
}
|
||||
|
||||
buildPlugins(stack) {
|
||||
|
||||
}
|
||||
|
||||
buildTransformers() {
|
||||
var file = this;
|
||||
|
||||
var transformers = {};
|
||||
var transformers = this.transformers = {};
|
||||
|
||||
var secondaryStack = [];
|
||||
var stack = [];
|
||||
|
||||
this.buildPlugins(stack);
|
||||
|
||||
// build internal transformers
|
||||
each(transform.transformers, function (transformer, key) {
|
||||
var pass = transformers[key] = transformer.buildPass(file);
|
||||
|
||||
@@ -197,15 +209,23 @@ export default class File {
|
||||
}
|
||||
});
|
||||
|
||||
// init plugins!
|
||||
var beforePlugins = [];
|
||||
var afterPlugins = [];
|
||||
for (var i = 0; i < file.opts.plugins.length; i++) {
|
||||
this.addPlugin(file.opts.plugins[i], beforePlugins, afterPlugins);
|
||||
}
|
||||
stack = beforePlugins.concat(stack, afterPlugins);
|
||||
|
||||
// register
|
||||
this.transformerStack = stack.concat(secondaryStack);
|
||||
this.transformers = transformers;
|
||||
}
|
||||
|
||||
getModuleFormatter(type: string) {
|
||||
var ModuleFormatter = isFunction(type) ? type : transform.moduleFormatters[type];
|
||||
|
||||
if (!ModuleFormatter) {
|
||||
var loc = util.resolve(type);
|
||||
var loc = util.resolveRelative(type);
|
||||
if (loc) ModuleFormatter = require(loc);
|
||||
}
|
||||
|
||||
@@ -216,6 +236,60 @@ export default class File {
|
||||
return new ModuleFormatter(this);
|
||||
}
|
||||
|
||||
addPlugin(name, before, after) {
|
||||
var position = "before";
|
||||
var plugin;
|
||||
|
||||
if (name) {
|
||||
if (typeof name === "object" && name.transformer) {
|
||||
plugin = name.transformer;
|
||||
position ||= name.position;
|
||||
} else if (typeof name === "string") {
|
||||
// this is a plugin in the form of "foobar" or "foobar:after"
|
||||
// where the optional colon is the delimiter for plugin position in the transformer stack
|
||||
|
||||
[name, position = "before"] = name.split(":");
|
||||
|
||||
var loc = util.resolveRelative(name) || util.resolveRelative(`babel-plugin-${name}`);
|
||||
if (loc) {
|
||||
plugin = require(loc)
|
||||
} else {
|
||||
throw new ReferenceError(`Unknown plugin ${JSON.stringify(name)}`);
|
||||
}
|
||||
} else {
|
||||
// not a string so we'll just assume that it's a direct Transformer instance, if not then
|
||||
// the checks later on will complain
|
||||
plugin = name;
|
||||
}
|
||||
} else {
|
||||
throw new TypeError(`Ilegal kind ${typeof name} for plugin name ${JSON.stringify(name)}`);
|
||||
}
|
||||
|
||||
// validate position
|
||||
if (position !== "before" && position !== "after") {
|
||||
throw new TypeError(`Plugin ${JSON.stringify(name)} has an illegal position of ${JSON.stringify(position)}`);
|
||||
}
|
||||
|
||||
// validate transformer key
|
||||
var key = plugin.key;
|
||||
if (this.transformers[key]) {
|
||||
throw new ReferenceError(`The key for plugin ${JSON.stringify(name)} of ${key} collides with an existing plugin`);
|
||||
}
|
||||
|
||||
// validate Transformer instance
|
||||
if (!plugin.buildPass || plugin.constructor.name !== "Transformer") {
|
||||
throw new TypeError(`Plugin ${JSON.stringify(name)} didn't export a default Transformer instance`);
|
||||
}
|
||||
|
||||
// build!
|
||||
var pass = this.transformers[key] = plugin.buildPass(this);
|
||||
if (pass.canTransform()) {
|
||||
var stack = before;
|
||||
if (position === "after") stack = after;
|
||||
stack.push(pass);
|
||||
}
|
||||
}
|
||||
|
||||
parseInputSourceMap(code: string) {
|
||||
var opts = this.opts;
|
||||
|
||||
@@ -269,7 +343,7 @@ export default class File {
|
||||
return source;
|
||||
}
|
||||
|
||||
addImport(source: string, name?: string, noDefault?: boolean): Object {
|
||||
addImport(source: string, name?: string, type?: string): Object {
|
||||
name ||= source;
|
||||
var id = this.dynamicImportIds[name];
|
||||
|
||||
@@ -281,11 +355,14 @@ export default class File {
|
||||
var declar = t.importDeclaration(specifiers, t.literal(source));
|
||||
declar._blockHoist = 3;
|
||||
|
||||
this.dynamicImported.push(declar);
|
||||
if (noDefault) this.dynamicImportedNoDefault.push(declar);
|
||||
if (type) {
|
||||
var modules = this.dynamicImportTypes[type] ||= [];
|
||||
modules.push(declar);
|
||||
}
|
||||
|
||||
if (this.transformers["es6.modules"].canTransform()) {
|
||||
this.moduleFormatter.importSpecifier(specifiers[0], declar, this.dynamicImports);
|
||||
this.moduleFormatter.hasLocalImports = true;
|
||||
} else {
|
||||
this.dynamicImports.push(declar);
|
||||
}
|
||||
@@ -311,7 +388,9 @@ export default class File {
|
||||
}
|
||||
|
||||
addHelper(name: string): Object {
|
||||
if (!includes(File.helpers, name)) {
|
||||
var isSolo = includes(File.soloHelpers, name);
|
||||
|
||||
if (!isSolo && !includes(File.helpers, name)) {
|
||||
throw new ReferenceError(`Unknown helper ${name}`);
|
||||
}
|
||||
|
||||
@@ -322,24 +401,26 @@ export default class File {
|
||||
|
||||
this.usedHelpers[name] = true;
|
||||
|
||||
var generator = this.get("helperGenerator");
|
||||
var runtime = this.get("helpersNamespace");
|
||||
if (generator) {
|
||||
return generator(name);
|
||||
} else if (runtime) {
|
||||
var id = t.identifier(t.toIdentifier(name));
|
||||
return t.memberExpression(runtime, id);
|
||||
} else {
|
||||
var ref = util.template("helper-" + name);
|
||||
ref._compact = true;
|
||||
var uid = this.scope.generateUidIdentifier(name);
|
||||
this.scope.push({
|
||||
key: name,
|
||||
id: uid,
|
||||
init: ref
|
||||
});
|
||||
return uid;
|
||||
if (!isSolo) {
|
||||
var generator = this.get("helperGenerator");
|
||||
var runtime = this.get("helpersNamespace");
|
||||
if (generator) {
|
||||
return generator(name);
|
||||
} else if (runtime) {
|
||||
var id = t.identifier(t.toIdentifier(name));
|
||||
return t.memberExpression(runtime, id);
|
||||
}
|
||||
}
|
||||
|
||||
var ref = util.template("helper-" + name);
|
||||
ref._compact = true;
|
||||
var uid = this.scope.generateUidIdentifier(name);
|
||||
this.scope.push({
|
||||
key: name,
|
||||
id: uid,
|
||||
init: ref
|
||||
});
|
||||
return uid;
|
||||
}
|
||||
|
||||
errorWithNode(node, msg, Error = SyntaxError) {
|
||||
@@ -358,31 +439,16 @@ export default class File {
|
||||
|
||||
shouldIgnore() {
|
||||
var opts = this.opts;
|
||||
|
||||
var filename = opts.filename;
|
||||
var ignore = opts.ignore;
|
||||
var only = opts.only;
|
||||
|
||||
if (only.length) {
|
||||
for (var i = 0; i < only.length; i++) {
|
||||
if (only[i].test(filename)) return false;
|
||||
}
|
||||
return true;
|
||||
} else if (ignore.length) {
|
||||
for (var i = 0; i < ignore.length; i++) {
|
||||
if (ignore[i].test(filename)) return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return util.shouldIgnore(opts.filename, opts.ignore, opts.only);
|
||||
}
|
||||
|
||||
parse(code: string) {
|
||||
if (this.shouldIgnore()) {
|
||||
return {
|
||||
code: code,
|
||||
map: null,
|
||||
ast: null
|
||||
metadata: {},
|
||||
code: code,
|
||||
map: null,
|
||||
ast: null
|
||||
};
|
||||
}
|
||||
|
||||
@@ -394,6 +460,7 @@ export default class File {
|
||||
|
||||
var parseOpts = {
|
||||
highlightCode: opts.highlightCode,
|
||||
nonStandard: opts.nonStandard,
|
||||
filename: opts.filename,
|
||||
plugins: {}
|
||||
};
|
||||
@@ -514,13 +581,14 @@ export default class File {
|
||||
var ast = this.ast;
|
||||
|
||||
var result = {
|
||||
code: "",
|
||||
map: null,
|
||||
ast: null
|
||||
metadata: {},
|
||||
code: "",
|
||||
map: null,
|
||||
ast: null
|
||||
};
|
||||
|
||||
if (this.opts.returnUsedHelpers) {
|
||||
result.usedHelpers = Object.keys(this.usedHelpers);
|
||||
if (this.opts.metadataUsedHelpers) {
|
||||
result.metadata.usedHelpers = Object.keys(this.usedHelpers);
|
||||
}
|
||||
|
||||
if (opts.ast) result.ast = ast;
|
||||
@@ -535,7 +603,9 @@ export default class File {
|
||||
result.code = `${this.shebang}\n${result.code}`;
|
||||
}
|
||||
|
||||
result.map = this.mergeSourceMap(result.map);
|
||||
if (result.map) {
|
||||
result.map = this.mergeSourceMap(result.map);
|
||||
}
|
||||
|
||||
if (opts.sourceMaps === "inline" || opts.sourceMaps === "both") {
|
||||
result.code += "\n" + convertSourceMap.fromObject(result.map).toComment();
|
||||
|
||||
@@ -7,7 +7,7 @@ export default class Logger {
|
||||
}
|
||||
|
||||
_buildMessage(msg: string): string {
|
||||
var parts = this.filename;
|
||||
var parts = `[BABEL] ${this.filename}`;
|
||||
if (msg) parts += `: ${msg}`;
|
||||
return parts;
|
||||
}
|
||||
@@ -18,7 +18,7 @@ export default class Logger {
|
||||
|
||||
deprecate(msg) {
|
||||
if (!this.file.opts.suppressDeprecationMessages) {
|
||||
console.error(msg);
|
||||
console.error(this._buildMessage(msg));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,10 @@ export function transformerList(key, val) {
|
||||
return transform._ensureTransformerNames(key, val);
|
||||
}
|
||||
|
||||
export function number(key, val) {
|
||||
return +val;
|
||||
}
|
||||
|
||||
export function boolean(key, val) {
|
||||
return !!val;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"filename": {
|
||||
"type": "string",
|
||||
"description": "Filename to use when reading from stdin - this will be used in source-maps, errors etc",
|
||||
"description": "filename to use when reading from stdin - this will be used in source-maps, errors etc",
|
||||
"default": "unknown",
|
||||
"shorthand": "f"
|
||||
},
|
||||
@@ -15,12 +15,24 @@
|
||||
"hidden": true
|
||||
},
|
||||
|
||||
"moduleId": {
|
||||
|
||||
"extra": {
|
||||
"hidden": true,
|
||||
"default": {}
|
||||
},
|
||||
|
||||
"highlightTheme": {
|
||||
"hidden": true
|
||||
"moduleId": {
|
||||
"description": "specify a custom name for module ids",
|
||||
"type": "string"
|
||||
},
|
||||
|
||||
"nonStandard": {
|
||||
"type": "boolean",
|
||||
"default": true,
|
||||
"description": "enable support for JSX and Flow"
|
||||
},
|
||||
|
||||
"experimental": {
|
||||
"deprecated": "use `--stage 0`/`{ stage: 0 }` instead"
|
||||
},
|
||||
|
||||
"highlightCode": {
|
||||
@@ -39,34 +51,34 @@
|
||||
"hidden": true
|
||||
},
|
||||
|
||||
"experimental": {
|
||||
"description": "Enable all ES7+ transformers",
|
||||
"stage": {
|
||||
"description": "ECMAScript proposal stage version to allow [0-4]",
|
||||
"shorthand": "e",
|
||||
"type": "boolean",
|
||||
"default": false
|
||||
"type": "number",
|
||||
"default": 2
|
||||
},
|
||||
|
||||
"blacklist": {
|
||||
"type": "transformerList",
|
||||
"description": "Blacklist of transformers to NOT use",
|
||||
"description": "blacklist of transformers to NOT use",
|
||||
"shorthand": "b"
|
||||
},
|
||||
|
||||
"whitelist": {
|
||||
"type": "transformerList",
|
||||
"optional": true,
|
||||
"description": "Whitelist of transformers to ONLY use",
|
||||
"description": "whitelist of transformers to ONLY use",
|
||||
"shorthand": "l"
|
||||
},
|
||||
|
||||
"optional": {
|
||||
"type": "transformerList",
|
||||
"description": "List of optional transformers to enable"
|
||||
"description": "list of optional transformers to enable"
|
||||
},
|
||||
|
||||
"modules": {
|
||||
"type": "string",
|
||||
"description": "Module formatter type to use [common]",
|
||||
"description": "module formatter type to use [common]",
|
||||
"default": "common",
|
||||
"shorthand": "m"
|
||||
},
|
||||
@@ -74,21 +86,36 @@
|
||||
"moduleIds": {
|
||||
"type": "boolean",
|
||||
"default": false,
|
||||
"shorthand": "M"
|
||||
"shorthand": "M",
|
||||
"description": "insert an explicit id for modules"
|
||||
},
|
||||
|
||||
"loose": {
|
||||
"type": "transformerList",
|
||||
"description": "List of transformers to enable loose mode ON",
|
||||
"description": "list of transformers to enable loose mode ON",
|
||||
"shorthand": "L"
|
||||
},
|
||||
|
||||
"jsxPragma": {
|
||||
"type": "string",
|
||||
"description": "custom pragma to use with JSX (same functionality as @jsx comments)",
|
||||
"default": "React.createElement",
|
||||
"shorthand": "P"
|
||||
},
|
||||
|
||||
"plugins": {
|
||||
"type": "list",
|
||||
"description": ""
|
||||
},
|
||||
|
||||
"ignore": {
|
||||
"type": "list"
|
||||
"type": "list",
|
||||
"description": "list of glob paths to **not** compile"
|
||||
},
|
||||
|
||||
"only": {
|
||||
"type": "list"
|
||||
"type": "list",
|
||||
"description": "list of glob paths to **only** compile"
|
||||
},
|
||||
|
||||
"code": {
|
||||
@@ -105,17 +132,19 @@
|
||||
|
||||
"comments": {
|
||||
"type": "boolean",
|
||||
"default": true
|
||||
"default": true,
|
||||
"description": "output comments in generated output"
|
||||
},
|
||||
|
||||
"compact": {
|
||||
"type": "string",
|
||||
"default": "auto"
|
||||
"type": "booleanString",
|
||||
"default": "auto",
|
||||
"description": "do not include superfluous whitespace characters and line terminators [true|false|auto]"
|
||||
},
|
||||
|
||||
"keepModuleIdExtensions": {
|
||||
"type": "boolean",
|
||||
"description": "Keep extensions when generating module ids",
|
||||
"description": "keep extensions when generating module ids",
|
||||
"default": false,
|
||||
"shorthand": "k"
|
||||
},
|
||||
@@ -123,16 +152,18 @@
|
||||
"auxiliaryComment": {
|
||||
"type": "string",
|
||||
"default": "",
|
||||
"shorthand": "a"
|
||||
"shorthand": "a",
|
||||
"description": "attach a comment before all helper declarations and auxiliary code"
|
||||
},
|
||||
|
||||
"externalHelpers": {
|
||||
"type": "string",
|
||||
"default": false,
|
||||
"shorthand": "r"
|
||||
"shorthand": "r",
|
||||
"description": "uses a reference to `babelHelpers` instead of placing helpers at the top of your code."
|
||||
},
|
||||
|
||||
"returnUsedHelpers": {
|
||||
"metadataUsedHelpers": {
|
||||
"type": "boolean",
|
||||
"default": false,
|
||||
"hidden": true
|
||||
@@ -145,23 +176,36 @@
|
||||
|
||||
"sourceMaps": {
|
||||
"type": "booleanString",
|
||||
"description": "[true|false|inline]",
|
||||
"default": false,
|
||||
"shorthand": "s"
|
||||
},
|
||||
|
||||
"sourceMapName": {
|
||||
"type": "string"
|
||||
"type": "string",
|
||||
"description": "set `file` on returned source map"
|
||||
},
|
||||
|
||||
"sourceFileName": {
|
||||
"type": "string"
|
||||
"type": "string",
|
||||
"description": "set `sources[0]` on returned source map"
|
||||
},
|
||||
|
||||
"sourceRoot": {
|
||||
"type": "string"
|
||||
"type": "string",
|
||||
"description": "the root from which all sources are relative"
|
||||
},
|
||||
|
||||
"moduleRoot": {
|
||||
"type": "string"
|
||||
"type": "string",
|
||||
"description": "optional prefix for the AMD module formatter that will be prepend to the filename on module definitions"
|
||||
},
|
||||
|
||||
"breakConfig": {
|
||||
"type": "boolean",
|
||||
"default": false,
|
||||
"hidden": true,
|
||||
"description": "stop trying to load .babelrc files"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ export default function (exports, opts) {
|
||||
exports.JSXIdentifier = function (node, parent) {
|
||||
if (node.name === "this" && t.isReferenced(node, parent)) {
|
||||
return t.thisExpression();
|
||||
} else if (esutils.keyword.isIdentifierName(node.name)) {
|
||||
} else if (esutils.keyword.isIdentifierNameES6(node.name)) {
|
||||
node.type = "Identifier";
|
||||
} else {
|
||||
return t.literal(node.name);
|
||||
@@ -57,6 +57,8 @@ export default function (exports, opts) {
|
||||
|
||||
exports.JSXOpeningElement = {
|
||||
exit(node, parent, scope, file) {
|
||||
parent.children = react.buildChildren(parent);
|
||||
|
||||
var tagExpr = node.name;
|
||||
var args = [];
|
||||
|
||||
@@ -147,20 +149,7 @@ export default function (exports, opts) {
|
||||
exit(node) {
|
||||
var callExpr = node.openingElement;
|
||||
|
||||
for (var i = 0; i < node.children.length; i++) {
|
||||
var child = node.children[i];
|
||||
|
||||
if (t.isLiteral(child) && typeof child.value === "string") {
|
||||
cleanJSXElementLiteralChild(child, callExpr.arguments);
|
||||
continue;
|
||||
} else if (t.isJSXEmptyExpression(child)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
callExpr.arguments.push(child);
|
||||
}
|
||||
|
||||
callExpr.arguments = flatten(callExpr.arguments);
|
||||
callExpr.arguments = callExpr.arguments.concat(node.children);
|
||||
|
||||
if (callExpr.arguments.length >= 3) {
|
||||
callExpr._prettyCall = true;
|
||||
@@ -170,69 +159,6 @@ export default function (exports, opts) {
|
||||
}
|
||||
};
|
||||
|
||||
var isStringLiteral = function (node) {
|
||||
return t.isLiteral(node) && isString(node.value);
|
||||
};
|
||||
|
||||
var flatten = function (args) {
|
||||
var flattened = [];
|
||||
var last;
|
||||
|
||||
for (var i = 0; i < args.length; i++) {
|
||||
var arg = args[i];
|
||||
if (isStringLiteral(arg) && isStringLiteral(last)) {
|
||||
last.value += arg.value;
|
||||
} else {
|
||||
last = arg;
|
||||
flattened.push(arg);
|
||||
}
|
||||
}
|
||||
|
||||
return flattened;
|
||||
};
|
||||
|
||||
var cleanJSXElementLiteralChild = function (child, args) {
|
||||
var lines = child.value.split(/\r\n|\n|\r/);
|
||||
|
||||
var lastNonEmptyLine = 0;
|
||||
var i;
|
||||
|
||||
for (i = 0; i < lines.length; i++) {
|
||||
if (lines[i].match(/[^ \t]/)) {
|
||||
lastNonEmptyLine = i;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < lines.length; i++) {
|
||||
var line = lines[i];
|
||||
|
||||
var isFirstLine = i === 0;
|
||||
var isLastLine = i === lines.length - 1;
|
||||
var isLastNonEmptyLine = i === lastNonEmptyLine;
|
||||
|
||||
// replace rendered whitespace tabs with spaces
|
||||
var trimmedLine = line.replace(/\t/g, " ");
|
||||
|
||||
// trim whitespace touching a newline
|
||||
if (!isFirstLine) {
|
||||
trimmedLine = trimmedLine.replace(/^[ ]+/, "");
|
||||
}
|
||||
|
||||
// trim whitespace touching an endline
|
||||
if (!isLastLine) {
|
||||
trimmedLine = trimmedLine.replace(/[ ]+$/, "");
|
||||
}
|
||||
|
||||
if (trimmedLine) {
|
||||
if (!isLastNonEmptyLine) {
|
||||
trimmedLine += " ";
|
||||
}
|
||||
|
||||
args.push(t.literal(trimmedLine));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// display names
|
||||
|
||||
var addDisplayName = function (id, call) {
|
||||
|
||||
@@ -1,10 +1,33 @@
|
||||
import traverse from "../../traversal";
|
||||
import * as t from "../../types";
|
||||
|
||||
export default function (node) {
|
||||
var container = t.functionExpression(null, [], node.body, node.generator, node.async);
|
||||
container.shadow = true;
|
||||
var visitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
if (this.isThisExpression() || this.isReferencedIdentifier({ name: "arguments" })) {
|
||||
state.found = true;
|
||||
this.stop();
|
||||
}
|
||||
|
||||
var call = t.callExpression(container, []);
|
||||
if (this.isFunction()) {
|
||||
this.skip();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default function (node, scope) {
|
||||
var container = t.functionExpression(null, [], node.body, node.generator, node.async);
|
||||
|
||||
var callee = container;
|
||||
var args = [];
|
||||
|
||||
var state = { found: false };
|
||||
scope.traverse(node, visitor, state);
|
||||
if (state.found) {
|
||||
callee = t.memberExpression(container, t.identifier("apply"));
|
||||
args = [t.thisExpression(), t.identifier("arguments")];
|
||||
}
|
||||
|
||||
var call = t.callExpression(callee, args);
|
||||
if (node.generator) call = t.yieldExpression(call, true);
|
||||
|
||||
return t.returnStatement(call);
|
||||
|
||||
@@ -4,17 +4,45 @@ import each from "lodash/collection/each";
|
||||
import has from "lodash/object/has";
|
||||
import * as t from "../../types";
|
||||
|
||||
export function push(mutatorMap, key, kind, computed, value) {
|
||||
var alias = t.toKeyAlias({ computed }, key);
|
||||
export function push(mutatorMap, node, kind, file) {
|
||||
var alias = t.toKeyAlias(node);
|
||||
|
||||
//
|
||||
|
||||
var map = {};
|
||||
if (has(mutatorMap, alias)) map = mutatorMap[alias];
|
||||
mutatorMap[alias] = map;
|
||||
|
||||
map._key = key;
|
||||
if (computed) map._computed = true;
|
||||
//
|
||||
|
||||
map[kind] = value;
|
||||
map._inherits ||= [];
|
||||
map._inherits.push(node);
|
||||
|
||||
map._key = node.key;
|
||||
|
||||
if (node.computed) {
|
||||
map._computed = true;
|
||||
}
|
||||
|
||||
if (node.decorators) {
|
||||
var decorators = map.decorators ||= t.arrayExpression([]);
|
||||
decorators.elements = decorators.elements.concat(node.decorators.map(dec => dec.expression));
|
||||
}
|
||||
|
||||
if (map.value || map.initializer) {
|
||||
throw file.errorWithNode(node, "Key conflict with sibling node");
|
||||
}
|
||||
|
||||
if (node.value) {
|
||||
if (node.kind === "init") kind = "value";
|
||||
if (node.kind === "get") kind = "get";
|
||||
if (node.kind === "set") kind = "set";
|
||||
|
||||
t.inheritsComments(node.value, node);
|
||||
map[kind] = node.value;
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
export function hasComputed(mutatorMap) {
|
||||
|
||||
33
src/babel/transformation/helpers/memoise-decorators.js
Normal file
33
src/babel/transformation/helpers/memoise-decorators.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import * as t from "../../types";
|
||||
|
||||
export default function (decorators, scope) {
|
||||
for (var i = 0; i < decorators.length; i++) {
|
||||
var decorator = decorators[i];
|
||||
var expression = decorator.expression;
|
||||
if (!t.isMemberExpression(expression)) continue;
|
||||
|
||||
var temp = scope.generateMemoisedReference(expression.object);
|
||||
var ref;
|
||||
|
||||
var nodes = [];
|
||||
|
||||
if (temp) {
|
||||
ref = temp;
|
||||
nodes.push(t.assignmentExpression("=", temp, expression.object));
|
||||
} else {
|
||||
ref = expression.object;
|
||||
}
|
||||
|
||||
nodes.push(t.callExpression(
|
||||
t.memberExpression(
|
||||
t.memberExpression(ref, expression.property, expression.computed),
|
||||
t.identifier("bind")
|
||||
),
|
||||
[ref]
|
||||
));
|
||||
|
||||
decorator.expression = t.sequenceExpression(nodes);
|
||||
}
|
||||
|
||||
return decorators;
|
||||
}
|
||||
@@ -25,13 +25,13 @@ var wrap = function (state, method, id, scope) {
|
||||
var template = util.template(templateName, {
|
||||
FUNCTION: method,
|
||||
FUNCTION_ID: id,
|
||||
FUNCTION_KEY: scope.generateUidIdentifier(id.name),
|
||||
WRAPPER_KEY: scope.generateUidIdentifier(id.name + "Wrapper")
|
||||
FUNCTION_KEY: scope.generateUidIdentifier(id.name)
|
||||
});
|
||||
template.callee._skipModulesRemap = true;
|
||||
|
||||
// shim in dummy params to retain function arity, if you try to read the
|
||||
// source then you'll get the original since it's proxied so it's all good
|
||||
var params = template.callee.body.body[0].declarations[0].init.params;
|
||||
var params = template.callee.body.body[0].params;
|
||||
for (var i = 0, len = getFunctionArity(method); i < len; i++) {
|
||||
params.push(scope.generateUidIdentifier("x"));
|
||||
}
|
||||
@@ -55,7 +55,7 @@ var visit = function (node, name, scope) {
|
||||
// check to see if we have a local binding of the id we're setting inside of
|
||||
// the function, this is important as there are caveats associated
|
||||
|
||||
var bindingInfo = null; // todo: proper scope not being passed in es6/classes // scope.getOwnBindingInfo(name);
|
||||
var bindingInfo = scope.getOwnBindingInfo(name);
|
||||
|
||||
if (bindingInfo) {
|
||||
if (bindingInfo.kind === "param") {
|
||||
@@ -99,6 +99,7 @@ export function property(node, file, scope) {
|
||||
if (!t.isLiteral(key)) return node; // we can't set a function id with this
|
||||
|
||||
var name = t.toIdentifier(key.value);
|
||||
if (name === "eval" || name === "arguments") name = "_" + name;
|
||||
var id = t.identifier(name);
|
||||
|
||||
var method = node.value;
|
||||
|
||||
70
src/babel/transformation/helpers/react.js
vendored
70
src/babel/transformation/helpers/react.js
vendored
@@ -1,3 +1,4 @@
|
||||
import isString from "lodash/lang/isString";
|
||||
import * as t from "../../types";
|
||||
|
||||
var isCreateClassCallExpression = t.buildMatchMemberExpression("React.createClass");
|
||||
@@ -24,3 +25,72 @@ export var isReactComponent = t.buildMatchMemberExpression("React.Component");
|
||||
export function isCompatTag(tagName) {
|
||||
return tagName && /^[a-z]|\-/.test(tagName);
|
||||
}
|
||||
|
||||
function isStringLiteral(node) {
|
||||
return t.isLiteral(node) && isString(node.value);
|
||||
}
|
||||
|
||||
function cleanJSXElementLiteralChild(child, args) {
|
||||
var lines = child.value.split(/\r\n|\n|\r/);
|
||||
|
||||
var lastNonEmptyLine = 0;
|
||||
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
if (lines[i].match(/[^ \t]/)) {
|
||||
lastNonEmptyLine = i;
|
||||
}
|
||||
}
|
||||
|
||||
var str = "";
|
||||
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
var line = lines[i];
|
||||
|
||||
var isFirstLine = i === 0;
|
||||
var isLastLine = i === lines.length - 1;
|
||||
var isLastNonEmptyLine = i === lastNonEmptyLine;
|
||||
|
||||
// replace rendered whitespace tabs with spaces
|
||||
var trimmedLine = line.replace(/\t/g, " ");
|
||||
|
||||
// trim whitespace touching a newline
|
||||
if (!isFirstLine) {
|
||||
trimmedLine = trimmedLine.replace(/^[ ]+/, "");
|
||||
}
|
||||
|
||||
// trim whitespace touching an endline
|
||||
if (!isLastLine) {
|
||||
trimmedLine = trimmedLine.replace(/[ ]+$/, "");
|
||||
}
|
||||
|
||||
if (trimmedLine) {
|
||||
if (!isLastNonEmptyLine) {
|
||||
trimmedLine += " ";
|
||||
}
|
||||
|
||||
str += trimmedLine;
|
||||
}
|
||||
}
|
||||
|
||||
if (str) args.push(t.literal(str));
|
||||
}
|
||||
|
||||
export function buildChildren(node) {
|
||||
var elems = [];
|
||||
|
||||
for (var i = 0; i < node.children.length; i++) {
|
||||
var child = node.children[i];
|
||||
|
||||
if (t.isLiteral(child) && typeof child.value === "string") {
|
||||
cleanJSXElementLiteralChild(child, elems);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (t.isJSXExpressionContainer(child)) child = child.expression;
|
||||
if (t.isJSXEmptyExpression(child)) continue;
|
||||
|
||||
elems.push(child);
|
||||
}
|
||||
|
||||
return elems;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
module.exports = ReplaceSupers;
|
||||
|
||||
import * as messages from "../../messages";
|
||||
import * as t from "../../types";
|
||||
|
||||
|
||||
function isIllegalBareSuper(node, parent) {
|
||||
if (!t.isSuper(node)) return false;
|
||||
if (t.isMemberExpression(parent, { computed: false })) return false;
|
||||
@@ -11,6 +8,10 @@ function isIllegalBareSuper(node, parent) {
|
||||
return true;
|
||||
}
|
||||
|
||||
function isMemberExpressionSuper(node) {
|
||||
return t.isMemberExpression(node) && t.isSuper(node.object);
|
||||
}
|
||||
|
||||
var visitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
var topLevel = state.topLevel;
|
||||
@@ -18,7 +19,7 @@ var visitor = {
|
||||
|
||||
if (t.isFunction(node) && !t.isArrowFunctionExpression(node)) {
|
||||
// we need to call traverseLevel again so we're context aware
|
||||
self.traverseLevel(node, false);
|
||||
self.traverseLevel(this, false);
|
||||
return this.skip();
|
||||
}
|
||||
|
||||
@@ -35,7 +36,10 @@ var visitor = {
|
||||
|
||||
var callback = self.specHandle;
|
||||
if (self.isLoose) callback = self.looseHandle;
|
||||
return callback.call(self, this, getThisReference);
|
||||
var result = callback.call(self, this, getThisReference);
|
||||
if (result) this.hasSuper = true;
|
||||
if (result === true) return;
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -47,6 +51,7 @@ export default class ReplaceSupers {
|
||||
|
||||
constructor(opts: Object, inClass?: boolean = false) {
|
||||
this.topLevelThisReference = opts.topLevelThisReference;
|
||||
this.methodPath = opts.methodPath;
|
||||
this.methodNode = opts.methodNode;
|
||||
this.superRef = opts.superRef;
|
||||
this.isStatic = opts.isStatic;
|
||||
@@ -118,16 +123,16 @@ export default class ReplaceSupers {
|
||||
*/
|
||||
|
||||
replace() {
|
||||
this.traverseLevel(this.methodNode.value, true);
|
||||
this.traverseLevel(this.methodPath.get("value"), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Description
|
||||
*/
|
||||
|
||||
traverseLevel(node: Object, topLevel: boolean) {
|
||||
traverseLevel(path: TraversalPath, topLevel: boolean) {
|
||||
var state = { self: this, topLevel: topLevel };
|
||||
this.scope.traverse(node, visitor, state);
|
||||
path.traverse(visitor, state);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -190,7 +195,6 @@ export default class ReplaceSupers {
|
||||
looseHandle(path: TraversalPath, getThisReference: Function) {
|
||||
var node = path.node;
|
||||
if (path.isSuper()) {
|
||||
this.hasSuper = true;
|
||||
return this.getLooseSuperProperty(node, path.parent);
|
||||
} else if (path.isCallExpression()) {
|
||||
var callee = node.callee;
|
||||
@@ -198,9 +202,31 @@ export default class ReplaceSupers {
|
||||
if (!t.isSuper(callee.object)) return;
|
||||
|
||||
// super.test(); -> objectRef.prototype.MethodName.call(this);
|
||||
this.hasSuper = true;
|
||||
t.appendToMemberExpression(callee, t.identifier("call"));
|
||||
node.arguments.unshift(getThisReference());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description
|
||||
*/
|
||||
|
||||
specHandleAssignmentExpression(ref, path, node, getThisReference) {
|
||||
if (node.operator === "=") {
|
||||
// super.name = "val"; -> _set(Object.getPrototypeOf(objectRef.prototype), "name", this);
|
||||
return this.setSuperProperty(node.left.property, node.right, node.left.computed, getThisReference());
|
||||
} else {
|
||||
// super.age += 2; -> var _ref = super.age; super.age = _ref + 2;
|
||||
ref ||= path.scope.generateUidIdentifier("ref");
|
||||
return [
|
||||
t.variableDeclaration("var", [
|
||||
t.variableDeclarator(ref, node.left)
|
||||
]),
|
||||
t.expressionStatement(
|
||||
t.assignmentExpression("=", node.left, t.binaryExpression(node.operator[0], ref, node.right))
|
||||
)
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -237,7 +263,7 @@ export default class ReplaceSupers {
|
||||
var methodName = methodNode.key.name || "METHOD_NAME";
|
||||
throw this.file.errorWithNode(node, messages.get("classesIllegalSuperCall", methodName));
|
||||
}
|
||||
} else if (t.isMemberExpression(callee) && t.isSuper(callee.object)) {
|
||||
} else if (isMemberExpressionSuper(callee)) {
|
||||
// super.test(); -> _get(Object.getPrototypeOf(objectRef.prototype), "test", this).call(this);
|
||||
property = callee.property;
|
||||
computed = callee.computed;
|
||||
@@ -247,16 +273,22 @@ export default class ReplaceSupers {
|
||||
// super.name; -> _get(Object.getPrototypeOf(objectRef.prototype), "name", this);
|
||||
property = node.property;
|
||||
computed = node.computed;
|
||||
} else if (t.isAssignmentExpression(node) && t.isSuper(node.left.object) && methodNode.kind === "set") {
|
||||
// super.name = "val"; -> _set(Object.getPrototypeOf(objectRef.prototype), "name", this);
|
||||
this.hasSuper = true;
|
||||
return this.setSuperProperty(node.left.property, node.right, node.left.computed, getThisReference());
|
||||
} else if (t.isUpdateExpression(node) && isMemberExpressionSuper(node.argument)) {
|
||||
var binary = t.binaryExpression(node.operator[0], node.argument, t.literal(1))
|
||||
if (node.prefix) {
|
||||
// ++super.foo; -> super.foo += 1;
|
||||
return this.specHandleAssignmentExpression(null, path, binary, getThisReference);
|
||||
} else {
|
||||
// super.foo++; -> var _ref = super.foo; super.foo = _ref + 1;
|
||||
var ref = path.scope.generateUidIdentifier("ref");
|
||||
return this.specHandleAssignmentExpression(ref, path, binary, getThisReference).concat(t.expressionStatement(ref));
|
||||
}
|
||||
} else if (t.isAssignmentExpression(node) && isMemberExpressionSuper(node.left)) {
|
||||
return this.specHandleAssignmentExpression(null, path, node, getThisReference);
|
||||
}
|
||||
|
||||
if (!property) return;
|
||||
|
||||
this.hasSuper = true;
|
||||
|
||||
thisReference = getThisReference();
|
||||
var superProperty = this.getSuperProperty(property, computed, thisReference);
|
||||
if (args) {
|
||||
|
||||
@@ -7,37 +7,56 @@ import * as t from "../../types";
|
||||
|
||||
var remapVisitor = {
|
||||
enter(node, parent, scope, formatter) {
|
||||
if (t.isUpdateExpression(node) && formatter.isLocalReference(node.argument, scope)) {
|
||||
this.skip();
|
||||
|
||||
// expand to long file assignment expression
|
||||
var assign = t.assignmentExpression(node.operator[0] + "=", node.argument, t.literal(1));
|
||||
|
||||
// remap this assignment expression
|
||||
var remapped = formatter.remapExportAssignment(assign);
|
||||
|
||||
// we don't need to change the result
|
||||
if (t.isExpressionStatement(parent) || node.prefix) {
|
||||
return remapped;
|
||||
var remap = formatter.internalRemap[node.name];
|
||||
if (this.isReferencedIdentifier() && remap) {
|
||||
if (!scope.hasBinding(node.name) || scope.bindingIdentifierEquals(node.name, formatter.localImports[node.name])) {
|
||||
return remap;
|
||||
}
|
||||
|
||||
var nodes = [];
|
||||
nodes.push(remapped);
|
||||
|
||||
var operator;
|
||||
if (node.operator === "--") {
|
||||
operator = "+";
|
||||
} else { // "++"
|
||||
operator = "-";
|
||||
}
|
||||
nodes.push(t.binaryExpression(operator, node.argument, t.literal(1)));
|
||||
|
||||
return t.sequenceExpression(nodes);
|
||||
}
|
||||
|
||||
if (t.isAssignmentExpression(node) && formatter.isLocalReference(node.left, scope)) {
|
||||
this.skip();
|
||||
return formatter.remapExportAssignment(node);
|
||||
if (t.isUpdateExpression(node)) {
|
||||
var exported = formatter.getExport(node.argument, scope);
|
||||
|
||||
if (exported) {
|
||||
this.skip();
|
||||
|
||||
// expand to long file assignment expression
|
||||
var assign = t.assignmentExpression(node.operator[0] + "=", node.argument, t.literal(1));
|
||||
|
||||
// remap this assignment expression
|
||||
var remapped = formatter.remapExportAssignment(assign, exported);
|
||||
|
||||
// we don't need to change the result
|
||||
if (t.isExpressionStatement(parent) || node.prefix) {
|
||||
return remapped;
|
||||
}
|
||||
|
||||
var nodes = [];
|
||||
nodes.push(remapped);
|
||||
|
||||
var operator;
|
||||
if (node.operator === "--") {
|
||||
operator = "+";
|
||||
} else { // "++"
|
||||
operator = "-";
|
||||
}
|
||||
nodes.push(t.binaryExpression(operator, node.argument, t.literal(1)));
|
||||
|
||||
return t.sequenceExpression(nodes);
|
||||
}
|
||||
}
|
||||
|
||||
if (node._skipModulesRemap) {
|
||||
return this.skip();
|
||||
}
|
||||
},
|
||||
|
||||
exit(node, parent, scope, formatter) {
|
||||
if (t.isAssignmentExpression(node) && !node._ignoreModulesRemap) {
|
||||
var exported = formatter.getExport(node.left, scope);
|
||||
if (exported) {
|
||||
return formatter.remapExportAssignment(node, exported);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -47,7 +66,6 @@ var importsVisitor = {
|
||||
enter(node, parent, scope, formatter) {
|
||||
formatter.hasLocalImports = true;
|
||||
extend(formatter.localImports, this.getBindingIdentifiers());
|
||||
formatter.bumpImportOccurences(node);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -55,19 +73,32 @@ var importsVisitor = {
|
||||
var exportsVisitor = traverse.explode({
|
||||
ExportDeclaration: {
|
||||
enter(node, parent, scope, formatter) {
|
||||
formatter.hasLocalImports = true;
|
||||
formatter.hasLocalExports = true;
|
||||
|
||||
var declar = this.get("declaration");
|
||||
if (declar.isStatement()) {
|
||||
extend(formatter.localExports, declar.getBindingIdentifiers());
|
||||
var bindings = declar.getBindingIdentifiers()
|
||||
for (var name in bindings) {
|
||||
var binding = bindings[name];
|
||||
formatter._addExport(name, binding);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.isExportNamedDeclaration() && node.specifiers) {
|
||||
for (var i = 0; i < node.specifiers.length; i++) {
|
||||
var specifier = node.specifiers[i];
|
||||
var local = specifier.local;
|
||||
if (!local) continue;
|
||||
|
||||
formatter._addExport(local.name, specifier.exported);
|
||||
}
|
||||
}
|
||||
|
||||
if (!t.isExportDefaultDeclaration(node)) {
|
||||
formatter.hasNonDefaultExports = true;
|
||||
}
|
||||
|
||||
if (node.source) {
|
||||
formatter.bumpImportOccurences(node);
|
||||
var onlyDefault = node.specifiers && node.specifiers.length === 1 && t.isSpecifierDefault(node.specifiers[0]);
|
||||
if (!onlyDefault) {
|
||||
formatter.hasNonDefaultExports = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -75,36 +106,35 @@ var exportsVisitor = traverse.explode({
|
||||
|
||||
export default class DefaultFormatter {
|
||||
constructor(file) {
|
||||
this.scope = file.scope;
|
||||
this.file = file;
|
||||
this.ids = object();
|
||||
this.internalRemap = object();
|
||||
this.defaultIds = object();
|
||||
this.scope = file.scope;
|
||||
this.file = file;
|
||||
this.ids = object();
|
||||
|
||||
this.hasNonDefaultExports = false;
|
||||
|
||||
this.hasLocalExports = false;
|
||||
this.hasLocalImports = false;
|
||||
|
||||
this.localImportOccurences = object();
|
||||
this.localExports = object();
|
||||
this.localImports = object();
|
||||
|
||||
this.getLocalExports();
|
||||
this.getLocalImports();
|
||||
}
|
||||
|
||||
isModuleType(node, type) {
|
||||
var modules = this.file.dynamicImportTypes[type];
|
||||
return modules && modules.indexOf(node) >= 0;
|
||||
}
|
||||
|
||||
transform() {
|
||||
this.remapAssignments();
|
||||
}
|
||||
|
||||
doDefaultExportInterop(node) {
|
||||
return t.isExportDefaultDeclaration(node) && !this.noInteropRequireExport && !this.hasNonDefaultExports;
|
||||
}
|
||||
|
||||
bumpImportOccurences(node) {
|
||||
var source = node.source.value;
|
||||
var occurs = this.localImportOccurences;
|
||||
occurs[source] ||= 0;
|
||||
if (node.specifiers) {
|
||||
occurs[source] += node.specifiers.length;
|
||||
}
|
||||
return (t.isExportDefaultDeclaration(node) || t.isSpecifierDefault(node)) && !this.noInteropRequireExport && !this.hasNonDefaultExports;
|
||||
}
|
||||
|
||||
getLocalExports() {
|
||||
@@ -116,32 +146,40 @@ export default class DefaultFormatter {
|
||||
}
|
||||
|
||||
remapAssignments() {
|
||||
if (this.hasLocalImports) {
|
||||
if (this.hasLocalExports || this.hasLocalImports) {
|
||||
this.file.path.traverse(remapVisitor, this);
|
||||
}
|
||||
}
|
||||
|
||||
isLocalReference(node) {
|
||||
var localImports = this.localImports;
|
||||
return t.isIdentifier(node) && localImports[node.name] && localImports[node.name] !== node;
|
||||
remapExportAssignment(node, exported) {
|
||||
var assign = node;
|
||||
|
||||
for (var i = 0; i < exported.length; i++) {
|
||||
assign = t.assignmentExpression(
|
||||
"=",
|
||||
t.memberExpression(t.identifier("exports"), exported[i]),
|
||||
assign
|
||||
);
|
||||
}
|
||||
|
||||
return assign;
|
||||
}
|
||||
|
||||
remapExportAssignment(node) {
|
||||
return t.assignmentExpression(
|
||||
"=",
|
||||
node.left,
|
||||
t.assignmentExpression(
|
||||
node.operator,
|
||||
t.memberExpression(t.identifier("exports"), node.left),
|
||||
node.right
|
||||
)
|
||||
);
|
||||
_addExport(name, exported) {
|
||||
var info = this.localExports[name] ||= {
|
||||
binding: this.scope.getBindingIdentifier(name),
|
||||
exported: []
|
||||
};
|
||||
info.exported.push(exported);
|
||||
}
|
||||
|
||||
isLocalReference(node, scope) {
|
||||
var localExports = this.localExports;
|
||||
var name = node.name;
|
||||
return t.isIdentifier(node) && localExports[name] && localExports[name] === scope.getBindingIdentifier(name);
|
||||
getExport(node, scope) {
|
||||
if (!t.isIdentifier(node)) return;
|
||||
|
||||
var local = this.localExports[node.name];
|
||||
if (local && local.binding === scope.getBindingIdentifier(node.name)) {
|
||||
return local.exported;
|
||||
}
|
||||
}
|
||||
|
||||
getModuleName() {
|
||||
@@ -219,6 +257,10 @@ export default class DefaultFormatter {
|
||||
nodes.push(this.buildExportsWildcard(ref, node));
|
||||
}
|
||||
|
||||
isLoose() {
|
||||
return this.file.isLoose("es6.modules");
|
||||
}
|
||||
|
||||
exportSpecifier(specifier, node, nodes) {
|
||||
if (node.source) {
|
||||
var ref = this.getExternalReference(node, nodes);
|
||||
@@ -228,6 +270,11 @@ export default class DefaultFormatter {
|
||||
ref = t.callExpression(this.file.addHelper("interop-require"), [ref]);
|
||||
} else {
|
||||
ref = t.memberExpression(ref, specifier.local);
|
||||
|
||||
if (!this.isLoose()) {
|
||||
nodes.push(this.buildExportsFromAssignment(specifier.exported, ref, node));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// export { foo } from "test";
|
||||
@@ -245,6 +292,14 @@ export default class DefaultFormatter {
|
||||
]));
|
||||
}
|
||||
|
||||
buildExportsFromAssignment(id, init) {
|
||||
this.checkExportIdentifier(id);
|
||||
return util.template("exports-from-assign", {
|
||||
INIT: init,
|
||||
ID: t.literal(id.name)
|
||||
}, true);
|
||||
}
|
||||
|
||||
buildExportsAssignment(id, init) {
|
||||
this.checkExportIdentifier(id);
|
||||
return util.template("exports-assign", {
|
||||
|
||||
@@ -6,7 +6,9 @@ import * as util from "../../util";
|
||||
import * as t from "../../types";
|
||||
|
||||
export default class AMDFormatter extends DefaultFormatter {
|
||||
init = CommonFormatter.prototype.init;
|
||||
init() {
|
||||
CommonFormatter.prototype._init.call(this, this.hasNonDefaultExports);
|
||||
}
|
||||
|
||||
buildDependencyLiterals() {
|
||||
var names = [];
|
||||
@@ -21,6 +23,8 @@ export default class AMDFormatter extends DefaultFormatter {
|
||||
*/
|
||||
|
||||
transform(program) {
|
||||
DefaultFormatter.prototype.transform.apply(this, arguments);
|
||||
|
||||
var body = program.body;
|
||||
|
||||
// build an array of module names
|
||||
@@ -69,16 +73,28 @@ export default class AMDFormatter extends DefaultFormatter {
|
||||
}
|
||||
|
||||
importSpecifier(specifier, node, nodes) {
|
||||
var key = node.source.value;
|
||||
var ref = this.getExternalReference(node);
|
||||
|
||||
if (includes(this.file.dynamicImportedNoDefault, node)) {
|
||||
// Prevent unnecessary renaming of dynamic imports.
|
||||
if (t.isImportNamespaceSpecifier(specifier) || t.isImportDefaultSpecifier(specifier)) {
|
||||
this.defaultIds[key] = specifier.local;
|
||||
}
|
||||
|
||||
if (this.isModuleType(node, "absolute")) {
|
||||
// absolute module reference
|
||||
} else if (this.isModuleType(node, "absoluteDefault")) {
|
||||
// prevent unnecessary renaming of dynamic imports
|
||||
this.ids[node.source.value] = ref;
|
||||
ref = t.memberExpression(ref, t.identifier("default"));
|
||||
} else if (t.isImportNamespaceSpecifier(specifier)) {
|
||||
// import * as bar from "foo";
|
||||
} else if (!includes(this.file.dynamicImported, node) && t.isSpecifierDefault(specifier) && !this.noInteropRequireImport) {
|
||||
// import foo from "foo";
|
||||
ref = t.callExpression(this.file.addHelper("interop-require"), [ref]);
|
||||
var uid = this.scope.generateUidIdentifier(specifier.local.name);
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(uid, t.callExpression(this.file.addHelper("interop-require"), [ref]))
|
||||
]));
|
||||
ref = uid;
|
||||
} else {
|
||||
// import { foo } from "foo";
|
||||
var imported = specifier.imported;
|
||||
@@ -86,16 +102,38 @@ export default class AMDFormatter extends DefaultFormatter {
|
||||
ref = t.memberExpression(ref, imported);
|
||||
}
|
||||
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(specifier.local, ref)
|
||||
]));
|
||||
this.internalRemap[specifier.local.name] = ref;
|
||||
}
|
||||
|
||||
exportDeclaration(node) {
|
||||
exportSpecifier(specifier, node, nodes) {
|
||||
if (this.doDefaultExportInterop(specifier)) {
|
||||
this.passModuleArg = true;
|
||||
nodes.push(util.template("exports-default-assign", {
|
||||
VALUE: specifier.local
|
||||
}, true));
|
||||
} else {
|
||||
CommonFormatter.prototype.exportSpecifier.apply(this, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
exportDeclaration(node, nodes) {
|
||||
if (this.doDefaultExportInterop(node)) {
|
||||
this.passModuleArg = true;
|
||||
|
||||
var declar = node.declaration;
|
||||
var assign = util.template("exports-default-assign", {
|
||||
VALUE: this._pushStatement(declar, nodes)
|
||||
}, true);
|
||||
|
||||
if (t.isFunctionDeclaration(declar)) {
|
||||
// we can hoist this assignment to the top of the file
|
||||
assign._blockHoist = 3;
|
||||
}
|
||||
|
||||
nodes.push(assign);
|
||||
return;
|
||||
}
|
||||
|
||||
CommonFormatter.prototype.exportDeclaration.apply(this, arguments);
|
||||
DefaultFormatter.prototype.exportDeclaration.apply(this, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,17 +5,38 @@ import * as t from "../../types";
|
||||
|
||||
export default class CommonJSFormatter extends DefaultFormatter {
|
||||
init() {
|
||||
this._init(this.hasLocalExports);
|
||||
}
|
||||
|
||||
_init(conditional) {
|
||||
var file = this.file;
|
||||
var scope = file.scope;
|
||||
|
||||
scope.rename("module");
|
||||
scope.rename("exports");
|
||||
|
||||
if (!this.noInteropRequireImport && this.hasNonDefaultExports) {
|
||||
if (!this.noInteropRequireImport && conditional) {
|
||||
var templateName = "exports-module-declaration";
|
||||
if (this.file.isLoose("es6.modules")) templateName += "-loose";
|
||||
file.ast.program.body.unshift(util.template(templateName, true));
|
||||
var declar = util.template(templateName, true);
|
||||
declar._blockHoist = 3;
|
||||
file.ast.program.body.unshift(declar);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
transform(program) {
|
||||
DefaultFormatter.prototype.transform.apply(this, arguments);
|
||||
|
||||
if (this.hasDefaultOnlyExport) {
|
||||
program.body.push(
|
||||
t.expressionStatement(t.assignmentExpression(
|
||||
"=",
|
||||
t.memberExpression(t.identifier("module"), t.identifier("exports")),
|
||||
t.memberExpression(t.identifier("exports"), t.identifier("default"))
|
||||
))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
importSpecifier(specifier, node, nodes) {
|
||||
var variableName = specifier.local;
|
||||
@@ -24,14 +45,21 @@ export default class CommonJSFormatter extends DefaultFormatter {
|
||||
|
||||
// import foo from "foo";
|
||||
if (t.isSpecifierDefault(specifier)) {
|
||||
if (!includes(this.file.dynamicImportedNoDefault, node)) {
|
||||
if (this.noInteropRequireImport || includes(this.file.dynamicImported, node)) {
|
||||
ref = t.memberExpression(ref, t.identifier("default"));
|
||||
} else {
|
||||
ref = t.callExpression(this.file.addHelper("interop-require"), [ref]);
|
||||
}
|
||||
if (this.isModuleType(node, "absolute")) {
|
||||
// absolute module reference
|
||||
} else if (this.isModuleType(node, "absoluteDefault")) {
|
||||
this.internalRemap[variableName.name] = ref;
|
||||
} else if (this.noInteropRequireImport) {
|
||||
this.internalRemap[variableName.name] = t.memberExpression(ref, t.identifier("default"));
|
||||
} else {
|
||||
var uid = this.scope.generateUidBasedOnNode(node, "import");
|
||||
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(uid, t.callExpression(this.file.addHelper("interop-require-wildcard"), [ref]))
|
||||
]));
|
||||
|
||||
this.internalRemap[variableName.name] = t.memberExpression(uid, t.identifier("default"));
|
||||
}
|
||||
nodes.push(t.variableDeclaration("var", [t.variableDeclarator(variableName, ref)]));
|
||||
} else {
|
||||
if (t.isImportNamespaceSpecifier(specifier)) {
|
||||
if (!this.noInteropRequireImport) {
|
||||
@@ -44,12 +72,7 @@ export default class CommonJSFormatter extends DefaultFormatter {
|
||||
]));
|
||||
} else {
|
||||
// import { foo } from "foo";
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(
|
||||
variableName,
|
||||
t.memberExpression(ref, specifier.imported)
|
||||
)
|
||||
]));
|
||||
this.internalRemap[variableName.name] = t.memberExpression(ref, specifier.imported);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -61,20 +84,17 @@ export default class CommonJSFormatter extends DefaultFormatter {
|
||||
}, true));
|
||||
}
|
||||
|
||||
exportSpecifier(specifier, node, nodes) {
|
||||
if (this.doDefaultExportInterop(specifier)) {
|
||||
this.hasDefaultOnlyExport = true;
|
||||
}
|
||||
|
||||
DefaultFormatter.prototype.exportSpecifier.apply(this, arguments);
|
||||
}
|
||||
|
||||
exportDeclaration(node, nodes) {
|
||||
if (this.doDefaultExportInterop(node)) {
|
||||
var declar = node.declaration;
|
||||
var assign = util.template("exports-default-assign", {
|
||||
VALUE: this._pushStatement(declar, nodes)
|
||||
}, true);
|
||||
|
||||
if (t.isFunctionDeclaration(declar)) {
|
||||
// we can hoist this assignment to the top of the file
|
||||
assign._blockHoist = 3;
|
||||
}
|
||||
|
||||
nodes.push(assign);
|
||||
return;
|
||||
this.hasDefaultOnlyExport = true;
|
||||
}
|
||||
|
||||
DefaultFormatter.prototype.exportDeclaration.apply(this, arguments);
|
||||
@@ -82,16 +102,24 @@ export default class CommonJSFormatter extends DefaultFormatter {
|
||||
|
||||
_getExternalReference(node, nodes) {
|
||||
var source = node.source.value;
|
||||
var call = t.callExpression(t.identifier("require"), [node.source]);
|
||||
|
||||
if (this.localImportOccurences[source] > 1) {
|
||||
var uid = this.scope.generateUidIdentifier(source);
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(uid, call)
|
||||
]));
|
||||
return uid;
|
||||
var call = t.callExpression(t.identifier("require"), [node.source]);
|
||||
var uid;
|
||||
|
||||
if (this.isModuleType(node, "absolute")) {
|
||||
// absolute module reference
|
||||
} else if (this.isModuleType(node, "absoluteDefault")) {
|
||||
call = t.memberExpression(call, t.identifier("default"));
|
||||
} else {
|
||||
return call;
|
||||
uid = this.scope.generateUidBasedOnNode(node, "import");
|
||||
}
|
||||
|
||||
uid ||= node.specifiers[0].local;
|
||||
|
||||
var declar = t.variableDeclaration("var", [
|
||||
t.variableDeclarator(uid, call)
|
||||
]);
|
||||
nodes.push(declar);
|
||||
return uid;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import map from "lodash/collection/map";
|
||||
import * as t from "../../types";
|
||||
|
||||
var hoistVariablesVisitor = {
|
||||
enter(node, parent, scope, hoistDeclarators) {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isFunction(node)) {
|
||||
// nothing inside is accessible
|
||||
return this.skip();
|
||||
@@ -20,13 +20,13 @@ var hoistVariablesVisitor = {
|
||||
}
|
||||
|
||||
// ignore block hoisted nodes as these can be left in
|
||||
if (node._blockHoist) return;
|
||||
if (state.formatter.canHoist(node)) return;
|
||||
|
||||
var nodes = [];
|
||||
|
||||
for (var i = 0; i < node.declarations.length; i++) {
|
||||
var declar = node.declarations[i];
|
||||
hoistDeclarators.push(t.variableDeclarator(declar.id));
|
||||
state.hoistDeclarators.push(t.variableDeclarator(declar.id));
|
||||
if (declar.init) {
|
||||
// no initializer so we can just hoist it as-is
|
||||
var assign = t.expressionStatement(t.assignmentExpression("=", declar.id, declar.init));
|
||||
@@ -36,14 +36,8 @@ var hoistVariablesVisitor = {
|
||||
|
||||
// for (var i in test)
|
||||
// for (var i = 0;;)
|
||||
if (t.isFor(parent)) {
|
||||
if (parent.left === node) {
|
||||
return node.declarations[0].id;
|
||||
}
|
||||
|
||||
if (parent.init === node) {
|
||||
return nodes;
|
||||
}
|
||||
if (t.isFor(parent) && parent.left === node) {
|
||||
return node.declarations[0].id;
|
||||
}
|
||||
|
||||
return nodes;
|
||||
@@ -52,11 +46,11 @@ var hoistVariablesVisitor = {
|
||||
};
|
||||
|
||||
var hoistFunctionsVisitor = {
|
||||
enter(node, parent, scope, handlerBody) {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isFunction(node)) this.skip();
|
||||
|
||||
if (t.isFunctionDeclaration(node) || node._blockHoist) {
|
||||
handlerBody.push(node);
|
||||
if (t.isFunctionDeclaration(node) || state.formatter.canHoist(node)) {
|
||||
state.handlerBody.push(node);
|
||||
this.remove();
|
||||
}
|
||||
}
|
||||
@@ -90,10 +84,8 @@ export default class SystemFormatter extends AMDFormatter {
|
||||
DefaultFormatter.apply(this, arguments);
|
||||
}
|
||||
|
||||
init() {}
|
||||
|
||||
_addImportSource(node, exportNode) {
|
||||
node._importSource = exportNode.source && exportNode.source.value;
|
||||
if (node) node._importSource = exportNode.source && exportNode.source.value;
|
||||
return node;
|
||||
}
|
||||
|
||||
@@ -119,8 +111,18 @@ export default class SystemFormatter extends AMDFormatter {
|
||||
return this._addImportSource(call, node);
|
||||
}
|
||||
|
||||
remapExportAssignment(node) {
|
||||
return this.buildExportCall(t.literal(node.left.name), node);
|
||||
buildExportsFromAssignment() {
|
||||
return this.buildExportsAssignment(...arguments);
|
||||
}
|
||||
|
||||
remapExportAssignment(node, exported) {
|
||||
var assign = node;
|
||||
|
||||
for (var i = 0; i < exported.length; i++) {
|
||||
assign = this.buildExportCall(t.literal(exported[i].name), assign);
|
||||
}
|
||||
|
||||
return assign;
|
||||
}
|
||||
|
||||
buildExportCall(id, init, isStatement) {
|
||||
@@ -134,6 +136,15 @@ export default class SystemFormatter extends AMDFormatter {
|
||||
|
||||
importSpecifier(specifier, node, nodes) {
|
||||
AMDFormatter.prototype.importSpecifier.apply(this, arguments);
|
||||
|
||||
for (var name in this.internalRemap) {
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(t.identifier(name), this.internalRemap[name])
|
||||
]));
|
||||
}
|
||||
|
||||
this.internalRemap = {};
|
||||
|
||||
this._addImportSource(last(nodes), node);
|
||||
}
|
||||
|
||||
@@ -153,7 +164,13 @@ export default class SystemFormatter extends AMDFormatter {
|
||||
}));
|
||||
}
|
||||
|
||||
canHoist(node) {
|
||||
return node._blockHoist && !this.file.dynamicImports.length;
|
||||
}
|
||||
|
||||
transform(program) {
|
||||
DefaultFormatter.prototype.transform.apply(this, arguments);
|
||||
|
||||
var hoistDeclarators = [];
|
||||
var moduleName = this.getModuleName();
|
||||
var moduleNameLiteral = t.literal(moduleName);
|
||||
@@ -174,7 +191,10 @@ export default class SystemFormatter extends AMDFormatter {
|
||||
var returnStatement = handlerBody.pop();
|
||||
|
||||
// hoist up all variable declarations
|
||||
this.file.scope.traverse(block, hoistVariablesVisitor, hoistDeclarators);
|
||||
this.file.scope.traverse(block, hoistVariablesVisitor, {
|
||||
formatter: this,
|
||||
hoistDeclarators: hoistDeclarators
|
||||
});
|
||||
|
||||
if (hoistDeclarators.length) {
|
||||
var hoistDeclar = t.variableDeclaration("var", hoistDeclarators);
|
||||
@@ -183,7 +203,10 @@ export default class SystemFormatter extends AMDFormatter {
|
||||
}
|
||||
|
||||
// hoist up function declarations for circular references
|
||||
this.file.scope.traverse(block, hoistFunctionsVisitor, handlerBody);
|
||||
this.file.scope.traverse(block, hoistFunctionsVisitor, {
|
||||
formatter: this,
|
||||
handlerBody: handlerBody
|
||||
});
|
||||
|
||||
handlerBody.push(returnStatement);
|
||||
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
import DefaultFormatter from "./_default";
|
||||
import AMDFormatter from "./amd";
|
||||
import values from "lodash/object/values";
|
||||
import path from "path";
|
||||
import * as util from "../../util";
|
||||
import * as t from "../../types";
|
||||
|
||||
export default class UMDFormatter extends AMDFormatter {
|
||||
transform(program) {
|
||||
DefaultFormatter.prototype.transform.apply(this, arguments);
|
||||
|
||||
var body = program.body;
|
||||
|
||||
// build an array of module names
|
||||
|
||||
var names = [];
|
||||
for (var name in this.ids) {
|
||||
for (let name in this.ids) {
|
||||
names.push(t.literal(name));
|
||||
}
|
||||
|
||||
@@ -44,22 +48,36 @@ export default class UMDFormatter extends AMDFormatter {
|
||||
|
||||
// globals
|
||||
|
||||
//var umdArgs = [];
|
||||
var browserArgs = [];
|
||||
if (this.passModuleArg) browserArgs.push(t.identifier("mod"));
|
||||
|
||||
for (let name in this.ids) {
|
||||
var id = this.defaultIds[name] || t.identifier(t.toIdentifier(path.basename(name, path.extname(name))));
|
||||
browserArgs.push(t.memberExpression(t.identifier("global"), id));
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
var moduleName = this.getModuleName();
|
||||
if (moduleName) defineArgs.unshift(t.literal(moduleName));
|
||||
|
||||
//
|
||||
var globalArg = this.file.opts.basename;
|
||||
if (moduleName) globalArg = moduleName;
|
||||
globalArg = t.identifier(t.toIdentifier(globalArg));
|
||||
|
||||
var runner = util.template("umd-runner-body", {
|
||||
AMD_ARGUMENTS: defineArgs,
|
||||
COMMON_TEST: commonTests,
|
||||
COMMON_ARGUMENTS: commonArgs
|
||||
COMMON_ARGUMENTS: commonArgs,
|
||||
BROWSER_ARGUMENTS: browserArgs,
|
||||
GLOBAL_ARG: globalArg
|
||||
});
|
||||
|
||||
//
|
||||
|
||||
var call = t.callExpression(runner, [factory]);
|
||||
program.body = [t.expressionStatement(call)];
|
||||
program.body = [t.expressionStatement(
|
||||
t.callExpression(runner, [t.thisExpression(), factory])
|
||||
)];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"blacklist": ["useStrict", "es6.blockScoping", "regenerator"],
|
||||
"blacklist": ["useStrict", "es6.blockScoping", "regenerator", "es6.spread"],
|
||||
"loose": ["es6.modules"]
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
if (SUPER_NAME != null) {
|
||||
var NATIVE_REF = new SUPER_NAME(...arguments);
|
||||
NATIVE_REF.__proto__ = CLASS_NAME.prototype;
|
||||
return NATIVE_REF;
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
CORE_ID.$for.isIterable(VALUE);
|
||||
@@ -1 +0,0 @@
|
||||
CORE_ID.$for.getIterator(VALUE);
|
||||
@@ -0,0 +1,6 @@
|
||||
Object.defineProperty(exports, ID, {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return INIT;
|
||||
}
|
||||
});
|
||||
@@ -4,7 +4,7 @@
|
||||
var descriptor = props[i];
|
||||
descriptor.enumerable = descriptor.enumerable || false;
|
||||
descriptor.configurable = true;
|
||||
if (descriptor.value) descriptor.writable = true;
|
||||
if ("value" in descriptor) descriptor.writable = true;
|
||||
Object.defineProperty(target, descriptor.key, descriptor);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
throw new TypeError("The decorator for method " + descriptor.key + " is of the invalid type " + typeof decorator);
|
||||
}
|
||||
}
|
||||
initializers[key] = descriptor.initializer;
|
||||
if (initializers) initializers[key] = descriptor.initializer;
|
||||
}
|
||||
|
||||
Object.defineProperty(target, key, descriptor);
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
(function (descriptors) {
|
||||
var target = {};
|
||||
|
||||
for (var i = 0; i < descriptors.length; i ++) {
|
||||
var descriptor = descriptors[i];
|
||||
var decorators = descriptor.decorators;
|
||||
var key = descriptor.key;
|
||||
|
||||
// don't want to expose these to userland since i know people will rely on them
|
||||
// and think it's spec behaviour
|
||||
delete descriptor.key;
|
||||
delete descriptor.decorators;
|
||||
|
||||
descriptor.enumerable = true;
|
||||
descriptor.configurable = true;
|
||||
if ("value" in descriptor) descriptor.writable = true;
|
||||
|
||||
if (decorators) {
|
||||
for (var f = 0; f < decorators.length; f++) {
|
||||
var decorator = decorators[f];
|
||||
if (typeof decorator === "function") {
|
||||
descriptor = decorator(target, key, descriptor) || descriptor;
|
||||
} else {
|
||||
throw new TypeError("The decorator for method " + descriptor.key + " is of the invalid type " + typeof decorator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Object.defineProperty(target, key, descriptor);
|
||||
}
|
||||
|
||||
return target;
|
||||
})
|
||||
10
src/babel/transformation/templates/helper-default-props.js
Normal file
10
src/babel/transformation/templates/helper-default-props.js
Normal file
@@ -0,0 +1,10 @@
|
||||
(function (defaultProps, props) {
|
||||
if (defaultProps) {
|
||||
for (var propName in defaultProps) {
|
||||
if (typeof props[propName] === "undefined") {
|
||||
props[propName] = defaultProps[propName];
|
||||
}
|
||||
}
|
||||
}
|
||||
return props;
|
||||
})
|
||||
@@ -9,7 +9,7 @@
|
||||
} else {
|
||||
return get(parent, property, receiver);
|
||||
}
|
||||
} else if ("value" in desc && desc.writable) {
|
||||
} else if ("value" in desc) {
|
||||
return desc.value;
|
||||
} else {
|
||||
var getter = desc.get;
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
Object(RIGHT)[LEFT] !== undefined;
|
||||
@@ -0,0 +1,4 @@
|
||||
(function (proxy, directory) {
|
||||
directory.push(proxy);
|
||||
return proxy;
|
||||
})
|
||||
@@ -0,0 +1 @@
|
||||
[];
|
||||
@@ -5,15 +5,17 @@
|
||||
var parent = Object.getPrototypeOf(object);
|
||||
|
||||
if (parent !== null) {
|
||||
return set(parent, property, value, receiver);
|
||||
set(parent, property, value, receiver);
|
||||
}
|
||||
} else if ("value" in desc && desc.writable) {
|
||||
return desc.value = value;
|
||||
desc.value = value;
|
||||
} else {
|
||||
var setter = desc.set;
|
||||
|
||||
if (setter !== undefined) {
|
||||
return setter.call(receiver, value);
|
||||
setter.call(receiver, value);
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
});
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
(function (FUNCTION_KEY) {
|
||||
var WRAPPER_KEY = function* FUNCTION_ID() {
|
||||
function* FUNCTION_ID() {
|
||||
return yield* FUNCTION_KEY.apply(this, arguments);
|
||||
};
|
||||
}
|
||||
|
||||
WRAPPER_KEY.toString = function () {
|
||||
FUNCTION_ID.toString = function () {
|
||||
return FUNCTION_KEY.toString();
|
||||
};
|
||||
|
||||
return WRAPPER_KEY;
|
||||
return FUNCTION_ID;
|
||||
})(FUNCTION)
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
(function (FUNCTION_KEY) {
|
||||
var WRAPPER_KEY = function FUNCTION_ID() {
|
||||
function FUNCTION_ID() {
|
||||
return FUNCTION_KEY.apply(this, arguments);
|
||||
};
|
||||
}
|
||||
|
||||
WRAPPER_KEY.toString = function () {
|
||||
FUNCTION_ID.toString = function () {
|
||||
return FUNCTION_KEY.toString();
|
||||
};
|
||||
}
|
||||
|
||||
return WRAPPER_KEY;
|
||||
return FUNCTION_ID;
|
||||
})(FUNCTION)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
(function (root, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(AMD_ARGUMENTS, factory);
|
||||
} else if (typeof exports === 'object') {
|
||||
} else if (typeof exports === "object") {
|
||||
factory(COMMON_ARGUMENTS);
|
||||
} else {
|
||||
factory(BROWSER_ARGUMENTS);
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
(function (factory) {
|
||||
(function (global, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(AMD_ARGUMENTS, factory);
|
||||
} else if (COMMON_TEST) {
|
||||
factory(COMMON_ARGUMENTS);
|
||||
} else {
|
||||
var mod = { exports: {} };
|
||||
factory(mod.exports, BROWSER_ARGUMENTS);
|
||||
global.GLOBAL_ARG = mod.exports;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -32,8 +32,9 @@ export default class TransformerPass {
|
||||
var whitelist = opts.whitelist;
|
||||
if (whitelist) return includes(whitelist, key);
|
||||
|
||||
// experimental
|
||||
if (transformer.metadata.experimental && opts.experimental) return true;
|
||||
// stage
|
||||
var stage = transformer.metadata.stage;
|
||||
if (stage != null && stage >= opts.stage) return true;
|
||||
|
||||
// optional
|
||||
if (transformer.metadata.optional && !includes(opts.optional, key)) return false;
|
||||
|
||||
@@ -9,7 +9,7 @@ import each from "lodash/collection/each";
|
||||
|
||||
/**
|
||||
* This is the class responsible for normalising a transformers handlers
|
||||
* as well as constructing a `TransformerPass` that is repsonsible for
|
||||
* as well as constructing a `TransformerPass` that is responsible for
|
||||
* actually running the transformer over the provided `File`.
|
||||
*/
|
||||
|
||||
@@ -30,6 +30,10 @@ export default class Transformer {
|
||||
this.post = take("post");
|
||||
this.pre = take("pre");
|
||||
|
||||
if (this.metadata.stage != null) {
|
||||
this.metadata.optional = true;
|
||||
}
|
||||
|
||||
this.handlers = this.normalize(transformer);
|
||||
this.opts ||= {};
|
||||
this.key = transformerKey;
|
||||
@@ -65,8 +69,9 @@ export default class Transformer {
|
||||
}
|
||||
|
||||
buildPass(file: File): TransformerPass {
|
||||
// validate Transformer instance
|
||||
if (!(file instanceof File)) {
|
||||
throw new Error("Multiple versions of babel are interacting, this is either due to a version mismatch in a plugin or it was resolved incorrectly");
|
||||
throw new TypeError(`Transformer ${this.key} is resolving to a different Babel version to what is doing the actual transformation...`);
|
||||
}
|
||||
|
||||
return new TransformerPass(file, this);
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
{
|
||||
"useStrict": "strict"
|
||||
"useStrict": "strict",
|
||||
"es5.runtime": "runtime",
|
||||
"es6.runtime": "runtime"
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
{
|
||||
"selfContained": "runtime",
|
||||
"unicode-regex": "regex.unicode",
|
||||
"spec.typeofSymbol": "es6.symbols",
|
||||
"spec.typeofSymbol": "es6.spec.symbols",
|
||||
"es6.symbols": "es6.spec.symbols",
|
||||
"es6.blockScopingTDZ": "es6.spec.blockScoping",
|
||||
|
||||
"minification.deadCodeElimination": "utility.deadCodeElimination",
|
||||
"minification.removeConsoleCalls": "utility.removeConsole",
|
||||
|
||||
@@ -5,14 +5,14 @@ export function check(node) {
|
||||
return t.isProperty(node) && (node.kind === "get" || node.kind === "set");
|
||||
}
|
||||
|
||||
export function ObjectExpression(node) {
|
||||
export function ObjectExpression(node, parent, scope, file) {
|
||||
var mutatorMap = {};
|
||||
var hasAny = false;
|
||||
|
||||
node.properties = node.properties.filter(function (prop) {
|
||||
if (prop.kind === "get" || prop.kind === "set") {
|
||||
hasAny = true;
|
||||
defineMap.push(mutatorMap, prop.key, prop.kind, prop.computed, prop.value);
|
||||
defineMap.push(mutatorMap, prop, prop.kind, file);
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
|
||||
@@ -44,7 +44,7 @@ export function check(node) {
|
||||
export function VariableDeclaration(node, parent, scope, file) {
|
||||
if (!isLet(node, parent)) return;
|
||||
|
||||
if (isLetInitable(node) && file.transformers["es6.blockScopingTDZ"].canTransform()) {
|
||||
if (isLetInitable(node) && file.transformers["es6.spec.blockScoping"].canTransform()) {
|
||||
var nodes = [node];
|
||||
|
||||
for (var i = 0; i < node.declarations.length; i++) {
|
||||
@@ -70,13 +70,13 @@ export function Loop(node, parent, scope, file) {
|
||||
node.body._letDeclarators = [init];
|
||||
}
|
||||
|
||||
var blockScoping = new BlockScoping(this, node.body, parent, scope, file);
|
||||
var blockScoping = new BlockScoping(this, this.get("body"), parent, scope, file);
|
||||
return blockScoping.run();
|
||||
}
|
||||
|
||||
export function BlockStatement(block, parent, scope, file) {
|
||||
if (!t.isLoop(parent)) {
|
||||
var blockScoping = new BlockScoping(null, block, parent, scope, file);
|
||||
var blockScoping = new BlockScoping(null, this, parent, scope, file);
|
||||
blockScoping.run();
|
||||
}
|
||||
}
|
||||
@@ -122,12 +122,14 @@ var letReferenceFunctionVisitor = {
|
||||
// not a direct reference
|
||||
if (!this.isReferencedIdentifier()) return;
|
||||
|
||||
// this scope has a variable with the same name so it couldn't belong
|
||||
// to our let scope
|
||||
if (scope.hasOwnBinding(node.name)) return;
|
||||
var ref = state.letReferences[node.name];
|
||||
|
||||
// not a part of our scope
|
||||
if (!state.letReferences[node.name]) return;
|
||||
if (!ref) return;
|
||||
|
||||
// this scope has a variable with the same name so it couldn't belong
|
||||
// to our let scope
|
||||
if (scope.getBindingIdentifier(node.name) !== ref) return;
|
||||
|
||||
state.closurify = true;
|
||||
}
|
||||
@@ -159,6 +161,18 @@ var loopLabelVisitor = {
|
||||
}
|
||||
};
|
||||
|
||||
var continuationVisitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
if (this.isAssignmentExpression() || this.isUpdateExpression()) {
|
||||
var bindings = this.getBindingIdentifiers();
|
||||
for (var name in bindings) {
|
||||
if (state.outsideReferences[name] !== scope.getBindingIdentifier(name)) continue;
|
||||
state.reassignments[name] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var loopNodeTo = function (node) {
|
||||
if (t.isBreakStatement(node)) {
|
||||
return "break";
|
||||
@@ -225,20 +239,23 @@ class BlockScoping {
|
||||
* Description
|
||||
*/
|
||||
|
||||
constructor(loopPath?: TraversalPath, block: Object, parent: Object, scope: Scope, file: File) {
|
||||
constructor(loopPath?: TraversalPath, blockPath: TraversalPath, parent: Object, scope: Scope, file: File) {
|
||||
this.parent = parent;
|
||||
this.scope = scope;
|
||||
this.block = block;
|
||||
this.file = file;
|
||||
|
||||
this.blockPath = blockPath;
|
||||
this.block = blockPath.node;
|
||||
|
||||
this.outsideLetReferences = object();
|
||||
this.hasLetReferences = false;
|
||||
this.letReferences = block._letReferences = object();
|
||||
this.letReferences = this.block._letReferences = object();
|
||||
this.body = [];
|
||||
|
||||
if (loopPath) {
|
||||
this.loopParent = loopPath.parent;
|
||||
this.loopLabel = t.isLabeledStatement(this.loopParent) && this.loopParent.label;
|
||||
this.loopPath = loopPath;
|
||||
this.loop = loopPath.node;
|
||||
}
|
||||
}
|
||||
@@ -314,7 +331,7 @@ class BlockScoping {
|
||||
traverseReplace(loop.update, loop, scope, remaps);
|
||||
}
|
||||
|
||||
scope.traverse(this.block, replaceVisitor, remaps);
|
||||
this.blockPath.traverse(replaceVisitor, remaps);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -352,16 +369,29 @@ class BlockScoping {
|
||||
|
||||
// turn outsideLetReferences into an array
|
||||
var params = values(outsideRefs);
|
||||
var args = values(outsideRefs);
|
||||
|
||||
// build the closure that we're going to wrap the block with
|
||||
var fn = t.functionExpression(null, params, t.blockStatement(block.body));
|
||||
fn.shadow = true;
|
||||
|
||||
// continuation
|
||||
this.addContinuations(fn);
|
||||
|
||||
// replace the current block body with the one we're going to build
|
||||
block.body = this.body;
|
||||
|
||||
var ref = fn;
|
||||
|
||||
if (this.loop) {
|
||||
ref = this.scope.generateUidIdentifier("loop");
|
||||
this.loopPath.insertBefore(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(ref, fn)
|
||||
]));
|
||||
}
|
||||
|
||||
// build a call and a unique id that we can assign the return value to
|
||||
var call = t.callExpression(fn, params);
|
||||
var call = t.callExpression(ref, args);
|
||||
var ret = this.scope.generateUidIdentifier("ret");
|
||||
|
||||
// handle generators
|
||||
@@ -378,7 +408,50 @@ class BlockScoping {
|
||||
call = t.awaitExpression(call);
|
||||
}
|
||||
|
||||
this.build(ret, call);
|
||||
this.buildClosure(ret, call);
|
||||
}
|
||||
|
||||
/**
|
||||
* Push the closure to the body.
|
||||
*/
|
||||
|
||||
buildClosure(ret: { type: "Identifier" }, call: { type: "CallExpression" }) {
|
||||
var has = this.has;
|
||||
if (has.hasReturn || has.hasBreakContinue) {
|
||||
this.buildHas(ret, call);
|
||||
} else {
|
||||
this.body.push(t.expressionStatement(call));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If any of the outer let variables are reassigned then we need to rename them in
|
||||
* the closure so we can get direct access to the outer variable to continue the
|
||||
* iteration with bindings based on each iteration.
|
||||
*
|
||||
* Reference: https://github.com/babel/babel/issues/1078
|
||||
*/
|
||||
|
||||
addContinuations(fn) {
|
||||
var state = {
|
||||
reassignments: {},
|
||||
outsideReferences: this.outsideLetReferences
|
||||
};
|
||||
|
||||
this.scope.traverse(fn, continuationVisitor, state);
|
||||
|
||||
for (var i = 0; i < fn.params.length; i++) {
|
||||
var param = fn.params[i];
|
||||
if (!state.reassignments[param.name]) continue;
|
||||
|
||||
var newParam = this.scope.generateUidIdentifier(param.name);
|
||||
fn.params[i] = newParam;
|
||||
|
||||
this.scope.rename(param.name, newParam.name, fn);
|
||||
|
||||
// assign outer reference as it's been modified internally and needs to be retained
|
||||
fn.body.body.push(t.expressionStatement(t.assignmentExpression("=", param, newParam)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -392,14 +465,14 @@ class BlockScoping {
|
||||
var declar;
|
||||
|
||||
//
|
||||
for (var i = 0; i < declarators.length; i++) {
|
||||
for (let i = 0; i < declarators.length; i++) {
|
||||
declar = declarators[i];
|
||||
extend(this.outsideLetReferences, t.getBindingIdentifiers(declar));
|
||||
}
|
||||
|
||||
//
|
||||
if (block.body) {
|
||||
for (i = 0; i < block.body.length; i++) {
|
||||
for (let i = 0; i < block.body.length; i++) {
|
||||
declar = block.body[i];
|
||||
if (isLet(declar, block)) {
|
||||
declarators = declarators.concat(declar.declarations);
|
||||
@@ -408,7 +481,7 @@ class BlockScoping {
|
||||
}
|
||||
|
||||
//
|
||||
for (i = 0; i < declarators.length; i++) {
|
||||
for (let i = 0; i < declarators.length; i++) {
|
||||
declar = declarators[i];
|
||||
var keys = t.getBindingIdentifiers(declar);
|
||||
extend(this.letReferences, keys);
|
||||
@@ -428,7 +501,7 @@ class BlockScoping {
|
||||
|
||||
// traverse through this block, stopping on functions and checking if they
|
||||
// contain any local let references
|
||||
this.scope.traverse(this.block, letReferenceBlockVisitor, state);
|
||||
this.blockPath.traverse(letReferenceBlockVisitor, state);
|
||||
|
||||
return state.closurify;
|
||||
}
|
||||
@@ -452,8 +525,8 @@ class BlockScoping {
|
||||
map: {}
|
||||
};
|
||||
|
||||
this.scope.traverse(this.block, loopLabelVisitor, state);
|
||||
this.scope.traverse(this.block, loopVisitor, state);
|
||||
this.blockPath.traverse(loopLabelVisitor, state);
|
||||
this.blockPath.traverse(loopVisitor, state);
|
||||
|
||||
return state;
|
||||
}
|
||||
@@ -464,7 +537,7 @@ class BlockScoping {
|
||||
*/
|
||||
|
||||
hoistVarDeclarations() {
|
||||
traverse(this.block, hoistVarDeclarationsVisitor, this.scope, this);
|
||||
this.blockPath.traverse(hoistVarDeclarationsVisitor, this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -490,19 +563,6 @@ class BlockScoping {
|
||||
return replace;
|
||||
}
|
||||
|
||||
/**
|
||||
* Push the closure to the body.
|
||||
*/
|
||||
|
||||
build(ret: { type: "Identifier" }, call: { type: "CallExpression" }) {
|
||||
var has = this.has;
|
||||
if (has.hasReturn || has.hasBreakContinue) {
|
||||
this.buildHas(ret, call);
|
||||
} else {
|
||||
this.body.push(t.expressionStatement(call));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description
|
||||
*/
|
||||
@@ -542,7 +602,7 @@ class BlockScoping {
|
||||
single.consequent[0]
|
||||
)));
|
||||
} else {
|
||||
// #998
|
||||
// https://github.com/babel/babel/issues/998
|
||||
for (var i = 0; i < cases.length; i++) {
|
||||
var caseConsequent = cases[i].consequent[0];
|
||||
if (t.isBreakStatement(caseConsequent) && !caseConsequent.label) {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import memoiseDecorators from "../../helpers/memoise-decorators";
|
||||
import ReplaceSupers from "../../helpers/replace-supers";
|
||||
import * as nameMethod from "../../helpers/name-method";
|
||||
import * as defineMap from "../../helpers/define-map";
|
||||
@@ -8,6 +9,8 @@ import each from "lodash/collection/each";
|
||||
import has from "lodash/object/has";
|
||||
import * as t from "../../../types";
|
||||
|
||||
const PROPERTY_COLLISION_METHOD_NAME = "__initializeProperties";
|
||||
|
||||
export var check = t.isClass;
|
||||
|
||||
export function ClassDeclaration(node, parent, scope, file) {
|
||||
@@ -20,6 +23,20 @@ export function ClassExpression(node, parent, scope, file) {
|
||||
return new ClassTransformer(this, file).run();
|
||||
}
|
||||
|
||||
var collectPropertyReferencesVisitor = {
|
||||
Identifier: {
|
||||
enter(node, parent, scope, state) {
|
||||
if (this.parentPath.isClassProperty({ key: node })) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.isReferenced() && scope.getBinding(node.name) === state.scope.getBinding(node.name)) {
|
||||
state.references[node.name] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var verifyConstructorVisitor = traverse.explode({
|
||||
MethodDefinition: {
|
||||
enter() {
|
||||
@@ -34,7 +51,7 @@ var verifyConstructorVisitor = traverse.explode({
|
||||
},
|
||||
|
||||
CallExpression: {
|
||||
enter(node, parent, scope, state) {
|
||||
exit(node, parent, scope, state) {
|
||||
if (this.get("callee").isSuper()) {
|
||||
state.hasBareSuper = true;
|
||||
state.bareSuper = this;
|
||||
@@ -46,11 +63,27 @@ var verifyConstructorVisitor = traverse.explode({
|
||||
}
|
||||
},
|
||||
|
||||
FunctionDeclaration: {
|
||||
enter() {
|
||||
this.skip();
|
||||
}
|
||||
},
|
||||
|
||||
FunctionExpression: {
|
||||
enter() {
|
||||
this.skip();
|
||||
}
|
||||
},
|
||||
|
||||
ThisExpression: {
|
||||
enter(node, parent, scope, state) {
|
||||
if (state.hasSuper && !state.hasBareSuper) {
|
||||
throw this.errorWithNode("'this' is not allowed before super()");
|
||||
}
|
||||
|
||||
if (state.isNativeSuper) {
|
||||
return state.nativeSuperRef;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -75,6 +108,7 @@ class ClassTransformer {
|
||||
this.staticMutatorMap = {};
|
||||
|
||||
this.instancePropBody = [];
|
||||
this.instancePropRefs = {};
|
||||
this.staticPropBody = [];
|
||||
this.body = [];
|
||||
|
||||
@@ -104,6 +138,15 @@ class ClassTransformer {
|
||||
|
||||
//
|
||||
|
||||
var superClass = this.node.superClass;
|
||||
this.isNativeSuper = superClass && t.isIdentifier(superClass) && t.NATIVE_TYPE_NAMES.indexOf(superClass.name) >= 0;
|
||||
|
||||
if (this.isNativeSuper) {
|
||||
this.nativeSuperRef = this.scope.generateUidIdentifier("this");
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
var body = this.body;
|
||||
|
||||
//
|
||||
@@ -126,11 +169,10 @@ class ClassTransformer {
|
||||
var closureArgs = [];
|
||||
|
||||
//
|
||||
|
||||
if (this.hasSuper) {
|
||||
closureArgs.push(superName);
|
||||
|
||||
superName = this.scope.generateUidBasedOnNode(superName, this.file);
|
||||
superName = this.scope.generateUidBasedOnNode(superName);
|
||||
closureParams.push(superName);
|
||||
|
||||
this.superName = superName;
|
||||
@@ -138,27 +180,50 @@ class ClassTransformer {
|
||||
}
|
||||
|
||||
//
|
||||
var decorators = this.node.decorators;
|
||||
if (decorators) {
|
||||
// create a class reference to use later on
|
||||
this.classRef = this.scope.generateUidIdentifier(classRef);
|
||||
|
||||
// this is so super calls and the decorators have access to the raw function
|
||||
body.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(this.classRef, classRef)
|
||||
]));
|
||||
}
|
||||
|
||||
//
|
||||
this.buildBody();
|
||||
|
||||
// make sure this class isn't directly called
|
||||
constructorBody.body.unshift(t.expressionStatement(t.callExpression(file.addHelper("class-call-check"), [
|
||||
t.thisExpression(),
|
||||
classRef
|
||||
this.classRef
|
||||
])));
|
||||
|
||||
//
|
||||
|
||||
var decorators = this.node.decorators;
|
||||
if (decorators) {
|
||||
// reverse the decorators so we execute them in the right order
|
||||
decorators = decorators.reverse();
|
||||
|
||||
for (var i = 0; i < decorators.length; i++) {
|
||||
var decorator = decorators[i];
|
||||
body.push(util.template("class-decorator", {
|
||||
|
||||
var decoratorNode = util.template("class-decorator", {
|
||||
DECORATOR: decorator.expression,
|
||||
CLASS_REF: classRef
|
||||
}, true));
|
||||
}, true);
|
||||
decoratorNode.expression._ignoreModulesRemap = true;
|
||||
body.push(decoratorNode);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.isNativeSuper) {
|
||||
// we've determined this is inheriting from a native class so return the constructed
|
||||
// instance
|
||||
constructorBody.body.push(t.returnStatement(this.nativeSuperRef));
|
||||
}
|
||||
|
||||
if (this.className) {
|
||||
// named class with only a constructor
|
||||
if (body.length === 1) return t.toExpression(body[0]);
|
||||
@@ -199,42 +264,15 @@ class ClassTransformer {
|
||||
mutatorMap = this.instanceMutatorMap;
|
||||
}
|
||||
|
||||
var alias = t.toKeyAlias(node);
|
||||
|
||||
//
|
||||
|
||||
var map = {};
|
||||
if (has(mutatorMap, alias)) map = mutatorMap[alias];
|
||||
mutatorMap[alias] = map;
|
||||
|
||||
//
|
||||
|
||||
map._inherits ||= [];
|
||||
map._inherits.push(node);
|
||||
|
||||
map._key = node.key;
|
||||
var map = defineMap.push(mutatorMap, node, kind, this.file);
|
||||
|
||||
if (enumerable) {
|
||||
map.enumerable = t.literal(true)
|
||||
}
|
||||
|
||||
if (node.computed) {
|
||||
map._computed = true;
|
||||
}
|
||||
|
||||
if (node.decorators) {
|
||||
if (map.decorators) {
|
||||
this.hasDecorators = true;
|
||||
var decorators = map.decorators ||= t.arrayExpression([]);
|
||||
decorators.elements = decorators.elements.concat(node.decorators.map(dec => dec.expression));
|
||||
}
|
||||
|
||||
if (map.value || map.initializer) {
|
||||
throw this.file.errorWithNode(node, "Key conflict with sibling node");
|
||||
}
|
||||
|
||||
if (node.kind === "get") kind = "get";
|
||||
if (node.kind === "set") kind = "set";
|
||||
map[kind] = node.value;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -253,11 +291,18 @@ class ClassTransformer {
|
||||
|
||||
for (var i = 0; i < classBody.length; i++) {
|
||||
var node = classBody[i];
|
||||
var path = classBodyPaths[i];
|
||||
|
||||
if (node.decorators) {
|
||||
memoiseDecorators(node.decorators, this.scope);
|
||||
}
|
||||
|
||||
if (t.isMethodDefinition(node)) {
|
||||
var isConstructor = node.kind === "constructor";
|
||||
if (isConstructor) this.verifyConstructor(classBodyPaths[i]);
|
||||
if (isConstructor) this.verifyConstructor(path);
|
||||
|
||||
var replaceSupers = new ReplaceSupers({
|
||||
methodPath: path,
|
||||
methodNode: node,
|
||||
objectRef: this.classRef,
|
||||
superRef: this.superName,
|
||||
@@ -270,7 +315,7 @@ class ClassTransformer {
|
||||
replaceSupers.replace();
|
||||
|
||||
if (isConstructor) {
|
||||
this.pushConstructor(node);
|
||||
this.pushConstructor(node, path);
|
||||
} else {
|
||||
this.pushMethod(node);
|
||||
}
|
||||
@@ -283,7 +328,9 @@ class ClassTransformer {
|
||||
if (!this.hasConstructor && this.hasSuper) {
|
||||
var helperName = "class-super-constructor-call";
|
||||
if (this.isLoose) helperName += "-loose";
|
||||
if (this.isNativeSuper) helperName = "class-super-native-constructor-call";
|
||||
constructorBody.body.push(util.template(helperName, {
|
||||
NATIVE_REF: this.nativeSuperRef,
|
||||
CLASS_NAME: className,
|
||||
SUPER_NAME: this.superName
|
||||
}, true));
|
||||
@@ -359,8 +406,25 @@ class ClassTransformer {
|
||||
|
||||
placePropertyInitializers() {
|
||||
var body = this.instancePropBody;
|
||||
if (body.length) {
|
||||
// todo: check for scope conflicts and shift into a method
|
||||
if (!body.length) return;
|
||||
|
||||
if (this.hasPropertyCollision()) {
|
||||
var call = t.expressionStatement(t.callExpression(
|
||||
t.memberExpression(t.thisExpression(), t.identifier(PROPERTY_COLLISION_METHOD_NAME)),
|
||||
[]
|
||||
));
|
||||
|
||||
this.pushMethod(t.methodDefinition(
|
||||
t.identifier(PROPERTY_COLLISION_METHOD_NAME),
|
||||
t.functionExpression(null, [], t.blockStatement(body))
|
||||
), true);
|
||||
|
||||
if (this.hasSuper) {
|
||||
this.bareSuper.insertAfter(call);
|
||||
} else {
|
||||
this.constructorBody.body.unshift(call);
|
||||
}
|
||||
} else {
|
||||
if (this.hasSuper) {
|
||||
if (this.hasConstructor) {
|
||||
this.bareSuper.insertAfter(body);
|
||||
@@ -373,32 +437,69 @@ class ClassTransformer {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description
|
||||
*/
|
||||
|
||||
hasPropertyCollision(): boolean {
|
||||
if (this.userConstructorPath) {
|
||||
for (var name in this.instancePropRefs) {
|
||||
if (this.userConstructorPath.scope.hasOwnBinding(name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Description
|
||||
*/
|
||||
|
||||
verifyConstructor(path: TraversalPath) {
|
||||
var state = {
|
||||
hasBareSuper: false,
|
||||
bareSuper: null,
|
||||
hasSuper: this.hasSuper,
|
||||
file: this.file
|
||||
nativeSuperRef: this.nativeSuperRef,
|
||||
isNativeSuper: this.isNativeSuper,
|
||||
hasBareSuper: false,
|
||||
bareSuper: null,
|
||||
hasSuper: this.hasSuper,
|
||||
file: this.file
|
||||
};
|
||||
|
||||
path.traverse(verifyConstructorVisitor, state);
|
||||
path.get("value").traverse(verifyConstructorVisitor, state);
|
||||
|
||||
this.bareSuper = state.bareSuper;
|
||||
|
||||
if (!state.hasBareSuper && this.hasSuper) {
|
||||
throw path.errorWithNode("Derived constructor must call super()");
|
||||
}
|
||||
|
||||
if (this.isNativeSuper && this.bareSuper) {
|
||||
this.bareSuper.replaceWithMultiple([
|
||||
t.variableDeclaration("var", [
|
||||
t.variableDeclarator(this.nativeSuperRef, t.newExpression(this.superName, this.bareSuper.node.arguments))
|
||||
]),
|
||||
|
||||
t.expressionStatement(t.assignmentExpression(
|
||||
"=",
|
||||
t.memberExpression(this.nativeSuperRef, t.identifier("__proto__")),
|
||||
t.memberExpression(this.classRef, t.identifier("prototype"))
|
||||
)),
|
||||
t.expressionStatement(this.nativeSuperRef)
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Push a method to its respective mutatorMap.
|
||||
*/
|
||||
|
||||
pushMethod(node: { type: "MethodDefinition" }) {
|
||||
pushMethod(node: { type: "MethodDefinition" }, allowedIllegal?) {
|
||||
if (!allowedIllegal && t.isLiteral(t.toComputedKey(node), { value: PROPERTY_COLLISION_METHOD_NAME })) {
|
||||
throw this.file.errorWithNode(node, messages.get("illegalMethodName", PROPERTY_COLLISION_METHOD_NAME));
|
||||
}
|
||||
|
||||
if (node.kind === "method") {
|
||||
nameMethod.property(node, this.file, this.scope);
|
||||
|
||||
@@ -424,10 +525,13 @@ class ClassTransformer {
|
||||
*/
|
||||
|
||||
pushProperty(node: { type: "ClassProperty" }) {
|
||||
if (!node.value && !node.decorators) return;
|
||||
|
||||
var key;
|
||||
|
||||
this.scope.traverse(node, collectPropertyReferencesVisitor, {
|
||||
references: this.instancePropRefs,
|
||||
scope: this.scope
|
||||
});
|
||||
|
||||
if (node.decorators) {
|
||||
var body = [];
|
||||
if (node.value) body.push(t.returnStatement(node.value));
|
||||
@@ -447,16 +551,22 @@ class ClassTransformer {
|
||||
}, true));
|
||||
}
|
||||
} else {
|
||||
if (node.static) {
|
||||
// can just be added to the static map
|
||||
this.pushToMap(node, true);
|
||||
} else {
|
||||
if (!node.static && node.value) {
|
||||
// add this to the instancePropBody which will be added after the super call in a derived constructor
|
||||
// or at the start of a constructor for a non-derived constructor
|
||||
this.instancePropBody.push(t.expressionStatement(
|
||||
t.assignmentExpression("=", t.memberExpression(t.thisExpression(), node.key), node.value)
|
||||
));
|
||||
|
||||
node.value = null;
|
||||
}
|
||||
|
||||
if (!node.value) {
|
||||
node.value = t.identifier("undefined");
|
||||
}
|
||||
|
||||
// can just be added to the static map
|
||||
this.pushToMap(node, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -464,12 +574,19 @@ class ClassTransformer {
|
||||
* Replace the constructor body of our class.
|
||||
*/
|
||||
|
||||
pushConstructor(method: { type: "MethodDefinition" }) {
|
||||
pushConstructor(method: { type: "MethodDefinition" }, path: TraversalPath) {
|
||||
// https://github.com/babel/babel/issues/1077
|
||||
var fnPath = path.get("value");
|
||||
if (fnPath.scope.hasOwnBinding(this.classRef.name)) {
|
||||
fnPath.scope.rename(this.classRef.name);
|
||||
}
|
||||
|
||||
var construct = this.constructor;
|
||||
var fn = method.value;
|
||||
|
||||
this.userConstructor = fn;
|
||||
this.hasConstructor = true;
|
||||
this.userConstructorPath = fnPath;
|
||||
this.userConstructor = fn;
|
||||
this.hasConstructor = true;
|
||||
|
||||
t.inheritsComments(construct, method);
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import * as messages from "../../../messages";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export function check(node) {
|
||||
return t.isVariableDeclaration(node, { kind: "const" });
|
||||
return t.isVariableDeclaration(node, { kind: "const" }) || t.isImportDeclaration(node);
|
||||
}
|
||||
|
||||
var visitor = {
|
||||
@@ -38,7 +38,7 @@ var visitor = {
|
||||
|
||||
export function Scopable(node, parent, scope, file) {
|
||||
this.traverse(visitor, {
|
||||
constants: scope.getAllBindingsOfKind("const"),
|
||||
constants: scope.getAllBindingsOfKind("const", "module"),
|
||||
file: file
|
||||
});
|
||||
}
|
||||
|
||||
@@ -126,10 +126,11 @@ export function AssignmentExpression(node, parent, scope, file) {
|
||||
if (!t.isPattern(node.left)) return;
|
||||
|
||||
var ref = scope.generateUidIdentifier("temp");
|
||||
scope.push({ id: ref });
|
||||
|
||||
var nodes = [];
|
||||
nodes.push(t.expressionStatement(t.assignmentExpression("=", ref, node.right)));
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(ref, node.right)
|
||||
]));
|
||||
|
||||
var destructuring = new DestructuringTransformer({
|
||||
operator: node.operator,
|
||||
@@ -137,6 +138,11 @@ export function AssignmentExpression(node, parent, scope, file) {
|
||||
scope: scope,
|
||||
nodes: nodes
|
||||
});
|
||||
|
||||
if (t.isArrayExpression(node.right)) {
|
||||
destructuring.arrays[ref.name] = true;
|
||||
}
|
||||
|
||||
destructuring.init(node.left, ref);
|
||||
|
||||
nodes.push(t.expressionStatement(ref));
|
||||
@@ -332,7 +338,7 @@ class DestructuringTransformer {
|
||||
if (t.isSpreadProperty(prop)) continue;
|
||||
|
||||
var key = prop.key;
|
||||
if (t.isIdentifier(key)) key = t.literal(prop.key.name);
|
||||
if (t.isIdentifier(key) && !prop.computed) key = t.literal(prop.key.name);
|
||||
keys.push(key);
|
||||
}
|
||||
|
||||
@@ -397,9 +403,14 @@ class DestructuringTransformer {
|
||||
if (pattern.elements.length > arr.elements.length) return;
|
||||
if (pattern.elements.length < arr.elements.length && !hasRest(pattern)) return false;
|
||||
|
||||
// deopt on holes
|
||||
for (var i = 0; i < pattern.elements.length; i++) {
|
||||
if (!pattern.elements[i]) return false;
|
||||
var elem = pattern.elements[i];
|
||||
|
||||
// deopt on holes
|
||||
if (!elem) return false;
|
||||
|
||||
// deopt on member expressions
|
||||
if (t.isMemberExpression(elem)) return false;
|
||||
}
|
||||
|
||||
// deopt on reference to left side identifiers
|
||||
|
||||
@@ -17,9 +17,6 @@ export function ForOfStatement(node, parent, scope, file) {
|
||||
var loop = build.loop;
|
||||
var block = loop.body;
|
||||
|
||||
// inherit comments from the original loop
|
||||
t.inheritsComments(loop, node);
|
||||
|
||||
// ensure that it's a block so we can take all its statements
|
||||
t.ensureBlock(node);
|
||||
|
||||
@@ -32,9 +29,14 @@ export function ForOfStatement(node, parent, scope, file) {
|
||||
block.body = block.body.concat(node.body.body);
|
||||
|
||||
t.inherits(loop, node);
|
||||
t.inherits(loop.body, node.body);
|
||||
|
||||
if (build.replaceParent) this.parentPath.replaceWithMultiple(build.node);
|
||||
return build.node;
|
||||
if (build.replaceParent) {
|
||||
this.parentPath.replaceWithMultiple(build.node);
|
||||
this.remove();
|
||||
} else {
|
||||
this.replaceWithMultiple(build.node);
|
||||
}
|
||||
}
|
||||
|
||||
export function _ForOfStatementArray(node, scope, file) {
|
||||
|
||||
@@ -3,7 +3,7 @@ import * as t from "../../../types";
|
||||
|
||||
export var check = t.isSuper;
|
||||
|
||||
function Property(node, scope, getObjectRef, file) {
|
||||
function Property(path, node, scope, getObjectRef, file) {
|
||||
if (!node.method) return;
|
||||
|
||||
var value = node.value;
|
||||
@@ -13,6 +13,7 @@ function Property(node, scope, getObjectRef, file) {
|
||||
topLevelThisReference: thisExpr,
|
||||
getObjectRef: getObjectRef,
|
||||
methodNode: node,
|
||||
methodPath: path,
|
||||
isStatic: true,
|
||||
scope: scope,
|
||||
file: file
|
||||
@@ -33,8 +34,9 @@ export function ObjectExpression(node, parent, scope, file) {
|
||||
var objectRef;
|
||||
var getObjectRef = () => objectRef ||= scope.generateUidIdentifier("obj");
|
||||
|
||||
var propPaths = this.get("properties");
|
||||
for (var i = 0; i < node.properties.length; i++) {
|
||||
Property(node.properties[i], scope, getObjectRef, file);
|
||||
Property(propPaths[i], node.properties[i], scope, getObjectRef, file);
|
||||
}
|
||||
|
||||
if (objectRef) {
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user