Compare commits

...

186 Commits

Author SHA1 Message Date
Sebastian McKenzie
078b09676f v2.13.2 2015-01-19 09:00:15 +11:00
Sebastian McKenzie
b719eaf6ab add missing semicolon 2015-01-19 08:58:11 +11:00
Sebastian McKenzie
b63accca25 remove --mangle sort from uglify since it's causing issues in safari/ios 2015-01-19 08:57:43 +11:00
Sebastian McKenzie
2d41b09c3b add 2.12.3 changelog 2015-01-19 08:57:04 +11:00
Sebastian McKenzie
0b44137d1f exclude coverage folder from npm 2015-01-19 08:56:58 +11:00
Sebastian McKenzie
8d8dd5fa37 add let scoping transformer rename note 2015-01-19 08:54:26 +11:00
Sebastian McKenzie
16b7ff972c split up tdz into an optional transformer until it has a better implementation - fixes #527 2015-01-19 08:54:04 +11:00
Sebastian McKenzie
ca41612109 use process.stdin.write instead of console.log to avoid console.log sprintf - fixes #527 2015-01-19 08:53:42 +11:00
Sebastian McKenzie
8fc7af5480 properly reference method body - fixes #530 2015-01-19 08:35:01 +11:00
Sebastian McKenzie
1c6cb7ce40 Merge pull request #526 from tricknotes/remove-unused
Remove unused local variable
2015-01-18 22:30:24 +11:00
Ryunosuke SATO
24d6c3f488 Remove unused local variable
`hasOptional` is assigned but unused from anywhere.
2015-01-18 20:16:32 +09:00
Sebastian McKenzie
d877a04397 v2.13.1 2015-01-18 21:40:21 +11:00
Sebastian McKenzie
4844882f5e break let scoping transformer if there are no block scoped references 2015-01-18 21:37:37 +11:00
Sebastian McKenzie
a80945cfb4 ignore function declarations in TDZ detection 2015-01-18 21:33:22 +11:00
Sebastian McKenzie
6a884c58a7 add 2.13.1 changelog 2015-01-18 21:28:34 +11:00
Sebastian McKenzie
7c4701716c implement block scoping TDZ 2015-01-18 21:26:02 +11:00
Sebastian McKenzie
ecfd5fadc6 v2.13.0 2015-01-18 19:31:07 +11:00
Sebastian McKenzie
c6be5cccd7 update bin/6to5/file to new source-map api 2015-01-18 19:29:18 +11:00
Sebastian McKenzie
dd256dc3f8 add acorn-6to5 changes to 2.13.0 changelog 2015-01-18 19:26:15 +11:00
Sebastian McKenzie
622da331a6 add 2.13.0 changelog 2015-01-18 19:24:54 +11:00
Sebastian McKenzie
5a44793dcb upgrade acorn-6to5 2015-01-18 19:24:49 +11:00
Sebastian McKenzie
9c9a9b0325 make regenerator test future-proof 2015-01-18 18:51:11 +11:00
Sebastian McKenzie
2534f7f2a0 upgrade dependencies 2015-01-18 18:50:58 +11:00
Sebastian McKenzie
36a933a004 add strict mode to every file 2015-01-18 18:44:08 +11:00
Sebastian McKenzie
62c168b13c fix linting errors 2015-01-18 18:41:24 +11:00
Sebastian McKenzie
810c97d7ab actually make destructuring use the provided operator 2015-01-18 18:24:16 +11:00
Sebastian McKenzie
c2b20b18b3 make name method helper export a property method instead of a default 2015-01-18 18:24:05 +11:00
Sebastian McKenzie
309d19960f better traversal context variable names 2015-01-18 18:23:46 +11:00
Sebastian McKenzie
774cb66d9b add isBlockedScoped types helper 2015-01-18 18:23:37 +11:00
Sebastian McKenzie
d360bd5bb7 update misc tests 2015-01-18 18:23:27 +11:00
Sebastian McKenzie
74f5a73d31 update traceur 2015-01-18 18:23:14 +11:00
Sebastian McKenzie
26395a86fa add block scoped functions - fixes #514 2015-01-18 18:23:03 +11:00
Sebastian McKenzie
436c488ee3 revamp let scoping transformer - closes #510 2015-01-18 18:22:37 +11:00
Sebastian McKenzie
93d99077e5 Revert "add loose mode to spread and destructuring"
This reverts commit 95d9f59668.
2015-01-18 12:52:16 +11:00
Sebastian McKenzie
0bb52d6e3d Merge branch 'master' of github.com:6to5/6to5 2015-01-18 12:16:23 +11:00
Sebastian McKenzie
320c78b815 make let scoping collision detection much more reliable 2015-01-18 12:15:50 +11:00
Sebastian McKenzie
ddd59b94b0 upgrade acorn-6to5 2015-01-18 12:15:31 +11:00
Sebastian McKenzie
c210d64557 add loose all option 2015-01-18 12:13:08 +11:00
Sebastian McKenzie
a0f605bbab add way to automatically write expected tests 2015-01-18 12:12:58 +11:00
Sebastian McKenzie
f4b2768194 add todo note to duplicate expression 2015-01-18 12:12:32 +11:00
Sebastian McKenzie
95d9f59668 add loose mode to spread and destructuring 2015-01-18 12:12:12 +11:00
Sebastian McKenzie
c41d799b9b Merge pull request #522 from fabiomcosta/lint_fixes
Fixes lint warnings that are blocking test run
2015-01-18 10:21:12 +11:00
Fabio M. Costa
e7d4642d48 Fixes lint warnings that are blocking test run 2015-01-17 14:19:54 -08:00
Sebastian McKenzie
41949fd58b push uids to scope tracking - fixes #515 2015-01-18 02:36:37 +11:00
Sebastian McKenzie
d0dc972840 fix up formatting in traverse 2015-01-18 02:35:19 +11:00
Sebastian McKenzie
621e66e9ac add private declaration and comprehension expression support to scope tracking and id building 2015-01-18 02:35:08 +11:00
Sebastian McKenzie
529544ce0e use generateUidIdentifier instead of generateUid 2015-01-18 01:16:09 +11:00
Sebastian McKenzie
f298cd3f0f allow single idents in binary assignment operator transformer 2015-01-18 01:15:35 +11:00
Sebastian McKenzie
67029ac8e8 better helpers for assignment operators 2015-01-18 00:37:51 +11:00
Sebastian McKenzie
b54800234f clean up conditional assignment operator transformers 2015-01-17 22:56:49 +11:00
Sebastian McKenzie
ecd85f53b4 Merge pull request #516 from jridgewell/mallot
Playground Proposal: Mallet operator
2015-01-17 21:59:23 +11:00
Sebastian McKenzie
9098852897 abstract out classes super replace 2015-01-17 18:53:49 +11:00
Sebastian McKenzie
3eb4d5b466 fix up styling of #518 2015-01-17 18:53:40 +11:00
Sebastian McKenzie
ddb1c718f1 Merge pull request #518 from gaearon/perf-v8
RFC: V8-specific performance impovements
2015-01-17 18:33:35 +11:00
Sebastian McKenzie
8919873ea1 clean up react/jsx transformer 2015-01-17 18:26:14 +11:00
Sebastian McKenzie
eb9cd95d5a update for head tests 2015-01-17 17:54:04 +11:00
Sebastian McKenzie
4ec701fc44 ignore variable declarations inside loop head in newline generation - fixes #519 2015-01-17 17:44:16 +11:00
Dan Abramov
ff9511d435 Proof of concept of how traversal would look like with state parameter 2015-01-17 05:03:23 +03:00
Justin Ridgewell
ac5a6da1fa Use all falsey values 2015-01-16 19:25:28 -05:00
Justin Ridgewell
616640a128 Playground Proposal: Mallet operator
The mallet operator is similar to the current memoization operator,
except it can be used outside of just objects.

In Ruby, it’s almost the same as `a = a || b`. Note that only `nil` and
`false` are falsey in Ruby. I’ve defined it as `== null`, though that
could be changed to any JS falsey value.
2015-01-16 18:57:15 -05:00
Sebastian McKenzie
1f9ce96e43 fix rest parameter array size error in rest parameter comment 2015-01-17 10:27:01 +11:00
Sebastian McKenzie
dc6a862bec better clarification of rest parameter size construction comment 2015-01-17 10:23:53 +11:00
Dan Abramov
f9480b5280 Avoid closures when traversing 2015-01-17 00:13:39 +03:00
Dan Abramov
8dc4996547 Refactor traversal into smaller methods 2015-01-17 00:13:39 +03:00
Dan Abramov
4f01f67dd6 Avoid delete in favor of null assignment to prevent deoptimizations 2015-01-17 00:13:39 +03:00
Dan Abramov
938026abeb Use toFastProperties to speed up t.* method access 2015-01-17 00:13:38 +03:00
Sebastian McKenzie
76b8945207 v2.12.6 2015-01-16 22:52:12 +11:00
Sebastian McKenzie
e78859fae0 add 2.12.6 changelog 2015-01-16 22:49:31 +11:00
Sebastian McKenzie
d68f1e9910 add flow type generation skeleton 2015-01-16 22:47:55 +11:00
Sebastian McKenzie
20e97f2d9b add flow type visitor keys - none currently as we don't need to traverse over any of them - closes #513 2015-01-16 22:46:09 +11:00
Sebastian McKenzie
750ec7783f v2.12.5 2015-01-16 08:08:52 +11:00
Sebastian McKenzie
14ae438735 remove declarators push in let scoping 2015-01-16 08:06:38 +11:00
Sebastian McKenzie
2f8bdd7e27 add 2.12.5 changelog 2015-01-16 07:59:40 +11:00
Sebastian McKenzie
87da9fcfc5 fix let scoping for loop - closes #509 2015-01-16 07:58:14 +11:00
Sebastian McKenzie
51f6cfddca remove instanbul inclusion 2015-01-16 02:47:20 +11:00
Sebastian McKenzie
b9ad4c2773 v2.12.4 2015-01-16 02:41:23 +11:00
Sebastian McKenzie
228b3dbc34 fix linting errors 2015-01-16 02:38:45 +11:00
Sebastian McKenzie
a01802300f fix default parameters transformer for loops 2015-01-16 02:36:56 +11:00
Sebastian McKenzie
51c6a3fffc fix let scoping body not existing 2015-01-16 02:31:36 +11:00
Sebastian McKenzie
4668e1d67b fix linting errors 2015-01-16 02:29:35 +11:00
Sebastian McKenzie
75e9097e19 add 2.12.4 changelog 2015-01-16 02:29:02 +11:00
Sebastian McKenzie
0627cd94d4 turn array for ins into fors 2015-01-16 02:25:41 +11:00
Sebastian McKenzie
71879d6e8f Merge branch 'master' of github.com:6to5/6to5 2015-01-16 00:50:55 +11:00
Sebastian McKenzie
d7810cac08 add ambiguous rest parameters comment 2015-01-16 00:50:38 +11:00
Sebastian McKenzie
dc9efd21d3 Merge pull request #507 from leebyron/unused
Rm unused dependencies (pass lint)
2015-01-16 00:47:55 +11:00
Lee Byron
347e490614 Rm unused dependencies (pass lint) 2015-01-15 08:47:03 -05:00
Sebastian McKenzie
e512264be7 Merge pull request #506 from leebyron/rest
Improve performance of rest parameter.
2015-01-16 00:43:56 +11:00
Lee Byron
9624663e60 Improve performance of rest parameter.
Rather than initing an empty array and filling, create an array of the correct size up-front. Minor gain on chromium, but considerably (~5x) faster in spidermonkey/firefox.
2015-01-15 08:41:53 -05:00
Sebastian McKenzie
3cf8a6df01 rename replaceInstanceSuperReferences to replaceSuperReferences in classes transformer and add breaks on method definitions 2015-01-15 23:08:28 +11:00
Sebastian McKenzie
88eacecd72 clarify traceur ignore tests 2015-01-15 23:08:06 +11:00
Sebastian McKenzie
fcd3c9ce65 remove register blacklist tests 2015-01-15 23:07:52 +11:00
Sebastian McKenzie
1733bac3bb move chokidar to top 2015-01-15 23:07:46 +11:00
Sebastian McKenzie
21cd063274 Merge pull request #503 from shinnn/output-file-sync
Use output-file-sync instead of mkdirp
2015-01-15 20:32:48 +11:00
Shinnosuke Watanabe
db5750643a use output-file-sync instead of mkdirp
https://github.com/shinnn/output-file-sync
2015-01-15 18:29:40 +09:00
Sebastian McKenzie
60dae7a88d add build comprehension helper 2015-01-15 20:13:24 +11:00
Sebastian McKenzie
790c924b54 abstract out build comprehension helper 2015-01-15 20:13:16 +11:00
Sebastian McKenzie
50d7a46c7f abstract away remap async to generator helper 2015-01-15 20:09:50 +11:00
Sebastian McKenzie
6f7da38957 abstract out named method helper 2015-01-15 20:07:32 +11:00
Sebastian McKenzie
80764c214a add rest parameter comments 2015-01-15 19:52:53 +11:00
Sebastian McKenzie
55054e45da Revert "Improve performance of rest parameter."
This reverts commit e5463cc2a9.
2015-01-15 19:34:33 +11:00
Sebastian McKenzie
060aa5ba0b fix rest parameter check in types.isReferenced 2015-01-15 19:29:54 +11:00
Sebastian McKenzie
688d619bfe add identifier check to class id inheritance 2015-01-15 19:29:54 +11:00
Sebastian McKenzie
6db7fce543 fix computed member expressions in playground memoization 2015-01-15 19:29:54 +11:00
Sebastian McKenzie
41ff1d4ace upgrade acorn-6to5 2015-01-15 19:29:54 +11:00
Sebastian McKenzie
1c6eeba2e9 Merge pull request #499 from leebyron/rest
Improve performance of rest parameter.
2015-01-15 19:28:32 +11:00
Sebastian McKenzie
488b719fde Merge pull request #494 from gaearon/perf-2
Performance impovements
2015-01-15 19:28:04 +11:00
Sebastian McKenzie
af32080287 Merge pull request #498 from leebyron/patch-1
Update CONTRIBUTING.md
2015-01-15 19:27:07 +11:00
Sebastian McKenzie
4c1cd13536 Merge pull request #501 from shinnn/package-json
Update repository info and homepage URL
2015-01-15 14:36:39 +11:00
Dan Abramov
1002cf7796 Avoid Node allocations by making prototype call statics 2015-01-15 04:18:18 +03:00
Shinnosuke Watanabe
081dd33e73 Update repository info and homepage URL
* link to https://6to5.org
* use user/repo style
2015-01-15 10:07:54 +09:00
Sebastian McKenzie
bd9dc4f15d Merge pull request #500 from shinnn/container-based-build
Use container-based infrastructure on Travis CI
2015-01-15 12:01:33 +11:00
Shinnosuke Watanabe
c7ba566ad6 Use container-based infrastructure on Travis CI
http://docs.travis-ci.com/user/workers/container-based-infrastructure/
2015-01-15 09:55:10 +09:00
Dan Abramov
a1b326a0ab Use object lookup instead of array 2015-01-15 03:51:22 +03:00
Dan Abramov
a452f781b8 Slightly refactor and add explanations for optimized functions 2015-01-15 03:39:24 +03:00
Lee Byron
e5463cc2a9 Improve performance of rest parameter.
Rather than initing an empty array and filling, create an array of the correct size up-front. Minor gain on chromium, but considerably (~5x) faster in spidermonkey/firefox.
2015-01-14 19:08:14 -05:00
Lee Byron
7d732ef503 Update CONTRIBUTING.md 2015-01-14 19:01:53 -05:00
Lee Byron
25cb0c6344 Update CONTRIBUTING.md
Explain git init submodules step when checking out
2015-01-14 18:23:49 -05:00
Sebastian McKenzie
45a52bf478 Merge pull request #496 from swirlycheetah/master
Expand upon the contributing guidelines
2015-01-15 09:41:12 +11:00
Chris Wheatley
a14f971b8a add further commands required for workflow 2015-01-14 22:25:20 +00:00
Chris Wheatley
da65500545 expand upon the contributing guidelines 2015-01-14 22:11:46 +00:00
Dan Abramov
58a91ee9e9 Optimize node type lookup 2015-01-14 23:01:58 +03:00
Dan Abramov
19eaa181a5 Speed up common case where consumer moves only forward 2015-01-14 20:02:40 +03:00
Dan Abramov
c7c90acf3f Store ranges instead of line indexes for performance 2015-01-14 19:14:47 +03:00
Dan Abramov
c6f3a55c03 Refactor buffer for clarity and avoid regex for performance 2015-01-14 19:14:47 +03:00
Sebastian McKenzie
e01b7d288f v2.12.3 2015-01-15 02:29:44 +11:00
Sebastian McKenzie
98c0e185b8 add missing semicolon 2015-01-15 02:27:45 +11:00
Sebastian McKenzie
bf66d78210 add 2.12.3 changelog 2015-01-15 02:27:17 +11:00
Sebastian McKenzie
b60eca0a76 better typeof symbol transformer 2015-01-15 02:24:32 +11:00
Sebastian McKenzie
44f06c0b4c instance and static class method names 2015-01-15 02:24:23 +11:00
Sebastian McKenzie
6c5606b7e8 call transformer methods on traverse context 2015-01-15 02:24:05 +11:00
Sebastian McKenzie
07ddfbeb5d remove pointless prototypeProperties helper call 2015-01-15 01:20:06 +11:00
Sebastian McKenzie
87b890c172 fix parentheses deletion not support multilines - fixes #490 2015-01-15 01:14:49 +11:00
Sebastian McKenzie
245fcfe110 move mutator map checking to after loose test 2015-01-15 01:09:13 +11:00
Sebastian McKenzie
4744719040 v2.12.2 2015-01-14 18:48:29 +11:00
Sebastian McKenzie
a3b814a897 fix default parameters closure scope 2015-01-14 18:46:36 +11:00
Sebastian McKenzie
f5f17f0ccb ignore function params, rest and catch clauses - webpack/webpack#688 2015-01-14 18:39:07 +11:00
Sebastian McKenzie
f6a2acdfb1 v2.12.1 2015-01-14 18:06:51 +11:00
Sebastian McKenzie
e8dba2ad1e add 2.12.1 changelog 2015-01-14 18:05:11 +11:00
Sebastian McKenzie
8ce5c5b608 add jsx to possible extensions 2015-01-14 18:04:14 +11:00
Sebastian McKenzie
0dfea1a51b update notes 2015-01-14 14:54:31 +11:00
Sebastian McKenzie
a4e528e2b3 v2.12.0 2015-01-14 10:14:14 +11:00
Sebastian McKenzie
99ca9b7e9a fix linting errors 2015-01-14 10:12:11 +11:00
Sebastian McKenzie
313e932e7c fix linting errors 2015-01-14 10:11:41 +11:00
Sebastian McKenzie
6a57a4e565 fix linting errors 2015-01-14 10:11:07 +11:00
Sebastian McKenzie
fc3a5f25fb add 2.12.0 changelog 2015-01-14 10:09:47 +11:00
Sebastian McKenzie
e258b85420 clean up loose template literals 2015-01-14 08:55:24 +11:00
Sebastian McKenzie
6b836de306 add #480 test 2015-01-14 07:56:45 +11:00
Sebastian McKenzie
fe6f53d471 Merge pull request #480 from monsanto/loose-templates
Add loose transform for tagged template literals
2015-01-14 07:54:19 +11:00
Sebastian McKenzie
3b259c6b1e clean up 6to5 register-browser 2015-01-14 07:31:51 +11:00
Sebastian McKenzie
2005df3fa2 support non-string jsx literals - fixes #479 2015-01-14 07:31:28 +11:00
Christopher Monsanto
4fdcf685d3 add loose transform for tagged template literals 2015-01-13 14:48:31 -05:00
Sebastian McKenzie
afd3af834d add loose option to computed property names 2015-01-14 00:35:45 +11:00
Sebastian McKenzie
8afec8b12a rename fast mode to loose #465 2015-01-14 00:24:34 +11:00
Sebastian McKenzie
f25ed0b5de add minimum of 1 newline after line comments - fixes #442 and fixes #477 2015-01-13 23:27:52 +11:00
Sebastian McKenzie
4898770d60 ignore test262 in istanbul 2015-01-13 23:27:26 +11:00
Sebastian McKenzie
bd2fa77446 fix fast forOf and add tests 2015-01-13 23:18:46 +11:00
Sebastian McKenzie
1c2bafe0e1 use assignment instead of define for fast classes 2015-01-13 23:18:38 +11:00
Sebastian McKenzie
f8915333b0 remove double spaces in version read in makefile 2015-01-13 22:44:26 +11:00
Sebastian McKenzie
f8275adc6f upgrade acorn-6to5 2015-01-13 22:44:06 +11:00
Sebastian McKenzie
2a97a4233d remove invalid jshint options 2015-01-13 22:26:33 +11:00
Sebastian McKenzie
70eae9f6ce remove functions inside of loops 2015-01-13 22:18:58 +11:00
Sebastian McKenzie
f560062d82 remove loopfunc: false from jshint - thanks @dcousens... 2015-01-13 22:02:30 +11:00
Sebastian McKenzie
0f55a66f5b add "fast" option for transformers 2015-01-13 22:02:04 +11:00
Sebastian McKenzie
f14f72b635 add --mangle option to uglify 2015-01-13 22:01:37 +11:00
Sebastian McKenzie
9aa010d987 v2.11.4 2015-01-13 20:15:35 +11:00
Sebastian McKenzie
bd179ace86 add 2.11.4 changelog 2015-01-13 20:08:38 +11:00
Sebastian McKenzie
4df6d6043e make dependency versions static - fixes #475 2015-01-13 20:07:34 +11:00
Sebastian McKenzie
7ed7475c46 v2.11.3 2015-01-13 14:35:14 +11:00
Sebastian McKenzie
b2dc560a2d allow a string to be passed as the optional option 2015-01-13 14:33:18 +11:00
Sebastian McKenzie
010dbe1cce v2.11.2 2015-01-13 11:29:46 +11:00
Sebastian McKenzie
75cd0bab02 add 2.11.2 changelog 2015-01-13 11:26:07 +11:00
Sebastian McKenzie
3283991ed0 flip es6 geneartors include regenerator tests 2015-01-13 10:56:36 +11:00
Sebastian McKenzie
a66ce5b6ce add ignore ast check to es6 generators test because regenerator outputs an invalid ast 2015-01-13 10:44:17 +11:00
Sebastian McKenzie
9620f50f22 fix #474 test 2015-01-13 10:32:39 +11:00
Sebastian McKenzie
cc049ea851 Merge pull request #474 from amasad/master
Handle esprima-like AST catch clause in a TryStatement. Fixes #473
2015-01-13 10:26:47 +11:00
Amjad Masad
e5a04ae117 Add test 2015-01-12 17:56:16 -05:00
Amjad Masad
e07e74f010 Handle esprima-like AST catch clause in a TryStatement. Fixes #473 2015-01-12 17:32:02 -05:00
Sebastian McKenzie
7b04c501eb fix formatting of #472 2015-01-13 08:54:43 +11:00
Sebastian McKenzie
70349dd4b3 Merge pull request #472 from monsanto/no-traceback-on-syntax-err
don't print traceback on syntax error in CLI
2015-01-13 08:52:42 +11:00
Christopher Monsanto
2cedc843a8 don't print traceback on syntax error in CLI 2015-01-12 16:49:28 -05:00
Sebastian McKenzie
cf40fe3430 v2.11.1 2015-01-13 08:21:54 +11:00
Sebastian McKenzie
09ffeed139 add 2.11.1 changelog 2015-01-13 08:19:33 +11:00
Sebastian McKenzie
18f79d6253 Merge pull request #470 from monsanto/fix-dict-aliasing
remove Dict from core aliasable constructors
2015-01-13 08:17:23 +11:00
Christopher Monsanto
6f5270f38f remove Dict from core aliasable constructors 2015-01-12 14:00:43 -05:00
Brian Donovan
aac7aac373 Merge pull request #469 from clavery/master
remove console.log from react transforms
2015-01-12 10:29:29 -08:00
Charles Lavery
9361470dc7 remove console.log from react transforms 2015-01-12 13:19:32 -05:00
193 changed files with 2718 additions and 1482 deletions

