Compare commits
347 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
20d19735fc | ||
|
|
b5b6bf4ad5 | ||
|
|
844c10cac0 | ||
|
|
43583e4e9d | ||
|
|
f5b921cda9 | ||
|
|
763892aa79 | ||
|
|
3e6eae4d1a | ||
|
|
7c090c8580 | ||
|
|
a5f6c1c389 | ||
|
|
c159f2d982 | ||
|
|
9205f10244 | ||
|
|
4cd7bcad59 | ||
|
|
7e9660efd3 | ||
|
|
2d66ce5224 | ||
|
|
1257b2cf40 | ||
|
|
f21d935de5 | ||
|
|
2e20364793 | ||
|
|
e47e8a187a | ||
|
|
26924d5944 | ||
|
|
5eb1850a55 | ||
|
|
333e287226 | ||
|
|
80a77bd6a2 | ||
|
|
c9286a1de1 | ||
|
|
52f614dcdf | ||
|
|
600367ae25 | ||
|
|
b761cba135 | ||
|
|
947d3e262d | ||
|
|
4061bea528 | ||
|
|
de195e5bfc | ||
|
|
3bcef86973 | ||
|
|
fa670ac71e | ||
|
|
572261f9ce | ||
|
|
8a320d53a5 | ||
|
|
0650eedeb6 | ||
|
|
2282d066a2 | ||
|
|
f4d7cc55c1 | ||
|
|
eaaa279aa5 | ||
|
|
0595e06e29 | ||
|
|
9b27a170ae | ||
|
|
1db232da9e | ||
|
|
4cc844f410 | ||
|
|
024ae670cb | ||
|
|
429edda9c0 | ||
|
|
05b13b9ea3 | ||
|
|
b7320ce400 | ||
|
|
0c37b7b973 | ||
|
|
bb36dbd8d9 | ||
|
|
2dd8c40618 | ||
|
|
33128b0ccf | ||
|
|
cf25424295 | ||
|
|
7492074794 | ||
|
|
c4a491123e | ||
|
|
55ad88fe4e | ||
|
|
7170dbced8 | ||
|
|
b0971412a2 | ||
|
|
a6b374a681 | ||
|
|
2d0355b3b9 | ||
|
|
7fade101be | ||
|
|
0918da8569 | ||
|
|
917db622c4 | ||
|
|
f7ee6fbd20 | ||
|
|
5899e9a0be | ||
|
|
d41cb11545 | ||
|
|
3ad909a4ae | ||
|
|
4bafdf733c | ||
|
|
b825998c63 | ||
|
|
6b02ca47c3 | ||
|
|
ea1b85bffa | ||
|
|
3cffe47eea | ||
|
|
e5d5a9fb27 | ||
|
|
ca97fa63a9 | ||
|
|
f4cc27bc0e | ||
|
|
8cea575e2e | ||
|
|
c91baee4d5 | ||
|
|
8055ce29f7 | ||
|
|
4596ae48b8 | ||
|
|
6c268cdf21 | ||
|
|
fce977f1d7 | ||
|
|
a298075949 | ||
|
|
66599c3779 | ||
|
|
60340244b1 | ||
|
|
eb72ea3e5a | ||
|
|
ede6237b6f | ||
|
|
e91e10aae6 | ||
|
|
9c3cca0d25 | ||
|
|
8eee5367f3 | ||
|
|
40d55a3d44 | ||
|
|
75330304dc | ||
|
|
776c508418 | ||
|
|
e804741632 | ||
|
|
3d3cb4be4f | ||
|
|
64f4209119 | ||
|
|
2ede226ef9 | ||
|
|
f5cf641c0a | ||
|
|
1abd3419f6 | ||
|
|
75699db716 | ||
|
|
7c3572f08c | ||
|
|
9dacde6d07 | ||
|
|
8c3aab9a26 | ||
|
|
ba4550c953 | ||
|
|
d0ac65a934 | ||
|
|
a4c70bb029 | ||
|
|
795cf0c0b1 | ||
|
|
e64b90e322 | ||
|
|
d69b0973e1 | ||
|
|
6296f49653 | ||
|
|
9f2b739046 | ||
|
|
da1d5e5577 | ||
|
|
7333b4e392 | ||
|
|
b0442d0784 | ||
|
|
295e69f8f8 | ||
|
|
cfe844fa39 | ||
|
|
0f4ea2d2a6 | ||
|
|
4b85b05839 | ||
|
|
2539d08dce | ||
|
|
c26fd7a819 | ||
|
|
2053610429 | ||
|
|
cd4f83b299 | ||
|
|
ec29ba19a9 | ||
|
|
9dc03e0978 | ||
|
|
bc4258eca9 | ||
|
|
b0e58f9770 | ||
|
|
a1e2641c91 | ||
|
|
9108422f99 | ||
|
|
9e8f4b25ca | ||
|
|
1cecd24823 | ||
|
|
97f6e1469b | ||
|
|
ec46eaf224 | ||
|
|
a102692103 | ||
|
|
0376ec8ff0 | ||
|
|
5932a07610 | ||
|
|
6110b0c0b3 | ||
|
|
365e221d95 | ||
|
|
acbc4859c0 | ||
|
|
55750e05e7 | ||
|
|
ba9c4db673 | ||
|
|
ba8a63a69e | ||
|
|
25581981b5 | ||
|
|
02a6feed73 | ||
|
|
f3acedbf08 | ||
|
|
35ab4ffaab | ||
|
|
ddfb7f2c11 | ||
|
|
e2ca03f92d | ||
|
|
c75a3c68e5 | ||
|
|
5d617ea99b | ||
|
|
58059a2c48 | ||
|
|
4c2936773a | ||
|
|
c7c16ad9c1 | ||
|
|
7b676d608b | ||
|
|
48ecec1e2e | ||
|
|
9dc72e71d4 | ||
|
|
4a7a02b9af | ||
|
|
7cb460d2f3 | ||
|
|
0a7dac8744 | ||
|
|
667d595a73 | ||
|
|
2d2d957be5 | ||
|
|
5aef7afade | ||
|
|
d5e7b5616c | ||
|
|
9aa17a6cc2 | ||
|
|
0f13097f59 | ||
|
|
355ffbdaf8 | ||
|
|
45b0d63d72 | ||
|
|
c515271fbd | ||
|
|
6f3501876a | ||
|
|
f8afc5fa17 | ||
|
|
4ecda93147 | ||
|
|
41847162b1 | ||
|
|
0f7e010e3e | ||
|
|
8201ea0aa2 | ||
|
|
a7848c1822 | ||
|
|
d4abaeeecf | ||
|
|
871a82df0a | ||
|
|
ffbf5b0b47 | ||
|
|
9dcceaeb40 | ||
|
|
556e58a4ab | ||
|
|
b6c35743e6 | ||
|
|
0abd34f7e6 | ||
|
|
cafd7f8e39 | ||
|
|
43c0a0e65f | ||
|
|
7b359f46fd | ||
|
|
880827f6ce | ||
|
|
0c300058a3 | ||
|
|
d35be22f7d | ||
|
|
ff8d90061f | ||
|
|
e66109f9d0 | ||
|
|
1934c16f0f | ||
|
|
012ac7d9ec | ||
|
|
efb3b04225 | ||
|
|
1488d88c81 | ||
|
|
80a7f13460 | ||
|
|
8919fc22b1 | ||
|
|
b1273cb774 | ||
|
|
0843b8355e | ||
|
|
6457677fb4 | ||
|
|
f8a860bf21 | ||
|
|
f2bed33db8 | ||
|
|
7d2c6525d8 | ||
|
|
de652dc747 | ||
|
|
9f1053212b | ||
|
|
76690a3deb | ||
|
|
50f2f2fc98 | ||
|
|
5c27234d19 | ||
|
|
6f5d16f397 | ||
|
|
e42a43e3d1 | ||
|
|
bfd307cd57 | ||
|
|
beb5ee1333 | ||
|
|
cde871555d | ||
|
|
51b0ffa7bf | ||
|
|
255c819727 | ||
|
|
152ccb2ce8 | ||
|
|
bdb3adfeea | ||
|
|
236086bee5 | ||
|
|
1436753e6d | ||
|
|
eadf8ef799 | ||
|
|
f1e20cf6ff | ||
|
|
82a0851526 | ||
|
|
07a992e6d3 | ||
|
|
f642c2baa8 | ||
|
|
2f7743cafb | ||
|
|
44112f2d87 | ||
|
|
99c4810770 | ||
|
|
9f2ac15695 | ||
|
|
19c1ee157f | ||
|
|
973270772b | ||
|
|
7334326042 | ||
|
|
4043e8e8b9 | ||
|
|
7fd403998c | ||
|
|
1f7fcc2985 | ||
|
|
ba19bd36a4 | ||
|
|
e119681b1f | ||
|
|
fbe22e53a9 | ||
|
|
79e826fcc8 | ||
|
|
cf940268b9 | ||
|
|
96bb572307 | ||
|
|
5dafe0b66a | ||
|
|
29e24a4cbe | ||
|
|
f979d55b71 | ||
|
|
164b2116a3 | ||
|
|
55e623a099 | ||
|
|
a60b4b1302 | ||
|
|
bf23e59bbf | ||
|
|
eb4cee89c5 | ||
|
|
fb94a87a4d | ||
|
|
9613c7fdb6 | ||
|
|
d8c97fba15 | ||
|
|
0f663560d4 | ||
|
|
4901075960 | ||
|
|
4aec242979 | ||
|
|
d1d95e0e49 | ||
|
|
d3c643b004 | ||
|
|
98eb30c482 | ||
|
|
2d43ffb5b4 | ||
|
|
a195701de2 | ||
|
|
f639f4bea7 | ||
|
|
334cbb06c6 | ||
|
|
f4a3b2f01a | ||
|
|
dbe6f1b9a9 | ||
|
|
73ada57a17 | ||
|
|
8e2b743f7e | ||
|
|
a533042503 | ||
|
|
49953e3464 | ||
|
|
0bf95d6aea | ||
|
|
7d88a1ca0b | ||
|
|
ec74eb41cf | ||
|
|
3139482358 | ||
|
|
32f19aff99 | ||
|
|
23bead9226 | ||
|
|
bd3812c255 | ||
|
|
e498185a76 | ||
|
|
167eda4750 | ||
|
|
3b52ddce6d | ||
|
|
0e771c5d38 | ||
|
|
2ec1544f2d | ||
|
|
049ef430b2 | ||
|
|
34eb2babdb | ||
|
|
8706754550 | ||
|
|
c7b507e119 | ||
|
|
221c632c05 | ||
|
|
7dbde208ef | ||
|
|
52a2e3e17c | ||
|
|
fa22d7dca0 | ||
|
|
9fcdebde08 | ||
|
|
92157161f0 | ||
|
|
82b5479436 | ||
|
|
91a037af55 | ||
|
|
067fccc2c9 | ||
|
|
348c0d2542 | ||
|
|
f2f6bbb02c | ||
|
|
16f7b967b5 | ||
|
|
270a8be68d | ||
|
|
82254d9d9b | ||
|
|
759a265fb0 | ||
|
|
ee46f45c90 | ||
|
|
c45ce61550 | ||
|
|
b36cf11ac8 | ||
|
|
dca0f72e4d | ||
|
|
68a99ed1e9 | ||
|
|
af4feb4d88 | ||
|
|
de1e965fec | ||
|
|
430c5df0e7 | ||
|
|
9b12f799f7 | ||
|
|
f3a9c6ee5d | ||
|
|
e0aceeafef | ||
|
|
7643d86047 | ||
|
|
98af01a73c | ||
|
|
e280a810c3 | ||
|
|
7a59575d1e | ||
|
|
b01d8448a0 | ||
|
|
9b3c8569f7 | ||
|
|
12104f822f | ||
|
|
b4cd2df745 | ||
|
|
887ffed84c | ||
|
|
9be3d9c8e1 | ||
|
|
9d3dff3b7a | ||
|
|
55114ec631 | ||
|
|
9c3d00d3c3 | ||
|
|
90b8826e73 | ||
|
|
65f39bbf6f | ||
|
|
e9eced846f | ||
|
|
a0befe349d | ||
|
|
04992effb3 | ||
|
|
9ae54d2a50 | ||
|
|
fe72a40159 | ||
|
|
ea510d09d0 | ||
|
|
375689a1ff | ||
|
|
9908e331b7 | ||
|
|
cd8655708d | ||
|
|
62b94f297a | ||
|
|
01d399ee3c | ||
|
|
30e3908484 | ||
|
|
ff12046009 | ||
|
|
1a058b4a6e | ||
|
|
02dfd18023 | ||
|
|
9ab7df5f47 | ||
|
|
7a5b140f92 | ||
|
|
6f83111c55 | ||
|
|
f3f60368da | ||
|
|
7a09640b20 | ||
|
|
2916d1262b | ||
|
|
741abb73d2 | ||
|
|
2e46755260 | ||
|
|
2cff9519e1 | ||
|
|
f9c2c6e988 | ||
|
|
5ae3dc01f1 | ||
|
|
af4fd69dd0 | ||
|
|
4d4493f325 | ||
|
|
e70d474b39 |
@@ -3,18 +3,21 @@
|
||||
"rules": {
|
||||
"strict": 0,
|
||||
"no-underscore-dangle": 0,
|
||||
"no-unused-vars": 0,
|
||||
"curly": 0,
|
||||
"no-multi-spaces": 0,
|
||||
"key-spacing": 0,
|
||||
"no-return-assign": 0,
|
||||
"consistent-return": 0,
|
||||
"no-shadow": 0,
|
||||
"no-comma-dangle": 0,
|
||||
"comma-dangle": 0,
|
||||
"no-use-before-define": 0,
|
||||
"no-empty": 0,
|
||||
"new-parens": 0,
|
||||
"no-cond-assign": 0
|
||||
"no-cond-assign": 0,
|
||||
"no-fallthrough": 0,
|
||||
"new-cap": 0,
|
||||
"no-loop-func": 0,
|
||||
"no-unreachable": 0
|
||||
},
|
||||
"env": {
|
||||
"node": true
|
||||
|
||||
14
.gitignore
vendored
14
.gitignore
vendored
@@ -8,10 +8,10 @@ test/core/tmp
|
||||
/browser.js
|
||||
/browser-polyfill.js
|
||||
/runtime.js
|
||||
coverage
|
||||
dist
|
||||
.package.json
|
||||
packages/babel-runtime/core-js
|
||||
packages/babel-runtime/helpers/*.js
|
||||
packages/babel-runtime/regenerator/*.js
|
||||
lib
|
||||
/coverage
|
||||
/dist
|
||||
/.package.json
|
||||
/packages/babel-runtime/core-js
|
||||
/packages/babel-runtime/helpers/*.js
|
||||
/packages/babel-runtime/regenerator/*.js
|
||||
/lib
|
||||
|
||||
@@ -11,9 +11,4 @@ before_script: "npm install -g codeclimate-test-reporter"
|
||||
script: "make test-travis"
|
||||
|
||||
notifications:
|
||||
webhooks:
|
||||
urls:
|
||||
- https://webhooks.gitter.im/e/acf1870e9d223c65e8d5
|
||||
on_success: always
|
||||
on_failure: always
|
||||
on_start: false
|
||||
slack: babeljs:5Wy4QX13KVkGy9CnU0rmvgeK
|
||||
|
||||
111
CHANGELOG.md
111
CHANGELOG.md
@@ -13,12 +13,121 @@ _Note: Gaps between patch versions are faulty/broken releases._
|
||||
|
||||
See [CHANGELOG - 6to5](CHANGELOG-6to5.md) for the pre-4.0.0 version changelog.
|
||||
|
||||
## 5.5.6
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix `let` binding collision in loop head not properly replacing `AssignmentExpression`s.
|
||||
|
||||
## 5.5.5
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix `file.opts` not being set before `file.log.deprecate` was called causing a `ReferenceError` as it was checking for a property on it.
|
||||
|
||||
## 5.5.4
|
||||
|
||||
* **Bug Fix**
|
||||
* Add back missing `shouldIgnore` check.
|
||||
* Log message on deprecated options rather than throw an error.
|
||||
* Fix name of `auxiliaryComment` option when attempting Istanbul interop in `babel/register`.
|
||||
|
||||
## 5.5.3
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix weird state bug when traversing overa `node` `ClassProperty` instead of `path` in the `es6.classes` transformer.
|
||||
|
||||
## 5.5.2
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix `NodePath#isPure` on `Property` nodes.
|
||||
* Use cwd instead of entry file directory when working out relative directory for `babel/register`.
|
||||
* **Internal**
|
||||
* Add scary warning for those few who choose to use the WIP experimental transformers.
|
||||
|
||||
## 5.5.1
|
||||
|
||||
* **Bug Fix**
|
||||
* Remove `ClassProperty` nodes always in the `Flow` transformer. This is fine now since class properties aren't supported in any engine that supports classes but the `es7.classProperties` transformer will need to be updated in the future to desugar to ES6 classes instead of relying on the `es6.classes` transformer from being ran.
|
||||
|
||||
## 5.5.0
|
||||
|
||||
* **Bug Fix**
|
||||
* Allow pushing declarations to `SwitchStatement`s.
|
||||
* Fix `minification.removeDebugger` to remove `DebuggerStatement`s rather than `ExpressionStatement`s with an identifier of `debugger`.
|
||||
* Check LHS in `ForInStatement` and `ForOfStatement` for constant violations.
|
||||
* Register function `id` as a reference when naming methods to avoid collisions.
|
||||
* Support key literals when checking for the existence of `displayName` property when attempting to add it for `React.createClass`.
|
||||
* Remove `ExportDefaultSpecifier` check from `t.isDefaultSpecifier`.
|
||||
* Don't consider `JSXIdentifier` HTML tag identifiers to be references.
|
||||
* **Polish**
|
||||
* Update `minification.deadCodeElimination` transformer to remove all statements after completion statements.
|
||||
* Update `minification.deadCodeElimination` transformer to not inline single used bindings that exist in different scopes.
|
||||
* When performing Istanbul interop in `babel/register`, add the auxiliary comment `"istanbul ignore text"` to get more accurate coverage.
|
||||
* Add `--nolazy` argument to `babel-node`.
|
||||
* Add support for `cluster` forking.
|
||||
* Perform scope tracking in a single pass instead of multiple.
|
||||
* Smarten up type inferrence and resolution to support the whole array of language constructs.
|
||||
* Optimise module metadata retrieval into a single pass.
|
||||
* Ignore trailing commas when inferring newlines.
|
||||
* Rename `minification.inlineExpressions` transformer to `minification.constantFolding`.
|
||||
* Check path relative to entry file when checking to see if we're inside `node_modules` when using `babel/register`.
|
||||
* Upgrade `regenerator`.
|
||||
|
||||
## 5.4.7
|
||||
|
||||
* **Bug Fix**
|
||||
* Don't consider `JSXAttribute` `names` to be valid `ReferencedIdentifier`s.
|
||||
|
||||
## 5.4.6
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix `spec.functionName` transformer incorrectly attempting to rename a binding that doesn't exist as it's a global.
|
||||
* **Internal**
|
||||
* Deprecate custom module formatters.
|
||||
|
||||
## 5.4.5
|
||||
|
||||
* **Bug Fix**
|
||||
* Add `JSXIdentifier` as a valid `ReferencedIdentifier` visitor virtual type.
|
||||
* Ignore `CallExpression` `_prettyCall` when the `retainLines` option is enabled.
|
||||
* Inherit comments to new declaration node when exploding module declarations.
|
||||
* Fix `es6.tailCall` transformer failing on calls that exceed the max parameters of the function.
|
||||
|
||||
## 5.4.4
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix bug where replacing variable declarations in the head of a `for` loop would turn them into `ExpressionStatement`s.
|
||||
* Fix renaming of assignment expressions that were non-identifiers ie. patterns.
|
||||
* Force space before `class` `id` to avoid breaking named classes when using `compact` mode.
|
||||
* Add assignment pattern explosion to avoid initial duplicate nodes.
|
||||
* Ignore this and arguments when performing TCO on shadowed functions.
|
||||
* **Polish**
|
||||
* Rename `sourceMapName` option to `sourceMapTarget`. Thanks [@getify](https://github.com/getify)!
|
||||
* Better detection of completion records, ignore those in `Function`s.
|
||||
* Clarified descriptions of the options that are enabled by default.
|
||||
* Resolve `\`babel-plugin-${name}\`` plugin names **before** just checking the `name`. Thanks [@jquense](https://github.com/jquense)!
|
||||
* Update AMD module formatter to add import default remapping.
|
||||
|
||||
## 5.4.3
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix `module` being incorrectly rewritten when used as in an export declaration.
|
||||
* When performing single-reference inlining, ensure that the single reference isn't a child of the binding itself.
|
||||
* Fix a bug in `minification.deadCodeElimination` where a new binding instance was being created for local class bindings instead of just inheriting the parent one.
|
||||
* Fix bug with paren printing in `compact` and `retainLines` mode where a left paren was already printed before catching up.
|
||||
* **Internal**
|
||||
* Handle contexts for paths much better. This will ensure that the path node location info is in sync.
|
||||
|
||||
## 5.4.2
|
||||
|
||||
* **Polish**
|
||||
* `ignore` and `only` patterns are now **very** liberal. The pattern can now exist anywhere in the path.
|
||||
|
||||
## 5.4.1
|
||||
|
||||
* **Bug Fix**
|
||||
* Add missing `slash` dependency. Thanks [@browncolyn](https://github.com/browncolyn)!
|
||||
* **Polish**
|
||||
* Clean up `shouldIgnore` algorithm to work how you'd expect rather than being a hacky piece of shit.
|
||||
* Clean up `shouldIgnore` algorithm to work how you'd expect rather than being a hacky piece of shit. It now crawls the entire path, checking each section of it against the input ignore/only patterns. This means that the pattern `foo` will ignore the paths `foo/bar.js`, `bar/foo` etc.
|
||||
|
||||
## 5.4.0
|
||||
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
# NOTE: BEFORE OPENING AN ISSUE PLEASE SEE THE [README](https://github.com/babel/babel#readme).
|
||||
|
||||
----
|
||||
|
||||
<p align="center">
|
||||
<strong><a href="#setup">Setup</a></strong>
|
||||
|
|
||||
@@ -8,6 +12,8 @@
|
||||
<strong><a href="#dependencies">Dependencies</a></strong>
|
||||
|
|
||||
<strong><a href="#code-standards">Code Standards</a></strong>
|
||||
|
|
||||
<strong><a href="#internals">Internals</a></strong>
|
||||
</p>
|
||||
|
||||
----
|
||||
@@ -21,6 +27,9 @@ contributing, please read the
|
||||
|
||||
## Developing
|
||||
|
||||
>Note: Babel moves fast. Only the latest release is guaranteed to build correctly.
|
||||
>Older releases are not officially supported. If you attempt to build them, do that at your own risk.
|
||||
|
||||
#### Setup
|
||||
|
||||
```sh
|
||||
@@ -79,7 +88,6 @@ your [`$PATH`](http://unix.stackexchange.com/questions/26047/how-to-correctly-ad
|
||||
* 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.
|
||||
@@ -167,3 +175,6 @@ your [`$PATH`](http://unix.stackexchange.com/questions/26047/how-to-correctly-ad
|
||||
* **Declaration**
|
||||
* No unused variables
|
||||
* No pollution of global variables and prototypes
|
||||
|
||||
#### Internals
|
||||
Please see [`/doc`](/doc) for internals documentation relevant to developing babel.
|
||||
|
||||
18
Makefile
18
Makefile
@@ -5,6 +5,7 @@ UGLIFY_CMD = node_modules/uglify-js/bin/uglifyjs
|
||||
#UGLIFY_CMD = node_modules/uglify-js/bin/uglifyjs --mangle sort
|
||||
MOCHA_CMD = node_modules/mocha/bin/_mocha
|
||||
BABEL_CMD = node_modules/babel/bin/babel
|
||||
BROWSERIFY_IGNORE = -i esprima-fb
|
||||
|
||||
export NODE_ENV = test
|
||||
|
||||
@@ -22,6 +23,9 @@ watch-core: clean-core
|
||||
clean-core:
|
||||
rm -rf lib
|
||||
|
||||
lint:
|
||||
eslint src/babel
|
||||
|
||||
build:
|
||||
mkdir -p dist
|
||||
make build-core
|
||||
@@ -31,8 +35,10 @@ build:
|
||||
node $(BROWSERIFY_CMD) -e lib/babel/polyfill.js >dist/polyfill.js
|
||||
node $(UGLIFY_CMD) dist/polyfill.js >dist/polyfill.min.js
|
||||
|
||||
node $(BROWSERIFY_CMD) lib/babel/api/browser.js -s babel >dist/babel.js
|
||||
node $(UGLIFY_CMD) dist/babel.js >dist/babel.min.js
|
||||
node $(BROWSERIFY_CMD) lib/babel/api/browser.js -s babel $(BROWSERIFY_IGNORE) >dist/browser.js
|
||||
node $(UGLIFY_CMD) dist/browser.js >dist/browser.min.js
|
||||
|
||||
node $(BROWSERIFY_CMD) lib/babel/api/node.js --node $(BROWSERIFY_IGNORE) >dist/node.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
|
||||
@@ -61,7 +67,7 @@ test-cov:
|
||||
test-parser:
|
||||
node test/acorn/run.js
|
||||
|
||||
test-travis: bootstrap build test
|
||||
test-travis: bootstrap lint build test
|
||||
|
||||
test-browser:
|
||||
mkdir -p dist
|
||||
@@ -73,7 +79,7 @@ test-browser:
|
||||
|
||||
test -n "`which open`" && open test/browser.html
|
||||
|
||||
publish:
|
||||
publish: lint
|
||||
git pull --rebase
|
||||
|
||||
make test
|
||||
@@ -83,8 +89,8 @@ publish:
|
||||
|
||||
make build
|
||||
|
||||
cp dist/babel.js browser.js
|
||||
cp dist/babel.min.js browser.min.js
|
||||
cp dist/browser.js browser.js
|
||||
cp dist/browser.min.js browser.min.js
|
||||
|
||||
cp dist/polyfill.js browser-polyfill.js
|
||||
cp dist/polyfill.min.js browser-polyfill.min.js
|
||||
|
||||
@@ -9,7 +9,11 @@
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
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.
|
||||
For questions and support please visit the <a href="https://babel-slack.herokuapp.com">slack channel</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">
|
||||
Issues without instructions to reproduce <strong>will be immediately closed<strong>.
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
|
||||
4
doc/index.md
Normal file
4
doc/index.md
Normal file
@@ -0,0 +1,4 @@
|
||||
This is a collection of documentation about babel internals, for use in development of babel.
|
||||
|
||||
# [Properties of nodes](/doc/node-props.md)
|
||||
These are properties babel stores in AST node objects for internal use, as opposed to properties that are part of the AST spec (ESTree at the time of this writing).
|
||||
11
doc/node-props.md
Normal file
11
doc/node-props.md
Normal file
@@ -0,0 +1,11 @@
|
||||
# Properties of nodes
|
||||
These are properties babel stores in AST node objects for internal use, as opposed to properties that are part of the AST spec (ESTree at the time of this writing).
|
||||
|
||||
## `_blockHoist`
|
||||
`node._blockHoist != null` triggers the [block-hoist transformer](/src/babel/transformation/transformers/internal/block-hoist.js). Value should be `true` or an integer in the range `0..3`. `true` is equivalent to `2`. The value indicates whether the node should be hoisted and to what degree. See the source code for more detailed information.
|
||||
|
||||
## `_paths`
|
||||
Stores a representation of a node's position in the tree and relationship to other nodes.
|
||||
|
||||
## `shadow`
|
||||
A truthy value on a function node triggers the [shadow-functions transformer](/src/babel/transformation/transformers/internal/shadow-functions.js), which transforms the node so that it references (or inherits) `arguments` and `this` from the parent scope. It is invoked for arrow functions, for example.
|
||||
14
package.json
14
package.json
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "babel-core",
|
||||
"description": "Turn ES6 code into readable vanilla ES5 with source maps",
|
||||
"version": "5.4.1",
|
||||
"description": "A compiler for writing next generation JavaScript",
|
||||
"version": "5.5.7",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>",
|
||||
"homepage": "https://babeljs.io/",
|
||||
"license": "MIT",
|
||||
@@ -41,6 +41,7 @@
|
||||
"esutils": "^2.0.0",
|
||||
"fs-readdir-recursive": "^0.1.0",
|
||||
"globals": "^6.4.0",
|
||||
"home-or-tmp": "^1.0.0",
|
||||
"is-integer": "^1.0.4",
|
||||
"js-tokens": "1.0.0",
|
||||
"leven": "^1.0.1",
|
||||
@@ -50,7 +51,7 @@
|
||||
"output-file-sync": "^1.1.0",
|
||||
"path-is-absolute": "^1.0.0",
|
||||
"private": "^0.1.6",
|
||||
"regenerator": "^0.8.20",
|
||||
"regenerator": "0.8.28",
|
||||
"regexpu": "^1.1.2",
|
||||
"repeating": "^1.1.2",
|
||||
"resolve": "^1.1.6",
|
||||
@@ -60,15 +61,14 @@
|
||||
"source-map-support": "^0.2.10",
|
||||
"strip-json-comments": "^1.0.2",
|
||||
"to-fast-properties": "^1.0.0",
|
||||
"trim-right": "^1.0.0",
|
||||
"user-home": "^1.1.1"
|
||||
"trim-right": "^1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel": "5.3.1",
|
||||
"browserify": "^9.0.8",
|
||||
"chai": "^2.2.0",
|
||||
"eslint": "^0.18.0",
|
||||
"babel-eslint": "^2.0.0",
|
||||
"eslint": "^0.21.2",
|
||||
"babel-eslint": "^3.1.9",
|
||||
"esvalid": "^1.1.0",
|
||||
"istanbul": "^0.3.5",
|
||||
"matcha": "^0.6.0",
|
||||
|
||||
@@ -107,6 +107,7 @@ if (program.eval || program.print) {
|
||||
|
||||
// add back on node and concat the sliced args
|
||||
process.argv = ["node"].concat(args);
|
||||
process.execArgv.unshift(__filename);
|
||||
|
||||
Module.runMain();
|
||||
} else {
|
||||
|
||||
@@ -412,6 +412,7 @@ babelArgs.forEach(function(arg){
|
||||
case "--prof_auto":
|
||||
case "--prof_lazy":
|
||||
case "--sliding_state_window":
|
||||
case "--nolazy":
|
||||
args.unshift(arg);
|
||||
break;
|
||||
|
||||
|
||||
@@ -14,7 +14,8 @@ module.exports = function (commander, filenames, opts) {
|
||||
var dest = path.join(commander.outDir, relative);
|
||||
|
||||
var data = util.compile(src, {
|
||||
sourceFileName: slash(path.relative(dest + "/..", src))
|
||||
sourceFileName: slash(path.relative(dest + "/..", src)),
|
||||
sourceMapTarget: path.basename(relative)
|
||||
});
|
||||
if (data.ignored) return;
|
||||
|
||||
|
||||
@@ -16,7 +16,8 @@ module.exports = function (commander, filenames, opts) {
|
||||
|
||||
var buildResult = function () {
|
||||
var map = new sourceMap.SourceMapGenerator({
|
||||
file: slash(commander.outFile || "stdout")
|
||||
file: path.basename(commander.outFile) || "stdout",
|
||||
sourceRoot: opts.sourceRoot
|
||||
});
|
||||
|
||||
var code = "";
|
||||
|
||||
@@ -35,7 +35,7 @@ each(options, function (option, key) {
|
||||
if (option.description) desc.push(option.description);
|
||||
|
||||
commander.option(arg, desc.join(" "));
|
||||
})
|
||||
});
|
||||
|
||||
commander.option("-x, --extensions [extensions]", "List of extensions to compile when a directory has been input [.es6,.js,.es,.jsx]");
|
||||
commander.option("-w, --watch", "Recompile files on changes");
|
||||
@@ -51,7 +51,7 @@ commander.on("--help", function () {
|
||||
each(keys(obj).sort(), function (key) {
|
||||
if (key[0] === "_") return;
|
||||
|
||||
if (obj[key].optional) key = "[" + key + "]";
|
||||
if (obj[key].metadata && obj[key].metadata.optional) key = "[" + key + "]";
|
||||
|
||||
console.log(" - " + key);
|
||||
});
|
||||
@@ -79,7 +79,9 @@ if (commander.extensions) {
|
||||
var errors = [];
|
||||
|
||||
var filenames = commander.args.reduce(function (globbed, input) {
|
||||
return globbed.concat(glob.sync(input));
|
||||
var files = glob.sync(input);
|
||||
if (!files.length) files = [input];
|
||||
return globbed.concat(files);
|
||||
}, []);
|
||||
|
||||
each(filenames, function (filename) {
|
||||
@@ -116,11 +118,16 @@ if (errors.length) {
|
||||
var opts = exports.opts = {};
|
||||
|
||||
each(options, function (opt, key) {
|
||||
opts[key] = commander[key];
|
||||
if (commander[key] !== undefined) {
|
||||
opts[key] = commander[key];
|
||||
}
|
||||
});
|
||||
|
||||
opts.ignore = util.arrayify(opts.ignore, util.regexify);
|
||||
opts.only = util.arrayify(opts.only, util.regexify);
|
||||
|
||||
if (opts.only) {
|
||||
opts.only = util.arrayify(opts.only, util.regexify);
|
||||
}
|
||||
|
||||
var fn;
|
||||
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
{
|
||||
"name": "babel",
|
||||
"description": "Turn ES6 code into readable vanilla ES5 with source maps",
|
||||
"version": "5.4.0",
|
||||
"version": "5.5.6",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>",
|
||||
"homepage": "https://babeljs.io/",
|
||||
"license": "MIT",
|
||||
"repository": "babel/babel",
|
||||
"preferGlobal": true,
|
||||
"dependencies": {
|
||||
"babel-core": "^5.4.0",
|
||||
"babel-core": "^5.5.6",
|
||||
"chokidar": "^1.0.0",
|
||||
"commander": "^2.6.0",
|
||||
"convert-source-map": "^1.1.0",
|
||||
@@ -25,4 +25,4 @@
|
||||
"babel-node": "./bin/babel-node",
|
||||
"babel-external-helpers": "./bin/babel-external-helpers"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "babel-runtime",
|
||||
"description": "babel selfContained runtime",
|
||||
"version": "5.4.0",
|
||||
"version": "5.5.6",
|
||||
"license": "MIT",
|
||||
"repository": "babel/babel",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>",
|
||||
|
||||
@@ -415,6 +415,9 @@ pp.flow_identToTypeAnnotation = function (start, node, id) {
|
||||
case "boolean":
|
||||
return this.finishNode(node, "BooleanTypeAnnotation")
|
||||
|
||||
case "mixed":
|
||||
return this.finishNode(node, "MixedTypeAnnotation")
|
||||
|
||||
case "number":
|
||||
return this.finishNode(node, "NumberTypeAnnotation")
|
||||
|
||||
|
||||
@@ -274,7 +274,13 @@ pp.parseExprAtom = function(refShorthandDefaultPos) {
|
||||
if (this.options.features["es7.doExpressions"]) {
|
||||
let node = this.startNode()
|
||||
this.next()
|
||||
var oldInFunction = this.inFunction
|
||||
var oldLabels = this.labels
|
||||
this.labels = []
|
||||
this.inFunction = false
|
||||
node.body = this.parseBlock()
|
||||
this.inFunction = oldInFunction
|
||||
this.labels = oldLabels
|
||||
return this.finishNode(node, "DoExpression")
|
||||
}
|
||||
|
||||
@@ -286,7 +292,7 @@ pp.parseExprAtom = function(refShorthandDefaultPos) {
|
||||
//
|
||||
if (this.options.features["es7.asyncFunctions"]) {
|
||||
// async functions!
|
||||
if (id.name === "async") {
|
||||
if (id.name === "async" && !this.canInsertSemicolon()) {
|
||||
// arrow functions
|
||||
if (this.type === tt.parenL) {
|
||||
let expr = this.parseParenAndDistinguishExpression(start, true, true)
|
||||
@@ -507,7 +513,7 @@ pp.parseNew = function() {
|
||||
pp.parseTemplateElement = function() {
|
||||
let elem = this.startNode()
|
||||
elem.value = {
|
||||
raw: this.input.slice(this.start, this.end),
|
||||
raw: this.input.slice(this.start, this.end).replace(/\r\n?/g, '\n'),
|
||||
cooked: this.value
|
||||
}
|
||||
this.next()
|
||||
|
||||
@@ -9,6 +9,7 @@ var STATE_KEYS = [
|
||||
"lastTokEnd",
|
||||
"lineStart",
|
||||
"startLoc",
|
||||
"curLine",
|
||||
"endLoc",
|
||||
"start",
|
||||
"pos",
|
||||
|
||||
@@ -83,10 +83,13 @@ pp.parseStatement = function(declaration, topLevel) {
|
||||
return starttype === tt._import ? this.parseImport(node) : this.parseExport(node)
|
||||
|
||||
case tt.name:
|
||||
if (this.options.features["es7.asyncFunctions"] && this.value === "async" && this.lookahead().type === tt._function) {
|
||||
this.next();
|
||||
this.expect(tt._function);
|
||||
return this.parseFunction(node, true, false, true);
|
||||
if (this.options.features["es7.asyncFunctions"] && this.value === "async") {
|
||||
var lookahead = this.lookahead();
|
||||
if (lookahead.type === tt._function && !this.canInsertSemicolon.call(lookahead)) {
|
||||
this.next();
|
||||
this.expect(tt._function);
|
||||
return this.parseFunction(node, true, false, true);
|
||||
}
|
||||
}
|
||||
|
||||
// If the statement does not start with a statement keyword or a
|
||||
@@ -168,12 +171,6 @@ pp.parseDoStatement = function(node) {
|
||||
this.labels.push(loopLabel)
|
||||
node.body = this.parseStatement(false)
|
||||
this.labels.pop()
|
||||
if (this.options.features["es7.doExpressions"] && this.type !== tt._while) {
|
||||
let container = this.startNodeAt(start)
|
||||
container.expression = this.finishNode(node, "DoExpression")
|
||||
this.semicolon()
|
||||
return this.finishNode(container, "ExpressionStatement")
|
||||
}
|
||||
this.expect(tt._while)
|
||||
node.test = this.parseParenExpression()
|
||||
if (this.options.ecmaVersion >= 6)
|
||||
@@ -574,7 +571,7 @@ pp.parseExport = function(node) {
|
||||
this.parseExportFrom(node)
|
||||
return this.finishNode(node, "ExportAllDeclaration")
|
||||
}
|
||||
} else if (this.isExportDefaultSpecifier()) {
|
||||
} else if (this.options.features["es7.exportExtensions"] && this.isExportDefaultSpecifier()) {
|
||||
let specifier = this.startNode()
|
||||
specifier.exported = this.parseIdent(true)
|
||||
node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")]
|
||||
|
||||
@@ -577,11 +577,15 @@ pp.readTmplToken = function() {
|
||||
} else if (isNewLine(ch)) {
|
||||
out += this.input.slice(chunkStart, this.pos)
|
||||
++this.pos
|
||||
if (ch === 13 && this.input.charCodeAt(this.pos) === 10) {
|
||||
++this.pos
|
||||
out += "\n"
|
||||
} else {
|
||||
out += String.fromCharCode(ch)
|
||||
switch (ch) {
|
||||
case 13:
|
||||
if (this.input.charCodeAt(this.pos) === 10) ++this.pos;
|
||||
case 10:
|
||||
out += "\n";
|
||||
break;
|
||||
default:
|
||||
out += String.fromCharCode(ch);
|
||||
break;
|
||||
}
|
||||
if (this.options.locations) {
|
||||
++this.curLine
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
/* eslint no-new-func: 0 */
|
||||
|
||||
require("./node");
|
||||
var transform = module.exports = require("../transformation");
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ export { util, acorn, transform };
|
||||
export { pipeline } from "../transformation";
|
||||
export { canCompile } from "../util";
|
||||
|
||||
export { default as options } from "../transformation/file/options";
|
||||
export { default as options } from "../transformation/file/options/config";
|
||||
export { default as Transformer } from "../transformation/transformer";
|
||||
export { default as TransformerPipeline } from "../transformation/transformer-pipeline";
|
||||
export { default as traverse } from "../traversal";
|
||||
@@ -61,8 +61,8 @@ export function parse(code, opts = {}) {
|
||||
opts.sourceType = "module";
|
||||
opts.ecmaVersion = Infinity;
|
||||
opts.plugins = {
|
||||
flow: true,
|
||||
jsx: true
|
||||
jsx: true,
|
||||
flow: true
|
||||
};
|
||||
opts.features = {};
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// required to safely use babel/register within a browserify codebase
|
||||
|
||||
export default function () {};
|
||||
export default function () {}
|
||||
|
||||
import "../../polyfill";
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import path from "path";
|
||||
import os from "os";
|
||||
import fs from "fs";
|
||||
import userHome from "user-home";
|
||||
import homeOrTmp from "home-or-tmp";
|
||||
|
||||
const FILENAME = process.env.BABEL_CACHE_PATH || path.join(userHome || os.tmpdir(), ".babel.json");
|
||||
const FILENAME = process.env.BABEL_CACHE_PATH || path.join(homeOrTmp, ".babel.json");
|
||||
var data = {};
|
||||
|
||||
export function save() {
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import sourceMapSupport from "source-map-support";
|
||||
import * as registerCache from "./cache";
|
||||
import resolveRc from "../../tools/resolve-rc";
|
||||
import resolveRc from "../../transformation/file/options/resolve-rc";
|
||||
import extend from "lodash/object/extend";
|
||||
import * as babel from "../node";
|
||||
import each from "lodash/collection/each";
|
||||
import * as util from "../../util";
|
||||
import fs from "fs";
|
||||
import slash from "slash";
|
||||
import path from "path";
|
||||
|
||||
sourceMapSupport.install({
|
||||
handleUncaughtExceptions: false,
|
||||
@@ -38,14 +38,20 @@ var only;
|
||||
var oldHandlers = {};
|
||||
var maps = {};
|
||||
|
||||
var cwd = process.cwd();
|
||||
|
||||
var getRelativePath = function (filename){
|
||||
return path.relative(cwd, filename);
|
||||
};
|
||||
|
||||
var mtime = function (filename) {
|
||||
return +fs.statSync(filename).mtime;
|
||||
};
|
||||
|
||||
var compile = function (filename) {
|
||||
var compile = function (filename, opts = {}) {
|
||||
var result;
|
||||
|
||||
var opts = extend({}, transformOpts);
|
||||
opts = extend(opts, transformOpts);
|
||||
|
||||
// this will be done when the file is transformed anyway but we need all
|
||||
// the options so we can generate the cache key
|
||||
@@ -53,6 +59,9 @@ var compile = function (filename) {
|
||||
|
||||
var cacheKey = `${filename}:${JSON.stringify(opts)}:${babel.version}`;
|
||||
|
||||
var env = process.env.BABEL_ENV || process.env.NODE_ENV;
|
||||
if (env) cacheKey += `:${env}`;
|
||||
|
||||
if (cache) {
|
||||
var cached = cache[cacheKey];
|
||||
if (cached && cached.mtime === mtime(filename)) {
|
||||
@@ -79,9 +88,9 @@ var compile = function (filename) {
|
||||
|
||||
var shouldIgnore = function (filename) {
|
||||
if (!ignore && !only) {
|
||||
return /node_modules/.test(filename);
|
||||
return getRelativePath(filename).split(path.sep).indexOf("node_modules") >= 0;
|
||||
} else {
|
||||
return util.shouldIgnore(filename, ignore || [], only || []);
|
||||
return util.shouldIgnore(filename, ignore || [], only);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -95,7 +104,9 @@ if (process.env.running_under_istanbul) {
|
||||
fs.readFileSync = function (filename) {
|
||||
if (istanbulMonkey[filename]) {
|
||||
delete istanbulMonkey[filename];
|
||||
var code = compile(filename);
|
||||
var code = compile(filename, {
|
||||
auxiliaryComment: "istanbul ignore next"
|
||||
});
|
||||
istanbulMonkey[filename] = true;
|
||||
return code;
|
||||
} else {
|
||||
@@ -161,4 +172,4 @@ export default function (opts = {}) {
|
||||
delete opts.only;
|
||||
|
||||
extend(transformOpts, opts);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -117,8 +117,9 @@ export default class Buffer {
|
||||
|
||||
_removeSpacesAfterLastNewline() {
|
||||
var lastNewlineIndex = this.buf.lastIndexOf("\n");
|
||||
if (lastNewlineIndex === -1)
|
||||
if (lastNewlineIndex === -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
var index = this.buf.length - 1;
|
||||
while (index > lastNewlineIndex) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export function File(node, print) {
|
||||
print(node.program);
|
||||
print.plain(node.program);
|
||||
}
|
||||
|
||||
export function Program(node, print) {
|
||||
@@ -17,3 +17,7 @@ export function BlockStatement(node, print) {
|
||||
this.rightBrace();
|
||||
}
|
||||
}
|
||||
|
||||
export function Noop() {
|
||||
|
||||
}
|
||||
|
||||
@@ -3,16 +3,16 @@ export function ClassDeclaration(node, print) {
|
||||
this.push("class");
|
||||
|
||||
if (node.id) {
|
||||
this.space();
|
||||
print(node.id);
|
||||
this.push(" ");
|
||||
print.plain(node.id);
|
||||
}
|
||||
|
||||
print(node.typeParameters);
|
||||
print.plain(node.typeParameters);
|
||||
|
||||
if (node.superClass) {
|
||||
this.push(" extends ");
|
||||
print(node.superClass);
|
||||
print(node.superTypeParameters);
|
||||
print.plain(node.superClass);
|
||||
print.plain(node.superTypeParameters);
|
||||
}
|
||||
|
||||
if (node.implements) {
|
||||
@@ -21,7 +21,7 @@ export function ClassDeclaration(node, print) {
|
||||
}
|
||||
|
||||
this.space();
|
||||
print(node.body);
|
||||
print.plain(node.body);
|
||||
}
|
||||
|
||||
export { ClassDeclaration as ClassExpression };
|
||||
@@ -46,13 +46,13 @@ export function ClassProperty(node, print) {
|
||||
print.list(node.decorators);
|
||||
|
||||
if (node.static) this.push("static ");
|
||||
print(node.key);
|
||||
print(node.typeAnnotation);
|
||||
print.plain(node.key);
|
||||
print.plain(node.typeAnnotation);
|
||||
if (node.value) {
|
||||
this.space();
|
||||
this.push("=");
|
||||
this.space();
|
||||
print(node.value);
|
||||
print.plain(node.value);
|
||||
}
|
||||
this.semicolon();
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
export function ComprehensionBlock(node, print) {
|
||||
this.keyword("for");
|
||||
this.push("(");
|
||||
print(node.left);
|
||||
print.plain(node.left);
|
||||
this.push(" of ");
|
||||
print(node.right);
|
||||
print.plain(node.right);
|
||||
this.push(")");
|
||||
}
|
||||
|
||||
@@ -16,12 +16,12 @@ export function ComprehensionExpression(node, print) {
|
||||
if (node.filter) {
|
||||
this.keyword("if");
|
||||
this.push("(");
|
||||
print(node.filter);
|
||||
print.plain(node.filter);
|
||||
this.push(")");
|
||||
this.space();
|
||||
}
|
||||
|
||||
print(node.body);
|
||||
print.plain(node.body);
|
||||
|
||||
this.push(node.generator ? ")" : "]");
|
||||
}
|
||||
|
||||
@@ -16,40 +16,40 @@ export function UnaryExpression(node, print) {
|
||||
|
||||
this.push(node.operator);
|
||||
if (hasSpace) this.push(" ");
|
||||
print(node.argument);
|
||||
print.plain(node.argument);
|
||||
}
|
||||
|
||||
export function DoExpression(node, print) {
|
||||
this.push("do");
|
||||
this.space();
|
||||
print(node.body);
|
||||
print.plain(node.body);
|
||||
}
|
||||
|
||||
export function UpdateExpression(node, print) {
|
||||
if (node.prefix) {
|
||||
this.push(node.operator);
|
||||
print(node.argument);
|
||||
print.plain(node.argument);
|
||||
} else {
|
||||
print(node.argument);
|
||||
print.plain(node.argument);
|
||||
this.push(node.operator);
|
||||
}
|
||||
}
|
||||
|
||||
export function ConditionalExpression(node, print) {
|
||||
print(node.test);
|
||||
print.plain(node.test);
|
||||
this.space();
|
||||
this.push("?");
|
||||
this.space();
|
||||
print(node.consequent);
|
||||
print.plain(node.consequent);
|
||||
this.space();
|
||||
this.push(":");
|
||||
this.space();
|
||||
print(node.alternate);
|
||||
print.plain(node.alternate);
|
||||
}
|
||||
|
||||
export function NewExpression(node, print) {
|
||||
this.push("new ");
|
||||
print(node.callee);
|
||||
print.plain(node.callee);
|
||||
this.push("(");
|
||||
print.list(node.arguments);
|
||||
this.push(")");
|
||||
@@ -69,17 +69,20 @@ export function Super() {
|
||||
|
||||
export function Decorator(node, print) {
|
||||
this.push("@");
|
||||
print(node.expression);
|
||||
print.plain(node.expression);
|
||||
this.newline();
|
||||
}
|
||||
|
||||
export function CallExpression(node, print) {
|
||||
print(node.callee);
|
||||
print.plain(node.callee);
|
||||
|
||||
this.push("(");
|
||||
|
||||
var separator = ",";
|
||||
|
||||
if (node._prettyCall) {
|
||||
var isPrettyCall = node._prettyCall && !this.format.retainLines;
|
||||
|
||||
if (isPrettyCall) {
|
||||
separator += "\n";
|
||||
this.newline();
|
||||
this.indent();
|
||||
@@ -89,7 +92,7 @@ export function CallExpression(node, print) {
|
||||
|
||||
print.list(node.arguments, { separator: separator });
|
||||
|
||||
if (node._prettyCall) {
|
||||
if (isPrettyCall) {
|
||||
this.newline();
|
||||
this.dedent();
|
||||
}
|
||||
@@ -106,8 +109,8 @@ var buildYieldAwait = function (keyword) {
|
||||
}
|
||||
|
||||
if (node.argument) {
|
||||
this.space();
|
||||
print(node.argument);
|
||||
this.push(" ");
|
||||
print.plain(node.argument);
|
||||
}
|
||||
};
|
||||
};
|
||||
@@ -120,23 +123,23 @@ export function EmptyStatement() {
|
||||
}
|
||||
|
||||
export function ExpressionStatement(node, print) {
|
||||
print(node.expression);
|
||||
print.plain(node.expression);
|
||||
this.semicolon();
|
||||
}
|
||||
|
||||
export function AssignmentExpression(node, print) {
|
||||
// todo: add cases where the spaces can be dropped when in compact mode
|
||||
print(node.left);
|
||||
print.plain(node.left);
|
||||
this.push(" ");
|
||||
this.push(node.operator);
|
||||
this.push(" ");
|
||||
print(node.right);
|
||||
print.plain(node.right);
|
||||
}
|
||||
|
||||
export function BindExpression(node, print) {
|
||||
print(node.object);
|
||||
print.plain(node.object);
|
||||
this.push("::");
|
||||
print(node.callee);
|
||||
print.plain(node.callee);
|
||||
}
|
||||
|
||||
export {
|
||||
@@ -149,7 +152,7 @@ var SCIENTIFIC_NOTATION = /e/i;
|
||||
|
||||
export function MemberExpression(node, print) {
|
||||
var obj = node.object;
|
||||
print(obj);
|
||||
print.plain(obj);
|
||||
|
||||
if (!node.computed && t.isMemberExpression(node.property)) {
|
||||
throw new TypeError("Got a MemberExpression for MemberExpression property");
|
||||
@@ -162,7 +165,7 @@ export function MemberExpression(node, print) {
|
||||
|
||||
if (computed) {
|
||||
this.push("[");
|
||||
print(node.property);
|
||||
print.plain(node.property);
|
||||
this.push("]");
|
||||
} else {
|
||||
// 5..toFixed(2);
|
||||
@@ -171,12 +174,12 @@ export function MemberExpression(node, print) {
|
||||
}
|
||||
|
||||
this.push(".");
|
||||
print(node.property);
|
||||
print.plain(node.property);
|
||||
}
|
||||
}
|
||||
|
||||
export function MetaProperty(node, print) {
|
||||
print(node.meta);
|
||||
print.plain(node.meta);
|
||||
this.push(".");
|
||||
print(node.property);
|
||||
print.plain(node.property);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ export function AnyTypeAnnotation() {
|
||||
}
|
||||
|
||||
export function ArrayTypeAnnotation(node, print) {
|
||||
print(node.elementType);
|
||||
print.plain(node.elementType);
|
||||
this.push("[");
|
||||
this.push("]");
|
||||
}
|
||||
@@ -21,27 +21,27 @@ export function DeclareClass(node, print) {
|
||||
|
||||
export function DeclareFunction(node, print) {
|
||||
this.push("declare function ");
|
||||
print(node.id);
|
||||
print(node.id.typeAnnotation.typeAnnotation);
|
||||
print.plain(node.id);
|
||||
print.plain(node.id.typeAnnotation.typeAnnotation);
|
||||
this.semicolon();
|
||||
}
|
||||
|
||||
export function DeclareModule(node, print) {
|
||||
this.push("declare module ");
|
||||
print(node.id);
|
||||
print.plain(node.id);
|
||||
this.space();
|
||||
print(node.body);
|
||||
print.plain(node.body);
|
||||
}
|
||||
|
||||
export function DeclareVariable(node, print) {
|
||||
this.push("declare var ");
|
||||
print(node.id);
|
||||
print(node.id.typeAnnotation);
|
||||
print.plain(node.id);
|
||||
print.plain(node.id.typeAnnotation);
|
||||
this.semicolon();
|
||||
}
|
||||
|
||||
export function FunctionTypeAnnotation(node, print, parent) {
|
||||
print(node.typeParameters);
|
||||
print.plain(node.typeParameters);
|
||||
this.push("(");
|
||||
print.list(node.params);
|
||||
|
||||
@@ -51,7 +51,7 @@ export function FunctionTypeAnnotation(node, print, parent) {
|
||||
this.space();
|
||||
}
|
||||
this.push("...");
|
||||
print(node.rest);
|
||||
print.plain(node.rest);
|
||||
}
|
||||
|
||||
this.push(")");
|
||||
@@ -65,33 +65,33 @@ export function FunctionTypeAnnotation(node, print, parent) {
|
||||
}
|
||||
|
||||
this.space();
|
||||
print(node.returnType);
|
||||
print.plain(node.returnType);
|
||||
}
|
||||
|
||||
export function FunctionTypeParam(node, print) {
|
||||
print(node.name);
|
||||
print.plain(node.name);
|
||||
if (node.optional) this.push("?");
|
||||
this.push(":");
|
||||
this.space();
|
||||
print(node.typeAnnotation);
|
||||
print.plain(node.typeAnnotation);
|
||||
}
|
||||
|
||||
export function InterfaceExtends(node, print) {
|
||||
print(node.id);
|
||||
print(node.typeParameters);
|
||||
print.plain(node.id);
|
||||
print.plain(node.typeParameters);
|
||||
}
|
||||
|
||||
export { InterfaceExtends as ClassImplements, InterfaceExtends as GenericTypeAnnotation };
|
||||
|
||||
export function _interfaceish(node, print) {
|
||||
print(node.id);
|
||||
print(node.typeParameters);
|
||||
print.plain(node.id);
|
||||
print.plain(node.typeParameters);
|
||||
if (node.extends.length) {
|
||||
this.push(" extends ");
|
||||
print.join(node.extends, { separator: ", " });
|
||||
}
|
||||
this.space();
|
||||
print(node.body);
|
||||
print.plain(node.body);
|
||||
}
|
||||
|
||||
export function InterfaceDeclaration(node, print) {
|
||||
@@ -103,9 +103,13 @@ export function IntersectionTypeAnnotation(node, print) {
|
||||
print.join(node.types, { separator: " & " });
|
||||
}
|
||||
|
||||
export function MixedTypeAnnotation() {
|
||||
this.push("mixed");
|
||||
}
|
||||
|
||||
export function NullableTypeAnnotation(node, print) {
|
||||
this.push("?");
|
||||
print(node.typeAnnotation);
|
||||
print.plain(node.typeAnnotation);
|
||||
}
|
||||
|
||||
export function NumberTypeAnnotation() {
|
||||
@@ -128,17 +132,17 @@ export function TupleTypeAnnotation(node, print) {
|
||||
|
||||
export function TypeofTypeAnnotation(node, print) {
|
||||
this.push("typeof ");
|
||||
print(node.argument);
|
||||
print.plain(node.argument);
|
||||
}
|
||||
|
||||
export function TypeAlias(node, print) {
|
||||
this.push("type ");
|
||||
print(node.id);
|
||||
print(node.typeParameters);
|
||||
print.plain(node.id);
|
||||
print.plain(node.typeParameters);
|
||||
this.space();
|
||||
this.push("=");
|
||||
this.space();
|
||||
print(node.right);
|
||||
print.plain(node.right);
|
||||
this.semicolon();
|
||||
}
|
||||
|
||||
@@ -146,7 +150,7 @@ export function TypeAnnotation(node, print) {
|
||||
this.push(":");
|
||||
this.space();
|
||||
if (node.optional) this.push("?");
|
||||
print(node.typeAnnotation);
|
||||
print.plain(node.typeAnnotation);
|
||||
}
|
||||
|
||||
export function TypeParameterInstantiation(node, print) {
|
||||
@@ -183,37 +187,37 @@ export function ObjectTypeAnnotation(node, print) {
|
||||
|
||||
export function ObjectTypeCallProperty(node, print) {
|
||||
if (node.static) this.push("static ");
|
||||
print(node.value);
|
||||
print.plain(node.value);
|
||||
}
|
||||
|
||||
export function ObjectTypeIndexer(node, print) {
|
||||
if (node.static) this.push("static ");
|
||||
this.push("[");
|
||||
print(node.id);
|
||||
print.plain(node.id);
|
||||
this.push(":");
|
||||
this.space();
|
||||
print(node.key);
|
||||
print.plain(node.key);
|
||||
this.push("]");
|
||||
this.push(":");
|
||||
this.space();
|
||||
print(node.value);
|
||||
print.plain(node.value);
|
||||
}
|
||||
|
||||
export function ObjectTypeProperty(node, print) {
|
||||
if (node.static) this.push("static ");
|
||||
print(node.key);
|
||||
print.plain(node.key);
|
||||
if (node.optional) this.push("?");
|
||||
if (!t.isFunctionTypeAnnotation(node.value)) {
|
||||
this.push(":");
|
||||
this.space();
|
||||
}
|
||||
print(node.value);
|
||||
print.plain(node.value);
|
||||
}
|
||||
|
||||
export function QualifiedTypeIdentifier(node, print) {
|
||||
print(node.qualification);
|
||||
print.plain(node.qualification);
|
||||
this.push(".");
|
||||
print(node.id);
|
||||
print.plain(node.id);
|
||||
}
|
||||
|
||||
export function UnionTypeAnnotation(node, print) {
|
||||
@@ -222,8 +226,8 @@ export function UnionTypeAnnotation(node, print) {
|
||||
|
||||
export function TypeCastExpression(node, print) {
|
||||
this.push("(");
|
||||
print(node.expression);
|
||||
print(node.typeAnnotation);
|
||||
print.plain(node.expression);
|
||||
print.plain(node.typeAnnotation);
|
||||
this.push(")");
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import each from "lodash/collection/each";
|
||||
import * as t from "../../types";
|
||||
|
||||
export function JSXAttribute(node, print) {
|
||||
print(node.name);
|
||||
print.plain(node.name);
|
||||
if (node.value) {
|
||||
this.push("=");
|
||||
print(node.value);
|
||||
print.plain(node.value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,32 +13,32 @@ export function JSXIdentifier(node) {
|
||||
}
|
||||
|
||||
export function JSXNamespacedName(node, print) {
|
||||
print(node.namespace);
|
||||
print.plain(node.namespace);
|
||||
this.push(":");
|
||||
print(node.name);
|
||||
print.plain(node.name);
|
||||
}
|
||||
|
||||
export function JSXMemberExpression(node, print) {
|
||||
print(node.object);
|
||||
print.plain(node.object);
|
||||
this.push(".");
|
||||
print(node.property);
|
||||
print.plain(node.property);
|
||||
}
|
||||
|
||||
export function JSXSpreadAttribute(node, print) {
|
||||
this.push("{...");
|
||||
print(node.argument);
|
||||
print.plain(node.argument);
|
||||
this.push("}");
|
||||
}
|
||||
|
||||
export function JSXExpressionContainer(node, print) {
|
||||
this.push("{");
|
||||
print(node.expression);
|
||||
print.plain(node.expression);
|
||||
this.push("}");
|
||||
}
|
||||
|
||||
export function JSXElement(node, print) {
|
||||
var open = node.openingElement;
|
||||
print(open);
|
||||
print.plain(open);
|
||||
if (open.selfClosing) return;
|
||||
|
||||
this.indent();
|
||||
@@ -47,17 +46,17 @@ export function JSXElement(node, print) {
|
||||
if (t.isLiteral(child)) {
|
||||
this.push(child.value, true);
|
||||
} else {
|
||||
print(child);
|
||||
print.plain(child);
|
||||
}
|
||||
}
|
||||
this.dedent();
|
||||
|
||||
print(node.closingElement);
|
||||
print.plain(node.closingElement);
|
||||
}
|
||||
|
||||
export function JSXOpeningElement(node, print) {
|
||||
this.push("<");
|
||||
print(node.name);
|
||||
print.plain(node.name);
|
||||
if (node.attributes.length > 0) {
|
||||
this.push(" ");
|
||||
print.join(node.attributes, { separator: " " });
|
||||
@@ -67,7 +66,7 @@ export function JSXOpeningElement(node, print) {
|
||||
|
||||
export function JSXClosingElement(node, print) {
|
||||
this.push("</");
|
||||
print(node.name);
|
||||
print.plain(node.name);
|
||||
this.push(">");
|
||||
}
|
||||
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
import * as t from "../../types";
|
||||
|
||||
export function _params(node, print) {
|
||||
print(node.typeParameters);
|
||||
print.plain(node.typeParameters);
|
||||
this.push("(");
|
||||
print.list(node.params, {
|
||||
iterator: (node) =>{
|
||||
if (node.optional) this.push("?");
|
||||
print(node.typeAnnotation);
|
||||
print.plain(node.typeAnnotation);
|
||||
}
|
||||
});
|
||||
this.push(")");
|
||||
|
||||
if (node.returnType) {
|
||||
print(node.returnType);
|
||||
print.plain(node.returnType);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,15 +35,15 @@ export function _method(node, print) {
|
||||
|
||||
if (node.computed) {
|
||||
this.push("[");
|
||||
print(key);
|
||||
print.plain(key);
|
||||
this.push("]");
|
||||
} else {
|
||||
print(key);
|
||||
print.plain(key);
|
||||
}
|
||||
|
||||
this._params(value, print);
|
||||
this.push(" ");
|
||||
print(value.body);
|
||||
print.plain(value.body);
|
||||
}
|
||||
|
||||
export function FunctionExpression(node, print) {
|
||||
@@ -53,14 +53,14 @@ export function FunctionExpression(node, print) {
|
||||
|
||||
if (node.id) {
|
||||
this.push(" ");
|
||||
print(node.id);
|
||||
print.plain(node.id);
|
||||
} else {
|
||||
this.space();
|
||||
}
|
||||
|
||||
this._params(node, print);
|
||||
this.space();
|
||||
print(node.body);
|
||||
print.plain(node.body);
|
||||
}
|
||||
|
||||
export { FunctionExpression as FunctionDeclaration };
|
||||
@@ -69,7 +69,7 @@ export function ArrowFunctionExpression(node, print) {
|
||||
if (node.async) this.push("async ");
|
||||
|
||||
if (node.params.length === 1 && t.isIdentifier(node.params[0])) {
|
||||
print(node.params[0]);
|
||||
print.plain(node.params[0]);
|
||||
} else {
|
||||
this._params(node, print);
|
||||
}
|
||||
@@ -82,7 +82,7 @@ export function ArrowFunctionExpression(node, print) {
|
||||
this.push("(");
|
||||
}
|
||||
|
||||
print(node.body);
|
||||
print.plain(node.body);
|
||||
|
||||
if (bodyNeedsParens) {
|
||||
this.push(")");
|
||||
|
||||
@@ -1,43 +1,42 @@
|
||||
import each from "lodash/collection/each";
|
||||
import * as t from "../../types";
|
||||
|
||||
export function ImportSpecifier(node, print) {
|
||||
print(node.imported);
|
||||
print.plain(node.imported);
|
||||
if (node.local && node.local.name !== node.imported.name) {
|
||||
this.push(" as ");
|
||||
print(node.local);
|
||||
print.plain(node.local);
|
||||
}
|
||||
}
|
||||
|
||||
export function ImportDefaultSpecifier(node, print) {
|
||||
print(node.local);
|
||||
print.plain(node.local);
|
||||
}
|
||||
|
||||
export function ExportDefaultSpecifier(node, print) {
|
||||
print(node.exported);
|
||||
print.plain(node.exported);
|
||||
}
|
||||
|
||||
export function ExportSpecifier(node, print) {
|
||||
print(node.local);
|
||||
print.plain(node.local);
|
||||
if (node.exported && node.local.name !== node.exported.name) {
|
||||
this.push(" as ");
|
||||
print(node.exported);
|
||||
print.plain(node.exported);
|
||||
}
|
||||
}
|
||||
|
||||
export function ExportNamespaceSpecifier(node, print) {
|
||||
this.push("* as ");
|
||||
print(node.exported);
|
||||
print.plain(node.exported);
|
||||
}
|
||||
|
||||
export function ExportAllDeclaration(node, print) {
|
||||
this.push("export *");
|
||||
if (node.exported) {
|
||||
this.push(" as ");
|
||||
print(node.exported);
|
||||
print.plain(node.exported);
|
||||
}
|
||||
this.push(" from ");
|
||||
print(node.source);
|
||||
print.plain(node.source);
|
||||
this.semicolon();
|
||||
}
|
||||
|
||||
@@ -56,14 +55,14 @@ function ExportDeclaration(node, print) {
|
||||
|
||||
if (node.declaration) {
|
||||
var declar = node.declaration;
|
||||
print(declar);
|
||||
print.plain(declar);
|
||||
if (t.isStatement(declar) || t.isFunction(declar) || t.isClass(declar)) return;
|
||||
} else {
|
||||
var first = specifiers[0];
|
||||
var hasSpecial = false;
|
||||
if (t.isExportDefaultSpecifier(first) || t.isExportNamespaceSpecifier(first)) {
|
||||
hasSpecial = true;
|
||||
print(specifiers.shift());
|
||||
print.plain(specifiers.shift());
|
||||
if (specifiers.length) {
|
||||
this.push(", ");
|
||||
}
|
||||
@@ -81,7 +80,7 @@ function ExportDeclaration(node, print) {
|
||||
|
||||
if (node.source) {
|
||||
this.push(" from ");
|
||||
print(node.source);
|
||||
print.plain(node.source);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,7 +98,7 @@ export function ImportDeclaration(node, print) {
|
||||
if (specfiers && specfiers.length) {
|
||||
var first = node.specifiers[0];
|
||||
if (t.isImportDefaultSpecifier(first) || t.isImportNamespaceSpecifier(first)) {
|
||||
print(node.specifiers.shift());
|
||||
print.plain(node.specifiers.shift());
|
||||
if (node.specifiers.length) {
|
||||
this.push(", ");
|
||||
}
|
||||
@@ -107,20 +106,20 @@ export function ImportDeclaration(node, print) {
|
||||
|
||||
if (node.specifiers.length) {
|
||||
this.push("{");
|
||||
this.space()
|
||||
this.space();
|
||||
print.join(node.specifiers, { separator: ", " });
|
||||
this.space()
|
||||
this.space();
|
||||
this.push("}");
|
||||
}
|
||||
|
||||
this.push(" from ");
|
||||
}
|
||||
|
||||
print(node.source);
|
||||
print.plain(node.source);
|
||||
this.semicolon();
|
||||
}
|
||||
|
||||
export function ImportNamespaceSpecifier(node, print) {
|
||||
this.push("* as ");
|
||||
print(node.local);
|
||||
print.plain(node.local);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import * as t from "../../types";
|
||||
export function WithStatement(node, print) {
|
||||
this.keyword("with");
|
||||
this.push("(");
|
||||
print(node.object);
|
||||
print.plain(node.object);
|
||||
this.push(")");
|
||||
print.block(node.body);
|
||||
}
|
||||
@@ -12,7 +12,7 @@ export function WithStatement(node, print) {
|
||||
export function IfStatement(node, print) {
|
||||
this.keyword("if");
|
||||
this.push("(");
|
||||
print(node.test);
|
||||
print.plain(node.test);
|
||||
this.push(")");
|
||||
this.space();
|
||||
|
||||
@@ -29,18 +29,18 @@ export function ForStatement(node, print) {
|
||||
this.keyword("for");
|
||||
this.push("(");
|
||||
|
||||
print(node.init);
|
||||
print.plain(node.init);
|
||||
this.push(";");
|
||||
|
||||
if (node.test) {
|
||||
this.push(" ");
|
||||
print(node.test);
|
||||
print.plain(node.test);
|
||||
}
|
||||
this.push(";");
|
||||
|
||||
if (node.update) {
|
||||
this.push(" ");
|
||||
print(node.update);
|
||||
print.plain(node.update);
|
||||
}
|
||||
|
||||
this.push(")");
|
||||
@@ -50,7 +50,7 @@ export function ForStatement(node, print) {
|
||||
export function WhileStatement(node, print) {
|
||||
this.keyword("while");
|
||||
this.push("(");
|
||||
print(node.test);
|
||||
print.plain(node.test);
|
||||
this.push(")");
|
||||
print.block(node.body);
|
||||
}
|
||||
@@ -59,9 +59,9 @@ var buildForXStatement = function (op) {
|
||||
return function (node, print) {
|
||||
this.keyword("for");
|
||||
this.push("(");
|
||||
print(node.left);
|
||||
print.plain(node.left);
|
||||
this.push(` ${op} `);
|
||||
print(node.right);
|
||||
print.plain(node.right);
|
||||
this.push(")");
|
||||
print.block(node.body);
|
||||
};
|
||||
@@ -72,11 +72,11 @@ export var ForOfStatement = buildForXStatement("of");
|
||||
|
||||
export function DoWhileStatement(node, print) {
|
||||
this.push("do ");
|
||||
print(node.body);
|
||||
print.plain(node.body);
|
||||
this.space();
|
||||
this.keyword("while");
|
||||
this.push("(");
|
||||
print(node.test);
|
||||
print.plain(node.test);
|
||||
this.push(");");
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@ var buildLabelStatement = function (prefix, key) {
|
||||
var label = node[key || "label"];
|
||||
if (label) {
|
||||
this.push(" ");
|
||||
print(label);
|
||||
print.plain(label);
|
||||
}
|
||||
|
||||
this.semicolon();
|
||||
@@ -99,50 +99,50 @@ export var ReturnStatement = buildLabelStatement("return", "argument");
|
||||
export var BreakStatement = buildLabelStatement("break");
|
||||
|
||||
export function LabeledStatement(node, print) {
|
||||
print(node.label);
|
||||
print.plain(node.label);
|
||||
this.push(": ");
|
||||
print(node.body);
|
||||
print.plain(node.body);
|
||||
}
|
||||
|
||||
export function TryStatement(node, print) {
|
||||
this.keyword("try");
|
||||
print(node.block);
|
||||
print.plain(node.block);
|
||||
this.space();
|
||||
|
||||
// Esprima bug puts the catch clause in a `handlers` array.
|
||||
// see https://code.google.com/p/esprima/issues/detail?id=433
|
||||
// We run into this from regenerator generated ast.
|
||||
if (node.handlers) {
|
||||
print(node.handlers[0]);
|
||||
print.plain(node.handlers[0]);
|
||||
} else {
|
||||
print(node.handler);
|
||||
print.plain(node.handler);
|
||||
}
|
||||
|
||||
if (node.finalizer) {
|
||||
this.space();
|
||||
this.push("finally ");
|
||||
print(node.finalizer);
|
||||
print.plain(node.finalizer);
|
||||
}
|
||||
}
|
||||
|
||||
export function CatchClause(node, print) {
|
||||
this.keyword("catch");
|
||||
this.push("(");
|
||||
print(node.param);
|
||||
print.plain(node.param);
|
||||
this.push(") ");
|
||||
print(node.body);
|
||||
print.plain(node.body);
|
||||
}
|
||||
|
||||
export function ThrowStatement(node, print) {
|
||||
this.push("throw ");
|
||||
print(node.argument);
|
||||
print.plain(node.argument);
|
||||
this.semicolon();
|
||||
}
|
||||
|
||||
export function SwitchStatement(node, print) {
|
||||
this.keyword("switch");
|
||||
this.push("(");
|
||||
print(node.discriminant);
|
||||
print.plain(node.discriminant);
|
||||
this.push(")");
|
||||
this.space();
|
||||
this.push("{");
|
||||
@@ -160,7 +160,7 @@ export function SwitchStatement(node, print) {
|
||||
export function SwitchCase(node, print) {
|
||||
if (node.test) {
|
||||
this.push("case ");
|
||||
print(node.test);
|
||||
print.plain(node.test);
|
||||
this.push(":");
|
||||
} else {
|
||||
this.push("default:");
|
||||
@@ -207,12 +207,12 @@ export function VariableDeclaration(node, print, parent) {
|
||||
}
|
||||
|
||||
export function VariableDeclarator(node, print) {
|
||||
print(node.id);
|
||||
print(node.id.typeAnnotation);
|
||||
print.plain(node.id);
|
||||
print.plain(node.id.typeAnnotation);
|
||||
if (node.init) {
|
||||
this.space();
|
||||
this.push("=");
|
||||
this.space();
|
||||
print(node.init);
|
||||
print.plain(node.init);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import each from "lodash/collection/each";
|
||||
|
||||
export function TaggedTemplateExpression(node, print) {
|
||||
print(node.tag);
|
||||
print(node.quasi);
|
||||
print.plain(node.tag);
|
||||
print.plain(node.quasi);
|
||||
}
|
||||
|
||||
export function TemplateElement(node) {
|
||||
@@ -16,11 +14,11 @@ export function TemplateLiteral(node, print) {
|
||||
var len = quasis.length;
|
||||
|
||||
for (var i = 0; i < len; i++) {
|
||||
print(quasis[i]);
|
||||
print.plain(quasis[i]);
|
||||
|
||||
if (i + 1 < len) {
|
||||
this.push("${ ");
|
||||
print(node.expressions[i]);
|
||||
print.plain(node.expressions[i]);
|
||||
this.push(" }");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import each from "lodash/collection/each";
|
||||
/* eslint quotes: 0 */
|
||||
|
||||
import * as t from "../../types";
|
||||
|
||||
export function Identifier(node) {
|
||||
@@ -7,7 +8,7 @@ export function Identifier(node) {
|
||||
|
||||
export function RestElement(node, print) {
|
||||
this.push("...");
|
||||
print(node.argument);
|
||||
print.plain(node.argument);
|
||||
}
|
||||
|
||||
export { RestElement as SpreadElement, RestElement as SpreadProperty };
|
||||
@@ -36,10 +37,16 @@ export function Property(node, print) {
|
||||
} else {
|
||||
if (node.computed) {
|
||||
this.push("[");
|
||||
print(node.key);
|
||||
print.plain(node.key);
|
||||
this.push("]");
|
||||
} else {
|
||||
print(node.key);
|
||||
// print `({ foo: foo = 5 } = {})` as `({ foo = 5 } = {});`
|
||||
if (t.isAssignmentPattern(node.value) && t.isIdentifier(node.key) && node.key.name === node.value.left.name) {
|
||||
print.plain(node.value);
|
||||
return;
|
||||
}
|
||||
|
||||
print.plain(node.key);
|
||||
|
||||
// shorthand!
|
||||
if (node.shorthand &&
|
||||
@@ -52,7 +59,7 @@ export function Property(node, print) {
|
||||
|
||||
this.push(":");
|
||||
this.space();
|
||||
print(node.value);
|
||||
print.plain(node.value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +80,7 @@ export function ArrayExpression(node, print) {
|
||||
this.push(",");
|
||||
} else {
|
||||
if (i > 0) this.push(" ");
|
||||
print(elem);
|
||||
print.plain(elem);
|
||||
if (i < len - 1) this.push(",");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import detectIndent from "detect-indent";
|
||||
import Whitespace from "./whitespace";
|
||||
import NodePrinter from "./node/printer";
|
||||
import repeating from "repeating";
|
||||
import SourceMap from "./source-map";
|
||||
import Position from "./position";
|
||||
@@ -20,7 +21,7 @@ class CodeGenerator {
|
||||
this.opts = opts;
|
||||
this.ast = ast;
|
||||
|
||||
this.whitespace = new Whitespace(this.tokens, this.comments, this.format);
|
||||
this.whitespace = new Whitespace(this.tokens);
|
||||
this.position = new Position;
|
||||
this.map = new SourceMap(this.position, opts, code);
|
||||
this.buffer = new Buffer(this.position, this.format);
|
||||
@@ -67,7 +68,6 @@ class CodeGenerator {
|
||||
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] === "'") {
|
||||
@@ -77,6 +77,7 @@ class CodeGenerator {
|
||||
}
|
||||
|
||||
checked++;
|
||||
if (checked >= 3) break;
|
||||
}
|
||||
|
||||
if (occurences.single > occurences.double) {
|
||||
@@ -120,40 +121,15 @@ class CodeGenerator {
|
||||
}
|
||||
|
||||
buildPrint(parent) {
|
||||
var print = (node, opts) => {
|
||||
return this.print(node, parent, opts);
|
||||
};
|
||||
|
||||
print.sequence = (nodes, opts = {}) => {
|
||||
opts.statement = true;
|
||||
return this.printJoin(print, nodes, opts);
|
||||
};
|
||||
|
||||
print.join = (nodes, opts) => {
|
||||
return this.printJoin(print, nodes, opts);
|
||||
};
|
||||
|
||||
print.list = function (items, opts = {}) {
|
||||
if (opts.separator == null) opts.separator = ", ";
|
||||
print.join(items, opts);
|
||||
};
|
||||
|
||||
print.block = (node) => {
|
||||
return this.printBlock(print, node);
|
||||
};
|
||||
|
||||
print.indentOnComments = (node) => {
|
||||
return this.printAndIndentOnComments(print, node);
|
||||
};
|
||||
|
||||
return print;
|
||||
return new NodePrinter(this, parent);
|
||||
}
|
||||
|
||||
catchUp(node, parent) {
|
||||
catchUp(node, parent, leftParenPrinted) {
|
||||
// catch up to this nodes newline if we're behind
|
||||
if (node.loc && this.format.retainLines && this.buffer.buf) {
|
||||
var needsParens = false;
|
||||
if (parent && this.position.line < node.loc.start.line && t.isTerminatorless(parent)) {
|
||||
if (!leftParenPrinted && parent &&
|
||||
this.position.line < node.loc.start.line && t.isTerminatorless(parent)) {
|
||||
needsParens = true;
|
||||
this._push("(");
|
||||
}
|
||||
@@ -165,6 +141,36 @@ class CodeGenerator {
|
||||
return false;
|
||||
}
|
||||
|
||||
_printNewline(leading, node, parent, opts) {
|
||||
if (!opts.statement && !n.isUserWhitespacable(node, parent)) {
|
||||
return;
|
||||
}
|
||||
|
||||
var lines = 0;
|
||||
|
||||
if (node.start != null && !node._ignoreUserWhitespace) {
|
||||
// user node
|
||||
if (leading) {
|
||||
lines = this.whitespace.getNewlinesBefore(node);
|
||||
} else {
|
||||
lines = this.whitespace.getNewlinesAfter(node);
|
||||
}
|
||||
} else {
|
||||
// generated node
|
||||
if (!leading) lines++; // always include at least a single line after
|
||||
if (opts.addNewlines) lines += opts.addNewlines(leading, node) || 0;
|
||||
|
||||
var needs = n.needsWhitespaceAfter;
|
||||
if (leading) needs = n.needsWhitespaceBefore;
|
||||
if (needs(node, parent)) lines++;
|
||||
|
||||
// generated nodes can't add starting file whitespace
|
||||
if (!this.buffer.buf) lines = 0;
|
||||
}
|
||||
|
||||
this.newline(lines);
|
||||
}
|
||||
|
||||
print(node, parent, opts = {}) {
|
||||
if (!node) return;
|
||||
|
||||
@@ -177,36 +183,6 @@ class CodeGenerator {
|
||||
this.format.concise = true;
|
||||
}
|
||||
|
||||
var newline = (leading) => {
|
||||
if (!opts.statement && !n.isUserWhitespacable(node, parent)) {
|
||||
return;
|
||||
}
|
||||
|
||||
var lines = 0;
|
||||
|
||||
if (node.start != null && !node._ignoreUserWhitespace) {
|
||||
// user node
|
||||
if (leading) {
|
||||
lines = this.whitespace.getNewlinesBefore(node);
|
||||
} else {
|
||||
lines = this.whitespace.getNewlinesAfter(node);
|
||||
}
|
||||
} else {
|
||||
// generated node
|
||||
if (!leading) lines++; // always include at least a single line after
|
||||
if (opts.addNewlines) lines += opts.addNewlines(leading, node) || 0;
|
||||
|
||||
var needs = n.needsWhitespaceAfter;
|
||||
if (leading) needs = n.needsWhitespaceBefore;
|
||||
if (needs(node, parent)) lines++;
|
||||
|
||||
// generated nodes can't add starting file whitespace
|
||||
if (!this.buffer.buf) lines = 0;
|
||||
}
|
||||
|
||||
this.newline(lines);
|
||||
};
|
||||
|
||||
if (this[node.type]) {
|
||||
var needsNoLineTermParens = n.needsParensNoLineTerminator(node, parent);
|
||||
var needsParens = needsNoLineTermParens || n.needsParens(node, parent);
|
||||
@@ -216,9 +192,9 @@ class CodeGenerator {
|
||||
|
||||
this.printLeadingComments(node, parent);
|
||||
|
||||
var needsParensFromCatchup = this.catchUp(node, parent);
|
||||
var needsParensFromCatchup = this.catchUp(node, parent, needsParens);
|
||||
|
||||
newline(true);
|
||||
this._printNewline(true, node, parent, opts);
|
||||
|
||||
if (opts.before) opts.before();
|
||||
this.map.mark(node, "start");
|
||||
@@ -236,7 +212,7 @@ class CodeGenerator {
|
||||
|
||||
this.format.concise = oldConcise;
|
||||
|
||||
newline(false);
|
||||
this._printNewline(false, node, parent, opts);
|
||||
|
||||
this.printTrailingComments(node, parent);
|
||||
} else {
|
||||
@@ -251,21 +227,23 @@ class CodeGenerator {
|
||||
|
||||
if (opts.indent) this.indent();
|
||||
|
||||
var printOpts = {
|
||||
statement: opts.statement,
|
||||
addNewlines: opts.addNewlines,
|
||||
after: () => {
|
||||
if (opts.iterator) {
|
||||
opts.iterator(node, i);
|
||||
}
|
||||
|
||||
if (opts.separator && i < len - 1) {
|
||||
this.push(opts.separator);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
for (var i = 0; i < nodes.length; i++) {
|
||||
var node = nodes[i];
|
||||
print(node, {
|
||||
statement: opts.statement,
|
||||
addNewlines: opts.addNewlines,
|
||||
after: () => {
|
||||
if (opts.iterator) {
|
||||
opts.iterator(node, i);
|
||||
}
|
||||
|
||||
if (opts.separator && i < len - 1) {
|
||||
this.push(opts.separator);
|
||||
}
|
||||
}
|
||||
});
|
||||
print.plain(node, printOpts);
|
||||
}
|
||||
|
||||
if (opts.indent) this.dedent();
|
||||
@@ -274,7 +252,7 @@ class CodeGenerator {
|
||||
printAndIndentOnComments(print, node) {
|
||||
var indent = !!node.leadingComments;
|
||||
if (indent) this.indent();
|
||||
print(node);
|
||||
print.plain(node);
|
||||
if (indent) this.dedent();
|
||||
}
|
||||
|
||||
@@ -283,13 +261,13 @@ class CodeGenerator {
|
||||
this.semicolon();
|
||||
} else {
|
||||
this.push(" ");
|
||||
print(node);
|
||||
print.plain(node);
|
||||
}
|
||||
}
|
||||
|
||||
generateComment(comment) {
|
||||
var val = comment.value;
|
||||
if (comment.type === "Line") {
|
||||
if (comment.type === "CommentLine") {
|
||||
val = `//${val}`;
|
||||
} else {
|
||||
val = `/*${val}*/`;
|
||||
@@ -317,7 +295,7 @@ class CodeGenerator {
|
||||
nodes.push(node.argument);
|
||||
}
|
||||
|
||||
for (var node of (nodes: Array)) {
|
||||
for (let node of (nodes: Array)) {
|
||||
comments = comments.concat(this._getComments(key, node));
|
||||
}
|
||||
|
||||
@@ -330,7 +308,6 @@ class CodeGenerator {
|
||||
|
||||
_printComments(comments) {
|
||||
if (this.format.compact) return;
|
||||
|
||||
if (!this.format.comments) return;
|
||||
if (!comments || !comments.length) return;
|
||||
|
||||
@@ -366,8 +343,8 @@ class CodeGenerator {
|
||||
}
|
||||
|
||||
//
|
||||
if (comment.type === "Block" && this.format.indent.adjustMultilineComment) {
|
||||
var offset = comment.loc.start.column;
|
||||
if (comment.type === "CommentBlock" && this.format.indent.adjustMultilineComment) {
|
||||
var offset = comment.loc && comment.loc.start.column;
|
||||
if (offset) {
|
||||
var newlineRegex = new RegExp("\\n\\s{1," + offset + "}", "g");
|
||||
val = val.replace(newlineRegex, "\n");
|
||||
@@ -383,7 +360,7 @@ class CodeGenerator {
|
||||
|
||||
// force a newline for line comments when retainLines is set in case the next printed node
|
||||
// doesn't catch up
|
||||
if (this.format.retainLines && comment.type === "Line") {
|
||||
if (this.format.retainLines && comment.type === "CommentLine") {
|
||||
val += "\n";
|
||||
}
|
||||
|
||||
|
||||
32
src/babel/generation/node/printer.js
Normal file
32
src/babel/generation/node/printer.js
Normal file
@@ -0,0 +1,32 @@
|
||||
export default class NodePrinter {
|
||||
constructor(generator, parent) {
|
||||
this.generator = generator;
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
plain(node, opts) {
|
||||
return this.generator.print(node, this.parent, opts);
|
||||
}
|
||||
|
||||
sequence(nodes, opts = {}) {
|
||||
opts.statement = true;
|
||||
return this.generator.printJoin(this, nodes, opts);
|
||||
}
|
||||
|
||||
join(nodes, opts) {
|
||||
return this.generator.printJoin(this, nodes, opts);
|
||||
}
|
||||
|
||||
list(items, opts = {}) {
|
||||
if (opts.separator == null) opts.separator = ", ";
|
||||
return this.join(items, opts);
|
||||
}
|
||||
|
||||
block(node) {
|
||||
return this.generator.printBlock(this, node);
|
||||
}
|
||||
|
||||
indentOnComments(node) {
|
||||
return this.generator.printAndIndentOnComments(this, node);
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ export default class SourceMap {
|
||||
|
||||
if (opts.sourceMaps) {
|
||||
this.map = new sourceMap.SourceMapGenerator({
|
||||
file: opts.sourceMapName,
|
||||
file: opts.sourceMapTarget,
|
||||
sourceRoot: opts.sourceRoot
|
||||
});
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import sortBy from "lodash/collection/sortBy";
|
||||
|
||||
/**
|
||||
* Returns `i`th number from `base`, continuing from 0 when `max` is reached.
|
||||
* Useful for shifting `for` loop by a fixed number but going over all items.
|
||||
@@ -21,8 +19,8 @@ function getLookupIndex(i, base, max) {
|
||||
}
|
||||
|
||||
export default class Whitespace {
|
||||
constructor(tokens, comments) {
|
||||
this.tokens = sortBy(tokens.concat(comments), "start");
|
||||
constructor(tokens) {
|
||||
this.tokens = tokens;
|
||||
this.used = {};
|
||||
|
||||
// Profiling this code shows that while generator passes over it, indexes
|
||||
@@ -40,12 +38,11 @@ export default class Whitespace {
|
||||
var startToken;
|
||||
var endToken;
|
||||
var tokens = this.tokens;
|
||||
var token;
|
||||
|
||||
for (var j = 0; j < tokens.length; j++) {
|
||||
// optimize for forward traversal by shifting for loop index
|
||||
var i = getLookupIndex(j, this._lastFoundIndex, this.tokens.length);
|
||||
token = tokens[i];
|
||||
var token = tokens[i];
|
||||
|
||||
// this is the token this node starts with
|
||||
if (node.start === token.start) {
|
||||
@@ -64,17 +61,17 @@ export default class Whitespace {
|
||||
var startToken;
|
||||
var endToken;
|
||||
var tokens = this.tokens;
|
||||
var token;
|
||||
|
||||
for (var j = 0; j < tokens.length; j++) {
|
||||
// optimize for forward traversal by shifting for loop index
|
||||
var i = getLookupIndex(j, this._lastFoundIndex, this.tokens.length);
|
||||
token = tokens[i];
|
||||
var token = tokens[i];
|
||||
|
||||
// this is the token this node ends with
|
||||
if (node.end === token.end) {
|
||||
startToken = token;
|
||||
endToken = tokens[i + 1];
|
||||
if (endToken.type.label === ",") endToken = tokens[i + 2];
|
||||
|
||||
this._lastFoundIndex = i;
|
||||
break;
|
||||
@@ -85,7 +82,7 @@ export default class Whitespace {
|
||||
return 1;
|
||||
} else {
|
||||
var lines = this.getNewlinesBetween(startToken, endToken);
|
||||
if (node.type === "Line" && !lines) {
|
||||
if (node.type === "CommentLine" && !lines) {
|
||||
// line comment
|
||||
return 1;
|
||||
} else {
|
||||
|
||||
@@ -85,4 +85,4 @@ export default function (lines: number, lineNumber: number, colNumber: number, o
|
||||
params.before = params.before.replace(/^./, ">");
|
||||
}
|
||||
}).join("\n");
|
||||
};
|
||||
}
|
||||
|
||||
@@ -6,4 +6,4 @@ export default function (ast, comments, tokens) {
|
||||
} else {
|
||||
throw new Error("Not a valid ast?");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
export default function () {
|
||||
return Object.create(null);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,8 +3,9 @@ import estraverse from "estraverse";
|
||||
import * as acorn from "../../acorn";
|
||||
|
||||
export default function (code, opts = {}) {
|
||||
var comments = [];
|
||||
var tokens = [];
|
||||
var commentsAndTokens = [];
|
||||
var comments = [];
|
||||
var tokens = [];
|
||||
|
||||
var parseOpts = {
|
||||
allowImportExportEverywhere: opts.looseModules,
|
||||
@@ -14,21 +15,38 @@ export default function (code, opts = {}) {
|
||||
strictMode: opts.strictMode,
|
||||
sourceType: opts.sourceType,
|
||||
locations: true,
|
||||
onComment: comments,
|
||||
features: opts.features || {},
|
||||
plugins: opts.plugins || {},
|
||||
onToken: tokens,
|
||||
ranges: true
|
||||
};
|
||||
|
||||
parseOpts.onToken = function (token) {
|
||||
tokens.push(token);
|
||||
commentsAndTokens.push(token);
|
||||
};
|
||||
|
||||
parseOpts.onComment = function (block, text, start, end, startLoc, endLoc) {
|
||||
var comment = {
|
||||
type: block ? "CommentBlock" : "CommentLine",
|
||||
value: text,
|
||||
start: start,
|
||||
end: end,
|
||||
loc: new acorn.SourceLocation(this, startLoc, endLoc),
|
||||
range: [start, end]
|
||||
};
|
||||
|
||||
commentsAndTokens.push(comment);
|
||||
comments.push(comment);
|
||||
};
|
||||
|
||||
if (opts.nonStandard) {
|
||||
parseOpts.plugins.jsx = true;
|
||||
parseOpts.plugins.flow = true;
|
||||
}
|
||||
|
||||
var ast = acorn.parse(code, parseOpts);
|
||||
|
||||
estraverse.attachComments(ast, comments, tokens);
|
||||
ast = normalizeAst(ast, comments, tokens);
|
||||
ast = normalizeAst(ast, comments, commentsAndTokens);
|
||||
return ast;
|
||||
}
|
||||
|
||||
@@ -7,8 +7,6 @@ export const MESSAGES = {
|
||||
classesIllegalSuperCall: "Direct super call is illegal in non-constructor, use super.$1() instead",
|
||||
classesIllegalConstructorKind: "Illegal kind for constructor method",
|
||||
scopeDuplicateDeclaration: "Duplicate declaration $1",
|
||||
undeclaredVariable: "Reference to undeclared variable $1",
|
||||
undeclaredVariableSuggestion: "Reference to undeclared variable $1 - did you mean $2?",
|
||||
settersInvalidParamLength: "Setters must have exactly one parameter",
|
||||
settersNoRest: "Setters aren't allowed to have a rest",
|
||||
noAssignmentsInForHead: "No assignments allowed in for-in/of head",
|
||||
@@ -22,6 +20,11 @@ export const MESSAGES = {
|
||||
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",
|
||||
illegalMethodName: "Illegal method name $1",
|
||||
lostTrackNodePath: "We lost track of this nodes position, likely because the AST was directly manipulated",
|
||||
|
||||
undeclaredVariable: "Reference to undeclared variable $1",
|
||||
undeclaredVariableType: "Referencing a type alias outside of a type annotation",
|
||||
undeclaredVariableSuggestion: "Reference to undeclared variable $1 - did you mean $2?",
|
||||
|
||||
traverseNeedsParent: "Must pass a scope and parentPath unless traversing a Program/File got a $1 node",
|
||||
traverseVerifyRootFunction: "You passed `traverse()` a function when it expected a visitor object, are you sure you didn't mean `{ enter: Function }`?",
|
||||
|
||||
@@ -17,6 +17,8 @@ var or = types.Type.or;
|
||||
// .build("program")
|
||||
// .field("program", def("Program"));
|
||||
|
||||
def("Noop");
|
||||
|
||||
def("AssignmentPattern")
|
||||
.bases("Pattern")
|
||||
.build("left", "right")
|
||||
|
||||
@@ -83,4 +83,4 @@ export default function (whitelist, outputType = "global") {
|
||||
}
|
||||
|
||||
return generator(tree).code;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,59 +1,5 @@
|
||||
import stripJsonComments from "strip-json-comments";
|
||||
import merge from "../helpers/merge";
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
console.trace("I know someone out there is hotlinking straight to this file. This is a " +
|
||||
"PRIVATE API. I hate you, but I wont break your code just because you're a " +
|
||||
"programmer who practices bad habits.");
|
||||
|
||||
var cache = {};
|
||||
var jsons = {};
|
||||
|
||||
function exists(filename) {
|
||||
if (!fs.existsSync) return false;
|
||||
|
||||
var cached = cache[filename];
|
||||
if (cached != null) return cached;
|
||||
return cache[filename] = fs.existsSync(filename);
|
||||
}
|
||||
|
||||
export default function (loc, opts = {}) {
|
||||
var rel = ".babelrc";
|
||||
|
||||
if (!opts.babelrc) {
|
||||
opts.babelrc = [];
|
||||
}
|
||||
|
||||
function find(start, rel) {
|
||||
var file = path.join(start, rel);
|
||||
|
||||
if (opts.babelrc.indexOf(file) >= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (exists(file)) {
|
||||
var content = fs.readFileSync(file, "utf8");
|
||||
var json;
|
||||
|
||||
try {
|
||||
json = jsons[content] = jsons[content] || JSON.parse(stripJsonComments(content));
|
||||
} catch (err) {
|
||||
err.message = `${file}: ${err.message}`;
|
||||
throw err;
|
||||
}
|
||||
|
||||
opts.babelrc.push(file);
|
||||
|
||||
if (json.breakConfig) return;
|
||||
merge(opts, json);
|
||||
}
|
||||
|
||||
var up = path.dirname(start);
|
||||
if (up !== start) { // root
|
||||
find(up, rel);
|
||||
}
|
||||
}
|
||||
|
||||
if (opts.babelrc.indexOf(loc) < 0 && opts.breakConfig !== true) {
|
||||
find(loc, rel);
|
||||
}
|
||||
|
||||
return opts;
|
||||
};
|
||||
export { default } from "../transformation/file/options/resolve-rc";
|
||||
|
||||
@@ -1,29 +1,27 @@
|
||||
import { validateOption, normaliseOptions, config as optionsConfig } from "./options";
|
||||
import convertSourceMap from "convert-source-map";
|
||||
import * as optionParsers from "./option-parsers";
|
||||
import moduleFormatters from "../modules";
|
||||
import PluginManager from "./plugin-manager";
|
||||
import shebangRegex from "shebang-regex";
|
||||
import TraversalPath from "../../traversal/path";
|
||||
import NodePath from "../../traversal/path";
|
||||
import Transformer from "../transformer";
|
||||
import isFunction from "lodash/lang/isFunction";
|
||||
import isAbsolute from "path-is-absolute";
|
||||
import resolveRc from "../../tools/resolve-rc";
|
||||
import resolveRc from "./options/resolve-rc";
|
||||
import sourceMap from "source-map";
|
||||
import transform from "./../index";
|
||||
import generate from "../../generation";
|
||||
import codeFrame from "../../helpers/code-frame";
|
||||
import defaults from "lodash/object/defaults";
|
||||
import includes from "lodash/collection/includes";
|
||||
import traverse from "../../traversal";
|
||||
import Hub from "../../traversal/hub";
|
||||
import assign from "lodash/object/assign";
|
||||
import Logger from "./logger";
|
||||
import parse from "../../helpers/parse";
|
||||
import Scope from "../../traversal/scope";
|
||||
import merge from "../../helpers/merge";
|
||||
import slash from "slash";
|
||||
import clone from "lodash/lang/clone";
|
||||
import * as util from "../../util";
|
||||
import * as api from "../../api/node";
|
||||
import path from "path";
|
||||
import * as t from "../../types";
|
||||
|
||||
@@ -38,15 +36,27 @@ export default class File {
|
||||
this.declarations = {};
|
||||
this.usedHelpers = {};
|
||||
this.dynamicData = {};
|
||||
this.metadata = {};
|
||||
this.data = {};
|
||||
|
||||
this.metadata = {
|
||||
modules: {
|
||||
imports: [],
|
||||
exports: {
|
||||
exported: [],
|
||||
specifiers: []
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.pipeline = pipeline;
|
||||
this.log = new Logger(this, opts.filename || "unknown");
|
||||
this.opts = this.normalizeOptions(opts);
|
||||
this.ast = {};
|
||||
|
||||
this.normaliseOptions(opts);
|
||||
|
||||
this.buildTransformers();
|
||||
|
||||
this.hub = new Hub(this);
|
||||
}
|
||||
|
||||
static helpers = [
|
||||
@@ -88,19 +98,19 @@ export default class File {
|
||||
|
||||
static soloHelpers = [];
|
||||
|
||||
static options = require("./options");
|
||||
static options = optionsConfig;
|
||||
|
||||
normalizeOptions(opts: Object) {
|
||||
opts = assign({}, opts);
|
||||
normaliseOptions(opts: Object) {
|
||||
opts = this.opts = normaliseOptions(assign({}, opts));
|
||||
|
||||
// resolve babelrc
|
||||
if (opts.filename) {
|
||||
var rcFilename = opts.filename;
|
||||
if (!isAbsolute(rcFilename)) rcFilename = path.join(process.cwd(), rcFilename);
|
||||
opts = resolveRc(rcFilename, opts);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
// check for unknown options
|
||||
for (let key in opts) {
|
||||
if (key[0] === "_") continue;
|
||||
|
||||
@@ -108,26 +118,30 @@ export default class File {
|
||||
if (!option) this.log.error(`Unknown option: ${key}`, ReferenceError);
|
||||
}
|
||||
|
||||
// merge in environment options
|
||||
var envKey = process.env.BABEL_ENV || process.env.NODE_ENV || "development";
|
||||
if (opts.env) merge(opts, opts.env[envKey]);
|
||||
if (opts.env) merge(opts, normaliseOptions(opts.env[envKey]));
|
||||
|
||||
// normalise options
|
||||
for (let key in File.options) {
|
||||
let option = File.options[key];
|
||||
var val = opts[key];
|
||||
|
||||
var val = opts[key];
|
||||
// optional
|
||||
if (!val && option.optional) continue;
|
||||
|
||||
// deprecated
|
||||
if (val && option.deprecated) {
|
||||
throw new Error("Deprecated option " + key + ": " + option.deprecated);
|
||||
this.log.deprecate("Deprecated option " + key + ": " + option.deprecated);
|
||||
}
|
||||
|
||||
if (val == null) {
|
||||
val = clone(option.default);
|
||||
}
|
||||
// default
|
||||
if (val == null) val = clone(option.default);
|
||||
|
||||
var optionParser = optionParsers[option.type];
|
||||
if (optionParser) val = optionParser(key, val, this.pipeline);
|
||||
// validate
|
||||
if (val) val = validateOption(key, val, this.pipeline);
|
||||
|
||||
// aaliases
|
||||
if (option.alias) {
|
||||
opts[option.alias] = opts[option.alias] || val;
|
||||
} else {
|
||||
@@ -152,7 +166,10 @@ export default class File {
|
||||
opts.basename = path.basename(opts.filename, path.extname(opts.filename));
|
||||
|
||||
opts.ignore = util.arrayify(opts.ignore, util.regexify);
|
||||
opts.only = util.arrayify(opts.only, util.regexify);
|
||||
|
||||
if (opts.only) {
|
||||
opts.only = util.arrayify(opts.only, util.regexify);
|
||||
}
|
||||
|
||||
defaults(opts, {
|
||||
moduleRoot: opts.sourceRoot
|
||||
@@ -168,7 +185,7 @@ export default class File {
|
||||
|
||||
defaults(opts, {
|
||||
sourceFileName: opts.filenameRelative,
|
||||
sourceMapName: opts.filenameRelative
|
||||
sourceMapTarget: opts.filenameRelative
|
||||
});
|
||||
|
||||
//
|
||||
@@ -195,7 +212,7 @@ export default class File {
|
||||
// build internal transformers
|
||||
for (var key in this.pipeline.transformers) {
|
||||
var transformer = this.pipeline.transformers[key];
|
||||
var pass = transformers[key] = transformer.buildPass(file);
|
||||
let pass = transformers[key] = transformer.buildPass(file);
|
||||
|
||||
if (pass.canTransform()) {
|
||||
stack.push(pass);
|
||||
@@ -228,7 +245,7 @@ export default class File {
|
||||
this.uncollapsedTransformerStack = stack = stack.concat(secondaryStack);
|
||||
|
||||
// build dependency graph
|
||||
for (var pass of (stack: Array)) {
|
||||
for (let pass of (stack: Array)) {
|
||||
for (var dep of (pass.transformer.dependencies: Array)) {
|
||||
this.transformerDependencies[dep] = pass.key;
|
||||
}
|
||||
@@ -268,7 +285,6 @@ export default class File {
|
||||
}
|
||||
var visitor = traverse.visitors.merge(visitors);
|
||||
var mergeTransformer = new Transformer(group, visitor);
|
||||
//console.log(mergeTransformer);
|
||||
stack.push(mergeTransformer.buildPass(this));
|
||||
}
|
||||
|
||||
@@ -330,14 +346,24 @@ export default class File {
|
||||
}
|
||||
|
||||
attachAuxiliaryComment(node: Object): Object {
|
||||
var comment = this.opts.auxiliaryComment;
|
||||
if (comment) {
|
||||
var beforeComment = this.opts.auxiliaryCommentBefore;
|
||||
if (beforeComment) {
|
||||
node.leadingComments = node.leadingComments || [];
|
||||
node.leadingComments.push({
|
||||
type: "Line",
|
||||
value: " " + comment
|
||||
type: "CommentLine",
|
||||
value: " " + beforeComment
|
||||
});
|
||||
}
|
||||
|
||||
var afterComment = this.opts.auxiliaryCommentAfter;
|
||||
if (afterComment) {
|
||||
node.trailingComments = node.trailingComments || [];
|
||||
node.trailingComments.push({
|
||||
type: "CommentLine",
|
||||
value: " " + afterComment
|
||||
});
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
@@ -348,8 +374,6 @@ export default class File {
|
||||
throw new ReferenceError(`Unknown helper ${name}`);
|
||||
}
|
||||
|
||||
var program = this.ast.program;
|
||||
|
||||
var declar = this.declarations[name];
|
||||
if (declar) return declar;
|
||||
|
||||
@@ -390,9 +414,15 @@ export default class File {
|
||||
}
|
||||
|
||||
errorWithNode(node, msg, Error = SyntaxError) {
|
||||
var loc = node.loc.start;
|
||||
var err = new Error(`Line ${loc.line}: ${msg}`);
|
||||
err.loc = loc;
|
||||
var err;
|
||||
if (node && node.loc) {
|
||||
var loc = node.loc.start;
|
||||
err = new Error(`Line ${loc.line}: ${msg}`);
|
||||
err.loc = loc;
|
||||
} else {
|
||||
// todo: find errors with nodes inside to at least point to something
|
||||
err = new Error("There's been an error on a dynamic node. This is almost certainly an internal error. Please report it.");
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -410,7 +440,7 @@ export default class File {
|
||||
outputMapGenerator.applySourceMap(inputMapConsumer);
|
||||
|
||||
var mergedMap = outputMapGenerator.toJSON();
|
||||
mergedMap.sources = inputMap.sources
|
||||
mergedMap.sources = inputMap.sources;
|
||||
mergedMap.file = inputMap.file;
|
||||
return mergedMap;
|
||||
}
|
||||
@@ -420,6 +450,10 @@ export default class File {
|
||||
|
||||
|
||||
getModuleFormatter(type: string) {
|
||||
if (isFunction(type) || !moduleFormatters[type]) {
|
||||
this.log.deprecate("Custom module formatters are deprecated and will be removed in the next major. Please use Babel plugins instead.");
|
||||
}
|
||||
|
||||
var ModuleFormatter = isFunction(type) ? type : moduleFormatters[type];
|
||||
|
||||
if (!ModuleFormatter) {
|
||||
@@ -463,7 +497,13 @@ export default class File {
|
||||
}
|
||||
|
||||
_addAst(ast) {
|
||||
this.path = TraversalPath.get(null, null, ast, ast, "program", this);
|
||||
this.path = NodePath.get({
|
||||
hub: this.hub,
|
||||
parentPath: null,
|
||||
parent: ast,
|
||||
container: ast,
|
||||
key: "program"
|
||||
}).setContext();
|
||||
this.scope = this.path.scope;
|
||||
this.ast = ast;
|
||||
}
|
||||
@@ -479,12 +519,16 @@ export default class File {
|
||||
modFormatter.init();
|
||||
}
|
||||
this.log.debug("End module formatter init");
|
||||
}
|
||||
|
||||
transform() {
|
||||
this.call("pre");
|
||||
for (var pass of (this.transformerStack: Array)) {
|
||||
pass.transform();
|
||||
}
|
||||
this.call("post");
|
||||
|
||||
return this.generate();
|
||||
}
|
||||
|
||||
wrap(code, callback) {
|
||||
@@ -492,18 +536,10 @@ export default class File {
|
||||
|
||||
try {
|
||||
if (this.shouldIgnore()) {
|
||||
return {
|
||||
metadata: this.metadata,
|
||||
ignored: true,
|
||||
code: code,
|
||||
map: null,
|
||||
ast: null
|
||||
};
|
||||
return this.makeResult({ code, ignored: true });
|
||||
} else {
|
||||
return callback();
|
||||
}
|
||||
|
||||
callback();
|
||||
|
||||
return this.generate();
|
||||
} catch (err) {
|
||||
if (err._babel) {
|
||||
throw err;
|
||||
@@ -532,15 +568,15 @@ export default class File {
|
||||
}
|
||||
}
|
||||
|
||||
addCode(code: string, parseCode?) {
|
||||
addCode(code: string) {
|
||||
code = (code || "") + "";
|
||||
code = this.parseInputSourceMap(code);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
if (parseCode) {
|
||||
this.parseShebang();
|
||||
this.addAst(this.parse(this.code));
|
||||
}
|
||||
parseCode() {
|
||||
this.parseShebang();
|
||||
this.addAst(this.parse(this.code));
|
||||
}
|
||||
|
||||
shouldIgnore() {
|
||||
@@ -577,28 +613,37 @@ export default class File {
|
||||
}
|
||||
}
|
||||
|
||||
generate(): {
|
||||
usedHelpers?: Array<string>;
|
||||
code: string;
|
||||
map?: Object;
|
||||
ast?: Object;
|
||||
} {
|
||||
var opts = this.opts;
|
||||
var ast = this.ast;
|
||||
|
||||
makeResult({ code, map = null, ast, ignored }) {
|
||||
var result = {
|
||||
metadata: this.metadata,
|
||||
code: "",
|
||||
map: null,
|
||||
ast: null
|
||||
metadata: null,
|
||||
ignored: !!ignored,
|
||||
code: null,
|
||||
ast: null,
|
||||
map: map
|
||||
};
|
||||
|
||||
if (this.opts.metadataUsedHelpers) {
|
||||
if (this.opts.code) {
|
||||
result.code = code;
|
||||
}
|
||||
|
||||
if (this.opts.ast) {
|
||||
result.ast = ast;
|
||||
}
|
||||
|
||||
if (this.opts.metadata) {
|
||||
result.metadata = this.metadata;
|
||||
result.metadata.usedHelpers = Object.keys(this.usedHelpers);
|
||||
}
|
||||
|
||||
if (opts.ast) result.ast = ast;
|
||||
if (!opts.code) return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
generate() {
|
||||
var opts = this.opts;
|
||||
var ast = this.ast;
|
||||
|
||||
var result = { ast };
|
||||
if (!opts.code) return this.makeResult(result);
|
||||
|
||||
this.log.debug("Generation start");
|
||||
|
||||
@@ -625,6 +670,6 @@ export default class File {
|
||||
result.map = null;
|
||||
}
|
||||
|
||||
return result;
|
||||
return this.makeResult(result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type File from "./index";
|
||||
import buildDebug from "debug/node";
|
||||
|
||||
var verboseDebug = buildDebug("babel:verbose");
|
||||
@@ -15,6 +16,10 @@ export default class Logger {
|
||||
return parts;
|
||||
}
|
||||
|
||||
warn(msg) {
|
||||
console.warn(this._buildMessage(msg));
|
||||
}
|
||||
|
||||
error(msg: string, Constructor = Error) {
|
||||
throw new Constructor(this._buildMessage(msg));
|
||||
}
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
import * as util from "../../util";
|
||||
|
||||
export function transformerList(key, val, pipeline) {
|
||||
val = util.arrayify(val);
|
||||
|
||||
if (val.indexOf("all") >= 0 || val.indexOf(true) >= 0) {
|
||||
val = Object.keys(pipeline.transformers);
|
||||
}
|
||||
|
||||
return pipeline._ensureTransformerNames(key, val);
|
||||
}
|
||||
|
||||
export function number(key, val) {
|
||||
return +val;
|
||||
}
|
||||
|
||||
export function boolean(key, val) {
|
||||
return !!val;
|
||||
}
|
||||
|
||||
export function booleanString(key, val) {
|
||||
return util.booleanify(val);
|
||||
}
|
||||
|
||||
export function list(key, val) {
|
||||
return util.list(val);
|
||||
}
|
||||
@@ -43,15 +43,17 @@
|
||||
"nonStandard": {
|
||||
"type": "boolean",
|
||||
"default": true,
|
||||
"description": "enable support for JSX and Flow"
|
||||
"description": "enable/disable support for JSX and Flow (on by default)"
|
||||
},
|
||||
|
||||
"experimental": {
|
||||
"deprecated": "use `--stage 0`/`{ stage: 0 }` instead"
|
||||
"type": "boolean",
|
||||
"description": "allow use of experimental transformers",
|
||||
"default": false
|
||||
},
|
||||
|
||||
"highlightCode": {
|
||||
"description": "ANSI syntax highlight code frames",
|
||||
"description": "enable/disable ANSI syntax highlighting of code frames (on by default)",
|
||||
"type": "boolean",
|
||||
"default": true
|
||||
},
|
||||
@@ -76,7 +78,8 @@
|
||||
"blacklist": {
|
||||
"type": "transformerList",
|
||||
"description": "blacklist of transformers to NOT use",
|
||||
"shorthand": "b"
|
||||
"shorthand": "b",
|
||||
"default": []
|
||||
},
|
||||
|
||||
"whitelist": {
|
||||
@@ -88,7 +91,8 @@
|
||||
|
||||
"optional": {
|
||||
"type": "transformerList",
|
||||
"description": "list of optional transformers to enable"
|
||||
"description": "list of optional transformers to enable",
|
||||
"default": []
|
||||
},
|
||||
|
||||
"modules": {
|
||||
@@ -120,12 +124,14 @@
|
||||
|
||||
"plugins": {
|
||||
"type": "list",
|
||||
"description": ""
|
||||
"description": "",
|
||||
"default": []
|
||||
},
|
||||
|
||||
"ignore": {
|
||||
"type": "list",
|
||||
"description": "list of glob paths to **not** compile"
|
||||
"description": "list of glob paths to **not** compile",
|
||||
"default": []
|
||||
},
|
||||
|
||||
"only": {
|
||||
@@ -139,6 +145,12 @@
|
||||
"type": "boolean"
|
||||
},
|
||||
|
||||
"metadata": {
|
||||
"hidden": true,
|
||||
"default": true,
|
||||
"type": "boolean"
|
||||
},
|
||||
|
||||
"ast": {
|
||||
"hidden": true,
|
||||
"default": true,
|
||||
@@ -148,7 +160,7 @@
|
||||
"comments": {
|
||||
"type": "boolean",
|
||||
"default": true,
|
||||
"description": "output comments in generated output"
|
||||
"description": "strip/output comments in generated output (on by default)"
|
||||
},
|
||||
|
||||
"compact": {
|
||||
@@ -165,12 +177,23 @@
|
||||
},
|
||||
|
||||
"auxiliaryComment": {
|
||||
"deprecated": "renamed to auxiliaryCommentBefore",
|
||||
"shorthand": "a",
|
||||
"alias": "auxiliaryCommentBefore"
|
||||
},
|
||||
|
||||
"auxiliaryCommentBefore": {
|
||||
"type": "string",
|
||||
"default": "",
|
||||
"shorthand": "a",
|
||||
"description": "attach a comment before all helper declarations and auxiliary code"
|
||||
},
|
||||
|
||||
"auxiliaryCommentAfter": {
|
||||
"type": "string",
|
||||
"default": "",
|
||||
"description": "attach a comment after all helper declarations and auxiliary code"
|
||||
},
|
||||
|
||||
"externalHelpers": {
|
||||
"type": "boolean",
|
||||
"default": false,
|
||||
@@ -179,6 +202,7 @@
|
||||
},
|
||||
|
||||
"metadataUsedHelpers": {
|
||||
"deprecated": "Not required anymore as this is enabled by default",
|
||||
"type": "boolean",
|
||||
"default": false,
|
||||
"hidden": true
|
||||
@@ -197,6 +221,11 @@
|
||||
},
|
||||
|
||||
"sourceMapName": {
|
||||
"alias": "sourceMapTarget",
|
||||
"description": "DEPRECATED - Please use sourceMapTarget"
|
||||
},
|
||||
|
||||
"sourceMapTarget": {
|
||||
"type": "string",
|
||||
"description": "set `file` on returned source map"
|
||||
},
|
||||
31
src/babel/transformation/file/options/index.js
Normal file
31
src/babel/transformation/file/options/index.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import * as parsers from "./parsers";
|
||||
import config from "./config";
|
||||
|
||||
export { config };
|
||||
|
||||
export function validateOption(key, val, pipeline) {
|
||||
var opt = config[key];
|
||||
var parser = opt && parsers[opt.type];
|
||||
if (parser && parser.validate) {
|
||||
return parser.validate(key, val, pipeline);
|
||||
} else {
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
export function normaliseOptions(options = {}) {
|
||||
for (var key in options) {
|
||||
var val = options[key];
|
||||
if (val == null) continue;
|
||||
|
||||
var opt = config[key];
|
||||
if (!opt) continue;
|
||||
|
||||
var parser = parsers[opt.type];
|
||||
if (parser) val = parser(val);
|
||||
|
||||
options[key] = val;
|
||||
}
|
||||
|
||||
return options;
|
||||
}
|
||||
29
src/babel/transformation/file/options/parsers.js
Normal file
29
src/babel/transformation/file/options/parsers.js
Normal file
@@ -0,0 +1,29 @@
|
||||
import * as util from "../../../util";
|
||||
|
||||
export function transformerList(val) {
|
||||
return util.arrayify(val);
|
||||
}
|
||||
|
||||
transformerList.validate = function (key, val, pipeline) {
|
||||
if (val.indexOf("all") >= 0 || val.indexOf(true) >= 0) {
|
||||
val = Object.keys(pipeline.transformers);
|
||||
}
|
||||
|
||||
return pipeline._ensureTransformerNames(key, val);
|
||||
};
|
||||
|
||||
export function number(val) {
|
||||
return +val;
|
||||
}
|
||||
|
||||
export function boolean(val) {
|
||||
return !!val;
|
||||
}
|
||||
|
||||
export function booleanString(val) {
|
||||
return util.booleanify(val);
|
||||
}
|
||||
|
||||
export function list(val) {
|
||||
return util.list(val);
|
||||
}
|
||||
62
src/babel/transformation/file/options/resolve-rc.js
Normal file
62
src/babel/transformation/file/options/resolve-rc.js
Normal file
@@ -0,0 +1,62 @@
|
||||
import stripJsonComments from "strip-json-comments";
|
||||
import { normaliseOptions } from "./index";
|
||||
import merge from "../../../helpers/merge";
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
|
||||
var cache = {};
|
||||
var jsons = {};
|
||||
|
||||
function exists(filename) {
|
||||
if (!fs.existsSync) return false;
|
||||
|
||||
var cached = cache[filename];
|
||||
if (cached != null) return cached;
|
||||
return cache[filename] = fs.existsSync(filename);
|
||||
}
|
||||
|
||||
export default function (loc, opts = {}) {
|
||||
var rel = ".babelrc";
|
||||
|
||||
if (!opts.babelrc) {
|
||||
opts.babelrc = [];
|
||||
}
|
||||
|
||||
function find(start, rel) {
|
||||
var file = path.join(start, rel);
|
||||
|
||||
if (opts.babelrc.indexOf(file) >= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (exists(file)) {
|
||||
var content = fs.readFileSync(file, "utf8");
|
||||
var json;
|
||||
|
||||
try {
|
||||
json = jsons[content] = jsons[content] || JSON.parse(stripJsonComments(content));
|
||||
normaliseOptions(json);
|
||||
} catch (err) {
|
||||
err.message = `${file}: ${err.message}`;
|
||||
throw err;
|
||||
}
|
||||
|
||||
opts.babelrc.push(file);
|
||||
|
||||
if (json.breakConfig) return;
|
||||
|
||||
merge(opts, json);
|
||||
}
|
||||
|
||||
var up = path.dirname(start);
|
||||
if (up !== start) { // root
|
||||
find(up, rel);
|
||||
}
|
||||
}
|
||||
|
||||
if (opts.babelrc.indexOf(loc) < 0 && opts.breakConfig !== true) {
|
||||
find(loc, rel);
|
||||
}
|
||||
|
||||
return opts;
|
||||
}
|
||||
@@ -35,11 +35,12 @@ export default class PluginManager {
|
||||
var match = name.match(/^(.*?):(after|before)$/);
|
||||
if (match) [, name, position] = match;
|
||||
|
||||
var loc = util.resolveRelative(name) || util.resolveRelative(`babel-plugin-${name}`);
|
||||
var loc = util.resolveRelative(`babel-plugin-${name}`) || util.resolveRelative(name);
|
||||
if (loc) {
|
||||
var plugin = require(loc);
|
||||
return {
|
||||
position: position,
|
||||
plugin: require(loc)
|
||||
plugin: plugin.default || plugin
|
||||
};
|
||||
} else {
|
||||
throw new ReferenceError(messages.get("pluginUnknown", name));
|
||||
|
||||
@@ -44,4 +44,4 @@ export default function (opts) {
|
||||
};
|
||||
|
||||
return exports;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -42,4 +42,4 @@ export default function (exports, opts) {
|
||||
|
||||
return nodes;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import * as react from "./react";
|
||||
import * as t from "../../types";
|
||||
|
||||
export default function (exports, opts) {
|
||||
exports.JSXIdentifier = function (node, parent) {
|
||||
exports.JSXIdentifier = function (node) {
|
||||
if (node.name === "this" && this.isReferenced()) {
|
||||
return t.thisExpression();
|
||||
} else if (esutils.keyword.isIdentifierNameES6(node.name)) {
|
||||
@@ -20,7 +20,7 @@ export default function (exports, opts) {
|
||||
}
|
||||
};
|
||||
|
||||
exports.JSXNamespacedName = function (node, parent, scope, file) {
|
||||
exports.JSXNamespacedName = function () {
|
||||
throw this.errorWithNode(messages.get("JSXNamespacedTags"));
|
||||
};
|
||||
|
||||
@@ -161,7 +161,8 @@ export default function (exports, opts) {
|
||||
|
||||
for (var i = 0; i < props.length; i++) {
|
||||
var prop = props[i];
|
||||
if (t.isIdentifier(prop.key, { name: "displayName" })) {
|
||||
var key = t.toComputedKey(prop);
|
||||
if (t.isLiteral(key, { value: "displayName" })) {
|
||||
safe = false;
|
||||
break;
|
||||
}
|
||||
@@ -202,4 +203,4 @@ export default function (exports, opts) {
|
||||
addDisplayName(left.name, right);
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import traverse from "../../traversal";
|
||||
import * as t from "../../types";
|
||||
|
||||
var visitor = {
|
||||
@@ -7,10 +6,10 @@ var visitor = {
|
||||
state.found = true;
|
||||
this.stop();
|
||||
}
|
||||
},
|
||||
|
||||
if (this.isFunction()) {
|
||||
this.skip();
|
||||
}
|
||||
Function() {
|
||||
this.skip();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import cloneDeep from "lodash/lang/cloneDeep";
|
||||
import traverse from "../../traversal";
|
||||
import each from "lodash/collection/each";
|
||||
import has from "lodash/object/has";
|
||||
import * as t from "../../types";
|
||||
|
||||
@@ -68,4 +68,4 @@ export default function (node, nodes, file, scope, allowedSingleIdent) {
|
||||
uid: uid,
|
||||
ref: ref
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,7 +3,10 @@ import * as t from "../../types";
|
||||
export default function (node) {
|
||||
var lastNonDefault = 0;
|
||||
for (var i = 0; i < node.params.length; i++) {
|
||||
if (!t.isAssignmentPattern(node.params[i])) lastNonDefault = i + 1;
|
||||
var param = node.params[i];
|
||||
if (!t.isAssignmentPattern(param) && !t.isRestElement(param)) {
|
||||
lastNonDefault = i + 1;
|
||||
}
|
||||
}
|
||||
return lastNonDefault;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2,25 +2,35 @@ import getFunctionArity from "./get-function-arity";
|
||||
import * as util from "../../util";
|
||||
import * as t from "../../types";
|
||||
|
||||
function visitIdentifier(context, node, scope, state) {
|
||||
// check if this node matches our function id
|
||||
if (node.name !== state.name) return;
|
||||
|
||||
// check that we don't have a local variable declared as that removes the need
|
||||
// for the wrapper
|
||||
var localDeclar = scope.getBindingIdentifier(state.name);
|
||||
if (localDeclar !== state.outerDeclar) return;
|
||||
|
||||
state.selfReference = true;
|
||||
context.stop();
|
||||
}
|
||||
|
||||
var visitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
// check if this node is a referenced identifier that matches the same as our
|
||||
// function id
|
||||
if (!this.isReferencedIdentifier({ name: state.name })) return;
|
||||
ReferencedIdentifier(node, parent, scope, state) {
|
||||
visitIdentifier(this, node, scope, state);
|
||||
},
|
||||
|
||||
// check that we don't have a local variable declared as that removes the need
|
||||
// for the wrapper
|
||||
var localDeclar = scope.getBindingIdentifier(state.name);
|
||||
if (localDeclar !== state.outerDeclar) return;
|
||||
|
||||
state.selfReference = true;
|
||||
this.stop();
|
||||
AssignmentExpression(node, parent, scope, state) {
|
||||
var ids = this.getBindingIdentifiers();
|
||||
for (var name in ids) {
|
||||
visitIdentifier(this, ids[name], scope, state);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var wrap = function (state, method, id, scope) {
|
||||
if (state.selfReference) {
|
||||
if (scope.hasBinding(id.name)) {
|
||||
if (scope.hasBinding(id.name) && !scope.hasGlobal(id.name)) {
|
||||
// we can just munge the local binding
|
||||
scope.rename(id.name);
|
||||
} else {
|
||||
@@ -46,6 +56,7 @@ var wrap = function (state, method, id, scope) {
|
||||
}
|
||||
|
||||
method.id = id;
|
||||
scope.getProgramParent().references[id.name] = true;
|
||||
};
|
||||
|
||||
var visit = function (node, name, scope) {
|
||||
|
||||
5
src/babel/transformation/helpers/react.js
vendored
5
src/babel/transformation/helpers/react.js
vendored
@@ -1,4 +1,3 @@
|
||||
import isString from "lodash/lang/isString";
|
||||
import * as t from "../../types";
|
||||
|
||||
var isCreateClassCallExpression = t.buildMatchMemberExpression("React.createClass");
|
||||
@@ -26,10 +25,6 @@ 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/);
|
||||
|
||||
|
||||
@@ -1,35 +1,37 @@
|
||||
import * as t from "../../types";
|
||||
|
||||
var awaitVisitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isFunction(node)) this.skip();
|
||||
Function() {
|
||||
this.skip();
|
||||
},
|
||||
|
||||
if (t.isAwaitExpression(node)) {
|
||||
node.type = "YieldExpression";
|
||||
AwaitExpression(node) {
|
||||
node.type = "YieldExpression";
|
||||
|
||||
if (node.all) {
|
||||
// await* foo; -> yield Promise.all(foo);
|
||||
node.all = false;
|
||||
node.argument = t.callExpression(t.memberExpression(t.identifier("Promise"), t.identifier("all")), [node.argument]);
|
||||
}
|
||||
if (node.all) {
|
||||
// await* foo; -> yield Promise.all(foo);
|
||||
node.all = false;
|
||||
node.argument = t.callExpression(t.memberExpression(t.identifier("Promise"), t.identifier("all")), [node.argument]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var referenceVisitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
ReferencedIdentifier(node, parent, scope, state) {
|
||||
var name = state.id.name;
|
||||
if (this.isReferencedIdentifier({ name: name }) && scope.bindingIdentifierEquals(name, state.id)) {
|
||||
if (node.name === name && scope.bindingIdentifierEquals(name, state.id)) {
|
||||
return state.ref = state.ref || scope.generateUidIdentifier(name);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default function (node, callId, scope) {
|
||||
export default function (path, callId) {
|
||||
var node = path.node;
|
||||
|
||||
node.async = false;
|
||||
node.generator = true;
|
||||
|
||||
scope.traverse(node, awaitVisitor, state);
|
||||
path.traverse(awaitVisitor, state);
|
||||
|
||||
var call = t.callExpression(callId, [node]);
|
||||
|
||||
@@ -44,15 +46,15 @@ export default function (node, callId, scope) {
|
||||
return declar;
|
||||
} else {
|
||||
if (id) {
|
||||
var state = { id: id };
|
||||
scope.traverse(node, referenceVisitor, state);
|
||||
var state = { id };
|
||||
path.traverse(referenceVisitor, state);
|
||||
|
||||
if (state.ref) {
|
||||
scope.parent.push({ id: state.ref });
|
||||
path.scope.parent.push({ id: state.ref });
|
||||
return t.assignmentExpression("=", state.ref, call);
|
||||
}
|
||||
}
|
||||
|
||||
return call;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type NodePath from "../../traversal/path";
|
||||
import * as messages from "../../messages";
|
||||
import * as t from "../../types";
|
||||
|
||||
@@ -130,7 +131,7 @@ export default class ReplaceSupers {
|
||||
* Description
|
||||
*/
|
||||
|
||||
traverseLevel(path: TraversalPath, topLevel: boolean) {
|
||||
traverseLevel(path: NodePath, topLevel: boolean) {
|
||||
var state = { self: this, topLevel: topLevel };
|
||||
path.traverse(visitor, state);
|
||||
}
|
||||
@@ -192,7 +193,7 @@ export default class ReplaceSupers {
|
||||
* Description
|
||||
*/
|
||||
|
||||
looseHandle(path: TraversalPath, getThisReference: Function) {
|
||||
looseHandle(path: NodePath, getThisReference: Function) {
|
||||
var node = path.node;
|
||||
if (path.isSuper()) {
|
||||
return this.getLooseSuperProperty(node, path.parent);
|
||||
@@ -234,7 +235,7 @@ export default class ReplaceSupers {
|
||||
* Description
|
||||
*/
|
||||
|
||||
specHandle(path: TraversalPath, getThisReference: Function) {
|
||||
specHandle(path: NodePath, getThisReference: Function) {
|
||||
var methodNode = this.methodNode;
|
||||
var property;
|
||||
var computed;
|
||||
@@ -274,7 +275,7 @@ export default class ReplaceSupers {
|
||||
property = node.property;
|
||||
computed = node.computed;
|
||||
} else if (t.isUpdateExpression(node) && isMemberExpressionSuper(node.argument)) {
|
||||
var binary = t.binaryExpression(node.operator[0], node.argument, t.literal(1))
|
||||
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);
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
import * as t from "../../types";
|
||||
|
||||
export function has(node) {
|
||||
var first = node.body[0];
|
||||
return t.isExpressionStatement(first) && t.isLiteral(first.expression, { value: "use strict" });
|
||||
}
|
||||
|
||||
export function wrap(node, callback) {
|
||||
var useStrictNode;
|
||||
if (has(node)) {
|
||||
useStrictNode = node.body.shift();
|
||||
}
|
||||
|
||||
callback();
|
||||
|
||||
if (useStrictNode) {
|
||||
node.body.unshift(useStrictNode);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
import * as messages from "../../messages";
|
||||
import traverse from "../../traversal";
|
||||
import extend from "lodash/object/extend";
|
||||
import object from "../../helpers/object";
|
||||
import * as util from "../../util";
|
||||
import * as t from "../../types";
|
||||
|
||||
var remapVisitor = {
|
||||
enter(node, parent, scope, formatter) {
|
||||
enter(node) {
|
||||
if (node._skipModulesRemap) {
|
||||
return this.skip();
|
||||
}
|
||||
@@ -69,46 +68,158 @@ var remapVisitor = {
|
||||
}
|
||||
};
|
||||
|
||||
var importsVisitor = {
|
||||
var metadataVisitor = {
|
||||
ModuleDeclaration: {
|
||||
enter(node, parent, scope, formatter) {
|
||||
if (node.source) {
|
||||
node.source.value = formatter.file.resolveModuleSource(node.source.value);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
ImportDeclaration: {
|
||||
enter(node, parent, scope, formatter) {
|
||||
exit(node, parent, scope, formatter) {
|
||||
formatter.hasLocalImports = true;
|
||||
extend(formatter.localImports, this.getBindingIdentifiers());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var exportsVisitor = {
|
||||
ExportDeclaration: {
|
||||
enter(node, parent, scope, formatter) {
|
||||
formatter.hasLocalExports = true;
|
||||
var specifiers = [];
|
||||
var imported = [];
|
||||
formatter.metadata.imports.push({
|
||||
source: node.source.value,
|
||||
imported,
|
||||
specifiers
|
||||
});
|
||||
|
||||
var declar = this.get("declaration");
|
||||
if (declar.isStatement()) {
|
||||
var bindings = declar.getBindingIdentifiers()
|
||||
for (var name in bindings) {
|
||||
var binding = bindings[name];
|
||||
formatter._addExport(name, binding);
|
||||
for (var specifier of (this.get("specifiers"): Array)) {
|
||||
var ids = specifier.getBindingIdentifiers();
|
||||
extend(formatter.localImports, ids);
|
||||
|
||||
var local = specifier.node.local.name;
|
||||
|
||||
if (specifier.isImportDefaultSpecifier()) {
|
||||
imported.push("default");
|
||||
specifiers.push({
|
||||
kind: "named",
|
||||
imported: "default",
|
||||
local
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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 (specifier.isImportSpecifier()) {
|
||||
var importedName = specifier.node.imported.name;
|
||||
imported.push(importedName);
|
||||
specifiers.push({
|
||||
kind: "named",
|
||||
imported: importedName,
|
||||
local
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (!t.isExportDefaultDeclaration(node)) {
|
||||
var onlyDefault = node.specifiers && node.specifiers.length === 1 && t.isSpecifierDefault(node.specifiers[0]);
|
||||
if (!onlyDefault) {
|
||||
formatter.hasNonDefaultExports = true;
|
||||
if (specifier.isImportNamespaceSpecifier()) {
|
||||
imported.push("*");
|
||||
specifiers.push({
|
||||
kind: "namespace",
|
||||
local
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
ExportDeclaration(node, parent, scope, formatter) {
|
||||
formatter.hasLocalExports = true;
|
||||
|
||||
var source = node.source ? node.source.value : null;
|
||||
var exports = formatter.metadata.exports;
|
||||
|
||||
// export function foo() {}
|
||||
// export var foo = "bar";
|
||||
var declar = this.get("declaration");
|
||||
if (declar.isStatement()) {
|
||||
var bindings = declar.getBindingIdentifiers();
|
||||
|
||||
for (var name in bindings) {
|
||||
var binding = bindings[name];
|
||||
formatter._addExport(name, binding);
|
||||
|
||||
exports.exported.push(name);
|
||||
exports.specifiers.push({
|
||||
kind: "local",
|
||||
local: name,
|
||||
exported: this.isExportDefaultDeclaration() ? "default" : name
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (this.isExportNamedDeclaration() && node.specifiers) {
|
||||
for (var specifier of (node.specifiers: Array)) {
|
||||
var exported = specifier.exported.name;
|
||||
exports.exported.push(exported);
|
||||
|
||||
// export foo from "bar";
|
||||
if (t.isExportDefaultSpecifier(specifier)) {
|
||||
exports.specifiers.push({
|
||||
kind: "external",
|
||||
local: exported,
|
||||
exported,
|
||||
source
|
||||
});
|
||||
}
|
||||
|
||||
// export * as foo from "bar";
|
||||
if (t.isExportNamespaceSpecifier(specifier)) {
|
||||
exports.specifiers.push({
|
||||
kind: "external-namespace",
|
||||
exported,
|
||||
source
|
||||
});
|
||||
}
|
||||
|
||||
var local = specifier.local;
|
||||
if (!local) continue;
|
||||
|
||||
formatter._addExport(local.name, specifier.exported);
|
||||
|
||||
// export { foo } from "bar";
|
||||
// export { foo as bar } from "bar";
|
||||
if (source) {
|
||||
exports.specifiers.push({
|
||||
kind: "external",
|
||||
local: local.name,
|
||||
exported,
|
||||
source
|
||||
});
|
||||
}
|
||||
|
||||
// export { foo };
|
||||
// export { foo as bar };
|
||||
if (!source) {
|
||||
exports.specifiers.push({
|
||||
kind: "local",
|
||||
local: local.name,
|
||||
exported
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// export * from "bar";
|
||||
if (this.isExportAllDeclaration()) {
|
||||
exports.specifiers.push({
|
||||
kind: "external-all",
|
||||
source
|
||||
});
|
||||
}
|
||||
|
||||
if (!t.isExportDefaultDeclaration(node) && !declar.isTypeAlias()) {
|
||||
var onlyDefault = node.specifiers && node.specifiers.length === 1 && t.isSpecifierDefault(node.specifiers[0]);
|
||||
if (!onlyDefault) {
|
||||
formatter.hasNonDefaultExports = true;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
Scope() {
|
||||
this.skip();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -128,8 +239,8 @@ export default class DefaultFormatter {
|
||||
this.localExports = object();
|
||||
this.localImports = object();
|
||||
|
||||
this.getLocalExports();
|
||||
this.getLocalImports();
|
||||
this.metadata = file.metadata.modules;
|
||||
this.getMetadata();
|
||||
}
|
||||
|
||||
isModuleType(node, type) {
|
||||
@@ -145,12 +256,15 @@ export default class DefaultFormatter {
|
||||
return (t.isExportDefaultDeclaration(node) || t.isSpecifierDefault(node)) && !this.noInteropRequireExport && !this.hasNonDefaultExports;
|
||||
}
|
||||
|
||||
getLocalExports() {
|
||||
this.file.path.traverse(exportsVisitor, this);
|
||||
}
|
||||
|
||||
getLocalImports() {
|
||||
this.file.path.traverse(importsVisitor, this);
|
||||
getMetadata() {
|
||||
var has = false;
|
||||
for (var node of (this.file.ast.program.body: Array)) {
|
||||
if (t.isModuleDeclaration(node)) {
|
||||
has = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (has) this.file.path.traverse(metadataVisitor, this);
|
||||
}
|
||||
|
||||
remapAssignments() {
|
||||
|
||||
@@ -10,4 +10,4 @@ export default function (Parent) {
|
||||
util.inherits(Constructor, Parent);
|
||||
|
||||
return Constructor;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ import * as util from "../../util";
|
||||
import * as t from "../../types";
|
||||
|
||||
export default class AMDFormatter extends DefaultFormatter {
|
||||
init() {
|
||||
CommonFormatter.prototype._init.call(this, this.hasNonDefaultExports);
|
||||
setup() {
|
||||
CommonFormatter.prototype._setup.call(this, this.hasNonDefaultExports);
|
||||
}
|
||||
|
||||
buildDependencyLiterals() {
|
||||
@@ -92,9 +92,9 @@ export default class AMDFormatter extends DefaultFormatter {
|
||||
// import foo from "foo";
|
||||
var uid = this.scope.generateUidIdentifier(specifier.local.name);
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(uid, t.callExpression(this.file.addHelper("interop-require"), [ref]))
|
||||
t.variableDeclarator(uid, t.callExpression(this.file.addHelper("interop-require-default"), [ref]))
|
||||
]));
|
||||
ref = uid;
|
||||
ref = t.memberExpression(uid, t.identifier("default"));
|
||||
} else {
|
||||
// import { foo } from "foo";
|
||||
var imported = specifier.imported;
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
import DefaultFormatter from "./_default";
|
||||
import includes from "lodash/collection/includes";
|
||||
import * as util from "../../util";
|
||||
import * as t from "../../types";
|
||||
|
||||
export default class CommonJSFormatter extends DefaultFormatter {
|
||||
init() {
|
||||
this._init(this.hasLocalExports);
|
||||
setup() {
|
||||
this._setup(this.hasLocalExports);
|
||||
}
|
||||
|
||||
_init(conditional) {
|
||||
_setup(conditional) {
|
||||
var file = this.file;
|
||||
var scope = file.scope;
|
||||
|
||||
@@ -84,7 +83,7 @@ export default class CommonJSFormatter extends DefaultFormatter {
|
||||
}, true));
|
||||
}
|
||||
|
||||
exportSpecifier(specifier, node, nodes) {
|
||||
exportSpecifier(specifier) {
|
||||
if (this.doDefaultExportInterop(specifier)) {
|
||||
this.hasDefaultOnlyExport = true;
|
||||
}
|
||||
@@ -92,7 +91,7 @@ export default class CommonJSFormatter extends DefaultFormatter {
|
||||
DefaultFormatter.prototype.exportSpecifier.apply(this, arguments);
|
||||
}
|
||||
|
||||
exportDeclaration(node, nodes) {
|
||||
exportDeclaration(node) {
|
||||
if (this.doDefaultExportInterop(node)) {
|
||||
this.hasDefaultOnlyExport = true;
|
||||
}
|
||||
@@ -101,8 +100,6 @@ export default class CommonJSFormatter extends DefaultFormatter {
|
||||
}
|
||||
|
||||
_getExternalReference(node, nodes) {
|
||||
var source = node.source.value;
|
||||
|
||||
var call = t.callExpression(t.identifier("require"), [node.source]);
|
||||
var uid;
|
||||
|
||||
|
||||
@@ -3,56 +3,55 @@ import AMDFormatter from "./amd";
|
||||
import object from "../../helpers/object";
|
||||
import * as util from "../../util";
|
||||
import last from "lodash/array/last";
|
||||
import each from "lodash/collection/each";
|
||||
import map from "lodash/collection/map";
|
||||
import * as t from "../../types";
|
||||
|
||||
var hoistVariablesVisitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isFunction(node)) {
|
||||
// nothing inside is accessible
|
||||
return this.skip();
|
||||
Function() {
|
||||
// nothing inside is accessible
|
||||
this.skip();
|
||||
},
|
||||
|
||||
VariableDeclaration(node, parent, scope, state) {
|
||||
if (node.kind !== "var" && !t.isProgram(parent)) { // let, const
|
||||
// can't be accessed
|
||||
return;
|
||||
}
|
||||
|
||||
if (t.isVariableDeclaration(node)) {
|
||||
if (node.kind !== "var" && !t.isProgram(parent)) { // let, const
|
||||
// can't be accessed
|
||||
return;
|
||||
// ignore block hoisted nodes as these can be left in
|
||||
if (state.formatter._canHoist(node)) return;
|
||||
|
||||
var nodes = [];
|
||||
|
||||
for (var i = 0; i < node.declarations.length; i++) {
|
||||
var declar = node.declarations[i];
|
||||
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));
|
||||
nodes.push(assign);
|
||||
}
|
||||
|
||||
// ignore block hoisted nodes as these can be left in
|
||||
if (state.formatter._canHoist(node)) return;
|
||||
|
||||
var nodes = [];
|
||||
|
||||
for (var i = 0; i < node.declarations.length; i++) {
|
||||
var declar = node.declarations[i];
|
||||
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));
|
||||
nodes.push(assign);
|
||||
}
|
||||
}
|
||||
|
||||
// for (var i in test)
|
||||
// for (var i = 0;;)
|
||||
if (t.isFor(parent) && parent.left === node) {
|
||||
return node.declarations[0].id;
|
||||
}
|
||||
|
||||
return nodes;
|
||||
}
|
||||
|
||||
// for (var i in test)
|
||||
// for (var i = 0;;)
|
||||
if (t.isFor(parent) && (parent.left === node || parent.init === node)) {
|
||||
return node.declarations[0].id;
|
||||
}
|
||||
|
||||
return nodes;
|
||||
}
|
||||
};
|
||||
|
||||
var hoistFunctionsVisitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isFunction(node)) this.skip();
|
||||
Function() {
|
||||
this.skip();
|
||||
},
|
||||
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isFunctionDeclaration(node) || state.formatter._canHoist(node)) {
|
||||
state.handlerBody.push(node);
|
||||
this.remove();
|
||||
this.dangerouslyRemove();
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -71,7 +70,7 @@ var runnerSettersVisitor = {
|
||||
state.nodes.push(node);
|
||||
}
|
||||
|
||||
this.remove();
|
||||
this.dangerouslyRemove();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
if (VARIABLE_NAME === undefined) VARIABLE_NAME = DEFAULT_VALUE;
|
||||
@@ -1,5 +1,6 @@
|
||||
import includes from "lodash/collection/includes";
|
||||
import type Transformer from "./transformer";
|
||||
import traverse from "../traversal";
|
||||
import type File from "./file";
|
||||
|
||||
/**
|
||||
* This class is responsible for traversing over the provided `File`s
|
||||
@@ -11,8 +12,13 @@ export default class TransformerPass {
|
||||
this.transformer = transformer;
|
||||
this.handlers = transformer.handlers;
|
||||
this.file = file;
|
||||
this.ran = false;
|
||||
this.key = transformer.key;
|
||||
|
||||
if (this.canTransform() && transformer.metadata.experimental && !file.opts.experimental) {
|
||||
file.log.warn(`THE TRANSFORMER ${this.key} HAS BEEN MARKED AS EXPERIMENTAL AND IS WIP. USE AT YOUR OWN RISK. ` +
|
||||
"THIS WILL HIGHLY LIKELY BREAK YOUR CODE SO USE WITH **EXTREME** CAUTION. ENABLE THE " +
|
||||
"`experimental` OPTION TO IGNORE THIS WARNING.");
|
||||
}
|
||||
}
|
||||
|
||||
canTransform(): boolean {
|
||||
@@ -22,13 +28,8 @@ export default class TransformerPass {
|
||||
|
||||
transform() {
|
||||
var file = this.file;
|
||||
|
||||
file.log.debug(`Start transformer ${this.key}`);
|
||||
|
||||
traverse(file.ast, this.handlers, file.scope, file);
|
||||
|
||||
file.log.debug(`Finish transformer ${this.key}`);
|
||||
|
||||
this.ran = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,10 +57,21 @@ export default class TransformerPipeline {
|
||||
return true;
|
||||
}
|
||||
|
||||
pretransform(code: string, opts?: Object) {
|
||||
var file = new File(opts, this);
|
||||
return file.wrap(code, function () {
|
||||
file.addCode(code);
|
||||
file.parseCode(code);
|
||||
return file;
|
||||
});
|
||||
}
|
||||
|
||||
transform(code: string, opts?: Object) {
|
||||
var file = new File(opts, this);
|
||||
return file.wrap(code, function () {
|
||||
file.addCode(code, true);
|
||||
file.addCode(code);
|
||||
file.parseCode(code);
|
||||
return file.transform();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -71,6 +82,7 @@ export default class TransformerPipeline {
|
||||
return file.wrap(code, function () {
|
||||
file.addCode(code);
|
||||
file.addAst(ast);
|
||||
return file.transform();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import isFunction from "lodash/lang/isFunction";
|
||||
import traverse from "../traversal";
|
||||
import isObject from "lodash/lang/isObject";
|
||||
import assign from "lodash/object/assign";
|
||||
import * as acorn from "../../acorn";
|
||||
import File from "./file";
|
||||
import each from "lodash/collection/each";
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
{
|
||||
"useStrict": "strict",
|
||||
"es5.runtime": "runtime",
|
||||
"es6.runtime": "runtime"
|
||||
"es6.runtime": "runtime",
|
||||
"minification.inlineExpressions": "minification.constantFolding"
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
"es6.symbols": "es6.spec.symbols",
|
||||
"es6.blockScopingTDZ": "es6.spec.blockScoping",
|
||||
|
||||
"utility.inlineExpressions": "minification.inlineExpressions",
|
||||
"utility.inlineExpressions": "minification.constantFolding",
|
||||
"utility.deadCodeElimination": "minification.deadCodeElimination",
|
||||
"utility.removeConsoleCalls": "minification.removeConsole",
|
||||
"utility.removeDebugger": "minification.removeDebugger"
|
||||
|
||||
@@ -2,12 +2,19 @@ import * as defineMap from "../../helpers/define-map";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export function ObjectExpression(node, parent, scope, file) {
|
||||
var mutatorMap = {};
|
||||
var hasAny = false;
|
||||
for (var prop of (node.properties: Array)) {
|
||||
if (prop.kind === "get" || prop.kind === "set") {
|
||||
hasAny = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!hasAny) return;
|
||||
|
||||
var mutatorMap = {};
|
||||
|
||||
node.properties = node.properties.filter(function (prop) {
|
||||
if (prop.kind === "get" || prop.kind === "set") {
|
||||
hasAny = true;
|
||||
defineMap.push(mutatorMap, prop, prop.kind, file);
|
||||
return false;
|
||||
} else {
|
||||
@@ -15,8 +22,6 @@ export function ObjectExpression(node, parent, scope, file) {
|
||||
}
|
||||
});
|
||||
|
||||
if (!hasAny) return;
|
||||
|
||||
return t.callExpression(
|
||||
t.memberExpression(t.identifier("Object"), t.identifier("defineProperties")),
|
||||
[node, defineMap.toDefineObject(mutatorMap)]
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import type NodePath from "../../../traversal/path";
|
||||
import type Scope from "../../../traversal/scope";
|
||||
import type File from "../../file";
|
||||
import traverse from "../../../traversal";
|
||||
import object from "../../../helpers/object";
|
||||
import * as util from "../../../util";
|
||||
@@ -84,8 +87,6 @@ export function BlockStatement(block, parent, scope, file) {
|
||||
export { BlockStatement as Program };
|
||||
|
||||
function replace(node, parent, scope, remaps) {
|
||||
if (!t.isReferencedIdentifier(node, parent)) return;
|
||||
|
||||
var remap = remaps[node.name];
|
||||
if (!remap) return;
|
||||
|
||||
@@ -100,11 +101,28 @@ function replace(node, parent, scope, remaps) {
|
||||
}
|
||||
|
||||
var replaceVisitor = {
|
||||
enter: replace
|
||||
ReferencedIdentifier: replace,
|
||||
|
||||
AssignmentExpression(node, parent, scope, remaps) {
|
||||
var ids = this.getBindingIdentifiers();
|
||||
for (var name in ids) {
|
||||
replace(ids[name], node, scope, remaps);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
function traverseReplace(node, parent, scope, remaps) {
|
||||
replace(node, parent, scope, remaps);
|
||||
if (t.isIdentifier(node)) {
|
||||
replace(node, parent, scope, remaps);
|
||||
}
|
||||
|
||||
if (t.isAssignmentExpression(node)) {
|
||||
var ids = t.getBindingIdentifiers(node);
|
||||
for (var name in ids) {
|
||||
replace(ids[name], parent, scope, remaps);
|
||||
}
|
||||
}
|
||||
|
||||
scope.traverse(node, replaceVisitor, remaps);
|
||||
}
|
||||
|
||||
@@ -250,7 +268,7 @@ class BlockScoping {
|
||||
* Description
|
||||
*/
|
||||
|
||||
constructor(loopPath?: TraversalPath, blockPath: TraversalPath, parent: Object, scope: Scope, file: File) {
|
||||
constructor(loopPath?: NodePath, blockPath: NodePath, parent: Object, scope: Scope, file: File) {
|
||||
this.parent = parent;
|
||||
this.scope = scope;
|
||||
this.file = file;
|
||||
@@ -319,6 +337,7 @@ class BlockScoping {
|
||||
// this is the defining identifier of a declaration
|
||||
var ref = letRefs[key];
|
||||
|
||||
// todo: could skip this if the colliding binding is in another function
|
||||
if (scope.parentHasBinding(key) || scope.hasGlobal(key)) {
|
||||
var uid = scope.generateUidIdentifier(ref.name).name;
|
||||
ref.name = uid;
|
||||
@@ -589,7 +608,6 @@ class BlockScoping {
|
||||
t.variableDeclarator(ret, call)
|
||||
]));
|
||||
|
||||
var loop = this.loop;
|
||||
var retCheck;
|
||||
var has = this.has;
|
||||
var cases = [];
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import type NodePath from "../../../traversal/path";
|
||||
import type File from "../../file";
|
||||
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";
|
||||
import * as messages from "../../../messages";
|
||||
import * as util from "../../../util";
|
||||
import traverse from "../../../traversal";
|
||||
import each from "lodash/collection/each";
|
||||
import has from "lodash/object/has";
|
||||
import * as t from "../../../types";
|
||||
|
||||
const PROPERTY_COLLISION_METHOD_NAME = "__initializeProperties";
|
||||
@@ -35,22 +34,6 @@ var collectPropertyReferencesVisitor = {
|
||||
}
|
||||
};
|
||||
|
||||
var constructorVisitor = {
|
||||
ThisExpression: {
|
||||
enter(node, parent, scope, ref) {
|
||||
return ref;
|
||||
}
|
||||
},
|
||||
|
||||
Function: {
|
||||
enter(node) {
|
||||
if (!node.shadow) {
|
||||
this.skip();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var verifyConstructorVisitor = {
|
||||
MethodDefinition: {
|
||||
enter() {
|
||||
@@ -104,7 +87,7 @@ class ClassTransformer {
|
||||
* Description
|
||||
*/
|
||||
|
||||
constructor(path: TraversalPath, file: File) {
|
||||
constructor(path: NodePath, file: File) {
|
||||
this.parent = path.parent;
|
||||
this.scope = path.scope;
|
||||
this.node = path.node;
|
||||
@@ -141,8 +124,6 @@ class ClassTransformer {
|
||||
|
||||
run() {
|
||||
var superName = this.superName;
|
||||
var className = this.className;
|
||||
var classBody = this.node.body.body;
|
||||
var classRef = this.classRef;
|
||||
var file = this.file;
|
||||
|
||||
@@ -219,6 +200,8 @@ class ClassTransformer {
|
||||
}
|
||||
}
|
||||
|
||||
body = body.concat(this.staticPropBody);
|
||||
|
||||
if (this.className) {
|
||||
// named class with only a constructor
|
||||
if (body.length === 1) return t.toExpression(body[0]);
|
||||
@@ -233,8 +216,6 @@ class ClassTransformer {
|
||||
t.inheritsComments(body[0], this.node);
|
||||
}
|
||||
|
||||
body = body.concat(this.staticPropBody);
|
||||
|
||||
//
|
||||
|
||||
body.push(t.returnStatement(classRef));
|
||||
@@ -262,7 +243,7 @@ class ClassTransformer {
|
||||
var map = defineMap.push(mutatorMap, node, kind, this.file);
|
||||
|
||||
if (enumerable) {
|
||||
map.enumerable = t.literal(true)
|
||||
map.enumerable = t.literal(true);
|
||||
}
|
||||
|
||||
if (map.decorators) {
|
||||
@@ -276,9 +257,6 @@ class ClassTransformer {
|
||||
|
||||
buildBody() {
|
||||
var constructorBody = this.constructorBody;
|
||||
var constructor = this.constructor;
|
||||
var className = this.className;
|
||||
var superName = this.superName;
|
||||
var classBody = this.node.body.body;
|
||||
var body = this.body;
|
||||
|
||||
@@ -315,7 +293,7 @@ class ClassTransformer {
|
||||
this.pushMethod(node, path);
|
||||
}
|
||||
} else if (t.isClassProperty(node)) {
|
||||
this.pushProperty(node);
|
||||
this.pushProperty(node, path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -375,7 +353,7 @@ class ClassTransformer {
|
||||
}
|
||||
|
||||
var lastNonNullIndex = 0;
|
||||
for (var i = 0; i < args.length; i++) {
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
if (args[i] !== nullNode) lastNonNullIndex = i;
|
||||
}
|
||||
args = args.slice(0, lastNonNullIndex + 1);
|
||||
@@ -450,7 +428,7 @@ class ClassTransformer {
|
||||
* Description
|
||||
*/
|
||||
|
||||
verifyConstructor(path: TraversalPath) {
|
||||
verifyConstructor(path: NodePath) {
|
||||
var state = {
|
||||
hasBareSuper: false,
|
||||
bareSuper: null,
|
||||
@@ -471,7 +449,7 @@ class ClassTransformer {
|
||||
* Push a method to its respective mutatorMap.
|
||||
*/
|
||||
|
||||
pushMethod(node: { type: "MethodDefinition" }, path?: TraversalPath, allowedIllegal?) {
|
||||
pushMethod(node: { type: "MethodDefinition" }, path?: NodePath, 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));
|
||||
}
|
||||
@@ -500,10 +478,8 @@ class ClassTransformer {
|
||||
* Description
|
||||
*/
|
||||
|
||||
pushProperty(node: { type: "ClassProperty" }) {
|
||||
var key;
|
||||
|
||||
this.scope.traverse(node, collectPropertyReferencesVisitor, {
|
||||
pushProperty(node: { type: "ClassProperty" }, path: NodePath) {
|
||||
path.traverse(collectPropertyReferencesVisitor, {
|
||||
references: this.instancePropRefs,
|
||||
scope: this.scope
|
||||
});
|
||||
@@ -519,7 +495,6 @@ class ClassTransformer {
|
||||
this.pushToMap(node, true, "initializer");
|
||||
|
||||
var initializers;
|
||||
var body;
|
||||
var target;
|
||||
if (node.static) {
|
||||
initializers = this.staticInitializersId = this.staticInitializersId || this.scope.generateUidIdentifier("staticInitializers");
|
||||
@@ -558,7 +533,7 @@ class ClassTransformer {
|
||||
* Replace the constructor body of our class.
|
||||
*/
|
||||
|
||||
pushConstructor(method: { type: "MethodDefinition" }, path: TraversalPath) {
|
||||
pushConstructor(method: { type: "MethodDefinition" }, path: NodePath) {
|
||||
// https://github.com/babel/babel/issues/1077
|
||||
var fnPath = path.get("value");
|
||||
if (fnPath.scope.hasOwnBinding(this.classRef.name)) {
|
||||
|
||||
@@ -1,31 +1,18 @@
|
||||
import * as messages from "../../../messages";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export function AssignmentExpression(node, parent, scope, file) {
|
||||
var ids = this.getBindingIdentifiers();
|
||||
|
||||
for (var name in ids) {
|
||||
var id = ids[name];
|
||||
|
||||
var binding = scope.getBinding(name);
|
||||
|
||||
// no binding exists
|
||||
if (!binding) continue;
|
||||
export function Scope(node, parent, scope) {
|
||||
for (var name in scope.bindings) {
|
||||
var binding = scope.bindings[name];
|
||||
|
||||
// not a constant
|
||||
if (binding.kind !== "const" && binding.kind !== "module") continue;
|
||||
|
||||
// check if the assignment id matches the constant declaration id
|
||||
// if it does then it was the id used to initially declare the
|
||||
// constant so we can just ignore it
|
||||
if (binding.identifier === id) continue;
|
||||
|
||||
throw file.errorWithNode(id, messages.get("readOnly", name));
|
||||
for (var violation of (binding.constantViolations: Array)) {
|
||||
throw violation.errorWithNode(messages.get("readOnly", name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export { AssignmentExpression as UpdateExpression };
|
||||
|
||||
export function VariableDeclaration(node) {
|
||||
if (node.kind === "const") node.kind = "let";
|
||||
}
|
||||
|
||||
@@ -56,15 +56,22 @@ export function ForOfStatement(node, parent, scope, file) {
|
||||
export { ForOfStatement as ForInStatement };
|
||||
|
||||
export function Func/*tion*/(node, parent, scope, file) {
|
||||
var hasDestructuring = false;
|
||||
for (let pattern of (node.params: Array)) {
|
||||
if (t.isPattern(pattern)) {
|
||||
hasDestructuring = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!hasDestructuring) return;
|
||||
|
||||
var nodes = [];
|
||||
|
||||
var hasDestructuring = false;
|
||||
for (var i = 0; i < node.params.length; i++) {
|
||||
let pattern = node.params[i];
|
||||
if (!t.isPattern(pattern)) continue;
|
||||
|
||||
node.params = node.params.map(function (pattern, i) {
|
||||
if (!t.isPattern(pattern)) return pattern;
|
||||
|
||||
hasDestructuring = true;
|
||||
var ref = scope.generateUidIdentifier("ref");
|
||||
var ref = node.params[i] = scope.generateUidIdentifier("ref");
|
||||
t.inherits(ref, pattern);
|
||||
|
||||
var destructuring = new DestructuringTransformer({
|
||||
@@ -74,12 +81,9 @@ export function Func/*tion*/(node, parent, scope, file) {
|
||||
file: file,
|
||||
kind: "let"
|
||||
});
|
||||
|
||||
destructuring.init(pattern, ref);
|
||||
|
||||
return ref;
|
||||
});
|
||||
|
||||
if (!hasDestructuring) return;
|
||||
}
|
||||
|
||||
t.ensureBlock(node);
|
||||
|
||||
@@ -107,30 +111,10 @@ export function CatchClause(node, parent, scope, file) {
|
||||
node.body.body = nodes.concat(node.body.body);
|
||||
}
|
||||
|
||||
export function ExpressionStatement(node, parent, scope, file) {
|
||||
var expr = node.expression;
|
||||
if (expr.type !== "AssignmentExpression") return;
|
||||
if (!t.isPattern(expr.left)) return;
|
||||
if (this.isCompletionRecord()) return;
|
||||
|
||||
var destructuring = new DestructuringTransformer({
|
||||
operator: expr.operator,
|
||||
scope: scope,
|
||||
file: file,
|
||||
});
|
||||
|
||||
return destructuring.init(expr.left, expr.right);
|
||||
}
|
||||
|
||||
export function AssignmentExpression(node, parent, scope, file) {
|
||||
if (!t.isPattern(node.left)) return;
|
||||
|
||||
var ref = scope.generateUidIdentifier("temp");
|
||||
|
||||
var nodes = [];
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(ref, node.right)
|
||||
]));
|
||||
|
||||
var destructuring = new DestructuringTransformer({
|
||||
operator: node.operator,
|
||||
@@ -139,13 +123,24 @@ export function AssignmentExpression(node, parent, scope, file) {
|
||||
nodes: nodes
|
||||
});
|
||||
|
||||
if (t.isArrayExpression(node.right)) {
|
||||
destructuring.arrays[ref.name] = true;
|
||||
var ref;
|
||||
if (this.isCompletionRecord() || !this.parentPath.isExpressionStatement()) {
|
||||
ref = scope.generateUidIdentifierBasedOnNode(node.right, "ref");
|
||||
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(ref, node.right)
|
||||
]));
|
||||
|
||||
if (t.isArrayExpression(node.right)) {
|
||||
destructuring.arrays[ref.name] = true;
|
||||
}
|
||||
}
|
||||
|
||||
destructuring.init(node.left, ref);
|
||||
destructuring.init(node.left, ref || node.right);
|
||||
|
||||
nodes.push(t.expressionStatement(ref));
|
||||
if (ref) {
|
||||
nodes.push(t.expressionStatement(ref));
|
||||
}
|
||||
|
||||
return nodes;
|
||||
}
|
||||
@@ -160,7 +155,7 @@ function variableDeclarationHasPattern(node) {
|
||||
}
|
||||
|
||||
export function VariableDeclaration(node, parent, scope, file) {
|
||||
if (t.isForInStatement(parent) || t.isForOfStatement(parent)) return;
|
||||
if (t.isForXStatement(parent)) return;
|
||||
if (!variableDeclarationHasPattern(node)) return;
|
||||
|
||||
var nodes = [];
|
||||
@@ -225,8 +220,8 @@ function hasRest(pattern) {
|
||||
}
|
||||
|
||||
var arrayUnpackVisitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
if (this.isReferencedIdentifier() && state.bindings[node.name]) {
|
||||
ReferencedIdentifier(node, parent, scope, state) {
|
||||
if (state.bindings[node.name]) {
|
||||
state.deopt = true;
|
||||
this.stop();
|
||||
}
|
||||
@@ -498,7 +493,6 @@ class DestructuringTransformer {
|
||||
// trying to destructure a value that we can't evaluate more than once so we
|
||||
// need to save it to a variable
|
||||
|
||||
var shouldMemoise = true;
|
||||
if (!t.isArrayExpression(ref) && !t.isMemberExpression(ref)) {
|
||||
var memo = this.scope.maybeGenerateMemoised(ref, true);
|
||||
if (memo) {
|
||||
|
||||
@@ -31,7 +31,7 @@ export function ForOfStatement(node, parent, scope, file) {
|
||||
|
||||
if (build.replaceParent) {
|
||||
this.parentPath.replaceWithMultiple(build.node);
|
||||
this.remove();
|
||||
this.dangerouslyRemove();
|
||||
} else {
|
||||
return build.node;
|
||||
}
|
||||
@@ -70,6 +70,10 @@ export function _ForOfStatementArray(node, scope, file) {
|
||||
loop.body.body.unshift(t.expressionStatement(t.assignmentExpression("=", left, iterationValue)));
|
||||
}
|
||||
|
||||
if (this.parentPath.isLabeledStatement()) {
|
||||
loop = t.labeledStatement(this.parentPath.node.label, loop);
|
||||
}
|
||||
|
||||
nodes.push(loop);
|
||||
|
||||
return nodes;
|
||||
|
||||
@@ -19,8 +19,8 @@ export function ImportDeclaration(node, parent, scope, file) {
|
||||
var nodes = [];
|
||||
|
||||
if (node.specifiers.length) {
|
||||
for (var i = 0; i < node.specifiers.length; i++) {
|
||||
file.moduleFormatter.importSpecifier(node.specifiers[i], node, nodes, parent);
|
||||
for (var specifier of (node.specifiers: Array)) {
|
||||
file.moduleFormatter.importSpecifier(specifier, node, nodes, parent);
|
||||
}
|
||||
} else {
|
||||
file.moduleFormatter.importDeclaration(node, nodes, parent);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import callDelegate from "../../helpers/call-delegate";
|
||||
import getFunctionArity from "../../helpers/get-function-arity";
|
||||
import * as util from "../../../util";
|
||||
import traverse from "../../../traversal";
|
||||
import * as t from "../../../types";
|
||||
|
||||
var hasDefaults = function (node) {
|
||||
@@ -25,37 +25,54 @@ var iifeVisitor = {
|
||||
export function Func/*tion*/(node, parent, scope, file) {
|
||||
if (!hasDefaults(node)) return;
|
||||
|
||||
// ensure it's a block, useful for arrow functions
|
||||
t.ensureBlock(node);
|
||||
|
||||
var state = {
|
||||
iife: false,
|
||||
scope: scope
|
||||
};
|
||||
|
||||
var body = [];
|
||||
|
||||
//
|
||||
var argsIdentifier = t.identifier("arguments");
|
||||
argsIdentifier._shadowedFunctionLiteral = true;
|
||||
|
||||
var lastNonDefaultParam = 0;
|
||||
|
||||
var state = { iife: false, scope: scope };
|
||||
|
||||
var pushDefNode = function (left, right, i) {
|
||||
var defNode = util.template("default-parameter", {
|
||||
VARIABLE_NAME: left,
|
||||
DEFAULT_VALUE: right,
|
||||
ARGUMENT_KEY: t.literal(i),
|
||||
ARGUMENTS: argsIdentifier
|
||||
}, true);
|
||||
// push a default parameter definition
|
||||
function pushDefNode(left, right, i) {
|
||||
var defNode;
|
||||
if (exceedsLastNonDefault(i) || t.isPattern(left) || file.transformers["es6.spec.blockScoping"].canTransform()) {
|
||||
defNode = util.template("default-parameter", {
|
||||
VARIABLE_NAME: left,
|
||||
DEFAULT_VALUE: right,
|
||||
ARGUMENT_KEY: t.literal(i),
|
||||
ARGUMENTS: argsIdentifier
|
||||
}, true);
|
||||
} else {
|
||||
defNode = util.template("default-parameter-assign", {
|
||||
VARIABLE_NAME: left,
|
||||
DEFAULT_VALUE: right
|
||||
}, true);
|
||||
}
|
||||
defNode._blockHoist = node.params.length - i;
|
||||
body.push(defNode);
|
||||
};
|
||||
}
|
||||
|
||||
// check if an index exceeds the functions arity
|
||||
function exceedsLastNonDefault(i) {
|
||||
return i + 1 > lastNonDefaultParam;
|
||||
}
|
||||
|
||||
//
|
||||
var lastNonDefaultParam = getFunctionArity(node);
|
||||
|
||||
//
|
||||
var params = this.get("params");
|
||||
for (var i = 0; i < params.length; i++) {
|
||||
var param = params[i];
|
||||
|
||||
if (!param.isAssignmentPattern()) {
|
||||
if (!param.isRestElement()) {
|
||||
lastNonDefaultParam = i + 1;
|
||||
}
|
||||
|
||||
if (!param.isIdentifier()) {
|
||||
param.traverse(iifeVisitor, state);
|
||||
}
|
||||
@@ -70,9 +87,13 @@ export function Func/*tion*/(node, parent, scope, file) {
|
||||
var left = param.get("left");
|
||||
var right = param.get("right");
|
||||
|
||||
var placeholder = scope.generateUidIdentifier("x");
|
||||
placeholder._isDefaultPlaceholder = true;
|
||||
node.params[i] = placeholder;
|
||||
if (exceedsLastNonDefault(i) || left.isPattern()) {
|
||||
var placeholder = scope.generateUidIdentifier("x");
|
||||
placeholder._isDefaultPlaceholder = true;
|
||||
node.params[i] = placeholder;
|
||||
} else {
|
||||
node.params[i] = left.node;
|
||||
}
|
||||
|
||||
if (!state.iife) {
|
||||
if (right.isIdentifier() && scope.hasOwnBinding(right.node.name)) {
|
||||
|
||||
@@ -1,13 +1,21 @@
|
||||
import isNumber from "lodash/lang/isNumber";
|
||||
import * as util from "../../../util";
|
||||
import * as t from "../../../types";
|
||||
|
||||
var memberExpressionOptimisationVisitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
Scope(node, parent, scope, state) {
|
||||
// check if this scope has a local binding that will shadow the rest parameter
|
||||
if (this.isScope() && !scope.bindingIdentifierEquals(state.name, state.outerBinding)) {
|
||||
return this.skip();
|
||||
if (!scope.bindingIdentifierEquals(state.name, state.outerBinding)) {
|
||||
this.skip();
|
||||
}
|
||||
},
|
||||
|
||||
enter(node, parent, scope, state) {
|
||||
var stop = () => {
|
||||
state.canOptimise = false;
|
||||
this.stop();
|
||||
};
|
||||
|
||||
if (this.isArrowFunctionExpression()) return stop();
|
||||
|
||||
// skip over functions as whatever `arguments` we reference inside will refer
|
||||
// to the wrong function
|
||||
@@ -24,19 +32,20 @@ var memberExpressionOptimisationVisitor = {
|
||||
if (!state.noOptimise && t.isMemberExpression(parent) && parent.computed) {
|
||||
// if we know that this member expression is referencing a number then we can safely
|
||||
// optimise it
|
||||
var prop = parent.property;
|
||||
if (isNumber(prop.value) || t.isUnaryExpression(prop) || t.isBinaryExpression(prop)) {
|
||||
var prop = this.parentPath.get("property");
|
||||
if (prop.isBaseType("number")) {
|
||||
state.candidates.push(this);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
state.canOptimise = false;
|
||||
this.stop();
|
||||
stop();
|
||||
}
|
||||
};
|
||||
|
||||
function optimizeMemberExpression(parent, offset) {
|
||||
if (offset === 0) return;
|
||||
|
||||
var newExpr;
|
||||
var prop = parent.property;
|
||||
|
||||
@@ -90,8 +99,7 @@ export function Func/*tion*/(node, parent, scope, file) {
|
||||
|
||||
// we only have shorthands and there's no other references
|
||||
if (state.canOptimise && state.candidates.length) {
|
||||
for (var i = 0; i < state.candidates.length; i++) {
|
||||
var candidate = state.candidates[i];
|
||||
for (var candidate of (state.candidates: Array)) {
|
||||
candidate.replaceWith(argsId);
|
||||
optimizeMemberExpression(candidate.parent, node.params.length);
|
||||
}
|
||||
|
||||
@@ -1,29 +1,59 @@
|
||||
import traverse from "../../../traversal";
|
||||
import * as t from "../../../types";
|
||||
|
||||
function buildAssert(node, file) {
|
||||
return t.callExpression(
|
||||
file.addHelper("temporal-assert-defined"),
|
||||
[node, t.literal(node.name), file.addHelper("temporal-undefined")]
|
||||
);
|
||||
}
|
||||
|
||||
function references(node, scope, state) {
|
||||
var declared = state.letRefs[node.name];
|
||||
if (!declared) return false;
|
||||
|
||||
// declared node is different in this scope
|
||||
return scope.getBindingIdentifier(node.name) === declared;
|
||||
}
|
||||
|
||||
var visitor = {
|
||||
ReferencedIdentifier(node, parent, scope, state) {
|
||||
if (t.isFor(parent) && parent.left === node) return;
|
||||
|
||||
var declared = state.letRefs[node.name];
|
||||
if (!declared) return;
|
||||
if (!references(node, scope, state)) return;
|
||||
|
||||
// declared node is different in this scope
|
||||
if (scope.getBindingIdentifier(node.name) !== declared) return;
|
||||
|
||||
var assert = t.callExpression(
|
||||
state.file.addHelper("temporal-assert-defined"),
|
||||
[node, t.literal(node.name), state.file.addHelper("temporal-undefined")]
|
||||
);
|
||||
var assert = buildAssert(node, state.file);
|
||||
|
||||
this.skip();
|
||||
|
||||
if (t.isAssignmentExpression(parent) || t.isUpdateExpression(parent)) {
|
||||
if (t.isUpdateExpression(parent)) {
|
||||
if (parent._ignoreBlockScopingTDZ) return;
|
||||
this.parentPath.replaceWith(t.sequenceExpression([assert, parent]));
|
||||
} else {
|
||||
return t.logicalExpression("&&", assert, node);
|
||||
}
|
||||
},
|
||||
|
||||
AssignmentExpression: {
|
||||
exit(node, parent, scope, state) {
|
||||
if (node._ignoreBlockScopingTDZ) return;
|
||||
|
||||
var nodes = [];
|
||||
var ids = this.getBindingIdentifiers();
|
||||
|
||||
for (var name in ids) {
|
||||
var id = ids[name];
|
||||
|
||||
if (references(id, scope, state)) {
|
||||
nodes.push(buildAssert(id, state.file));
|
||||
}
|
||||
}
|
||||
|
||||
if (nodes.length) {
|
||||
node._ignoreBlockScopingTDZ = true;
|
||||
nodes.push(node);
|
||||
return nodes.map(t.expressionStatement);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import * as t from "../../../types";
|
||||
|
||||
export var metadata = {
|
||||
optional: true
|
||||
optional: true,
|
||||
group: "builtin-pre"
|
||||
};
|
||||
|
||||
export function TemplateLiteral(node, parent, scope, file) {
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import includes from "lodash/collection/includes";
|
||||
import * as t from "../../../types";
|
||||
|
||||
function getSpreadLiteral(spread, scope) {
|
||||
if (scope.file.isLoose("es6.spread")) {
|
||||
if (scope.hub.file.isLoose("es6.spread")) {
|
||||
return spread.argument;
|
||||
} else {
|
||||
return scope.toArray(spread.argument, true);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import reduceRight from "lodash/collection/reduceRight";
|
||||
import * as messages from "../../../messages";
|
||||
import flatten from "lodash/array/flatten";
|
||||
import traverse from "../../../traversal";
|
||||
import * as util from "../../../util";
|
||||
import map from "lodash/collection/map";
|
||||
import * as t from "../../../types";
|
||||
@@ -21,7 +20,7 @@ function returnBlock(expr) {
|
||||
}
|
||||
|
||||
var visitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
enter(node, parent) {
|
||||
if (t.isTryStatement(parent)) {
|
||||
if (node === parent.block) {
|
||||
this.skip();
|
||||
@@ -35,7 +34,7 @@ var visitor = {
|
||||
return state.subTransform(node.argument);
|
||||
},
|
||||
|
||||
Function(node, parent, scope, state) {
|
||||
Function() {
|
||||
this.skip();
|
||||
},
|
||||
|
||||
@@ -44,12 +43,14 @@ var visitor = {
|
||||
},
|
||||
|
||||
ThisExpression(node, parent, scope, state) {
|
||||
state.needsThis = true;
|
||||
state.thisPaths.push(this);
|
||||
if (!state.isShadowed) {
|
||||
state.needsThis = true;
|
||||
state.thisPaths.push(this);
|
||||
}
|
||||
},
|
||||
|
||||
ReferencedIdentifier(node, parent, scope, state) {
|
||||
if (node.name === "arguments") {
|
||||
if (node.name === "arguments" && (!state.isShadowed || node._shadowedFunctionLiteral)) {
|
||||
state.needsArguments = true;
|
||||
state.argumentsPaths.push(this);
|
||||
}
|
||||
@@ -67,8 +68,9 @@ class TailCallTransformer {
|
||||
this.needsThis = false;
|
||||
this.thisPaths = [];
|
||||
|
||||
this.ownerId = path.node.id;
|
||||
this.vars = [];
|
||||
this.isShadowed = path.isArrowFunctionExpression() || path.is("shadow");
|
||||
this.ownerId = path.node.id;
|
||||
this.vars = [];
|
||||
|
||||
this.scope = scope;
|
||||
this.path = path;
|
||||
@@ -125,7 +127,6 @@ class TailCallTransformer {
|
||||
}
|
||||
|
||||
run() {
|
||||
var scope = this.scope;
|
||||
var node = this.node;
|
||||
|
||||
// only tail recursion can be optimized as for now, so we can skip anonymous
|
||||
@@ -201,13 +202,14 @@ class TailCallTransformer {
|
||||
}
|
||||
|
||||
if (this.needsArguments || this.setsArguments) {
|
||||
for (var path of (this.argumentsPaths: Array)) {
|
||||
for (let path of (this.argumentsPaths: Array)) {
|
||||
path.replaceWith(this.argumentsId);
|
||||
}
|
||||
|
||||
var decl = t.variableDeclarator(this.argumentsId);
|
||||
if (this.argumentsId) {
|
||||
decl.init = t.identifier("arguments");
|
||||
decl.init._shadowedFunctionLiteral = true;
|
||||
}
|
||||
topVars.push(decl);
|
||||
}
|
||||
@@ -288,7 +290,8 @@ class TailCallTransformer {
|
||||
}
|
||||
|
||||
subTransformCallExpression(node) {
|
||||
var callee = node.callee, thisBinding, args;
|
||||
var callee = node.callee;
|
||||
var thisBinding, args;
|
||||
|
||||
if (t.isMemberExpression(callee, { computed: false }) && t.isIdentifier(callee.property)) {
|
||||
switch (callee.property.name) {
|
||||
@@ -298,6 +301,7 @@ class TailCallTransformer {
|
||||
|
||||
case "apply":
|
||||
args = node.arguments[1] || t.identifier("undefined");
|
||||
this.needsArguments = true;
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -331,6 +335,10 @@ class TailCallTransformer {
|
||||
args = t.arrayExpression(node.arguments);
|
||||
}
|
||||
|
||||
if (t.isArrayExpression(args) && args.elements.length > this.node.params.length) {
|
||||
this.needsArguments = true;
|
||||
}
|
||||
|
||||
var argumentsId = this.getArgumentsId();
|
||||
var params = this.getParams();
|
||||
|
||||
@@ -346,14 +354,14 @@ class TailCallTransformer {
|
||||
var elems = args.elements;
|
||||
for (let i = 0; i < elems.length && i < params.length; i++) {
|
||||
let param = params[i];
|
||||
var elem = elems[i] || (elems[i] = t.identifier("undefined"));
|
||||
let elem = elems[i] || (elems[i] = t.identifier("undefined"));
|
||||
if (!param._isDefaultPlaceholder) {
|
||||
elems[i] = t.assignmentExpression("=", param, elem);
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.needsArguments) {
|
||||
for (var elem of (elems: Array)) {
|
||||
for (let elem of (elems: Array)) {
|
||||
body.push(t.expressionStatement(elem));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,26 @@
|
||||
/* eslint no-unused-vars: 0 */
|
||||
|
||||
import * as t from "../../../types";
|
||||
|
||||
var buildBinaryExpression = function (left, right) {
|
||||
return t.binaryExpression("+", left, right);
|
||||
export var metadata = {
|
||||
group: "builtin-pre"
|
||||
};
|
||||
|
||||
function buildBinaryExpression(left, right) {
|
||||
var node = t.binaryExpression("+", left, right);
|
||||
node._templateLiteralProduced = true;
|
||||
return node;
|
||||
}
|
||||
|
||||
function crawl(path) {
|
||||
if (path.is("_templateLiteralProduced")) {
|
||||
crawl(path.get("left"));
|
||||
crawl(path.get("right"));
|
||||
} else if (!path.isBaseType("string") && !path.isBaseType("number")) {
|
||||
path.replaceWith(t.callExpression(t.identifier("String"), [path.node]));
|
||||
}
|
||||
}
|
||||
|
||||
export function TaggedTemplateExpression(node, parent, scope, file) {
|
||||
var quasi = node.quasi;
|
||||
var args = [];
|
||||
@@ -11,8 +28,7 @@ export function TaggedTemplateExpression(node, parent, scope, file) {
|
||||
var strings = [];
|
||||
var raw = [];
|
||||
|
||||
for (var i = 0; i < quasi.quasis.length; i++) {
|
||||
var elem = quasi.quasis[i];
|
||||
for (var elem of (quasi.quasis: Array)) {
|
||||
strings.push(t.literal(elem.value.cooked));
|
||||
raw.push(t.literal(elem.value.raw));
|
||||
}
|
||||
@@ -31,11 +47,8 @@ export function TaggedTemplateExpression(node, parent, scope, file) {
|
||||
|
||||
export function TemplateLiteral(node, parent, scope, file) {
|
||||
var nodes = [];
|
||||
var i;
|
||||
|
||||
for (i = 0; i < node.quasis.length; i++) {
|
||||
var elem = node.quasis[i];
|
||||
|
||||
for (let elem of (node.quasis: Array)) {
|
||||
nodes.push(t.literal(elem.value.cooked));
|
||||
|
||||
var expr = node.expressions.shift();
|
||||
@@ -49,11 +62,12 @@ export function TemplateLiteral(node, parent, scope, file) {
|
||||
|
||||
var root = buildBinaryExpression(nodes.shift(), nodes.shift());
|
||||
|
||||
for (i = 0; i < nodes.length; i++) {
|
||||
root = buildBinaryExpression(root, nodes[i]);
|
||||
for (let node of (nodes: Array)) {
|
||||
root = buildBinaryExpression(root, node);
|
||||
}
|
||||
|
||||
return root;
|
||||
this.replaceWith(root);
|
||||
//crawl(this);
|
||||
} else {
|
||||
return nodes[0];
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ export var metadata = {
|
||||
export function ComprehensionExpression(node, parent, scope, file) {
|
||||
var callback = array;
|
||||
if (node.generator) callback = generator;
|
||||
return callback(node, parent, scope, file);
|
||||
return callback(node, parent, scope);
|
||||
}
|
||||
|
||||
function generator(node) {
|
||||
@@ -25,7 +25,7 @@ function generator(node) {
|
||||
return t.callExpression(container, []);
|
||||
}
|
||||
|
||||
function array(node, parent, scope, file) {
|
||||
function array(node, parent, scope) {
|
||||
var uid = scope.generateUidIdentifierBasedOnNode(parent);
|
||||
|
||||
var container = util.template("array-comprehension-container", {
|
||||
|
||||
@@ -10,8 +10,8 @@ export var metadata = {
|
||||
|
||||
export function ObjectExpression(node, parent, scope, file) {
|
||||
var hasDecorators = false;
|
||||
for (var i = 0; i < node.properties.length; i++) {
|
||||
var prop = node.properties[i];
|
||||
for (let i = 0; i < node.properties.length; i++) {
|
||||
let prop = node.properties[i];
|
||||
if (prop.decorators) {
|
||||
hasDecorators = true;
|
||||
break;
|
||||
@@ -21,12 +21,12 @@ export function ObjectExpression(node, parent, scope, file) {
|
||||
|
||||
var mutatorMap = {};
|
||||
|
||||
for (var i = 0; i < node.properties.length; i++) {
|
||||
var prop = node.properties[i];
|
||||
for (let i = 0; i < node.properties.length; i++) {
|
||||
let prop = node.properties[i];
|
||||
if (prop.decorators) memoiseDecorators(prop.decorators, scope);
|
||||
|
||||
|
||||
if (prop.kind === "init") {
|
||||
if (prop.kind === "init" && !prop.method) {
|
||||
prop.kind = "";
|
||||
prop.value = t.functionExpression(null, [], t.blockStatement([
|
||||
t.returnStatement(prop.value)
|
||||
|
||||
@@ -1,16 +1,21 @@
|
||||
export default {
|
||||
//- builtin-setup
|
||||
//- builtin-prepass
|
||||
"minification.constantFolding": require("./minification/constant-folding"),
|
||||
|
||||
//- builtin-pre
|
||||
strict: require("./other/strict"),
|
||||
eval: require("./other/eval"),
|
||||
_explode: require("./internal/explode"),
|
||||
_validation: require("./internal/validation"),
|
||||
_hoistDirectives: require("./internal/hoist-directives"),
|
||||
"minification.removeDebugger": require("./minification/remove-debugger"),
|
||||
"minification.removeConsole": require("./minification/remove-console"),
|
||||
"utility.inlineEnvironmentVariables": require("./utility/inline-environment-variables"),
|
||||
"minification.inlineExpressions": require("./minification/inline-expressions"),
|
||||
"minification.deadCodeElimination": require("./minification/dead-code-elimination"),
|
||||
_modules: require("./internal/modules"),
|
||||
"spec.functionName": require("./spec/function-name"),
|
||||
"es6.spec.templateLiterals": require("./es6/spec.template-literals"),
|
||||
"es6.templateLiterals": require("./es6/template-literals"),
|
||||
|
||||
//- builtin-basic
|
||||
// this is where the bulk of the ES6 transformations take place, none of them require traversal state
|
||||
@@ -32,8 +37,6 @@ export default {
|
||||
"es6.objectSuper": require("./es6/object-super"),
|
||||
"es7.objectRestSpread": require("./es7/object-rest-spread"),
|
||||
"es7.exponentiationOperator": require("./es7/exponentiation-operator"),
|
||||
"es6.spec.templateLiterals": require("./es6/spec.template-literals"),
|
||||
"es6.templateLiterals": require("./es6/template-literals"),
|
||||
"es5.properties.mutators": require("./es5/properties.mutators"),
|
||||
"es6.properties.shorthand": require("./es6/properties.shorthand"),
|
||||
"es6.properties.computed": require("./es6/properties.computed"),
|
||||
@@ -51,8 +54,6 @@ export default {
|
||||
"es6.spec.symbols": require("./es6/spec.symbols"),
|
||||
"es7.functionBind": require("./es7/function-bind"),
|
||||
"spec.undefinedToVoid": require("./spec/undefined-to-void"),
|
||||
jscript: require("./other/jscript"),
|
||||
flow: require("./other/flow"),
|
||||
|
||||
//- builtin-advanced
|
||||
"es6.destructuring": require("./es6/destructuring"),
|
||||
@@ -60,13 +61,11 @@ export default {
|
||||
"es6.spec.blockScoping": require("./es6/spec.block-scoping"),
|
||||
reactCompat: require("./other/react-compat"),
|
||||
react: require("./other/react"),
|
||||
regenerator: require("./other/regenerator"),
|
||||
|
||||
// es6 syntax transformation is **forbidden** past this point since regenerator will chuck a massive
|
||||
// hissy fit
|
||||
|
||||
//- regenerator
|
||||
regenerator: require("./other/regenerator"),
|
||||
|
||||
//- builtin-modules
|
||||
runtime: require("./other/runtime"),
|
||||
"es6.modules": require("./es6/modules"),
|
||||
@@ -83,4 +82,6 @@ export default {
|
||||
"minification.memberExpressionLiterals": require("./minification/member-expression-literals"),
|
||||
"minification.propertyLiterals": require("./minification/property-literals"),
|
||||
_blockHoist: require("./internal/block-hoist"),
|
||||
jscript: require("./other/jscript"),
|
||||
flow: require("./other/flow"),
|
||||
};
|
||||
|
||||
@@ -2,12 +2,12 @@ import clone from "lodash/lang/clone";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export var metadata = {
|
||||
group: "builtin-setup"
|
||||
group: "builtin-pre"
|
||||
};
|
||||
|
||||
function buildClone(bindingKey, refKey) {
|
||||
function buildClone(bindingKey, refKey, check?) {
|
||||
return function (node) {
|
||||
if (node[bindingKey] === node[refKey]) {
|
||||
if (node[bindingKey] === node[refKey] || (check && check(node))) {
|
||||
node[refKey] = t.removeComments(clone(node[refKey]));
|
||||
}
|
||||
};
|
||||
@@ -25,6 +25,9 @@ function buildListClone(listKey, bindingKey, refKey) {
|
||||
};
|
||||
}
|
||||
|
||||
export var Property = buildClone("value", "key");
|
||||
export var Property = buildClone("value", "key", function (node) {
|
||||
return t.isAssignmentPattern(node.value) && node.value.left === node.key;
|
||||
});
|
||||
|
||||
export var ExportDeclaration = buildListClone("specifiers", "local", "exported");
|
||||
export var ImportDeclaration = buildListClone("specifiers", "local", "imported");
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user