Compare commits

..

401 Commits

Author SHA1 Message Date
Sebastian McKenzie
8f10dc5c39 v5.0.0-beta2 2015-03-28 05:46:57 +11:00
Sebastian McKenzie
0f99bbe293 fix test tmp directory gitignore 2015-03-28 05:45:46 +11:00
Sebastian McKenzie
0a5d2c098f add tag beta to publish scripts 2015-03-28 05:45:37 +11:00
Sebastian McKenzie
dcf4381e26 5.0.0-beta1 2015-03-28 05:45:22 +11:00
Sebastian McKenzie
543582ef47 fix helper filename in build-runtime script 2015-03-28 05:42:55 +11:00
Sebastian McKenzie
a4db58ccaa v5.0.0-beta1 2015-03-28 05:40:16 +11:00
Sebastian McKenzie
d3971e0874 move es6.arrowFunctions transformer up 2015-03-28 05:38:31 +11:00
Sebastian McKenzie
ad98186afc remove redundant semicolons from export declarations 2015-03-28 05:38:22 +11:00
Sebastian McKenzie
df251fbfff fix test paths to reflect new test directory structure 2015-03-28 05:38:12 +11:00
Sebastian McKenzie
dfbcf95b0e disable sourcemap stdin tests 2015-03-28 05:37:55 +11:00
Sebastian McKenzie
07646475d9 fix spread binding with the wrong context 2015-03-28 05:37:38 +11:00
Sebastian McKenzie
9bc67c28e3 fix path api usage in es6.forOf 2015-03-28 05:37:15 +11:00
Sebastian McKenzie
c2990d3548 add array cache to es6.destructuring to avoid running toArray multiple times 2015-03-28 05:37:06 +11:00
Sebastian McKenzie
803f8a3bbc fix paths in api test 2015-03-28 04:38:26 +11:00
Sebastian McKenzie
ee6efee4f8 disable es6 do expressions and decorators tests 2015-03-28 04:37:30 +11:00
Sebastian McKenzie
3f144e6e74 ensure that nodes are only traversed once per context 2015-03-28 04:34:05 +11:00
Sebastian McKenzie
afedfe15ae fix external helper build script to reflect filename schema changes 2015-03-28 04:08:02 +11:00
Sebastian McKenzie
b522294d3c fix helper slice filename 2015-03-28 04:07:44 +11:00
Sebastian McKenzie
954af72476 update various tests to reflect the new architecture 2015-03-28 04:00:20 +11:00
Sebastian McKenzie
de42a6f796 add existence check for child and parent when inheriting comments 2015-03-28 04:00:02 +11:00
Sebastian McKenzie
06a58f228c add TraversalPath#replaceInline, fix traversal path not updating sibling keys correctly 2015-03-28 03:59:51 +11:00
Sebastian McKenzie
7a44576c26 cache rc json 2015-03-28 03:59:14 +11:00
Sebastian McKenzie
eaa3865427 remove es7.classProperties check from parser 2015-03-28 03:59:01 +11:00
Sebastian McKenzie
b83907cc67 restructure testing infrastructure to be more modular 2015-03-28 01:21:48 +11:00
Sebastian McKenzie
a3620a58f8 add externalHelpers to source maps tests 2015-03-28 00:26:11 +11:00
Sebastian McKenzie
67a12f03fa use replaceWithNodes rather than replace in es6.forOf transformer 2015-03-28 00:26:04 +11:00
Sebastian McKenzie
c49871d33c stylistic changes to tests 2015-03-28 00:25:45 +11:00
Sebastian McKenzie
c828688d21 add existence check to t.inherits 2015-03-28 00:23:07 +11:00
Sebastian McKenzie
bd052aea50 inherit first replacement node from the current node 2015-03-28 00:22:59 +11:00
Sebastian McKenzie
ed74ccaa7d add support for decorators before class exports 2015-03-28 00:22:38 +11:00
Sebastian McKenzie
f3c9551b75 add MetaProperty visitor keys 2015-03-28 00:22:18 +11:00
Sebastian McKenzie
710a7f882d add loose mode for es6.spread and es6.destructuring that assumes all values are arrays - emberjs/ember.js#10730 2015-03-28 00:21:37 +11:00
Sebastian McKenzie
902ebd3fe7 switch back helpers to Array.isArray 2015-03-28 00:21:15 +11:00
Sebastian McKenzie
23c20cc47c inherit from the user constructor properly 2015-03-26 01:02:47 +11:00
Sebastian McKenzie
9b627ace5d impose a strict API for traversal paths and ensure AST doesn't have holes mid-transform 2015-03-26 01:02:30 +11:00
Sebastian McKenzie
4fa17341f5 add support for object expressions and patterns to scope.generateUidBasedOnNode 2015-03-26 01:00:32 +11:00
Sebastian McKenzie
1e019c0f16 realign alias keys data 2015-03-26 01:00:12 +11:00
Sebastian McKenzie
ce25ebacbf only coerce the right side in template literal implosion 2015-03-26 00:59:58 +11:00
Sebastian McKenzie
daecec2a86 better parsing of decorators - support class expressions 2015-03-26 00:58:38 +11:00
Sebastian McKenzie
238f9c2e7e call resolveModuleSource on dynamic imports - fixes #1085 2015-03-26 00:50:58 +11:00
Sebastian McKenzie
bc5f2b9be6 add param to list of duplicate declaration kinds - fixes #1084 2015-03-26 00:48:17 +11:00
Sebastian McKenzie
5390715705 update tests to latest experimental changes 2015-03-25 20:20:51 +11:00
Sebastian McKenzie
aa1333123c Merge branch 'master' into experimental
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
2015-03-25 14:17:46 +11:00
Sebastian McKenzie
e60c9fba6a better node injection mechanics that handle statement lists 2015-03-24 04:37:56 +11:00
Sebastian McKenzie
902db03e96 add loose mode to template literals 2015-03-24 03:34:42 +11:00
Sebastian McKenzie
de88b28988 add class property initializers, add more TraversalPath flexibility with additional node injection method 2015-03-24 03:34:34 +11:00
Sebastian McKenzie
0ee230d13c coerce template literal expressions to strings - fixes #1065 2015-03-23 22:04:40 +11:00
Sebastian McKenzie
c12cfe3ed0 make method definitions with expression bodies illegal - fixes #1066 2015-03-23 20:24:32 +11:00
Sebastian McKenzie
1376f2f18f move helper templates to a separate namespace 2015-03-23 17:26:54 +11:00
Sebastian McKenzie
972c621f59 remove template literals from bin 2015-03-23 17:26:31 +11:00
Sebastian McKenzie
5c41a33e41 save register cache on tick instead of sigint - fixes #1062 2015-03-23 17:26:08 +11:00
Sebastian McKenzie
bc6d01d6fc enable strict mode on babel-node eval flag - fixes #1060 2015-03-23 16:53:03 +11:00
Sebastian McKenzie
01463faf36 remove apply-constructor helper from es6.spread transformer - fixes #1052 2015-03-23 16:51:14 +11:00
Sebastian McKenzie
b234603e24 add callDelegate transformation helper - fixes #1064 2015-03-23 16:47:17 +11:00
Sebastian McKenzie
ca79fff0c4 remove acorn compiled source 2015-03-22 04:09:18 +11:00
Sebastian McKenzie
3c8f3cb521 update couple tests to reflect new statement explosion 2015-03-22 04:08:55 +11:00
Sebastian McKenzie
c9974a42ea remove legacy react metadata 2015-03-22 04:08:45 +11:00
Sebastian McKenzie
a87f6f6fdd clean up statement to expression explosion 2015-03-22 04:08:35 +11:00
Sebastian McKenzie
3d28ce7903 git ignore entire lib folder 2015-03-22 04:07:53 +11:00
Sebastian McKenzie
e04ecc79c7 add support for class decorators 2015-03-22 04:07:38 +11:00
Sebastian McKenzie
3e26511fb2 remove ShadowFunctionExpression 2015-03-22 04:07:00 +11:00
Sebastian McKenzie
9c3493e02f add do expressions 2015-03-22 04:06:15 +11:00
Sebastian McKenzie
a14e979897 use module syntax for strict module formatters 2015-03-21 15:27:32 +11:00
Sebastian McKenzie
a4ce653bed update tests to reflect fixed destructuring behaviour and statement to expression explosion 2015-03-21 15:27:13 +11:00
Sebastian McKenzie
70d068226a add ability to explode a list of statements when trying to replace an expression - damn this is powerful and allows some SUPER cool scenarios 2015-03-21 15:26:55 +11:00
Sebastian McKenzie
30b1c0154d update to modular acorn 2015-03-21 15:25:32 +11:00
Sebastian McKenzie
a602873281 update to latest acorn, better array shortcuts, don't add code frame to error message 2015-03-20 20:05:29 +11:00
Sebastian McKenzie
c8b88182f6 fix bin tests 2015-03-20 20:03:49 +11:00
Sebastian McKenzie
64f1c114c1 fix modules generation logic and remove dead node types 2015-03-19 03:17:28 +11:00
Sebastian McKenzie
4ac1a856ae fix remaining issues in estree port 2015-03-19 02:59:44 +11:00
Sebastian McKenzie
0e68b6ca11 add highlightErrors option to parser options 2015-03-19 02:59:30 +11:00
Sebastian McKenzie
2dccc8c919 more estree updates - finish flow parsing 2015-03-19 01:48:44 +11:00
Sebastian McKenzie
fdea18a289 properly support iterators in sliced-to-array helper... 2015-03-19 01:47:19 +11:00
Sebastian McKenzie
c3bdecbd25 move arrow functions transformer to before function names 2015-03-19 01:46:53 +11:00
Sebastian McKenzie
ee63fb52b7 check whitelist for existence rather than length - #1044 2015-03-19 01:46:10 +11:00
Sebastian McKenzie
8575392606 4.7.16 2015-03-18 23:37:24 +11:00
Sebastian McKenzie
d28583bea1 v4.7.16 2015-03-18 23:36:04 +11:00
Sebastian McKenzie
b166f35218 add typed arrays to native types list - #1043 2015-03-18 23:34:37 +11:00
Sebastian McKenzie
0914378bba add 4.7.16 changelog 2015-03-18 23:34:26 +11:00
Sebastian McKenzie
660d764390 remove loop check when aliasing breaks, continues etc - fixes #1051 2015-03-18 23:31:11 +11:00
Sebastian McKenzie
27206d6b7c add complete list of native constructors 2015-03-18 23:27:14 +11:00
Sebastian McKenzie
34dc56b435 Merge branch 'master' into experimental
# Conflicts:
#	src/babel/transformation/transformers/es7/abstract-references.js
#	src/babel/transformation/transformers/playground/mallet-operator.js
#	src/babel/transformation/transformers/playground/memoization-operator.js
#	src/babel/types/index.js
2015-03-18 19:18:25 +11:00
Sebastian McKenzie
d919d7837c 4.7.15 2015-03-18 19:17:07 +11:00
Sebastian McKenzie
b49cf4e779 v4.7.15 2015-03-18 19:16:01 +11:00
Sebastian McKenzie
e30dd7042b fix constructor spreading of collections #1043 2015-03-18 19:14:23 +11:00
Sebastian McKenzie
16d8d928e8 4.7.14 2015-03-18 19:14:02 +11:00
Sebastian McKenzie
4966028397 v4.7.14 2015-03-18 19:11:21 +11:00
Sebastian McKenzie
82e04fffac add 4.7.14 changelog 2015-03-18 19:08:53 +11:00
Sebastian McKenzie
a46313903d deprecate abstract references and remaining playground features 2015-03-18 19:08:05 +11:00
Sebastian McKenzie
68340b1288 add Promise to the list of native types - fixes #1043 2015-03-18 19:06:34 +11:00
Sebastian McKenzie
6a1b33b443 more updates to estree and new acorn 2015-03-18 19:02:15 +11:00
Sebastian McKenzie
df7524d909 update babel dev dependency and use for-of in sliced-to-array helper - fixes #1047 2015-03-18 19:01:58 +11:00
Sebastian McKenzie
2c39d406d1 move acorn into vendor 2015-03-18 19:01:30 +11:00
Sebastian McKenzie
2ff1a5d98b Merge branch 'master' into experimental
# Conflicts:
#	src/babel/transformation/transformers/internal/strict.js
2015-03-17 12:14:32 +11:00
Sebastian McKenzie
7f9fe4af15 4.7.13 2015-03-17 12:12:51 +11:00
Sebastian McKenzie
977290c7e4 v4.7.13 2015-03-17 12:11:26 +11:00
Sebastian McKenzie
ebbdb95278 add 4.7.13 changelog 2015-03-17 12:09:16 +11:00
Sebastian McKenzie
9b586c7587 add support for left side patterns in assignment pattern destructuring - fixes #1037 2015-03-17 12:03:13 +11:00
Brian Donovan
4b5ba6c8c6 Add a test for the this-spread fix in 976e8c1.
Addresses #1033.
2015-03-17 11:31:09 +11:00
Sebastian McKenzie
67efb1b427 ignore this expressions in Scope#generateTempBasedOnNode - fixes #1033 2015-03-17 11:31:00 +11:00
Sebastian McKenzie
bc9c1ab61e handle comments and use strict directives better - fixes #1030 2015-03-17 11:30:30 +11:00
Sebastian McKenzie
65ffe07551 remove react test option 2015-03-17 11:26:28 +11:00
Sebastian McKenzie
b606cc79ca remove class playground property initializer tests 2015-03-17 11:26:18 +11:00
Sebastian McKenzie
0e7fd0741d inject transformer parser plugins 2015-03-17 11:26:06 +11:00
Sebastian McKenzie
276cafaae1 Merge branch 'master' of github.com:babel/babel 2015-03-17 02:45:36 +11:00
Sebastian McKenzie
4f00de50d6 remove playground code gen, remove react metadata, start update of modules to estree 2015-03-17 02:45:16 +11:00
Brian Donovan
b07341a1f7 Add a note about using the --grep option to focus test runs. 2015-03-16 08:45:05 -07:00
Sebastian McKenzie
ec526f9224 embed acorn 2015-03-17 02:44:05 +11:00
Brian Donovan
76301a7713 Fix grammar. 2015-03-16 08:43:45 -07:00
Brian Donovan
67ac91815b Add a test for the this-spread fix in 976e8c1.
Addresses #1033.
2015-03-16 08:40:32 -07:00
Sebastian McKenzie
976e8c1cfd ignore this expressions in Scope#generateTempBasedOnNode - fixes #1033 2015-03-17 02:20:54 +11:00
Sebastian McKenzie
4d965b0f46 consolidate transformer metadata, remove abstract references, make react transformer optional 2015-03-16 19:26:50 +11:00
Sebastian McKenzie
b495370740 only compile babel folder in src 2015-03-16 19:25:58 +11:00
Sebastian McKenzie
b1c5915ed3 remove playground 2015-03-16 16:56:24 +11:00
Sebastian McKenzie
d94bb2e048 handle comments and use strict directives better - fixes #1030 2015-03-16 13:07:28 +11:00
Sebastian McKenzie
25fa10e592 fix syntax errors 2015-03-16 02:00:19 +11:00
Sebastian McKenzie
8d3a7acbd1 add symbol check to defineProperty helper - fixes #1010 2015-03-16 02:00:10 +11:00
Sebastian McKenzie
29d33dd31a Merge branch 'master' of github.com:babel/babel 2015-03-16 01:52:14 +11:00
Sebastian McKenzie
4afd0f553e Merge branch 'experimental'
# Conflicts:
#	src/babel/transformation/transformers/playground/method-binding.js
2015-03-16 01:51:52 +11:00
Sebastian McKenzie
1fb877e9f5 add highlightErrors option - fixes #837 2015-03-16 01:50:37 +11:00
Sebastian McKenzie
866d3a8eab move file into a separate folder 2015-03-16 01:45:08 +11:00
Sebastian McKenzie
76e1f0dd9f add babelrc to vendor 2015-03-16 01:44:17 +11:00
Sebastian McKenzie
1bbe52a022 split up module class declarations - fixes #1021 2015-03-16 01:44:10 +11:00
Sebastian McKenzie
cc0f43813c small style fixes 2015-03-16 01:26:26 +11:00
Sebastian McKenzie
b13b4bbfba add buildPass File check in preperation for plugin api 2015-03-16 01:25:59 +11:00
Sebastian McKenzie
1c6ac0a07a designate all playground and es7 transformers as optional 2015-03-16 01:25:25 +11:00
Sebastian McKenzie
5a319fd553 automate option generation 2015-03-16 01:23:57 +11:00
Sebastian McKenzie
a8cedf813b Revert "remove es7.abstractReferences, playground.methodBinding and playground.objectGetterMemoisation"
This reverts commit f4ce216d1c.
2015-03-16 01:02:35 +11:00
Sebastian McKenzie
9f037fa75e Merge pull request #1025 from thejameskyle/tjk/fix-flag
Correct --gdbjit v8 flag
2015-03-15 18:41:36 +11:00
James Kyle
8681a78618 Correct --gdbjit v8 flag 2015-03-15 00:39:48 -07:00
Sebastian McKenzie
2d72d423b3 Merge pull request #1024 from thejameskyle/tjk/1020-v8-flags
Add all v8 flags (node & iojs)
2015-03-15 18:35:19 +11:00
James Kyle
280964eafe Add all v8 flags (node & iojs). Fixes #1020 2015-03-15 00:33:39 -07:00
Sebastian McKenzie
5ee9b3c37e remove irrelevant test 2015-03-14 04:50:46 +11:00
Sebastian McKenzie
9676056f08 add note about member expression resolution 2015-03-14 04:50:23 +11:00
Sebastian McKenzie
080a844457 rename "reassigned" to "constant" to reflect purpose of the property better 2015-03-14 04:44:51 +11:00
Sebastian McKenzie
1f9f57a2a6 use paths in replace supers helper 2015-03-14 03:58:18 +11:00
Sebastian McKenzie
056b7fc634 hoist uids to their parent function 2015-03-14 03:58:02 +11:00
Sebastian McKenzie
547ecc2f73 update tests to reflect new for-of behaviour 2015-03-14 03:57:53 +11:00
Sebastian McKenzie
a1ab8fa0c1 remove playground.memoisationOperator deprecation message 2015-03-14 03:57:39 +11:00
Sebastian McKenzie
9e61a307d4 fix scope traversal 2015-03-14 03:34:02 +11:00
Sebastian McKenzie
d4d18f4230 Merge branch 'experimental' of github.com:babel/babel into experimental
# Conflicts:
#	src/babel/transformation/transformers/playground/object-getter-memoization.js
#	src/babel/traversal/path/index.js
#	src/babel/traversal/scope.js
2015-03-14 03:10:17 +11:00
Sebastian McKenzie
f4ce216d1c remove es7.abstractReferences, playground.methodBinding and playground.objectGetterMemoisation 2015-03-14 03:00:41 +11:00
Sebastian McKenzie
d71c72de3c 4.7.12 2015-03-14 02:39:09 +11:00
Sebastian McKenzie
b1c21b8d4d v4.7.12 2015-03-14 02:37:30 +11:00
Sebastian McKenzie
e9b0005cf5 deprecate playground.methodBinding 2015-03-14 02:35:10 +11:00
Sebastian McKenzie
63bac550cd Merge branch 'master' into experimental
# Conflicts:
#	src/babel/transformation/transformers/es6/for-of.js
#	src/babel/transformation/transformers/playground/object-getter-memoization.js
2015-03-14 02:34:03 +11:00
Sebastian McKenzie
d0b6aa9882 4.7.11 2015-03-14 02:30:59 +11:00
Sebastian McKenzie
0e877acfdf v4.7.11 2015-03-14 02:29:42 +11:00
Sebastian McKenzie
b670ee18df fix unicode regex test 2015-03-14 02:28:32 +11:00
Sebastian McKenzie
24807120bf fix unicode regexes stripping their unicode flag before being passed on two regexpu - fixes #1009 2015-03-14 02:26:20 +11:00
Sebastian McKenzie
33f7c0b67e rejigger regex flag pulling 2015-03-14 02:12:13 +11:00
Sebastian McKenzie
c23b47f796 4.7.10 2015-03-14 02:02:06 +11:00
Sebastian McKenzie
2871d8a710 v4.7.10 2015-03-14 01:57:26 +11:00
Sebastian McKenzie
3172b5de5d Merge branch 'master' of github.com:babel/babel 2015-03-14 01:53:15 +11:00
Sebastian McKenzie
2c05303813 deprecate some playground transformers 2015-03-14 01:52:59 +11:00
Sebastian McKenzie
75dda10057 add 4.7.10 changelog 2015-03-14 01:52:51 +11:00
Sebastian McKenzie
12664c6cd6 start migration of core from nodes to paths 2015-03-14 01:48:46 +11:00
Sebastian McKenzie
581f70c803 Merge pull request #1013 from Rich-Harris/patch-1
fix inputSourceMap option
2015-03-14 01:47:55 +11:00
Rich Harris
a00d2c33d8 fix inputSourceMap option
See #827 - it looks like `opts.inputSourceMap` is the wrong way round, babel should only use an input sourcemap if the `inputSourceMap` option is *not* `false`
2015-03-13 10:46:50 -04:00
Sebastian McKenzie
c906bd3edc start migration of core from nodes to paths 2015-03-14 01:00:02 +11:00
Sebastian McKenzie
27fc574e9f 4.7.9 2015-03-13 13:33:50 +11:00
Sebastian McKenzie
a3b1fcc79c v4.7.9 2015-03-13 13:30:14 +11:00
Sebastian McKenzie
491cb26c1f remove console.log 2015-03-13 13:29:13 +11:00
Sebastian McKenzie
7c3d052714 add 4.7.9 changelog 2015-03-13 13:27:40 +11:00
Sebastian McKenzie
4971d0c7f0 fix labeled nested for ofs - fixes #996 2015-03-13 13:25:37 +11:00
Sebastian McKenzie
eb4922b1ec update acorn-babel 2015-03-13 13:25:19 +11:00
Sebastian McKenzie
bb26183b44 set inputSourceMap to null by default 2015-03-13 13:14:17 +11:00
Sebastian McKenzie
4b066f7f1b add input sourcemap false option #827 2015-03-13 13:12:38 +11:00
Sebastian McKenzie
41a8257005 various cleanup, remove redundant whitespace, realign object keys etc 2015-03-13 01:09:04 +11:00
Sebastian McKenzie
b44ba25d11 separate binding logic from scope to a binding class, move binding type resolution to the path so it can be used on any expression - #653 2015-03-13 01:08:46 +11:00
Sebastian McKenzie
40a111abbf always resolve rc for absolute filenames 2015-03-13 01:07:33 +11:00
Sebastian McKenzie
9cff51915d switch scope tracking to a path based system 2015-03-12 14:35:55 +11:00
Sebastian McKenzie
3cd110a7c9 fix block scoping break switch collision - fixes #998 2015-03-12 14:35:30 +11:00
Sebastian McKenzie
a7f9e035a4 infer computed literal function names - fixes #993 2015-03-12 14:13:24 +11:00
Sebastian McKenzie
146b9e6708 4.7.8 2015-03-11 10:17:09 +11:00
Sebastian McKenzie
0953c48620 v4.7.8 2015-03-11 07:49:54 +11:00
Sebastian McKenzie
056b90831d manually define each property in computed class helper - fixes #984 2015-03-11 07:45:29 +11:00
Sebastian McKenzie
8ba276b209 4.7.7 2015-03-11 01:25:41 +11:00
Sebastian McKenzie
8eaa9d29e3 v4.7.7 2015-03-11 01:20:34 +11:00
Sebastian McKenzie
6212791356 fix types API exposure - babel/babel-eslint#8 2015-03-11 01:14:36 +11:00
Sebastian McKenzie
d436d32d82 4.7.6 2015-03-11 00:13:03 +11:00
Sebastian McKenzie
2a29f70bba v4.7.6 2015-03-11 00:11:35 +11:00
Sebastian McKenzie
6ccb8957bd add 4.7.6 changelog 2015-03-11 00:09:52 +11:00
Sebastian McKenzie
dc45415ee0 hoist esModule to the top 2015-03-11 00:08:06 +11:00
Sebastian McKenzie
98ca541fde restructure types 2015-03-11 00:04:06 +11:00
Sebastian McKenzie
8328f638c2 add support for if and block statements to t.toSequenceExpression 2015-03-10 17:20:26 +11:00
Sebastian McKenzie
5586ce280f move build-external-helpers to tools 2015-03-10 17:20:14 +11:00
Sebastian McKenzie
0ca71f5e15 add transformation logger 2015-03-10 17:19:51 +11:00
Sebastian McKenzie
212776e220 remove detection 2015-03-10 17:19:35 +11:00
Sebastian McKenzie
4a95a9ec8f remove duplicate insert_final_newline 2015-03-10 17:19:23 +11:00
Sebastian McKenzie
45953ffc8a 4.7.5 2015-03-10 13:39:04 +11:00
Sebastian McKenzie
9c79290bb2 v4.7.5 2015-03-10 13:20:20 +11:00
Sebastian McKenzie
cfdf6b7385 add 4.7.5 changelog 2015-03-10 13:12:19 +11:00
Sebastian McKenzie
907e0e0f86 drop support for node 0.10 to save travis some unnecessary cycles <3 2015-03-10 13:11:06 +11:00
Sebastian McKenzie
f3bd9cbcb8 use a different helper if a class contains class methods to avoid non-enumerability and delegation to es6.properties.computed transformer - fixes #984, closes #986 2015-03-10 13:04:02 +11:00
Sebastian McKenzie
3bd14f9e07 add babel version to register hook cache key 2015-03-10 13:03:22 +11:00
Sebastian McKenzie
102b4b3d1e remove redundant break wrapping in for-of - fixes #985 2015-03-10 12:14:21 +11:00
Sebastian McKenzie
6564f1ff76 4.7.4 2015-03-10 06:13:37 +11:00
Sebastian McKenzie
58d7a5e069 more versatile scope pushing 2015-03-10 06:13:32 +11:00
Sebastian McKenzie
f5db53cebe remove core-js library from babel-runtime 2015-03-10 06:13:16 +11:00
Sebastian McKenzie
2da010fcae clean up #982 2015-03-10 06:11:55 +11:00
Sebastian McKenzie
52b99bdf93 Merge pull request #982 from tricknotes/error-stack
Ignore assignment to Error#stack if it is readonly property
2015-03-10 06:09:03 +11:00
Ryunosuke SATO
9d7e953451 Ignore assignment to Error#stack if it is readonly property
`Error#stack` may be an readonly property in some environments
such as PhantomJS 1.9.2 and Safari 7.0.
2015-03-10 04:05:53 +09:00
Sebastian McKenzie
b1252b865b v4.7.4 2015-03-10 03:29:47 +11:00
Sebastian McKenzie
068b1341d9 fix erroneous whitespace 2015-03-10 03:28:36 +11:00
Sebastian McKenzie
20eb143915 remove Number from the list of valid runtime constructors - fixes #981 2015-03-10 03:27:13 +11:00
Sebastian McKenzie
1302a86bf2 scope -> scopable class alias key 2015-03-10 03:22:55 +11:00
Sebastian McKenzie
b7831f1d7b prepend mocha command with node 2015-03-10 03:15:31 +11:00
Sebastian McKenzie
e46cabb21f remove code coverage from travis make task 2015-03-10 03:12:50 +11:00
Sebastian McKenzie
fff06a047e Revert "delegate node path removal to after call"
This reverts commit 5e0236d9a7.
2015-03-10 02:50:43 +11:00
Sebastian McKenzie
6b0320fc83 disable test262 tests by default 2015-03-10 02:34:35 +11:00
Sebastian McKenzie
5dde63fa0a more elaborate tests for #980 2015-03-10 02:13:00 +11:00
Sebastian McKenzie
672118149a hoist all directives - fixes #980 2015-03-10 01:53:51 +11:00
Sebastian McKenzie
5e0236d9a7 delegate node path removal to after call 2015-03-10 01:44:58 +11:00
Sebastian McKenzie
5101664e7d update 4.7.4 changelog 2015-03-10 01:44:44 +11:00
Sebastian McKenzie
2d684a06d3 update changelog version to 4.7.4 2015-03-10 01:23:24 +11:00
Sebastian McKenzie
e31bad8f42 add 4.7.3 changelog 2015-03-10 01:17:14 +11:00
Sebastian McKenzie
644b4373fc rewrite named function expressions in async function transformers - fixes #979 2015-03-10 01:16:38 +11:00
Sebastian McKenzie
62f37c1e62 fix util.booleanify 2015-03-10 01:10:58 +11:00
Sebastian McKenzie
fc0e89463b use flow types for annotations - goodbye JSDoc! 2015-03-10 00:35:52 +11:00
Sebastian McKenzie
56b6a795a4 remove path.refreshScope until i'm more confident on it's functionality so it doesn't break peopls stuff 2015-03-09 23:09:22 +11:00
Sebastian McKenzie
17b34a2959 dynamic scope tracking, toot toot - fixes #957 2015-03-09 22:07:05 +11:00
Sebastian McKenzie
c4da0253c5 move flattenable keys to types 2015-03-09 16:55:00 +11:00
Sebastian McKenzie
5f2df40f6d consistent whitespace in patch 2015-03-09 16:54:50 +11:00
Sebastian McKenzie
7d20a9b882 add traversal path todos 2015-03-09 16:54:41 +11:00
Sebastian McKenzie
ee0ac9f149 use spread in messages 2015-03-09 16:54:30 +11:00
Sebastian McKenzie
c5d3f42d8a alias core-js/library in babel-runtime - closes #965 2015-03-09 16:54:19 +11:00
Sebastian McKenzie
d162919a53 update symols transformer name 2015-03-09 03:39:30 +11:00
Sebastian McKenzie
d1f712344d add istanbul auxiliary comment task to makefile 2015-03-09 03:38:39 +11:00
Sebastian McKenzie
e985912b54 add Binary node cleanup 2015-03-09 03:38:27 +11:00
Sebastian McKenzie
97a21e4a34 update spec.typeofSymbol references to es6.symbols 2015-03-09 03:38:13 +11:00
Sebastian McKenzie
d8fb7812f7 more utility.removeConsole tests 2015-03-09 03:37:42 +11:00
Sebastian McKenzie
bfe63f5885 rename spec.typeofSymbol transformer to es6.symbols 2015-03-09 03:37:26 +11:00
Sebastian McKenzie
44ca6873a3 add super to contextVariables 2015-03-09 03:37:05 +11:00
Sebastian McKenzie
2f66ea7338 4.7.3 2015-03-08 04:33:50 +11:00
Sebastian McKenzie
70967cf53c v4.7.3 2015-03-08 04:32:46 +11:00
Sebastian McKenzie
dc117a74ae both -> inline - thanks @dkieks 2015-03-08 04:29:48 +11:00
Sebastian McKenzie
b0f32a822d pass file.addImport a name instead of an identifier 2015-03-08 04:10:53 +11:00
Sebastian McKenzie
fd99ea1749 Merge branch 'master' of github.com:babel/babel 2015-03-08 04:05:44 +11:00
Sebastian McKenzie
572129542d fallback to current parentPath if none was provided 2015-03-08 04:04:48 +11:00
Sebastian McKenzie
6f53980bfc move runtime helpers to separate modules - babel/ember-cli-babel#24 2015-03-08 04:04:40 +11:00
Sebastian McKenzie
348af1990d Merge pull request #972 from neVERberleRfellerER/node-external-helpers-tests
add tests for babel-external-helpers
2015-03-08 03:50:28 +11:00
Ondrej Kraus
bd5f350728 add tests for babel-external-helpers 2015-03-07 17:46:40 +01:00
Sebastian McKenzie
958fa282f4 4.7.2 2015-03-08 03:12:09 +11:00
Sebastian McKenzie
31e6b18346 v4.7.2 2015-03-08 03:09:43 +11:00
Sebastian McKenzie
d17ac92a3f fix changelog version 2015-03-08 03:06:46 +11:00
Sebastian McKenzie
188bcb70f7 add 4.7.1 changelog 2015-03-08 03:03:17 +11:00
Sebastian McKenzie
b8bd11a0e7 fix incorrect builder reference 2015-03-08 03:02:19 +11:00
Sebastian McKenzie
45bc74efe7 use a lookup table instead of an if - #945 2015-03-08 02:57:37 +11:00
Sebastian McKenzie
7eb169a894 Merge pull request #945 from neVERberleRfellerER/helper-globals-fix
Add choice of output format to babel-external-helpers
2015-03-08 02:56:30 +11:00
Sebastian McKenzie
eed4f312d8 add sourceMap both option - closes #966 2015-03-08 02:54:23 +11:00
Sebastian McKenzie
44e4dc970f more es6 2015-03-08 02:52:23 +11:00
Sebastian McKenzie
5d32432e67 split up export variable declarations - fixes #939, fixes #964 2015-03-08 02:52:10 +11:00
Sebastian McKenzie
9c9af6dbbd add utility.removeClass tests and move parentPath setting to setContext - fixes #967 2015-03-08 02:49:58 +11:00
Sebastian McKenzie
70cd650e10 remove dead t.isFalsyExpression and ignore private properties in t.cloneDeep - fixes #962 2015-03-08 02:45:14 +11:00
Sebastian McKenzie
c7bb00d58d ignore case breaks - fixes #963 2015-03-07 17:41:56 +11:00
Ondrej Kraus
623be068c4 add unsupportedOutputType to messages.js 2015-03-07 01:53:34 +01:00
Ondrej Kraus
71f5c9791d replace switch with if-else chain 2015-03-07 01:50:50 +01:00
Ondrej Kraus
3e6e86d073 order template keys by length 2015-03-07 01:50:49 +01:00
Ondrej Kraus
a0fb398ca2 add possibility to select format of external helpers 2015-03-07 01:50:49 +01:00
Ondrej Kraus
71b9f19e6a change to normal UMD (fixes bug with leaking variable in AMD mode) 2015-03-07 01:50:48 +01:00
Ondrej Kraus
c45ce58f0c stop assigning to global in generated helpers code 2015-03-07 01:50:48 +01:00
Sebastian McKenzie
d32f587e3c expose dynamicImports when es6.modules is blacklisted babel/ember-cli-babel#24 2015-03-07 03:31:24 +11:00
Sebastian McKenzie
9e1874ba89 4.7.1 2015-03-07 03:31:05 +11:00
Sebastian McKenzie
35fdc5c5d8 v4.7.1 2015-03-07 02:52:17 +11:00
Sebastian McKenzie
a4035fc257 use convert-source-map package - closes #960 2015-03-07 02:46:15 +11:00
Sebastian McKenzie
42e3dc7a70 inherit all options from input source map 2015-03-07 02:28:33 +11:00
Sebastian McKenzie
cdccf24515 4.7.0 2015-03-07 02:04:58 +11:00
Sebastian McKenzie
c8cd5c108b v4.7.0 2015-03-07 01:59:02 +11:00
Sebastian McKenzie
2b95b876e1 return only constructor if class was a named class with only a constructor/no constructor 2015-03-07 01:50:34 +11:00
Sebastian McKenzie
f801772fc2 add inputSourceMap option - fixes #827, related babel/babel-loader#35 2015-03-07 01:50:07 +11:00
Sebastian McKenzie
8ad678e5bc make it illegal to have a rest on a setter 2015-03-07 01:32:03 +11:00
Sebastian McKenzie
35c49dbef7 merge validation transformers 2015-03-07 01:25:44 +11:00
Sebastian McKenzie
119314df74 update traceur 2015-03-07 01:25:37 +11:00
Sebastian McKenzie
51e336b037 class declarations also have a lexical self binding 2015-03-07 01:25:18 +11:00
Sebastian McKenzie
7d446807a9 remove rogue console.log 2015-03-06 23:40:30 +11:00
Sebastian McKenzie
8afdeaf557 replicate module environment in babel-node -e - closes #695, fixes #592 2015-03-06 23:39:49 +11:00
Sebastian McKenzie
4df50954a2 use instance indexOf instead of lodash - #951 2015-03-06 23:31:11 +11:00
Sebastian McKenzie
70b6317865 Merge pull request #951 from neVERberleRfellerER/babel-node-args-fix
add possiblity of passing colliding user arguments by separating them with --
2015-03-06 23:30:31 +11:00
Sebastian McKenzie
5ebdc44297 remove babel-runtime version check to node api 2015-03-06 23:20:48 +11:00
Sebastian McKenzie
a4659fd239 add reference checks for module specifiers - fixes #956 2015-03-06 23:19:26 +11:00
Sebastian McKenzie
19bfa4a35b add debug message for travis... 2015-03-06 23:15:20 +11:00
Sebastian McKenzie
f7fff7d35e add alternate to list of STATEMENT_OR_BLOCK_KEYS - fixes #955 2015-03-06 23:13:03 +11:00
Sebastian McKenzie
130e0ebe6b better classes, more spec compliant and nicer output - fixes #952 2015-03-06 23:08:10 +11:00
Sebastian McKenzie
69c836fc8d Merge branch 'master' of github.com:babel/babel 2015-03-06 02:27:15 +11:00
Sebastian McKenzie
f62a3ef394 further develop ast paths that represent a single location in the ast as an abstraction around a node-parent relationship 2015-03-06 02:26:04 +11:00
Sebastian McKenzie
7a6e568940 clean up classes output 2015-03-06 02:25:24 +11:00
Sebastian McKenzie
65998c3437 add error message for incompatible babel-runtime versions 2015-03-06 02:23:30 +11:00
Ondrej Kraus
160de340b0 add possiblity of passing user arguments by separating them with --
Now, when user arguments have names colliding with node arguments,
they can be separated by -- and will be parsed correctly.
2015-03-05 14:35:53 +01:00
Sebastian McKenzie
76ca40f698 Merge pull request #944 from sindresorhus/modularize-tofastproperties
modularize `util.toFastProperties()`
2015-03-05 01:10:26 +11:00
Sindre Sorhus
01ed824b5c modularize util.toFastProperties()
https://github.com/sindresorhus/to-fast-properties
2015-03-04 21:02:36 +08:00
Sebastian McKenzie
6b0dbc4486 bump regenerator-babel 2015-03-04 23:29:37 +11:00
Sebastian McKenzie
2648268f30 add contextVariables to scope 2015-03-04 22:54:00 +11:00
Sebastian McKenzie
8cf5bf7037 add filename arg to resolveModuleSource option 2015-03-04 22:53:50 +11:00
Sebastian McKenzie
60961bc3ff bump ast-types - fixes #930 2015-03-04 22:53:38 +11:00
Sebastian McKenzie
df16bc17e7 clean up formatting of system module formatter 2015-03-04 22:52:44 +11:00
Sebastian McKenzie
5337ab5a08 statically bind super references in object expressions - fixes #943 2015-03-04 22:52:07 +11:00
Sebastian McKenzie
2eaeebcce4 inherit from class constructor block statement - fixes #937 2015-03-03 23:38:11 +11:00
Sebastian McKenzie
ad71010144 4.6.6 2015-03-03 22:44:44 +11:00
Sebastian McKenzie
864169c1eb v4.6.6 2015-03-03 22:43:06 +11:00
Sebastian McKenzie
930d7f9aa6 add 4.6.6 changelog 2015-03-03 22:38:20 +11:00
Sebastian McKenzie
ed40ec03d1 more es6, template literals and modules 2015-03-03 22:31:49 +11:00
Sebastian McKenzie
73062ae240 take into consideration non-identifier nodes in es6.blockScopingTDZ transformer, also turn default and parameters into lets instead of vars - fixes #929 2015-03-03 22:30:52 +11:00
Sebastian McKenzie
bafa3e0d4c add missing is - fixes #931 - thanks @neVERberleRfellerER 2015-03-03 10:38:18 +11:00
Sebastian McKenzie
f3d2b2bb81 Merge branch 'master' of github.com:babel/babel 2015-03-03 09:49:35 +11:00
Sebastian McKenzie
a74b67d4eb Merge pull request #932 from hzoo/more-es6
More es6
2015-03-03 09:49:29 +11:00
Sebastian McKenzie
625416862f remove no es6 syntax line from contributing.md 2015-03-03 09:49:20 +11:00
Sebastian McKenzie
8dda8838ba check for body existence before length in dead code elimination transformer - fixes #931 2015-03-03 09:37:52 +11:00
Henry Zhu
96d3a25213 use more es6 concise methods 2015-03-02 10:34:30 -05:00
Sebastian McKenzie
727208f376 4.6.5 2015-03-02 22:55:36 +11:00
Sebastian McKenzie
1b046a6ecb v4.6.5 2015-03-02 22:54:35 +11:00
Sebastian McKenzie
3b8ed0d401 add 4.6.5 changelog 2015-03-02 22:51:21 +11:00
Sebastian McKenzie
d4c98d7738 add transformer aliases 2015-03-02 22:47:00 +11:00
Sebastian McKenzie
4e44af819f fix rogue strict reference 2015-03-02 22:45:21 +11:00
Sebastian McKenzie
a65acd73ca fix rogue useStrict transformer references 2015-03-02 22:41:55 +11:00
Sebastian McKenzie
b7cb2bcb7b rename useStrict transformer to strict 2015-03-02 22:37:51 +11:00
Sebastian McKenzie
5ac4dc0541 don't evaluate ArrayExpressions 2015-03-02 17:16:10 +11:00
Sebastian McKenzie
99ddd02b0a change Function export to avoid messing with istanbul 2015-03-02 15:38:43 +11:00
Sebastian McKenzie
19cfee14fd 4.6.4 2015-03-02 15:38:31 +11:00
Sebastian McKenzie
fea487bf91 v4.6.4 2015-03-02 15:34:10 +11:00
Sebastian McKenzie
ff2cbd5a2e Merge branch 'master' of github.com:babel/babel 2015-03-02 15:30:30 +11:00
Sebastian McKenzie
12e01f9e71 remove optional property on function params - fixes #927 2015-03-02 15:30:05 +11:00
Sebastian McKenzie
f62436909d Merge pull request #923 from mathiasbynens/patch-4
Explicitly update the regexpu dependency to v1.1.2
2015-03-02 11:32:27 +11:00
Mathias Bynens
e42c1adeb6 Explicitly update the regexpu dependency to v1.1.2 2015-03-01 18:50:17 +01:00
Sebastian McKenzie
e30dbbab94 properly replace labels in spec for of transformer - fixes #913, closes #914 2015-03-02 01:41:13 +11:00
Sebastian McKenzie
e3daa28e60 rename minification tests to utility 2015-03-02 01:28:34 +11:00
Sebastian McKenzie
9e5d94126c add base type tests 2015-03-02 01:28:17 +11:00
Sebastian McKenzie
95798bee0b add t.valueToNode 2015-03-02 01:28:10 +11:00
Sebastian McKenzie
63f25ab038 check parent for variable collisions when remapping closurified block scopes - fixes #915, closes #922 2015-03-02 01:27:58 +11:00
Sebastian McKenzie
27a8f2d2ea rename broke property to confident in t.evaluate 2015-03-01 18:16:27 +11:00
Sebastian McKenzie
78434bb404 add t.evaluate method 2015-03-01 17:01:46 +11:00
Sebastian McKenzie
82833a8901 update 4.6.3 changelog 2015-03-01 13:45:09 +11:00
Sebastian McKenzie
93d5288d71 4.6.3 2015-03-01 11:05:33 +11:00
Sebastian McKenzie
5d3074b460 v4.6.3 2015-03-01 11:03:55 +11:00
Sebastian McKenzie
a57475abc9 better rest param member expression deopt 2015-03-01 11:01:06 +11:00
Sebastian McKenzie
09e68d8d46 v4.6.2 2015-03-01 10:12:43 +11:00
Sebastian McKenzie
ee850c3aeb merge fix for #917 with #918 2015-03-01 10:09:03 +11:00
Sebastian McKenzie
ff5a149cec Merge branch 'master' of github.com:babel/babel
# Conflicts:
#	src/babel/transformation/transformers/es6/parameters.rest.js
2015-03-01 10:04:52 +11:00
Sebastian McKenzie
957118fb41 delay rest optimisation, deopt on unoptimisable references - fixes #918 2015-03-01 10:04:16 +11:00
Sebastian McKenzie
e7ad0a9741 Merge pull request #919 from neVERberleRfellerER/rest-args-optimization-fix
fix rest optimization in arrow functions
2015-03-01 09:38:00 +11:00
Ondrej Kraus
24f4b041c7 fix rest optimization in arrow functions and add advanced test 2015-02-28 23:28:21 +01:00
Sebastian McKenzie
4c77d04b56 more es6 modules 2015-03-01 01:01:51 +11:00
Sebastian McKenzie
e945f0d10f add support for outputting flow types - fixes #665 2015-03-01 00:32:36 +11:00
Sebastian McKenzie
fb04b2561f copy over files in watch mode 2015-02-28 21:39:40 +11:00
Sebastian McKenzie
a4f8b41507 4.6.1 2015-02-28 11:56:46 +11:00
Sebastian McKenzie
1fb6018e9c v4.6.1 2015-02-28 11:55:47 +11:00
Sebastian McKenzie
bca233d22c add 4.6.1 changelog 2015-02-28 11:52:21 +11:00
Sebastian McKenzie
c78703e194 blacklist regenerator from templates directory - fixes #912 2015-02-28 11:47:18 +11:00
Sebastian McKenzie
43fdbe3d6c fix whitespace 2015-02-28 11:31:43 +11:00
Sebastian McKenzie
8650ca7d55 properly expose util - fixes #52 2015-02-28 11:24:51 +11:00
Sebastian McKenzie
3a0c226a34 upgrade babel 2015-02-28 00:31:35 +11:00
Sebastian McKenzie
73e8bdd048 update 4.6.0 changelog 2015-02-28 00:31:28 +11:00
Sebastian McKenzie
e7c52a734e 4.6.0 2015-02-27 21:14:20 +11:00
Sebastian McKenzie
5a81b02569 v4.6.0 2015-02-27 21:09:17 +11:00
Sebastian McKenzie
999baf2888 fix regex tests 2015-02-27 21:01:13 +11:00
Sebastian McKenzie
70b25e8942 add sticky regex desugaring #904 2015-02-27 20:51:48 +11:00
Sebastian McKenzie
2ddbd4eecc fix es for of break test expected 2015-02-27 20:33:42 +11:00
Sebastian McKenzie
7520807df3 move eslint out of the core into a separate plugin 2015-02-27 16:30:53 +11:00
Sebastian McKenzie
974b71bcc3 flesh out eslint support 2015-02-27 16:11:47 +11:00
Sebastian McKenzie
0aee3c06ec add noCheckAst option to enforce newline test 2015-02-27 15:45:09 +11:00
Sebastian McKenzie
a4382580fc ignore user whitespace when splitting up module declaration - fixes #906 2015-02-27 15:25:13 +11:00
Sebastian McKenzie
166b2eda87 remove rogue export in node api 2015-02-27 15:08:01 +11:00
Sebastian McKenzie
83c23d266f add eslint with an acorn-babel compatibility layer, yay! coming soon to a production release near you 2015-02-27 13:17:22 +11:00
Sebastian McKenzie
a9db70b60d close iterators on abrupt completion - google/traceur-compiler#1773 #838 2015-02-27 11:44:13 +11:00
Sebastian McKenzie
5d90c442cb fix es6 rest parameters transformer comment 2015-02-26 23:25:48 +11:00
Sebastian McKenzie
32606ddb93 remove linting mention from contributing guide 2015-02-26 22:34:54 +11:00
Sebastian McKenzie
330f6910c6 remove unnecessary exception catch and process.exit - fixes #901 2015-02-26 22:33:36 +11:00
Sebastian McKenzie
4ca8a0e6e2 bump browserify 2015-02-26 21:41:15 +11:00
Sebastian McKenzie
f097ddeac3 bump source-map dependency 2015-02-26 21:39:54 +11:00
Sebastian McKenzie
1eb53dd13a add try-catch to bin watching compilation - fixes #901 2015-02-26 21:28:54 +11:00
Sebastian McKenzie
75b5f32e7a make js-tokens version fixed - explanation in #900 2015-02-26 21:15:54 +11:00
Sebastian McKenzie
f753cf4845 Merge pull request #900 from lydell/js-tokens-1.0
Upgrade to js-tokens@1.0.0
2015-02-26 21:13:55 +11:00
Simon Lydell
9a569f64da Upgrade to js-tokens@1.0.0
This commit also adds syntax highlighting for `null`, `false` and `true`.
2015-02-26 11:11:23 +01:00
Sebastian McKenzie
8efeae80af remove export function Function 2015-02-26 20:39:30 +11:00
Sebastian McKenzie
4df9cf6c05 add missing transform import and change babel import to a wildcard 2015-02-26 20:26:08 +11:00
Sebastian McKenzie
7729cb4b68 start using es6 modules 2015-02-26 20:13:00 +11:00
Sebastian McKenzie
307ffcd107 correctly get old extension handler in registerExtension 2015-02-26 16:00:06 +11:00
Sebastian McKenzie
751557aef1 make #889 more efficient and flexible 2015-02-26 15:24:12 +11:00
Sebastian McKenzie
3f146b54ff Merge branch 'master' of github.com:6to5/6to5 2015-02-26 15:04:21 +11:00
Sebastian McKenzie
19a173c622 Merge pull request #899 from xjamundx/patch-1
feat(6to5/register) don't override uncaughtException handler
2015-02-26 15:04:02 +11:00
Sebastian McKenzie
89bcb307a2 Merge pull request #889 from neVERberleRfellerER/rest-args-optimization-v2
Add rest parameters optimization
2015-02-26 15:03:51 +11:00
Sebastian McKenzie
ded1e02da7 move buildHelpers require down a slot 2015-02-26 13:06:15 +11:00
Sebastian McKenzie
564ba67190 4.5.5 2015-02-26 13:06:08 +11:00
Ondrej Kraus
b5f3c3f4cc stop traversal when it is clear that optimization is impossible 2015-02-26 02:05:26 +01:00
Ondrej Kraus
5d83638583 set literal MemberExpression as computed in rest array destructuring 2015-02-26 01:22:00 +01:00
Jamund Ferguson
24ace3c8c2 feat(6to5/register) don't override uncaughtException handler
Maybe we could do this by default or even make it configurable?
2015-02-25 13:51:26 -08:00
Ondrej Kraus
687b0f3180 remove unused function argument 2015-02-25 15:38:32 +01:00
Ondrej Kraus
e6855b974b update tests 2015-02-25 15:30:22 +01:00
Ondrej Kraus
a808742c19 remove strict-mode specific optimizations due to unsolvable ambiguities 2015-02-25 15:24:45 +01:00
Ondrej Kraus
c3f4091b6d replace for-of with with for for performance reasons 2015-02-25 14:41:20 +01:00
Ondrej Kraus
edb880f87c add tests 2015-02-25 14:26:28 +01:00
Ondrej Kraus
e8741daee3 use some ES6 to simplify code 2015-02-25 14:13:15 +01:00
Ondrej Kraus
c5913564f8 replace quotes to conform to coding style 2015-02-25 13:34:18 +01:00
Ondrej Kraus
662bddbaca simplify optimization code 2015-02-25 13:34:17 +01:00
Ondrej Kraus
8f540dfff3 call non-strict mode optimizer before strict mode one to simplify literals 2015-02-25 13:34:17 +01:00
Ondrej Kraus
1265bc5a92 add better optimization when strictMode transformer is enabled 2015-02-25 13:34:15 +01:00
Ondrej Kraus
e677c72d58 add patterns support 2015-02-25 13:34:15 +01:00
Ondrej Kraus
8ca854156a simplify and optimize local binding handling 2015-02-25 13:34:14 +01:00
Ondrej Kraus
81ae656358 remove unused function 2015-02-25 13:34:13 +01:00
Ondrej Kraus
f2981b7e95 replace direct node type checking with helper functions 2015-02-25 13:34:12 +01:00
Ondrej Kraus
99b2e00d33 add rest parameters optimization 2015-02-25 13:34:11 +01:00
1648 changed files with 75834 additions and 6691 deletions

View File

@@ -4,7 +4,6 @@ root = true
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
end_of_line = lf
[*.{js,json}]

1
.eslintignore Normal file
View File

@@ -0,0 +1 @@
src/babel/transformation/templates

22
.eslintrc Normal file
View File

@@ -0,0 +1,22 @@
{
"parser": "babel-eslint",
"rules": {
"strict": 0,
"no-underscore-dangle": 0,
"no-unused-vars": 0,
"curly": 0,
"no-multi-spaces": 0,
"key-spacing": 0,
"no-return-assign": 0,
"consistent-return": 0,
"no-shadow": 0,
"no-comma-dangle": 0,
"no-use-before-define": 0,
"no-empty": 0,
"new-parens": 0,
"no-cond-assign": 0
},
"env": {
"node": true
}
}

3
.gitignore vendored
View File

@@ -1,6 +1,6 @@
.DS_Store
node_modules
test/tmp
test/core/tmp
*.log
*.cache
/templates.json
@@ -12,5 +12,6 @@ coverage
dist
.package.json
packages/babel-runtime/*.js
packages/babel-runtime/helpers/*.js
packages/babel-runtime/regenerator/*.js
lib

View File

@@ -1,7 +1,6 @@
sudo: false
language: node_js
node_js:
- "0.10"
- "0.12"
- "iojs"

View File

@@ -13,6 +13,171 @@ _Note: Gaps between patch versions are faulty/broken releases._
See [CHANGELOG - 6to5](CHANGELOG-6to5.md) for the pre-4.0.0 version changelog.
## 4.7.16
* **Bug Fix**
* Fix constructor spreading of typed arrays.
* Fix break/continue/return aliasing of non-loops in block scoping transformer.
## 4.7.15
* **Bug Fix**
* Fix constructor spreading of collections.
## 4.7.14
* **Bug Fix**
* Fix constructor spreading of `Promise`.
* **Internal**
* Deprecate remaining playground transformers and abstract references.
## 4.7.13
* **Bug Fix**
* Handle comments on use strict directives.
* Fix assignment patterns with a left side pattern.
* **Polish**
* Special case `this` when doing expression memoisation.
## 4.7.12
* **Bug Fix**
* Deprecate `playground.methodBinding`.
## 4.7.11
* **Bug Fix**
* Fix unicode regexes stripping their unicode flag before being passed on two `regexpu`.
## 4.7.10
* **Internal**
* Deprecate `playground.methodBinding` and `playground.objectGetterMemoization`.
* **Bug Fix**
* Fix `inputSourceMap` option. Thanks [@Rich-Harris](https://github.com/Rich-Harris)!
## 4.7.9
* **Polish**
* Allow `inputSourceMap` to be set to `false` to skip the source map inference.
* Infer computed literal property names.
* **Bug Fix**
* Fix nested labeled for-ofs.
* Fix block scoping `break` colliding with the parent switch case.
* **Internal**
* Upgrade `acorn-babel`.
## 4.7.8
* **Bug Fix**
* Fix computed classes not properly setting symbols.
## 4.7.7
* **Bug Fix**
* Fix `types` API exposure.
## 4.7.6
* **Bug Fix**
* Fix non-Identifier/Literal computed class methods.
* **Polish**
* Add a fallback if `stack` on an error is unconfigurable.
* Hoist `esModule` module declarations to the top of the file to handle circular dependencies better.
## 4.7.5
* **Bug Fix**
* Don't remap` break`s to call the iterator return.
* **Polish**
* Use a different helper for computed classes for much nicer output. Also fixes a bug in symbols being non-enumerable so they wouldn't be set on the class.
## 4.7.4
* **Bug Fix**
* Rewrite named function expressions in optional async function transformers.
* Hoist directives.
* Remove `Number` from the list of valid `runtime` constructors.
* **Internal**
* `spec.typeofSymbol` transformer has been renamed to `es6.symbols`.
## 4.7.2
* **New Feature**
* `"both"` option for `sourceMap`.
* Add output types to external helpers. Thanks [@neVERberleRfellerER](https://github.com/neVERberleRfellerER)!
* **Bug Fix**
* Fix node duplication sometimes resulting in a recursion error.
* Ignore `break`s within cases inside `for...of`.
* **Polish**
* Split up variable declarations and export declarations to allow easier transformation.
## 4.7.0
* **Bug Fix**
* Add `alternate` to list of `STATEMENT_OR_BLOCK` keys.
* Add support for module specifiers to `t.isReferenced`.
* **New Feature**
* Add `inputSourceMap` option.
* **Polish**
* Throw an error on different `babel` and `babel-runtime` versions.
* Replicate module environment for `babel-node` eval.
* Clean up classes output.
* **Spec Compliancy**
* Make it illegal to use a rest parameter on a setter.
## 4.6.6
* **Bug Fix**
* Fix incorrect method call in `utility.deadCodeElimination` transformer.
* Fix `es6.blockScopingTDZ` transformer duplicating binding nodes.
## 4.6.5
* **Internal**
* `useStrict` transformer has been renamed to `strict`.
## 4.6.4
* **Bug Fix**
* Fix `ForOfStatement` not proplery inheriting labels.
* When in closure mode in block scoping transformer, properly check for variable shadowing.
* **New Feature**
* New `utility.inlineEnvironmentVariables` and `utility.inlineExpression` transformers.
## 4.6.3
* **Bug Fix**
* Fix `arguments` being incorrectly aliased in arrow function rest parameter optimisation.
* Make deoptimisation trigger safer.
* **New Feature**
* Flow types are now retained when blacklisting the `flow` transformer.
## 4.6.1
* **Bug Fix**
* Fix generators in template directory being transformed.
* Fix exposure of `util` for plugins.
## 4.6.0
* **New Feature**
* Desugar sticky regexes to a new constructor expression so it can be handled by a polyfill.
* **Spec Compliancy**
* `for...of` now outputs in a lengthy `try...catch` this is to ensure spec compliancy in regards to iterator returns and abrupt completions. See [google/traceur-compiler#1773](https://github.com/google/traceur-compiler/issues/1773) and [babel/babel/#838](https://github.com/babel/babel/issues/838) for more information.
* **Polish**
* Rest parameters that are only refered to via number properties on member expressions are desugared into a direct `arguments` reference. Thanks [@neVERberleRfellerER](https://github.com/neVERberleRfellerER)!
* `$ babel` no longer exits on syntax errors.
* **Internal**
* Upgrade `browserify`.
* Upgrade `source-map`.
* Publicly expose more internals.
## 4.5.5
* **Polish**
* Delete old extensions when overriding them in `babel/register`.
## 4.5.3
* **Bug Fix**

View File

@@ -4,10 +4,6 @@ Contributions are always welcome, no matter how large or small. Before
contributing, please read the
[code of conduct](https://github.com/babel/babel/blob/master/CODE_OF_CONDUCT.md).
**NOTE:** Please do not send pull requests that fix linting issues. It's highly
likely that they've already been fixed by the time it's submitted and it just
pollutes the git tree.
## Developing
#### Setup
@@ -36,7 +32,7 @@ $ make test
```
This will usually take around two minutes as it's compiling the entire
[test262](https://github.com/tc39/test262) test suite and validating it's AST.
[test262](https://github.com/tc39/test262) test suite and validating its AST.
This is mostly overkill and you can limit the tests to a select few by directly
running them with `mocha`:
@@ -44,13 +40,10 @@ running them with `mocha`:
$ mocha test/transformation.js
```
#### Linting
Please follow the correct code style, this ensures that the code is consistent
and increases maintainability.
Use mocha's `--grep` option to run a subset of tests by name:
```sh
$ make lint
$ mocha test/transformation.js --grep es7
```
#### Workflow
@@ -66,7 +59,6 @@ $ make lint
#### Code Standards
* **General**
* No ES6 syntax features or methods, exclusively ES5.
* Max of five arguments for functions
* Max depth of four nested blocks
* 2-spaced soft tabs

View File

@@ -8,11 +8,14 @@ BABEL_CMD = node_modules/babel/bin/babel
export NODE_ENV = test
.PHONY: clean test test-cov test-clean test-travis test-simple test-all test-browser publish build bootstrap publish-core publish-runtime build-core watch-core
.PHONY: clean test test-cov test-clean test-travis test-simple test-all test-browser test-parser publish build bootstrap publish-core publish-runtime build-core watch-core build-core-test
build-core:
node $(BABEL_CMD) src --out-dir lib --copy-files
build-core-test:
node $(BABEL_CMD) src --out-dir lib --copy-files --auxiliary-comment "istanbul ignore next"
watch-core:
node $(BABEL_CMD) src --out-dir lib --watch --copy-files
@@ -39,34 +42,30 @@ clean:
test-clean:
rm -rf test/tmp
test:
$(MOCHA_CMD)
test: test-parser
node $(MOCHA_CMD) test/core
make test-clean
test-simple:
# excludes test262
export SIMPLE_BABEL_TESTS=1; \
make test
test-all:
export ALL_BABEL_TESTS=1; \
make test
test-cov:
rm -rf coverage
export SIMPLE_BABEL_TESTS=1; \
node $(ISTANBUL_CMD) $(MOCHA_CMD) --
make build-core-test
node $(ISTANBUL_CMD) $(MOCHA_CMD) -- test/core
test-travis: bootstrap build
node $(ISTANBUL_CMD) $(MOCHA_CMD) --
if test -n "$$CODECLIMATE_REPO_TOKEN"; then codeclimate < coverage/lcov.info; fi
test-parser:
node test/acorn/run.js
test-travis: bootstrap build test
test-browser:
mkdir -p dist
node tools/cache-templates
node tools/cache-tests
node $(BROWSERIFY_CMD) -e test/_browser.js >dist/babel-test.js
node tools/build-tests
node $(BROWSERIFY_CMD) -e test/core/_browser.js >dist/babel-test.js
rm -rf templates.json tests.json
test -n "`which open`" && open test/browser.html
@@ -87,7 +86,7 @@ publish:
node tools/cache-templates
test -f templates.json
npm publish
npm publish --tag beta
git push --follow-tags
@@ -100,14 +99,14 @@ publish-runtime:
cd packages; \
node build-runtime.js; \
cd babel-runtime; \
npm publish
npm publish --tag beta
publish-core:
tools/generate-core-package-json >package2.json
mv package.json .package.json
mv package2.json package.json
npm publish
npm publish --tag beta
rm -rf package.json
mv .package.json package.json

View File

@@ -17,7 +17,6 @@ program.option("-p, --print [code]", "Evaluate script and print result");
program.option("-i, --ignore [regex]", "Ignore all files that match this regex when using the require hook");
program.option("-x, --extensions [extensions]", "List of extensions to hook into [.es6,.js,.es,.jsx]");
program.option("-r, --experimental", "Enable experimental support for proposed ES7 features");
program.option("-g, --playground", "Enable playground support");
program.option("-w, --whitelist [whitelist]", "Whitelist of transformers to ONLY use", util.list);
program.option("-b, --blacklist [blacklist]", "Blacklist of transformers to NOT use", util.list);
program.option("-o, --optional [optional]", "List of optional transformers to enable", util.list);
@@ -32,7 +31,6 @@ program.parse(process.argv);
babel.register({
experimental: program.experimental,
extensions: program.extensions,
playground: program.playground,
blacklist: program.blacklist,
whitelist: program.whitelist,
optional: program.optional,
@@ -42,13 +40,14 @@ babel.register({
//
var _eval = function (code, filename) {
if (!code) return undefined;
code = babel.transform(code, {
filename: filename,
blacklist: ["useStrict"].concat(program.blacklist || []),
blacklist: program.blacklist,
whitelist: program.whitelist,
optional: program.optional,
experimental: program.experimental,
playground: program.playground
experimental: program.experimental
}).code;
return vm.runInThisContext(code, {
@@ -60,7 +59,18 @@ if (program.eval || program.print) {
var code = program.eval;
if (!code || code === true) code = program.print;
var result = _eval(code, "eval");
global.__filename = "[eval]";
global.__dirname = process.cwd();
var module = new Module(global.__filename);
module.filename = global.__filename;
module.paths = Module._nodeModulePaths(global.__dirname);
global.exports = module.exports;
global.module = module;
global.require = module.require.bind(module);
var result = _eval(code, global.__filename);
if (program.print) console.log(result);
} else {
if (program.args.length) {
@@ -88,7 +98,7 @@ if (program.eval || program.print) {
args = args.slice(i);
// make the filename absolute
var filename = args[0]
var filename = args[0];
if (!pathIsAbsolute(filename)) args[0] = path.join(process.cwd(), filename);
// add back on node and concat the sliced args

View File

@@ -1,4 +1,13 @@
#!/usr/bin/env node
var runtime = require("../lib/babel/build-external-helpers");
console.log(runtime());
var commander = require("commander");
var util = require("../lib/babel/util");
var runtime = require("../lib/babel/tools/build-external-helpers");
commander.option("-l, --whitelist [whitelist]", "Whitelist of helpers to ONLY include", util.list);
commander.option("-t, --output-type [type]", "Type of output (global|umd|var)", "global");
commander.usage("[options]");
commander.parse(process.argv);
console.log(runtime(commander.whitelist, commander.outputType));

View File

@@ -7,7 +7,17 @@
var args = [__dirname + "/_babel-node"];
process.argv.slice(2).forEach(function(arg){
var babelArgs = process.argv.slice(2);
var userArgs;
// separate node arguments from script arguments
var argSeparator = babelArgs.indexOf("--");
if (argSeparator > -1) {
userArgs = babelArgs.slice(argSeparator); // including the --
babelArgs = babelArgs.slice(0, argSeparator);
}
babelArgs.forEach(function(arg){
var flag = arg.split("=")[0];
switch (flag) {
@@ -26,16 +36,380 @@ process.argv.slice(2).forEach(function(arg){
args.unshift("--expose-gc");
break;
case "--gc-global":
case "--use_strict":
case "--es_staging":
case "--harmony":
case "--harmony-proxies":
case "--harmony-collections":
case "--harmony-generators":
case "--no-deprecation":
case "--harmony_shipping":
case "--harmony_modules":
case "--harmony_arrays":
case "--harmony_array_includes":
case "--harmony_regexps":
case "--harmony_arrow_functions":
case "--harmony_proxies":
case "--harmony_sloppy":
case "--harmony_unicode":
case "--harmony_tostring":
case "--harmony_numeric_literals":
case "--harmony_strings":
case "--harmony_scoping":
case "--harmony_classes":
case "--harmony_object_literals":
case "--harmony_templates":
case "--compiled_keyed_generic_loads":
case "--pretenuring_call_new":
case "--allocation_site_pretenuring":
case "--trace_pretenuring":
case "--trace_pretenuring_statistics":
case "--track_fields":
case "--track_double_fields":
case "--track_heap_object_fields":
case "--track_computed_fields":
case "--track_field_types":
case "--smi_binop":
case "--vector_ics":
case "--optimize_for_size":
case "--unbox_double_arrays":
case "--string_slices":
case "--crankshaft":
case "--hydrogen_filter":
case "--use_gvn":
case "--gvn_iterations":
case "--use_canonicalizing":
case "--use_inlining":
case "--use_escape_analysis":
case "--use_allocation_folding":
case "--use_local_allocation_folding":
case "--use_write_barrier_elimination":
case "--max_inlining_levels":
case "--max_inlined_source_size":
case "--max_inlined_nodes":
case "--max_inlined_nodes_cumulative":
case "--loop_invariant_code_motion":
case "--fast_math":
case "--collect_megamorphic_maps_from_stub_cache":
case "--hydrogen_stats":
case "--trace_check_elimination":
case "--trace_hydrogen":
case "--trace_hydrogen_filter":
case "--trace_hydrogen_stubs":
case "--trace_hydrogen_file":
case "--trace_phase":
case "--trace_inlining":
case "--trace_load_elimination":
case "--trace_store_elimination":
case "--trace_alloc":
case "--trace_all_uses":
case "--trace_range":
case "--trace_gvn":
case "--trace_representation":
case "--trace_removable_simulates":
case "--trace_escape_analysis":
case "--trace_allocation_folding":
case "--trace_track_allocation_sites":
case "--trace_migration":
case "--trace_generalization":
case "--stress_pointer_maps":
case "--stress_environments":
case "--deopt_every_n_times":
case "--deopt_every_n_garbage_collections":
case "--print_deopt_stress":
case "--trap_on_deopt":
case "--trap_on_stub_deopt":
case "--deoptimize_uncommon_cases":
case "--polymorphic_inlining":
case "--use_osr":
case "--array_bounds_checks_elimination":
case "--trace_bce":
case "--array_bounds_checks_hoisting":
case "--array_index_dehoisting":
case "--analyze_environment_liveness":
case "--load_elimination":
case "--check_elimination":
case "--store_elimination":
case "--dead_code_elimination":
case "--fold_constants":
case "--trace_dead_code_elimination":
case "--unreachable_code_elimination":
case "--trace_osr":
case "--stress_runs":
case "--lookup_sample_by_shared":
case "--cache_optimized_code":
case "--flush_optimized_code_cache":
case "--inline_construct":
case "--inline_arguments":
case "--inline_accessors":
case "--escape_analysis_iterations":
case "--optimize_for_in":
case "--concurrent_recompilation":
case "--job_based_recompilation":
case "--trace_concurrent_recompilation":
case "--concurrent_recompilation_queue_length":
case "--concurrent_recompilation_delay":
case "--block_concurrent_recompilation":
case "--concurrent_osr":
case "--omit_map_checks_for_leaf_maps":
case "--turbo_filter":
case "--trace_turbo":
case "--trace_turbo_graph":
case "--trace_turbo_cfg_file":
case "--trace_turbo_types":
case "--trace_turbo_scheduler":
case "--trace_turbo_reduction":
case "--trace_turbo_jt":
case "--turbo_asm":
case "--turbo_verify":
case "--turbo_stats":
case "--turbo_types":
case "--turbo_source_positions":
case "--context_specialization":
case "--turbo_deoptimization":
case "--turbo_inlining":
case "--turbo_inlining_intrinsics":
case "--trace_turbo_inlining":
case "--loop_assignment_analysis":
case "--turbo_profiling":
case "--turbo_reuse_spill_slots":
case "--turbo_delay_ssa_decon":
case "--turbo_move_optimization":
case "--turbo_jt":
case "--typed_array_max_size_in_heap":
case "--frame_count":
case "--interrupt_budget":
case "--type_info_threshold":
case "--generic_ic_threshold":
case "--self_opt_count":
case "--trace_opt_verbose":
case "--debug_code":
case "--code_comments":
case "--enable_sse3":
case "--enable_sse4_1":
case "--enable_sahf":
case "--enable_avx":
case "--enable_fma3":
case "--enable_vfp3":
case "--enable_armv7":
case "--enable_armv8":
case "--enable_neon":
case "--enable_sudiv":
case "--enable_mls":
case "--enable_movw_movt":
case "--enable_unaligned_accesses":
case "--enable_32dregs":
case "--enable_vldr_imm":
case "--force_long_branches":
case "--expose_natives_as":
case "--expose_debug_as":
case "--expose_free_buffer":
case "--expose_gc":
case "--expose_gc_as":
case "--expose_externalize_string":
case "--expose_trigger_failure":
case "--stack_trace_limit":
case "--builtins_in_stack_traces":
case "--disable_native_files":
case "--inline_new":
case "--trace_codegen":
case "--trace":
case "--mask_constants_with_cookie":
case "--lazy":
case "--trace_opt":
case "--trace_opt_stats":
case "--opt":
case "--always_opt":
case "--always_osr":
case "--prepare_always_opt":
case "--trace_deopt":
case "--trace_stub_failures":
case "--serialize_toplevel":
case "--serialize_inner":
case "--trace_serializer":
case "--min_preparse_length":
case "--max_opt_count":
case "--compilation_cache":
case "--cache_prototype_transitions":
case "--cpu_profiler_sampling_interval":
case "--trace_debug_json":
case "--trace_js_array_abuse":
case "--trace_external_array_abuse":
case "--trace_array_abuse":
case "--enable_liveedit":
case "--hard_abort":
case "--stack_size":
case "--max_stack_trace_source_length":
case "--always_inline_smi_code":
case "--min_semi_space_size":
case "--target_semi_space_size":
case "--max_semi_space_size":
case "--semi_space_growth_factor":
case "--experimental_new_space_growth_heuristic":
case "--max_old_space_size":
case "--initial_old_space_size":
case "--max_executable_size":
case "--gc_global":
case "--gc_interval":
case "--trace_gc":
case "--trace_gc_nvp":
case "--trace_gc_ignore_scavenger":
case "--trace_idle_notification":
case "--trace_idle_notification_verbose":
case "--print_cumulative_gc_stat":
case "--print_max_heap_committed":
case "--trace_gc_verbose":
case "--trace_fragmentation":
case "--collect_maps":
case "--weak_embedded_maps_in_optimized_code":
case "--weak_embedded_objects_in_optimized_code":
case "--flush_code":
case "--flush_code_incrementally":
case "--trace_code_flushing":
case "--age_code":
case "--incremental_marking":
case "--incremental_marking_steps":
case "--concurrent_sweeping":
case "--trace_incremental_marking":
case "--track_gc_object_stats":
case "--heap_profiler_trace_objects":
case "--use_idle_notification":
case "--use_ic":
case "--trace_ic":
case "--native_code_counters":
case "--always_compact":
case "--never_compact":
case "--compact_code_space":
case "--incremental_code_compaction":
case "--cleanup_code_caches_at_gc":
case "--use_marking_progress_bar":
case "--zap_code_space":
case "--random_seed":
case "--trace_weak_arrays":
case "--track_prototype_users":
case "--use_verbose_printer":
case "--allow_natives_syntax":
case "--trace_parse":
case "--trace_sim":
case "--debug_sim":
case "--check_icache":
case "--stop_sim_at":
case "--sim_stack_alignment":
case "--sim_stack_size":
case "--log_regs_modified":
case "--log_colour":
case "--ignore_asm_unimplemented_break":
case "--trace_sim_messages":
case "--stack_trace_on_illegal":
case "--abort_on_uncaught_exception":
case "--randomize_hashes":
case "--hash_seed":
case "--profile_deserialization":
case "--regexp_optimization":
case "--testing_bool_flag":
case "--testing_maybe_bool_flag":
case "--testing_int_flag":
case "--testing_float_flag":
case "--testing_string_flag":
case "--testing_prng_seed":
case "--testing_serialization_file":
case "--startup_blob":
case "--profile_hydrogen_code_stub_compilation":
case "--predictable":
case "--help":
case "--dump_counters":
case "--debugger":
case "--map_counters":
case "--js_arguments":
case "--gdbjit":
case "--gdbjit_full":
case "--gdbjit_dump":
case "--gdbjit_dump_filter":
case "--force_marking_deque_overflows":
case "--stress_compaction":
case "--log":
case "--log_all":
case "--log_api":
case "--log_code":
case "--log_gc":
case "--log_handles":
case "--log_snapshot_positions":
case "--log_suspect":
case "--prof":
case "--throw-deprecation":
case "--trace-deprecation":
case "--use-strict":
case "--prof_browser_mode":
case "--log_regexp":
case "--logfile":
case "--logfile_per_isolate":
case "--ll_prof":
case "--perf_basic_prof":
case "--perf_jit_prof":
case "--gc_fake_mmap":
case "--log_internal_timer_events":
case "--log_timer_events":
case "--log_instruction_stats":
case "--log_instruction_file":
case "--log_instruction_period":
case "--redirect_code_traces":
case "--redirect_code_traces_to":
case "--hydrogen_track_positions":
case "--trace_elements_transitions":
case "--trace_creation_allocation_sites":
case "--print_code_stubs":
case "--test_secondary_stub_cache":
case "--test_primary_stub_cache":
case "--print_code":
case "--print_opt_code":
case "--print_unopt_code":
case "--print_code_verbose":
case "--print_builtin_code":
case "--sodium":
case "--print_all_code":
case "--es5_readonly":
case "--es52_globals":
case "--harmony_typeof":
case "--harmony_collections":
case "--packed_arrays":
case "--smi_only_arrays":
case "--clever_optimizations":
case "--use_range":
case "--eliminate_dead_phis":
case "--optimize_closures":
case "--loop_weight":
case "--opt_safe_uint32_operations":
case "--parallel_recompilation":
case "--trace_parallel_recompilation":
case "--parallel_recompilation_queue_length":
case "--experimental_profiler":
case "--watch_ic_patching":
case "--self_optimization":
case "--direct_self_opt":
case "--retry_self_opt":
case "--count_based_interrupts":
case "--interrupt_at_exit":
case "--weighted_back_edges":
case "--debug_code (generate extra code":
case "--enable_sse2":
case "--enable_cmov":
case "--enable_rdtsc":
case "--enable_vfp2":
case "--enable_fpu":
case "--stack_trace_on_abort":
case "--always_full_compiler":
case "--debugger_auto_break":
case "--break_on_abort":
case "--max_new_space_size":
case "--trace_external_memory":
case "--lazy_sweeping":
case "--trace_exception":
case "--preallocate_message_memory":
case "--preemption":
case "--extra_code":
case "--remote_debugger":
case "--debugger_agent":
case "--debugger_port":
case "--debug_compile_events":
case "--debug_script_collected_events":
case "--gdbjit":
case "--log_runtime":
case "--prof_auto":
case "--prof_lazy":
case "--sliding_state_window":
args.unshift(arg);
break;
@@ -49,6 +423,11 @@ process.argv.slice(2).forEach(function(arg){
}
});
// append arguments passed after --
if (argSeparator > -1) {
args = args.concat(userArgs);
}
try {
var kexec = require("kexec");
kexec(process.argv[0], args);

View File

@@ -6,10 +6,6 @@ var fs = require("fs");
var _ = require("lodash");
module.exports = function (commander, filenames, opts) {
if (commander.sourceMapsInline) {
opts.sourceMap = "inline";
}
var write = function (src, relative) {
// remove extension and then append back on .js
relative = relative.replace(/\.(\w*?)$/, "") + ".js";
@@ -20,7 +16,7 @@ module.exports = function (commander, filenames, opts) {
sourceFileName: path.relative(dest + "/..", src)
});
if (commander.sourceMaps) {
if (commander.sourceMaps && commander.sourceMaps !== "inline") {
var mapLoc = dest + ".map";
data.code = util.addSourceMappingUrl(data.code, mapLoc);
outputFileSync(mapLoc, JSON.stringify(data.map));
@@ -31,6 +27,14 @@ module.exports = function (commander, filenames, opts) {
console.log(src + " -> " + dest);
};
var handleFile = function (src, filename) {
if (util.canCompile(filename)) {
write(src, filename);
} else if (commander.copyFiles) {
outputFileSync(path.join(commander.outDir, filename), fs.readFileSync(src));
}
};
var handle = function (filename) {
if (!fs.existsSync(filename)) return;
@@ -41,11 +45,7 @@ module.exports = function (commander, filenames, opts) {
_.each(util.readdir(dirname), function (filename) {
var src = path.join(dirname, filename);
if (util.canCompile(filename)) {
write(src, filename);
} else if (commander.copyFiles) {
outputFileSync(path.join(commander.outDir, filename), fs.readFileSync(src));
}
handleFile(src, filename);
});
} else {
write(filename, filename);
@@ -64,7 +64,11 @@ module.exports = function (commander, filenames, opts) {
_.each(["add", "change"], function (type) {
watcher.on(type, function (filename) {
var relative = path.relative(dirname, filename) || filename;
if (util.canCompile(filename)) write(filename, relative);
try {
handleFile(filename, relative);
} catch (err) {
console.error(err.stack);
}
});
});
});

View File

@@ -1,12 +1,16 @@
var sourceMapToComment = require("source-map-to-comment");
var sourceMap = require("source-map");
var chokidar = require("chokidar");
var path = require("path");
var util = require("./util");
var fs = require("fs");
var _ = require("lodash");
var convertSourceMap = require("convert-source-map");
var sourceMap = require("source-map");
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.sourceMaps === "inline") {
opts.sourceMaps = true;
}
module.exports = function (commander, filenames) {
var results = [];
var buildResult = function () {
@@ -41,8 +45,8 @@ module.exports = function (commander, filenames) {
}
});
if (commander.sourceMapsInline || (!commander.outFile && commander.sourceMaps)) {
code += "\n" + sourceMapToComment(map);
if (commander.sourceMaps === "inline" || (!commander.outFile && commander.sourceMaps)) {
code += "\n" + convertSourceMap.fromObject(map).toComment();
}
return {
@@ -55,7 +59,7 @@ module.exports = function (commander, filenames) {
var result = buildResult();
if (commander.outFile) {
if (commander.sourceMaps) {
if (commander.sourceMaps && commander.sourceMaps !== "inline") {
var mapLoc = commander.outFile + ".map";
result.code = util.addSourceMappingUrl(result.code, mapLoc);
fs.writeFileSync(mapLoc, JSON.stringify(result.map));
@@ -119,7 +123,11 @@ module.exports = function (commander, filenames) {
}).on("all", function (type, filename) {
if (type === "add" || type === "change") {
console.log(type, filename);
walk();
try {
walk();
} catch (err) {
console.error(err.stack);
}
}
});
}

View File

@@ -2,32 +2,38 @@
var commander = require("commander");
var transform = require("../../lib/babel/transformation");
var kebabCase = require("lodash/string/kebabCase");
var File = require("../../lib/babel/transformation/file");
var util = require("../../lib/babel/util");
var fs = require("fs");
var each = require("lodash/collection/each");
var keys = require("lodash/object/keys");
var fs = require("fs");
each(File.options, function (option, key) {
if (option.hidden) return;
var arg = kebabCase(key);
if (option.type !== "boolean") {
arg += " [" + (option.type || "string") + "]";
}
if (option.type === "boolean" && option.default === true) {
arg = "no-" + key;
}
arg = "--" + arg;
if (option.shorthand) {
arg = "-" + option.shorthand + ", " + arg;
}
commander.option(arg, option.description);
})
commander.option("-t, --source-maps-inline", "Append sourceMappingURL comment to bottom of code");
commander.option("-s, --source-maps", "Save source map alongside the compiled code");
commander.option("-f, --filename [filename]", "Filename to use when reading from stdin - this will be used in source-maps, errors etc [stdin]", "stdin");
commander.option("-w, --watch", "Recompile files on changes");
commander.option("-r, --external-helpers", "Replace helpers with references to a `babelHelpers` global");
commander.option("-e, --experimental", "Enable experimental support for proposed ES7 features");
commander.option("-p, --playground", "Enable playground support");
commander.option("-c, --compact [mode]", "When set to \"auto\" compact is `true` when the input size exceeds 100KB. (auto|true|false)", "auto");
commander.option("-m, --modules [modules]", "Module formatter type to use [common]", "common");
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("-L, --loose [list]", "List of transformers to enable loose mode ON", util.list);
commander.option("-o, --out-file [out]", "Compile all input files into a single file");
commander.option("-d, --out-dir [out]", "Compile an input directory of modules into an output directory");
commander.option("-c, --remove-comments", "Remove comments from the compiled code", false);
commander.option("-M, --module-ids", "Insert module id in modules", false);
commander.option("-R, --react-compat", "Makes the react transformer produce pre-v0.12 code");
commander.option("--keep-module-id-extensions", "Keep extensions when generating module ids", false);
commander.option("-a, --auxiliary-comment [comment]", "Comment text to prepend to all auxiliary code");
commander.option("-D, --copy-files", "When compiling a directory copy over non-compilable files");
commander.on("--help", function () {
@@ -38,9 +44,7 @@ commander.on("--help", function () {
each(keys(obj).sort(), function (key) {
if (key[0] === "_") return;
if (obj[key].optional) {
key = "[" + key + "]";
}
if (obj[key].optional) key = "[" + key + "]";
console.log(" - " + key);
});
@@ -87,12 +91,6 @@ if (commander.watch) {
}
}
if (commander.sourceMaps) {
if (!commander.outFile && !commander.outDir) {
errors.push("--source-maps requires --out-file or --out-dir");
}
}
if (errors.length) {
console.error(errors.join(". "));
process.exit(2);
@@ -100,24 +98,11 @@ if (errors.length) {
//
exports.opts = {
keepModuleIdExtensions: commander.keepModuleIdExtensions,
auxiliaryComment: commander.auxiliaryComment,
externalHelpers: commander.externalHelpers,
sourceMapName: commander.outFile,
experimental: commander.experimental,
reactCompat: commander.reactCompat,
playground: commander.playground,
moduleIds: commander.moduleIds,
blacklist: commander.blacklist,
whitelist: commander.whitelist,
sourceMap: commander.sourceMaps || commander.sourceMapsInline,
optional: commander.optional,
comments: !commander.removeComments,
modules: commander.modules,
compact: commander.compact,
loose: commander.loose
};
exports.opts = {};
each(File.options, function (opt, key) {
exports.opts[key] = commander[key];
});
var fn;

View File

@@ -1,11 +1,10 @@
var resolveRc = require("../../lib/babel/api/register/resolve-rc");
var readdir = require("fs-readdir-recursive");
var index = require("./index");
var babel = require("../../lib/babel/api/node");
var util = require("../../lib/babel/util");
var path = require("path");
var fs = require("fs");
var _ = require("lodash");
var readdir = require("fs-readdir-recursive");
var index = require("./index");
var babel = require("../../lib/babel/api/node");
var util = require("../../lib/babel/util");
var path = require("path");
var fs = require("fs");
var _ = require("lodash");
exports.readdirFilter = function (filename) {
return readdir(filename).filter(function (filename) {
@@ -24,19 +23,8 @@ exports.addSourceMappingUrl = function (code, loc) {
exports.transform = function (filename, code, opts) {
opts = _.defaults(opts || {}, index.opts);
opts.filename = filename;
resolveRc(filename, opts);
var result;
try {
result = babel.transform(code, opts);
} catch (e) {
if (e instanceof SyntaxError) {
console.error("SyntaxError:", e.message);
process.exit(1);
} else {
throw e;
}
}
var result = babel.transform(code, opts);
result.filename = filename;
result.actual = code;
return result;

View File

@@ -1,7 +1,7 @@
{
"name": "babel",
"description": "Turn ES6 code into readable vanilla ES5 with source maps",
"version": "4.5.5",
"version": "5.0.0-beta2",
"author": "Sebastian McKenzie <sebmck@gmail.com>",
"homepage": "https://babeljs.io/",
"repository": "babel/babel",
@@ -36,11 +36,11 @@
"test": "make test"
},
"dependencies": {
"acorn-babel": "0.11.1-35",
"ast-types": "~0.6.1",
"ast-types": "~0.7.0",
"chalk": "^1.0.0",
"chokidar": "^0.12.6",
"commander": "^2.6.0",
"convert-source-map": "^0.5.0",
"core-js": "^0.6.1",
"debug": "^2.1.1",
"detect-indent": "^3.0.0",
@@ -49,27 +49,30 @@
"fs-readdir-recursive": "^0.1.0",
"globals": "^6.2.0",
"is-integer": "^1.0.4",
"js-tokens": "0.4.1",
"js-tokens": "1.0.0",
"leven": "^1.0.1",
"line-numbers": "0.2.0",
"lodash": "^3.2.0",
"minimatch": "^2.0.3",
"output-file-sync": "^1.1.0",
"path-is-absolute": "^1.0.0",
"private": "^0.1.6",
"regenerator-babel": "0.8.13-1",
"regexpu": "^1.1.1",
"regenerator-babel": "0.8.13-2",
"regexpu": "^1.1.2",
"repeating": "^1.1.2",
"shebang-regex": "^1.0.0",
"slash": "^1.0.0",
"source-map": "^0.1.43",
"source-map": "^0.4.0",
"source-map-support": "^0.2.9",
"source-map-to-comment": "^1.0.0",
"to-fast-properties": "^1.0.0",
"trim-right": "^1.0.0"
},
"devDependencies": {
"babel": "4.5.1",
"browserify": "^8.1.3",
"babel": "4.7.13",
"browserify": "^9.0.3",
"chai": "^2.0.0",
"eslint": "^0.15.1",
"babel-eslint": "^1.0.1",
"esvalid": "^1.1.0",
"istanbul": "^0.3.5",
"matcha": "^0.6.0",

View File

@@ -1,7 +1,10 @@
{
"name": "babel-runtime",
"description": "babel selfContained runtime",
"version": "4.5.4",
"version": "5.0.0-beta1",
"repository": "babel/babel",
"author": "Sebastian McKenzie <sebmck@gmail.com>"
"author": "Sebastian McKenzie <sebmck@gmail.com>",
"dependencies": {
"core-js": "^0.6.1"
}
}

View File

@@ -1,11 +1,12 @@
"use strict";
var buildHelpers = require("../lib/babel/build-helpers");
var transform = require("../lib/babel/transformation");
var util = require("../lib/babel/util");
var fs = require("fs");
var t = require("../lib/babel/types");
var _ = require("lodash");
var transform = require("../lib/babel/transformation");
var each = require("lodash/collection/each");
var File = require("../lib/babel/transformation/file");
var util = require("../lib/babel/util");
var fs = require("fs");
var t = require("../lib/babel/types");
var _ = require("lodash");
var relative = function (filename) {
return __dirname + "/babel-runtime/" + filename;
@@ -39,22 +40,26 @@ var updatePackage = function () {
var selfContainify = function (code) {
return transform(code, {
optional: ["selfContained"]
optional: ["runtime"]
}).code;
};
var buildHelpers2 = function () {
var body = util.template("self-contained-helpers-head");
var tree = t.program(body);
buildHelpers(body, t.identifier("helpers"));
var buildHelper = function (helperName) {
var tree = t.program(
util.template("self-contained-helpers-head", {
HELPER: util.template("helper-" + helperName)
})
);
return transform.fromAst(tree, null, {
optional: ["selfContained"]
optional: ["runtime"]
}).code;
};
writeFile("helpers.js", buildHelpers2());
each(File.helpers, function (helperName) {
writeFile("helpers/" + helperName + ".js", buildHelper(helperName));
});
writeFile("core-js.js", readFile("core-js/library", true));
writeFile("regenerator/index.js", readFile("regenerator-babel/runtime-module", true));
writeFile("regenerator/runtime.js", selfContainify(readFile("regenerator-babel/runtime")));

32
src/acorn/AUTHORS Executable file
View File

@@ -0,0 +1,32 @@
List of Acorn contributors. Updated before every release.
Alistair Braidwood
Aparajita Fishman
Arian Stolwijk
Artem Govorov
Brandon Mills
Charles Hughes
Conrad Irwin
David Bonnet
impinball
Ingvar Stepanyan
Jiaxing Wang
Johannes Herr
Jürg Lehni
keeyipchan
krator
Marijn Haverbeke
Martin Carlberg
Mathias Bynens
Mathieu 'p01' Henri
Max Schaefer
Mihai Bazon
Mike Rennie
Oskar Schöldström
Paul Harper
Peter Rust
PlNG
r-e-d
Rich Harris
Sebastian McKenzie
zsjforcn

19
src/acorn/LICENSE Executable file
View File

@@ -0,0 +1,19 @@
Copyright (C) 2012-2014 by various contributors (see AUTHORS)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

44
src/acorn/package.json Executable file
View File

@@ -0,0 +1,44 @@
{
"name": "acorn",
"description": "ECMAScript parser",
"homepage": "https://github.com/marijnh/acorn",
"main": "src/index.js",
"version": "1.0.0",
"engines": {
"node": ">=0.4.0"
},
"maintainers": [
{
"name": "Marijn Haverbeke",
"email": "marijnh@gmail.com",
"web": "http://marijnhaverbeke.nl"
},
{
"name": "Ingvar Stepanyan",
"email": "me@rreverser.com",
"web": "http://rreverser.com/"
}
],
"repository": {
"type": "git",
"url": "https://github.com/marijnh/acorn.git"
},
"licenses": [
{
"type": "MIT",
"url": "https://raw.githubusercontent.com/marijnh/acorn/master/LICENSE"
}
],
"scripts": {
"test": "node test/run.js",
"prepublish": "node bin/prepublish.sh"
},
"bin": {
"acorn": "./bin/acorn"
},
"devDependencies": {
"babelify": "^5.0.4",
"browserify": "^9.0.3",
"unicode-7.0.0": "~0.1.5"
}
}

825
src/acorn/plugins/flow.js Normal file
View File

@@ -0,0 +1,825 @@
var acorn = require("..")
var pp = acorn.Parser.prototype
var tt = acorn.tokTypes
pp.isRelational = function (op) {
return this.type === tt.relational && this.value === op
}
pp.expectRelational = function (op) {
if (this.isRelational(op)) {
this.next()
} else {
this.unexpected()
}
}
pp.flow_parseDeclareClass = function (node) {
this.next()
this.flow_parseInterfaceish(node, true)
return this.finishNode(node, "DeclareClass")
}
pp.flow_parseDeclareFunction = function (node) {
this.next()
var id = node.id = this.parseIdent()
var typeNode = this.startNode()
var typeContainer = this.startNode()
if (this.isRelational("<")) {
typeNode.typeParameters = this.flow_parseTypeParameterDeclaration()
} else {
typeNode.typeParameters = null
}
this.expect(tt.parenL)
var tmp = this.flow_parseFunctionTypeParams()
typeNode.params = tmp.params
typeNode.rest = tmp.rest
this.expect(tt.parenR)
this.expect(tt.colon)
typeNode.returnType = this.flow_parseType()
typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation")
id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation")
this.finishNode(id, id.type)
this.semicolon()
return this.finishNode(node, "DeclareFunction")
}
pp.flow_parseDeclare = function (node) {
if (this.type === tt._class) {
return this.flow_parseDeclareClass(node)
} else if (this.type === tt._function) {
return this.flow_parseDeclareFunction(node)
} else if (this.type === tt._var) {
return this.flow_parseDeclareVariable(node)
} else if (this.isContextual("module")) {
return this.flow_parseDeclareModule(node)
} else {
this.unexpected()
}
}
pp.flow_parseDeclareVariable = function (node) {
this.next()
node.id = this.flow_parseTypeAnnotatableIdentifier()
this.semicolon()
return this.finishNode(node, "DeclareVariable")
}
pp.flow_parseDeclareModule = function (node) {
this.next()
if (this.type === tt.string) {
node.id = this.parseExprAtom()
} else {
node.id = this.parseIdent()
}
var bodyNode = node.body = this.startNode()
var body = bodyNode.body = []
this.expect(tt.braceL)
while (this.type !== tt.braceR) {
var node2 = this.startNode()
// todo: declare check
this.next()
body.push(this.flow_parseDeclare(node2))
}
this.expect(tt.braceR)
this.finishNode(bodyNode, "BlockStatement")
return this.finishNode(node, "DeclareModule")
}
// Interfaces
pp.flow_parseInterfaceish = function (node, allowStatic) {
node.id = this.parseIdent()
if (this.isRelational("<")) {
node.typeParameters = this.flow_parseTypeParameterDeclaration()
} else {
node.typeParameters = null
}
node.extends = []
if (this.eat(tt._extends)) {
do {
node.extends.push(this.flow_parseInterfaceExtends())
} while(this.eat(tt.comma))
}
node.body = this.flow_parseObjectType(allowStatic)
}
pp.flow_parseInterfaceExtends = function () {
var node = this.startNode()
node.id = this.parseIdent()
if (this.isRelational("<")) {
node.typeParameters = this.flow_parseTypeParameterInstantiation()
} else {
node.typeParameters = null
}
return this.finishNode(node, "InterfaceExtends")
}
pp.flow_parseInterface = function (node) {
this.flow_parseInterfaceish(node, false)
return this.finishNode(node, "InterfaceDeclaration")
}
// Type aliases
pp.flow_parseTypeAlias = function (node) {
node.id = this.parseIdent()
if (this.isRelational("<")) {
node.typeParameters = this.flow_parseTypeParameterDeclaration()
} else {
node.typeParameters = null
}
this.expect(tt.eq)
node.right = this.flow_parseType()
this.semicolon()
return this.finishNode(node, "TypeAlias")
}
// Type annotations
pp.flow_parseTypeParameterDeclaration = function () {
var node = this.startNode()
node.params = []
this.expectRelational("<")
while (!this.isRelational(">")) {
node.params.push(this.flow_parseTypeAnnotatableIdentifier())
if (!this.isRelational(">")) {
this.expect(tt.comma)
}
}
this.expectRelational(">")
return this.finishNode(node, "TypeParameterDeclaration")
}
pp.flow_parseTypeParameterInstantiation = function () {
var node = this.startNode(), oldInType = this.inType
node.params = []
this.inType = true
this.expectRelational("<")
while (!this.isRelational(">")) {
node.params.push(this.flow_parseType())
if (!this.isRelational(">")) {
this.expect(tt.comma)
}
}
this.expectRelational(">")
this.inType = oldInType
return this.finishNode(node, "TypeParameterInstantiation")
}
pp.flow_parseObjectPropertyKey = function () {
return (this.type === tt.num || this.type === tt.string) ? this.parseExprAtom() : this.parseIdent(true)
}
pp.flow_parseObjectTypeIndexer = function (node, isStatic) {
node.static = isStatic
this.expect(tt.bracketL)
node.id = this.flow_parseObjectPropertyKey()
this.expect(tt.colon)
node.key = this.flow_parseType()
this.expect(tt.bracketR)
this.expect(tt.colon)
node.value = this.flow_parseType()
return this.finishNode(node, "ObjectTypeIndexer")
}
pp.flow_parseObjectTypeMethodish = function (node) {
node.params = []
node.rest = null
node.typeParameters = null
if (this.isRelational("<")) {
node.typeParameters = this.flow_parseTypeParameterDeclaration()
}
this.expect(tt.parenL)
while (this.type === tt.name) {
node.params.push(this.flow_parseFunctionTypeParam())
if (this.type !== tt.parenR) {
this.expect(tt.comma)
}
}
if (this.eat(tt.ellipsis)) {
node.rest = this.flow_parseFunctionTypeParam()
}
this.expect(tt.parenR)
this.expect(tt.colon)
node.returnType = this.flow_parseType()
return this.finishNode(node, "FunctionTypeAnnotation")
}
pp.flow_parseObjectTypeMethod = function (start, isStatic, key) {
var node = this.startNodeAt(start)
node.value = this.flow_parseObjectTypeMethodish(this.startNodeAt(start))
node.static = isStatic
node.key = key
node.optional = false
return this.finishNode(node, "ObjectTypeProperty")
}
pp.flow_parseObjectTypeCallProperty = function (node, isStatic) {
var valueNode = this.startNode()
node.static = isStatic
node.value = this.flow_parseObjectTypeMethodish(valueNode)
return this.finishNode(node, "ObjectTypeCallProperty")
}
pp.flow_parseObjectType = function (allowStatic) {
var nodeStart = this.startNode()
var node
var optional = false
var property
var propertyKey
var propertyTypeAnnotation
var token
var isStatic
nodeStart.callProperties = []
nodeStart.properties = []
nodeStart.indexers = []
this.expect(tt.braceL)
while (this.type !== tt.braceR) {
var start = this.markPosition()
node = this.startNode()
if (allowStatic && this.isContextual("static")) {
this.next()
isStatic = true
}
if (this.type === tt.bracketL) {
nodeStart.indexers.push(this.flow_parseObjectTypeIndexer(node, isStatic))
} else if (this.type === tt.parenL || this.isRelational("<")) {
nodeStart.callProperties.push(this.flow_parseObjectTypeCallProperty(node, allowStatic))
} else {
if (isStatic && this.type === tt.colon) {
propertyKey = this.parseIdent()
} else {
propertyKey = this.flow_parseObjectPropertyKey()
}
if (this.isRelational("<") || this.type === tt.parenL) {
// This is a method property
nodeStart.properties.push(this.flow_parseObjectTypeMethod(start, isStatic, propertyKey))
} else {
if (this.eat(tt.question)) {
optional = true
}
this.expect(tt.colon)
node.key = propertyKey
node.value = this.flow_parseType()
node.optional = optional
node.static = isStatic
nodeStart.properties.push(this.finishNode(node, "ObjectTypeProperty"))
}
}
if (!this.eat(tt.semi) && this.type !== tt.braceR) {
this.unexpected()
}
}
this.expect(tt.braceR)
return this.finishNode(nodeStart, "ObjectTypeAnnotation")
}
pp.flow_parseGenericType = function (start, id) {
var node = this.startNodeAt(start)
node.typeParameters = null
node.id = id
while (this.eat(tt.dot)) {
var node2 = this.startNodeAt(start)
node2.qualification = node.id
node2.id = this.parseIdent()
node.id = this.finishNode(node2, "QualifiedTypeIdentifier")
}
if (this.isRelational("<")) {
node.typeParameters = this.flow_parseTypeParameterInstantiation()
}
return this.finishNode(node, "GenericTypeAnnotation")
}
pp.flow_parseVoidType = function () {
var node = this.startNode()
this.expect(tt._void)
return this.finishNode(node, "VoidTypeAnnotation")
}
pp.flow_parseTypeofType = function () {
var node = this.startNode()
this.expect(tt._typeof)
node.argument = this.flow_parsePrimaryType()
return this.finishNode(node, "TypeofTypeAnnotation")
}
pp.flow_parseTupleType = function () {
var node = this.startNode()
node.types = []
this.expect(tt.bracketL)
// We allow trailing commas
while (this.pos < this.input.length && this.type !== tt.bracketR) {
node.types.push(this.flow_parseType())
if (this.type === tt.bracketR) break
this.expect(tt.comma)
}
this.expect(tt.bracketR)
return this.finishNode(node, "TupleTypeAnnotation")
}
pp.flow_parseFunctionTypeParam = function () {
var optional = false
var node = this.startNode()
node.name = this.parseIdent()
if (this.eat(tt.question)) {
optional = true
}
this.expect(tt.colon)
node.optional = optional
node.typeAnnotation = this.flow_parseType()
return this.finishNode(node, "FunctionTypeParam")
}
pp.flow_parseFunctionTypeParams = function () {
var ret = { params: [], rest: null }
while (this.type === tt.name) {
ret.params.push(this.flow_parseFunctionTypeParam())
if (this.type !== tt.parenR) {
this.expect(tt.comma)
}
}
if (this.eat(tt.ellipsis)) {
ret.rest = this.flow_parseFunctionTypeParam()
}
return ret
}
pp.flow_identToTypeAnnotation = function (start, node, id) {
switch (id.name) {
case "any":
return this.finishNode(node, "AnyTypeAnnotation")
case "bool":
case "boolean":
return this.finishNode(node, "BooleanTypeAnnotation")
case "number":
return this.finishNode(node, "NumberTypeAnnotation")
case "string":
return this.finishNode(node, "StringTypeAnnotation")
default:
return this.flow_parseGenericType(start, id)
}
}
// The parsing of types roughly parallels the parsing of expressions, and
// primary types are kind of like primary expressions...they're the
// primitives with which other types are constructed.
pp.flow_parsePrimaryType = function () {
var typeIdentifier = null
var params = null
var returnType = null
var start = this.markPosition()
var node = this.startNode()
var rest = null
var tmp
var typeParameters
var token
var type
var isGroupedType = false
switch (this.type) {
case tt.name:
return this.flow_identToTypeAnnotation(start, node, this.parseIdent())
case tt.braceL:
return this.flow_parseObjectType()
case tt.bracketL:
return this.flow_parseTupleType()
case tt.relational:
if (this.value === "<") {
node.typeParameters = this.flow_parseTypeParameterDeclaration()
this.expect(tt.parenL)
tmp = this.flow_parseFunctionTypeParams()
node.params = tmp.params
node.rest = tmp.rest
this.expect(tt.parenR)
this.expect(tt.arrow)
node.returnType = this.flow_parseType()
return this.finishNode(node, "FunctionTypeAnnotation")
}
case tt.parenL:
this.next()
// Check to see if this is actually a grouped type
if (this.type !== tt.parenR && this.type !== tt.ellipsis) {
if (this.type === tt.name) {
var token = this.lookahead().type
isGroupedType = token !== tt.question && token !== tt.colon
} else {
isGroupedType = true
}
}
if (isGroupedType) {
type = this.flow_parseType()
this.expect(tt.parenR)
// If we see a => next then someone was probably confused about
// function types, so we can provide a better error message
if (this.eat(tt.arrow)) {
this.raise(node,
"Unexpected token =>. It looks like " +
"you are trying to write a function type, but you ended up " +
"writing a grouped type followed by an =>, which is a syntax " +
"error. Remember, function type parameters are named so function " +
"types look like (name1: type1, name2: type2) => returnType. You " +
"probably wrote (type1) => returnType"
)
}
return type
}
tmp = this.flow_parseFunctionTypeParams()
node.params = tmp.params
node.rest = tmp.rest
this.expect(tt.parenR)
this.expect(tt.arrow)
node.returnType = this.flow_parseType()
node.typeParameters = null
return this.finishNode(node, "FunctionTypeAnnotation")
case tt.string:
node.value = this.value
node.raw = this.input.slice(this.start, this.end)
this.next()
return this.finishNode(node, "StringLiteralTypeAnnotation")
default:
if (this.type.keyword) {
switch (this.type.keyword) {
case "void":
return this.flow_parseVoidType()
case "typeof":
return this.flow_parseTypeofType()
}
}
}
this.unexpected()
}
pp.flow_parsePostfixType = function () {
var node = this.startNode()
var type = node.elementType = this.flow_parsePrimaryType()
if (this.type === tt.bracketL) {
this.expect(tt.bracketL)
this.expect(tt.bracketR)
return this.finishNode(node, "ArrayTypeAnnotation")
}
return type
}
pp.flow_parsePrefixType = function () {
var node = this.startNode()
if (this.eat(tt.question)) {
node.typeAnnotation = this.flow_parsePrefixType()
return this.finishNode(node, "NullableTypeAnnotation")
}
return this.flow_parsePostfixType()
}
pp.flow_parseIntersectionType = function () {
var node = this.startNode()
var type = this.flow_parsePrefixType()
node.types = [type]
while (this.eat(tt.bitwiseAND)) {
node.types.push(this.flow_parsePrefixType())
}
return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation")
}
pp.flow_parseUnionType = function () {
var node = this.startNode()
var type = this.flow_parseIntersectionType()
node.types = [type]
while (this.eat(tt.bitwiseOR)) {
node.types.push(this.flow_parseIntersectionType())
}
return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation")
}
pp.flow_parseType = function () {
var oldInType = this.inType
this.inType = true
var type = this.flow_parseUnionType()
this.inType = oldInType
return type
}
pp.flow_parseTypeAnnotation = function () {
var node = this.startNode()
var oldInType = this.inType
this.inType = true
this.expect(tt.colon)
node.typeAnnotation = this.flow_parseType()
this.inType = oldInType
return this.finishNode(node, "TypeAnnotation")
}
pp.flow_parseTypeAnnotatableIdentifier = function (requireTypeAnnotation, canBeOptionalParam) {
var node = this.startNode()
var ident = this.parseIdent()
var isOptionalParam = false
if (canBeOptionalParam && this.eat(tt.question)) {
this.expect(tt.question)
isOptionalParam = true
}
if (requireTypeAnnotation || this.type === tt.colon) {
ident.typeAnnotation = this.flow_parseTypeAnnotation()
this.finishNode(ident, ident.type)
}
if (isOptionalParam) {
ident.optional = true
this.finishNode(ident, ident.type)
}
return ident
}
acorn.plugins.flow = function (instance) {
// function name(): string {}
instance.extend("parseFunctionBody", function (inner) {
return function (node, allowExpression) {
if (this.type === tt.colon) {
node.returnType = this.flow_parseTypeAnnotation()
}
return inner.call(this, node, allowExpression)
}
})
instance.extend("parseStatement", function (inner) {
return function(declaration, topLevel) {
// strict mode handling of `interface` since it's a reserved word
if (this.strict && this.type === tt.name && this.value === "interface") {
var node = this.startNode()
this.next()
return this.flow_parseInterface(node)
} else {
return inner.call(this, declaration, topLevel)
}
}
})
instance.extend("parseExpressionStatement", function (inner) {
return function (node, expr) {
if (expr.type === "Identifier") {
if (expr.name === "declare") {
if (this.type === tt._class || this.type === tt.name || this.type === tt._function || this.type === tt._var) {
return this.flow_parseDeclare(node)
}
} else if (this.type === tt.name) {
if (expr.name === "interface") {
return this.flow_parseInterface(node)
} else if (expr.name === "type") {
return this.flow_parseTypeAlias(node)
}
}
}
return inner.call(this, node, expr)
}
})
instance.extend("shouldParseExportDeclaration", function (inner) {
return function () {
return this.isContextual("type") || inner.call(this)
}
})
instance.extend("parseParenItem", function (inner) {
return function (node, start) {
if (this.type === tt.colon) {
var typeCastNode = this.startNodeAt(start)
typeCastNode.expression = node
typeCastNode.typeAnnotation = this.flow_parseTypeAnnotation()
return this.finishNode(typeCastNode, "TypeCastExpression")
} else {
return node
}
}
})
instance.extend("parseClassId", function (inner) {
return function (node, isStatement) {
inner.call(this, node, isStatement)
if (this.isRelational("<")) {
node.typeParameters = this.flow_parseTypeParameterDeclaration()
}
}
})
instance.extend("readToken", function (inner) {
return function(code) {
if (this.inType && (code === 62 || code === 60)) {
return this.finishOp(tt.relational, 1)
} else {
return inner.call(this, code)
}
}
})
instance.extend("jsx_readToken", function (inner) {
return function () {
if (!this.inType) return inner.call(this)
}
})
instance.extend("parseParenArrowList", function (inner) {
return function (start, exprList, isAsync) {
for (var i = 0; i < exprList.length; i++) {
var listItem = exprList[i]
if (listItem.type === "TypeCastExpression") {
var expr = listItem.expression
expr.typeAnnotation = listItem.typeAnnotation
exprList[i] = expr
}
}
return inner.call(this, start, exprList, isAsync)
}
})
instance.extend("parseClassProperty", function (inner) {
return function (node) {
if (this.type === tt.colon) {
node.typeAnnotation = this.flow_parseTypeAnnotation()
}
return inner.call(this, node)
}
})
instance.extend("isClassProperty", function (inner) {
return function () {
return this.type === tt.colon || inner.call(this)
}
})
instance.extend("parseClassMethod", function (inner) {
return function (classBody, method, isGenerator, isAsync) {
var typeParameters
if (this.isRelational("<")) {
typeParameters = this.flow_parseTypeParameterDeclaration()
}
method.value = this.parseMethod(isGenerator, isAsync)
method.value.typeParameters = typeParameters
classBody.body.push(this.finishNode(method, "MethodDefinition"))
}
})
instance.extend("parseClassSuper", function (inner) {
return function (node, isStatement) {
inner.call(this, node, isStatement)
if (node.superClass && this.isRelational("<")) {
node.superTypeParameters = this.flow_parseTypeParameterInstantiation()
}
if (this.isContextual("implements")) {
this.next()
var implemented = node.implements = []
do {
var node = this.startNode()
node.id = this.parseIdent()
if (this.isRelational("<")) {
node.typeParameters = this.flow_parseTypeParameterInstantiation()
} else {
node.typeParameters = null
}
implemented.push(this.finishNode(node, "ClassImplements"))
} while(this.eat(tt.comma))
}
}
})
instance.extend("parseObjPropValue", function (inner) {
return function (prop) {
var typeParameters
if (this.isRelational("<")) {
typeParameters = this.flow_parseTypeParameterDeclaration()
if (this.type !== tt.parenL) this.unexpected()
}
inner.apply(this, arguments)
prop.value.typeParameters = typeParameters
}
})
instance.extend("parseAssignableListItemTypes", function (inner) {
return function (param) {
if (this.eat(tt.question)) {
param.optional = true
}
if (this.type === tt.colon) {
param.typeAnnotation = this.flow_parseTypeAnnotation()
}
this.finishNode(param, param.type)
return param
}
})
instance.extend("parseImportSpecifiers", function (inner) {
return function (node) {
node.isType = false
if (this.isContextual("type")) {
var start = this.markPosition()
var typeId = this.parseIdent()
if ((this.type === tt.name && this.value !== "from") || this.type === tt.braceL || this.type === tt.star) {
node.isType = true
} else {
node.specifiers.push(this.parseImportSpecifierDefault(typeId, start))
if (this.isContextual("from")) return
this.eat(tt.comma)
}
}
inner.call(this, node)
}
})
// function foo<T>() {}
instance.extend("parseFunctionParams", function (inner) {
return function (node) {
if (this.isRelational("<")) {
node.typeParameters = this.flow_parseTypeParameterDeclaration()
}
inner.call(this, node)
}
})
// var foo: string = bar
instance.extend("parseVarHead", function (inner) {
return function (decl) {
inner.call(this, decl)
if (this.type === tt.colon) {
decl.id.typeAnnotation = this.flow_parseTypeAnnotation()
this.finishNode(decl.id, decl.id.type)
}
}
})
}

658
src/acorn/plugins/jsx.js Normal file
View File

@@ -0,0 +1,658 @@
var acorn = require("..")
var tt = acorn.tokTypes;
var tc = acorn.tokContexts;
tc.j_oTag = new acorn.TokContext("<tag", false);
tc.j_cTag = new acorn.TokContext("</tag", false);
tc.j_expr = new acorn.TokContext("<tag>...</tag>", true, true);
tt.jsxName = new acorn.TokenType("jsxName");
tt.jsxText = new acorn.TokenType("jsxText", {beforeExpr: true});
tt.jsxTagStart = new acorn.TokenType("jsxTagStart");
tt.jsxTagEnd = new acorn.TokenType("jsxTagEnd");
tt.jsxTagStart.updateContext = function() {
this.context.push(tc.j_expr); // treat as beginning of JSX expression
this.context.push(tc.j_oTag); // start opening tag context
this.exprAllowed = false;
};
tt.jsxTagEnd.updateContext = function(prevType) {
var out = this.context.pop();
if (out === tc.j_oTag && prevType === tt.slash || out === tc.j_cTag) {
this.context.pop();
this.exprAllowed = this.curContext() === tc.j_expr;
} else {
this.exprAllowed = true;
}
};
var pp = acorn.Parser.prototype;
// Reads inline JSX contents token.
pp.jsx_readToken = function() {
var out = "", chunkStart = this.pos;
for (;;) {
if (this.pos >= this.input.length)
this.raise(this.start, "Unterminated JSX contents");
var ch = this.input.charCodeAt(this.pos);
switch (ch) {
case 60: // '<'
case 123: // '{'
if (this.pos === this.start) {
if (ch === 60 && this.exprAllowed) {
++this.pos;
return this.finishToken(tt.jsxTagStart);
}
return this.getTokenFromCode(ch);
}
out += this.input.slice(chunkStart, this.pos);
return this.finishToken(tt.jsxText, out);
case 38: // '&'
out += this.input.slice(chunkStart, this.pos);
out += this.jsx_readEntity();
chunkStart = this.pos;
break;
default:
if (acorn.isNewLine(ch)) {
out += this.input.slice(chunkStart, this.pos);
++this.pos;
if (ch === 13 && this.input.charCodeAt(this.pos) === 10) {
++this.pos;
out += "\n";
} else {
out += String.fromCharCode(ch);
}
if (this.options.locations) {
++this.curLine;
this.lineStart = this.pos;
}
chunkStart = this.pos;
} else {
++this.pos;
}
}
}
};
pp.jsx_readString = function(quote) {
var out = "", chunkStart = ++this.pos;
for (;;) {
if (this.pos >= this.input.length)
this.raise(this.start, "Unterminated string constant");
var ch = this.input.charCodeAt(this.pos);
if (ch === quote) break;
if (ch === 38) { // '&'
out += this.input.slice(chunkStart, this.pos);
out += this.jsx_readEntity();
chunkStart = this.pos;
} else {
++this.pos;
}
}
out += this.input.slice(chunkStart, this.pos++);
return this.finishToken(tt.string, out);
};
var XHTMLEntities = {
quot: '\u0022',
amp: '&',
apos: '\u0027',
lt: '<',
gt: '>',
nbsp: '\u00A0',
iexcl: '\u00A1',
cent: '\u00A2',
pound: '\u00A3',
curren: '\u00A4',
yen: '\u00A5',
brvbar: '\u00A6',
sect: '\u00A7',
uml: '\u00A8',
copy: '\u00A9',
ordf: '\u00AA',
laquo: '\u00AB',
not: '\u00AC',
shy: '\u00AD',
reg: '\u00AE',
macr: '\u00AF',
deg: '\u00B0',
plusmn: '\u00B1',
sup2: '\u00B2',
sup3: '\u00B3',
acute: '\u00B4',
micro: '\u00B5',
para: '\u00B6',
middot: '\u00B7',
cedil: '\u00B8',
sup1: '\u00B9',
ordm: '\u00BA',
raquo: '\u00BB',
frac14: '\u00BC',
frac12: '\u00BD',
frac34: '\u00BE',
iquest: '\u00BF',
Agrave: '\u00C0',
Aacute: '\u00C1',
Acirc: '\u00C2',
Atilde: '\u00C3',
Auml: '\u00C4',
Aring: '\u00C5',
AElig: '\u00C6',
Ccedil: '\u00C7',
Egrave: '\u00C8',
Eacute: '\u00C9',
Ecirc: '\u00CA',
Euml: '\u00CB',
Igrave: '\u00CC',
Iacute: '\u00CD',
Icirc: '\u00CE',
Iuml: '\u00CF',
ETH: '\u00D0',
Ntilde: '\u00D1',
Ograve: '\u00D2',
Oacute: '\u00D3',
Ocirc: '\u00D4',
Otilde: '\u00D5',
Ouml: '\u00D6',
times: '\u00D7',
Oslash: '\u00D8',
Ugrave: '\u00D9',
Uacute: '\u00DA',
Ucirc: '\u00DB',
Uuml: '\u00DC',
Yacute: '\u00DD',
THORN: '\u00DE',
szlig: '\u00DF',
agrave: '\u00E0',
aacute: '\u00E1',
acirc: '\u00E2',
atilde: '\u00E3',
auml: '\u00E4',
aring: '\u00E5',
aelig: '\u00E6',
ccedil: '\u00E7',
egrave: '\u00E8',
eacute: '\u00E9',
ecirc: '\u00EA',
euml: '\u00EB',
igrave: '\u00EC',
iacute: '\u00ED',
icirc: '\u00EE',
iuml: '\u00EF',
eth: '\u00F0',
ntilde: '\u00F1',
ograve: '\u00F2',
oacute: '\u00F3',
ocirc: '\u00F4',
otilde: '\u00F5',
ouml: '\u00F6',
divide: '\u00F7',
oslash: '\u00F8',
ugrave: '\u00F9',
uacute: '\u00FA',
ucirc: '\u00FB',
uuml: '\u00FC',
yacute: '\u00FD',
thorn: '\u00FE',
yuml: '\u00FF',
OElig: '\u0152',
oelig: '\u0153',
Scaron: '\u0160',
scaron: '\u0161',
Yuml: '\u0178',
fnof: '\u0192',
circ: '\u02C6',
tilde: '\u02DC',
Alpha: '\u0391',
Beta: '\u0392',
Gamma: '\u0393',
Delta: '\u0394',
Epsilon: '\u0395',
Zeta: '\u0396',
Eta: '\u0397',
Theta: '\u0398',
Iota: '\u0399',
Kappa: '\u039A',
Lambda: '\u039B',
Mu: '\u039C',
Nu: '\u039D',
Xi: '\u039E',
Omicron: '\u039F',
Pi: '\u03A0',
Rho: '\u03A1',
Sigma: '\u03A3',
Tau: '\u03A4',
Upsilon: '\u03A5',
Phi: '\u03A6',
Chi: '\u03A7',
Psi: '\u03A8',
Omega: '\u03A9',
alpha: '\u03B1',
beta: '\u03B2',
gamma: '\u03B3',
delta: '\u03B4',
epsilon: '\u03B5',
zeta: '\u03B6',
eta: '\u03B7',
theta: '\u03B8',
iota: '\u03B9',
kappa: '\u03BA',
lambda: '\u03BB',
mu: '\u03BC',
nu: '\u03BD',
xi: '\u03BE',
omicron: '\u03BF',
pi: '\u03C0',
rho: '\u03C1',
sigmaf: '\u03C2',
sigma: '\u03C3',
tau: '\u03C4',
upsilon: '\u03C5',
phi: '\u03C6',
chi: '\u03C7',
psi: '\u03C8',
omega: '\u03C9',
thetasym: '\u03D1',
upsih: '\u03D2',
piv: '\u03D6',
ensp: '\u2002',
emsp: '\u2003',
thinsp: '\u2009',
zwnj: '\u200C',
zwj: '\u200D',
lrm: '\u200E',
rlm: '\u200F',
ndash: '\u2013',
mdash: '\u2014',
lsquo: '\u2018',
rsquo: '\u2019',
sbquo: '\u201A',
ldquo: '\u201C',
rdquo: '\u201D',
bdquo: '\u201E',
dagger: '\u2020',
Dagger: '\u2021',
bull: '\u2022',
hellip: '\u2026',
permil: '\u2030',
prime: '\u2032',
Prime: '\u2033',
lsaquo: '\u2039',
rsaquo: '\u203A',
oline: '\u203E',
frasl: '\u2044',
euro: '\u20AC',
image: '\u2111',
weierp: '\u2118',
real: '\u211C',
trade: '\u2122',
alefsym: '\u2135',
larr: '\u2190',
uarr: '\u2191',
rarr: '\u2192',
darr: '\u2193',
harr: '\u2194',
crarr: '\u21B5',
lArr: '\u21D0',
uArr: '\u21D1',
rArr: '\u21D2',
dArr: '\u21D3',
hArr: '\u21D4',
forall: '\u2200',
part: '\u2202',
exist: '\u2203',
empty: '\u2205',
nabla: '\u2207',
isin: '\u2208',
notin: '\u2209',
ni: '\u220B',
prod: '\u220F',
sum: '\u2211',
minus: '\u2212',
lowast: '\u2217',
radic: '\u221A',
prop: '\u221D',
infin: '\u221E',
ang: '\u2220',
and: '\u2227',
or: '\u2228',
cap: '\u2229',
cup: '\u222A',
'int': '\u222B',
there4: '\u2234',
sim: '\u223C',
cong: '\u2245',
asymp: '\u2248',
ne: '\u2260',
equiv: '\u2261',
le: '\u2264',
ge: '\u2265',
sub: '\u2282',
sup: '\u2283',
nsub: '\u2284',
sube: '\u2286',
supe: '\u2287',
oplus: '\u2295',
otimes: '\u2297',
perp: '\u22A5',
sdot: '\u22C5',
lceil: '\u2308',
rceil: '\u2309',
lfloor: '\u230A',
rfloor: '\u230B',
lang: '\u2329',
rang: '\u232A',
loz: '\u25CA',
spades: '\u2660',
clubs: '\u2663',
hearts: '\u2665',
diams: '\u2666'
};
var hexNumber = /^[\da-fA-F]+$/;
var decimalNumber = /^\d+$/;
pp.jsx_readEntity = function() {
var str = "", count = 0, entity;
var ch = this.input[this.pos];
if (ch !== "&")
this.raise(this.pos, "Entity must start with an ampersand");
var startPos = ++this.pos;
while (this.pos < this.input.length && count++ < 10) {
ch = this.input[this.pos++];
if (ch === ";") {
if (str[0] === "#") {
if (str[1] === "x") {
str = str.substr(2);
if (hexNumber.test(str))
entity = String.fromCharCode(parseInt(str, 16));
} else {
str = str.substr(1);
if (decimalNumber.test(str))
entity = String.fromCharCode(parseInt(str, 10));
}
} else {
entity = XHTMLEntities[str];
}
break;
}
str += ch;
}
if (!entity) {
this.pos = startPos;
return "&";
}
return entity;
};
// Read a JSX identifier (valid tag or attribute name).
//
// Optimized version since JSX identifiers can't contain
// escape characters and so can be read as single slice.
// Also assumes that first character was already checked
// by isIdentifierStart in readToken.
pp.jsx_readWord = function() {
var ch, start = this.pos;
do {
ch = this.input.charCodeAt(++this.pos);
} while (acorn.isIdentifierChar(ch) || ch === 45); // '-'
return this.finishToken(tt.jsxName, this.input.slice(start, this.pos));
};
// Transforms JSX element name to string.
function getQualifiedJSXName(object) {
if (object.type === "JSXIdentifier")
return object.name;
if (object.type === "JSXNamespacedName")
return object.namespace.name + ':' + object.name.name;
if (object.type === "JSXMemberExpression")
return getQualifiedJSXName(object.object) + '.' +
getQualifiedJSXName(object.property);
}
// Parse next token as JSX identifier
pp.jsx_parseIdentifier = function() {
var node = this.startNode();
if (this.type === tt.jsxName)
node.name = this.value;
else if (this.type.keyword)
node.name = this.type.keyword;
else
this.unexpected();
this.next();
return this.finishNode(node, "JSXIdentifier");
};
// Parse namespaced identifier.
pp.jsx_parseNamespacedName = function() {
var start = this.markPosition();
var name = this.jsx_parseIdentifier();
if (!this.eat(tt.colon)) return name;
var node = this.startNodeAt(start);
node.namespace = name;
node.name = this.jsx_parseIdentifier();
return this.finishNode(node, "JSXNamespacedName");
};
// Parses element name in any form - namespaced, member
// or single identifier.
pp.jsx_parseElementName = function() {
var start = this.markPosition();
var node = this.jsx_parseNamespacedName();
while (this.eat(tt.dot)) {
var newNode = this.startNodeAt(start);
newNode.object = node;
newNode.property = this.jsx_parseIdentifier();
node = this.finishNode(newNode, "JSXMemberExpression");
}
return node;
};
// Parses any type of JSX attribute value.
pp.jsx_parseAttributeValue = function() {
switch (this.type) {
case tt.braceL:
var node = this.jsx_parseExpressionContainer();
if (node.expression.type === "JSXEmptyExpression")
this.raise(node.start, "JSX attributes must only be assigned a non-empty expression");
return node;
case tt.jsxTagStart:
case tt.string:
return this.parseExprAtom();
default:
this.raise(this.start, "JSX value should be either an expression or a quoted JSX text");
}
};
// JSXEmptyExpression is unique type since it doesn't actually parse anything,
// and so it should start at the end of last read token (left brace) and finish
// at the beginning of the next one (right brace).
pp.jsx_parseEmptyExpression = function() {
var tmp = this.start;
this.start = this.lastTokEnd;
this.lastTokEnd = tmp;
tmp = this.startLoc;
this.startLoc = this.lastTokEndLoc;
this.lastTokEndLoc = tmp;
return this.finishNode(this.startNode(), "JSXEmptyExpression");
};
// Parses JSX expression enclosed into curly brackets.
pp.jsx_parseExpressionContainer = function() {
var node = this.startNode();
this.next();
node.expression = this.type === tt.braceR
? this.jsx_parseEmptyExpression()
: this.parseExpression();
this.expect(tt.braceR);
return this.finishNode(node, "JSXExpressionContainer");
};
// Parses following JSX attribute name-value pair.
pp.jsx_parseAttribute = function() {
var node = this.startNode();
if (this.eat(tt.braceL)) {
this.expect(tt.ellipsis);
node.argument = this.parseMaybeAssign();
this.expect(tt.braceR);
return this.finishNode(node, "JSXSpreadAttribute");
}
node.name = this.jsx_parseNamespacedName();
node.value = this.eat(tt.eq) ? this.jsx_parseAttributeValue() : null;
return this.finishNode(node, "JSXAttribute");
};
// Parses JSX opening tag starting after '<'.
pp.jsx_parseOpeningElementAt = function(start) {
var node = this.startNodeAt(start);
node.attributes = [];
node.name = this.jsx_parseElementName();
while (this.type !== tt.slash && this.type !== tt.jsxTagEnd)
node.attributes.push(this.jsx_parseAttribute());
node.selfClosing = this.eat(tt.slash);
this.expect(tt.jsxTagEnd);
return this.finishNode(node, "JSXOpeningElement");
};
// Parses JSX closing tag starting after '</'.
pp.jsx_parseClosingElementAt = function(start) {
var node = this.startNodeAt(start);
node.name = this.jsx_parseElementName();
this.expect(tt.jsxTagEnd);
return this.finishNode(node, "JSXClosingElement");
};
// Parses entire JSX element, including it's opening tag
// (starting after '<'), attributes, contents and closing tag.
pp.jsx_parseElementAt = function(start) {
var node = this.startNodeAt(start);
var children = [];
var openingElement = this.jsx_parseOpeningElementAt(start);
var closingElement = null;
if (!openingElement.selfClosing) {
contents: for (;;) {
switch (this.type) {
case tt.jsxTagStart:
start = this.markPosition();
this.next();
if (this.eat(tt.slash)) {
closingElement = this.jsx_parseClosingElementAt(start);
break contents;
}
children.push(this.jsx_parseElementAt(start));
break;
case tt.jsxText:
children.push(this.parseExprAtom());
break;
case tt.braceL:
children.push(this.jsx_parseExpressionContainer());
break;
default:
this.unexpected();
}
}
if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name))
this.raise(
closingElement.start,
"Expected corresponding JSX closing tag for <" + getQualifiedJSXName(openingElement.name) + ">");
}
node.openingElement = openingElement;
node.closingElement = closingElement;
node.children = children;
return this.finishNode(node, "JSXElement");
};
// Parses entire JSX element from current position.
pp.jsx_parseElement = function() {
var start = this.markPosition();
this.next();
return this.jsx_parseElementAt(start);
};
acorn.plugins.jsx = function(instance) {
instance.extend("parseExprAtom", function(inner) {
return function(refShortHandDefaultPos) {
if (this.type === tt.jsxText)
return this.parseLiteral(this.value);
else if (this.type === tt.jsxTagStart)
return this.jsx_parseElement();
else
return inner.call(this, refShortHandDefaultPos);
};
});
instance.extend("readToken", function(inner) {
return function(code) {
var context = this.curContext();
if (context === tc.j_expr) return this.jsx_readToken();
if (context === tc.j_oTag || context === tc.j_cTag) {
if (acorn.isIdentifierStart(code)) return this.jsx_readWord();
if (code == 62) {
++this.pos;
return this.finishToken(tt.jsxTagEnd);
}
if ((code === 34 || code === 39) && context == tc.j_oTag)
return this.jsx_readString(code);
}
if (code === 60 && this.exprAllowed) {
++this.pos;
return this.finishToken(tt.jsxTagStart);
}
return inner.call(this, code);
};
});
instance.extend("updateContext", function(inner) {
return function(prevType) {
if (this.type == tt.braceL) {
var curContext = this.curContext();
if (curContext == tc.j_oTag) this.context.push(tc.b_expr);
else if (curContext == tc.j_expr) this.context.push(tc.b_tmpl);
else inner.call(this, prevType);
this.exprAllowed = true;
} else if (this.type === tt.slash && prevType === tt.jsxTagStart) {
this.context.length -= 2; // do not consider JSX expr -> JSX open tag -> ... anymore
this.context.push(tc.j_cTag); // reconsider as closing tag context
this.exprAllowed = false;
} else {
return inner.call(this, prevType);
}
};
});
}

760
src/acorn/src/expression.js Executable file
View File

@@ -0,0 +1,760 @@
// A recursive descent parser operates by defining functions for all
// syntactic elements, and recursively calling those, each function
// advancing the input stream and returning an AST node. Precedence
// of constructs (for example, the fact that `!x[1]` means `!(x[1])`
// instead of `(!x)[1]` is handled by the fact that the parser
// function that parses unary prefix operators is called first, and
// in turn calls the function that parses `[]` subscripts — that
// way, it'll receive the node for `x[1]` already parsed, and wraps
// *that* in the unary operator node.
//
// Acorn uses an [operator precedence parser][opp] to handle binary
// operator precedence, because it is much more compact than using
// the technique outlined above, which uses different, nesting
// functions to specify precedence, for all of the ten binary
// precedence levels that JavaScript defines.
//
// [opp]: http://en.wikipedia.org/wiki/Operator-precedence_parser
import {types as tt} from "./tokentype"
import {Parser} from "./state"
import {reservedWords} from "./identifier"
import {has} from "./util"
const pp = Parser.prototype
// Check if property name clashes with already added.
// Object/class getters and setters are not allowed to clash —
// either with each other or with an init property — and in
// strict mode, init properties are also not allowed to be repeated.
pp.checkPropClash = function(prop, propHash) {
if (this.options.ecmaVersion >= 6) return
let key = prop.key, name
switch (key.type) {
case "Identifier": name = key.name; break
case "Literal": name = String(key.value); break
default: return
}
let kind = prop.kind || "init", other
if (has(propHash, name)) {
other = propHash[name]
let isGetSet = kind !== "init"
if ((this.strict || isGetSet) && other[kind] || !(isGetSet ^ other.init))
this.raise(key.start, "Redefinition of property")
} else {
other = propHash[name] = {
init: false,
get: false,
set: false
}
}
other[kind] = true
}
// ### Expression parsing
// These nest, from the most general expression type at the top to
// 'atomic', nondivisible expression types at the bottom. Most of
// the functions will simply let the function(s) below them parse,
// and, *if* the syntactic construct they handle is present, wrap
// the AST node that the inner parser gave them in another node.
// Parse a full expression. The optional arguments are used to
// forbid the `in` operator (in for loops initalization expressions)
// and provide reference for storing '=' operator inside shorthand
// property assignment in contexts where both object expression
// and object pattern might appear (so it's possible to raise
// delayed syntax error at correct position).
pp.parseExpression = function(noIn, refShorthandDefaultPos) {
let start = this.markPosition()
let expr = this.parseMaybeAssign(noIn, refShorthandDefaultPos)
if (this.type === tt.comma) {
let node = this.startNodeAt(start)
node.expressions = [expr]
while (this.eat(tt.comma)) node.expressions.push(this.parseMaybeAssign(noIn, refShorthandDefaultPos))
return this.finishNode(node, "SequenceExpression")
}
return expr
}
// Parse an assignment expression. This includes applications of
// operators like `+=`.
pp.parseMaybeAssign = function(noIn, refShorthandDefaultPos, afterLeftParse) {
if (this.type == tt._yield && this.inGenerator) return this.parseYield()
let failOnShorthandAssign
if (!refShorthandDefaultPos) {
refShorthandDefaultPos = {start: 0}
failOnShorthandAssign = true
} else {
failOnShorthandAssign = false
}
let start = this.markPosition()
let left = this.parseMaybeConditional(noIn, refShorthandDefaultPos)
if (afterLeftParse) left = afterLeftParse.call(this, left, start)
if (this.type.isAssign) {
let node = this.startNodeAt(start)
node.operator = this.value
node.left = this.type === tt.eq ? this.toAssignable(left) : left
refShorthandDefaultPos.start = 0; // reset because shorthand default was used correctly
this.checkLVal(left)
if (left.parenthesizedExpression) {
if (left.type === "ObjectPattern") {
this.raise(left.start, "You're trying to assign to a parenthesized expression, instead of `({ foo }) = {}` use `({ foo } = {})`");
} else {
this.raise(left.start, "Parenthesized left hand expressions are illegal");
}
}
this.next()
node.right = this.parseMaybeAssign(noIn)
return this.finishNode(node, "AssignmentExpression")
} else if (failOnShorthandAssign && refShorthandDefaultPos.start) {
this.unexpected(refShorthandDefaultPos.start)
}
return left
}
// Parse a ternary conditional (`?:`) operator.
pp.parseMaybeConditional = function(noIn, refShorthandDefaultPos) {
let start = this.markPosition()
let expr = this.parseExprOps(noIn, refShorthandDefaultPos)
if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr
if (this.eat(tt.question)) {
let node = this.startNodeAt(start)
node.test = expr
node.consequent = this.parseMaybeAssign()
this.expect(tt.colon)
node.alternate = this.parseMaybeAssign(noIn)
return this.finishNode(node, "ConditionalExpression")
}
return expr
}
// Start the precedence parser.
pp.parseExprOps = function(noIn, refShorthandDefaultPos) {
let start = this.markPosition()
let expr = this.parseMaybeUnary(refShorthandDefaultPos)
if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr
return this.parseExprOp(expr, start, -1, noIn)
}
// Parse binary operators with the operator precedence parsing
// algorithm. `left` is the left-hand side of the operator.
// `minPrec` provides context that allows the function to stop and
// defer further parser to one of its callers when it encounters an
// operator that has a lower precedence than the set it is parsing.
pp.parseExprOp = function(left, leftStart, minPrec, noIn) {
let prec = this.type.binop
if (prec != null && (!noIn || this.type !== tt._in)) {
if (prec > minPrec) {
let node = this.startNodeAt(leftStart)
node.left = left
node.operator = this.value
let op = this.type
this.next()
let start = this.markPosition()
node.right = this.parseExprOp(this.parseMaybeUnary(), start, op.rightAssociative ? (prec - 1) : prec, noIn)
this.finishNode(node, (op === tt.logicalOR || op === tt.logicalAND) ? "LogicalExpression" : "BinaryExpression")
return this.parseExprOp(node, leftStart, minPrec, noIn)
}
}
return left
}
// Parse unary operators, both prefix and postfix.
pp.parseMaybeUnary = function(refShorthandDefaultPos) {
if (this.type.prefix) {
let node = this.startNode(), update = this.type === tt.incDec
node.operator = this.value
node.prefix = true
this.next()
node.argument = this.parseMaybeUnary()
if (refShorthandDefaultPos && refShorthandDefaultPos.start) this.unexpected(refShorthandDefaultPos.start)
if (update) this.checkLVal(node.argument)
else if (this.strict && node.operator === "delete" &&
node.argument.type === "Identifier")
this.raise(node.start, "Deleting local variable in strict mode")
return this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression")
}
let start = this.markPosition()
let expr = this.parseExprSubscripts(refShorthandDefaultPos)
if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr
while (this.type.postfix && !this.canInsertSemicolon()) {
let node = this.startNodeAt(start)
node.operator = this.value
node.prefix = false
node.argument = expr
this.checkLVal(expr)
this.next()
expr = this.finishNode(node, "UpdateExpression")
}
return expr
}
// Parse call, dot, and `[]`-subscript expressions.
pp.parseExprSubscripts = function(refShorthandDefaultPos) {
let start = this.markPosition()
let expr = this.parseExprAtom(refShorthandDefaultPos)
if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr
return this.parseSubscripts(expr, start)
}
pp.parseSubscripts = function(base, start, noCalls) {
if (this.eat(tt.dot)) {
let node = this.startNodeAt(start)
node.object = base
node.property = this.parseIdent(true)
node.computed = false
return this.parseSubscripts(this.finishNode(node, "MemberExpression"), start, noCalls)
} else if (this.eat(tt.bracketL)) {
let node = this.startNodeAt(start)
node.object = base
node.property = this.parseExpression()
node.computed = true
this.expect(tt.bracketR)
return this.parseSubscripts(this.finishNode(node, "MemberExpression"), start, noCalls)
} else if (!noCalls && this.eat(tt.parenL)) {
let node = this.startNodeAt(start)
node.callee = base
node.arguments = this.parseExprList(tt.parenR, false)
return this.parseSubscripts(this.finishNode(node, "CallExpression"), start, noCalls)
} else if (this.type === tt.backQuote) {
let node = this.startNodeAt(start)
node.tag = base
node.quasi = this.parseTemplate()
return this.parseSubscripts(this.finishNode(node, "TaggedTemplateExpression"), start, noCalls)
} return base
}
// Parse an atomic expression — either a single token that is an
// expression, an expression started by a keyword like `function` or
// `new`, or an expression wrapped in punctuation like `()`, `[]`,
// or `{}`.
pp.parseExprAtom = function(refShorthandDefaultPos) {
let node
switch (this.type) {
case tt._this:
case tt._super:
let type = this.type === tt._this ? "ThisExpression" : "Super"
node = this.startNode()
this.next()
return this.finishNode(node, type)
case tt._yield:
if (this.inGenerator) unexpected()
case tt._do:
if (this.options.features["es7.doExpressions"]) {
let node = this.startNode()
this.next()
node.body = this.parseBlock()
return this.finishNode(node, "DoExpression")
}
case tt.name:
let start = this.markPosition()
node = this.startNode()
let id = this.parseIdent(this.type !== tt.name)
//
if (this.options.features["es7.asyncFunctions"]) {
// async functions!
if (id.name === "async") {
// arrow functions
if (this.type === tt.parenL) {
let expr = this.parseParenAndDistinguishExpression(start, true)
if (expr.type === "ArrowFunctionExpression") {
return expr
} else {
node.callee = id
if (expr.type === "SequenceExpression") {
node.arguments = expr.expressions
} else {
node.arguments = [expr]
}
return this.parseSubscripts(this.finishNode(node, "CallExpression"), start)
}
} else if (this.type === tt.name) {
id = this.parseIdent()
this.expect(tt.arrow)
return this.parseArrowExpression(node, [id], true)
}
// normal functions
if (this.type === tt._function && !this.canInsertSemicolon()) {
this.next()
return this.parseFunction(node, false, false, true)
}
} else if (id.name === "await") {
if (this.inAsync) return this.parseAwait(node)
}
}
//
if (!this.canInsertSemicolon() && this.eat(tt.arrow)) {
return this.parseArrowExpression(this.startNodeAt(start), [id])
}
return id
case tt.regexp:
let value = this.value
node = this.parseLiteral(value.value)
node.regex = {pattern: value.pattern, flags: value.flags}
return node
case tt.num: case tt.string:
return this.parseLiteral(this.value)
case tt._null: case tt._true: case tt._false:
node = this.startNode()
node.value = this.type === tt._null ? null : this.type === tt._true
node.raw = this.type.keyword
this.next()
return this.finishNode(node, "Literal")
case tt.parenL:
return this.parseParenAndDistinguishExpression()
case tt.bracketL:
node = this.startNode()
this.next()
// check whether this is array comprehension or regular array
if ((this.options.ecmaVersion >= 7 || this.options.features["es7.comprehensions"]) && this.type === tt._for) {
return this.parseComprehension(node, false)
}
node.elements = this.parseExprList(tt.bracketR, true, true, refShorthandDefaultPos)
return this.finishNode(node, "ArrayExpression")
case tt.braceL:
return this.parseObj(false, refShorthandDefaultPos)
case tt._function:
node = this.startNode()
this.next()
return this.parseFunction(node, false)
case tt.at:
this.parseDecorators()
case tt._class:
node = this.startNode()
this.takeDecorators(node)
return this.parseClass(node, false)
case tt._new:
return this.parseNew()
case tt.backQuote:
return this.parseTemplate()
default:
this.unexpected()
}
}
pp.parseLiteral = function(value) {
let node = this.startNode()
node.value = value
node.raw = this.input.slice(this.start, this.end)
this.next()
return this.finishNode(node, "Literal")
}
pp.parseParenExpression = function() {
this.expect(tt.parenL)
let val = this.parseExpression()
this.expect(tt.parenR)
return val
}
pp.parseParenAndDistinguishExpression = function(start, isAsync) {
start = start || this.markPosition()
let val
if (this.options.ecmaVersion >= 6) {
this.next()
if ((this.options.features["es7.comprehensions"] || this.options.ecmaVersion >= 7) && this.type === tt._for) {
return this.parseComprehension(this.startNodeAt(start), true)
}
let innerStart = this.markPosition(), exprList = [], first = true
let refShorthandDefaultPos = {start: 0}, spreadStart, innerParenStart
while (this.type !== tt.parenR) {
first ? first = false : this.expect(tt.comma)
if (this.type === tt.ellipsis) {
let spreadNodeStart = this.markPosition()
spreadStart = this.start
exprList.push(this.parseParenItem(this.parseRest(), spreadNodeStart))
break
} else {
if (this.type === tt.parenL && !innerParenStart) {
innerParenStart = this.start
}
exprList.push(this.parseMaybeAssign(false, refShorthandDefaultPos, this.parseParenItem))
}
}
let innerEnd = this.markPosition()
this.expect(tt.parenR)
if (!this.canInsertSemicolon() && this.eat(tt.arrow)) {
if (innerParenStart) this.unexpected(innerParenStart)
return this.parseParenArrowList(start, exprList, isAsync)
}
if (!exprList.length) this.unexpected(this.lastTokStart)
if (spreadStart) this.unexpected(spreadStart)
if (refShorthandDefaultPos.start) this.unexpected(refShorthandDefaultPos.start)
if (exprList.length > 1) {
val = this.startNodeAt(innerStart)
val.expressions = exprList
this.finishNodeAt(val, "SequenceExpression", innerEnd)
} else {
val = exprList[0]
}
} else {
val = this.parseParenExpression()
}
if (this.options.preserveParens) {
let par = this.startNodeAt(start)
par.expression = val
return this.finishNode(par, "ParenthesizedExpression")
} else {
val.parenthesizedExpression = true
return val
}
}
pp.parseParenArrowList = function(start, exprList, isAsync) {
return this.parseArrowExpression(this.startNodeAt(start), exprList, isAsync)
}
pp.parseParenItem = function(node, start) {
return node
}
// New's precedence is slightly tricky. It must allow its argument
// to be a `[]` or dot subscript expression, but not a call — at
// least, not without wrapping it in parentheses. Thus, it uses the
const empty = []
pp.parseNew = function() {
let node = this.startNode()
let meta = this.parseIdent(true)
if (this.options.ecmaVersion >= 6 && this.eat(tt.dot)) {
node.meta = meta
node.property = this.parseIdent(true)
if (node.property.name !== "target")
this.raise(node.property.start, "The only valid meta property for new is new.target")
return this.finishNode(node, "MetaProperty")
}
let start = this.markPosition()
node.callee = this.parseSubscripts(this.parseExprAtom(), start, true)
if (this.eat(tt.parenL)) node.arguments = this.parseExprList(tt.parenR, false)
else node.arguments = empty
return this.finishNode(node, "NewExpression")
}
// Parse template expression.
pp.parseTemplateElement = function() {
let elem = this.startNode()
elem.value = {
raw: this.input.slice(this.start, this.end),
cooked: this.value
}
this.next()
elem.tail = this.type === tt.backQuote
return this.finishNode(elem, "TemplateElement")
}
pp.parseTemplate = function() {
let node = this.startNode()
this.next()
node.expressions = []
let curElt = this.parseTemplateElement()
node.quasis = [curElt]
while (!curElt.tail) {
this.expect(tt.dollarBraceL)
node.expressions.push(this.parseExpression())
this.expect(tt.braceR)
node.quasis.push(curElt = this.parseTemplateElement())
}
this.next()
return this.finishNode(node, "TemplateLiteral")
}
// Parse an object literal or binding pattern.
pp.parseObj = function(isPattern, refShorthandDefaultPos) {
let node = this.startNode(), first = true, propHash = {}
node.properties = []
this.next()
while (!this.eat(tt.braceR)) {
if (!first) {
this.expect(tt.comma)
if (this.afterTrailingComma(tt.braceR)) break
} else first = false
let prop = this.startNode(), isGenerator = false, isAsync = false, start
if (this.options.features["es7.objectRestSpread"] && this.type === tt.ellipsis) {
prop = this.parseSpread()
prop.type = "SpreadProperty"
node.properties.push(prop)
continue
}
if (this.options.ecmaVersion >= 6) {
prop.method = false
prop.shorthand = false
if (isPattern || refShorthandDefaultPos)
start = this.markPosition()
if (!isPattern)
isGenerator = this.eat(tt.star)
}
if (this.options.features["es7.asyncFunctions"] && this.isContextual("async")) {
if (isGenerator || isPattern) this.unexpected()
var asyncId = this.parseIdent()
if (this.type === tt.colon || this.type === tt.parenL) {
prop.key = asyncId
} else {
isAsync = true
this.parsePropertyName(prop)
}
} else {
this.parsePropertyName(prop)
}
this.parseObjPropValue(prop, start, isGenerator, isAsync, isPattern, refShorthandDefaultPos);
this.checkPropClash(prop, propHash)
node.properties.push(this.finishNode(prop, "Property"))
}
return this.finishNode(node, isPattern ? "ObjectPattern" : "ObjectExpression")
}
pp.parseObjPropValue = function (prop, start, isGenerator, isAsync, isPattern, refShorthandDefaultPos) {
if (this.eat(tt.colon)) {
prop.value = isPattern ? this.parseMaybeDefault() : this.parseMaybeAssign(false, refShorthandDefaultPos)
prop.kind = "init"
} else if (this.options.ecmaVersion >= 6 && this.type === tt.parenL) {
if (isPattern) this.unexpected()
prop.kind = "init"
prop.method = true
prop.value = this.parseMethod(isGenerator, isAsync)
} else if (this.options.ecmaVersion >= 5 && !prop.computed && prop.key.type === "Identifier" &&
(prop.key.name === "get" || prop.key.name === "set") &&
(this.type != tt.comma && this.type != tt.braceR)) {
if (isGenerator || isAsync || isPattern) this.unexpected()
prop.kind = prop.key.name
this.parsePropertyName(prop)
prop.value = this.parseMethod(false)
} else if (this.options.ecmaVersion >= 6 && !prop.computed && prop.key.type === "Identifier") {
prop.kind = "init"
if (isPattern) {
if (this.isKeyword(prop.key.name) ||
(this.strict && (reservedWords.strictBind(prop.key.name) || reservedWords.strict(prop.key.name))) ||
(!this.options.allowReserved && this.isReservedWord(prop.key.name)))
this.raise(prop.key.start, "Binding " + prop.key.name)
prop.value = this.parseMaybeDefault(start, prop.key)
} else if (this.type === tt.eq && refShorthandDefaultPos) {
if (!refShorthandDefaultPos.start)
refShorthandDefaultPos.start = this.start
prop.value = this.parseMaybeDefault(start, prop.key)
} else {
prop.value = prop.key
}
prop.shorthand = true
} else this.unexpected()
}
pp.parsePropertyName = function(prop) {
if (this.options.ecmaVersion >= 6) {
if (this.eat(tt.bracketL)) {
prop.computed = true
prop.key = this.parseMaybeAssign()
this.expect(tt.bracketR)
return
} else {
prop.computed = false
}
}
prop.key = (this.type === tt.num || this.type === tt.string) ? this.parseExprAtom() : this.parseIdent(true)
}
// Initialize empty function node.
pp.initFunction = function(node, isAsync) {
node.id = null
if (this.options.ecmaVersion >= 6) {
node.generator = false
node.expression = false
}
if (this.options.features["es7.asyncFunctions"]) {
node.async = !!isAsync
}
}
// Parse object or class method.
pp.parseMethod = function(isGenerator, isAsync) {
let node = this.startNode()
this.initFunction(node, isAsync)
this.expect(tt.parenL)
node.params = this.parseBindingList(tt.parenR, false, false)
if (this.options.ecmaVersion >= 6) {
node.generator = isGenerator
}
this.parseFunctionBody(node)
return this.finishNode(node, "FunctionExpression")
}
// Parse arrow function expression with given parameters.
pp.parseArrowExpression = function(node, params, isAsync) {
this.initFunction(node, isAsync)
node.params = this.toAssignableList(params, true)
this.parseFunctionBody(node, true)
return this.finishNode(node, "ArrowFunctionExpression")
}
// Parse function body and check parameters.
pp.parseFunctionBody = function(node, allowExpression) {
let isExpression = allowExpression && this.type !== tt.braceL
var oldInAsync = this.inAsync
this.inAsync = node.async
if (isExpression) {
node.body = this.parseMaybeAssign()
node.expression = true
} else {
// Start a new scope with regard to labels and the `inFunction`
// flag (restore them to their old value afterwards).
let oldInFunc = this.inFunction, oldInGen = this.inGenerator, oldLabels = this.labels
this.inFunction = true; this.inGenerator = node.generator; this.labels = []
node.body = this.parseBlock(true)
node.expression = false
this.inFunction = oldInFunc; this.inGenerator = oldInGen; this.labels = oldLabels
}
this.inAsync = oldInAsync
// If this is a strict mode function, verify that argument names
// are not repeated, and it does not try to bind the words `eval`
// or `arguments`.
if (this.strict || !isExpression && node.body.body.length && this.isUseStrict(node.body.body[0])) {
let nameHash = {}, oldStrict = this.strict
this.strict = true
if (node.id)
this.checkLVal(node.id, true)
for (let i = 0; i < node.params.length; i++)
this.checkLVal(node.params[i], true, nameHash)
this.strict = oldStrict
}
}
// Parses a comma-separated list of expressions, and returns them as
// an array. `close` is the token type that ends the list, and
// `allowEmpty` can be turned on to allow subsequent commas with
// nothing in between them to be parsed as `null` (which is needed
// for array literals).
pp.parseExprList = function(close, allowTrailingComma, allowEmpty, refShorthandDefaultPos) {
let elts = [], first = true
while (!this.eat(close)) {
if (!first) {
this.expect(tt.comma)
if (allowTrailingComma && this.afterTrailingComma(close)) break
} else first = false
if (allowEmpty && this.type === tt.comma) {
elts.push(null)
} else {
if (this.type === tt.ellipsis)
elts.push(this.parseSpread(refShorthandDefaultPos))
else
elts.push(this.parseMaybeAssign(false, refShorthandDefaultPos))
}
}
return elts
}
// Parse the next token as an identifier. If `liberal` is true (used
// when parsing properties), it will also convert keywords into
// identifiers.
pp.parseIdent = function(liberal) {
let node = this.startNode()
if (liberal && this.options.allowReserved == "never") liberal = false
if (this.type === tt.name) {
if (!liberal &&
((!this.options.allowReserved && this.isReservedWord(this.value)) ||
(this.strict && reservedWords.strict(this.value)) &&
(this.options.ecmaVersion >= 6 ||
this.input.slice(this.start, this.end).indexOf("\\") == -1)))
this.raise(this.start, "The keyword '" + this.value + "' is reserved")
node.name = this.value
} else if (liberal && this.type.keyword) {
node.name = this.type.keyword
} else {
this.unexpected()
}
this.next()
return this.finishNode(node, "Identifier")
}
// Parses await expression inside async function.
pp.parseAwait = function (node) {
if (this.eat(tt.semi) || this.canInsertSemicolon()) {
this.unexpected()
}
node.all = this.eat(tt.star)
node.argument = this.parseMaybeAssign(true)
return this.finishNode(node, "AwaitExpression")
};
// Parses yield expression inside generator.
pp.parseYield = function() {
let node = this.startNode()
this.next()
if (this.type == tt.semi || this.canInsertSemicolon() || (this.type != tt.star && !this.type.startsExpr)) {
node.delegate = false
node.argument = null
} else {
node.delegate = this.eat(tt.star)
node.argument = this.parseMaybeAssign()
}
return this.finishNode(node, "YieldExpression")
}
// Parses array and generator comprehensions.
pp.parseComprehension = function(node, isGenerator) {
node.blocks = []
while (this.type === tt._for) {
let block = this.startNode()
this.next()
this.expect(tt.parenL)
block.left = this.parseBindingAtom()
this.checkLVal(block.left, true)
this.expectContextual("of")
block.right = this.parseExpression()
this.expect(tt.parenR)
node.blocks.push(this.finishNode(block, "ComprehensionBlock"))
}
node.filter = this.eat(tt._if) ? this.parseParenExpression() : null
node.body = this.parseExpression()
this.expect(isGenerator ? tt.parenR : tt.bracketR)
node.generator = isGenerator
return this.finishNode(node, "ComprehensionExpression")
}

129
src/acorn/src/identifier.js Executable file
View File

@@ -0,0 +1,129 @@
// This is a trick taken from Esprima. It turns out that, on
// non-Chrome browsers, to check whether a string is in a set, a
// predicate containing a big ugly `switch` statement is faster than
// a regular expression, and on Chrome the two are about on par.
// This function uses `eval` (non-lexical) to produce such a
// predicate from a space-separated string of words.
//
// It starts by sorting the words by length.
function makePredicate(words) {
words = words.split(" ")
let f = "", cats = []
out: for (let i = 0; i < words.length; ++i) {
for (let j = 0; j < cats.length; ++j)
if (cats[j][0].length == words[i].length) {
cats[j].push(words[i])
continue out
}
cats.push([words[i]])
}
function compareTo(arr) {
if (arr.length == 1) return f += "return str === " + JSON.stringify(arr[0]) + ";"
f += "switch(str){"
for (let i = 0; i < arr.length; ++i) f += "case " + JSON.stringify(arr[i]) + ":"
f += "return true}return false;"
}
// When there are more than three length categories, an outer
// switch first dispatches on the lengths, to save on comparisons.
if (cats.length > 3) {
cats.sort((a, b) => b.length - a.length)
f += "switch(str.length){"
for (let i = 0; i < cats.length; ++i) {
let cat = cats[i]
f += "case " + cat[0].length + ":"
compareTo(cat)
}
f += "}"
// Otherwise, simply generate a flat `switch` statement.
} else {
compareTo(words)
}
return new Function("str", f)
}
// Reserved word lists for various dialects of the language
export const reservedWords = {
3: makePredicate("abstract boolean byte char class double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized throws transient volatile"),
5: makePredicate("class enum extends super const export import"),
6: makePredicate("enum await"),
strict: makePredicate("implements interface let package private protected public static yield"),
strictBind: makePredicate("eval arguments")
}
// And the keywords
var ecma5AndLessKeywords = "break case catch continue debugger default do else finally for function if return switch throw try var while with null true false instanceof typeof void delete new in this"
export const keywords = {
5: makePredicate(ecma5AndLessKeywords),
6: makePredicate(ecma5AndLessKeywords + " let const class extends export import yield super")
}
// ## Character categories
// Big ugly regular expressions that match characters in the
// whitespace, identifier, and identifier-start categories. These
// are only applied when a character is found to actually have a
// code point above 128.
// Generated by `tools/generate-identifier-regex.js`.
let nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0561-\u0587\u05d0-\u05ea\u05f0-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u08a0-\u08b2\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58\u0c59\u0c60\u0c61\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d60\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e87\u0e88\u0e8a\u0e8d\u0e94-\u0e97\u0e99-\u0e9f\u0ea1-\u0ea3\u0ea5\u0ea7\u0eaa\u0eab\u0ead-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f4\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f8\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1877\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19c1-\u19c7\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1ce9-\u1cec\u1cee-\u1cf1\u1cf5\u1cf6\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312d\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fcc\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua78e\ua790-\ua7ad\ua7b0\ua7b1\ua7f7-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\ua9e0-\ua9e4\ua9e6-\ua9ef\ua9fa-\ua9fe\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa7e-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uab30-\uab5a\uab5c-\uab5f\uab64\uab65\uabc0-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc"
let nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u08e4-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b56\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c03\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0d01-\u0d03\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d82\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0eb9\u0ebb\u0ebc\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19b0-\u19c0\u19c8\u19c9\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf2-\u1cf4\u1cf8\u1cf9\u1dc0-\u1df5\u1dfc-\u1dff\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua620-\ua629\ua66f\ua674-\ua67d\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\ua880\ua881\ua8b4-\ua8c4\ua8d0-\ua8d9\ua8e0-\ua8f1\ua900-\ua909\ua926-\ua92d\ua947-\ua953\ua980-\ua983\ua9b3-\ua9c0\ua9d0-\ua9d9\ua9e5\ua9f0-\ua9f9\uaa29-\uaa36\uaa43\uaa4c\uaa4d\uaa50-\uaa59\uaa7b-\uaa7d\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uaaeb-\uaaef\uaaf5\uaaf6\uabe3-\uabea\uabec\uabed\uabf0-\uabf9\ufb1e\ufe00-\ufe0f\ufe20-\ufe2d\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f"
const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]")
const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]")
nonASCIIidentifierStartChars = nonASCIIidentifierChars = null
// These are a run-length and offset encoded representation of the
// >0xffff code points that are a valid part of identifiers. The
// offset starts at 0x10000, and each pair of numbers represents an
// offset to the next range, and then a size of the range. They were
// generated by tools/generate-identifier-regex.js
var astralIdentifierStartCodes = [0,11,2,25,2,18,2,1,2,14,3,13,35,122,70,52,268,28,4,48,48,31,17,26,6,37,11,29,3,35,5,7,2,4,43,157,99,39,9,51,157,310,10,21,11,7,153,5,3,0,2,43,2,1,4,0,3,22,11,22,10,30,98,21,11,25,71,55,7,1,65,0,16,3,2,2,2,26,45,28,4,28,36,7,2,27,28,53,11,21,11,18,14,17,111,72,955,52,76,44,33,24,27,35,42,34,4,0,13,47,15,3,22,0,38,17,2,24,133,46,39,7,3,1,3,21,2,6,2,1,2,4,4,0,32,4,287,47,21,1,2,0,185,46,82,47,21,0,60,42,502,63,32,0,449,56,1288,920,104,110,2962,1070,13266,568,8,30,114,29,19,47,17,3,32,20,6,18,881,68,12,0,67,12,16481,1,3071,106,6,12,4,8,8,9,5991,84,2,70,2,1,3,0,3,1,3,3,2,11,2,0,2,6,2,64,2,3,3,7,2,6,2,27,2,3,2,4,2,0,4,6,2,339,3,24,2,24,2,30,2,24,2,30,2,24,2,30,2,24,2,30,2,24,2,7,4149,196,1340,3,2,26,2,1,2,0,3,0,2,9,2,3,2,0,2,0,7,0,5,0,2,0,2,0,2,2,2,1,2,0,3,0,2,0,2,0,2,0,2,0,2,1,2,0,3,3,2,6,2,3,2,3,2,0,2,9,2,16,6,2,2,4,2,16,4421,42710,42,4148,12,221,16355,541]
var astralIdentifierCodes = [509,0,227,0,150,4,294,9,1368,2,2,1,6,3,41,2,5,0,166,1,1306,2,54,14,32,9,16,3,46,10,54,9,7,2,37,13,2,9,52,0,13,2,49,13,16,9,83,11,168,11,6,9,8,2,57,0,2,6,3,1,3,2,10,0,11,1,3,6,4,4,316,19,13,9,214,6,3,8,112,16,16,9,82,12,9,9,535,9,20855,9,135,4,60,6,26,9,1016,45,17,3,19723,1,5319,4,4,5,9,7,3,6,31,3,149,2,1418,49,4305,6,792618,239]
// This has a complexity linear to the value of the code. The
// assumption is that looking up astral identifier characters is
// rare.
function isInAstralSet(code, set) {
let pos = 0x10000
for (let i = 0; i < set.length; i += 2) {
pos += set[i]
if (pos > code) return false
pos += set[i + 1]
if (pos >= code) return true
}
}
// Test whether a given character code starts an identifier.
export function isIdentifierStart(code, astral) {
if (code < 65) return code === 36
if (code < 91) return true
if (code < 97) return code === 95
if (code < 123) return true
if (code <= 0xffff) return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code))
if (astral === false) return false
return isInAstralSet(code, astralIdentifierStartCodes)
}
// Test whether a given character is part of an identifier.
export function isIdentifierChar(code, astral) {
if (code < 48) return code === 36
if (code < 58) return true
if (code < 65) return false
if (code < 91) return true
if (code < 97) return code === 95
if (code < 123) return true
if (code <= 0xffff) return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code))
if (astral === false) return false
return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes)
}

82
src/acorn/src/index.js Executable file
View File

@@ -0,0 +1,82 @@
// Acorn is a tiny, fast JavaScript parser written in JavaScript.
//
// Acorn was written by Marijn Haverbeke, Ingvar Stepanyan, and
// various contributors and released under an MIT license.
//
// Git repositories for Acorn are available at
//
// http://marijnhaverbeke.nl/git/acorn
// https://github.com/marijnh/acorn.git
//
// Please use the [github bug tracker][ghbt] to report issues.
//
// [ghbt]: https://github.com/marijnh/acorn/issues
//
// This file defines the main parser interface. The library also comes
// with a [error-tolerant parser][dammit] and an
// [abstract syntax tree walker][walk], defined in other files.
//
// [dammit]: acorn_loose.js
// [walk]: util/walk.js
import {Parser} from "./state"
import {getOptions} from "./options"
import "./parseutil"
import "./statement"
import "./lval"
import "./expression"
import "./lookahead"
export {Parser, plugins} from "./state"
export {defaultOptions} from "./options"
export {SourceLocation} from "./location"
export {getLineInfo} from "./location"
export {Node} from "./node"
export {TokenType, types as tokTypes} from "./tokentype"
export {TokContext, types as tokContexts} from "./tokencontext"
export {isIdentifierChar, isIdentifierStart} from "./identifier"
export {Token} from "./tokenize"
export {isNewLine, lineBreak, lineBreakG} from "./whitespace"
import "../plugins/flow";
import "../plugins/jsx";
export const version = "1.0.0"
// The main exported interface (under `self.acorn` when in the
// browser) is a `parse` function that takes a code string and
// returns an abstract syntax tree as specified by [Mozilla parser
// API][api].
//
// [api]: https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API
export function parse(input, options) {
let p = parser(options, input)
let startPos = p.options.locations ? [p.pos, p.curPosition()] : p.pos
p.nextToken()
return p.parseTopLevel(p.options.program || p.startNodeAt(startPos))
}
// This function tries to parse a single expression at a given
// offset in a string. Useful for parsing mixed-language formats
// that embed JavaScript expressions.
export function parseExpressionAt(input, pos, options) {
let p = parser(options, input, pos)
p.nextToken()
return p.parseExpression()
}
// Acorn is organized as a tokenizer and a recursive-descent parser.
// The `tokenize` export provides an interface to the tokenizer.
// Because the tokenizer is optimized for being efficiently used by
// the Acorn parser itself, this interface is somewhat crude and not
// very modular.
export function tokenizer(input, options) {
return parser(options, input)
}
function parser(options, input) {
return new Parser(getOptions(options), String(input))
}

67
src/acorn/src/location.js Executable file
View File

@@ -0,0 +1,67 @@
import {Parser} from "./state"
import {lineBreakG} from "./whitespace"
// These are used when `options.locations` is on, for the
// `startLoc` and `endLoc` properties.
export class Position {
constructor(line, col) {
this.line = line
this.column = col
}
offset(n) {
return new Position(this.line, this.column + n)
}
}
export class SourceLocation {
constructor(p, start, end) {
this.start = start
this.end = end
if (p.sourceFile !== null) this.source = p.sourceFile
}
}
// The `getLineInfo` function is mostly useful when the
// `locations` option is off (for performance reasons) and you
// want to find the line/column position for a given character
// offset. `input` should be the code string that the offset refers
// into.
export function getLineInfo(input, offset) {
for (let line = 1, cur = 0;;) {
lineBreakG.lastIndex = cur
let match = lineBreakG.exec(input)
if (match && match.index < offset) {
++line
cur = match.index + match[0].length
} else {
return new Position(line, offset - cur)
}
}
}
const pp = Parser.prototype
// This function is used to raise exceptions on parse errors. It
// takes an offset integer (into the current `input`) to indicate
// the location of the error, attaches the position to the end
// of the error message, and then raises a `SyntaxError` with that
// message.
pp.raise = function(pos, message) {
let loc = getLineInfo(this.input, pos)
message += " (" + loc.line + ":" + loc.column + ")"
let err = new SyntaxError(message)
err.pos = pos; err.loc = loc; err.raisedAt = this.pos
throw err
}
pp.curPosition = function() {
return new Position(this.curLine, this.pos - this.lineStart)
}
pp.markPosition = function() {
return this.options.locations ? [this.start, this.startLoc] : this.start
}

View File

@@ -0,0 +1,35 @@
import {Parser} from "./state"
const pp = Parser.prototype
var STATE_KEYS = [
"lastTokStartLoc",
"lastTokEndLoc",
"lastTokStart",
"lastTokEnd",
"lineStart",
"startLoc",
"endLoc",
"start",
"pos",
"end",
"type",
"value"
];
pp.getState = function () {
var state = {};
for (var i = 0; i < STATE_KEYS.length; i++) {
var key = STATE_KEYS[i];
state[key] = this[key];
}
return state;
};
pp.lookahead = function() {
var old = this.getState();
this.next();
var curr = this.getState();
for (var key in old) this[key] = old[key];
return curr;
};

197
src/acorn/src/lval.js Executable file
View File

@@ -0,0 +1,197 @@
import {types as tt} from "./tokentype"
import {Parser} from "./state"
import {reservedWords} from "./identifier"
import {has} from "./util"
const pp = Parser.prototype
// Convert existing expression atom to assignable pattern
// if possible.
pp.toAssignable = function(node, isBinding) {
if (this.options.ecmaVersion >= 6 && node) {
switch (node.type) {
case "Identifier":
case "ObjectPattern":
case "ArrayPattern":
case "AssignmentPattern":
break
case "ObjectExpression":
node.type = "ObjectPattern"
for (let i = 0; i < node.properties.length; i++) {
let prop = node.properties[i]
if (prop.kind !== "init") this.raise(prop.key.start, "Object pattern can't contain getter or setter")
this.toAssignable(prop.value, isBinding)
}
break
case "ArrayExpression":
node.type = "ArrayPattern"
this.toAssignableList(node.elements, isBinding)
break
case "AssignmentExpression":
if (node.operator === "=") {
node.type = "AssignmentPattern"
} else {
this.raise(node.left.end, "Only '=' operator can be used for specifying default value.")
}
break
case "MemberExpression":
if (!isBinding) break
default:
this.raise(node.start, "Assigning to rvalue")
}
}
return node
}
// Convert list of expression atoms to binding list.
pp.toAssignableList = function(exprList, isBinding) {
let end = exprList.length
if (end) {
let last = exprList[end - 1]
if (last && last.type == "RestElement") {
--end
} else if (last && last.type == "SpreadElement") {
last.type = "RestElement"
let arg = last.argument
this.toAssignable(arg, isBinding)
if (arg.type !== "Identifier" && arg.type !== "MemberExpression" && arg.type !== "ArrayPattern")
this.unexpected(arg.start)
--end
}
}
for (let i = 0; i < end; i++) {
let elt = exprList[i]
if (elt) this.toAssignable(elt, isBinding)
}
return exprList
}
// Parses spread element.
pp.parseSpread = function(refShorthandDefaultPos) {
let node = this.startNode()
this.next()
node.argument = this.parseMaybeAssign(refShorthandDefaultPos)
return this.finishNode(node, "SpreadElement")
}
pp.parseRest = function() {
let node = this.startNode()
this.next()
node.argument = this.type === tt.name || this.type === tt.bracketL ? this.parseBindingAtom() : this.unexpected()
return this.finishNode(node, "RestElement")
}
// Parses lvalue (assignable) atom.
pp.parseBindingAtom = function() {
if (this.options.ecmaVersion < 6) return this.parseIdent()
switch (this.type) {
case tt.name:
return this.parseIdent()
case tt.bracketL:
let node = this.startNode()
this.next()
node.elements = this.parseBindingList(tt.bracketR, true, true)
return this.finishNode(node, "ArrayPattern")
case tt.braceL:
return this.parseObj(true)
default:
this.unexpected()
}
}
pp.parseBindingList = function(close, allowEmpty, allowTrailingComma) {
var elts = [], first = true
while (!this.eat(close)) {
if (first) first = false
else this.expect(tt.comma)
if (allowEmpty && this.type === tt.comma) {
elts.push(null)
} else if (allowTrailingComma && this.afterTrailingComma(close)) {
break
} else if (this.type === tt.ellipsis) {
elts.push(this.parseAssignableListItemTypes(this.parseRest()))
this.expect(close)
break
} else {
elts.push(this.parseAssignableListItemTypes(this.parseMaybeDefault()))
}
}
return elts
}
pp.parseAssignableListItemTypes = function(param) {
return param
}
// Parses assignment pattern around given atom if possible.
pp.parseMaybeDefault = function(startPos, left) {
startPos = startPos || this.markPosition()
left = left || this.parseBindingAtom()
if (!this.eat(tt.eq)) return left
let node = this.startNodeAt(startPos)
node.operator = "="
node.left = left
node.right = this.parseMaybeAssign()
return this.finishNode(node, "AssignmentPattern")
}
// Verify that a node is an lval — something that can be assigned
// to.
pp.checkLVal = function(expr, isBinding, checkClashes) {
switch (expr.type) {
case "Identifier":
if (this.strict && (reservedWords.strictBind(expr.name) || reservedWords.strict(expr.name)))
this.raise(expr.start, (isBinding ? "Binding " : "Assigning to ") + expr.name + " in strict mode")
if (checkClashes) {
if (has(checkClashes, expr.name))
this.raise(expr.start, "Argument name clash in strict mode")
checkClashes[expr.name] = true
}
break
case "MemberExpression":
if (isBinding) this.raise(expr.start, (isBinding ? "Binding" : "Assigning to") + " member expression")
break
case "ObjectPattern":
for (let i = 0; i < expr.properties.length; i++) {
var prop = expr.properties[i];
if (prop.type === "Property") prop = prop.value;
this.checkLVal(prop, isBinding, checkClashes)
}
break
case "ArrayPattern":
for (let i = 0; i < expr.elements.length; i++) {
let elem = expr.elements[i]
if (elem) this.checkLVal(elem, isBinding, checkClashes)
}
break
case "AssignmentPattern":
this.checkLVal(expr.left, isBinding, checkClashes)
break
case "SpreadProperty":
case "RestElement":
this.checkLVal(expr.argument, isBinding, checkClashes)
break
default:
this.raise(expr.start, (isBinding ? "Binding" : "Assigning to") + " rvalue")
}
}

57
src/acorn/src/node.js Executable file
View File

@@ -0,0 +1,57 @@
import {Parser} from "./state"
import {SourceLocation} from "./location"
// Start an AST node, attaching a start offset.
const pp = Parser.prototype
export class Node {}
pp.startNode = function() {
let node = new Node
node.start = this.start
if (this.options.locations)
node.loc = new SourceLocation(this, this.startLoc)
if (this.options.directSourceFile)
node.sourceFile = this.options.directSourceFile
if (this.options.ranges)
node.range = [this.start, 0]
return node
}
pp.startNodeAt = function(pos) {
let node = new Node, start = pos
if (this.options.locations) {
node.loc = new SourceLocation(this, start[1])
start = pos[0]
}
node.start = start
if (this.options.directSourceFile)
node.sourceFile = this.options.directSourceFile
if (this.options.ranges)
node.range = [start, 0]
return node
}
// Finish an AST node, adding `type` and `end` properties.
pp.finishNode = function(node, type) {
node.type = type
node.end = this.lastTokEnd
if (this.options.locations)
node.loc.end = this.lastTokEndLoc
if (this.options.ranges)
node.range[1] = this.lastTokEnd
return node
}
// Finish node at given position
pp.finishNodeAt = function(node, type, pos) {
if (this.options.locations) { node.loc.end = pos[1]; pos = pos[0] }
node.type = type
node.end = pos
if (this.options.ranges)
node.range[1] = pos
return node
}

122
src/acorn/src/options.js Executable file
View File

@@ -0,0 +1,122 @@
import {has, isArray} from "./util"
import {SourceLocation} from "./location"
// A second optional argument can be given to further configure
// the parser process. These options are recognized:
export const defaultOptions = {
// `ecmaVersion` indicates the ECMAScript version to parse. Must
// be either 3, or 5, or 6. This influences support for strict
// mode, the set of reserved words, support for getters and
// setters and other features.
ecmaVersion: 5,
// Source type ("script" or "module") for different semantics
sourceType: "script",
// `onInsertedSemicolon` can be a callback that will be called
// when a semicolon is automatically inserted. It will be passed
// th position of the comma as an offset, and if `locations` is
// enabled, it is given the location as a `{line, column}` object
// as second argument.
onInsertedSemicolon: null,
// `onTrailingComma` is similar to `onInsertedSemicolon`, but for
// trailing commas.
onTrailingComma: null,
// By default, reserved words are not enforced. Disable
// `allowReserved` to enforce them. When this option has the
// value "never", reserved words and keywords can also not be
// used as property names.
allowReserved: true,
// When enabled, a return at the top level is not considered an
// error.
allowReturnOutsideFunction: false,
// When enabled, import/export statements are not constrained to
// appearing at the top of the program.
allowImportExportEverywhere: false,
// When enabled, hashbang directive in the beginning of file
// is allowed and treated as a line comment.
allowHashBang: false,
// When `locations` is on, `loc` properties holding objects with
// `start` and `end` properties in `{line, column}` form (with
// line being 1-based and column 0-based) will be attached to the
// nodes.
locations: false,
// A function can be passed as `onToken` option, which will
// cause Acorn to call that function with object in the same
// format as tokenize() returns. Note that you are not
// allowed to call the parser from the callback—that will
// corrupt its internal state.
onToken: null,
// A function can be passed as `onComment` option, which will
// cause Acorn to call that function with `(block, text, start,
// end)` parameters whenever a comment is skipped. `block` is a
// boolean indicating whether this is a block (`/* */`) comment,
// `text` is the content of the comment, and `start` and `end` are
// character offsets that denote the start and end of the comment.
// When the `locations` option is on, two more parameters are
// passed, the full `{line, column}` locations of the start and
// end of the comments. Note that you are not allowed to call the
// parser from the callback—that will corrupt its internal state.
onComment: null,
// Nodes have their start and end characters offsets recorded in
// `start` and `end` properties (directly on the node, rather than
// the `loc` object, which holds line/column data. To also add a
// [semi-standardized][range] `range` property holding a `[start,
// end]` array with the same numbers, set the `ranges` option to
// `true`.
//
// [range]: https://bugzilla.mozilla.org/show_bug.cgi?id=745678
ranges: false,
// It is possible to parse multiple files into a single AST by
// passing the tree produced by parsing the first file as
// `program` option in subsequent parses. This will add the
// toplevel forms of the parsed file to the `Program` (top) node
// of an existing parse tree.
program: null,
// When `locations` is on, you can pass this to record the source
// file in every node's `loc` object.
sourceFile: null,
// This value, if given, is stored in every node, whether
// `locations` is on or off.
directSourceFile: null,
// When enabled, parenthesized expressions are represented by
// (non-standard) ParenthesizedExpression nodes
preserveParens: false,
plugins: {},
// Babel-specific options
features: {},
strictMode: null
}
// Interpret and default an options object
export function getOptions(opts) {
let options = {}
for (let opt in defaultOptions)
options[opt] = opts && has(opts, opt) ? opts[opt] : defaultOptions[opt]
if (isArray(options.onToken)) {
let tokens = options.onToken
options.onToken = (token) => tokens.push(token)
}
if (isArray(options.onComment))
options.onComment = pushComment(options, options.onComment)
return options
}
function pushComment(options, array) {
return function (block, text, start, end, startLoc, endLoc) {
let comment = {
type: block ? 'Block' : 'Line',
value: text,
start: start,
end: end
}
if (options.locations)
comment.loc = new SourceLocation(this, startLoc, endLoc)
if (options.ranges)
comment.range = [start, end]
array.push(comment)
}
}

89
src/acorn/src/parseutil.js Executable file
View File

@@ -0,0 +1,89 @@
import {types as tt} from "./tokentype"
import {Parser} from "./state"
import {lineBreak} from "./whitespace"
const pp = Parser.prototype
// ## Parser utilities
// Test whether a statement node is the string literal `"use strict"`.
pp.isUseStrict = function(stmt) {
return this.options.ecmaVersion >= 5 && stmt.type === "ExpressionStatement" &&
stmt.expression.type === "Literal" && stmt.expression.value === "use strict"
}
// Predicate that tests whether the next token is of the given
// type, and if yes, consumes it as a side effect.
pp.eat = function(type) {
if (this.type === type) {
this.next()
return true
} else {
return false
}
}
// Tests whether parsed token is a contextual keyword.
pp.isContextual = function(name) {
return this.type === tt.name && this.value === name
}
// Consumes contextual keyword if possible.
pp.eatContextual = function(name) {
return this.value === name && this.eat(tt.name)
}
// Asserts that following token is given contextual keyword.
pp.expectContextual = function(name) {
if (!this.eatContextual(name)) this.unexpected()
}
// Test whether a semicolon can be inserted at the current position.
pp.canInsertSemicolon = function() {
return this.type === tt.eof ||
this.type === tt.braceR ||
lineBreak.test(this.input.slice(this.lastTokEnd, this.start))
}
pp.insertSemicolon = function() {
if (this.canInsertSemicolon()) {
if (this.options.onInsertedSemicolon)
this.options.onInsertedSemicolon(this.lastTokEnd, this.lastTokEndLoc)
return true
}
}
// Consume a semicolon, or, failing that, see if we are allowed to
// pretend that there is a semicolon at this position.
pp.semicolon = function() {
if (!this.eat(tt.semi) && !this.insertSemicolon()) this.unexpected()
}
pp.afterTrailingComma = function(tokType) {
if (this.type == tokType) {
if (this.options.onTrailingComma)
this.options.onTrailingComma(this.lastTokStart, this.lastTokStartLoc)
this.next()
return true
}
}
// Expect a token of a given type. If found, consume it, otherwise,
// raise an unexpected token error.
pp.expect = function(type) {
this.eat(type) || this.unexpected()
}
// Raise an unexpected token error.
pp.unexpected = function(pos) {
this.raise(pos != null ? pos : this.start, "Unexpected token")
}

75
src/acorn/src/state.js Executable file
View File

@@ -0,0 +1,75 @@
import {reservedWords, keywords} from "./identifier"
import {types as tt, lineBreak} from "./tokentype"
export function Parser(options, input, startPos) {
this.options = options
this.loadPlugins(this.options.plugins)
this.sourceFile = this.options.sourceFile || null
this.isKeyword = keywords[this.options.ecmaVersion >= 6 ? 6 : 5]
this.isReservedWord = reservedWords[this.options.ecmaVersion]
this.input = input
// Set up token state
// The current position of the tokenizer in the input.
if (startPos) {
this.pos = startPos
this.lineStart = Math.max(0, this.input.lastIndexOf("\n", startPos))
this.curLine = this.input.slice(0, this.lineStart).split(lineBreak).length
} else {
this.pos = this.lineStart = 0
this.curLine = 1
}
// Properties of the current token:
// Its type
this.type = tt.eof
// For tokens that include more information than their type, the value
this.value = null
// Its start and end offset
this.start = this.end = this.pos
// And, if locations are used, the {line, column} object
// corresponding to those offsets
this.startLoc = this.endLoc = null
// Position information for the previous token
this.lastTokEndLoc = this.lastTokStartLoc = null
this.lastTokStart = this.lastTokEnd = this.pos
// The context stack is used to superficially track syntactic
// context to predict whether a regular expression is allowed in a
// given position.
this.context = this.initialContext()
this.exprAllowed = true
// Figure out if it's a module code.
this.inModule = this.options.sourceType === "module"
this.strict = this.options.strictMode === false ? false : this.inModule
// Flags to track whether we are in a function, a generator.
this.inFunction = this.inGenerator = false
// Labels in scope.
this.labels = []
this.decorators = []
// If enabled, skip leading hashbang line.
if (this.pos === 0 && this.options.allowHashBang && this.input.slice(0, 2) === '#!')
this.skipLineComment(2)
}
Parser.prototype.extend = function(name, f) {
this[name] = f(this[name])
}
// Registered plugins
export const plugins = {}
Parser.prototype.loadPlugins = function(plugins) {
for (let name in plugins) {
let plugin = exports.plugins[name]
if (!plugin) throw new Error("Plugin '" + name + "' not found")
plugin(this, plugins[name])
}
}

696
src/acorn/src/statement.js Executable file
View File

@@ -0,0 +1,696 @@
import {types as tt} from "./tokentype"
import {Parser} from "./state"
import {lineBreak} from "./whitespace"
const pp = Parser.prototype
// ### Statement parsing
// Parse a program. Initializes the parser, reads any number of
// statements, and wraps them in a Program node. Optionally takes a
// `program` argument. If present, the statements will be appended
// to its body instead of creating a new node.
pp.parseTopLevel = function(node) {
let first = true
if (!node.body) node.body = []
while (this.type !== tt.eof) {
let stmt = this.parseStatement(true, true)
node.body.push(stmt)
if (first && this.isUseStrict(stmt)) this.setStrict(true)
first = false
}
this.next()
if (this.options.ecmaVersion >= 6) {
node.sourceType = this.options.sourceType
}
return this.finishNode(node, "Program")
}
const loopLabel = {kind: "loop"}, switchLabel = {kind: "switch"}
// Parse a single statement.
//
// If expecting a statement and finding a slash operator, parse a
// regular expression literal. This is to handle cases like
// `if (foo) /blah/.exec(foo)`, where looking at the previous token
// does not help.
pp.parseStatement = function(declaration, topLevel) {
if (this.type === tt.at) {
this.parseDecorators(true)
}
let starttype = this.type, node = this.startNode()
// Most types of statements are recognized by the keyword they
// start with. Many are trivial to parse, some require a bit of
// complexity.
switch (starttype) {
case tt._break: case tt._continue: return this.parseBreakContinueStatement(node, starttype.keyword)
case tt._debugger: return this.parseDebuggerStatement(node)
case tt._do: return this.parseDoStatement(node)
case tt._for: return this.parseForStatement(node)
case tt._function:
if (!declaration && this.options.ecmaVersion >= 6) this.unexpected()
return this.parseFunctionStatement(node)
case tt._class:
if (!declaration) this.unexpected()
this.takeDecorators(node)
return this.parseClass(node, true)
case tt._if: return this.parseIfStatement(node)
case tt._return: return this.parseReturnStatement(node)
case tt._switch: return this.parseSwitchStatement(node)
case tt._throw: return this.parseThrowStatement(node)
case tt._try: return this.parseTryStatement(node)
case tt._let: case tt._const: if (!declaration) this.unexpected() // NOTE: falls through to _var
case tt._var: return this.parseVarStatement(node, starttype)
case tt._while: return this.parseWhileStatement(node)
case tt._with: return this.parseWithStatement(node)
case tt.braceL: return this.parseBlock()
case tt.semi: return this.parseEmptyStatement(node)
case tt._export:
case tt._import:
if (!this.options.allowImportExportEverywhere) {
if (!topLevel)
this.raise(this.start, "'import' and 'export' may only appear at the top level")
if (!this.inModule)
this.raise(this.start, "'import' and 'export' may appear only with 'sourceType: module'")
}
return starttype === tt._import ? this.parseImport(node) : this.parseExport(node)
case tt.name:
if (this.options.features["es7.asyncFunctions"] && this.value === "async" && this.lookahead().type === tt._function) {
this.next();
this.expect(tt._function);
return this.parseFunction(node, true, false, true);
}
// If the statement does not start with a statement keyword or a
// brace, it's an ExpressionStatement or LabeledStatement. We
// simply start parsing an expression, and afterwards, if the
// next token is a colon and the expression was a simple
// Identifier node, we switch to interpreting it as a label.
default:
let maybeName = this.value, expr = this.parseExpression()
if (starttype === tt.name && expr.type === "Identifier" && this.eat(tt.colon))
return this.parseLabeledStatement(node, maybeName, expr)
else return this.parseExpressionStatement(node, expr)
}
}
pp.takeDecorators = function(node) {
if (this.decorators.length) {
node.decorators = this.decorators
this.decorators = []
}
}
pp.parseDecorators = function(allowExport) {
while (this.type === tt.at) {
this.decorators.push(this.parseDecorator());
}
if (allowExport && this.type === tt._export) {
return;
}
if (this.type !== tt._class) {
this.raise(this.start, "Leading decorators must be attached to a class declaration");
}
}
pp.parseDecorator = function(allowExport) {
if (!this.options.features["es7.decorators"]) {
this.unexpected()
}
let node = this.startNode()
this.next()
node.expression = this.parseMaybeAssign()
return this.finishNode(node, "Decorator")
}
pp.parseBreakContinueStatement = function(node, keyword) {
let isBreak = keyword == "break"
this.next()
if (this.eat(tt.semi) || this.insertSemicolon()) node.label = null
else if (this.type !== tt.name) this.unexpected()
else {
node.label = this.parseIdent()
this.semicolon()
}
// Verify that there is an actual destination to break or
// continue to.
for (var i = 0; i < this.labels.length; ++i) {
let lab = this.labels[i]
if (node.label == null || lab.name === node.label.name) {
if (lab.kind != null && (isBreak || lab.kind === "loop")) break
if (node.label && isBreak) break
}
}
if (i === this.labels.length) this.raise(node.start, "Unsyntactic " + keyword)
return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement")
}
pp.parseDebuggerStatement = function(node) {
this.next()
this.semicolon()
return this.finishNode(node, "DebuggerStatement")
}
pp.parseDoStatement = function(node) {
let start = this.markPosition()
this.next()
this.labels.push(loopLabel)
node.body = this.parseStatement(false)
this.labels.pop()
if (this.options.features["es7.doExpressions"] && this.type !== tt._while) {
let container = this.startNodeAt(start)
container.expression = this.finishNode(node, "DoExpression")
this.semicolon()
return this.finishNode(container, "ExpressionStatement")
}
this.expect(tt._while)
node.test = this.parseParenExpression()
if (this.options.ecmaVersion >= 6)
this.eat(tt.semi)
else
this.semicolon()
return this.finishNode(node, "DoWhileStatement")
}
// Disambiguating between a `for` and a `for`/`in` or `for`/`of`
// loop is non-trivial. Basically, we have to parse the init `var`
// statement or expression, disallowing the `in` operator (see
// the second parameter to `parseExpression`), and then check
// whether the next token is `in` or `of`. When there is no init
// part (semicolon immediately after the opening parenthesis), it
// is a regular `for` loop.
pp.parseForStatement = function(node) {
this.next()
this.labels.push(loopLabel)
this.expect(tt.parenL)
if (this.type === tt.semi) return this.parseFor(node, null)
if (this.type === tt._var || this.type === tt._let || this.type === tt._const) {
let init = this.startNode(), varKind = this.type
this.next()
this.parseVar(init, true, varKind)
this.finishNode(init, "VariableDeclaration")
if ((this.type === tt._in || (this.options.ecmaVersion >= 6 && this.isContextual("of"))) && init.declarations.length === 1 &&
!(varKind !== tt._var && init.declarations[0].init))
return this.parseForIn(node, init)
return this.parseFor(node, init)
}
let refShorthandDefaultPos = {start: 0}
let init = this.parseExpression(true, refShorthandDefaultPos)
if (this.type === tt._in || (this.options.ecmaVersion >= 6 && this.isContextual("of"))) {
this.toAssignable(init)
this.checkLVal(init)
return this.parseForIn(node, init)
} else if (refShorthandDefaultPos.start) {
this.unexpected(refShorthandDefaultPos.start)
}
return this.parseFor(node, init)
}
pp.parseFunctionStatement = function(node) {
this.next()
return this.parseFunction(node, true)
}
pp.parseIfStatement = function(node) {
this.next()
node.test = this.parseParenExpression()
node.consequent = this.parseStatement(false)
node.alternate = this.eat(tt._else) ? this.parseStatement(false) : null
return this.finishNode(node, "IfStatement")
}
pp.parseReturnStatement = function(node) {
if (!this.inFunction && !this.options.allowReturnOutsideFunction)
this.raise(this.start, "'return' outside of function")
this.next()
// In `return` (and `break`/`continue`), the keywords with
// optional arguments, we eagerly look for a semicolon or the
// possibility to insert one.
if (this.eat(tt.semi) || this.insertSemicolon()) node.argument = null
else { node.argument = this.parseExpression(); this.semicolon() }
return this.finishNode(node, "ReturnStatement")
}
pp.parseSwitchStatement = function(node) {
this.next()
node.discriminant = this.parseParenExpression()
node.cases = []
this.expect(tt.braceL)
this.labels.push(switchLabel)
// Statements under must be grouped (by label) in SwitchCase
// nodes. `cur` is used to keep the node that we are currently
// adding statements to.
for (var cur, sawDefault; this.type != tt.braceR;) {
if (this.type === tt._case || this.type === tt._default) {
let isCase = this.type === tt._case
if (cur) this.finishNode(cur, "SwitchCase")
node.cases.push(cur = this.startNode())
cur.consequent = []
this.next()
if (isCase) {
cur.test = this.parseExpression()
} else {
if (sawDefault) this.raise(this.lastTokStart, "Multiple default clauses")
sawDefault = true
cur.test = null
}
this.expect(tt.colon)
} else {
if (!cur) this.unexpected()
cur.consequent.push(this.parseStatement(true))
}
}
if (cur) this.finishNode(cur, "SwitchCase")
this.next() // Closing brace
this.labels.pop()
return this.finishNode(node, "SwitchStatement")
}
pp.parseThrowStatement = function(node) {
this.next()
if (lineBreak.test(this.input.slice(this.lastTokEnd, this.start)))
this.raise(this.lastTokEnd, "Illegal newline after throw")
node.argument = this.parseExpression()
this.semicolon()
return this.finishNode(node, "ThrowStatement")
}
// Reused empty array added for node fields that are always empty.
const empty = []
pp.parseTryStatement = function(node) {
this.next()
node.block = this.parseBlock()
node.handler = null
if (this.type === tt._catch) {
let clause = this.startNode()
this.next()
this.expect(tt.parenL)
clause.param = this.parseBindingAtom()
this.checkLVal(clause.param, true)
this.expect(tt.parenR)
clause.guard = null
clause.body = this.parseBlock()
node.handler = this.finishNode(clause, "CatchClause")
}
node.guardedHandlers = empty
node.finalizer = this.eat(tt._finally) ? this.parseBlock() : null
if (!node.handler && !node.finalizer)
this.raise(node.start, "Missing catch or finally clause")
return this.finishNode(node, "TryStatement")
}
pp.parseVarStatement = function(node, kind) {
this.next()
this.parseVar(node, false, kind)
this.semicolon()
return this.finishNode(node, "VariableDeclaration")
}
pp.parseWhileStatement = function(node) {
this.next()
node.test = this.parseParenExpression()
this.labels.push(loopLabel)
node.body = this.parseStatement(false)
this.labels.pop()
return this.finishNode(node, "WhileStatement")
}
pp.parseWithStatement = function(node) {
if (this.strict) this.raise(this.start, "'with' in strict mode")
this.next()
node.object = this.parseParenExpression()
node.body = this.parseStatement(false)
return this.finishNode(node, "WithStatement")
}
pp.parseEmptyStatement = function(node) {
this.next()
return this.finishNode(node, "EmptyStatement")
}
pp.parseLabeledStatement = function(node, maybeName, expr) {
for (let i = 0; i < this.labels.length; ++i)
if (this.labels[i].name === maybeName) this.raise(expr.start, "Label '" + maybeName + "' is already declared")
let kind = this.type.isLoop ? "loop" : this.type === tt._switch ? "switch" : null
this.labels.push({name: maybeName, kind: kind})
node.body = this.parseStatement(true)
this.labels.pop()
node.label = expr
return this.finishNode(node, "LabeledStatement")
}
pp.parseExpressionStatement = function(node, expr) {
node.expression = expr
this.semicolon()
return this.finishNode(node, "ExpressionStatement")
}
// Parse a semicolon-enclosed block of statements, handling `"use
// strict"` declarations when `allowStrict` is true (used for
// function bodies).
pp.parseBlock = function(allowStrict) {
let node = this.startNode(), first = true, oldStrict
node.body = []
this.expect(tt.braceL)
while (!this.eat(tt.braceR)) {
let stmt = this.parseStatement(true)
node.body.push(stmt)
if (first && allowStrict && this.isUseStrict(stmt)) {
oldStrict = this.strict
this.setStrict(this.strict = true)
}
first = false
}
if (oldStrict === false) this.setStrict(false)
return this.finishNode(node, "BlockStatement")
}
// Parse a regular `for` loop. The disambiguation code in
// `parseStatement` will already have parsed the init statement or
// expression.
pp.parseFor = function(node, init) {
node.init = init
this.expect(tt.semi)
node.test = this.type === tt.semi ? null : this.parseExpression()
this.expect(tt.semi)
node.update = this.type === tt.parenR ? null : this.parseExpression()
this.expect(tt.parenR)
node.body = this.parseStatement(false)
this.labels.pop()
return this.finishNode(node, "ForStatement")
}
// Parse a `for`/`in` and `for`/`of` loop, which are almost
// same from parser's perspective.
pp.parseForIn = function(node, init) {
let type = this.type === tt._in ? "ForInStatement" : "ForOfStatement"
this.next()
node.left = init
node.right = this.parseExpression()
this.expect(tt.parenR)
node.body = this.parseStatement(false)
this.labels.pop()
return this.finishNode(node, type)
}
// Parse a list of variable declarations.
pp.parseVar = function(node, isFor, kind) {
node.declarations = []
node.kind = kind.keyword
for (;;) {
let decl = this.startNode()
this.parseVarHead(decl)
if (this.eat(tt.eq)) {
decl.init = this.parseMaybeAssign(isFor)
} else if (kind === tt._const && !(this.type === tt._in || (this.options.ecmaVersion >= 6 && this.isContextual("of")))) {
this.unexpected()
} else if (decl.id.type != "Identifier" && !(isFor && (this.type === tt._in || this.isContextual("of")))) {
this.raise(this.lastTokEnd, "Complex binding patterns require an initialization value")
} else {
decl.init = null
}
node.declarations.push(this.finishNode(decl, "VariableDeclarator"))
if (!this.eat(tt.comma)) break
}
return node
}
pp.parseVarHead = function (decl) {
decl.id = this.parseBindingAtom()
this.checkLVal(decl.id, true)
}
// Parse a function declaration or literal (depending on the
// `isStatement` parameter).
pp.parseFunction = function(node, isStatement, allowExpressionBody, isAsync) {
this.initFunction(node, isAsync)
if (this.options.ecmaVersion >= 6)
node.generator = this.eat(tt.star)
if (isStatement || this.type === tt.name)
node.id = this.parseIdent()
this.parseFunctionParams(node);
this.parseFunctionBody(node, allowExpressionBody)
return this.finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression")
}
pp.parseFunctionParams = function(node) {
this.expect(tt.parenL)
node.params = this.parseBindingList(tt.parenR, false, false)
}
// Parse a class declaration or literal (depending on the
// `isStatement` parameter).
pp.parseClass = function(node, isStatement) {
this.next()
this.parseClassId(node, isStatement)
this.parseClassSuper(node)
var classBody = this.startNode()
classBody.body = []
this.expect(tt.braceL)
while (!this.eat(tt.braceR)) {
if (this.eat(tt.semi)) continue
if (this.type === tt.at) {
this.parseDecorator()
continue;
}
var method = this.startNode()
this.takeDecorators(method)
var isGenerator = this.eat(tt.star), isAsync = false
this.parsePropertyName(method)
if (this.type !== tt.parenL && !method.computed && method.key.type === "Identifier" &&
method.key.name === "static") {
if (isGenerator) this.unexpected()
method['static'] = true
isGenerator = this.eat(tt.star)
this.parsePropertyName(method)
} else {
method['static'] = false
}
if (!isGenerator && method.key.type === "Identifier" && !method.computed && this.isClassProperty()) {
classBody.body.push(this.parseClassProperty(method))
continue
}
if (this.options.features["es7.asyncFunctions"] && this.type !== tt.parenL &&
!method.computed && method.key.type === "Identifier" && method.key.name === "async") {
isAsync = true
this.parsePropertyName(method)
}
method.kind = "method"
if (!method.computed && !isGenerator) {
if (method.key.type === "Identifier") {
if (this.type !== tt.parenL && (method.key.name === "get" || method.key.name === "set")) {
method.kind = method.key.name
this.parsePropertyName(method)
} else if (!method['static'] && method.key.name === "constructor") {
method.kind = "constructor"
}
} else if (!method['static'] && method.key.type === "Literal" && method.key.value === "constructor") {
method.kind = "constructor"
}
}
if (method.kind === "constructor" && method.decorators) {
this.raise(method.start, "You can't attach decorators to a class constructor")
}
this.parseClassMethod(classBody, method, isGenerator, isAsync)
}
if (this.decorators.length) {
this.raise(this.start, "You have trailing decorators with no method");
}
node.body = this.finishNode(classBody, "ClassBody")
return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression")
}
pp.isClassProperty = function() {
return this.type === tt.eq || (this.type === tt.semi || this.canInsertSemicolon())
}
pp.parseClassProperty = function(node) {
if (this.type === tt.eq) {
if (!this.options.features["es7.classProperties"]) this.unexpected()
this.next()
node.value = this.parseMaybeAssign();
} else {
node.value = null;
}
this.semicolon()
return this.finishNode(node, "ClassProperty")
}
pp.parseClassMethod = function(classBody, method, isGenerator, isAsync) {
method.value = this.parseMethod(isGenerator, isAsync)
classBody.body.push(this.finishNode(method, "MethodDefinition"))
}
pp.parseClassId = function(node, isStatement) {
node.id = this.type === tt.name ? this.parseIdent() : isStatement ? this.unexpected() : null
}
pp.parseClassSuper = function(node) {
node.superClass = this.eat(tt._extends) ? this.parseExprSubscripts() : null
}
// Parses module export declaration.
pp.parseExport = function(node) {
this.next()
// export * from '...'
if (this.eat(tt.star)) {
this.expectContextual("from")
node.source = this.type === tt.string ? this.parseExprAtom() : this.unexpected()
this.semicolon()
this.checkExport(node)
return this.finishNode(node, "ExportAllDeclaration")
}
if (this.eat(tt._default)) { // export default ...
let expr = this.parseMaybeAssign()
let needsSemi = true
if (expr.type == "FunctionExpression" ||
expr.type == "ClassExpression") {
needsSemi = false
if (expr.id) {
expr.type = expr.type == "FunctionExpression"
? "FunctionDeclaration"
: "ClassDeclaration"
}
}
node.declaration = expr
if (needsSemi) this.semicolon()
this.checkExport(node)
return this.finishNode(node, "ExportDefaultDeclaration")
}
// export var|const|let|function|class ...
if (this.type.keyword || this.shouldParseExportDeclaration()) {
node.declaration = this.parseStatement(true)
node.specifiers = []
node.source = null
} else { // export { x, y as z } [from '...']
node.declaration = null
node.specifiers = this.parseExportSpecifiers()
if (this.eatContextual("from")) {
node.source = this.type === tt.string ? this.parseExprAtom() : this.unexpected()
} else {
node.source = null
}
this.semicolon()
}
this.checkExport(node)
return this.finishNode(node, "ExportNamedDeclaration")
}
pp.shouldParseExportDeclaration = function() {
return this.options.features["es7.asyncFunctions"] && this.isContextual("async")
}
pp.checkExport = function(node) {
if (this.decorators.length) {
var isClass = node.declaration && (node.declaration.type === "ClassDeclaration" || node.declaration.type === "ClassExpression")
if (!node.declaration || !isClass) {
this.raise(node.start, "You can only use decorators on an export when exporting a class");
}
this.takeDecorators(node.declaration)
}
}
// Parses a comma-separated list of module exports.
pp.parseExportSpecifiers = function() {
let nodes = [], first = true
// export { x, y as z } [from '...']
this.expect(tt.braceL)
while (!this.eat(tt.braceR)) {
if (!first) {
this.expect(tt.comma)
if (this.afterTrailingComma(tt.braceR)) break
} else first = false
let node = this.startNode()
node.local = this.parseIdent(this.type === tt._default)
node.exported = this.eatContextual("as") ? this.parseIdent(true) : node.local
nodes.push(this.finishNode(node, "ExportSpecifier"))
}
return nodes
}
// Parses import declaration.
pp.parseImport = function(node) {
this.next()
// import '...'
if (this.type === tt.string) {
node.specifiers = empty
node.source = this.parseExprAtom()
node.kind = ""
} else {
node.specifiers = []
this.parseImportSpecifiers(node)
this.expectContextual("from")
node.source = this.type === tt.string ? this.parseExprAtom() : this.unexpected()
}
this.semicolon()
return this.finishNode(node, "ImportDeclaration")
}
// Parses a comma-separated list of module imports.
pp.parseImportSpecifiers = function(node) {
var first = true
if (this.type === tt.name) {
// import defaultObj, { x, y as z } from '...'
var start = this.markPosition()
node.specifiers.push(this.parseImportSpecifierDefault(this.parseIdent(), start))
if (!this.eat(tt.comma)) return
}
if (this.type === tt.star) {
var specifier = this.startNode()
this.next()
this.expectContextual("as")
specifier.local = this.parseIdent()
this.checkLVal(specifier.local, true)
node.specifiers.push(this.finishNode(specifier, "ImportNamespaceSpecifier"))
return
}
this.expect(tt.braceL)
while (!this.eat(tt.braceR)) {
if (!first) {
this.expect(tt.comma)
if (this.afterTrailingComma(tt.braceR)) break
} else first = false
var specifier = this.startNode()
specifier.imported = this.parseIdent(true)
specifier.local = this.eatContextual("as") ? this.parseIdent() : specifier.imported
this.checkLVal(specifier.local, true)
node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"))
}
}
pp.parseImportSpecifierDefault = function (id, start) {
var node = this.startNodeAt(start)
node.local = id
this.checkLVal(node.local, true)
return this.finishNode(node, "ImportDefaultSpecifier")
}

107
src/acorn/src/tokencontext.js Executable file
View File

@@ -0,0 +1,107 @@
// The algorithm used to determine whether a regexp can appear at a
// given point in the program is loosely based on sweet.js' approach.
// See https://github.com/mozilla/sweet.js/wiki/design
import {Parser} from "./state"
import {types as tt} from "./tokentype"
import {lineBreak} from "./whitespace"
export class TokContext {
constructor(token, isExpr, preserveSpace, override) {
this.token = token
this.isExpr = isExpr
this.preserveSpace = preserveSpace
this.override = override
}
}
export const types = {
b_stat: new TokContext("{", false),
b_expr: new TokContext("{", true),
b_tmpl: new TokContext("${", true),
p_stat: new TokContext("(", false),
p_expr: new TokContext("(", true),
q_tmpl: new TokContext("`", true, true, p => p.readTmplToken()),
f_expr: new TokContext("function", true)
}
const pp = Parser.prototype
pp.initialContext = function() {
return [types.b_stat]
}
pp.braceIsBlock = function(prevType) {
let parent
if (prevType === tt.colon && (parent = this.curContext()).token == "{")
return !parent.isExpr
if (prevType === tt._return)
return lineBreak.test(this.input.slice(this.lastTokEnd, this.start))
if (prevType === tt._else || prevType === tt.semi || prevType === tt.eof)
return true
if (prevType == tt.braceL)
return this.curContext() === types.b_stat
return !this.exprAllowed
}
pp.updateContext = function(prevType) {
let update, type = this.type
if (type.keyword && prevType == tt.dot)
this.exprAllowed = false
else if (update = type.updateContext)
update.call(this, prevType)
else
this.exprAllowed = type.beforeExpr
}
// Token-specific context update code
tt.parenR.updateContext = tt.braceR.updateContext = function() {
if (this.context.length == 1) {
this.exprAllowed = true
return
}
let out = this.context.pop()
if (out === types.b_stat && this.curContext() === types.f_expr) {
this.context.pop()
this.exprAllowed = false
} else if (out === types.b_tmpl) {
this.exprAllowed = true
} else {
this.exprAllowed = !out.isExpr
}
}
tt.braceL.updateContext = function(prevType) {
this.context.push(this.braceIsBlock(prevType) ? types.b_stat : types.b_expr)
this.exprAllowed = true
}
tt.dollarBraceL.updateContext = function() {
this.context.push(types.b_tmpl)
this.exprAllowed = true
}
tt.parenL.updateContext = function(prevType) {
let statementParens = prevType === tt._if || prevType === tt._for || prevType === tt._with || prevType === tt._while
this.context.push(statementParens ? types.p_stat : types.p_expr)
this.exprAllowed = true
}
tt.incDec.updateContext = function() {
// tokExprAllowed stays unchanged
}
tt._function.updateContext = function() {
if (this.curContext() !== types.b_stat)
this.context.push(types.f_expr)
this.exprAllowed = false
}
tt.backQuote.updateContext = function() {
if (this.curContext() === types.q_tmpl)
this.context.pop()
else
this.context.push(types.q_tmpl)
this.exprAllowed = false
}

681
src/acorn/src/tokenize.js Executable file
View File

@@ -0,0 +1,681 @@
import {isIdentifierStart, isIdentifierChar} from "./identifier"
import {types as tt, keywords as keywordTypes} from "./tokentype"
import {Parser} from "./state"
import {SourceLocation} from "./location"
import {lineBreak, lineBreakG, isNewLine, nonASCIIwhitespace} from "./whitespace"
// Object type used to represent tokens. Note that normally, tokens
// simply exist as properties on the parser object. This is only
// used for the onToken callback and the external tokenizer.
export class Token {
constructor(p) {
this.type = p.type
this.value = p.value
this.start = p.start
this.end = p.end
if (p.options.locations)
this.loc = new SourceLocation(p, p.startLoc, p.endLoc)
if (p.options.ranges)
this.range = [p.start, p.end]
}
}
// ## Tokenizer
const pp = Parser.prototype
// Move to the next token
pp.next = function() {
if (this.options.onToken)
this.options.onToken(new Token(this))
this.lastTokEnd = this.end
this.lastTokStart = this.start
this.lastTokEndLoc = this.endLoc
this.lastTokStartLoc = this.startLoc
this.nextToken()
}
pp.getToken = function() {
this.next()
return new Token(this)
}
// If we're in an ES6 environment, make parsers iterable
if (typeof Symbol !== "undefined")
pp[Symbol.iterator] = function () {
let self = this
return {next: function () {
let token = self.getToken()
return {
done: token.type === tt.eof,
value: token
}
}}
}
// Toggle strict mode. Re-reads the next number or string to please
// pedantic tests (`"use strict"; 010;` should fail).
pp.setStrict = function(strict) {
this.strict = strict
if (this.type !== tt.num && this.type !== tt.string) return
this.pos = this.start
if (this.options.locations) {
while (this.pos < this.lineStart) {
this.lineStart = this.input.lastIndexOf("\n", this.lineStart - 2) + 1
--this.curLine
}
}
this.nextToken()
}
pp.curContext = function() {
return this.context[this.context.length - 1]
}
// Read a single token, updating the parser object's token-related
// properties.
pp.nextToken = function() {
let curContext = this.curContext()
if (!curContext || !curContext.preserveSpace) this.skipSpace()
this.start = this.pos
if (this.options.locations) this.startLoc = this.curPosition()
if (this.pos >= this.input.length) return this.finishToken(tt.eof)
if (curContext.override) return curContext.override(this)
else this.readToken(this.fullCharCodeAtPos())
}
pp.readToken = function(code) {
// Identifier or keyword. '\uXXXX' sequences are allowed in
// identifiers, so '\' also dispatches to that.
if (isIdentifierStart(code, this.options.ecmaVersion >= 6) || code === 92 /* '\' */)
return this.readWord()
return this.getTokenFromCode(code)
}
pp.fullCharCodeAtPos = function() {
let code = this.input.charCodeAt(this.pos)
if (code <= 0xd7ff || code >= 0xe000) return code
let next = this.input.charCodeAt(this.pos + 1)
return (code << 10) + next - 0x35fdc00
}
pp.skipBlockComment = function() {
let startLoc = this.options.onComment && this.options.locations && this.curPosition()
let start = this.pos, end = this.input.indexOf("*/", this.pos += 2)
if (end === -1) this.raise(this.pos - 2, "Unterminated comment")
this.pos = end + 2
if (this.options.locations) {
lineBreakG.lastIndex = start
let match
while ((match = lineBreakG.exec(this.input)) && match.index < this.pos) {
++this.curLine
this.lineStart = match.index + match[0].length
}
}
if (this.options.onComment)
this.options.onComment(true, this.input.slice(start + 2, end), start, this.pos,
startLoc, this.options.locations && this.curPosition())
}
pp.skipLineComment = function(startSkip) {
let start = this.pos
let startLoc = this.options.onComment && this.options.locations && this.curPosition()
let ch = this.input.charCodeAt(this.pos+=startSkip)
while (this.pos < this.input.length && ch !== 10 && ch !== 13 && ch !== 8232 && ch !== 8233) {
++this.pos
ch = this.input.charCodeAt(this.pos)
}
if (this.options.onComment)
this.options.onComment(false, this.input.slice(start + startSkip, this.pos), start, this.pos,
startLoc, this.options.locations && this.curPosition())
}
// Called at the start of the parse and after every token. Skips
// whitespace and comments, and.
pp.skipSpace = function() {
while (this.pos < this.input.length) {
let ch = this.input.charCodeAt(this.pos)
if (ch === 32) { // ' '
++this.pos
} else if (ch === 13) {
++this.pos
let next = this.input.charCodeAt(this.pos)
if (next === 10) {
++this.pos
}
if (this.options.locations) {
++this.curLine
this.lineStart = this.pos
}
} else if (ch === 10 || ch === 8232 || ch === 8233) {
++this.pos
if (this.options.locations) {
++this.curLine
this.lineStart = this.pos
}
} else if (ch > 8 && ch < 14) {
++this.pos
} else if (ch === 47) { // '/'
let next = this.input.charCodeAt(this.pos + 1)
if (next === 42) { // '*'
this.skipBlockComment()
} else if (next === 47) { // '/'
this.skipLineComment(2)
} else break
} else if (ch === 160) { // '\xa0'
++this.pos
} else if (ch >= 5760 && nonASCIIwhitespace.test(String.fromCharCode(ch))) {
++this.pos
} else {
break
}
}
}
// Called at the end of every token. Sets `end`, `val`, and
// maintains `context` and `exprAllowed`, and skips the space after
// the token, so that the next one's `start` will point at the
// right position.
pp.finishToken = function(type, val) {
this.end = this.pos
if (this.options.locations) this.endLoc = this.curPosition()
let prevType = this.type
this.type = type
this.value = val
this.updateContext(prevType)
}
// ### Token reading
// This is the function that is called to fetch the next token. It
// is somewhat obscure, because it works in character codes rather
// than characters, and because operator parsing has been inlined
// into it.
//
// All in the name of speed.
//
pp.readToken_dot = function() {
let next = this.input.charCodeAt(this.pos + 1)
if (next >= 48 && next <= 57) return this.readNumber(true)
let next2 = this.input.charCodeAt(this.pos + 2)
if (this.options.ecmaVersion >= 6 && next === 46 && next2 === 46) { // 46 = dot '.'
this.pos += 3
return this.finishToken(tt.ellipsis)
} else {
++this.pos
return this.finishToken(tt.dot)
}
}
pp.readToken_slash = function() { // '/'
let next = this.input.charCodeAt(this.pos + 1)
if (this.exprAllowed) {++this.pos; return this.readRegexp();}
if (next === 61) return this.finishOp(tt.assign, 2)
return this.finishOp(tt.slash, 1)
}
pp.readToken_mult_modulo = function(code) { // '%*'
var type = code === 42 ? tt.star : tt.modulo
var width = 1
var next = this.input.charCodeAt(this.pos + 1)
if (next === 42) { // '*'
width++
next = this.input.charCodeAt(this.pos + 2)
type = tt.exponent
}
if (next === 61) {
width++
type = tt.assign
}
return this.finishOp(type, width)
}
pp.readToken_pipe_amp = function(code) { // '|&'
let next = this.input.charCodeAt(this.pos + 1)
if (next === code) return this.finishOp(code === 124 ? tt.logicalOR : tt.logicalAND, 2)
if (next === 61) return this.finishOp(tt.assign, 2)
return this.finishOp(code === 124 ? tt.bitwiseOR : tt.bitwiseAND, 1)
}
pp.readToken_caret = function() { // '^'
let next = this.input.charCodeAt(this.pos + 1)
if (next === 61) return this.finishOp(tt.assign, 2)
return this.finishOp(tt.bitwiseXOR, 1)
}
pp.readToken_plus_min = function(code) { // '+-'
let next = this.input.charCodeAt(this.pos + 1)
if (next === code) {
if (next == 45 && this.input.charCodeAt(this.pos + 2) == 62 &&
lineBreak.test(this.input.slice(this.lastTokEnd, this.pos))) {
// A `-->` line comment
this.skipLineComment(3)
this.skipSpace()
return this.nextToken()
}
return this.finishOp(tt.incDec, 2)
}
if (next === 61) return this.finishOp(tt.assign, 2)
return this.finishOp(tt.plusMin, 1)
}
pp.readToken_lt_gt = function(code) { // '<>'
let next = this.input.charCodeAt(this.pos + 1)
let size = 1
if (next === code) {
size = code === 62 && this.input.charCodeAt(this.pos + 2) === 62 ? 3 : 2
if (this.input.charCodeAt(this.pos + size) === 61) return this.finishOp(tt.assign, size + 1)
return this.finishOp(tt.bitShift, size)
}
if (next == 33 && code == 60 && this.input.charCodeAt(this.pos + 2) == 45 &&
this.input.charCodeAt(this.pos + 3) == 45) {
if (this.inModule) unexpected()
// `<!--`, an XML-style comment that should be interpreted as a line comment
this.skipLineComment(4)
this.skipSpace()
return this.nextToken()
}
if (next === 61)
size = this.input.charCodeAt(this.pos + 2) === 61 ? 3 : 2
return this.finishOp(tt.relational, size)
}
pp.readToken_eq_excl = function(code) { // '=!'
let next = this.input.charCodeAt(this.pos + 1)
if (next === 61) return this.finishOp(tt.equality, this.input.charCodeAt(this.pos + 2) === 61 ? 3 : 2)
if (code === 61 && next === 62 && this.options.ecmaVersion >= 6) { // '=>'
this.pos += 2
return this.finishToken(tt.arrow)
}
return this.finishOp(code === 61 ? tt.eq : tt.prefix, 1)
}
pp.getTokenFromCode = function(code) {
switch (code) {
// The interpretation of a dot depends on whether it is followed
// by a digit or another two dots.
case 46: // '.'
return this.readToken_dot()
// Punctuation tokens.
case 40: ++this.pos; return this.finishToken(tt.parenL)
case 41: ++this.pos; return this.finishToken(tt.parenR)
case 59: ++this.pos; return this.finishToken(tt.semi)
case 44: ++this.pos; return this.finishToken(tt.comma)
case 91: ++this.pos; return this.finishToken(tt.bracketL)
case 93: ++this.pos; return this.finishToken(tt.bracketR)
case 123: ++this.pos; return this.finishToken(tt.braceL)
case 125: ++this.pos; return this.finishToken(tt.braceR)
case 58: ++this.pos; return this.finishToken(tt.colon)
case 63: ++this.pos; return this.finishToken(tt.question)
case 64: ++this.pos; return this.finishToken(tt.at)
case 96: // '`'
if (this.options.ecmaVersion < 6) break
++this.pos
return this.finishToken(tt.backQuote)
case 48: // '0'
let next = this.input.charCodeAt(this.pos + 1)
if (next === 120 || next === 88) return this.readRadixNumber(16); // '0x', '0X' - hex number
if (this.options.ecmaVersion >= 6) {
if (next === 111 || next === 79) return this.readRadixNumber(8); // '0o', '0O' - octal number
if (next === 98 || next === 66) return this.readRadixNumber(2); // '0b', '0B' - binary number
}
// Anything else beginning with a digit is an integer, octal
// number, or float.
case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: // 1-9
return this.readNumber(false)
// Quotes produce strings.
case 34: case 39: // '"', "'"
return this.readString(code)
// Operators are parsed inline in tiny state machines. '=' (61) is
// often referred to. `finishOp` simply skips the amount of
// characters it is given as second argument, and returns a token
// of the type given by its first argument.
case 47: // '/'
return this.readToken_slash()
case 37: case 42: // '%*'
return this.readToken_mult_modulo(code)
case 124: case 38: // '|&'
return this.readToken_pipe_amp(code)
case 94: // '^'
return this.readToken_caret()
case 43: case 45: // '+-'
return this.readToken_plus_min(code)
case 60: case 62: // '<>'
return this.readToken_lt_gt(code)
case 61: case 33: // '=!'
return this.readToken_eq_excl(code)
case 126: // '~'
return this.finishOp(tt.prefix, 1)
}
this.raise(this.pos, "Unexpected character '" + codePointToString(code) + "'")
}
pp.finishOp = function(type, size) {
let str = this.input.slice(this.pos, this.pos + size)
this.pos += size
return this.finishToken(type, str)
}
var regexpUnicodeSupport = false
try { new RegExp("\uffff", "u"); regexpUnicodeSupport = true }
catch(e) {}
// Parse a regular expression. Some context-awareness is necessary,
// since a '/' inside a '[]' set does not end the expression.
pp.readRegexp = function() {
let escaped, inClass, start = this.pos
for (;;) {
if (this.pos >= this.input.length) this.raise(start, "Unterminated regular expression")
let ch = this.input.charAt(this.pos)
if (lineBreak.test(ch)) this.raise(start, "Unterminated regular expression")
if (!escaped) {
if (ch === "[") inClass = true
else if (ch === "]" && inClass) inClass = false
else if (ch === "/" && !inClass) break
escaped = ch === "\\"
} else escaped = false
++this.pos
}
let content = this.input.slice(start, this.pos)
++this.pos
// Need to use `readWord1` because '\uXXXX' sequences are allowed
// here (don't ask).
let mods = this.readWord1()
let tmp = content
if (mods) {
let validFlags = /^[gmsiy]*$/
if (this.options.ecmaVersion >= 6) validFlags = /^[gmsiyu]*$/
if (!validFlags.test(mods)) this.raise(start, "Invalid regular expression flag")
if (mods.indexOf('u') >= 0 && !regexpUnicodeSupport) {
// Replace each astral symbol and every Unicode escape sequence that
// possibly represents an astral symbol or a paired surrogate with a
// single ASCII symbol to avoid throwing on regular expressions that
// are only valid in combination with the `/u` flag.
// Note: replacing with the ASCII symbol `x` might cause false
// negatives in unlikely scenarios. For example, `[\u{61}-b]` is a
// perfectly valid pattern that is equivalent to `[a-b]`, but it would
// be replaced by `[x-b]` which throws an error.
tmp = tmp.replace(/\\u([a-fA-F0-9]{4})|\\u\{([0-9a-fA-F]+)\}|[\uD800-\uDBFF][\uDC00-\uDFFF]/g, "x")
}
}
// Detect invalid regular expressions.
try {
new RegExp(tmp)
} catch (e) {
if (e instanceof SyntaxError) this.raise(start, "Error parsing regular expression: " + e.message)
this.raise(e)
}
// Get a regular expression object for this pattern-flag pair, or `null` in
// case the current environment doesn't support the flags it uses.
let value
try {
value = new RegExp(content, mods)
} catch (err) {
value = null
}
return this.finishToken(tt.regexp, {pattern: content, flags: mods, value: value})
}
// Read an integer in the given radix. Return null if zero digits
// were read, the integer value otherwise. When `len` is given, this
// will return `null` unless the integer has exactly `len` digits.
pp.readInt = function(radix, len) {
let start = this.pos, total = 0
for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
let code = this.input.charCodeAt(this.pos), val
if (code >= 97) val = code - 97 + 10; // a
else if (code >= 65) val = code - 65 + 10; // A
else if (code >= 48 && code <= 57) val = code - 48; // 0-9
else val = Infinity
if (val >= radix) break
++this.pos
total = total * radix + val
}
if (this.pos === start || len != null && this.pos - start !== len) return null
return total
}
pp.readRadixNumber = function(radix) {
this.pos += 2; // 0x
let val = this.readInt(radix)
if (val == null) this.raise(this.start + 2, "Expected number in radix " + radix)
if (isIdentifierStart(this.fullCharCodeAtPos())) this.raise(this.pos, "Identifier directly after number")
return this.finishToken(tt.num, val)
}
// Read an integer, octal integer, or floating-point number.
pp.readNumber = function(startsWithDot) {
let start = this.pos, isFloat = false, octal = this.input.charCodeAt(this.pos) === 48
if (!startsWithDot && this.readInt(10) === null) this.raise(start, "Invalid number")
if (this.input.charCodeAt(this.pos) === 46) {
++this.pos
this.readInt(10)
isFloat = true
}
let next = this.input.charCodeAt(this.pos)
if (next === 69 || next === 101) { // 'eE'
next = this.input.charCodeAt(++this.pos)
if (next === 43 || next === 45) ++this.pos; // '+-'
if (this.readInt(10) === null) this.raise(start, "Invalid number")
isFloat = true
}
if (isIdentifierStart(this.fullCharCodeAtPos())) this.raise(this.pos, "Identifier directly after number")
let str = this.input.slice(start, this.pos), val
if (isFloat) val = parseFloat(str)
else if (!octal || str.length === 1) val = parseInt(str, 10)
else if (/[89]/.test(str) || this.strict) this.raise(start, "Invalid number")
else val = parseInt(str, 8)
return this.finishToken(tt.num, val)
}
// Read a string value, interpreting backslash-escapes.
pp.readCodePoint = function() {
let ch = this.input.charCodeAt(this.pos), code
if (ch === 123) {
if (this.options.ecmaVersion < 6) this.unexpected()
++this.pos
code = this.readHexChar(this.input.indexOf('}', this.pos) - this.pos)
++this.pos
if (code > 0x10FFFF) this.unexpected()
} else {
code = this.readHexChar(4)
}
return code
}
function codePointToString(code) {
// UTF-16 Decoding
if (code <= 0xFFFF) return String.fromCharCode(code)
return String.fromCharCode(((code - 0x10000) >> 10) + 0xD800,
((code - 0x10000) & 1023) + 0xDC00)
}
pp.readString = function(quote) {
let out = "", chunkStart = ++this.pos
for (;;) {
if (this.pos >= this.input.length) this.raise(this.start, "Unterminated string constant")
let ch = this.input.charCodeAt(this.pos)
if (ch === quote) break
if (ch === 92) { // '\'
out += this.input.slice(chunkStart, this.pos)
out += this.readEscapedChar()
chunkStart = this.pos
} else {
if (isNewLine(ch)) this.raise(this.start, "Unterminated string constant")
++this.pos
}
}
out += this.input.slice(chunkStart, this.pos++)
return this.finishToken(tt.string, out)
}
// Reads template string tokens.
pp.readTmplToken = function() {
let out = "", chunkStart = this.pos
for (;;) {
if (this.pos >= this.input.length) this.raise(this.start, "Unterminated template")
let ch = this.input.charCodeAt(this.pos)
if (ch === 96 || ch === 36 && this.input.charCodeAt(this.pos + 1) === 123) { // '`', '${'
if (this.pos === this.start && this.type === tt.template) {
if (ch === 36) {
this.pos += 2
return this.finishToken(tt.dollarBraceL)
} else {
++this.pos
return this.finishToken(tt.backQuote)
}
}
out += this.input.slice(chunkStart, this.pos)
return this.finishToken(tt.template, out)
}
if (ch === 92) { // '\'
out += this.input.slice(chunkStart, this.pos)
out += this.readEscapedChar()
chunkStart = this.pos
} else if (isNewLine(ch)) {
out += this.input.slice(chunkStart, this.pos)
++this.pos
if (ch === 13 && this.input.charCodeAt(this.pos) === 10) {
++this.pos
out += "\n"
} else {
out += String.fromCharCode(ch)
}
if (this.options.locations) {
++this.curLine
this.lineStart = this.pos
}
chunkStart = this.pos
} else {
++this.pos
}
}
}
// Used to read escaped characters
pp.readEscapedChar = function() {
let ch = this.input.charCodeAt(++this.pos)
let octal = /^[0-7]+/.exec(this.input.slice(this.pos, this.pos + 3))
if (octal) octal = octal[0]
while (octal && parseInt(octal, 8) > 255) octal = octal.slice(0, -1)
if (octal === "0") octal = null
++this.pos
if (octal) {
if (this.strict) this.raise(this.pos - 2, "Octal literal in strict mode")
this.pos += octal.length - 1
return String.fromCharCode(parseInt(octal, 8))
} else {
switch (ch) {
case 110: return "\n"; // 'n' -> '\n'
case 114: return "\r"; // 'r' -> '\r'
case 120: return String.fromCharCode(this.readHexChar(2)); // 'x'
case 117: return codePointToString(this.readCodePoint()); // 'u'
case 116: return "\t"; // 't' -> '\t'
case 98: return "\b"; // 'b' -> '\b'
case 118: return "\u000b"; // 'v' -> '\u000b'
case 102: return "\f"; // 'f' -> '\f'
case 48: return "\0"; // 0 -> '\0'
case 13: if (this.input.charCodeAt(this.pos) === 10) ++this.pos; // '\r\n'
case 10: // ' \n'
if (this.options.locations) { this.lineStart = this.pos; ++this.curLine }
return ""
default: return String.fromCharCode(ch)
}
}
}
// Used to read character escape sequences ('\x', '\u', '\U').
pp.readHexChar = function(len) {
let n = this.readInt(16, len)
if (n === null) this.raise(this.start, "Bad character escape sequence")
return n
}
// Used to signal to callers of `readWord1` whether the word
// contained any escape sequences. This is needed because words with
// escape sequences must not be interpreted as keywords.
var containsEsc
// Read an identifier, and return it as a string. Sets `containsEsc`
// to whether the word contained a '\u' escape.
//
// Incrementally adds only escaped chars, adding other chunks as-is
// as a micro-optimization.
pp.readWord1 = function() {
containsEsc = false
let word = "", first = true, chunkStart = this.pos
let astral = this.options.ecmaVersion >= 6
while (this.pos < this.input.length) {
let ch = this.fullCharCodeAtPos()
if (isIdentifierChar(ch, astral)) {
this.pos += ch <= 0xffff ? 1 : 2
} else if (ch === 92) { // "\"
containsEsc = true
word += this.input.slice(chunkStart, this.pos)
let escStart = this.pos
if (this.input.charCodeAt(++this.pos) != 117) // "u"
this.raise(this.pos, "Expecting Unicode escape sequence \\uXXXX")
++this.pos
let esc = this.readCodePoint()
if (!(first ? isIdentifierStart : isIdentifierChar)(esc, astral))
this.raise(escStart, "Invalid Unicode escape")
word += codePointToString(esc)
chunkStart = this.pos
} else {
break
}
first = false
}
return word + this.input.slice(chunkStart, this.pos)
}
// Read an identifier or keyword token. Will check for reserved
// words when necessary.
pp.readWord = function() {
let word = this.readWord1()
let type = tt.name
if ((this.options.ecmaVersion >= 6 || !containsEsc) && this.isKeyword(word))
type = keywordTypes[word]
return this.finishToken(type, word)
}

145
src/acorn/src/tokentype.js Executable file
View File

@@ -0,0 +1,145 @@
// ## Token types
// The assignment of fine-grained, information-carrying type objects
// allows the tokenizer to store the information it has about a
// token in a way that is very cheap for the parser to look up.
// All token type variables start with an underscore, to make them
// easy to recognize.
// The `beforeExpr` property is used to disambiguate between regular
// expressions and divisions. It is set on all token types that can
// be followed by an expression (thus, a slash after them would be a
// regular expression).
//
// `isLoop` marks a keyword as starting a loop, which is important
// to know when parsing a label, in order to allow or disallow
// continue jumps to that label.
export class TokenType {
constructor(label, conf = {}) {
this.label = label
this.keyword = conf.keyword
this.beforeExpr = !!conf.beforeExpr
this.startsExpr = !!conf.startsExpr
this.rightAssociative = !!conf.rightAssociative
this.isLoop = !!conf.isLoop
this.isAssign = !!conf.isAssign
this.prefix = !!conf.prefix
this.postfix = !!conf.postfix
this.binop = conf.binop || null
this.updateContext = null
}
}
function binop(name, prec) {
return new TokenType(name, {beforeExpr: true, binop: prec})
}
const beforeExpr = {beforeExpr: true}, startsExpr = {startsExpr: true}
export const types = {
num: new TokenType("num", startsExpr),
regexp: new TokenType("regexp", startsExpr),
string: new TokenType("string", startsExpr),
name: new TokenType("name", startsExpr),
eof: new TokenType("eof"),
// Punctuation token types.
bracketL: new TokenType("[", {beforeExpr: true, startsExpr: true}),
bracketR: new TokenType("]"),
braceL: new TokenType("{", {beforeExpr: true, startsExpr: true}),
braceR: new TokenType("}"),
parenL: new TokenType("(", {beforeExpr: true, startsExpr: true}),
parenR: new TokenType(")"),
comma: new TokenType(",", beforeExpr),
semi: new TokenType(";", beforeExpr),
colon: new TokenType(":", beforeExpr),
dot: new TokenType("."),
question: new TokenType("?", beforeExpr),
arrow: new TokenType("=>", beforeExpr),
template: new TokenType("template"),
ellipsis: new TokenType("...", beforeExpr),
backQuote: new TokenType("`", startsExpr),
dollarBraceL: new TokenType("${", {beforeExpr: true, startsExpr: true}),
at: new TokenType("@"),
// Operators. These carry several kinds of properties to help the
// parser use them properly (the presence of these properties is
// what categorizes them as operators).
//
// `binop`, when present, specifies that this operator is a binary
// operator, and will refer to its precedence.
//
// `prefix` and `postfix` mark the operator as a prefix or postfix
// unary operator.
//
// `isAssign` marks all of `=`, `+=`, `-=` etcetera, which act as
// binary operators with a very low precedence, that should result
// in AssignmentExpression nodes.
eq: new TokenType("=", {beforeExpr: true, isAssign: true}),
assign: new TokenType("_=", {beforeExpr: true, isAssign: true}),
incDec: new TokenType("++/--", {prefix: true, postfix: true, startsExpr: true}),
prefix: new TokenType("prefix", {beforeExpr: true, prefix: true, startsExpr: true}),
logicalOR: binop("||", 1),
logicalAND: binop("&&", 2),
bitwiseOR: binop("|", 3),
bitwiseXOR: binop("^", 4),
bitwiseAND: binop("&", 5),
equality: binop("==/!=", 6),
relational: binop("</>", 7),
bitShift: binop("<</>>", 8),
plusMin: new TokenType("+/-", {beforeExpr: true, binop: 9, prefix: true, startsExpr: true}),
modulo: binop("%", 10),
star: binop("*", 10),
slash: binop("/", 10),
exponent: new TokenType("**", {beforeExpr: true, binop: 11, rightAssociative: true})
}
// Map keyword names to token types.
export const keywords = {}
// Succinct definitions of keyword token types
function kw(name, options = {}) {
options.keyword = name
keywords[name] = types["_" + name] = new TokenType(name, options)
}
kw("break")
kw("case", beforeExpr)
kw("catch")
kw("continue")
kw("debugger")
kw("default")
kw("do", {isLoop: true})
kw("else", beforeExpr)
kw("finally")
kw("for", {isLoop: true})
kw("function")
kw("if")
kw("return", beforeExpr)
kw("switch")
kw("throw", beforeExpr)
kw("try")
kw("var")
kw("let")
kw("const")
kw("while", {isLoop: true})
kw("with")
kw("new", {beforeExpr: true, startsExpr: true})
kw("this", startsExpr)
kw("super", startsExpr)
kw("class")
kw("extends", beforeExpr)
kw("export")
kw("import")
kw("yield", {beforeExpr: true, startsExpr: true})
kw("null", startsExpr)
kw("true", startsExpr)
kw("false", startsExpr)
kw("in", {beforeExpr: true, binop: 7})
kw("instanceof", {beforeExpr: true, binop: 7})
kw("typeof", {beforeExpr: true, prefix: true, startsExpr: true})
kw("void", {beforeExpr: true, prefix: true, startsExpr: true})
kw("delete", {beforeExpr: true, prefix: true, startsExpr: true})

9
src/acorn/src/util.js Executable file
View File

@@ -0,0 +1,9 @@
export function isArray(obj) {
return Object.prototype.toString.call(obj) === "[object Array]"
}
// Checks if an object has a property.
export function has(obj, propName) {
return Object.prototype.hasOwnProperty.call(obj, propName)
}

12
src/acorn/src/whitespace.js Executable file
View File

@@ -0,0 +1,12 @@
// Matches a whole line break (where CRLF is considered a single
// line break). Used to count lines.
export const lineBreak = /\r\n?|\n|\u2028|\u2029/
export const lineBreakG = new RegExp(lineBreak.source, "g")
export function isNewLine(code) {
return code === 10 || code === 13 || code === 0x2028 || code == 0x2029
}
export const nonASCIIwhitespace = /[\u1680\u180e\u2000-\u200a\u202f\u205f\u3000\ufeff]/

View File

@@ -4,14 +4,12 @@ transform.version = require("../../../package").version;
transform.transform = transform;
transform.run = function (code, opts) {
opts ||= {};
opts.sourceMap = "inline";
transform.run = function (code, opts = {}) {
opts.sourceMaps = "inline";
return new Function(transform(code, opts).code)();
};
transform.load = function (url, callback, opts, hold) {
opts ||= {};
transform.load = function (url, callback, opts = {}, hold) {
opts.filename ||= url;
var xhr = global.ActiveXObject ? new global.ActiveXObject("Microsoft.XMLHTTP") : new global.XMLHttpRequest();
@@ -27,7 +25,7 @@ transform.load = function (url, callback, opts, hold) {
if (!hold) transform.run.apply(transform, param);
if (callback) callback(param);
} else {
throw new Error("Could not load " + url);
throw new Error(`Could not load ${url}`);
}
};

View File

@@ -1,32 +1,33 @@
var isFunction = require("lodash/lang/isFunction");
var transform = require("../transformation");
var util = require("../util");
var fs = require("fs");
import isFunction from "lodash/lang/isFunction";
import transform from "../transformation";
import * as acorn from "../../acorn";
import * as util from "../util";
import fs from "fs";
exports.version = require("../../../package").version;
export { util };
export { canCompile } from "../util";
exports.buildExternalHelpers = require("../build-external-helpers");
export { acorn } from "../../acorn";
export { default as Transformer } from "../transformation/transformer";
export { default as transform } from "../transformation";
export { default as traverse } from "../traversal";
export { default as buildExternalHelpers } from "../tools/build-external-helpers";
export { version } from "../../../package";
exports.types = require("../types");
import * as t from "../types";
export { t as types };
exports.register = function (opts) {
var register = require("./register/node");
if (opts != null) register(opts);
return register;
};
export function register(opts?: Object) {
var callback = require("./register/node");
if (opts != null) callback(opts);
return callback;
}
exports.polyfill = function () {
export function polyfill() {
require("../polyfill");
};
}
exports.canCompile = util.canCompile;
// do not use this - this is for use by official maintained babel plugins
exports._util = util;
exports.transform = transform;
exports.transformFile = function (filename, opts, callback) {
export function transformFile(filename: string, opts?: Object, callback: Function) {
if (isFunction(opts)) {
callback = opts;
opts = {};
@@ -47,10 +48,9 @@ exports.transformFile = function (filename, opts, callback) {
callback(null, result);
});
};
}
exports.transformFileSync = function (filename, opts) {
opts ||= {};
export function transformFileSync(filename: string, opts?: Object = {}) {
opts.filename = filename;
return transform(fs.readFileSync(filename), opts);
};
}

View File

@@ -1,5 +1,5 @@
// required to safely use babel/register within a browserify codebase
module.exports = function () {};
export default function () {};
require("../../polyfill");
import "../../polyfill";

View File

@@ -1,26 +1,19 @@
var path = require("path");
var os = require("os");
var fs = require("fs");
import path from "path";
import os from "os";
import fs from "fs";
var FILENAME = process.env.BABEL_CACHE_PATH || path.join(os.tmpdir(), "babel.json");
const FILENAME = process.env.BABEL_CACHE_PATH || path.join(os.tmpdir(), "babel.json");
var data = {};
exports.save = function () {
export function save() {
fs.writeFileSync(FILENAME, JSON.stringify(data, null, " "));
};
}
exports.load = function () {
export function load() {
if (process.env.BABEL_DISABLE_CACHE) return;
process.on("exit", exports.save);
var sigint = function () {
process.removeListener("SIGINT", sigint);
exports.save();
process.kill(process.pid, "SIGINT");
};
process.on("SIGINT", sigint);
process.on("exit", save);
process.nextTick(save);
if (!fs.existsSync(FILENAME)) return;
@@ -29,8 +22,8 @@ exports.load = function () {
} catch (err) {
return;
}
};
}
exports.get = function () {
export function get() {
return data;
};
}

View File

@@ -1,15 +1,15 @@
require("../../polyfill");
var sourceMapSupport = require("source-map-support");
var registerCache = require("./cache");
var resolveRc = require("./resolve-rc");
var extend = require("lodash/object/extend");
var babel = require("../node");
var each = require("lodash/collection/each");
var util = require("../../util");
var fs = require("fs");
import "../../polyfill";
import sourceMapSupport from "source-map-support";
import * as registerCache from "./cache";
import resolveRc from "../../tools/resolve-rc";
import extend from "lodash/object/extend";
import * as babel from "../node";
import each from "lodash/collection/each";
import * as util from "../../util";
import fs from "fs";
sourceMapSupport.install({
handleUncaughtExceptions: false,
retrieveSourceMap(source) {
var map = maps && maps[source];
if (map) {
@@ -33,7 +33,7 @@ var cache = registerCache.get();
var transformOpts = {};
var ignoreRegex = /node_modules/;
var onlyRegex;
var exts = {};
var oldHandlers = {};
var maps = {};
var mtime = function (filename) {
@@ -44,9 +44,12 @@ var compile = function (filename) {
var result;
var opts = extend({}, transformOpts);
// this will be done when the file is transformed anyway but we need all
// the options so we can generate the cache key
resolveRc(filename, opts);
var cacheKey = filename + ":" + JSON.stringify(opts);
var cacheKey = `${filename}:${JSON.stringify(opts)}:${babel.version}`;
if (cache) {
var cached = cache[cacheKey];
@@ -57,7 +60,7 @@ var compile = function (filename) {
if (!result) {
result = babel.transformFileSync(filename, extend(opts, {
sourceMap: true,
sourceMap: "both",
ast: false
}));
}
@@ -105,7 +108,7 @@ var normalLoader = function (m, filename) {
};
var registerExtension = function (ext) {
var old = require.extensions[ext];
var old = oldHandlers[ext] || oldHandlers[".js"];
var loader = normalLoader;
if (process.env.running_under_istanbul) loader = istanbulLoader; // jshint ignore:line
@@ -120,7 +123,7 @@ var registerExtension = function (ext) {
};
var hookExtensions = function (_exts) {
each(exts, function (old, ext) {
each(oldHandlers, function (old, ext) {
if (old === undefined) {
delete require.extensions[ext];
} else {
@@ -128,20 +131,17 @@ var hookExtensions = function (_exts) {
}
});
exts = {};
oldHandlers = {};
each(_exts, function (ext) {
exts[ext] = require.extensions[ext];
oldHandlers[ext] = require.extensions[ext];
registerExtension(ext);
});
};
hookExtensions(util.canCompile.EXTENSIONS);
module.exports = function (opts) {
// normalize options
opts ||= {};
export default function (opts = {}) {
if (opts.only != null) onlyRegex = util.regexify(opts.only);
if (opts.ignore != null) ignoreRegex = util.regexify(opts.ignore);
@@ -156,3 +156,16 @@ module.exports = function (opts) {
extend(transformOpts, opts);
};
//
try {
var runtimePackage = require("babel-runtime/package");
var version = require("../../package").version;
if (runtimePackage.version !== version) {
throw new ReferenceError(`The verison of babel-runtime of ${runtimePackage.runtime} that you have installed does not match the babel verison of ${version}`);
}
} catch (err) {
if (err.code !== "MODULE_NOT_FOUND") throw err;
}

View File

@@ -1,23 +0,0 @@
var buildHelpers = require("./build-helpers");
var generator = require("./generation");
var util = require("./util");
var t = require("./types");
module.exports = function (whitelist) {
var namespace = t.identifier("babelHelpers");
var body = [];
var container = t.functionExpression(null, [t.identifier("global")], t.blockStatement(body));
var tree = t.program([t.expressionStatement(t.callExpression(container, [util.template("self-global")]))]);
body.push(t.variableDeclaration("var", [
t.variableDeclarator(
namespace,
t.assignmentExpression("=", t.memberExpression(t.identifier("global"), namespace), t.objectExpression([]))
)
]));
buildHelpers(body, namespace, whitelist);
return generator(tree).code;
};

View File

@@ -1,15 +0,0 @@
var File = require("./transformation/file");
var util = require("./util");
var each = require("lodash/collection/each");
var t = require("./types");
module.exports = function (body, namespace, whitelist = []) {
each(File.helpers, function (name) {
if (whitelist.length && whitelist.indexOf(name) == -1) return;
var key = t.identifier(t.toIdentifier(name));
body.push(t.expressionStatement(
t.assignmentExpression("=", t.memberExpression(namespace, key), util.template(name))
));
});
};

View File

@@ -1,29 +0,0 @@
module.exports = detect;
var SYNTAX_KEYS = require("./syntax-keys");
var traverse = require("../traversal");
var visitors = traverse.explode(require("./visitors"));
function detect(ast) {
var stats = {
syntax: {},
builtins: {}
};
var detectedSyntax = function (name) {
stats.syntax[name] = true;
};
traverse(ast, {
enter(node, parent) {
if (SYNTAX_KEYS[node.type]) {
detectedSyntax(SYNTAX_KEYS[node.type]);
}
var visitor = visitors[node.type];
if (visitor) visitor(node, parent, detectedSyntax);
}
});
return stats;
}

View File

@@ -1,84 +0,0 @@
{
"ArrowFunctionExpression": "es6.arrowFunctions",
"AwaitExpression": "es7.asyncFunctions",
"ClassBody": "es6.classes",
"ClassDeclaration": "es6.classes",
"ClassExpression": "es6.classes",
"MethodDefinition": "es6.classes",
"ComprehensionBlock": "es7.comprehensions",
"ComprehensionExpression": "es7.comprehensions",
"ForOfStatement": "es6.forOf",
"ExportBatchSpecifier": "es6.modules",
"ExportDeclaration": "es6.modules",
"ExportSpecifier": "es6.modules",
"ImportBatchSpecifier": "es6.modules",
"ImportDeclaration": "es6.modules",
"ImportSpecifier": "es6.modules",
"ArrayPattern": "es6.destructuring",
"AssignmentPattern": "es6.destructuring",
"ObjectPattern": "es6.destructuring",
"RestElement": "es6.parameters.rest",
"SpreadElement": "es6.spread",
"SpreadProperty": "es7.objectSpread",
"TaggedTemplateExpression": "es6.templateLiterals",
"TemplateElement": "es6.templateLiterals",
"TemplateLiteral": "es6.templateLiterals",
"VirtualPropertyExpression": "es7.abstractReferences",
"PrivateDeclaration": "es7.abstractReferences",
"YieldExpression": "es6.generators",
"AnyTypeAnnotation": "flow",
"ArrayTypeAnnotation": "flow",
"BooleanTypeAnnotation": "flow",
"ClassProperty": "flow",
"DeclareClass": "flow",
"DeclareFunction": "flow",
"DeclareModule": "flow",
"DeclareVariable": "flow",
"FunctionTypeAnnotation": "flow",
"FunctionTypeParam": "flow",
"GenericTypeAnnotation": "flow",
"InterfaceExtends": "flow",
"InterfaceDeclaration": "flow",
"IntersectionTypeAnnotation": "flow",
"NullableTypeAnnotation": "flow",
"NumberTypeAnnotation": "flow",
"StringLiteralTypeAnnotation": "flow",
"StringTypeAnnotation": "flow",
"TupleTypeAnnotation": "flow",
"TypeofTypeAnnotation": "flow",
"TypeAlias": "flow",
"TypeAnnotation": "flow",
"TypeParameterDeclaration": "flow",
"TypeParameterInstantiation": "flow",
"ObjectTypeAnnotation": "flow",
"ObjectTypeCallProperty": "flow",
"ObjectTypeIndexer": "flow",
"ObjectTypeProperty": "flow",
"QualifiedTypeIdentifier": "flow",
"UnionTypeAnnotation": "flow",
"VoidTypeAnnotation": "flow",
"JSXAttribute": "jsx",
"JSXClosingElement": "jsx",
"JSXElement": "jsx",
"JSXEmptyExpression": "jsx",
"JSXExpressionContainer": "jsx",
"JSXIdentifier": "jsx",
"JSXMemberExpression": "jsx",
"JSXNamespacedName": "jsx",
"JSXOpeningElement": "jsx",
"JSXSpreadAttribute": "jsx"
}

View File

@@ -1,54 +0,0 @@
var t = require("../types");
var includes = require("lodash/collection/includes");
exports.AssignmentExpression = function (node, parent, detected) {
if (node.operator === "**=") {
detected("es6.exponentation");
}
};
exports.BinaryExpression = function (node, parent, detected) {
if (node.operator === "**") {
detected("es6.exponentation");
}
};
exports.VariableDeclaration = function (node, parent, detected) {
if (node.kind === "let" || node.kind === "const") {
detected("es6.blockScoping");
}
if (node.kind === "const") {
detected("es6.constants");
}
};
exports.Property = function (node, parent, detected) {
if (node.shorthand || node.method) {
detected("es6.properties.shorthand");
}
if (node.kind === "set" || node.kind === "get") {
detected("es5.properties.mutators");
}
if (node.computed) {
detected("es6.properties.computed");
}
};
exports.AssignmentPattern = function (node, parent, detected) {
if (t.isFunction(parent) && includes(parent.params, node)) {
detected("es6.parameters.default");
}
};
exports.Function = function (node, parent, detected) {
if (node.generator) {
detected("es6.generators");
}
if (node.async) {
detected("es7.asyncFunctions");
}
};

View File

@@ -1,8 +1,8 @@
var repeating = require("repeating");
var trimRight = require("trim-right");
var isBoolean = require("lodash/lang/isBoolean");
var includes = require("lodash/collection/includes");
var isNumber = require("lodash/lang/isNumber");
import repeating from "repeating";
import trimRight from "trim-right";
import isBoolean from "lodash/lang/isBoolean";
import includes from "lodash/collection/includes";
import isNumber from "lodash/lang/isNumber";
export default class Buffer {
constructor(position, format) {
@@ -140,7 +140,7 @@ export default class Buffer {
var indent = this.getIndent();
// replace all newlines with newlines with the indentation
str = str.replace(/\n/g, "\n" + indent);
str = str.replace(/\n/g, `\n${indent}`);
// we've got a newline before us so prepend on the indentation
if (this.isLast("\n")) this._push(indent);

View File

@@ -1,12 +1,12 @@
exports.File = function (node, print) {
export function File(node, print) {
print(node.program);
};
}
exports.Program = function (node, print) {
export function Program(node, print) {
print.sequence(node.body);
};
}
exports.BlockStatement = function (node, print) {
export function BlockStatement(node, print) {
if (node.body.length === 0) {
this.push("{}");
} else {
@@ -16,4 +16,4 @@ exports.BlockStatement = function (node, print) {
this.removeLast("\n");
this.rightBrace();
}
};
}

View File

@@ -1,5 +1,8 @@
exports.ClassExpression =
exports.ClassDeclaration = function (node, print) {
export function ClassDeclaration(node, print) {
if (node.decorators && node.decorators.length) {
print.list(node.decorators);
}
this.push("class");
if (node.id) {
@@ -7,16 +10,26 @@ exports.ClassDeclaration = function (node, print) {
print(node.id);
}
print(node.typeParameters);
if (node.superClass) {
this.push(" extends ");
print(node.superClass);
print(node.superTypeParameters);
}
if (node.implements) {
this.push(" implements ");
print.join(node.implements, { separator: ", " });
}
this.space();
print(node.body);
};
}
exports.ClassBody = function (node, print) {
export { ClassDeclaration as ClassExpression };
export function ClassBody(node, print) {
if (node.body.length === 0) {
this.push("{}");
} else {
@@ -29,12 +42,16 @@ exports.ClassBody = function (node, print) {
this.rightBrace();
}
};
}
export function MethodDefinition(node, print) {
if (node.decorators && node.decorators.length) {
print.list(node.decorators);
}
exports.MethodDefinition = function (node, print) {
if (node.static) {
this.push("static ");
}
this._method(node, print);
};
}

View File

@@ -1,13 +1,13 @@
exports.ComprehensionBlock = function (node, print) {
export function ComprehensionBlock(node, print) {
this.keyword("for");
this.push("(");
print(node.left);
this.push(" of ");
print(node.right);
this.push(")");
};
}
exports.ComprehensionExpression = function (node, print) {
export function ComprehensionExpression(node, print) {
this.push(node.generator ? "(" : "[");
print.join(node.blocks, { separator: " " });
@@ -24,4 +24,4 @@ exports.ComprehensionExpression = function (node, print) {
print(node.body);
this.push(node.generator ? ")" : "]");
};
}

View File

@@ -1,8 +1,8 @@
var isInteger = require("is-integer");
var isNumber = require("lodash/lang/isNumber");
var t = require("../../types");
import isInteger from "is-integer";
import isNumber from "lodash/lang/isNumber";
import * as t from "../../types";
exports.UnaryExpression = function (node, print) {
export function UnaryExpression(node, print) {
var hasSpace = /[a-z]$/.test(node.operator);
var arg = node.argument;
@@ -17,9 +17,15 @@ exports.UnaryExpression = function (node, print) {
this.push(node.operator);
if (hasSpace) this.push(" ");
print(node.argument);
};
}
exports.UpdateExpression = function (node, print) {
export function DoExpression(node, print) {
this.push("do");
this.space();
print(node.body);
}
export function UpdateExpression(node, print) {
if (node.prefix) {
this.push(node.operator);
print(node.argument);
@@ -27,9 +33,9 @@ exports.UpdateExpression = function (node, print) {
print(node.argument);
this.push(node.operator);
}
};
}
exports.ConditionalExpression = function (node, print) {
export function ConditionalExpression(node, print) {
print(node.test);
this.space();
this.push("?");
@@ -39,25 +45,34 @@ exports.ConditionalExpression = function (node, print) {
this.push(":");
this.space();
print(node.alternate);
};
}
exports.NewExpression = function (node, print) {
export function NewExpression(node, print) {
this.push("new ");
print(node.callee);
this.push("(");
print.list(node.arguments);
this.push(")");
};
}
exports.SequenceExpression = function (node, print) {
export function SequenceExpression(node, print) {
print.list(node.expressions);
};
}
exports.ThisExpression = function () {
export function ThisExpression() {
this.push("this");
};
}
exports.CallExpression = function (node, print) {
export function Super() {
this.push("super");
}
export function Decorator(node, print) {
this.push("@");
print(node.expression);
}
export function CallExpression(node, print) {
print(node.callee);
this.push("(");
@@ -80,7 +95,7 @@ exports.CallExpression = function (node, print) {
}
this.push(")");
};
}
var buildYieldAwait = function (keyword) {
return function (node, print) {
@@ -97,33 +112,36 @@ var buildYieldAwait = function (keyword) {
};
};
exports.YieldExpression = buildYieldAwait("yield");
exports.AwaitExpression = buildYieldAwait("await");
export var YieldExpression = buildYieldAwait("yield");
export var AwaitExpression = buildYieldAwait("await");
exports.EmptyStatement = function () {
export function EmptyStatement() {
this.semicolon();
};
}
exports.ExpressionStatement = function (node, print) {
export function ExpressionStatement(node, print) {
print(node.expression);
this.semicolon();
};
}
exports.BinaryExpression =
exports.LogicalExpression =
exports.AssignmentPattern =
exports.AssignmentExpression = function (node, print) {
export function AssignmentExpression(node, print) {
// todo: add cases where the spaces can be dropped when in compact mode
print(node.left);
this.push(" ");
this.push(node.operator);
this.push(" ");
print(node.right);
}
export {
AssignmentExpression as BinaryExpression,
AssignmentExpression as LogicalExpression,
AssignmentExpression as AssignmentPattern
};
var SCIENTIFIC_NOTATION = /e/i;
exports.MemberExpression = function (node, print) {
export function MemberExpression(node, print) {
var obj = node.object;
print(obj);
@@ -149,4 +167,6 @@ exports.MemberExpression = function (node, print) {
this.push(".");
print(node.property);
}
};
}
export { MemberExpression as MetaProperty };

View File

@@ -1,34 +1,226 @@
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.TypeCastExpression =
exports.VoidTypeAnnotation = function () {
// todo: implement these once we have a `--keep-types` option
};
import * as t from "../../types";
export function AnyTypeAnnotation() {
this.push("any");
}
export function ArrayTypeAnnotation(node, print) {
print(node.elementType);
this.push("[");
this.push("]");
}
export function BooleanTypeAnnotation(node) {
this.push("bool");
}
export function ClassProperty(node, print) {
if (node.static) this.push("static ");
print(node.key);
print(node.typeAnnotation);
this.semicolon();
}
export function DeclareClass(node, print) {
this.push("declare class ");
this._interfaceish(node, print);
}
export function DeclareFunction(node, print) {
this.push("declare function ");
print(node.id);
print(node.id.typeAnnotation.typeAnnotation);
this.semicolon();
}
export function DeclareModule(node, print) {
this.push("declare module ");
print(node.id);
this.space();
print(node.body);
}
export function DeclareVariable(node, print) {
this.push("declare var ");
print(node.id);
print(node.id.typeAnnotation);
this.semicolon();
}
export function FunctionTypeAnnotation(node, print, parent) {
print(node.typeParameters);
this.push("(");
print.list(node.params);
if (node.rest) {
if (node.params.length) {
this.push(",");
this.space();
}
this.push("...");
print(node.rest);
}
this.push(")");
// this node type is overloaded, not sure why but it makes it EXTREMELY annoying
if (parent.type === "ObjectTypeProperty" || parent.type === "ObjectTypeCallProperty" || parent.type === "DeclareFunction") {
this.push(":");
} else {
this.space();
this.push("=>");
}
this.space();
print(node.returnType);
}
export function FunctionTypeParam(node, print) {
print(node.name);
if (node.optional) this.push("?");
this.push(":");
this.space();
print(node.typeAnnotation);
}
export function InterfaceExtends(node, print) {
print(node.id);
print(node.typeParameters);
}
export { InterfaceExtends as ClassImplements, InterfaceExtends as GenericTypeAnnotation };
export function _interfaceish(node, print) {
print(node.id);
print(node.typeParameters);
if (node.extends.length) {
this.push(" extends ");
print.join(node.extends, { separator: ", " });
}
this.space();
print(node.body);
}
export function InterfaceDeclaration(node, print) {
this.push("interface ");
this._interfaceish(node, print);
}
export function IntersectionTypeAnnotation(node, print) {
print.join(node.types, { separator: " & " });
}
export function NullableTypeAnnotation(node, print) {
this.push("?");
print(node.typeAnnotation);
}
export function NumberTypeAnnotation() {
this.push("number");
}
export function StringLiteralTypeAnnotation(node) {
this._stringLiteral(node.value);
}
export function StringTypeAnnotation() {
this.push("string");
}
export function TupleTypeAnnotation(node, print) {
this.push("[");
print.join(node.types, { separator: ", " });
this.push("]");
}
export function TypeofTypeAnnotation(node, print) {
this.push("typeof ");
print(node.argument);
}
export function TypeAlias(node, print) {
this.push("type ");
print(node.id);
print(node.typeParameters);
this.space();
this.push("=");
this.space();
print(node.right);
this.semicolon();
}
export function TypeAnnotation(node, print) {
this.push(":");
this.space();
if (node.optional) this.push("?");
print(node.typeAnnotation);
}
export function TypeParameterInstantiation(node, print) {
this.push("<");
print.join(node.params, { separator: ", " });
this.push(">");
}
export { TypeParameterInstantiation as TypeParameterDeclaration };
export function ObjectTypeAnnotation(node, print) {
this.push("{");
var props = node.properties.concat(node.callProperties, node.indexers);
if (props.length) {
this.space();
print.list(props, { indent: true, separator: "; " });
this.space();
}
this.push("}");
}
export function ObjectTypeCallProperty(node, print) {
if (node.static) this.push("static ");
print(node.value);
}
export function ObjectTypeIndexer(node, print) {
if (node.static) this.push("static ");
this.push("[");
print(node.id);
this.push(":");
this.space();
print(node.key);
this.push("]");
this.push(":");
this.space();
print(node.value);
}
export function ObjectTypeProperty(node, print) {
if (node.static) this.push("static ");
print(node.key);
if (node.optional) this.push("?");
if (!t.isFunctionTypeAnnotation(node.value)) {
this.push(":");
this.space();
}
print(node.value);
}
export function QualifiedTypeIdentifier(node, print) {
print(node.qualification);
this.push(".");
print(node.id);
}
export function UnionTypeAnnotation(node, print) {
print.join(node.types, { separator: " | " });
}
export function TypeCastExpression(node, print) {
this.push("(");
print(node.expression);
print(node.typeAnnotation);
this.push(")");
}
export function VoidTypeAnnotation(node) {
this.push("void");
}

View File

@@ -1,43 +1,43 @@
var t = require("../../types");
var each = require("lodash/collection/each");
import each from "lodash/collection/each";
import * as t from "../../types";
exports.JSXAttribute = function (node, print) {
export function JSXAttribute(node, print) {
print(node.name);
if (node.value) {
this.push("=");
print(node.value);
}
};
}
exports.JSXIdentifier = function (node) {
export function JSXIdentifier(node) {
this.push(node.name);
};
}
exports.JSXNamespacedName = function (node, print) {
export function JSXNamespacedName(node, print) {
print(node.namespace);
this.push(":");
print(node.name);
};
}
exports.JSXMemberExpression = function (node, print) {
export function JSXMemberExpression(node, print) {
print(node.object);
this.push(".");
print(node.property);
};
}
exports.JSXSpreadAttribute = function (node, print) {
export function JSXSpreadAttribute(node, print) {
this.push("{...");
print(node.argument);
this.push("}");
};
}
exports.JSXExpressionContainer = function (node, print) {
export function JSXExpressionContainer(node, print) {
this.push("{");
print(node.expression);
this.push("}");
};
}
exports.JSXElement = function (node, print) {
export function JSXElement(node, print) {
var open = node.openingElement;
print(open);
if (open.selfClosing) return;
@@ -53,9 +53,9 @@ exports.JSXElement = function (node, print) {
this.dedent();
print(node.closingElement);
};
}
exports.JSXOpeningElement = function (node, print) {
export function JSXOpeningElement(node, print) {
this.push("<");
print(node.name);
if (node.attributes.length > 0) {
@@ -63,12 +63,12 @@ exports.JSXOpeningElement = function (node, print) {
print.join(node.attributes, { separator: " " });
}
this.push(node.selfClosing ? " />" : ">");
};
}
exports.JSXClosingElement = function (node, print) {
export function JSXClosingElement(node, print) {
this.push("</");
print(node.name);
this.push(">");
};
}
exports.JSXEmptyExpression = function () {};
export function JSXEmptyExpression() {}

View File

@@ -1,21 +1,33 @@
var t = require("../../types");
import * as t from "../../types";
exports._params = function (node, print) {
export function _params(node, print) {
print(node.typeParameters);
this.push("(");
print.list(node.params);
print.list(node.params, {
iterator: (node) =>{
if (node.optional) this.push("?");
print(node.typeAnnotation);
}
});
this.push(")");
};
exports._method = function (node, print) {
if (node.returnType) {
print(node.returnType);
}
}
export function _method(node, print) {
var value = node.value;
var kind = node.kind;
var key = node.key;
if (!kind || kind === "init") {
if (kind === "method" || kind === "init") {
if (value.generator) {
this.push("*");
}
} else {
}
if (kind === "get" || kind === "set") {
this.push(kind + " ");
}
@@ -32,10 +44,9 @@ exports._method = function (node, print) {
this._params(value, print);
this.push(" ");
print(value.body);
};
}
exports.FunctionDeclaration =
exports.FunctionExpression = function (node, print) {
export function FunctionExpression(node, print) {
if (node.async) this.push("async ");
this.push("function");
if (node.generator) this.push("*");
@@ -50,9 +61,11 @@ exports.FunctionExpression = function (node, print) {
this._params(node, print);
this.space();
print(node.body);
};
}
exports.ArrowFunctionExpression = function (node, print) {
export { FunctionExpression as FunctionDeclaration };
export function ArrowFunctionExpression(node, print) {
if (node.async) this.push("async ");
if (node.params.length === 1 && t.isIdentifier(node.params[0])) {
@@ -63,4 +76,4 @@ exports.ArrowFunctionExpression = function (node, print) {
this.push(" => ");
print(node.body);
};
}

View File

@@ -1,50 +1,56 @@
var t = require("../../types");
var each = require("lodash/collection/each");
import each from "lodash/collection/each";
import * as t from "../../types";
exports.ImportSpecifier = function (node, print) {
if (t.isSpecifierDefault(node)) {
print(t.getSpecifierName(node));
} else {
return exports.ExportSpecifier.apply(this, arguments);
}
};
exports.ExportSpecifier = function (node, print) {
print(node.id);
if (node.name) {
export function ImportSpecifier(node, print) {
print(node.imported);
if (node.local && node.local !== node.imported) {
this.push(" as ");
print(node.name);
print(node.local);
}
};
}
exports.ExportBatchSpecifier = function () {
this.push("*");
};
export function ImportDefaultSpecifier(node, print) {
print(node.local);
}
exports.ExportDeclaration = function (node, print) {
export function ExportSpecifier(node, print) {
print(node.local);
if (node.exported && node.local !== node.exported) {
this.push(" as ");
print(node.exported);
}
}
export function ExportAllDeclaration(node, print) {
this.push("export * from ");
print(node.source);
this.semicolon();
}
export function ExportNamedDeclaration(node, print) {
this.push("export ");
ExportDeclaration.call(this, node, print);
}
export function ExportDefaultDeclaration(node, print) {
this.push("export default ");
ExportDeclaration.call(this, node, print);
}
function ExportDeclaration(node, print) {
var specifiers = node.specifiers;
if (node.default) {
this.push("default ");
}
if (node.declaration) {
print(node.declaration);
if (t.isStatement(node.declaration)) return;
} else {
if (specifiers.length === 1 && t.isExportBatchSpecifier(specifiers[0])) {
print(specifiers[0]);
} else {
this.push("{");
if (specifiers.length) {
this.space();
print.join(specifiers, { separator: ", " });
this.space();
}
this.push("}");
this.push("{");
if (specifiers.length) {
this.space();
print.join(specifiers, { separator: ", " });
this.space();
}
this.push("}");
if (node.source) {
this.push(" from ");
@@ -53,9 +59,9 @@ exports.ExportDeclaration = function (node, print) {
}
this.ensureSemicolon();
};
}
exports.ImportDeclaration = function (node, print) {
export function ImportDeclaration(node, print) {
this.push("import ");
if (node.isType) {
@@ -66,20 +72,20 @@ exports.ImportDeclaration = function (node, print) {
if (specfiers && specfiers.length) {
var foundImportSpecifier = false;
each(node.specifiers, (spec, i) => {
if (+i > 0) {
for (var i = 0; i < node.specifiers.length; i++) {
var spec = node.specifiers[i];
if (i > 0) {
this.push(", ");
}
var isDefault = t.isSpecifierDefault(spec);
if (!isDefault && spec.type !== "ImportBatchSpecifier" && !foundImportSpecifier) {
if (!t.isImportDefaultSpecifier(spec) && !t.isImportNamespaceSpecifier(spec) && !foundImportSpecifier) {
foundImportSpecifier = true;
this.push("{ ");
}
print(spec);
});
}
if (foundImportSpecifier) {
this.push(" }");
@@ -90,9 +96,9 @@ exports.ImportDeclaration = function (node, print) {
print(node.source);
this.semicolon();
};
}
exports.ImportBatchSpecifier = function (node, print) {
export function ImportNamespaceSpecifier(node, print) {
this.push("* as ");
print(node.name);
};
print(node.local);
}

View File

@@ -1,7 +0,0 @@
var each = require("lodash/collection/each");
each(["BindMemberExpression", "BindFunctionExpression"], function (type) {
exports[type] = function () {
throw new ReferenceError("Trying to render non-standard playground node " + JSON.stringify(type));
};
});

View File

@@ -1,15 +1,15 @@
var repeating = require("repeating");
var t = require("../../types");
import repeating from "repeating";
import * as t from "../../types";
exports.WithStatement = function (node, print) {
export function WithStatement(node, print) {
this.keyword("with");
this.push("(");
print(node.object);
this.push(")");
print.block(node.body);
};
}
exports.IfStatement = function (node, print) {
export function IfStatement(node, print) {
this.keyword("if");
this.push("(");
print(node.test);
@@ -23,9 +23,9 @@ exports.IfStatement = function (node, print) {
this.push("else ");
print.indentOnComments(node.alternate);
}
};
}
exports.ForStatement = function (node, print) {
export function ForStatement(node, print) {
this.keyword("for");
this.push("(");
@@ -45,32 +45,32 @@ exports.ForStatement = function (node, print) {
this.push(")");
print.block(node.body);
};
}
exports.WhileStatement = function (node, print) {
export function WhileStatement(node, print) {
this.keyword("while");
this.push("(");
print(node.test);
this.push(")");
print.block(node.body);
};
}
var buildForXStatement = function (op) {
return function (node, print) {
this.keyword("for");
this.push("(");
print(node.left);
this.push(" " + op + " ");
this.push(` ${op} `);
print(node.right);
this.push(")");
print.block(node.body);
};
};
exports.ForInStatement = buildForXStatement("in");
exports.ForOfStatement = buildForXStatement("of");
export var ForInStatement = buildForXStatement("in");
export var ForOfStatement = buildForXStatement("of");
exports.DoWhileStatement = function (node, print) {
export function DoWhileStatement(node, print) {
this.keyword("do");
print(node.body);
this.space();
@@ -78,7 +78,7 @@ exports.DoWhileStatement = function (node, print) {
this.push("(");
print(node.test);
this.push(");");
};
}
var buildLabelStatement = function (prefix, key) {
return function (node, print) {
@@ -94,17 +94,17 @@ var buildLabelStatement = function (prefix, key) {
};
};
exports.ContinueStatement = buildLabelStatement("continue");
exports.ReturnStatement = buildLabelStatement("return", "argument");
exports.BreakStatement = buildLabelStatement("break");
export var ContinueStatement = buildLabelStatement("continue");
export var ReturnStatement = buildLabelStatement("return", "argument");
export var BreakStatement = buildLabelStatement("break");
exports.LabeledStatement = function (node, print) {
export function LabeledStatement(node, print) {
print(node.label);
this.push(": ");
print(node.body);
};
}
exports.TryStatement = function (node, print) {
export function TryStatement(node, print) {
this.keyword("try");
print(node.block);
this.space();
@@ -123,23 +123,23 @@ exports.TryStatement = function (node, print) {
this.push("finally ");
print(node.finalizer);
}
};
}
exports.CatchClause = function (node, print) {
export function CatchClause(node, print) {
this.keyword("catch");
this.push("(");
print(node.param);
this.push(") ");
print(node.body);
};
}
exports.ThrowStatement = function (node, print) {
export function ThrowStatement(node, print) {
this.push("throw ");
print(node.argument);
this.semicolon();
};
}
exports.SwitchStatement = function (node, print) {
export function SwitchStatement(node, print) {
this.keyword("switch");
this.push("(");
print(node.discriminant);
@@ -155,9 +155,9 @@ exports.SwitchStatement = function (node, print) {
});
this.push("}");
};
}
exports.SwitchCase = function (node, print) {
export function SwitchCase(node, print) {
if (node.test) {
this.push("case ");
print(node.test);
@@ -170,13 +170,13 @@ exports.SwitchCase = function (node, print) {
this.newline();
print.sequence(node.consequent, { indent: true });
}
};
}
exports.DebuggerStatement = function () {
export function DebuggerStatement() {
this.push("debugger;");
};
}
exports.VariableDeclaration = function (node, print, parent) {
export function VariableDeclaration(node, print, parent) {
this.push(node.kind + " ");
var hasInits = false;
@@ -192,7 +192,7 @@ exports.VariableDeclaration = function (node, print, parent) {
var sep = ",";
if (!this.format.compact && hasInits) {
sep += "\n" + repeating(" ", node.kind.length + 1);
sep += `\n${repeating(" ", node.kind.length + 1)}`;
} else {
sep += " ";
}
@@ -202,22 +202,15 @@ exports.VariableDeclaration = function (node, print, parent) {
if (!t.isFor(parent)) {
this.semicolon();
}
};
}
exports.PrivateDeclaration = function (node, print) {
this.push("private ");
print.join(node.declarations, { separator: ", " });
this.semicolon();
};
exports.VariableDeclarator = function (node, print) {
export function VariableDeclarator(node, print) {
print(node.id);
print(node.id.typeAnnotation);
if (node.init) {
print(node.id);
this.space();
this.push("=");
this.space();
print(node.init);
} else {
print(node.id);
}
};
}

View File

@@ -1,15 +1,15 @@
var each = require("lodash/collection/each");
import each from "lodash/collection/each";
exports.TaggedTemplateExpression = function (node, print) {
export function TaggedTemplateExpression(node, print) {
print(node.tag);
print(node.quasi);
};
}
exports.TemplateElement = function (node) {
export function TemplateElement(node) {
this._push(node.value.raw);
};
}
exports.TemplateLiteral = function (node, print) {
export function TemplateLiteral(node, print) {
this.push("`");
var quasis = node.quasis;
@@ -26,4 +26,4 @@ exports.TemplateLiteral = function (node, print) {
});
this._push("`");
};
}

View File

@@ -1,24 +1,17 @@
var each = require("lodash/collection/each");
import each from "lodash/collection/each";
exports.Identifier = function (node) {
export function Identifier(node) {
this.push(node.name);
};
}
exports.RestElement =
exports.SpreadElement =
exports.SpreadProperty = function (node, print) {
export function RestElement(node, print) {
this.push("...");
print(node.argument);
};
}
exports.VirtualPropertyExpression = function (node, print) {
print(node.object);
this.push("::");
print(node.property);
};
export { RestElement as SpreadElement, RestElement as SpreadProperty };
exports.ObjectExpression =
exports.ObjectPattern = function (node, print) {
export function ObjectExpression(node, print) {
var props = node.properties;
if (props.length) {
@@ -32,9 +25,11 @@ exports.ObjectPattern = function (node, print) {
} else {
this.push("{}");
}
};
}
exports.Property = function (node, print) {
export { ObjectExpression as ObjectPattern };
export function Property(node, print) {
if (node.method || node.kind === "get" || node.kind === "set") {
this._method(node, print);
} else {
@@ -51,10 +46,9 @@ exports.Property = function (node, print) {
this.space();
print(node.value);
}
};
}
exports.ArrayExpression =
exports.ArrayPattern = function (node, print) {
export function ArrayExpression(node, print) {
var elems = node.elements;
var len = elems.length;
@@ -76,28 +70,34 @@ exports.ArrayPattern = function (node, print) {
});
this.push("]");
};
}
exports.Literal = function (node) {
export { ArrayExpression as ArrayPattern };
export function Literal(node) {
var val = node.value;
var type = typeof val;
if (type === "string") {
val = JSON.stringify(val);
// escape illegal js but valid json unicode characters
val = val.replace(/[\u000A\u000D\u2028\u2029]/g, function (c) {
return "\\u" + ("0000" + c.charCodeAt(0).toString(16)).slice(-4);
});
this.push(val);
this._stringLiteral(val);
} else if (type === "number") {
this.push(val + "");
} else if (type === "boolean") {
this.push(val ? "true" : "false");
} else if (node.regex) {
this.push("/" + node.regex.pattern + "/" + node.regex.flags);
this.push(`/${node.regex.pattern}/${node.regex.flags}`);
} else if (val === null) {
this.push("null");
}
};
}
export function _stringLiteral(val) {
val = JSON.stringify(val);
// escape illegal js but valid json unicode characters
val = val.replace(/[\u000A\u000D\u2028\u2029]/g, function (c) {
return "\\u" + ("0000" + c.charCodeAt(0).toString(16)).slice(-4);
});
this.push(val);
}

View File

@@ -1,14 +1,14 @@
var detectIndent = require("detect-indent");
var Whitespace = require("./whitespace");
var repeating = require("repeating");
var SourceMap = require("./source-map");
var Position = require("./position");
var messages = require("../messages");
var Buffer = require("./buffer");
var extend = require("lodash/object/extend");
var each = require("lodash/collection/each");
var n = require("./node");
var t = require("../types");
import detectIndent from "detect-indent";
import Whitespace from "./whitespace";
import repeating from "repeating";
import SourceMap from "./source-map";
import Position from "./position";
import * as messages from "../messages";
import Buffer from "./buffer";
import extend from "lodash/object/extend";
import each from "lodash/collection/each";
import n from "./node";
import * as t from "../types";
class CodeGenerator {
constructor(ast, opts, code) {
@@ -59,7 +59,6 @@ class CodeGenerator {
comprehensions: require("./generators/comprehensions"),
expressions: require("./generators/expressions"),
statements: require("./generators/statements"),
playground: require("./generators/playground"),
classes: require("./generators/classes"),
methods: require("./generators/methods"),
modules: require("./generators/modules"),
@@ -91,8 +90,7 @@ class CodeGenerator {
return this.print(node, parent, opts);
};
print.sequence = (nodes, opts) => {
opts ||= {};
print.sequence = (nodes, opts = {}) => {
opts.statement = true;
return this.printJoin(print, nodes, opts);
};
@@ -101,8 +99,7 @@ class CodeGenerator {
return this.printJoin(print, nodes, opts);
};
print.list = function (items, opts) {
opts ||= {};
print.list = function (items, opts = {}) {
opts.separator ||= ", ";
print.join(items, opts);
};
@@ -118,8 +115,8 @@ class CodeGenerator {
return print;
}
print(node, parent, opts) {
if (!node) return "";
print(node, parent, opts = {}) {
if (!node) return;
if (parent && parent._compact) {
node._compact = true;
@@ -130,8 +127,6 @@ class CodeGenerator {
this.format.concise = true;
}
opts ||= {};
var newline = (leading) => {
if (!opts.statement && !n.isUserWhitespacable(node, parent)) {
return;
@@ -191,17 +186,15 @@ class CodeGenerator {
this.printTrailingComments(node, parent);
} else {
throw new ReferenceError("unknown node of type " + JSON.stringify(node.type) + " with constructor " + JSON.stringify(node && node.constructor.name));
throw new ReferenceError(`unknown node of type ${JSON.stringify(node.type)} with constructor ${JSON.stringify(node && node.constructor.name)}`);
}
this.format.concise = oldConcise;
}
printJoin(print, nodes, opts) {
printJoin(print, nodes, opts = {}) {
if (!nodes || !nodes.length) return;
opts ||= {};
var len = nodes.length;
if (opts.indent) this.indent();
@@ -244,9 +237,9 @@ class CodeGenerator {
generateComment(comment) {
var val = comment.value;
if (comment.type === "Line") {
val = "//" + val;
val = `//${val}`;
} else {
val = "/*" + val + "*/";
val = `/*${val}*/`;
}
return val;
}
@@ -325,7 +318,7 @@ class CodeGenerator {
}
var indent = Math.max(this.indentSize(), column);
val = val.replace(/\n/g, "\n" + repeating(" ", indent));
val = val.replace(/\n/g, `\n${repeating(" ", indent)}`);
}
if (column === 0) {

View File

@@ -1,8 +1,8 @@
var whitespace = require("./whitespace");
var parens = require("./parentheses");
var each = require("lodash/collection/each");
var some = require("lodash/collection/some");
var t = require("../../types");
import whitespace from "./whitespace";
import * as parens from "./parentheses";
import each from "lodash/collection/each";
import some from "lodash/collection/some";
import * as t from "../../types";
var find = function (obj, node, parent) {
if (!obj) return;

View File

@@ -1,7 +1,7 @@
var t = require("../../types");
var each = require("lodash/collection/each");
import each from "lodash/collection/each";
import * as t from "../../types";
var PRECEDENCE = {};
const PRECEDENCE = {};
each([
["||"],
@@ -21,14 +21,20 @@ each([
});
});
exports.UpdateExpression = function (node, parent) {
export function NullableTypeAnnotation(node, parent) {
return t.isArrayTypeAnnotation(parent);
}
export { NullableTypeAnnotation as FunctionTypeAnnotation };
export function UpdateExpression(node, parent) {
if (t.isMemberExpression(parent) && parent.object === node) {
// (foo++).test()
return true;
}
};
}
exports.ObjectExpression = function (node, parent) {
export function ObjectExpression(node, parent) {
if (t.isExpressionStatement(parent)) {
// ({ foo: "bar" });
return true;
@@ -40,9 +46,9 @@ exports.ObjectExpression = function (node, parent) {
}
return false;
};
}
exports.Binary = function (node, parent) {
export function Binary(node, parent) {
if ((t.isCallExpression(parent) || t.isNewExpression(parent)) && parent.callee === node) {
return true;
}
@@ -70,9 +76,9 @@ exports.Binary = function (node, parent) {
return true;
}
}
};
}
exports.BinaryExpression = function (node, parent) {
export function BinaryExpression(node, parent) {
if (node.operator === "in") {
// var i = (1 in []);
if (t.isVariableDeclarator(parent)) {
@@ -84,9 +90,9 @@ exports.BinaryExpression = function (node, parent) {
return true;
}
}
};
}
exports.SequenceExpression = function (node, parent) {
export function SequenceExpression(node, parent) {
if (t.isForStatement(parent)) {
// Although parentheses wouldn't hurt around sequence
// expressions in the head of for loops, traditional style
@@ -102,9 +108,9 @@ exports.SequenceExpression = function (node, parent) {
// Otherwise err on the side of overparenthesization, adding
// explicit exceptions above if this proves overzealous.
return true;
};
}
exports.YieldExpression = function (node, parent) {
export function YieldExpression(node, parent) {
return t.isBinary(parent) ||
t.isUnaryLike(parent) ||
t.isCallExpression(parent) ||
@@ -112,17 +118,17 @@ exports.YieldExpression = function (node, parent) {
t.isNewExpression(parent) ||
t.isConditionalExpression(parent) ||
t.isYieldExpression(parent);
};
}
exports.ClassExpression = function (node, parent) {
export function ClassExpression(node, parent) {
return t.isExpressionStatement(parent);
};
}
exports.UnaryLike = function (node, parent) {
export function UnaryLike(node, parent) {
return t.isMemberExpression(parent) && parent.object === node;
};
}
exports.FunctionExpression = function (node, parent) {
export function FunctionExpression(node, parent) {
// function () {};
if (t.isExpressionStatement(parent)) {
return true;
@@ -137,10 +143,9 @@ exports.FunctionExpression = function (node, parent) {
if (t.isCallExpression(parent) && parent.callee === node) {
return true;
}
};
}
exports.AssignmentExpression =
exports.ConditionalExpression = function (node, parent) {
export function ConditionalExpression(node, parent) {
if (t.isUnaryLike(parent)) {
return true;
}
@@ -164,4 +169,6 @@ exports.ConditionalExpression = function (node, parent) {
}
return false;
};
}
export { ConditionalExpression as AssignmentExpression };

View File

@@ -1,11 +1,9 @@
var isBoolean = require("lodash/lang/isBoolean");
var each = require("lodash/collection/each");
var map = require("lodash/collection/map");
var t = require("../../types");
var crawl = function (node, state) {
state ||= {};
import isBoolean from "lodash/lang/isBoolean";
import each from "lodash/collection/each";
import map from "lodash/collection/map";
import * as t from "../../types";
function crawl(node, state = {}) {
if (t.isMemberExpression(node)) {
crawl(node.object, state);
if (node.computed) crawl(node.property, state);
@@ -22,9 +20,9 @@ var crawl = function (node, state) {
}
return state;
};
}
var isHelper = function (node) {
function isHelper(node) {
if (t.isMemberExpression(node)) {
return isHelper(node.object) || isHelper(node.property);
} else if (t.isIdentifier(node)) {
@@ -36,12 +34,12 @@ var isHelper = function (node) {
} else {
return false;
}
};
}
var isType = function (node) {
function isType(node) {
return t.isLiteral(node) || t.isObjectExpression(node) || t.isArrayExpression(node) ||
t.isIdentifier(node) || t.isMemberExpression(node);
};
}
exports.nodes = {
AssignmentExpression(node) {

View File

@@ -1,12 +1,12 @@
var sourceMap = require("source-map");
var t = require("../types");
import sourceMap from "source-map";
import * as t from "../types";
export default class SourceMap {
constructor(position, opts, code) {
this.position = position;
this.opts = opts;
if (opts.sourceMap) {
if (opts.sourceMaps) {
this.map = new sourceMap.SourceMapGenerator({
file: opts.sourceMapName,
sourceRoot: opts.sourceRoot

View File

@@ -1,4 +1,4 @@
var sortBy = require("lodash/collection/sortBy");
import sortBy from "lodash/collection/sortBy";
/**
* Returns `i`th number from `base`, continuing from 0 when `max` is reached.
@@ -81,7 +81,7 @@ export default class Whitespace {
}
}
if (endToken && endToken.type.type === "eof") {
if (endToken && endToken.type.label === "eof") {
return 1;
} else {
var lines = this.getNewlinesBetween(startToken, endToken);

View File

@@ -1,69 +1,67 @@
var lineNumbers = require("line-numbers");
var repeating = require("repeating");
var jsTokens = require("js-tokens");
var esutils = require("esutils");
var chalk = require("chalk");
var ary = require("lodash/function/ary");
import lineNumbers from "line-numbers";
import repeating from "repeating";
import jsTokens from "js-tokens";
import esutils from "esutils";
import chalk from "chalk";
var defs = {
string: chalk.red,
punctuation: chalk.white.bold,
operator: chalk.white.bold,
curly: chalk.green,
parens: chalk.blue.bold,
square: chalk.yellow,
name: chalk.white,
keyword: chalk.cyan,
number: chalk.magenta,
regex: chalk.magenta,
comment: chalk.grey,
invalid: chalk.inverse
string: chalk.red,
punctuator: chalk.bold,
curly: chalk.green,
parens: chalk.blue.bold,
square: chalk.yellow,
keyword: chalk.cyan,
number: chalk.magenta,
regex: chalk.magenta,
comment: chalk.grey,
invalid: chalk.inverse
};
var newline = /\r\n|[\n\r\u2028\u2029]/;
const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
var highlight = function (text) {
var tokenType = function (match) {
var token = jsTokens.matchToToken(match);
if (token.type === "name" && esutils.keyword.isKeywordES6(token.value)) {
return "keyword";
function getTokenType(match) {
var token = jsTokens.matchToToken(match);
if (token.type === "name" && esutils.keyword.isReservedWordES6(token.value)) {
return "keyword";
}
if (token.type === "punctuator") {
switch (token.value) {
case "{":
case "}":
return "curly";
case "(":
case ")":
return "parens";
case "[":
case "]":
return "square";
}
}
if (token.type === "punctuation") {
switch (token.value) {
case "{":
case "}":
return "curly";
case "(":
case ")":
return "parens";
case "[":
case "]":
return "square";
}
return token.type;
}
function highlight(text) {
return text.replace(jsTokens, function (...args) {
var type = getTokenType(args);
var colorize = defs[type];
if (colorize) {
return args[0].split(NEWLINE).map(str => colorize(str)).join("\n");
} else {
return args[0];
}
return token.type;
};
return text.replace(jsTokens, function (match) {
var type = tokenType(arguments);
if (type in defs) {
var colorize = ary(defs[type], 1);
return match.split(newline).map(colorize).join("\n");
}
return match;
});
};
}
module.exports = function (lines, lineNumber, colNumber) {
export default function (lines: number, lineNumber: number, colNumber: number, opts = {}): string {
colNumber = Math.max(colNumber, 0);
if (chalk.supportsColor) {
if (opts.highlightCode && chalk.supportsColor) {
lines = highlight(lines);
}
lines = lines.split(newline);
lines = lines.split(NEWLINE);
var start = Math.max(lineNumber - 3, 0);
var end = Math.min(lines.length, lineNumber + 3);
@@ -73,7 +71,7 @@ module.exports = function (lines, lineNumber, colNumber) {
end = lines.length;
}
return "\n" + lineNumbers(lines.slice(start, end), {
return lineNumbers(lines.slice(start, end), {
start: start + 1,
before: " ",
after: " | ",
@@ -82,8 +80,7 @@ module.exports = function (lines, lineNumber, colNumber) {
return;
}
if (colNumber) {
params.line += "\n" + params.before + repeating(" ", params.width) +
params.after + repeating(" ", colNumber - 1) + "^";
params.line += `\n${params.before}${repeating(" ", params.width)}${params.after}${repeating(" ", colNumber - 1)}^`;
}
params.before = params.before.replace(/^./, ">");
}

View File

@@ -1,6 +1,6 @@
var t = require("../types");
import * as t from "../types";
module.exports = function (ast, comments, tokens) {
export default function (ast, comments, tokens) {
if (ast && ast.type === "Program") {
return t.file(ast, comments || [], tokens || []);
} else {

View File

@@ -1,3 +1,3 @@
module.exports = function () {
export default function () {
return Object.create(null);
};

View File

@@ -1,24 +1,31 @@
var normalizeAst = require("./normalize-ast");
var estraverse = require("estraverse");
var codeFrame = require("./code-frame");
var acorn = require("acorn-babel");
import normalizeAst from "./normalize-ast";
import estraverse from "estraverse";
import codeFrame from "./code-frame";
import * as acorn from "../../acorn";
module.exports = function (opts, code, callback) {
export default function (opts, code, callback) {
try {
var comments = [];
var tokens = [];
var ast = acorn.parse(code, {
allowImportExportEverywhere: opts.allowImportExportEverywhere,
allowReturnOutsideFunction: !opts._anal,
ecmaVersion: opts.experimental ? 7 : 6,
playground: opts.playground,
var parseOpts = {
allowImportExportEverywhere: opts.looseModules,
allowReturnOutsideFunction: opts.looseModules,
ecmaVersion: 6,
strictMode: opts.strictMode,
sourceType: opts.sourceType,
onComment: comments,
locations: true,
features: opts.features || {},
plugins: opts.plugins || {},
onToken: tokens,
ranges: true
});
};
parseOpts.plugins.jsx = true;
parseOpts.plugins.flow = true;
var ast = acorn.parse(code, parseOpts);
estraverse.attachComments(ast, comments, tokens);
@@ -32,16 +39,23 @@ module.exports = function (opts, code, callback) {
} catch (err) {
if (!err._babel) {
err._babel = true;
var message = opts.filename + ": " + err.message;
var message = err.message = `${opts.filename}: ${err.message}`;
var loc = err.loc;
if (loc) {
var frame = codeFrame(code, loc.line, loc.column + 1);
message += frame;
err.codeFrame = codeFrame(code, loc.line, loc.column + 1, opts);
message += "\n" + err.codeFrame;
}
if (err.stack) err.stack = err.stack.replace(err.message, message);
err.message = message;
if (err.stack) {
var newStack = err.stack.replace(err.message, message);
try {
err.stack = newStack;
} catch (e) {
// `err.stack` may be a readonly property in some environments
}
}
}
throw err;

View File

@@ -1,15 +0,0 @@
/**
* 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

@@ -1,6 +1,6 @@
var util = require("util");
import * as util from "util";
exports.messages = {
export const MESSAGES = {
tailCallReassignmentDeopt: "Function reference has been reassigned so it's probably be dereferenced so we can't optimise this with confidence",
JSXNamespacedTags: "Namespace tags are not supported. ReactJSX is not XML.",
classesIllegalBareSuper: "Illegal use of bare super",
@@ -10,6 +10,7 @@ exports.messages = {
undeclaredVariable: "Reference to undeclared variable $1",
undeclaredVariableSuggestion: "Reference to undeclared variable $1 - did you mean $2?",
settersInvalidParamLength: "Setters must have exactly one parameter",
settersNoRest: "Setters aren't allowed to have a rest",
noAssignmentsInForHead: "No assignments allowed in for-in/of head",
expectedMemberExpressionOrIdentifier: "Expected type MemeberExpression or Identifier",
invalidParentForThisNode: "We don't know how to handle this node within the current parent - please open an issue",
@@ -18,25 +19,23 @@ exports.messages = {
unknownForHead: "Unknown node type $1 in ForStatement",
didYouMean: "Did you mean $1?",
evalInStrictMode: "eval is not allowed in strict mode",
codeGeneratorDeopt: "Note: The code generator has deoptimised the styling of $1 as it exceeds the max of $2."
codeGeneratorDeopt: "Note: The code generator has deoptimised the styling of $1 as it exceeds the max of $2.",
missingTemplatesDirectory: "no templates directory - this is most likely the result of a broken `npm publish`. Please report to https://github.com/babel/babel/issues",
unsupportedOutputType: "Unsupported output type $1"
};
exports.get = function (key) {
var msg = exports.messages[key];
if (!msg) throw new ReferenceError("Unknown message `" + key + "`");
export function get(key: String, ...args) {
var msg = MESSAGES[key];
if (!msg) throw new ReferenceError(`Unknown message ${JSON.stringify(key)}`);
var args = [];
for (var i = 1; i < arguments.length; i++) {
args.push(arguments[i]);
}
args = exports.parseArgs(args);
args = parseArgs(args);
return msg.replace(/\$(\d+)/g, function (str, i) {
return args[--i];
});
};
}
exports.parseArgs = function (args) {
export function parseArgs(args: Array<any>) {
return args.map(function (val) {
if (val != null && val.inspect) {
return val.inspect();
@@ -48,4 +47,4 @@ exports.parseArgs = function (args) {
}
}
});
};
}

View File

@@ -1,16 +1,16 @@
var extend = require("lodash/object/extend");
var t = require("./types");
import estraverse from "estraverse";
import extend from "lodash/object/extend";
import types from "ast-types";
import * as t from "./types";
// estraverse
var estraverse = require("estraverse");
extend(estraverse.VisitorKeys, t.VISITOR_KEYS);
// regenerator-babel/ast-types
var types = require("ast-types");
var def = types.Type.def;
var or = types.Type.or;
var def = types.Type.def;
var or = types.Type.or;
def("File")
.bases("Node")
@@ -23,41 +23,31 @@ def("AssignmentPattern")
.field("left", def("Pattern"))
.field("right", def("Expression"));
// Acorn - Same as ImportNamespaceSpecifier but `id` is `name`
def("ImportBatchSpecifier")
.bases("Specifier")
.build("name")
.field("name", def("Identifier"));
def("RestElement")
.bases("Pattern")
.build("argument")
.field("argument", def("expression"));
// Abstract references
def("VirtualPropertyExpression")
.bases("Expression")
.build("object", "property")
.field("object", def("Expression"))
.field("property", or(def("Identifier"), def("Expression")));
def("PrivateDeclaration")
def("ExportDefaultDeclaration")
.bases("Declaration")
.build("declarations")
.field("declarations", [def("Identifier")]);
.build("declaration")
.field("declaration", or(
def("Declaration"),
def("Expression"),
null
));
// Playground
def("BindMemberExpression")
.bases("Expression")
.build("object", "property", "arguments")
.field("object", def("Expression"))
.field("property", or(def("Identifier"), def("Expression")))
.field("arguments", [def("Expression")]);
def("BindFunctionExpression")
.bases("Expression")
.build("callee", "arguments")
.field("callee", def("Expression"))
.field("arguments", [def("Expression")]);
def("ExportNamedDeclaration")
.bases("Declaration")
.build("declaration")
.field("declaration", or(
def("Declaration"),
def("Expression"),
null
))
.field("specifiers", [or(
def("ExportSpecifier")
)])
.field("source", or(def("ModuleSpecifier"), null));
types.finalize();

View File

@@ -3,5 +3,5 @@ if (global._babelPolyfill) {
}
global._babelPolyfill = true;
require("core-js/shim");
require("regenerator-babel/runtime");
import "core-js/shim";
import "regenerator-babel/runtime";

View File

@@ -0,0 +1,86 @@
import generator from "../generation";
import * as messages from "../messages";
import * as util from "../util";
import File from "../transformation/file";
import each from "lodash/collection/each";
import * as t from "../types";
function buildGlobal(namespace, builder) {
var body = [];
var container = t.functionExpression(null, [t.identifier("global")], t.blockStatement(body));
var tree = t.program([t.expressionStatement(t.callExpression(container, [util.template("helper-self-global")]))]);
body.push(t.variableDeclaration("var", [
t.variableDeclarator(
namespace,
t.assignmentExpression("=", t.memberExpression(t.identifier("global"), namespace), t.objectExpression([]))
)
]));
builder(body);
return tree;
}
function buildUmd(namespace, builder) {
var body = [];
body.push(t.variableDeclaration("var", [
t.variableDeclarator(namespace, t.identifier("global"))
]));
builder(body);
var container = util.template("umd-commonjs-strict", {
FACTORY_PARAMETERS: t.identifier("global"),
BROWSER_ARGUMENTS: t.assignmentExpression("=", t.memberExpression(t.identifier("root"), namespace), t.objectExpression({})),
COMMON_ARGUMENTS: t.identifier("exports"),
AMD_ARGUMENTS: t.arrayExpression([t.literal("exports")]),
FACTORY_BODY: body,
UMD_ROOT: t.identifier("this")
});
return t.program([container]);
}
function buildVar(namespace, builder) {
var body = [];
body.push(t.variableDeclaration("var", [
t.variableDeclarator(namespace, t.objectExpression({}))
]));
builder(body);
return t.program(body);
}
function buildHelpers(body, namespace, whitelist = []) {
each(File.helpers, function (name) {
if (whitelist.length && whitelist.indexOf(name) === -1) return;
var key = t.identifier(t.toIdentifier(name));
body.push(t.expressionStatement(
t.assignmentExpression("=", t.memberExpression(namespace, key), util.template("helper-" + name))
));
});
}
export default function (whitelist, outputType = "global") {
var namespace = t.identifier("babelHelpers");
var builder = function (body) {
return buildHelpers(body, namespace, whitelist);
};
var tree;
var build = {
global: buildGlobal,
umd: buildUmd,
var: buildVar
}[outputType];
if (build) {
tree = build(namespace, builder);
} else {
throw new Error(messages.get("unsupportedOutputType", outputType));
}
return generator(tree).code;
};

View File

@@ -1,18 +1,20 @@
var merge = require("lodash/object/merge");
var path = require("path");
var fs = require("fs");
import merge from "lodash/object/merge";
import path from "path";
import fs from "fs";
var cache = {};
var jsons = {};
function exists(filename) {
if (!fs.existsSync) return false;
var cached = cache[filename];
if (cached != null) return cached;
return cache[filename] = fs.existsSync(filename);
}
module.exports = function (loc, opts) {
export default function (loc, opts = {}) {
var rel = ".babelrc";
opts ||= {};
function find(start, rel) {
var file = path.join(start, rel);
@@ -22,9 +24,9 @@ module.exports = function (loc, opts) {
var json;
try {
json = JSON.parse(content);
json = jsons[content] ||= JSON.parse(content);
} catch (err) {
err.message = file + ": " + err.message;
err.message = `${file}: ${err.message}`;
throw err;
}

View File

@@ -1,478 +0,0 @@
var sourceMapToComment = require("source-map-to-comment");
var shebangRegex = require("shebang-regex");
var isFunction = require("lodash/lang/isFunction");
var transform = require("./index");
var generate = require("../generation");
var defaults = require("lodash/object/defaults");
var includes = require("lodash/collection/includes");
var assign = require("lodash/object/assign");
var parse = require("../helpers/parse");
var Scope = require("../traversal/scope");
var slash = require("slash");
var util = require("../util");
var path = require("path");
var each = require("lodash/collection/each");
var t = require("../types");
var checkTransformerVisitor = {
enter(node, parent, scope, state) {
checkNode(state.stack, node, scope);
}
};
var checkNode = function (stack, node, scope) {
each(stack, function (pass) {
if (pass.shouldRun) return;
pass.checkNode(node, scope);
});
};
export default class File {
constructor(opts) {
this.dynamicImportedNoDefault = [];
this.dynamicImportIds = {};
this.dynamicImported = [];
this.dynamicImports = [];
this.usedHelpers = {};
this.dynamicData = {};
this.data = {};
this.lastStatements = [];
this.opts = this.normalizeOptions(opts);
this.ast = {};
this.buildTransformers();
}
static helpers = [
"inherits",
"defaults",
"prototype-properties",
"apply-constructor",
"tagged-template-literal",
"tagged-template-literal-loose",
"interop-require",
"to-array",
"to-consumable-array",
"sliced-to-array",
"object-without-properties",
"has-own",
"slice",
"bind",
"define-property",
"async-to-generator",
"interop-require-wildcard",
"typeof",
"extends",
"get",
"set",
"class-call-check",
"object-destructuring-empty",
"temporal-undefined",
"temporal-assert-defined",
"self-global"
];
static validOptions = [
"filename",
"filenameRelative",
"blacklist",
"whitelist",
"loose",
"optional",
"modules",
"sourceMap",
"sourceMapName",
"sourceFileName",
"sourceRoot",
"moduleRoot",
"moduleIds",
"comments",
"reactCompat",
"keepModuleIdExtensions",
"code",
"ast",
"playground",
"experimental",
"externalHelpers",
"auxiliaryComment",
"compact",
"returnUsedHelpers",
"resolveModuleSource",
"moduleId",
// legacy
"format",
// these are used by plugins
"ignore",
"only",
"extensions",
"accept"
];
normalizeOptions(opts) {
opts = assign({}, opts);
for (var key in opts) {
if (key[0] !== "_" && File.validOptions.indexOf(key) < 0) {
throw new ReferenceError("Unknown option: " + key);
}
}
defaults(opts, {
keepModuleIdExtensions: false,
resolveModuleSource: null,
returnUsedHelpers: false,
externalHelpers: false,
auxilaryComment: "",
experimental: false,
reactCompat: false,
playground: false,
moduleIds: false,
blacklist: [],
whitelist: [],
sourceMap: false,
optional: [],
comments: true,
filename: "unknown",
modules: "common",
compact: "auto",
loose: [],
code: true,
ast: true
});
// normalize windows path separators to unix
opts.filename = slash(opts.filename);
if (opts.sourceRoot) {
opts.sourceRoot = slash(opts.sourceRoot);
}
if (opts.moduleId) {
opts.moduleIds = true;
}
opts.basename = path.basename(opts.filename, path.extname(opts.filename));
opts.blacklist = util.arrayify(opts.blacklist);
opts.whitelist = util.arrayify(opts.whitelist);
opts.optional = util.arrayify(opts.optional);
opts.compact = util.booleanify(opts.compact);
opts.loose = util.arrayify(opts.loose);
if (includes(opts.loose, "all") || includes(opts.loose, true)) {
opts.loose = Object.keys(transform.transformers);
}
defaults(opts, {
moduleRoot: opts.sourceRoot
});
defaults(opts, {
sourceRoot: opts.moduleRoot
});
defaults(opts, {
filenameRelative: opts.filename
});
defaults(opts, {
sourceFileName: opts.filenameRelative,
sourceMapName: opts.filenameRelative
});
if (opts.playground) {
opts.experimental = true;
}
if (opts.externalHelpers) {
this.set("helpersNamespace", t.identifier("babelHelpers"));
}
opts.blacklist = transform._ensureTransformerNames("blacklist", opts.blacklist);
opts.whitelist = transform._ensureTransformerNames("whitelist", opts.whitelist);
opts.optional = transform._ensureTransformerNames("optional", opts.optional);
opts.loose = transform._ensureTransformerNames("loose", opts.loose);
if (opts.reactCompat) {
opts.optional.push("reactCompat");
console.error("The reactCompat option has been moved into the optional transformer `reactCompat`");
}
var ensureEnabled = function (key) {
var namespace = transform.transformerNamespaces[key];
if (namespace === "es7") opts.experimental = true;
if (namespace === "playground") opts.playground = true;
};
each(opts.whitelist, ensureEnabled);
each(opts.optional, ensureEnabled);
return opts;
};
isLoose(key) {
return includes(this.opts.loose, key);
}
buildTransformers() {
var file = this;
var transformers = {};
var secondaryStack = [];
var stack = [];
each(transform.transformers, function (transformer, key) {
var pass = transformers[key] = transformer.buildPass(file);
if (pass.canRun(file)) {
stack.push(pass);
if (transformer.secondPass) {
secondaryStack.push(pass);
}
if (transformer.manipulateOptions) {
transformer.manipulateOptions(file.opts, file);
}
}
});
this.transformerStack = stack.concat(secondaryStack);
this.transformers = transformers;
}
debug(msg) {
var parts = this.opts.filename;
if (msg) parts += ": " + msg;
util.debug(parts);
}
getModuleFormatter(type) {
var ModuleFormatter = isFunction(type) ? type : transform.moduleFormatters[type];
if (!ModuleFormatter) {
var loc = util.resolve(type);
if (loc) ModuleFormatter = require(loc);
}
if (!ModuleFormatter) {
throw new ReferenceError("Unknown module formatter type " + JSON.stringify(type));
}
return new ModuleFormatter(this);
}
parseShebang(code) {
var shebangMatch = shebangRegex.exec(code);
if (shebangMatch) {
this.shebang = shebangMatch[0];
// remove shebang
code = code.replace(shebangRegex, "");
}
return code;
}
set(key, val) {
return this.data[key] = val;
};
setDynamic(key, fn) {
this.dynamicData[key] = fn;
}
get(key) {
var data = this.data[key];
if (data) {
return data;
} else {
var dynamic = this.dynamicData[key];
if (dynamic) {
return this.set(key, dynamic());
}
}
}
addImport(source, name, noDefault) {
name ||= source;
var id = this.dynamicImportIds[name];
if (!id) {
id = this.dynamicImportIds[name] = this.scope.generateUidIdentifier(name);
var specifiers = [t.importSpecifier(t.identifier("default"), id)];
var declar = t.importDeclaration(specifiers, t.literal(source));
declar._blockHoist = 3;
this.dynamicImported.push(declar);
if (noDefault) this.dynamicImportedNoDefault.push(declar);
this.moduleFormatter.importSpecifier(specifiers[0], declar, this.dynamicImports);
}
return id;
}
isConsequenceExpressionStatement(node) {
return t.isExpressionStatement(node) && this.lastStatements.indexOf(node) >= 0;
}
attachAuxiliaryComment(node) {
var comment = this.opts.auxiliaryComment;
if (comment) {
node.leadingComments ||= [];
node.leadingComments.push({
type: "Line",
value: " " + comment
});
}
return node;
}
addHelper(name) {
if (!includes(File.helpers, name)) {
throw new ReferenceError("Unknown helper " + name);
}
var program = this.ast.program;
var declar = program._declarations && program._declarations[name];
if (declar) return declar.id;
this.usedHelpers[name] = true;
var runtime = this.get("helpersNamespace");
if (runtime) {
name = t.identifier(t.toIdentifier(name));
return t.memberExpression(runtime, name);
} else {
var ref = util.template(name);
ref._compact = true;
var uid = this.scope.generateUidIdentifier(name);
this.scope.push({
key: name,
id: uid,
init: ref
});
return uid;
}
}
logDeopt() {
// todo, (node, msg)
}
errorWithNode(node, msg, Error) {
Error ||= SyntaxError;
var loc = node.loc.start;
var err = new Error("Line " + loc.line + ": " + msg);
err.loc = loc;
return err;
}
addCode(code) {
code = (code || "") + "";
this.code = code;
return this.parseShebang(code);
}
parse(code) {
code = this.addCode(code);
var opts = this.opts;
opts.allowImportExportEverywhere = this.isLoose("es6.modules");
opts.strictMode = this.transformers.useStrict.canRun();
return parse(opts, code, (tree) => {
this.transform(tree);
return this.generate();
});
}
transform(ast) {
this.debug();
this.ast = ast;
this.lastStatements = t.getLastStatements(ast.program);
this.scope = new Scope(ast.program, ast, null, this);
var modFormatter = this.moduleFormatter = this.getModuleFormatter(this.opts.modules);
if (modFormatter.init && this.transformers["es6.modules"].canRun()) {
modFormatter.init();
}
this.checkNode(ast);
this.call("pre");
each(this.transformerStack, function (pass) {
pass.transform();
});
this.call("post");
}
call(key) {
var stack = this.transformerStack;
for (var i = 0; i < stack.length; i++) {
var transformer = stack[i].transformer;
if (transformer[key]) {
transformer[key](this);
}
}
}
checkNode(node, scope) {
var stack = this.transformerStack;
scope ||= this.scope;
checkNode(stack, node, scope);
scope.traverse(node, checkTransformerVisitor, {
stack: stack
});
}
generate() {
var opts = this.opts;
var ast = this.ast;
var result = {
code: "",
map: null,
ast: null
};
if (this.opts.returnUsedHelpers) {
result.usedHelpers = Object.keys(this.usedHelpers);
}
if (opts.ast) result.ast = ast;
if (!opts.code) return result;
var _result = generate(ast, opts, this.code);
result.code = _result.code;
result.map = _result.map;
if (this.shebang) {
// add back shebang
result.code = this.shebang + "\n" + result.code;
}
if (opts.sourceMap === "inline") {
result.code += "\n" + sourceMapToComment(result.map);
result.map = null;
}
return result;
}
}

View File

@@ -0,0 +1,550 @@
import convertSourceMap from "convert-source-map";
import * as optionParsers from "./option-parsers";
import shebangRegex from "shebang-regex";
import TraversalPath from "../../traversal/path";
import isFunction from "lodash/lang/isFunction";
import isAbsolute from "path-is-absolute";
import resolveRc from "../../tools/resolve-rc";
import sourceMap from "source-map";
import transform from "./../index";
import generate from "../../generation";
import defaults from "lodash/object/defaults";
import includes from "lodash/collection/includes";
import traverse from "../../traversal";
import assign from "lodash/object/assign";
import Logger from "./logger";
import parse from "../../helpers/parse";
import Scope from "../../traversal/scope";
import slash from "slash";
import * as util from "../../util";
import path from "path";
import each from "lodash/collection/each";
import * as t from "../../types";
var checkTransformerVisitor = {
enter(node, parent, scope, state) {
checkNode(state.stack, node, scope);
}
};
function checkNode(stack, node, scope) {
each(stack, function (pass) {
if (pass.shouldRun || pass.ran) return;
pass.checkNode(node, scope);
});
}
export default class File {
constructor(opts = {}) {
this.dynamicImportedNoDefault = [];
this.dynamicImportIds = {};
this.dynamicImported = [];
this.dynamicImports = [];
this.usedHelpers = {};
this.dynamicData = {};
this.data = {};
this.lastStatements = [];
this.log = new Logger(this, opts.filename || "unknown");
this.opts = this.normalizeOptions(opts);
this.ast = {};
this.buildTransformers();
}
static helpers = [
"inherits",
"defaults",
"create-class",
"create-decorated-class",
"tagged-template-literal",
"tagged-template-literal-loose",
"interop-require",
"to-array",
"to-consumable-array",
"sliced-to-array",
"sliced-to-array-loose",
"object-without-properties",
"has-own",
"slice",
"bind",
"define-property",
"async-to-generator",
"interop-require-wildcard",
"typeof",
"extends",
"get",
"set",
"class-call-check",
"object-destructuring-empty",
"temporal-undefined",
"temporal-assert-defined",
"self-global"
];
static options = require("./options");
normalizeOptions(opts: Object) {
opts = assign({}, opts);
if (opts.filename && isAbsolute(opts.filename)) {
opts = resolveRc(opts.filename, opts);
}
//
for (let key in opts) {
if (key[0] === "_") continue;
let option = File.options[key];
if (!option) this.log.error(`Unknown option: ${key}`, ReferenceError);
}
for (let key in File.options) {
let option = File.options[key];
var val = opts[key];
if (!val && option.optional) continue;
if (val == null) val = option.default || val;
var optionParser = optionParsers[option.type];
if (optionParser) val = optionParser(key, val);
if (option.alias) {
opts[option.alias] ||= val;
} else {
opts[key] = val;
}
}
if (opts.inputSourceMap) {
opts.sourceMaps = true;
}
// normalize windows path separators to unix
opts.filename = slash(opts.filename);
if (opts.sourceRoot) {
opts.sourceRoot = slash(opts.sourceRoot);
}
if (opts.moduleId) {
opts.moduleIds = true;
}
opts.basename = path.basename(opts.filename, path.extname(opts.filename));
opts.ignore = util.arrayify(opts.ignore, util.regexify);
opts.only = util.arrayify(opts.only, util.regexify);
defaults(opts, {
moduleRoot: opts.sourceRoot
});
defaults(opts, {
sourceRoot: opts.moduleRoot
});
defaults(opts, {
filenameRelative: opts.filename
});
defaults(opts, {
sourceFileName: opts.filenameRelative,
sourceMapName: opts.filenameRelative
});
//
if (opts.externalHelpers) {
this.set("helpersNamespace", t.identifier("babelHelpers"));
}
return opts;
};
isLoose(key: string) {
return includes(this.opts.loose, key);
}
buildPlugins(stack) {
}
buildTransformers() {
var file = this;
var transformers = {};
var secondaryStack = [];
var stack = [];
this.buildPlugins(stack);
each(transform.transformers, function (transformer, key) {
var pass = transformers[key] = transformer.buildPass(file);
if (pass.canTransform()) {
stack.push(pass);
if (transformer.metadata.secondPass) {
secondaryStack.push(pass);
}
if (transformer.manipulateOptions) {
transformer.manipulateOptions(file.opts, file);
}
}
});
this.transformerStack = stack.concat(secondaryStack);
this.transformers = transformers;
}
getModuleFormatter(type: string) {
var ModuleFormatter = isFunction(type) ? type : transform.moduleFormatters[type];
if (!ModuleFormatter) {
var loc = util.resolve(type);
if (loc) ModuleFormatter = require(loc);
}
if (!ModuleFormatter) {
throw new ReferenceError(`Unknown module formatter type ${JSON.stringify(type)}`);
}
return new ModuleFormatter(this);
}
parseInputSourceMap(code: string) {
var opts = this.opts;
if (opts.inputSourceMap !== false) {
var inputMap = convertSourceMap.fromSource(code);
if (inputMap) {
opts.inputSourceMap = inputMap.toObject();
code = convertSourceMap.removeComments(code);
}
}
return code;
}
parseShebang(code: string) {
var shebangMatch = shebangRegex.exec(code);
if (shebangMatch) {
this.shebang = shebangMatch[0];
// remove shebang
code = code.replace(shebangRegex, "");
}
return code;
}
set(key: string, val): any {
return this.data[key] = val;
};
setDynamic(key: string, fn: Function) {
this.dynamicData[key] = fn;
}
get(key: string): any {
var data = this.data[key];
if (data) {
return data;
} else {
var dynamic = this.dynamicData[key];
if (dynamic) {
return this.set(key, dynamic());
}
}
}
resolveModuleSource(source: string): string {
var resolveModuleSource = this.opts.resolveModuleSource;
if (resolveModuleSource) source = resolveModuleSource(source, this.opts.filename);
return source;
}
addImport(source: string, name?: string, noDefault?: boolean): Object {
name ||= source;
var id = this.dynamicImportIds[name];
if (!id) {
source = this.resolveModuleSource(source);
id = this.dynamicImportIds[name] = this.scope.generateUidIdentifier(name);
var specifiers = [t.importDefaultSpecifier(id)];
var declar = t.importDeclaration(specifiers, t.literal(source));
declar._blockHoist = 3;
this.dynamicImported.push(declar);
if (noDefault) this.dynamicImportedNoDefault.push(declar);
if (this.transformers["es6.modules"].canTransform()) {
this.moduleFormatter.importSpecifier(specifiers[0], declar, this.dynamicImports);
} else {
this.dynamicImports.push(declar);
}
}
return id;
}
isConsequenceExpressionStatement(node: Object): boolean {
return t.isExpressionStatement(node) && this.lastStatements.indexOf(node) >= 0;
}
attachAuxiliaryComment(node: Object): Object {
var comment = this.opts.auxiliaryComment;
if (comment) {
node.leadingComments ||= [];
node.leadingComments.push({
type: "Line",
value: " " + comment
});
}
return node;
}
addHelper(name: string): Object {
if (!includes(File.helpers, name)) {
throw new ReferenceError(`Unknown helper ${name}`);
}
var program = this.ast.program;
var declar = program._declarations && program._declarations[name];
if (declar) return declar.id;
this.usedHelpers[name] = true;
var generator = this.get("helperGenerator");
var runtime = this.get("helpersNamespace");
if (generator) {
return generator(name);
} else if (runtime) {
var id = t.identifier(t.toIdentifier(name));
return t.memberExpression(runtime, id);
} else {
var ref = util.template("helper-" + name);
ref._compact = true;
var uid = this.scope.generateUidIdentifier(name);
this.scope.push({
key: name,
id: uid,
init: ref
});
return uid;
}
}
errorWithNode(node, msg, Error = SyntaxError) {
var loc = node.loc.start;
var err = new Error(`Line ${loc.line}: ${msg}`);
err.loc = loc;
return err;
}
addCode(code: string) {
code = (code || "") + "";
code = this.parseInputSourceMap(code);
this.code = code;
return this.parseShebang(code);
}
shouldIgnore() {
var opts = this.opts;
var filename = opts.filename;
var ignore = opts.ignore;
var only = opts.only;
if (only.length) {
for (var i = 0; i < only.length; i++) {
if (only[i].test(filename)) return false;
}
return true;
} else if (ignore.length) {
for (var i = 0; i < ignore.length; i++) {
if (ignore[i].test(filename)) return true;
}
}
return false;
}
parse(code: string) {
if (this.shouldIgnore()) {
return {
code: code,
map: null,
ast: null
};
}
code = this.addCode(code);
var opts = this.opts;
//
var parseOpts = {
highlightCode: opts.highlightCode,
filename: opts.filename,
plugins: {}
};
var features = parseOpts.features = {};
for (var key in this.transformers) {
var transformer = this.transformers[key];
features[key] = transformer.canTransform();
}
parseOpts.looseModules = this.isLoose("es6.modules");
parseOpts.strictMode = features.strict;
parseOpts.sourceType = "module";
//
return parse(parseOpts, code, (tree) => {
this.transform(tree);
return this.generate();
});
}
setAst(ast) {
this.path = TraversalPath.get(null, null, ast, ast, "program", this);
this.scope = this.path.scope;
this.ast = ast;
this.path.traverse({
enter(node, parent, scope) {
if (this.isScope()) {
for (var key in scope.bindings) {
scope.bindings[key].setTypeAnnotation();
}
}
}
});
}
transform(ast) {
this.log.debug();
this.setAst(ast);
this.lastStatements = t.getLastStatements(ast.program);
var modFormatter = this.moduleFormatter = this.getModuleFormatter(this.opts.modules);
if (modFormatter.init && this.transformers["es6.modules"].canTransform()) {
modFormatter.init();
}
this.checkNode(ast);
this.call("pre");
each(this.transformerStack, function (pass) {
pass.transform();
});
this.call("post");
}
call(key: string) {
var stack = this.transformerStack;
for (var i = 0; i < stack.length; i++) {
var transformer = stack[i].transformer;
var fn = transformer[key];
if (fn) fn(this);
}
}
checkNode(node, scope) {
if (Array.isArray(node)) {
for (var i = 0; i < node.length; i++) {
this.checkNode(node[i], scope);
}
return;
}
var stack = this.transformerStack;
scope ||= this.scope;
checkNode(stack, node, scope);
scope.traverse(node, checkTransformerVisitor, {
stack: stack
});
}
mergeSourceMap(map: Object) {
var opts = this.opts;
var inputMap = opts.inputSourceMap;
if (inputMap) {
map.sources[0] = inputMap.file;
var inputMapConsumer = new sourceMap.SourceMapConsumer(inputMap);
var outputMapConsumer = new sourceMap.SourceMapConsumer(map);
var outputMapGenerator = sourceMap.SourceMapGenerator.fromSourceMap(outputMapConsumer);
outputMapGenerator.applySourceMap(inputMapConsumer);
var mergedMap = outputMapGenerator.toJSON();
mergedMap.sources = inputMap.sources
mergedMap.file = inputMap.file;
return mergedMap;
}
return map;
}
generate(): {
usedHelpers?: Array<string>;
code: string;
map?: Object;
ast?: Object;
} {
var opts = this.opts;
var ast = this.ast;
var result = {
code: "",
map: null,
ast: null
};
if (this.opts.returnUsedHelpers) {
result.usedHelpers = Object.keys(this.usedHelpers);
}
if (opts.ast) result.ast = ast;
if (!opts.code) return result;
var _result = generate(ast, opts, this.code);
result.code = _result.code;
result.map = _result.map;
if (this.shebang) {
// add back shebang
result.code = `${this.shebang}\n${result.code}`;
}
result.map = this.mergeSourceMap(result.map);
if (opts.sourceMaps === "inline" || opts.sourceMaps === "both") {
result.code += "\n" + convertSourceMap.fromObject(result.map).toComment();
}
if (opts.sourceMaps === "inline") {
result.map = null;
}
return result;
}
}

View File

@@ -0,0 +1,32 @@
import * as util from "../../util";
export default class Logger {
constructor(file: File, filename: string) {
this.filename = filename;
this.file = file;
}
_buildMessage(msg: string): string {
var parts = this.filename;
if (msg) parts += `: ${msg}`;
return parts;
}
error(msg: string, Constructor = Error) {
throw new Constructor(this._buildMessage(msg));
}
deprecate(msg) {
if (!this.file.opts.suppressDeprecationMessages) {
console.error(msg);
}
}
debug(msg: string) {
util.debug(this._buildMessage(msg));
}
deopt(node: Object, msg: string) {
util.debug(this._buildMessage(msg));
}
}

View File

@@ -0,0 +1,24 @@
import transform from "./../index";
import * as util from "../../util";
export function transformerList(key, val) {
val = util.arrayify(val);
if (val.indexOf("all") >= 0 || val.indexOf(true) >= 0) {
val = Object.keys(transform.transformers);
}
return transform._ensureTransformerNames(key, val);
}
export function boolean(key, val) {
return !!val;
}
export function booleanString(key, val) {
return util.booleanify(val);
}
export function list(key, val) {
return util.list(val);
}

View File

@@ -0,0 +1,167 @@
{
"filename": {
"type": "string",
"description": "Filename to use when reading from stdin - this will be used in source-maps, errors etc",
"default": "unknown",
"shorthand": "f"
},
"filenameRelative": {
"hidden": true,
"type": "string"
},
"inputSourceMap": {
"hidden": true
},
"moduleId": {
},
"highlightTheme": {
"hidden": true
},
"highlightCode": {
"description": "ANSI syntax highlight code frames",
"type": "boolean",
"default": true
},
"suppressDeprecationMessages": {
"type": "boolean",
"default": false,
"hidden": true
},
"resolveModuleSource": {
"hidden": true
},
"experimental": {
"description": "Enable all ES7+ transformers",
"shorthand": "e",
"type": "boolean",
"default": false
},
"blacklist": {
"type": "transformerList",
"description": "Blacklist of transformers to NOT use",
"shorthand": "b"
},
"whitelist": {
"type": "transformerList",
"optional": true,
"description": "Whitelist of transformers to ONLY use",
"shorthand": "l"
},
"optional": {
"type": "transformerList",
"description": "List of optional transformers to enable"
},
"modules": {
"type": "string",
"description": "Module formatter type to use [common]",
"default": "common",
"shorthand": "m"
},
"moduleIds": {
"type": "boolean",
"default": false,
"shorthand": "M"
},
"loose": {
"type": "transformerList",
"description": "List of transformers to enable loose mode ON",
"shorthand": "L"
},
"ignore": {
"type": "list"
},
"only": {
"type": "list"
},
"code": {
"hidden": true,
"default": true,
"type": "boolean"
},
"ast": {
"hidden": true,
"default": true,
"type": "boolean"
},
"comments": {
"type": "boolean",
"default": true
},
"compact": {
"type": "string",
"default": "auto"
},
"keepModuleIdExtensions": {
"type": "boolean",
"description": "Keep extensions when generating module ids",
"default": false,
"shorthand": "k"
},
"auxiliaryComment": {
"type": "string",
"default": "",
"shorthand": "a"
},
"externalHelpers": {
"type": "string",
"default": false,
"shorthand": "r"
},
"returnUsedHelpers": {
"type": "boolean",
"default": false,
"hidden": true
},
"sourceMap": {
"alias": "sourceMaps",
"hidden": true
},
"sourceMaps": {
"type": "booleanString",
"default": false,
"shorthand": "s"
},
"sourceMapName": {
"type": "string"
},
"sourceFileName": {
"type": "string"
},
"sourceRoot": {
"type": "string"
},
"moduleRoot": {
"type": "string"
}
}

View File

@@ -1,7 +1,7 @@
var explode = require("./explode-assignable-expression");
var t = require("../../types");
import explode from "./explode-assignable-expression";
import * as t from "../../types";
module.exports = function (exports, opts) {
export default function (exports, opts) {
var isAssignment = function (node) {
return node.operator === opts.operator + "=";
};
@@ -33,8 +33,7 @@ module.exports = function (exports, opts) {
var nodes = [];
var exploded = explode(node.left, nodes, file, scope);
nodes.push(buildAssignment(exploded.ref, opts.build(exploded.uid, node.right)));
return t.toSequenceExpression(nodes, scope);
return nodes;
};
exports.BinaryExpression = function (node) {

View File

@@ -1,6 +1,6 @@
var t = require("../../types");
import * as t from "../../types";
module.exports = function build(node, buildBody) {
export default function build(node, buildBody) {
var self = node.blocks.shift();
if (!self) return;
@@ -20,4 +20,4 @@ module.exports = function build(node, buildBody) {
self.right,
t.blockStatement([child])
);
};
}

View File

@@ -1,7 +1,7 @@
var explode = require("./explode-assignable-expression");
var t = require("../../types");
import explode from "./explode-assignable-expression";
import * as t from "../../types";
module.exports = function (exports, opts) {
export default function (exports, opts) {
var buildAssignment = function (left, right) {
return t.assignmentExpression("=", left, right);
};
@@ -40,6 +40,6 @@ module.exports = function (exports, opts) {
// todo: duplicate expression node
nodes.push(exploded.ref);
return t.toSequenceExpression(nodes, scope);
return nodes;
};
};

View File

@@ -3,13 +3,13 @@
// jsx
var isString = require("lodash/lang/isString");
var messages = require("../../messages");
var esutils = require("esutils");
var react = require("./react");
var t = require("../../types");
import isString from "lodash/lang/isString";
import * as messages from "../../messages";
import esutils from "esutils";
import * as react from "./react";
import * as t from "../../types";
module.exports = function (exports, opts) {
export default function (exports, opts) {
exports.check = function (node) {
if (t.isJSX(node)) return true;
if (react.isCreateClass(node)) return true;
@@ -27,7 +27,7 @@ module.exports = function (exports, opts) {
};
exports.JSXNamespacedName = function (node, parent, scope, file) {
throw file.errorWithNode(node, messages.get("JSXNamespacedTags"));
throw this.errorWithNode(messages.get("JSXNamespacedTags"));
};
exports.JSXMemberExpression = {
@@ -252,8 +252,8 @@ module.exports = function (exports, opts) {
}
};
exports.ExportDeclaration = function (node, parent, scope, file) {
if (node.default && react.isCreateClass(node.declaration)) {
exports.ExportDefaultDeclaration = function (node, parent, scope, file) {
if (react.isCreateClass(node.declaration)) {
addDisplayName(file.opts.basename, node.declaration);
}
};

View File

@@ -0,0 +1,11 @@
import * as t from "../../types";
export default function (node) {
var container = t.functionExpression(null, [], node.body, node.generator, node.async);
container.shadow = true;
var call = t.callExpression(container, []);
if (node.generator) call = t.yieldExpression(call, true);
return t.returnStatement(call);
}

View File

@@ -1,39 +1,45 @@
var cloneDeep = require("lodash/lang/cloneDeep");
var traverse = require("../../traversal");
var clone = require("lodash/lang/clone");
var each = require("lodash/collection/each");
var has = require("lodash/object/has");
var t = require("../../types");
import cloneDeep from "lodash/lang/cloneDeep";
import traverse from "../../traversal";
import each from "lodash/collection/each";
import has from "lodash/object/has";
import * as t from "../../types";
exports.push = function (mutatorMap, key, kind, computed, value) {
var alias;
export function push(mutatorMap, key, kind, computed, value) {
var alias = t.toKeyAlias({ computed }, key);
if (t.isIdentifier(key)) {
alias = key.name;
if (computed) alias = "computed:" + alias;
} else if (t.isLiteral(key)) {
alias = String(key.value);
} else {
alias = JSON.stringify(traverse.removeProperties(cloneDeep(key)));
}
var map;
if (has(mutatorMap, alias)) {
map = mutatorMap[alias];
} else {
map = {};
}
var map = {};
if (has(mutatorMap, alias)) map = mutatorMap[alias];
mutatorMap[alias] = map;
map._key = key;
if (computed) {
map._computed = true;
}
if (computed) map._computed = true;
map[kind] = value;
};
}
exports.build = function (mutatorMap) {
export function hasComputed(mutatorMap) {
for (var key in mutatorMap) {
if (mutatorMap[key]._computed) {
return true;
}
}
return false;
}
export function toComputedObjectFromClass(obj) {
var objExpr = t.arrayExpression([]);
for (var i = 0; i < obj.properties.length; i++) {
var prop = obj.properties[i];
var val = prop.value;
val.properties.unshift(t.property("init", t.identifier("key"), t.toComputedKey(prop)));
objExpr.elements.push(val);
}
return objExpr;
}
export function toClassObject(mutatorMap) {
var objExpr = t.objectExpression([]);
each(mutatorMap, function (map) {
@@ -41,28 +47,16 @@ exports.build = function (mutatorMap) {
var propNode = t.property("init", map._key, mapNode, map._computed);
if (!map.get && !map.set) {
map.writable = t.literal(true);
}
if (map.enumerable === false) {
delete map.enumerable;
} else {
map.enumerable = t.literal(true);
}
map.configurable = t.literal(true);
each(map, function (node, key) {
if (key[0] === "_") return;
node = clone(node);
var inheritNode = node;
if (t.isMethodDefinition(node)) node = node.value;
if (t.isMethodDefinition(node) || t.isClassProperty(node)) node = node.value;
var prop = t.property("init", t.identifier(key), node);
t.inheritsComments(prop, inheritNode);
t.removeComments(inheritNode);
mapNode.properties.push(prop);
});
@@ -70,4 +64,14 @@ exports.build = function (mutatorMap) {
});
return objExpr;
};
}
export function toDefineObject(mutatorMap) {
each(mutatorMap, function (map) {
if (map.value) map.writable = t.literal(true);
map.configurable = t.literal(true);
map.enumerable = t.literal(true);
});
return toClassObject(mutatorMap);
}

View File

@@ -1,4 +1,4 @@
var t = require("../../types");
import * as t from "../../types";
var getObjRef = function (node, nodes, file, scope) {
var ref;
@@ -23,7 +23,7 @@ var getObjRef = function (node, nodes, file, scope) {
return ref;
}
} else {
throw new Error("We can't explode this node type " + node.type);
throw new Error(`We can't explode this node type ${node.type}`);
}
var temp = scope.generateUidBasedOnNode(ref);
@@ -45,7 +45,7 @@ var getPropRef = function (node, nodes, file, scope) {
return temp;
};
module.exports = function (node, nodes, file, scope, allowedSingleIdent) {
export default function (node, nodes, file, scope, allowedSingleIdent) {
var obj;
if (t.isIdentifier(node) && allowedSingleIdent) {
obj = node;

View File

@@ -1,6 +1,6 @@
var t = require("../../types");
import * as t from "../../types";
module.exports = function (node) {
export default function (node) {
var lastNonDefault = 0;
for (var i = 0; i < node.params.length; i++) {
if (!t.isAssignmentPattern(node.params[i])) lastNonDefault = i + 1;

View File

@@ -1,12 +1,12 @@
var getFunctionArity = require("./get-function-arity");
var util = require("../../util");
var t = require("../../types");
import getFunctionArity from "./get-function-arity";
import * as util from "../../util";
import * as t from "../../types";
var visitor = {
enter(node, parent, scope, state) {
// check if this node is a referenced identifier that matches the same as our
// function id
if (!t.isReferencedIdentifier(node, parent, { name: state.name })) return;
if (!this.isReferencedIdentifier({ name: state.name })) return;
// check that we don't have a local variable declared as that removes the need
// for the wrapper
@@ -49,7 +49,7 @@ var visit = function (node, name, scope) {
selfReference: false,
outerDeclar: scope.getBindingIdentifier(name),
references: [],
name: name,
name: name
};
// check to see if we have a local binding of the id we're setting inside of
@@ -89,7 +89,12 @@ var visit = function (node, name, scope) {
return state;
};
exports.property = function (node, file, scope) {
export function custom(node, id, scope) {
var state = visit(node, id.name, scope);
return wrap(state, node, id, scope);
}
export function property(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
@@ -99,28 +104,35 @@ exports.property = function (node, file, scope) {
var method = node.value;
var state = visit(method, name, scope);
node.value = wrap(state, method, id, scope);
};
}
exports.bare = function (node, parent, scope) {
export function bare(node, parent, scope) {
// has an `id` so we don't need to infer one
if (node.id) return;
if (node.id) return node;
var id;
if (t.isProperty(parent) && parent.kind === "init" && !parent.computed) {
if (t.isProperty(parent) && parent.kind === "init" && (!parent.computed || t.isLiteral(parent.key))) {
// { foo() {} };
id = parent.key;
} else if (t.isVariableDeclarator(parent)) {
// var foo = function () {};
id = parent.id;
} else {
return node;
}
var name;
if (t.isLiteral(id)) {
name = id.value;
} else if (t.isIdentifier(id)) {
name = id.name;
} else {
return;
}
if (!t.isIdentifier(id)) return;
var name = t.toIdentifier(id.name);
name = t.toIdentifier(name);
id = t.identifier(name);
var state = visit(node, name, scope);
return wrap(state, node, id, scope);
};
}

View File

@@ -1,8 +1,8 @@
var t = require("../../types");
import * as t from "../../types";
var isCreateClassCallExpression = t.buildMatchMemberExpression("React.createClass");
exports.isCreateClass = function (node) {
export function isCreateClass(node) {
if (!node || !t.isCallExpression(node)) return false;
// not React.createClass call member object
@@ -17,10 +17,10 @@ exports.isCreateClass = function (node) {
if (!t.isObjectExpression(first)) return false;
return true;
};
}
exports.isReactComponent = t.buildMatchMemberExpression("React.Component");
export var isReactComponent = t.buildMatchMemberExpression("React.Component");
exports.isCompatTag = function (tagName) {
export function isCompatTag(tagName) {
return tagName && /^[a-z]|\-/.test(tagName);
};
}

View File

@@ -0,0 +1,13 @@
import pull from "lodash/array/pull";
import * as t from "../../types";
export function is(node, flag) {
return t.isLiteral(node) && node.regex && node.regex.flags.indexOf(flag) >= 0;
}
export function pullFlag(node, flag) {
var flags = node.regex.flags.split("");
if (node.regex.flags.indexOf(flag) < 0) return;
pull(flags, flag);
node.regex.flags = flags.join("");
}

View File

@@ -1,7 +1,7 @@
var t = require("../../types");
import * as t from "../../types";
var visitor = {
enter(node) {
var awaitVisitor = {
enter(node, parent, scope, state) {
if (t.isFunction(node)) this.skip();
if (t.isAwaitExpression(node)) {
@@ -16,15 +16,25 @@ var visitor = {
}
};
module.exports = function (node, callId, scope) {
var referenceVisitor = {
enter(node, parent, scope, state) {
var name = state.id.name;
if (t.isReferencedIdentifier(node, parent, { name: name }) && scope.bindingIdentifierEquals(name, state.id)) {
return state.ref ||= scope.generateUidIdentifier(name);
}
}
};
export default function (node, callId, scope) {
node.async = false;
node.generator = true;
scope.traverse(node, visitor);
scope.traverse(node, awaitVisitor, state);
var call = t.callExpression(callId, [node]);
var id = node.id;
delete node.id;
node.id = null;
if (t.isFunctionDeclaration(node)) {
var declar = t.variableDeclaration("let", [
@@ -33,6 +43,16 @@ module.exports = function (node, callId, scope) {
declar._blockHoist = true;
return declar;
} else {
if (id) {
var state = { id: id };
scope.traverse(node, referenceVisitor, state);
if (state.ref) {
scope.parent.push({ id: state.ref });
return t.assignmentExpression("=", state.ref, call);
}
}
return call;
}
};

View File

@@ -1,19 +1,15 @@
module.exports = ReplaceSupers;
var messages = require("../../messages");
var t = require("../../types");
import * as messages from "../../messages";
import * as t from "../../types";
var isIllegalBareSuper = function (node, parent) {
if (!isSuper(node, parent)) return false;
function isIllegalBareSuper(node, parent) {
if (!t.isSuper(node)) return false;
if (t.isMemberExpression(parent, { computed: false })) return false;
if (t.isCallExpression(parent, { callee: node })) return false;
return true;
};
var isSuper = function (node, parent) {
return t.isIdentifier(node, { name: "super" }) && t.isReferenced(node, parent);
};
}
var visitor = {
enter(node, parent, scope, state) {
@@ -39,7 +35,7 @@ var visitor = {
var callback = self.specHandle;
if (self.isLoose) callback = self.looseHandle;
return callback.call(self, getThisReference, node, parent);
return callback.call(self, this, getThisReference);
}
};
@@ -47,22 +43,23 @@ export default class ReplaceSupers {
/**
* Description
*
* @param {Object} opts
* @param {Boolean} [inClass]
*/
constructor(opts, inClass) {
constructor(opts: Object, inClass?: boolean = false) {
this.topLevelThisReference = opts.topLevelThisReference;
this.methodNode = opts.methodNode;
this.className = opts.className;
this.superName = opts.superName;
this.superRef = opts.superRef;
this.isStatic = opts.isStatic;
this.hasSuper = false;
this.inClass = inClass;
this.isLoose = opts.isLoose;
this.scope = opts.scope;
this.file = opts.file;
this.opts = opts;
}
getObjectRef() {
return this.opts.objectRef || this.opts.getObjectRef();
}
/**
@@ -72,22 +69,16 @@ export default class ReplaceSupers {
*
* _set(Object.getPrototypeOf(CLASS.prototype), "METHOD", "VALUE", this)
*
* @param {Node} property
* @param {Node} value
* @param {Boolean} isComputed
* @param {Node} thisExpression
*
* @returns {Node}
*/
setSuperProperty(property, value, isComputed, thisExpression) {
setSuperProperty(property: Object, value: Object, isComputed: boolean, thisExpression: Object): Object {
return t.callExpression(
this.file.addHelper("set"),
[
t.callExpression(
t.memberExpression(t.identifier("Object"), t.identifier("getPrototypeOf")),
[
this.isStatic ? this.className : t.memberExpression(this.className, t.identifier("prototype"))
this.isStatic ? this.getObjectRef() : t.memberExpression(this.getObjectRef(), t.identifier("prototype"))
]
),
isComputed ? property : t.literal(property.name),
@@ -104,21 +95,16 @@ export default class ReplaceSupers {
*
* _get(Object.getPrototypeOf(CLASS.prototype), "METHOD", this)
*
* @param {Node} property
* @param {Boolean} isComputed
* @param {Node} thisExpression
*
* @returns {Node}
*/
getSuperProperty(property, isComputed, thisExpression) {
getSuperProperty(property: Object, isComputed: boolean, thisExpression: Object): Object {
return t.callExpression(
this.file.addHelper("get"),
[
t.callExpression(
t.memberExpression(t.identifier("Object"), t.identifier("getPrototypeOf")),
[
this.isStatic ? this.className : t.memberExpression(this.className, t.identifier("prototype"))
this.isStatic ? this.getObjectRef() : t.memberExpression(this.getObjectRef(), t.identifier("prototype"))
]
),
isComputed ? property : t.literal(property.name),
@@ -137,12 +123,9 @@ export default class ReplaceSupers {
/**
* Description
*
* @param {Object} node
* @param {Boolean} topLevel
*/
traverseLevel(node, topLevel) {
traverseLevel(node: Object, topLevel: boolean) {
var state = { self: this, topLevel: topLevel };
this.scope.traverse(node, visitor, state);
}
@@ -165,29 +148,24 @@ export default class ReplaceSupers {
/**
* Description
*
* @param {Object} node
* @param {Object} id
* @param {Object} parent
* @returns {Object}
*/
getLooseSuperProperty(id, parent) {
getLooseSuperProperty(id: Object, parent: Object) {
var methodNode = this.methodNode;
var methodName = methodNode.key;
var superName = this.superName || t.identifier("Function");
var superRef = this.superRef || t.identifier("Function");
if (parent.property === id) {
return;
} else if (t.isCallExpression(parent, { callee: id })) {
// super(); -> ClassName.prototype.MethodName.call(this);
// super(); -> objectRef.prototype.MethodName.call(this);
parent.arguments.unshift(t.thisExpression());
if (methodName.name === "constructor") {
// constructor() { super(); }
return t.memberExpression(superName, t.identifier("call"));
return t.memberExpression(superRef, t.identifier("call"));
} else {
id = superName;
id = superRef;
// foo() { super(); }
if (!methodNode.static) {
@@ -198,31 +176,28 @@ export default class ReplaceSupers {
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"));
// super.test -> objectRef.prototype.test
return t.memberExpression(superRef, t.identifier("prototype"));
} else {
return superName;
return superRef;
}
}
/**
* Description
*
* @param {Function} getThisReference
* @param {Object} node
* @param {Object} parent
*/
looseHandle(getThisReference, node, parent) {
if (t.isIdentifier(node, { name: "super" })) {
looseHandle(path: TraversalPath, getThisReference: Function) {
var node = path.node;
if (path.isSuper()) {
this.hasSuper = true;
return this.getLooseSuperProperty(node, parent);
} else if (t.isCallExpression(node)) {
return this.getLooseSuperProperty(node, path.parent);
} else if (path.isCallExpression()) {
var callee = node.callee;
if (!t.isMemberExpression(callee)) return;
if (callee.object.name !== "super") return;
if (!t.isSuper(callee.object)) return;
// super.test(); -> ClassName.prototype.MethodName.call(this);
// super.test(); -> objectRef.prototype.MethodName.call(this);
this.hasSuper = true;
t.appendToMemberExpression(callee, t.identifier("call"));
node.arguments.unshift(getThisReference());
@@ -231,27 +206,26 @@ export default class ReplaceSupers {
/**
* Description
*
* @param {Function} getThisReference
* @param {Object} node
* @param {Object} parent
*/
specHandle(getThisReference, node, parent) {
specHandle(path: TraversalPath, getThisReference: Function) {
var methodNode = this.methodNode;
var property;
var computed;
var args;
var thisReference;
var parent = path.parent;
var node = path.node;
if (isIllegalBareSuper(node, parent)) {
throw this.file.errorWithNode(node, messages.get("classesIllegalBareSuper"));
throw path.errorWithNode(messages.get("classesIllegalBareSuper"));
}
if (t.isCallExpression(node)) {
var callee = node.callee;
if (isSuper(callee, node)) {
// super(); -> _get(Object.getPrototypeOf(ClassName), "MethodName", this).call(this);
if (t.isSuper(callee)) {
// super(); -> _get(Object.getPrototypeOf(objectRef), "MethodName", this).call(this);
property = methodNode.key;
computed = methodNode.computed;
args = node.arguments;
@@ -263,18 +237,18 @@ export default class ReplaceSupers {
var methodName = methodNode.key.name || "METHOD_NAME";
throw this.file.errorWithNode(node, messages.get("classesIllegalSuperCall", methodName));
}
} else if (t.isMemberExpression(callee) && isSuper(callee.object, callee)) {
// super.test(); -> _get(Object.getPrototypeOf(ClassName.prototype), "test", this).call(this);
} else if (t.isMemberExpression(callee) && t.isSuper(callee.object)) {
// super.test(); -> _get(Object.getPrototypeOf(objectRef.prototype), "test", this).call(this);
property = callee.property;
computed = callee.computed;
args = node.arguments;
}
} else if (t.isMemberExpression(node) && isSuper(node.object, node)) {
// super.name; -> _get(Object.getPrototypeOf(ClassName.prototype), "name", this);
} else if (t.isMemberExpression(node) && t.isSuper(node.object)) {
// super.name; -> _get(Object.getPrototypeOf(objectRef.prototype), "name", this);
property = node.property;
computed = node.computed;
} else if (t.isAssignmentExpression(node) && isSuper(node.left.object, node.left) && methodNode.kind === "set") {
// super.name = "val"; -> _set(Object.getPrototypeOf(ClassName.prototype), "name", this);
} else if (t.isAssignmentExpression(node) && t.isSuper(node.left.object) && methodNode.kind === "set") {
// super.name = "val"; -> _set(Object.getPrototypeOf(objectRef.prototype), "name", this);
this.hasSuper = true;
return this.setSuperProperty(node.left.property, node.right, node.left.computed, getThisReference());
}

View File

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

View File

@@ -1,12 +1,10 @@
module.exports = transform;
import normalizeAst from "../helpers/normalize-ast";
import Transformer from "./transformer";
import object from "../helpers/object";
import File from "./file";
import each from "lodash/collection/each";
var normalizeAst = require("../helpers/normalize-ast");
var Transformer = require("./transformer");
var object = require("../helpers/object");
var File = require("./file");
var each = require("lodash/collection/each");
function transform(code, opts) {
export default function transform(code: string, opts?: Object) {
var file = new File(opts);
return file.parse(code);
}
@@ -20,16 +18,19 @@ transform.fromAst = function (ast, code, opts) {
return file.generate();
};
transform._ensureTransformerNames = function (type, rawKeys) {
transform._ensureTransformerNames = function (type: string, rawKeys: Array<string>) {
var keys = [];
for (var i = 0; i < rawKeys.length; i++) {
var key = rawKeys[i];
var deprecatedKey = transform.deprecatedTransformerMap[key];
if (deprecatedKey) {
var aliasKey = transform.aliasTransformerMap[key];
if (aliasKey) {
keys.push(aliasKey);
} else if (deprecatedKey) {
// deprecated key, remap it to the new one
console.error("The transformer " + key + " has been renamed to " + deprecatedKey);
console.error(`The transformer ${key} has been renamed to ${deprecatedKey}`);
rawKeys.push(deprecatedKey);
} else if (transform.transformers[key]) {
// valid key
@@ -39,7 +40,7 @@ transform._ensureTransformerNames = function (type, rawKeys) {
keys = keys.concat(transform.namespaces[key]);
} else {
// invalid key
throw new ReferenceError("Unknown transformer " + key + " specified in " + type);
throw new ReferenceError(`Unknown transformer ${key} specified in ${type}`);
}
}
@@ -51,9 +52,10 @@ transform.transformers = object();
transform.namespaces = object();
transform.deprecatedTransformerMap = require("./transformers/deprecated");
transform.aliasTransformerMap = require("./transformers/aliases");
transform.moduleFormatters = require("./modules");
var rawTransformers = require("./transformers");
import rawTransformers from "./transformers";
each(rawTransformers, function (transformer, key) {
var namespace = key.split(".")[0];

View File

@@ -1,8 +1,9 @@
var messages = require("../../messages");
var extend = require("lodash/object/extend");
var object = require("../../helpers/object");
var util = require("../../util");
var t = require("../../types");
import * as messages from "../../messages";
import traverse from "../../traversal";
import extend from "lodash/object/extend";
import object from "../../helpers/object";
import * as util from "../../util";
import * as t from "../../types";
var remapVisitor = {
enter(node, parent, scope, formatter) {
@@ -42,26 +43,26 @@ var remapVisitor = {
};
var importsVisitor = {
enter(node, parent, scope, formatter) {
if (t.isImportDeclaration(node)) {
ImportDeclaration: {
enter(node, parent, scope, formatter) {
formatter.hasLocalImports = true;
extend(formatter.localImports, t.getBindingIdentifiers(node));
extend(formatter.localImports, this.getBindingIdentifiers());
formatter.bumpImportOccurences(node);
}
}
};
var exportsVisitor = {
enter(node, parent, scope, formatter) {
var declar = node && node.declaration;
if (t.isExportDeclaration(node)) {
var exportsVisitor = traverse.explode({
ExportDeclaration: {
enter(node, parent, scope, formatter) {
formatter.hasLocalImports = true;
if (declar && t.isStatement(declar)) {
extend(formatter.localExports, t.getBindingIdentifiers(declar));
var declar = this.get("declaration");
if (declar.isStatement()) {
extend(formatter.localExports, declar.getBindingIdentifiers());
}
if (!node.default) {
if (!t.isExportDefaultDeclaration(node)) {
formatter.hasNonDefaultExports = true;
}
@@ -70,7 +71,7 @@ var exportsVisitor = {
}
}
}
};
});
export default class DefaultFormatter {
constructor(file) {
@@ -94,27 +95,29 @@ export default class DefaultFormatter {
}
doDefaultExportInterop(node) {
return node.default && !this.noInteropRequireExport && !this.hasNonDefaultExports;
return t.isExportDefaultDeclaration(node) && !this.noInteropRequireExport && !this.hasNonDefaultExports;
}
bumpImportOccurences(node) {
var source = node.source.value;
var occurs = this.localImportOccurences;
occurs[source] ||= 0;
occurs[source] += node.specifiers.length;
if (node.specifiers) {
occurs[source] += node.specifiers.length;
}
}
getLocalExports() {
this.file.scope.traverse(this.file.ast, exportsVisitor, this);
this.file.path.traverse(exportsVisitor, this);
}
getLocalImports() {
this.file.scope.traverse(this.file.ast, importsVisitor, this);
this.file.path.traverse(importsVisitor, this);
}
remapAssignments() {
if (this.hasLocalImports) {
this.file.scope.traverse(this.file.ast, remapVisitor, this);
this.file.path.traverse(remapVisitor, this);
}
}
@@ -211,31 +214,27 @@ export default class DefaultFormatter {
}
}
exportAllDeclaration(node, nodes) {
var ref = this.getExternalReference(node, nodes);
nodes.push(this.buildExportsWildcard(ref, node));
}
exportSpecifier(specifier, node, nodes) {
if (node.source) {
var ref = this.getExternalReference(node, nodes);
if (t.isExportBatchSpecifier(specifier)) {
// export * from "foo";
nodes.push(this.buildExportsWildcard(ref, node));
if (specifier.local.name === "default" && !this.noInteropRequireExport) {
// importing a default so we need to normalize it
ref = t.callExpression(this.file.addHelper("interop-require"), [ref]);
} else {
if (t.isSpecifierDefault(specifier) && !this.noInteropRequireExport) {
// importing a default so we need to normalize it
ref = t.callExpression(this.file.addHelper("interop-require"), [ref]);
} else {
ref = t.memberExpression(ref, t.getSpecifierId(specifier));
}
// export { foo } from "test";
nodes.push(this.buildExportsAssignment(
t.getSpecifierName(specifier),
ref,
node
));
ref = t.memberExpression(ref, specifier.local);
}
// export { foo } from "test";
nodes.push(this.buildExportsAssignment(specifier.exported, ref, node));
} else {
// export { foo };
nodes.push(this.buildExportsAssignment(t.getSpecifierName(specifier), specifier.id, node));
nodes.push(this.buildExportsAssignment(specifier.exported, specifier.local, node));
}
}
@@ -259,7 +258,7 @@ export default class DefaultFormatter {
var id = declar.id;
if (node.default) {
if (t.isExportDefaultDeclaration(node)) {
id = t.identifier("default");
}

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