View File

@@ -5,7 +5,6 @@
"camelcase": true,
"unused": true,
"eqnull": true,
"proto": true,
"newcap": true,
"supernew": true,
"noyield": true,
@@ -14,8 +13,6 @@
"boss": true,
"expr": true,
"undef": true,
"loopfunc": true,
"white": true,
"maxparams": 5,
"maxdepth": 4,

View File

@@ -10,3 +10,4 @@ dist
tests.json
CHANGELOG.md
.package.json
coverage

View File

@@ -1,3 +1,4 @@
sudo: false
language: node_js
node_js:
- "0.10"

View File

@@ -11,6 +11,113 @@
_Note: Gaps between patch versions are faulty/broken releases._
## 2.13.2
* **Bug Fix**
* Fix `super` inside of nested functions.
* **Internal**
* Move let scoping TDZ into a separate transformer until it's more solid.
* Use `process.stdin.write` instead of `console.log` in `bin` to avoid sprintfification.
## 2.13.1
* **New Feature**
* Temporal dead zone for block binding.
## 2.13.0
* **New Feature**
* Block scoped functions.
* Add `--loose all` option.
* Add playground mallet operator thanks to [@jridgewell](https://github.com/jridgewell).
* **Bug Fix**
* Rewrote large parts of the let scoping transformer. Now passes all known tests.
* **Internal**
* Even more optimisations thanks to the awesome [@gaearon](https://github.com/gaearon).
* Upgrade `acorn-6to5`.
* Fixes exponentation operator thanks to [@charliesome](https://github.com/charliesome).
* Fix flow optional parameters with types.
* Upgrade `chokidar` to `0.12.6`.
* Upgrade `commander` to `2.6.0`.
* Upgrade `core-js` to `0.4.5`.
* Upgrade `estraverse` to `1.9.1`.
* Upgrade `jshint` to `2.5.11`.
* Upgrade `regenerator` to `0.8.9`.
* Upgrade `regexpu` to `1.0.0`.
* Upgrade `source-map` to `0.1.43`.
* Upgrade `source-map-support` to `0.2.9`.
* Upgrade `browserify` to `8.1.1`.
* Upgrade `chai` to `1.10.0`.
* Upgrade `istanbul` to `0.3.5`.
* Upgrade `mocha` to `2.1.0`.
* Upgrade `uglify-js` to `2.4.16`.
## 2.12.6
* **Bug Fix**
* Add missing flow type traversal keys.
## 2.12.5
* **Internal**
* Fix incorrect `for...in` loop still causing `ember-script` issues.
## 2.12.4
* **Polish**
* Rest parameters now allocate the array before populating.
* **Internal**
* `for...in` loops have been changed to optimised `for` loops - better performance and no enumeration of protoype keys.
* Parts of the code generator have now been optimised thanks to [gaearon](https://github.com/gaearon).
## 2.12.3
* **Spec Compliancy**
* Optional `typeof` transformer checks for `undefined` before passing it to the helper.
* Class methods are now named.
## 2.12.2
* **Internal**
* Exclude nodes in function parameters and catch clauses from `isReferenced` check.
## 2.12.1
* **Internal**
* Add `.jsx` to list of allowed extensions.
## 2.12.0
* **Bug Fix**
* Support non-string JSX literals.
* **New Feature**
* Loose mode for some transformers that enables non-spec behaviour.
* **Internal**
* Uglify `--mangle sort` has been added to the build script, cutting minified scripts in half.
## 2.11.4
* **Internal**
* Make all dependency versions fixed.
## 2.11.3
* **Bug Fix**
* Allow a string to be passed as the `optional` option.
## 2.11.2
* **Bug Fix**
* Support esprima-style catch clause handlers.
* **Polish**
* Don't print a stacktrace for syntax errors in the CLI.
## 2.11.1
* **Bug Fix**
* Remove stray `console.log` outputting debug code.
* Remove `Dict` from `coreAliasing`.
## 2.11.0
* **Bug Fix**

View File

@@ -1,6 +1,20 @@
# Contributing
Before contributing, please read the [code of conduct](https://github.com/6to5/6to5/blob/master/CODE_OF_CONDUCT.md).
Contributions are always welcome, no matter how large or small. Before contributing, please read the [code of conduct](https://github.com/6to5/6to5/blob/master/CODE_OF_CONDUCT.md).
## Developing
#### Workflow
* Fork the repository
* Clone your fork and change directory to it (`git clone git@github.com:yourUserName/6to5.git && cd 6to5`)
* Install the project dependencies (`make bootstrap`)
* Link your forked clone (`npm link`)
* Develop your changes ensuring you're fetching updates from upstream often
* Ensure the test are passing (`make test`)
* Create new pull request explaining your proposed change or reference an issue in your commit message
#### Code Standards
* **General**
* No ES6 syntax features or methods, exclusively ES5.

View File

@@ -1,6 +1,7 @@
BROWSERIFY_CMD = node_modules/browserify/bin/cmd.js
ISTANBUL_CMD = node_modules/istanbul/lib/cli.js cover
UGLIFY_CMD = node_modules/uglify-js/bin/uglifyjs
#UGLIFY_CMD = node_modules/uglify-js/bin/uglifyjs --mangle sort
JSHINT_CMD = node_modules/jshint/bin/jshint
MOCHA_CMD = node_modules/mocha/bin/_mocha
@@ -71,7 +72,7 @@ publish:
make test
read -p "Version: " version; \
read -p "Version: " version; \
npm version $$version --message "v%s"
make build

View File

@@ -10,4 +10,5 @@
* Split up ES5 getter/setter transforming and ES6 property methods into separate transformers.
* Add autoindentation.
* Move `super` transformation from classes into a separate transformer that also supports object expressions.
* Prefix all fast transformers with `fast`, make them declare that they're a fast transformer similar to optional transformers and add a `--fast` flag to enable them all.
* Remove fast transformer backwards compatibility.
* Rename let scoping transformer to block scoping.

View File

@@ -1,9 +1,9 @@
var chokidar = require("chokidar");
var mkdirp = require("mkdirp");
var path = require("path");
var util = require("./util");
var fs = require("fs");
var _ = require("lodash");
var outputFileSync = require("output-file-sync");
var chokidar = require("chokidar");
var path = require("path");
var util = require("./util");
var fs = require("fs");
var _ = require("lodash");
module.exports = function (commander, filenames, opts) {
if (commander.sourceMapsInline) {
@@ -15,16 +15,13 @@ module.exports = function (commander, filenames, opts) {
var data = util.compile(src, { sourceMapName: dest });
var up = path.normalize(dest + "/..");
mkdirp.sync(up);
if (commander.sourceMaps) {
var mapLoc = dest + ".map";
data.code = util.addSourceMappingUrl(data.code, mapLoc);
fs.writeFileSync(mapLoc, JSON.stringify(data.map));
outputFileSync(mapLoc, JSON.stringify(data.map));
}
fs.writeFileSync(dest, data.code);
outputFileSync(dest, data.code);
console.log(src + " -> " + dest);
};

View File

@@ -28,7 +28,7 @@ module.exports = function (commander, filenames) {
map.setSourceContent(filename, result.actual);
consumer.eachMapping(function (mapping) {
map._mappings.push({
map._mappings.add({
generatedLine: mapping.generatedLine + offset,
generatedColumn: mapping.generatedColumn,
originalLine: mapping.originalLine,
@@ -63,7 +63,7 @@ module.exports = function (commander, filenames) {
fs.writeFileSync(commander.outFile, result.code);
} else {
console.log(result.code);
process.stdout.write(result.code + "\n");
}
};

View File

@@ -18,6 +18,7 @@ commander.option("-m, --modules [modules]", "Module formatter type to use [commo
commander.option("-l, --whitelist [whitelist]", "Whitelist of transformers to ONLY use", util.list);
commander.option("-b, --blacklist [blacklist]", "Blacklist of transformers to NOT use", util.list);
commander.option("-i, --optional [list]", "List of optional transformers to enable", util.list);
commander.option("--loose [list]", "List of transformers to enable their loose mode", util.list);
commander.option("-o, --out-file [out]", "Compile all input files into a single file");
commander.option("-d, --out-dir [out]", "Compile an input directory of modules into an output directory");
commander.option("-c, --remove-comments", "Remove comments from the compiled code", false);
@@ -33,13 +34,10 @@ commander.on("--help", function(){
console.log(" " + title + ":");
console.log();
var hasOptional = true;
_.each(_.keys(obj).sort(), function (key) {
if (key[0] === "_") return;
if (obj[key].optional) {
hasOptional = true;
key = "[" + key + "]";
}
@@ -116,6 +114,7 @@ exports.opts = {
comments: !commander.removeComments,
runtime: commander.runtime,
modules: commander.modules,
loose: commander.loose,
format: {
indent: {
style: util.repeat(parseInt(commander.indent))

View File

@@ -20,7 +20,17 @@ exports.transform = function (filename, code, opts) {
opts = _.extend(opts || {}, index.opts);
opts.filename = filename;
var result = to5.transform(code, opts);
var result;
try {
result = to5.transform(code, opts);
} catch (e) {
if (e instanceof SyntaxError) {
console.error("SyntaxError:", e.message);
process.exit(1);
} else {
throw e;
}
}
result.filename = filename;
result.actual = code;
return result;

View File

@@ -96,8 +96,8 @@ function replEval(code, context, filename, callback) {
var result;
try {
if (/^\((.*?)\n\)$/.test(code)) {
code = code.slice(1, -2); // remove "(" and "\n)"
if (code[0] === "(" && code[code.length - 1] === ")") {
code = code.slice(1, -1); // remove "(" and ")"
}
result = _eval(code, filename);

View File

@@ -1,3 +1,5 @@
"use strict";
var transform = module.exports = require("./transformation/transform");
transform.version = require("../../package").version;

View File

@@ -1,3 +1,5 @@
"use strict";
module.exports = File;
var SHEBANG_REGEX = /^\#\!.*/;
@@ -24,6 +26,7 @@ File.helpers = [
"prototype-properties",
"apply-constructor",
"tagged-template-literal",
"tagged-template-literal-loose",
"interop-require",
"to-array",
"sliced-to-array",
@@ -41,7 +44,8 @@ File.helpers = [
File.excludeHelpersFromRuntime = [
"async-to-generator",
"typeof"
"typeof",
"tagged-template-literal-loose"
];
File.normaliseOptions = function (opts) {
@@ -63,6 +67,7 @@ File.normaliseOptions = function (opts) {
filename: "unknown",
modules: "common",
runtime: false,
loose: [],
code: true,
ast: true
});
@@ -72,6 +77,23 @@ File.normaliseOptions = function (opts) {
opts.blacklist = util.arrayify(opts.blacklist);
opts.whitelist = util.arrayify(opts.whitelist);
opts.optional = util.arrayify(opts.optional);
opts.loose = util.arrayify(opts.loose);
if (_.contains(opts.loose, "all")) {
opts.loose = Object.keys(transform.transformers);
}
// todo: remove in 3.0.0
_.each({
fastForOf: "forOf",
classesFastSuper: "classes"
}, function (newTransformer, oldTransformer) {
if (_.contains(opts.optional, oldTransformer)) {
_.pull(opts.optional, oldTransformer);
opts.loose.push(newTransformer);
}
});
_.defaults(opts, {
moduleRoot: opts.sourceRoot
@@ -101,10 +123,15 @@ File.normaliseOptions = function (opts) {
transform._ensureTransformerNames("blacklist", opts.blacklist);
transform._ensureTransformerNames("whitelist", opts.whitelist);
transform._ensureTransformerNames("optional", opts.optional);
transform._ensureTransformerNames("loose", opts.loose);
return opts;
};
File.prototype.isLoose = function (key) {
return _.contains(this.opts.loose, key);
};
File.prototype.getTransformers = function () {
var file = this;
var transformers = [];
@@ -304,7 +331,10 @@ File.prototype.generateUid = function (name, scope) {
};
File.prototype.generateUidIdentifier = function (name, scope) {
return t.identifier(this.generateUid(name, scope));
scope = scope || this.scope;
var id = t.identifier(this.generateUid(name, scope));
scope.add(id);
return id;
};
File.prototype._generateUid = function (name) {

View File

@@ -1,3 +1,5 @@
"use strict";
module.exports = Buffer;
var util = require("../util");
@@ -67,32 +69,57 @@ Buffer.prototype.removeLast = function (cha) {
Buffer.prototype.newline = function (i, removeLast) {
if (this.format.compact) return;
if (_.isBoolean(i)) {
removeLast = i;
i = null;
}
removeLast = removeLast || false;
if (_.isNumber(i)) {
if (this.endsWith("{\n")) i--;
if (this.endsWith(util.repeat(i, "\n"))) return;
for (var j = 0; j < i; j++) {
this.newline(null, removeLast);
while (i--) {
this._newline(removeLast);
}
return;
}
if (_.isBoolean(i)) {
removeLast = i;
}
this._newline(removeLast);
};
Buffer.prototype._newline = function (removeLast) {
if (removeLast && this.isLast("\n")) this.removeLast("\n");
this.removeLast(" ");
// remove whitespace if last character was a newline
this.buf = this.buf.replace(/\n +$/, "\n");
this._removeSpacesAfterLastNewline();
this._push("\n");
};
/**
* If buffer ends with a newline and some spaces after it, trim those spaces.
*/
Buffer.prototype._removeSpacesAfterLastNewline = function () {
var lastNewlineIndex = this.buf.lastIndexOf('\n');
if (lastNewlineIndex === -1)
return;
var index = this.buf.length - 1;
while (index > lastNewlineIndex) {
if (this.buf[index] !== ' ') {
break;
}
index--;
}
if (index === lastNewlineIndex) {
this.buf = this.buf.substring(0, index + 1);
}
};
Buffer.prototype.push = function (str, noIndent) {
if (this._indent && !noIndent && str !== "\n") {
// we have an indent level and we aren't pushing a newline

View File

@@ -1,3 +1,5 @@
"use strict";
module.exports = function (ast, opts, code) {
var gen = new CodeGenerator(ast, opts, code);
return gen.generate();

View File

@@ -1,3 +1,5 @@
"use strict";
exports.File = function (node, print) {
print(node.program);
};

View File

@@ -1,3 +1,5 @@
"use strict";
exports.ClassExpression =
exports.ClassDeclaration = function (node, print) {
this.push("class");

View File

@@ -1,3 +1,5 @@
"use strict";
exports.ComprehensionBlock = function (node, print) {
this.keyword("for");
this.push("(");

View File

@@ -1,3 +1,5 @@
"use strict";
var util = require("../../util");
var t = require("../../types");
var _ = require("lodash");

View File

@@ -1,3 +1,35 @@
exports.ClassProperty = function () {
throw new Error("not implemented");
"use strict";
exports.AnyTypeAnnotation =
exports.ArrayTypeAnnotation =
exports.BooleanTypeAnnotation =
exports.ClassProperty =
exports.DeclareClass =
exports.DeclareFunction =
exports.DeclareModule =
exports.DeclareVariable =
exports.FunctionTypeAnnotation =
exports.FunctionTypeParam =
exports.GenericTypeAnnotation =
exports.InterfaceExtends =
exports.InterfaceDeclaration =
exports.IntersectionTypeAnnotation =
exports.NullableTypeAnnotation =
exports.NumberTypeAnnotation =
exports.StringLiteralTypeAnnotation =
exports.StringTypeAnnotation =
exports.TupleTypeAnnotation =
exports.TypeofTypeAnnotation =
exports.TypeAlias =
exports.TypeAnnotation =
exports.TypeParameterDeclaration =
exports.TypeParameterInstantiation =
exports.ObjectTypeAnnotation =
exports.ObjectTypeCallProperty =
exports.ObjectTypeIndexer =
exports.ObjectTypeProperty =
exports.QualifiedTypeIdentifier =
exports.UnionTypeAnnotation =
exports.VoidTypeAnnotation = function () {
// todo: implement these once we have a `--keep-types` option
};

View File

@@ -1,3 +1,5 @@
"use strict";
var t = require("../../types");
var _ = require("lodash");

View File

@@ -1,3 +1,5 @@
"use strict";
var t = require("../../types");
exports._params = function (node, print) {

View File

@@ -1,3 +1,5 @@
"use strict";
var t = require("../../types");
var _ = require("lodash");

View File

@@ -1,3 +1,5 @@
"use strict";
var _ = require("lodash");
_.each(["BindMemberExpression", "BindFunctionExpression"], function (type) {

View File

@@ -1,3 +1,5 @@
"use strict";
var util = require("../../util");
var t = require("../../types");
@@ -107,7 +109,16 @@ exports.TryStatement = function (node, print) {
this.keyword("try");
print(node.block);
this.space();
print(node.handler);
// Esprima bug puts the catch clause in a `handlers` array.
// see https://code.google.com/p/esprima/issues/detail?id=433
// We run into this from regenerator generated ast.
if (node.handlers) {
print(node.handlers[0]);
} else {
print(node.handler);
}
if (node.finalizer) {
this.space();
this.push("finally ");
@@ -160,16 +171,18 @@ exports.VariableDeclaration = function (node, print, parent) {
var inits = 0;
var noInits = 0;
for (var i in node.declarations) {
if (node.declarations[i].init) {
inits++;
} else {
noInits++;
if (!t.isFor(parent)) {
for (var i = 0; i < node.declarations.length; i++) {
if (node.declarations[i].init) {
inits++;
} else {
noInits++;
}
}
}
var sep = ",";
if (inits > noInits) { // more inits than noinits
if (inits > noInits) { // more inits than no inits so let's add a newline
sep += "\n" + util.repeat(node.kind.length + 1);
} else {
sep += " ";

View File

@@ -1,3 +1,5 @@
"use strict";
var _ = require("lodash");
exports.TaggedTemplateExpression = function (node, print) {

View File

@@ -1,3 +1,5 @@
"use strict";
var _ = require("lodash");
exports.Identifier = function (node) {

View File

@@ -1,3 +1,5 @@
"use strict";
module.exports = Node;
var whitespace = require("./whitespace");
@@ -13,7 +15,7 @@ var find = function (obj, node, parent) {
for (var i = 0; i < types.length; i++) {
var type = types[i];
if (t["is" + type](node)) {
if (t.is(type, node)) {
var fn = obj[type];
result = fn(node, parent);
if (result != null) break;
@@ -28,13 +30,11 @@ function Node(node, parent) {
this.node = node;
}
Node.prototype.isUserWhitespacable = function () {
return t.isUserWhitespacable(this.node);
Node.isUserWhitespacable = function (node) {
return t.isUserWhitespacable(node);
};
Node.prototype.needsWhitespace = function (type) {
var parent = this.parent;
var node = this.node;
Node.needsWhitespace = function (node, parent, type) {
if (!node) return 0;
if (t.isExpressionStatement(node)) {
@@ -51,18 +51,15 @@ Node.prototype.needsWhitespace = function (type) {
return lines || 0;
};
Node.prototype.needsWhitespaceBefore = function () {
return this.needsWhitespace("before");
Node.needsWhitespaceBefore = function (node, parent) {
return Node.needsWhitespace(node, parent, "before");
};
Node.prototype.needsWhitespaceAfter = function () {
return this.needsWhitespace("after");
Node.needsWhitespaceAfter = function (node, parent) {
return Node.needsWhitespace(node, parent, "after");
};
Node.prototype.needsParens = function () {
var parent = this.parent;
var node = this.node;
Node.needsParens = function (node, parent) {
if (!parent) return false;
if (t.isNewExpression(parent) && parent.callee === node) {
@@ -77,10 +74,7 @@ Node.prototype.needsParens = function () {
return find(parens, node, parent);
};
Node.prototype.needsParensNoLineTerminator = function () {
var parent = this.parent;
var node = this.node;
Node.needsParensNoLineTerminator = function (node, parent) {
if (!parent) return false;
// no comments
@@ -100,17 +94,18 @@ Node.prototype.needsParensNoLineTerminator = function () {
return false;
};
_.each(Node.prototype, function (fn, key) {
Node[key] = function (node, parent) {
var n = new Node(node, parent);
_.each(Node, function (fn, key) {
Node.prototype[key] = function () {
// Avoid leaking arguments to prevent deoptimization
var skipCount = 2;
var args = new Array(arguments.length - skipCount);
var args = new Array(arguments.length + 2);
args[0] = this.node;
args[1] = this.parent;
for (var i = 0; i < args.length; i++) {
args[i] = arguments[i + 2];
args[i + 2] = arguments[i];
}
return n[key].apply(n, args);
return Node[key].apply(null, args);
};
});

View File

@@ -1,3 +1,5 @@
"use strict";
var t = require("../../types");
var _ = require("lodash");

View File

@@ -1,3 +1,5 @@
"use strict";
var _ = require("lodash");
var t = require("../../types");

View File

@@ -1,3 +1,5 @@
"use strict";
module.exports = Position;
function Position() {

View File

@@ -1,3 +1,5 @@
"use strict";
module.exports = SourceMap;
var sourceMap = require("source-map");

View File

@@ -1,10 +1,40 @@
"use strict";
module.exports = Whitespace;
var _ = require("lodash");
var _ = require("lodash");
/**
* 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.
*
* @param {Number} i Current index in the loop
* @param {Number} base Start index for which to return 0
* @param {Number} max Array length
* @returns {Number} shiftedIndex
*/
function getLookupIndex(i, base, max) {
i += base;
if (i >= max)
i -= max;
return i;
}
function Whitespace(tokens, comments) {
this.tokens = _.sortBy(tokens.concat(comments), "start");
this.used = [];
this.used = {};
// Profiling this code shows that while generator passes over it, indexes
// returned by `getNewlinesBefore` and `getNewlinesAfter` are always increasing.
// We use this implementation detail for an optimization: instead of always
// starting to look from `this.tokens[0]`, we will start `for` loops from the
// previous successful match. We will enumerate all tokens—but the common
// case will be much faster.
this._lastFoundIndex = 0;
}
Whitespace.prototype.getNewlinesBefore = function (node) {
@@ -13,13 +43,17 @@ Whitespace.prototype.getNewlinesBefore = function (node) {
var tokens = this.tokens;
var token;
for (var i = 0; i < tokens.length; i++) {
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];
// this is the token this node starts with
if (node.start === token.start) {
startToken = tokens[i - 1];
endToken = token;
this._lastFoundIndex = i;
break;
}
}
@@ -33,13 +67,17 @@ Whitespace.prototype.getNewlinesAfter = function (node) {
var tokens = this.tokens;
var token;
for (var i = 0; i < tokens.length; i++) {
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];
// this is the token this node ends with
if (node.end === token.end) {
startToken = token;
endToken = tokens[i + 1];
this._lastFoundIndex = i;
break;
}
}
@@ -47,19 +85,24 @@ Whitespace.prototype.getNewlinesAfter = function (node) {
if (endToken.type.type === "eof") {
return 1;
} else {
return this.getNewlinesBetween(startToken, endToken);
var lines = this.getNewlinesBetween(startToken, endToken);
if (node.type === "Line" && !lines) {
// line comment
return 1;
} else {
return lines;
}
}
};
Whitespace.prototype.getNewlinesBetween = function (startToken, endToken) {
var start = startToken ? startToken.loc.end.line : 1;
var end = endToken.loc.start.line;
var lines = 0;
for (var line = start; line < end; line++) {
if (!_.contains(this.used, line)) {
this.used.push(line);
if (typeof this.used[line] === 'undefined') {
this.used[line] = true;
lines++;
}
}

View File

@@ -1,3 +1,5 @@
"use strict";
var transform = require("./transformation/transform");
var util = require("./util");
var fs = require("fs");

View File

@@ -1,3 +1,5 @@
"use strict";
var t = require("./types");
var _ = require("lodash");

View File

@@ -1,2 +1,4 @@
"use strict";
require("core-js/shim");
require("regenerator/runtime");

View File

@@ -1,4 +1,7 @@
// Required to safely use 6to5/register within a browserify codebase.
"use strict";
// required to safely use 6to5/register within a browserify codebase
module.exports = function () {};
require("./polyfill");

View File

@@ -1,3 +1,5 @@
"use strict";
require("./polyfill");
var sourceMapSupport = require("source-map-support");
@@ -21,51 +23,6 @@ sourceMapSupport.install({
}
});
//
var blacklist = [];
var blacklistTest = function (transformer, code) {
try {
if (_.isFunction(code)) {
code();
} else {
new Function(code);
}
blacklist.push(transformer);
} catch (err) {
if (err.name !== "SyntaxError") throw err;
}
};
blacklistTest("arrayComprehension", "var foo = [for (foo of bar) foo * foo];");
blacklistTest("generatorComprehension", "var foo = (for (foo of bar) foo * foo)");
blacklistTest("arrowFunctions", "var foo = x => x * x;");
blacklistTest("classes", "class Foo {}");
blacklistTest("computedPropertyNames", "var foo = { [foo]: bar };");
blacklistTest("constants", function () {
try {
new Function("const foo = 'foo';\nfoo = 'wow';");
} catch (err) {
return; // constants are supported
}
throw new SyntaxError;
});
blacklistTest("defaultParamaters", "var foo = function (bar = 0) {};");
blacklistTest("destructuring", "var { x, y } = { x: 0, y: 0 };");
blacklistTest("forOf", "for (var foo of bar) {}");
blacklistTest("generators", "function* foo() {}\nasync function bar() {}"); // generators/async functions delegated to same transformer
blacklistTest("letScoping", "let foo = 0;");
blacklistTest("modules", 'import foo from "from";');
blacklistTest("propertyMethodAssignment", "{ get foo() {} }");
blacklistTest("propertyNameShorthand", "var foo = { x, y };");
blacklistTest("restParameters", "function foo(...bar) {}");
blacklistTest("spread", "foo(...bar);");
blacklistTest("templateLiterals", "var foo = `foo`;");
blacklistTest("unicodeRegex", function () { new RegExp("foo", "u"); });
//
var transformOpts = {};
var ignoreRegex = /node_modules/;
var onlyRegex;
@@ -95,7 +52,6 @@ var loader = function (m, filename) {
result = result || to5.transformFileSync(filename, _.extend({
whitelist: whitelist,
blacklist: blacklist,
sourceMap: true,
ast: false
}, transformOpts));
@@ -123,7 +79,7 @@ var hookExtensions = function (_exts) {
});
};
hookExtensions([".es6", ".es", ".js"]);
hookExtensions(util.canCompile.EXTENSIONS);
module.exports = function (opts) {
// normalise options

View File

@@ -1,3 +1,5 @@
"use strict";
var generator = require("./generation/generator");
var util = require("./util");
var File = require("./file");

View File

@@ -0,0 +1,17 @@
"use strict";
/**
* A trick from Bluebird to force V8 to use fast properties for an object.
* Read more: http://stackoverflow.com/questions/24987896/
*
* Use %HasFastProperties(obj) and --allow-natives-syntax to check whether
* a particular object already has fast properties.
*/
module.exports = function toFastProperties(obj) {
/*jshint -W027*/
function f() {}
f.prototype = obj;
return f;
eval(obj);
};

View File

@@ -0,0 +1,45 @@
"use strict";
var explode = require("./explode-assignable-expression");
var t = require("../../types");
module.exports = function (exports, opts) {
var isAssignment = function (node) {
return node.operator === opts.operator + "=";
};
var buildAssignment = function (left, right) {
return t.assignmentExpression("=", left, right);
};
exports.ExpressionStatement = function (node, parent, scope, context, file) {
var expr = node.expression;
if (!isAssignment(expr)) return;
var nodes = [];
var exploded = explode(expr.left, nodes, file, scope, true);
nodes.push(t.expressionStatement(
buildAssignment(exploded.ref, opts.build(exploded.uid, expr.right))
));
return nodes;
};
exports.AssignmentExpression = function (node, parent, scope, context, file) {
if (t.isExpressionStatement(parent)) return;
if (!isAssignment(node)) return;
var nodes = [];
var exploded = explode(node.left, nodes, file, scope);
nodes.push(opts.build(exploded.uid, node.right));
nodes.push(exploded.ref);
return t.toSequenceExpression(nodes, scope);
};
exports.BinaryExpression = function (node) {
if (node.operator !== opts.operator) return;
return opts.build(node.left, node.right);
};
};

View File

@@ -0,0 +1,25 @@
"use strict";
var t = require("../../types");
module.exports = function build(node, buildBody) {
var self = node.blocks.shift();
if (!self) return;
var child = build(node, buildBody);
if (!child) {
// last item
child = buildBody();
// add a filter as this is our final stop
if (node.filter) {
child = t.ifStatement(node.filter, t.blockStatement([child]));
}
}
return t.forOfStatement(
t.variableDeclaration("let", [t.variableDeclarator(self.left)]),
self.right,
t.blockStatement([child])
);
};

View File

@@ -0,0 +1,45 @@
"use strict";
var explode = require("./explode-assignable-expression");
var t = require("../../types");
module.exports = function (exports, opts) {
var buildAssignment = function (left, right) {
return t.assignmentExpression("=", left, right);
};
exports.ExpressionStatement = function (node, parent, scope, context, file) {
var expr = node.expression;
if (!opts.is(expr, file)) return;
var nodes = [];
var exploded = explode(expr.left, nodes, file, scope);
nodes.push(t.ifStatement(
opts.build(exploded.uid, file),
t.expressionStatement(buildAssignment(exploded.ref, expr.right))
));
return nodes;
};
exports.AssignmentExpression = function (node, parent, scope, context, file) {
if (t.isExpressionStatement(parent)) return;
if (!opts.is(node, file)) return;
var nodes = [];
var exploded = explode(node.left, nodes, file, scope);
nodes.push(t.logicalExpression(
"&&",
opts.build(exploded.uid, file),
buildAssignment(exploded.ref, node.right)
));
// todo: duplicate expression node
nodes.push(exploded.ref);
return t.toSequenceExpression(nodes, scope);
};
};

View File

@@ -0,0 +1,57 @@
"use strict";
var t = require("../../types");
var getObjRef = function (node, nodes, file, scope) {
var ref;
if (t.isIdentifier(node)) {
ref = node;
} else if (t.isMemberExpression(node)) {
ref = node.object;
} else {
throw new Error("We can't explode this node type " + node.type);
}
var temp = scope.generateUidBasedOnNode(ref, file);
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(temp, ref)
]));
return temp;
};
var getPropRef = function (node, nodes, file, scope) {
var prop = node.property;
var key = t.toComputedKey(node, prop);
if (t.isLiteral(key)) return key;
var temp = scope.generateUidBasedOnNode(prop, file);
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(temp, prop)
]));
return temp;
};
module.exports = function (node, nodes, file, scope, allowedSingleIdent) {
var obj;
if (t.isIdentifier(node) && allowedSingleIdent) {
obj = node;
} else {
obj = getObjRef(node, nodes, file, scope);
}
var ref, uid;
if (t.isIdentifier(node)) {
ref = node;
uid = obj;
} else {
var prop = getPropRef(node, nodes, file, scope);
var computed = node.computed || t.isLiteral(prop);
uid = ref = t.memberExpression(obj, prop, computed);
}
return {
uid: uid,
ref: ref
};
};

View File

@@ -0,0 +1,50 @@
"use strict";
var traverse = require("../../traverse");
var util = require("../../util");
var t = require("../../types");
var traverser = {
enter: function (node, parent, scope, context, state) {
// check if this node is an identifier that matches the same as our function id
if (!t.isIdentifier(node, { name: state.id })) return;
// check if this node is the one referenced
if (!t.isReferenced(node, parent)) return;
// check that we don't have a local variable declared as that removes the need
// for the wrapper
var localDeclar = scope.get(state.id, true);
if (localDeclar !== state.outerDeclar) return;
state.selfReference = true;
context.stop();
}
};
exports.property = function (node, file, scope) {
var key = t.toComputedKey(node, node.key);
if (!t.isLiteral(key)) return node; // we can't set a function id with this
var id = t.toIdentifier(key.value);
key = t.identifier(id);
var state = {
id: id,
selfReference: false,
outerDeclar: scope.get(id, true),
};
traverse(node, traverser, scope, state);
if (state.selfReference) {
node.value = util.template("property-method-assignment-wrapper", {
FUNCTION: node.value,
FUNCTION_ID: key,
FUNCTION_KEY: file.generateUidIdentifier(id, scope),
WRAPPER_KEY: file.generateUidIdentifier(id + "Wrapper", scope)
});
} else {
node.value.id = key;
}
};

View File

@@ -0,0 +1,31 @@
"use strict";
var traverse = require("../../traverse");
var t = require("../../types");
module.exports = function (node, callId) {
node.async = false;
node.generator = true;
traverse(node, {
enter: function (node, parent, scope, context) {
if (t.isFunction(node)) context.skip();
if (t.isAwaitExpression(node)) {
node.type = "YieldExpression";
}
}
});
var call = t.callExpression(callId, [node]);
if (t.isFunctionDeclaration(node)) {
var declar = t.variableDeclaration("var", [
t.variableDeclarator(node.id, call)
]);
declar._blockHoist = true;
return declar;
} else {
return call;
}
};

View File

@@ -0,0 +1,246 @@
"use strict";
module.exports = ReplaceSupers;
var traverse = require("../../traverse");
var t = require("../../types");
/**
* Description
*
* @param {Object} methodNode
* @param {Object} className
* @param {Object} superName
* @param {Boolean} isLoose
* @param {File} file
*/
function ReplaceSupers(methodNode, className, superName, isLoose, file) {
this.topLevelThisReference = null;
this.methodNode = methodNode;
this.className = className;
this.superName = superName;
this.isLoose = isLoose;
this.file = file;
}
/**
* Gets a node representing the super class value of the named property.
*
* @example
*
* _get(Object.getPrototypeOf(CLASS.prototype), "METHOD", this)
*
* @param {Node} property
* @param {boolean} isStatic
* @param {boolean} isComputed
*
* @returns {Node}
*/
ReplaceSupers.prototype.superProperty = function (property, isStatic, isComputed, thisExpression) {
return t.callExpression(
this.file.addHelper("get"),
[
t.callExpression(
t.memberExpression(t.identifier("Object"), t.identifier("getPrototypeOf")),
[
isStatic ? this.className : t.memberExpression(this.className, t.identifier("prototype"))
]
),
isComputed ? property : t.literal(property.name),
thisExpression
]
);
};
/**
* Description
*
* @param {Object} node
* @param {Object} id
* @param {Object} parent
* @returns {Object}
*/
ReplaceSupers.prototype.looseSuperProperty = function (methodNode, id, parent) {
var methodName = methodNode.key;
var superName = this.superName || t.identifier("Function");
if (parent.property === id) {
return;
} else if (t.isCallExpression(parent, { callee: id })) {
// super(); -> ClassName.prototype.MethodName.call(this);
parent.arguments.unshift(t.thisExpression());
if (methodName.name === "constructor") {
// constructor() { super(); }
return t.memberExpression(superName, t.identifier("call"));
} else {
id = superName;
// foo() { super(); }
if (!methodNode.static) {
id = t.memberExpression(id, t.identifier("prototype"));
}
id = t.memberExpression(id, methodName, methodNode.computed);
return t.memberExpression(id, t.identifier("call"));
}
} else if (t.isMemberExpression(parent) && !methodNode.static) {
// super.test -> ClassName.prototype.test
return t.memberExpression(superName, t.identifier("prototype"));
} else {
return superName;
}
};
/**
* Description
*/
ReplaceSupers.prototype.replace = function () {
this.traverseLevel(this.methodNode.value, true);
};
/**
* Description
*
* @param {Object} node
* @param {Boolean} topLevel
*/
ReplaceSupers.prototype.traverseLevel = function (node, topLevel) {
var self = this;
traverse(node, {
enter: function (node, parent, scope, context) {
if (t.isFunction(node) && !t.isArrowFunctionExpression(node)) {
// we need to call traverseLevel again so we're context aware
self.traverseLevel(node, false);
return context.skip();
}
if (t.isProperty(node, { method: true }) || t.isMethodDefinition(node)) {
// break on object methods
return context.skip();
}
var getThisReference = function () {
if (topLevel) {
// top level so `this` is the instance
return t.thisExpression();
} else {
// not in the top level so we need to create a reference
return self.getThisReference();
}
};
var callback = self.specHandle;
if (self.isLoose) callback = self.looseHandle;
return callback.call(self, getThisReference, node, parent);
}
});
};
/**
* Description
*/
ReplaceSupers.prototype.getThisReference = function () {
if (this.topLevelThisReference) {
return this.topLevelThisReference;
} else {
var ref = this.topLevelThisReference = this.file.generateUidIdentifier("this");
this.methodNode.value.body.body.unshift(t.variableDeclaration("var", [
t.variableDeclarator(this.topLevelThisReference, t.thisExpression())
]));
return ref;
}
};
/**
* Description
*
* @param {Function} getThisReference
* @param {Object} node
* @param {Object} parent
*/
ReplaceSupers.prototype.looseHandle = function (getThisReference, node, parent) {
if (t.isIdentifier(node, { name: "super" })) {
return this.looseSuperProperty(this.methodNode, node, parent);
} else if (t.isCallExpression(node)) {
var callee = node.callee;
if (!t.isMemberExpression(callee)) return;
if (callee.object.name !== "super") return;
// super.test(); -> ClassName.prototype.MethodName.call(this);
t.appendToMemberExpression(callee, t.identifier("call"));
node.arguments.unshift(getThisReference());
}
};
/**
* Description
*
* @param {Function} getThisReference
* @param {Object} node
* @param {Object} parent
*/
ReplaceSupers.prototype.specHandle = function (getThisReference, node, parent) {
var methodNode = this.methodNode;
var property;
var computed;
var args;
if (t.isIdentifier(node, { name: "super" })) {
if (!(t.isMemberExpression(parent) && !parent.computed && parent.property === node)) {
throw this.file.errorWithNode(node, "illegal use of bare super");
}
} else if (t.isCallExpression(node)) {
var callee = node.callee;
if (t.isIdentifier(callee, { name: "super" })) {
// super(); -> _get(Object.getPrototypeOf(ClassName), "MethodName", this).call(this);
property = methodNode.key;
computed = methodNode.computed;
args = node.arguments;
} else {
if (!t.isMemberExpression(callee)) return;
if (callee.object.name !== "super") return;
// super.test(); -> _get(Object.getPrototypeOf(ClassName.prototype), "test", this).call(this);
property = callee.property;
computed = callee.computed;
args = node.arguments;
}
} else if (t.isMemberExpression(node)) {
if (!t.isIdentifier(node.object, { name: "super" })) return;
// super.name; -> _get(Object.getPrototypeOf(ClassName.prototype), "name", this);
property = node.property;
computed = node.computed;
}
if (!property) return;
var thisReference = getThisReference();
var superProperty = this.superProperty(property, methodNode.static, computed, thisReference);
if (args) {
if (args.length === 1 && t.isSpreadElement(args[0])) {
// super(...arguments);
return t.callExpression(
t.memberExpression(superProperty, t.identifier("apply")),
[thisReference, args[0].argument]
);
} else {
return t.callExpression(
t.memberExpression(superProperty, t.identifier("call")),
[thisReference].concat(args)
);
}
} else {
return superProperty;
}
};

View File

@@ -0,0 +1,21 @@
"use strict";
var t = require("../../types");
exports.has = function (node) {
var first = node.body[0];
return t.isExpressionStatement(first) && t.isLiteral(first.expression, { value: "use strict" });
};
exports.wrap = function (node, callback) {
var useStrictNode;
if (exports.has(node)) {
useStrictNode = node.body.shift();
}
callback();
if (useStrictNode) {
node.body.unshift(useStrictNode);
}
};

View File

@@ -1,3 +1,5 @@
"use strict";
module.exports = DefaultFormatter;
var traverse = require("../../traverse");
@@ -16,35 +18,51 @@ function DefaultFormatter(file) {
//this.checkCollisions();
}
var exportsTraverser = {
enter: function (node, parent, scope, context, localExports) {
var declar = node && node.declaration;
if (t.isExportDeclaration(node) && declar && t.isStatement(declar)) {
_.extend(localExports, t.getIds(declar, true));
}
}
};
DefaultFormatter.prototype.getLocalExports = function () {
var localExports = {};
traverse(this.file.ast, {
enter: function (node) {
var declar = node && node.declaration;
if (t.isExportDeclaration(node) && declar && t.isStatement(declar)) {
_.extend(localExports, t.getIds(declar, true));
}
}
});
traverse(this.file.ast, exportsTraverser, null, localExports);
return localExports;
};
var importsTraverser = {
enter: function (node, parent, scope, context, localImports) {
if (t.isImportDeclaration(node)) {
_.extend(localImports, t.getIds(node, true));
}
}
};
DefaultFormatter.prototype.getLocalImports = function () {
var localImports = {};
traverse(this.file.ast, {
enter: function (node) {
if (t.isImportDeclaration(node)) {
_.extend(localImports, t.getIds(node, true));
}
}
});
traverse(this.file.ast, importsTraverser, null, localImports);
return localImports;
};
var collissionsTraverser = {
enter: function (node, parent, scope, context, check) {
if (t.isAssignmentExpression(node)) {
var left = node.left;
if (t.isMemberExpression(left)) {
while (left.object) left = left.object;
}
check(left);
} else if (t.isDeclaration(node)) {
_.each(t.getIds(node, true), check);
}
}
};
DefaultFormatter.prototype.checkCollisions = function () {
// todo: all check export collissions
@@ -61,21 +79,7 @@ DefaultFormatter.prototype.checkCollisions = function () {
}
};
traverse(file.ast, {
enter: function (node) {
if (t.isAssignmentExpression(node)) {
var left = node.left;
if (t.isMemberExpression(left)) {
while (left.object) left = left.object;
}
check(left);
} else if (t.isDeclaration(node)) {
_.each(t.getIds(node, true), check);
}
}
});
traverse(file.ast, collissionsTraverser, null, check);
};
DefaultFormatter.prototype.remapExportAssignment = function (node) {
@@ -100,9 +104,9 @@ DefaultFormatter.prototype.remapAssignments = function () {
};
traverse(this.file.ast, {
enter: function (node, parent, scope) {
enter: function (node, parent, scope, context, isLocalReference) {
if (t.isUpdateExpression(node) && isLocalReference(node.argument, scope)) {
this.skip();
context.skip();
// expand to long file assignment expression
var assign = t.assignmentExpression(node.operator[0] + "=", node.argument, t.literal(1));
@@ -130,11 +134,11 @@ DefaultFormatter.prototype.remapAssignments = function () {
}
if (t.isAssignmentExpression(node) && isLocalReference(node.left, scope)) {
this.skip();
context.skip();
return self.remapExportAssignment(node);
}
}
});
}, null, isLocalReference);
};
DefaultFormatter.prototype.getModuleName = function () {
@@ -244,13 +248,13 @@ DefaultFormatter.prototype.exportDeclaration = function (node, nodes) {
var assign;
if (t.isVariableDeclaration(declar)) {
for (var i in declar.declarations) {
for (var i = 0; i < declar.declarations.length; i++) {
var decl = declar.declarations[i];
decl.init = this._exportsAssign(decl.id, decl.init, node).expression;
var newDeclar = t.variableDeclaration(declar.kind, [decl]);
if (i === "0") t.inherits(newDeclar, declar);
if (i === 0) t.inherits(newDeclar, declar);
nodes.push(newDeclar);
}
} else {

View File

@@ -1,3 +1,5 @@
"use strict";
var util = require("../../util");
module.exports = function (Parent) {

View File

@@ -1 +1,3 @@
"use strict";
module.exports = require("./_strict")(require("./amd"));

View File

@@ -1,3 +1,5 @@
"use strict";
module.exports = AMDFormatter;
var DefaultFormatter = require("./_default");

View File

@@ -1 +1,3 @@
"use strict";
module.exports = require("./_strict")(require("./common"));

View File

@@ -1,3 +1,5 @@
"use strict";
module.exports = CommonJSFormatter;
var DefaultFormatter = require("./_default");

View File

@@ -1,3 +1,5 @@
"use strict";
module.exports = IgnoreFormatter;
var t = require("../../types");

View File

@@ -1,7 +1,9 @@
"use strict";
module.exports = SystemFormatter;
var AMDFormatter = require("./amd");
var useStrict = require("../transformers/use-strict");
var useStrict = require("../helpers/use-strict");
var traverse = require("../../traverse");
var util = require("../../util");
var t = require("../../types");
@@ -63,28 +65,31 @@ SystemFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
SystemFormatter.prototype.buildRunnerSetters = function (block, hoistDeclarators) {
return t.arrayExpression(_.map(this.ids, function (uid, source) {
var nodes = [];
var state = {
nodes: [],
hoistDeclarators: hoistDeclarators
};
traverse(block, {
enter: function (node) {
enter: function (node, parent, scope, context, state) {
if (node._importSource === source) {
if (t.isVariableDeclaration(node)) {
_.each(node.declarations, function (declar) {
hoistDeclarators.push(t.variableDeclarator(declar.id));
nodes.push(t.expressionStatement(
state.hoistDeclarators.push(t.variableDeclarator(declar.id));
state.nodes.push(t.expressionStatement(
t.assignmentExpression("=", declar.id, declar.init)
));
});
} else {
nodes.push(node);
state.nodes.push(node);
}
this.remove();
context.remove();
}
}
});
}, null, state);
return t.functionExpression(null, [uid], t.blockStatement(nodes));
return t.functionExpression(null, [uid], t.blockStatement(state.nodes));
}));
};
@@ -112,10 +117,10 @@ SystemFormatter.prototype.transform = function (ast) {
// hoist up all variable declarations
traverse(block, {
enter: function (node, parent, scope) {
enter: function (node, parent, scope, context, hoistDeclarators) {
if (t.isFunction(node)) {
// nothing inside is accessible
return this.skip();
return context.skip();
}
if (t.isVariableDeclaration(node)) {
@@ -150,7 +155,8 @@ SystemFormatter.prototype.transform = function (ast) {
return nodes;
}
}
});
}, null, hoistDeclarators);
if (hoistDeclarators.length) {
var hoistDeclar = t.variableDeclaration("var", hoistDeclarators);
hoistDeclar._blockHoist = true;
@@ -159,19 +165,19 @@ SystemFormatter.prototype.transform = function (ast) {
// hoist up function declarations for circular references
traverse(block, {
enter: function (node) {
if (t.isFunction(node)) this.skip();
enter: function (node, parent, scope, context, handlerBody) {
if (t.isFunction(node)) context.skip();
if (t.isFunctionDeclaration(node) || node._blockHoist) {
handlerBody.push(node);
this.remove();
context.remove();
}
}
});
}, null, handlerBody);
handlerBody.push(returnStatement);
if (useStrict._has(block)) {
if (useStrict.has(block)) {
handlerBody.unshift(block.body.shift());
}

View File

@@ -1 +1,3 @@
"use strict";
module.exports = require("./_strict")(require("./umd"));

View File

@@ -1,3 +1,5 @@
"use strict";
module.exports = UMDFormatter;
var AMDFormatter = require("./amd");

View File

@@ -1,5 +0,0 @@
(function () {
if (SUPER_NAME != null) {
SUPER_NAME.apply(this, arguments);
}
});

View File

@@ -0,0 +1,3 @@
if (SUPER_NAME != null) {
SUPER_NAME.apply(this, arguments);
}

View File

@@ -1,3 +1,3 @@
for (var KEY = START; KEY < ARGUMENTS.length; KEY++) {
for (var LEN = ARGUMENTS.length, ARRAY = Array(ARRAY_LEN), KEY = START; KEY < LEN; KEY++) {
ARRAY[ARRAY_KEY] = ARGUMENTS[KEY];
}

View File

@@ -0,0 +1,4 @@
(function (strings, raw) {
strings.raw = raw;
return strings;
});

View File

@@ -1,3 +1,5 @@
"use strict";
module.exports = transform;
var Transformer = require("./transformer");
@@ -20,7 +22,7 @@ transform.fromAst = function (ast, code, opts) {
};
transform._ensureTransformerNames = function (type, keys) {
for (var i in keys) {
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (!_.has(transform.transformers, key)) {
throw new ReferenceError("unknown transformer " + key + " specified in " + type);
@@ -44,8 +46,10 @@ transform.moduleFormatters = {
_.each({
specNoForInOfAssignment: require("./transformers/spec-no-for-in-of-assignment"),
specSetters: require("./transformers/spec-setters"),
specBlockScopedFunctions: require("./transformers/spec-block-scoped-functions"),
// playground
malletOperator: require("./transformers/playground-mallet-operator"),
methodBinding: require("./transformers/playground-method-binding"),
memoizationOperator: require("./transformers/playground-memoization-operator"),
objectGetterMemoization: require("./transformers/playground-object-getter-memoization"),
@@ -59,7 +63,6 @@ _.each({
arrayComprehension: require("./transformers/es7-array-comprehension"),
generatorComprehension: require("./transformers/es7-generator-comprehension"),
arrowFunctions: require("./transformers/es6-arrow-functions"),
classesFastSuper: require("./transformers/optional-classes-fast-super"),
classes: require("./transformers/es6-classes"),
objectSpread: require("./transformers/es7-object-spread"),
@@ -70,13 +73,13 @@ _.each({
computedPropertyNames: require("./transformers/es6-computed-property-names"),
destructuring: require("./transformers/es6-destructuring"),
defaultParameters: require("./transformers/es6-default-parameters"),
forOfFast: require("./transformers/optional-for-of-fast"),
forOf: require("./transformers/es6-for-of"),
unicodeRegex: require("./transformers/es6-unicode-regex"),
abstractReferences: require("./transformers/es7-abstract-references"),
constants: require("./transformers/es6-constants"),
letScoping: require("./transformers/es6-let-scoping"),
blockScopingTDZ: require("./transformers/optional-block-scoping-tdz"),
_blockHoist: require("./transformers/_block-hoist"),

View File

@@ -1,9 +1,30 @@
"use strict";
module.exports = Transformer;
var traverse = require("../traverse");
var t = require("../types");
var _ = require("lodash");
function noop() { }
function enter(node, parent, scope, context, args) {
var fns = args[1][node.type];
if (!fns) return;
return fns.enter(node, parent, scope, context, args[0]);
}
function exit(node, parent, scope, context, args) {
var fns = args[1][node.type];
if (!fns) return;
return fns.exit(node, parent, scope, context, args[0]);
}
var traverseOpts = {
enter: enter,
exit: exit
};
function Transformer(key, transformer, opts) {
this.manipulateOptions = transformer.manipulateOptions;
this.experimental = !!transformer.experimental;
@@ -32,6 +53,9 @@ Transformer.prototype.normalise = function (transformer) {
if (!_.isObject(fns)) return;
if (!fns.enter) fns.enter = noop;
if (!fns.exit) fns.exit = noop;
transformer[type] = fns;
var aliases = t.FLIPPED_ALIAS_KEYS[type];
@@ -54,28 +78,8 @@ Transformer.prototype.astRun = function (file, key) {
};
Transformer.prototype.transform = function (file) {
var transformer = this.transformer;
var build = function (exit) {
return function (node, parent, scope) {
var fns = transformer[node.type];
if (!fns) return;
var fn = fns.enter;
if (exit) fn = fns.exit;
if (!fn) return;
return fn(node, parent, file, scope);
};
};
this.astRun(file, "before");
traverse(file.ast, {
enter: build(),
exit: build(true)
});
traverse(file.ast, traverseOpts, null, [file, this.transformer]);
this.astRun(file, "after");
};

View File

@@ -1,57 +1,64 @@
"use strict";
var traverse = require("../../traverse");
var t = require("../../types");
var functionChildrenTraverser = {
enter: function (node, parent, scope, context, state) {
if (t.isFunction(node) && !node._aliasFunction) {
return context.skip();
}
if (node._ignoreAliasFunctions) return context.skip();
var getId;
if (t.isIdentifier(node) && node.name === "arguments") {
getId = state.getArgumentsId;
} else if (t.isThisExpression(node)) {
getId = state.getThisId;
} else {
return;
}
if (t.isReferenced(node, parent)) return getId();
}
};
var functionTraverser = {
enter: function (node, parent, scope, context, state) {
if (!node._aliasFunction) {
if (t.isFunction(node)) {
// stop traversal of this node as it'll be hit again by this transformer
return context.skip();
} else {
return;
}
}
// traverse all child nodes of this function and find `arguments` and `this`
traverse(node, functionChildrenTraverser, null, state);
return context.skip();
}
};
var go = function (getBody, node, file, scope) {
var argumentsId;
var thisId;
var getArgumentsId = function () {
return argumentsId = argumentsId || file.generateUidIdentifier("arguments", scope);
};
var getThisId = function () {
return thisId = thisId || file.generateUidIdentifier("this", scope);
var state = {
getArgumentsId: function () {
return argumentsId = argumentsId || file.generateUidIdentifier("arguments", scope);
},
getThisId: function () {
return thisId = thisId || file.generateUidIdentifier("this", scope);
}
};
// traverse the function and find all alias functions so we can alias
// `arguments` and `this` if necessary
traverse(node, {
enter: function (node) {
if (!node._aliasFunction) {
if (t.isFunction(node)) {
// stop traversal of this node as it'll be hit again by this transformer
return this.skip();
} else {
return;
}
}
// traverse all child nodes of this function and find `arguments` and `this`
traverse(node, {
enter: function (node, parent) {
if (t.isFunction(node) && !node._aliasFunction) {
return this.skip();
}
if (node._ignoreAliasFunctions) return this.skip();
var getId;
if (t.isIdentifier(node) && node.name === "arguments") {
getId = getArgumentsId;
} else if (t.isThisExpression(node)) {
getId = getThisId;
} else {
return;
}
if (t.isReferenced(node, parent)) return getId();
}
});
return this.skip();
}
});
traverse(node, functionTraverser, null, state);
var body;
@@ -71,14 +78,14 @@ var go = function (getBody, node, file, scope) {
}
};
exports.Program = function (node, parent, file, scope) {
exports.Program = function (node, parent, scope, context, file) {
go(function () {
return node.body;
}, node, file, scope);
};
exports.FunctionDeclaration =
exports.FunctionExpression = function (node, parent, file, scope) {
exports.FunctionExpression = function (node, parent, scope, context, file) {
go(function () {
t.ensureBlock(node);
return node.body.body;

View File

@@ -1,4 +1,6 @@
var useStrict = require("./use-strict");
"use strict";
var useStrict = require("../helpers/use-strict");
var _ = require("lodash");
// Priority:
@@ -12,13 +14,13 @@ exports.BlockStatement =
exports.Program = {
exit: function (node) {
var hasChange = false;
for (var i in node.body) {
for (var i = 0; i < node.body.length; i++) {
var bodyNode = node.body[i];
if (bodyNode && bodyNode._blockHoist != null) hasChange = true;
}
if (!hasChange) return;
useStrict._wrap(node, function () {
useStrict.wrap(node, function () {
var nodePriorities = _.groupBy(node.body, function (bodyNode) {
var priority = bodyNode._blockHoist;
if (priority == null) priority = 1;

View File

@@ -1,14 +1,18 @@
var useStrict = require("./use-strict");
"use strict";
var useStrict = require("../helpers/use-strict");
var t = require("../../types");
exports.secondPass = true;
exports.BlockStatement =
exports.Program = function (node) {
if (!node._declarations) return;
var kinds = {};
var kind;
useStrict._wrap(node, function () {
useStrict.wrap(node, function () {
for (var i in node._declarations) {
var declar = node._declarations[i];

View File

@@ -1,3 +1,5 @@
"use strict";
var transform = require("../transform");
exports.ast = {

View File

@@ -1,3 +1,5 @@
"use strict";
var t = require("../../types");
exports.ArrowFunctionExpression = function (node) {

View File

@@ -1,12 +1,27 @@
var traverse = require("../../traverse");
var util = require("../../util");
var t = require("../../types");
"use strict";
exports.ClassDeclaration = function (node, parent, file, scope) {
var ReplaceSupers = require("../helpers/replace-supers");
var nameMethod = require("../helpers/name-method");
var util = require("../../util");
var t = require("../../types");
exports.ClassDeclaration = function (node, parent, scope, context, file) {
return new Class(node, file, scope, true).run();
};
exports.ClassExpression = function (node, parent, file, scope) {
exports.ClassExpression = function (node, parent, scope, context, file) {
if (!node.id) {
if (t.isProperty(parent) && parent.value === node && !parent.computed && t.isIdentifier(parent.key)) {
// var o = { foo: class {} };
node.id = parent.key;
}
if (t.isVariableDeclarator(parent) && t.isIdentifier(parent.id)) {
// var foo = class {};
node.id = parent.id;
}
}
return new Class(node, file, scope, false).run();
};
@@ -33,6 +48,7 @@ function Class(node, file, scope, isStatement) {
this.hasConstructor = false;
this.className = node.id || file.generateUidIdentifier("class", scope);
this.superName = node.superClass;
this.isLoose = file.isLoose("classes");
}
/**
@@ -117,27 +133,32 @@ Class.prototype.buildBody = function () {
var superName = this.superName;
var classBody = this.node.body.body;
var body = this.body;
var self = this;
for (var i in classBody) {
for (var i = 0; i < classBody.length; i++) {
var node = classBody[i];
if (t.isMethodDefinition(node)) {
self.replaceInstanceSuperReferences(node);
var replaceSupers = new ReplaceSupers(node, this.className, this.superName, this.isLoose, this.file);
replaceSupers.replace();
if (node.key.name === "constructor") {
self.pushConstructor(node);
this.pushConstructor(node);
} else {
self.pushMethod(node);
this.pushMethod(node);
}
} else if (t.isPrivateDeclaration(node)) {
self.closure = true;
this.closure = true;
body.unshift(node);
}
}
// we have no constructor, we have a super, and the super doesn't appear to be falsy
if (!this.hasConstructor && superName && !t.isFalsyExpression(superName)) {
constructor.body.body.push(util.template("class-super-constructor-call", {
CLASS_NAME: className
var defaultConstructorTemplate = "class-super-constructor-call";
if (this.isLoose) defaultConstructorTemplate += "-loose";
constructor.body.body.push(util.template(defaultConstructorTemplate, {
CLASS_NAME: className,
SUPER_NAME: this.superName
}, true));
}
@@ -174,6 +195,26 @@ Class.prototype.pushMethod = function (node) {
var methodName = node.key;
var kind = node.kind;
if (kind === "") {
nameMethod.property(node, this.file, this.scope);
if (this.isLoose) {
// use assignments instead of define properties for loose classes
var className = this.className;
if (!node.static) className = t.memberExpression(className, t.identifier("prototype"));
methodName = t.memberExpression(className, methodName, node.computed);
var expr = t.expressionStatement(t.assignmentExpression("=", methodName, node.value));
t.inheritsComments(expr, node);
this.body.push(expr);
return;
}
kind = "value";
}
var mutatorMap = this.instanceMutatorMap;
if (node.static) {
this.hasStaticMutators = true;
@@ -182,134 +223,9 @@ Class.prototype.pushMethod = function (node) {
this.hasInstanceMutators = true;
}
if (kind === "") {
kind = "value";
}
util.pushMutatorMap(mutatorMap, methodName, kind, node.computed, node);
};
/**
* Gets a node representing the super class value of the named property.
*
* @example
*
* _get(Object.getPrototypeOf(CLASS.prototype), "METHOD", this)
*
* @param {Node} property
* @param {boolean} isStatic
* @param {boolean} isComputed
*
* @returns {Node}
*/
Class.prototype.superProperty = function (property, isStatic, isComputed, thisExpression) {
return t.callExpression(
this.file.addHelper("get"),
[
t.callExpression(
t.memberExpression(t.identifier("Object"), t.identifier("getPrototypeOf")),
[
isStatic ? this.className : t.memberExpression(this.className, t.identifier("prototype"))
]
),
isComputed ? property : t.literal(property.name),
thisExpression
]
);
};
/**
* Replace all `super` references with a reference to our `superClass`.
*
* @param {Node} methodNode MethodDefinition
*/
Class.prototype.replaceInstanceSuperReferences = function (methodNode) {
var method = methodNode.value;
var self = this;
var topLevelThisReference;
traverse2(method, true);
if (topLevelThisReference) {
method.body.body.unshift(t.variableDeclaration("var", [
t.variableDeclarator(topLevelThisReference, t.thisExpression())
]));
}
function traverse2(node, topLevel) {
traverse(node, {
enter: function (node, parent) {
if (t.isFunction(node) && !t.isArrowFunctionExpression(node)) {
traverse2(node, false);
return this.skip();
}
var property;
var computed;
var args;
if (t.isIdentifier(node, { name: "super" })) {
if (!(t.isMemberExpression(parent) && !parent.computed && parent.property === node)) {
throw self.file.errorWithNode(node, "illegal use of bare super");
}
} else if (t.isCallExpression(node)) {
var callee = node.callee;
if (t.isIdentifier(callee, { name: "super" })) {
// super(); -> _get(Object.getPrototypeOf(ClassName), "MethodName", this).call(this);
property = methodNode.key;
computed = methodNode.computed;
args = node.arguments;
} else {
if (!t.isMemberExpression(callee)) return;
if (callee.object.name !== "super") return;
// super.test(); -> _get(Object.getPrototypeOf(ClassName.prototype), "test", this).call(this);
property = callee.property;
computed = callee.computed;
args = node.arguments;
}
} else if (t.isMemberExpression(node)) {
if (!t.isIdentifier(node.object, { name: "super" })) return;
// super.name; -> _get(Object.getPrototypeOf(ClassName.prototype), "name", this);
property = node.property;
computed = node.computed;
}
if (property) {
var thisReference;
if (topLevel) {
thisReference = t.thisExpression();
} else {
topLevelThisReference = thisReference = topLevelThisReference || self.file.generateUidIdentifier("this");
}
var superProperty = self.superProperty(property, methodNode.static, computed, thisReference);
if (args) {
if (args.length === 1 && t.isSpreadElement(args[0])) {
// super(...arguments);
return t.callExpression(
t.memberExpression(superProperty, t.identifier("apply"), false),
[thisReference, args[0].argument]
);
} else {
return t.callExpression(
t.memberExpression(superProperty, t.identifier("call"), false),
[thisReference].concat(args)
);
}
} else {
return superProperty;
}
}
}
});
}
};
/**
* Replace the constructor body of our class.
*

View File

@@ -1,29 +1,66 @@
"use strict";
var t = require("../../types");
exports.ObjectExpression = function (node, parent, file, scope) {
exports.ObjectExpression = function (node, parent, scope, context, file) {
var hasComputed = false;
var prop;
var key;
var i;
for (i in node.properties) {
for (var i = 0; i < node.properties.length; i++) {
hasComputed = t.isProperty(node.properties[i], { computed: true, kind: "init" });
if (hasComputed) break;
}
if (!hasComputed) return;
var initProps = [];
var objId = scope.generateUidBasedOnNode(parent, file);
//
var body = [];
var container = t.functionExpression(null, [], t.blockStatement(body));
container._aliasFunction = true;
//
var callback = spec;
if (file.isLoose("computedPropertyNames")) callback = loose;
var result = callback(node, body, objId, initProps, file);
if (result) return result;
//
body.unshift(t.variableDeclaration("var", [
t.variableDeclarator(objId, t.objectExpression(initProps))
]));
body.push(t.returnStatement(objId));
return t.callExpression(container, []);
};
var loose = function (node, body, objId) {
for (var i = 0; i < node.properties.length; i++) {
var prop = node.properties[i];
body.push(t.expressionStatement(
t.assignmentExpression(
"=",
t.memberExpression(objId, prop.key, prop.computed),
prop.value
)
));
}
};
var spec = function (node, body, objId, initProps, file) {
var props = node.properties;
var prop, key;
// normalise key
for (i in props) {
for (var i = 0; i < props.length; i++) {
prop = props[i];
if (prop.kind !== "init") continue;
@@ -36,10 +73,9 @@ exports.ObjectExpression = function (node, parent, file, scope) {
// add all non-computed properties and `__proto__` properties to the initializer
var initProps = [];
var broken = false;
for (i in props) {
for (i = 0; i < props.length; i++) {
prop = props[i];
if (prop.computed) {
@@ -55,7 +91,7 @@ exports.ObjectExpression = function (node, parent, file, scope) {
// add a simple assignment for all Symbol member expressions due to symbol polyfill limitations
// otherwise use Object.defineProperty
for (i in props) {
for (i = 0; i < props.length; i++) {
prop = props[i];
if (!prop) continue;
@@ -86,14 +122,4 @@ exports.ObjectExpression = function (node, parent, file, scope) {
return first;
}
}
//
body.unshift(t.variableDeclaration("var", [
t.variableDeclarator(objId, t.objectExpression(initProps))
]));
body.push(t.returnStatement(objId));
return t.callExpression(container, []);
};

View File

@@ -1,3 +1,5 @@
"use strict";
var traverse = require("../../traverse");
var t = require("../../types");
var _ = require("lodash");
@@ -6,7 +8,7 @@ exports.Program =
exports.BlockStatement =
exports.ForInStatement =
exports.ForOfStatement =
exports.ForStatement = function (node, parent, file) {
exports.ForStatement = function (node, parent, scope, context, file) {
var hasConstants = false;
var constants = {};
@@ -44,7 +46,7 @@ exports.ForStatement = function (node, parent, file) {
}
if (t.isVariableDeclaration(child, { kind: "const" })) {
for (var i in child.declarations) {
for (var i = 0; i < child.declarations.length; i++) {
var declar = child.declarations[i];
var ids = getIds(declar);
@@ -69,14 +71,19 @@ exports.ForStatement = function (node, parent, file) {
if (!hasConstants) return;
var state = {
check: check,
getIds: getIds
};
traverse(node, {
enter: function (child, parent, scope) {
enter: function (child, parent, scope, context, state) {
if (child._ignoreConstant) return;
if (t.isVariableDeclaration(child)) return;
if (t.isVariableDeclarator(child) || t.isDeclaration(child) || t.isAssignmentExpression(child)) {
check(parent, getIds(child), scope);
state.check(parent, state.getIds(child), scope);
}
}
});
}, null, state);
};

View File

@@ -1,9 +1,10 @@
"use strict";
var traverse = require("../../traverse");
var util = require("../../util");
var t = require("../../types");
var _ = require("lodash");
exports.Function = function (node, parent, file, scope) {
exports.Function = function (node, parent, scope, context, file) {
if (!node.defaults || !node.defaults.length) return;
t.ensureBlock(node);
@@ -12,10 +13,26 @@ exports.Function = function (node, parent, file, scope) {
});
var iife = false;
var i;
var def;
for (i in node.defaults) {
var checkTDZ = function (ids) {
var check = function (node, parent) {
if (!t.isIdentifier(node) || !t.isReferenced(node, parent)) return;
if (ids.indexOf(node.name) >= 0) {
throw file.errorWithNode(node, "Temporal dead zone - accessing a variable before it's initialized");
}
if (scope.has(node.name, true)) {
iife = true;
}
};
check(def, node);
traverse(def, { enter: check });
};
for (var i = 0; i < node.defaults.length; i++) {
def = node.defaults[i];
if (!def) continue;
@@ -23,25 +40,13 @@ exports.Function = function (node, parent, file, scope) {
// temporal dead zone check - here we prevent accessing of params that
// are to the right - ie. uninitialized parameters
_.each(ids.slice(i), function (ids) {
var check = function (node, parent) {
if (!t.isIdentifier(node) || !t.isReferenced(node, parent)) return;
if (ids.indexOf(node.name) >= 0) {
throw file.errorWithNode(node, "Temporal dead zone - accessing a variable before it's initialized");
}
if (scope.has(node.name, true)) {
iife = true;
}
};
check(def, node);
traverse(def, { enter: check });
});
var rightIds = ids.slice(i);
for (var i2 = 0; i2 < rightIds.length; i2++) {
checkTDZ(rightIds[i2]);
}
// we're accessing a variable that's already defined within this function
var has = scope.get(param.name);
var has = scope.get(param.name, true);
if (has && node.params.indexOf(has) < 0) {
iife = true;
}
@@ -54,7 +59,7 @@ exports.Function = function (node, parent, file, scope) {
var lastNonDefaultParam = 0;
for (i in node.defaults) {
for (i = 0; i < node.defaults.length; i++) {
def = node.defaults[i];
if (!def) {
lastNonDefaultParam = +i + 1;

View File

@@ -1,3 +1,5 @@
"use strict";
// TODO: Clean up
var t = require("../../types");
@@ -7,7 +9,7 @@ var buildVariableAssign = function (opts, id, init) {
if (t.isMemberExpression(id)) op = "=";
if (op) {
return t.expressionStatement(t.assignmentExpression("=", id, init));
return t.expressionStatement(t.assignmentExpression(op, id, init));
} else {
return t.variableDeclaration(opts.kind, [
t.variableDeclarator(id, init)
@@ -46,12 +48,12 @@ var pushAssignmentPattern = function (opts, nodes, pattern, parentId) {
};
var pushObjectPattern = function (opts, nodes, pattern, parentId) {
for (var i in pattern.properties) {
for (var i = 0; i < pattern.properties.length; i++) {
var prop = pattern.properties[i];
if (t.isSpreadProperty(prop)) {
// get all the keys that appear in this object before the current spread
var keys = [];
for (var i2 in pattern.properties) {
for (var i2 = 0; i2 < pattern.properties.length; i2++) {
var prop2 = pattern.properties[i2];
if (i2 >= i) break;
@@ -88,7 +90,7 @@ var pushArrayPattern = function (opts, nodes, pattern, parentId) {
var i;
var hasSpreadElement = false;
for (i in pattern.elements) {
for (i = 0; i < pattern.elements.length; i++) {
if (t.isSpreadElement(pattern.elements[i])) {
hasSpreadElement = true;
break;
@@ -103,7 +105,7 @@ var pushArrayPattern = function (opts, nodes, pattern, parentId) {
]));
parentId = _parentId;
for (i in pattern.elements) {
for (i = 0; i < pattern.elements.length; i++) {
var elem = pattern.elements[i];
if (!elem) continue;
@@ -148,7 +150,7 @@ var pushPattern = function (opts) {
};
exports.ForInStatement =
exports.ForOfStatement = function (node, parent, file, scope) {
exports.ForOfStatement = function (node, parent, scope, context, file) {
var declar = node.left;
if (!t.isVariableDeclaration(declar)) return;
@@ -174,7 +176,7 @@ exports.ForOfStatement = function (node, parent, file, scope) {
block.body = nodes.concat(block.body);
};
exports.Function = function (node, parent, file, scope) {
exports.Function = function (node, parent, scope, context, file) {
var nodes = [];
var hasDestructuring = false;
@@ -205,7 +207,7 @@ exports.Function = function (node, parent, file, scope) {
block.body = nodes.concat(block.body);
};
exports.CatchClause = function (node, parent, file, scope) {
exports.CatchClause = function (node, parent, scope, context, file) {
var pattern = node.param;
if (!t.isPattern(pattern)) return;
@@ -223,7 +225,7 @@ exports.CatchClause = function (node, parent, file, scope) {
node.body.body = nodes.concat(node.body.body);
};
exports.ExpressionStatement = function (node, parent, file, scope) {
exports.ExpressionStatement = function (node, parent, scope, context, file) {
var expr = node.expression;
if (expr.type !== "AssignmentExpression") return;
@@ -245,14 +247,13 @@ exports.ExpressionStatement = function (node, parent, file, scope) {
return nodes;
};
exports.AssignmentExpression = function (node, parent, file, scope) {
exports.AssignmentExpression = function (node, parent, scope, context, file) {
if (parent.type === "ExpressionStatement") return;
if (!t.isPattern(node.left)) return;
var tempName = file.generateUid("temp", scope);
var ref = t.identifier(tempName);
var ref = file.generateUidIdentifier("temp", scope);
scope.push({
key: tempName,
key: ref.name,
id: ref
});
@@ -270,7 +271,7 @@ exports.AssignmentExpression = function (node, parent, file, scope) {
return t.toSequenceExpression(nodes, scope);
};
exports.VariableDeclaration = function (node, parent, file, scope) {
exports.VariableDeclaration = function (node, parent, scope, context, file) {
if (t.isForInStatement(parent) || t.isForOfStatement(parent)) return;
var nodes = [];
@@ -278,7 +279,7 @@ exports.VariableDeclaration = function (node, parent, file, scope) {
var declar;
var hasPattern = false;
for (i in node.declarations) {
for (i = 0; i < node.declarations.length; i++) {
declar = node.declarations[i];
if (t.isPattern(declar.id)) {
hasPattern = true;
@@ -287,7 +288,7 @@ exports.VariableDeclaration = function (node, parent, file, scope) {
}
if (!hasPattern) return;
for (i in node.declarations) {
for (i = 0; i < node.declarations.length; i++) {
declar = node.declarations[i];
var patternId = declar.init;
@@ -317,7 +318,7 @@ exports.VariableDeclaration = function (node, parent, file, scope) {
if (!t.isProgram(parent) && !t.isBlockStatement(parent)) {
declar = null;
for (i in nodes) {
for (i = 0; i < nodes.length; i++) {
node = nodes[i];
declar = declar || t.variableDeclaration(node.kind, []);

View File

@@ -1,7 +1,71 @@
"use strict";
var util = require("../../util");
var t = require("../../types");
exports.ForOfStatement = function (node, parent, file, scope) {
exports.ForOfStatement = function (node, parent, scope, context, file) {
var callback = spec;
if (file.isLoose("forOf")) callback = loose;
var build = callback(node, parent, scope, context, file);
var declar = build.declar;
var loop = build.loop;
var block = loop.body;
// inherit comments from the original loop
t.inheritsComments(loop, node);
// ensure that it's a block so we can take all it's statemetns
t.ensureBlock(node);
// add the value declaration to the new loop body
if (declar){
if (build.shouldUnshift) {
block.body.unshift(declar);
} else {
block.body.push(declar);
}
}
// push the rest of the original loop body onto our new body
block.body = block.body.concat(node.body.body);
return loop;
};
var loose = function (node, parent, scope, context, file) {
var left = node.left;
var declar, id;
if (t.isIdentifier(left) || t.isPattern(left)) {
// for (i of test), for ({ i } of test)
id = left;
} else if (t.isVariableDeclaration(left)) {
// for (var i of test)
id = left.declarations[0].id;
declar = t.variableDeclaration(left.kind, [
t.variableDeclarator(id)
]);
} else {
throw file.errorWithNode(left, "Unknown node type " + left.type + " in ForOfStatement");
}
var loop = util.template("for-of-loose", {
LOOP_OBJECT: file.generateUidIdentifier("iterator", scope),
IS_ARRAY: file.generateUidIdentifier("isArray", scope),
OBJECT: node.right,
INDEX: file.generateUidIdentifier("i", scope),
ID: id
});
return {
shouldUnshift: true,
declar: declar,
loop: loop
};
};
var spec = function (node, parent, scope, context, file) {
var left = node.left;
var declar;
@@ -9,8 +73,10 @@ exports.ForOfStatement = function (node, parent, file, scope) {
var stepValue = t.memberExpression(stepKey, t.identifier("value"));
if (t.isIdentifier(left) || t.isPattern(left)) {
// for (i of test), for ({ i } of test)
declar = t.expressionStatement(t.assignmentExpression("=", left, stepValue));
} else if (t.isVariableDeclaration(left)) {
// for (var i of test)
declar = t.variableDeclaration(left.kind, [
t.variableDeclarator(left.declarations[0].id, stepValue)
]);
@@ -18,18 +84,14 @@ exports.ForOfStatement = function (node, parent, file, scope) {
throw file.errorWithNode(left, "Unknown node type " + left.type + " in ForOfStatement");
}
var node2 = util.template("for-of", {
var loop = util.template("for-of", {
ITERATOR_KEY: file.generateUidIdentifier("iterator", scope),
STEP_KEY: stepKey,
OBJECT: node.right
});
t.inheritsComments(node2, node);
t.ensureBlock(node);
var block = node2.body;
block.body.push(declar);
block.body = block.body.concat(node.body.body);
return node2;
return {
declar: declar,
loop: loop
};
};

View File

@@ -1,3 +1,5 @@
"use strict";
var regenerator = require("regenerator");
exports.ast = {

View File

@@ -1,3 +1,5 @@
"use strict";
var traverse = require("../../traverse");
var util = require("../../util");
var t = require("../../types");
@@ -25,7 +27,7 @@ var isVar = function (node, parent) {
};
var standardiseLets = function (declars) {
for (var i in declars) {
for (var i = 0; i < declars.length; i++) {
delete declars[i]._let;
}
};
@@ -34,7 +36,7 @@ exports.VariableDeclaration = function (node, parent) {
isLet(node, parent);
};
exports.Loop = function (node, parent, file, scope) {
exports.Loop = function (node, parent, scope, context, file) {
var init = node.left || node.init;
if (isLet(init, node)) {
t.ensureBlock(node);
@@ -46,7 +48,7 @@ exports.Loop = function (node, parent, file, scope) {
node.label = parent.label;
}
var letScoping = new LetScoping(node, node.body, parent, file, scope);
var letScoping = new LetScoping(node, node.body, parent, scope, file);
letScoping.run();
if (node.label && !t.isLabeledStatement(parent)) {
@@ -55,9 +57,10 @@ exports.Loop = function (node, parent, file, scope) {
}
};
exports.BlockStatement = function (block, parent, file, scope) {
exports.Program =
exports.BlockStatement = function (block, parent, scope, context, file) {
if (!t.isLoop(parent)) {
var letScoping = new LetScoping(false, block, parent, file, scope);
var letScoping = new LetScoping(false, block, parent, scope, file);
letScoping.run();
}
};
@@ -68,19 +71,21 @@ exports.BlockStatement = function (block, parent, file, scope) {
* @param {Boolean|Node} loopParent
* @param {Node} block
* @param {Node} parent
* @param {File} file
* @param {Scope} scope
* @param {File} file
*/
function LetScoping(loopParent, block, parent, file, scope) {
function LetScoping(loopParent, block, parent, scope, file) {
this.loopParent = loopParent;
this.parent = parent;
this.scope = scope;
this.block = block;
this.file = file;
this.letReferences = {};
this.body = [];
this.outsideLetReferences = {};
this.hasLetReferences = false;
this.letReferences = block._letReferences = {};
this.body = [];
}
/**
@@ -89,27 +94,95 @@ function LetScoping(loopParent, block, parent, file, scope) {
LetScoping.prototype.run = function () {
var block = this.block;
if (block._letDone) return;
block._letDone = true;
this.info = this.getInfo();
var needsClosure = this.getLetReferences();
// remap all let references that exist in upper scopes to their uid
this.remap();
// this is a block within a `Function/Program` so we can safely leave it be
if (t.isFunction(this.parent) || t.isProgram(this.block)) return;
// this is a block within a `Function` so we can safely leave it be
if (t.isFunction(this.parent)) return this.noClosure();
// we can skip everything
if (!this.hasLetReferences) return;
// this block has no let references so let's clean up
if (!this.info.keys.length) return this.noClosure();
if (needsClosure) {
this.needsClosure();
} else {
this.remap();
}
};
// returns whether or not there are any outside let references within any
// functions
var referencesInClosure = this.getLetReferences();
/**
* Description
*/
// no need for a closure so let's clean up
if (!referencesInClosure) return this.noClosure();
LetScoping.prototype.remap = function () {
var letRefs = this.letReferences;
var scope = this.scope;
var file = this.file;
// alright, so since we aren't wrapping this block in a closure
// we have to check if any of our let variables collide with
// those in upper scopes and then if they do, generate a uid
// for them and replace all references with it
var remaps = {};
for (var key in letRefs) {
// just an Identifier node we collected in `getLetReferences`
// this is the defining identifier of a declaration
var ref = letRefs[key];
if (scope.parentHas(key)) {
var uid = file.generateUidIdentifier(ref.name, scope).name;
ref.name = uid;
remaps[key] = remaps[uid] = {
node: ref,
uid: uid
};
}
}
//
var replace = function (node, parent, scope, context, remaps) {
if (!t.isIdentifier(node)) return;
if (!t.isReferenced(node, parent)) return;
var remap = remaps[node.name];
if (!remap) return;
var own = scope.get(node.name, true);
if (own === remap.node) {
node.name = remap.uid;
} else {
// scope already has it's own declaration that doesn't
// match the one we have a stored replacement for
if (context) context.skip();
}
};
var traverseReplace = function (node, parent) {
replace(node, parent, scope, null, remaps);
traverse(node, { enter: replace }, scope, remaps);
};
var loopParent = this.loopParent;
if (loopParent) {
traverseReplace(loopParent.right, loopParent);
traverseReplace(loopParent.test, loopParent);
traverseReplace(loopParent.update, loopParent);
}
traverse(this.block, { enter: replace }, scope, remaps);
};
/**
* Description
*/
LetScoping.prototype.needsClosure = function () {
var block = this.block;
// if we're inside of a for loop then we search to see if there are any
// `break`s, `continue`s, `return`s etc
@@ -118,22 +191,16 @@ LetScoping.prototype.run = function () {
// hoist var references to retain scope
this.hoistVarDeclarations();
// set let references to plain var references
standardiseLets(this.info.declarators);
// turn letReferences into an array
var letReferences = _.values(this.letReferences);
// turn outsideLetReferences into an array
var params = _.values(this.outsideLetReferences);
// build the closure that we're going to wrap the block with
var fn = t.functionExpression(null, letReferences, t.blockStatement(block.body));
var fn = t.functionExpression(null, params, t.blockStatement(block.body));
fn._aliasFunction = true;
// replace the current block body with the one we're going to build
block.body = this.body;
// change upper scope references with their uid if they have one
var params = this.getParams(letReferences);
// build a call and a unique id that we can assign the return value to
var call = t.callExpression(fn, params);
var ret = this.file.generateUidIdentifier("ret", this.scope);
@@ -147,116 +214,81 @@ LetScoping.prototype.run = function () {
this.build(ret, call);
};
/**
* There are no let references accessed within a closure so we can just turn the
* lets into vars.
*/
LetScoping.prototype.noClosure = function () {
standardiseLets(this.info.declarators);
};
/**
* Traverse through block and replace all references that exist in a higher
* scope to their uids.
*/
LetScoping.prototype.remap = function () {
var replacements = this.info.duplicates;
var block = this.block;
if (!this.info.hasDuplicates) return;
var replace = function (node, parent, scope) {
if (!t.isIdentifier(node)) return;
if (!t.isReferenced(node, parent)) return;
if (scope && scope.hasOwn(node.name)) return;
node.name = replacements[node.name] || node.name;
};
var traverseReplace = function (node, parent) {
replace(node, parent);
traverse(node, { enter: replace });
};
var loopParent = this.loopParent;
if (loopParent) {
traverseReplace(loopParent.right, loopParent);
traverseReplace(loopParent.test, loopParent);
traverseReplace(loopParent.update, loopParent);
}
traverse(block, { enter: replace });
};
/**
* Description
*
* @returns {Object}
*/
LetScoping.prototype.getInfo = function () {
LetScoping.prototype.getLetReferences = function () {
var block = this.block;
var scope = this.scope;
var file = this.file;
var opts = {
// array of `Identifier` names of let variables that appear lexically out of
// this scope but should be accessible - eg. `ForOfStatement`.left
outsideKeys: [],
// array of let `VariableDeclarator`s that are a part of this block
declarators: block._letDeclars || [],
// object of duplicate ids and their aliases - if there's an `Identifier`
// name that's used in an upper scope we generate a unique id and replace
// all references with it
duplicates: {},
hasDuplicates: false,
// array of `Identifier` names of let variables that are accessible within
// the current block
keys: []
};
var duplicates = function (id, key) {
var has = scope.parentGet(key);
if (has && has !== id) {
// there's a variable with this exact name in an upper scope so we need
// to generate a new name
opts.duplicates[key] = id.name = file.generateUid(key, scope);
opts.hasDuplicates = true;
}
};
var i;
var declarators = block._letDeclars || [];
var declar;
for (i in opts.declarators) {
declar = opts.declarators[i];
opts.declarators.push(declar);
//
for (var i = 0; i < declarators.length; i++) {
declar = declarators[i];
_.extend(this.outsideLetReferences, t.getIds(declar, true));
}
//
if (block.body) {
for (i = 0; i < block.body.length; i++) {
declar = block.body[i];
if (isLet(declar, block)) {
declarators = declarators.concat(declar.declarations);
}
}
}
//
for (i = 0; i < declarators.length; i++) {
declar = declarators[i];
var keys = t.getIds(declar, true);
_.each(keys, duplicates);
keys = Object.keys(keys);
opts.outsideKeys = opts.outsideKeys.concat(keys);
opts.keys = opts.keys.concat(keys);
_.extend(this.letReferences, keys);
this.hasLetReferences = true;
}
for (i in block.body) {
declar = block.body[i];
if (!isLet(declar, block)) continue;
// no let references so we can just quit
if (!this.hasLetReferences) return;
_.each(t.getIds(declar, true), function (id, key) {
duplicates(id, key);
opts.keys.push(key);
});
}
// set let references to plain var references
standardiseLets(declarators);
return opts;
var state = {
letReferences: this.letReferences,
closurify: false
};
// traverse through this block, stopping on functions and checking if they
// contain any local let references
traverse(this.block, {
enter: function (node, parent, scope, context, state) {
if (t.isFunction(node)) {
traverse(node, {
enter: function (node, parent) {
// not an identifier so we have no use
if (!t.isIdentifier(node)) return;
// not a direct reference
if (!t.isReferenced(node, parent)) return;
// this scope has a variable with the same name so it couldn't belong
// to our let scope
if (scope.hasOwn(node.name, true)) return;
// not a part of our scope
if (!state.letReferences[node.name]) return;
state.closurify = true;
}
}, scope, state);
return context.skip();
}
}
}, this.scope, state);
return state.closurify;
};
/**
@@ -276,11 +308,11 @@ LetScoping.prototype.checkLoop = function () {
};
traverse(this.block, {
enter: function (node, parent) {
enter: function (node, parent, scope, context) {
var replace;
if (t.isFunction(node) || t.isLoop(node)) {
return this.skip();
return context.skip();
}
if (node && !node.label) {
@@ -304,7 +336,7 @@ LetScoping.prototype.checkLoop = function () {
if (replace) return t.inherits(replace, node);
}
});
}, this.scope, has);
return has;
};
@@ -315,9 +347,8 @@ LetScoping.prototype.checkLoop = function () {
*/
LetScoping.prototype.hoistVarDeclarations = function () {
var self = this;
traverse(this.block, {
enter: function (node, parent) {
enter: function (node, parent, scope, context, self) {
if (t.isForStatement(node)) {
if (isVar(node.init, node)) {
node.init = t.sequenceExpression(self.pushDeclar(node.init));
@@ -329,71 +360,10 @@ LetScoping.prototype.hoistVarDeclarations = function () {
} else if (isVar(node, parent)) {
return self.pushDeclar(node).map(t.expressionStatement);
} else if (t.isFunction(node)) {
return this.skip();
return context.skip();
}
}
});
};
/**
* Build up a parameter list that we'll call our closure wrapper with, replacing
* all duplicate ids with their uid.
*
* @param {Array} params
* @returns {Array}
*/
LetScoping.prototype.getParams = function (params) {
var info = this.info;
params = _.cloneDeep(params);
_.each(params, function (param) {
param.name = info.duplicates[param.name] || param.name;
});
return params;
};
/**
* Get all let references within this block. Stopping whenever we reach another
* block.
*/
LetScoping.prototype.getLetReferences = function () {
var closurify = false;
var self = this;
// traverse through this block, stopping on functions and checking if they
// contain any outside let references
traverse(this.block, {
enter: function (node, parent, scope) {
if (t.isFunction(node)) {
traverse(node, {
enter: function (node, parent) {
// not an identifier so we have no use
if (!t.isIdentifier(node)) return;
// not a direct reference
if (!t.isReferenced(node, parent)) return;
// this scope has a variable with the same name so it couldn't belong
// to our let scope
if (scope.hasOwn(node.name, true)) return;
closurify = true;
// this key doesn't appear just outside our scope
if (!_.contains(self.info.outsideKeys, node.name)) return;
// push this badboy
self.letReferences[node.name] = node;
}
});
return this.skip();
}
}
});
return closurify;
}, this.scope, this);
};
/**
@@ -411,7 +381,7 @@ LetScoping.prototype.pushDeclar = function (node) {
var replace = [];
for (var i in node.declarations) {
for (var i = 0; i < node.declarations.length; i++) {
var declar = node.declarations[i];
if (!declar.init) continue;

View File

@@ -1,3 +1,5 @@
"use strict";
var t = require("../../types");
exports.ast = {
@@ -6,11 +8,11 @@ exports.ast = {
}
};
exports.ImportDeclaration = function (node, parent, file) {
exports.ImportDeclaration = function (node, parent, scope, context, file) {
var nodes = [];
if (node.specifiers.length) {
for (var i in node.specifiers) {
for (var i = 0; i < node.specifiers.length; i++) {
file.moduleFormatter.importSpecifier(node.specifiers[i], node, nodes, parent);
}
} else {
@@ -26,7 +28,7 @@ exports.ImportDeclaration = function (node, parent, file) {
return nodes;
};
exports.ExportDeclaration = function (node, parent, file) {
exports.ExportDeclaration = function (node, parent, scope, context, file) {
var nodes = [];
if (node.declaration) {
@@ -39,7 +41,7 @@ exports.ExportDeclaration = function (node, parent, file) {
file.moduleFormatter.exportDeclaration(node, nodes, parent);
} else {
for (var i in node.specifiers) {
for (var i = 0; i < node.specifiers.length; i++) {
file.moduleFormatter.exportSpecifier(node.specifiers[i], node, nodes, parent);
}
}

View File

@@ -1,49 +1,15 @@
var traverse = require("../../traverse");
var util = require("../../util");
var t = require("../../types");
"use strict";
exports.Property = function (node, parent, file, scope) {
var nameMethod = require("../helpers/name-method");
var util = require("../../util");
var t = require("../../types");
exports.Property = function (node, parent, scope, context, file) {
if (!node.method) return;
node.method = false;
var key = t.toComputedKey(node, node.key);
if (!t.isLiteral(key)) return; // we can't set a function id with this
var id = t.toIdentifier(key.value);
key = t.identifier(id);
var selfReference = false;
var outerDeclar = scope.get(id, true);
traverse(node, {
enter: function (node, parent, scope) {
// check if this node is an identifier that matches the same as our function id
if (!t.isIdentifier(node, { name: id })) return;
// check if this node is the one referenced
if (!t.isReferenced(node, parent)) return;
// check that we don't have a local variable declared as that removes the need
// for the wrapper
var localDeclar = scope.get(id, true);
if (localDeclar !== outerDeclar) return;
selfReference = true;
this.stop();
}
}, scope);
if (selfReference) {
node.value = util.template("property-method-assignment-wrapper", {
FUNCTION: node.value,
FUNCTION_ID: key,
FUNCTION_KEY: file.generateUidIdentifier(id, scope),
WRAPPER_KEY: file.generateUidIdentifier(id + "Wrapper", scope)
});
} else {
node.value.id = key;
}
nameMethod.property(node, file, scope);
};
exports.ObjectExpression = function (node) {

View File

@@ -1,3 +1,5 @@
"use strict";
var t = require("../../types");
var _ = require("lodash");

View File

@@ -1,36 +1,55 @@
"use strict";
var util = require("../../util");
var t = require("../../types");
exports.Function = function (node, parent, file) {
exports.Function = function (node, parent, scope, context, file) {
if (!node.rest) return;
var rest = node.rest;
delete node.rest;
node.rest = null;
t.ensureBlock(node);
var argsId = t.identifier("arguments");
// otherwise `arguments` will be remapped in arrow functions
argsId._ignoreAliasFunctions = true;
var start = t.literal(node.params.length);
var key = file.generateUidIdentifier("key");
var len = file.generateUidIdentifier("len");
var arrKey = key;
var arrLen = len;
if (node.params.length) {
arrKey = t.binaryExpression("-", arrKey, start);
// this method has additional params, so we need to subtract
// the index of the current argument position from the
// position in the array that we want to populate
arrKey = t.binaryExpression("-", key, start);
// we need to work out the size of the array that we're
// going to store all the rest parameters
//
// we need to add a check to avoid constructing the array
// with <0 if there are less arguments than params as it'll
// cause an error
arrLen = t.conditionalExpression(
t.binaryExpression(">", len, start),
t.binaryExpression("-", len, start),
t.literal(0)
);
}
node.body.body.unshift(
t.variableDeclaration("var", [
t.variableDeclarator(rest, t.arrayExpression([]))
]),
util.template("rest", {
ARGUMENTS: argsId,
ARRAY_KEY: arrKey,
ARRAY_LEN: arrLen,
START: start,
ARRAY: rest,
KEY: key
KEY: key,
LEN: len,
})
);
};

View File

@@ -1,3 +1,5 @@
"use strict";
var t = require("../../types");
var _ = require("lodash");
@@ -6,7 +8,7 @@ var getSpreadLiteral = function (spread, file) {
};
var hasSpread = function (nodes) {
for (var i in nodes) {
for (var i = 0; i < nodes.length; i++) {
if (t.isSpreadElement(nodes[i])) {
return true;
}
@@ -25,7 +27,7 @@ var build = function (props, file) {
_props = [];
};
for (var i in props) {
for (var i = 0; i < props.length; i++) {
var prop = props[i];
if (t.isSpreadElement(prop)) {
push();
@@ -40,7 +42,7 @@ var build = function (props, file) {
return nodes;
};
exports.ArrayExpression = function (node, parent, file) {
exports.ArrayExpression = function (node, parent, scope, context, file) {
var elements = node.elements;
if (!hasSpread(elements)) return;
@@ -55,7 +57,7 @@ exports.ArrayExpression = function (node, parent, file) {
return t.callExpression(t.memberExpression(first, t.identifier("concat")), nodes);
};
exports.CallExpression = function (node, parent, file, scope) {
exports.CallExpression = function (node, parent, scope, context, file) {
var args = node.arguments;
if (!hasSpread(args)) return;
@@ -95,7 +97,7 @@ exports.CallExpression = function (node, parent, file, scope) {
node.arguments.unshift(contextLiteral);
};
exports.NewExpression = function (node, parent, file) {
exports.NewExpression = function (node, parent, scope, context, file) {
var args = node.arguments;
if (!hasSpread(args)) return;

View File

@@ -1,26 +1,30 @@
"use strict";
var t = require("../../types");
var buildBinaryExpression = function (left, right) {
return t.binaryExpression("+", left, right);
};
exports.TaggedTemplateExpression = function (node, parent, file) {
exports.TaggedTemplateExpression = function (node, parent, scope, context, file) {
var args = [];
var quasi = node.quasi;
var strings = [];
var raw = [];
for (var i in quasi.quasis) {
for (var i = 0; i < quasi.quasis.length; i++) {
var elem = quasi.quasis[i];
strings.push(t.literal(elem.value.cooked));
raw.push(t.literal(elem.value.raw));
}
args.push(t.callExpression(file.addHelper("tagged-template-literal"), [
t.arrayExpression(strings),
t.arrayExpression(raw)
]));
strings = t.arrayExpression(strings);
raw = t.arrayExpression(raw);
var templateName = "tagged-template-literal";
if (file.isLoose("templateLiterals")) templateName += "-loose";
args.push(t.callExpression(file.addHelper(templateName), [strings, raw]));
args = args.concat(quasi.expressions);
@@ -31,7 +35,7 @@ exports.TemplateLiteral = function (node) {
var nodes = [];
var i;
for (i in node.quasis) {
for (i = 0; i < node.quasis.length; i++) {
var elem = node.quasis[i];
nodes.push(t.literal(elem.value.cooked));
@@ -47,7 +51,7 @@ exports.TemplateLiteral = function (node) {
var root = buildBinaryExpression(nodes.shift(), nodes.shift());
for (i in nodes) {
for (i = 0; i < nodes.length; i++) {
root = buildBinaryExpression(root, nodes[i]);
}

View File

@@ -1,3 +1,5 @@
"use strict";
var rewritePattern = require("regexpu/rewrite-pattern");
var _ = require("lodash");

View File

@@ -1,3 +1,5 @@
"use strict";
// https://github.com/zenparsing/es-abstract-refs
var util = require("../../util");
@@ -21,7 +23,7 @@ var container = function (parent, call, ret) {
}
};
exports.AssignmentExpression = function (node, parent, file, scope) {
exports.AssignmentExpression = function (node, parent, scope, context, file) {
var left = node.left;
if (!t.isVirtualPropertyExpression(left)) return;
@@ -74,7 +76,7 @@ exports.UnaryExpression = function (node, parent) {
return container(parent, call, t.literal(true));
};
exports.CallExpression = function (node, parent, file, scope) {
exports.CallExpression = function (node, parent, scope, context, file) {
var callee = node.callee;
if (!t.isVirtualPropertyExpression(callee)) return;

View File

@@ -1,10 +1,13 @@
var traverse = require("../../traverse");
var util = require("../../util");
var t = require("../../types");
"use strict";
var buildComprehension = require("../helpers/build-comprehension");
var traverse = require("../../traverse");
var util = require("../../util");
var t = require("../../types");
exports.experimental = true;
var build = function (node, parent, file, scope) {
var build = function (node, parent, scope, context, file) {
var uid = scope.generateUidBasedOnNode(parent, file);
var container = util.template("array-comprehension-container", {
@@ -22,7 +25,7 @@ var build = function (node, parent, file, scope) {
var returnStatement = body.pop();
body.push(exports._build(node, function () {
body.push(buildComprehension(node, function () {
return util.template("array-push", {
STATEMENT: node.body,
KEY: uid
@@ -33,30 +36,8 @@ var build = function (node, parent, file, scope) {
return container;
};
exports._build = function (node, buildBody) {
var self = node.blocks.shift();
if (!self) return;
var child = exports._build(node, buildBody);
if (!child) {
// last item
child = buildBody();
// add a filter as this is our final stop
if (node.filter) {
child = t.ifStatement(node.filter, t.blockStatement([child]));
}
}
return t.forOfStatement(
t.variableDeclaration("let", [t.variableDeclarator(self.left)]),
self.right,
t.blockStatement([child])
);
};
exports.ComprehensionExpression = function (node, parent, file, scope) {
exports.ComprehensionExpression = function (node, parent, scope, context, file) {
if (node.generator) return;
return build(node, parent, file, scope);
return build(node, parent, scope, context, file);
};

View File

@@ -1,18 +1,18 @@
"use strict";
// https://github.com/rwaldron/exponentiation-operator
exports.experimental = true;
var t = require("../../types");
var pow = t.memberExpression(t.identifier("Math"), t.identifier("pow"));
var build = require("../helpers/build-binary-assignment-operator-transformer");
var t = require("../../types");
exports.AssignmentExpression = function (node) {
if (node.operator !== "**=") return;
node.operator = "=";
node.right = t.callExpression(pow, [node.left, node.right]);
};
var MATH_POW = t.memberExpression(t.identifier("Math"), t.identifier("pow"));
exports.BinaryExpression = function (node) {
if (node.operator !== "**") return;
build(exports, {
operator: "**",
return t.callExpression(pow, [node.left, node.right]);
};
build: function (left, right) {
return t.callExpression(MATH_POW, [left, right]);
}
});

View File

@@ -1,4 +1,6 @@
var arrayComprehension = require("./es7-array-comprehension");
"use strict";
var buildComprehension = require("../helpers/build-comprehension");
var t = require("../../types");
exports.experimental = true;
@@ -10,7 +12,7 @@ exports.ComprehensionExpression = function (node) {
var container = t.functionExpression(null, [], t.blockStatement(body), true);
container._aliasFunction = true;
body.push(arrayComprehension._build(node, function () {
body.push(buildComprehension(node, function () {
return t.expressionStatement(t.yieldExpression(node.body));
}));

View File

@@ -1,14 +1,16 @@
"use strict";
// https://github.com/sebmarkbage/ecmascript-rest-spread
var t = require("../../types");
exports.experimental = true;
exports.ObjectExpression = function (node, parent, file) {
exports.ObjectExpression = function (node, parent, scope, context, file) {
var hasSpread = false;
var i;
var prop;
for (i in node.properties) {
for (i = 0; i < node.properties.length; i++) {
prop = node.properties[i];
if (t.isSpreadProperty(prop)) {
hasSpread = true;
@@ -26,7 +28,7 @@ exports.ObjectExpression = function (node, parent, file) {
props = [];
};
for (i in node.properties) {
for (i = 0; i < node.properties.length; i++) {
prop = node.properties[i];
if (t.isSpreadProperty(prop)) {
push();

View File

@@ -1,11 +1,14 @@
var bluebirdCoroutines = require("./optional-bluebird-coroutines");
"use strict";
var remapAsyncToGenerator = require("../helpers/remap-async-to-generator");
var bluebirdCoroutines = require("./optional-bluebird-coroutines");
exports.optional = true;
exports.manipulateOptions = bluebirdCoroutines.manipulateOptions;
exports.Function = function (node, parent, file) {
exports.Function = function (node, parent, scope, context, file) {
if (!node.async || node.generator) return;
return bluebirdCoroutines._Function(node, file.addHelper("async-to-generator"));
return remapAsyncToGenerator(node, file.addHelper("async-to-generator"));
};

View File

@@ -0,0 +1,47 @@
var traverse = require("../../traverse");
var t = require("../../types");
exports.optional = true;
exports.Loop =
exports.Program =
exports.BlockStatement = function (node, parent, scope, context, file) {
var letRefs = node._letReferences;
if (!letRefs) return;
var state = {
letRefs: letRefs,
file: file
};
traverse(node, {
enter: function (node, parent, scope, context, state) {
if (!t.isIdentifier(node)) return;
if (!t.isReferenced(node, parent)) return;
var declared = state.letRefs[node.name];
if (!declared) return;
// declared node is different in this scope
if (scope.get(node.name, true) !== declared) return;
var declaredLoc = declared.loc;
var referenceLoc = node.loc;
if (!declaredLoc || !referenceLoc) return;
// does this reference appear on a line before the declaration?
var before = referenceLoc.start.line < declaredLoc.start.line;
if (referenceLoc.start.line === declaredLoc.start.line) {
// this reference appears on the same line
// check it appears before the declaration
before = referenceLoc.start.col < declaredLoc.start.col;
}
if (before) {
throw state.file.errorWithNode(node, "Temporal dead zone - accessing a variable before it's initialized");
}
}
}, scope, state);
};

View File

@@ -1,5 +1,7 @@
var traverse = require("../../traverse");
var t = require("../../types");
"use strict";
var remapAsyncToGenerator = require("../helpers/remap-async-to-generator");
var t = require("../../types");
exports.manipulateOptions = function (opts) {
opts.experimental = true;
@@ -8,36 +10,12 @@ exports.manipulateOptions = function (opts) {
exports.optional = true;
exports._Function = function (node, callId) {
node.async = false;
node.generator = true;
traverse(node, {
enter: function (node) {
if (t.isFunction(node)) this.skip();
if (t.isAwaitExpression(node)) {
node.type = "YieldExpression";
}
}
});
var call = t.callExpression(callId, [node]);
if (t.isFunctionDeclaration(node)) {
var declar = t.variableDeclaration("var", [
t.variableDeclarator(node.id, call)
]);
declar._blockHoist = true;
return declar;
} else {
return call;
}
};
exports.Function = function (node, parent, file) {
exports.Function = function (node, parent, scope, context, file) {
if (!node.async || node.generator) return;
var id = file.addImport("bluebird");
return exports._Function(node, t.memberExpression(id, t.identifier("coroutine")));
return remapAsyncToGenerator(
node,
t.memberExpression(file.addImport("bluebird"), t.identifier("coroutine"))
);
};

View File

@@ -1,74 +0,0 @@
var traverse = require("../../traverse");
var util = require("../../util");
var t = require("../../types");
exports.optional = true;
exports.Class = function (node) {
var superClass = node.superClass || t.identifier("Function");
var hasConstructor = false;
var body = node.body.body;
for (var i in body) {
var methodNode = body[i];
hasConstructor = hasConstructor || methodNode.key.name === "constructor";
traverse(methodNode, {
enter: function (node, parent) {
if (t.isIdentifier(node, { name: "super" })) {
return superIdentifier(superClass, methodNode, node, parent);
} else if (t.isCallExpression(node)) {
var callee = node.callee;
if (!t.isMemberExpression(callee)) return;
if (callee.object.name !== "super") return;
// super.test(); -> ClassName.prototype.MethodName.call(this);
t.appendToMemberExpression(callee, t.identifier("call"));
node.arguments.unshift(t.thisExpression());
}
}
});
}
if (node.superClass && !hasConstructor) {
body.unshift(t.methodDefinition(
t.identifier("constructor"),
util.template("class-super-constructor-call-fast", {
SUPER_NAME: superClass
})
));
}
};
var superIdentifier = function (superClass, methodNode, id, parent) {
var methodName = methodNode.key;
if (parent.property === id) {
return;
} else if (t.isCallExpression(parent, { callee: id })) {
// super(); -> ClassName.prototype.MethodName.call(this);
parent.arguments.unshift(t.thisExpression());
if (methodName.name === "constructor") {
// constructor() { super(); }
return t.memberExpression(superClass, t.identifier("call"));
} else {
id = superClass;
// foo() { super(); }
if (!methodNode.static) {
id = t.memberExpression(id, t.identifier("prototype"));
}
id = t.memberExpression(id, methodName, methodNode.computed);
return t.memberExpression(id, t.identifier("call"));
}
} else if (t.isMemberExpression(parent) && !methodNode.static) {
// super.test -> ClassName.prototype.test
return t.memberExpression(superClass, t.identifier("prototype"));
} else {
return superClass;
}
};

View File

@@ -1,3 +1,5 @@
"use strict";
var traverse = require("../../traverse");
var util = require("../../util");
var core = require("core-js/library");
@@ -14,8 +16,7 @@ var ALIASABLE_CONSTRUCTORS = [
"Map",
"WeakMap",
"Set",
"WeakSet",
"Dict",
"WeakSet"
];
exports.optional = true;
@@ -27,7 +28,7 @@ exports.ast = {
exit: function (ast, file) {
traverse(ast, {
enter: function (node, parent) {
enter: function (node, parent, scope, context) {
var prop;
if (t.isMemberExpression(node) && t.isReferenced(node, parent)) {
@@ -38,7 +39,7 @@ exports.ast = {
if (!t.isReferenced(obj, node)) return;
if (!node.computed && coreHas(obj) && _.has(core[obj.name], prop.name)) {
this.skip();
context.skip();
return t.prependToMemberExpression(node, file._coreId);
}
} else if (t.isIdentifier(node) && !t.isMemberExpression(parent) && t.isReferenced(node, parent) && _.contains(ALIASABLE_CONSTRUCTORS, node.name)) {

View File

@@ -1,37 +0,0 @@
var util = require("../../util");
var t = require("../../types");
exports.optional = true;
exports.ForOfStatement = function (node, parent, file, scope) {
var left = node.left;
var declar, id;
if (t.isIdentifier(left) || t.isPattern(left)) {
id = left;
} else if (t.isVariableDeclaration(left)) {
id = left.declarations[0].id;
declar = t.variableDeclaration(left.kind, [
t.variableDeclarator(id)
]);
} else {
throw file.errorWithNode(left, "Unknown node type " + left.type + " in ForOfStatement");
}
var node2 = util.template("for-of-fast", {
LOOP_OBJECT: file.generateUidIdentifier("loopObject", scope),
IS_ARRAY: file.generateUidIdentifier("isArray", scope),
OBJECT: node.right,
INDEX: file.generateUidIdentifier("i", scope),
ID: id
});
t.inheritsComments(node2, node);
t.ensureBlock(node);
var block = node2.body;
if (declar) block.body.unshift(declar);
block.body = block.body.concat(node.body.body);
return node2;
};

View File

@@ -1,3 +1,5 @@
"use strict";
var t = require("../../types");
var _ = require("lodash");
@@ -17,7 +19,7 @@ var buildDefaultsCallExpression = function (expr, ref, file) {
exports.optional = true;
exports.secondPass = true;
exports.AssignmentExpression = function (node, parent, file, scope) {
exports.AssignmentExpression = function (node, parent, scope, context, file) {
if (t.isExpressionStatement(parent)) return;
if (!isProtoAssignmentExpression(node)) return;
@@ -32,7 +34,7 @@ exports.AssignmentExpression = function (node, parent, file, scope) {
return t.toSequenceExpression(nodes);
};
exports.ExpressionStatement = function (node, parent, file) {
exports.ExpressionStatement = function (node, parent, scope, context, file) {
var expr = node.expression;
if (!t.isAssignmentExpression(expr, { operator: "=" })) return;
@@ -41,10 +43,10 @@ exports.ExpressionStatement = function (node, parent, file) {
}
};
exports.ObjectExpression = function (node, parent, file) {
exports.ObjectExpression = function (node, parent, scope, context, file) {
var proto;
for (var i in node.properties) {
for (var i = 0; i < node.properties.length; i++) {
var prop = node.properties[i];
if (isProtoKey(prop)) {

View File

@@ -1,9 +1,23 @@
"use strict";
var t = require("../../types");
exports.optional = true;
exports.UnaryExpression = function (node, parent, file) {
exports.UnaryExpression = function (node, parent, scope, context, file) {
context.skip();
if (node.operator === "typeof") {
return t.callExpression(file.addHelper("typeof"), [node.argument]);
var call = t.callExpression(file.addHelper("typeof"), [node.argument]);
if (t.isIdentifier(node.argument)) {
var undefLiteral = t.literal("undefined");
return t.conditionalExpression(
t.binaryExpression("===", t.unaryExpression("typeof", node.argument), undefLiteral),
undefLiteral,
call
);
} else {
return call;
}
}
};

View File

@@ -1,3 +1,5 @@
"use strict";
var t = require("../../types");
exports.optional = true;

Some files were not shown because too many files have changed in this diff Show More