opal 1.0.3 → 1.1.1.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (408) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +13 -12
  3. data/.gitattributes +1 -1
  4. data/.github/FUNDING.yml +1 -0
  5. data/.github/workflows/build.yml +90 -0
  6. data/.jshintrc +1 -1
  7. data/.overcommit.yml +35 -0
  8. data/.rubocop.yml +44 -7
  9. data/{.rubocop_todo.yml → .rubocop/todo.yml} +4 -5
  10. data/CHANGELOG.md +152 -1
  11. data/Gemfile +2 -3
  12. data/HACKING.md +2 -9
  13. data/LICENSE +1 -1
  14. data/README.md +21 -18
  15. data/UNRELEASED.md +4 -2
  16. data/benchmark-ips/bm_array_pop_1.rb +8 -0
  17. data/benchmark-ips/bm_array_shift.rb +7 -0
  18. data/benchmark-ips/bm_js_symbols_vs_strings.rb +7 -2
  19. data/benchmark-ips/class_shovel_vs_singleton_class.rb +16 -0
  20. data/bin/build-browser-source-map-support +4 -0
  21. data/bin/format-filters +54 -0
  22. data/bin/git-submodule-fast-install +49 -0
  23. data/bin/remove-filters +39 -0
  24. data/bin/setup +4 -0
  25. data/bin/yarn +11 -0
  26. data/docs/releasing.md +18 -0
  27. data/docs/roda-sprockets.md +86 -0
  28. data/docs/sinatra.md +5 -12
  29. data/examples/rack/Gemfile +1 -0
  30. data/examples/rack/app/application.rb +3 -0
  31. data/examples/rack/config.ru +5 -4
  32. data/exe/opal-repl +2 -83
  33. data/lib/opal/ast/builder.rb +1 -1
  34. data/lib/opal/builder.rb +2 -10
  35. data/lib/opal/builder_processors.rb +1 -1
  36. data/lib/opal/cli.rb +4 -1
  37. data/lib/opal/cli_options.rb +11 -3
  38. data/lib/opal/cli_runners.rb +27 -37
  39. data/lib/opal/cli_runners/applescript.rb +5 -44
  40. data/lib/opal/cli_runners/chrome.js +7 -35
  41. data/lib/opal/cli_runners/chrome.rb +75 -17
  42. data/lib/opal/cli_runners/compiler.rb +17 -0
  43. data/lib/opal/cli_runners/nashorn.rb +9 -43
  44. data/lib/opal/cli_runners/nodejs.rb +18 -47
  45. data/lib/opal/cli_runners/server.rb +18 -6
  46. data/lib/opal/cli_runners/source-map-support-browser.js +6449 -0
  47. data/lib/opal/cli_runners/source-map-support-node.js +3704 -0
  48. data/lib/opal/cli_runners/source-map-support.js +639 -0
  49. data/lib/opal/cli_runners/system_runner.rb +45 -0
  50. data/lib/opal/compiler.rb +57 -29
  51. data/lib/opal/config.rb +0 -5
  52. data/lib/opal/erb.rb +1 -1
  53. data/lib/opal/magic_comments.rb +34 -0
  54. data/lib/opal/nodes/args/arity_check.rb +2 -1
  55. data/lib/opal/nodes/base.rb +1 -1
  56. data/lib/opal/nodes/call.rb +1 -4
  57. data/lib/opal/nodes/def.rb +2 -0
  58. data/lib/opal/nodes/iter.rb +1 -1
  59. data/lib/opal/nodes/literal.rb +19 -14
  60. data/lib/opal/nodes/logic.rb +5 -80
  61. data/lib/opal/nodes/masgn.rb +2 -0
  62. data/lib/opal/nodes/rescue.rb +1 -1
  63. data/lib/opal/nodes/super.rb +24 -10
  64. data/lib/opal/nodes/top.rb +5 -4
  65. data/lib/opal/parser/with_c_lexer.rb +2 -1
  66. data/lib/opal/parser/with_ruby_lexer.rb +1 -1
  67. data/lib/opal/path_reader.rb +2 -2
  68. data/lib/opal/paths.rb +8 -5
  69. data/lib/opal/repl.rb +103 -0
  70. data/lib/opal/rewriter.rb +4 -0
  71. data/lib/opal/rewriters/for_rewriter.rb +1 -1
  72. data/lib/opal/rewriters/js_reserved_words.rb +7 -7
  73. data/lib/opal/rewriters/numblocks.rb +31 -0
  74. data/lib/opal/rewriters/returnable_logic.rb +33 -0
  75. data/lib/opal/util.rb +11 -48
  76. data/lib/opal/version.rb +1 -1
  77. data/opal.gemspec +26 -26
  78. data/opal/corelib/array.rb +160 -118
  79. data/opal/corelib/array/pack.rb +5 -3
  80. data/opal/corelib/basic_object.rb +4 -4
  81. data/opal/corelib/class.rb +2 -1
  82. data/opal/corelib/comparable.rb +49 -31
  83. data/opal/corelib/constants.rb +5 -5
  84. data/opal/corelib/enumerable.rb +108 -46
  85. data/opal/corelib/enumerator.rb +27 -12
  86. data/opal/corelib/file.rb +3 -1
  87. data/opal/corelib/hash.rb +6 -1
  88. data/opal/corelib/helpers.rb +8 -28
  89. data/opal/corelib/io.rb +12 -7
  90. data/opal/corelib/kernel.rb +45 -14
  91. data/opal/corelib/kernel/format.rb +3 -1
  92. data/opal/corelib/math.rb +8 -6
  93. data/opal/corelib/method.rb +8 -0
  94. data/opal/corelib/module.rb +17 -2
  95. data/opal/corelib/number.rb +3 -12
  96. data/opal/corelib/proc.rb +16 -0
  97. data/opal/corelib/random/mersenne_twister.rb +147 -0
  98. data/opal/corelib/range.rb +3 -13
  99. data/opal/corelib/regexp.rb +10 -6
  100. data/opal/corelib/runtime.js +208 -81
  101. data/opal/corelib/string.rb +55 -49
  102. data/opal/corelib/string/encoding.rb +109 -32
  103. data/opal/corelib/string/unpack.rb +2 -17
  104. data/opal/corelib/struct.rb +14 -1
  105. data/opal/corelib/time.rb +4 -0
  106. data/opal/opal.rb +1 -1
  107. data/opal/opal/mini.rb +1 -1
  108. data/package.json +16 -0
  109. data/spec/README.md +10 -0
  110. data/spec/filters/bugs/array.rb +76 -0
  111. data/spec/filters/bugs/base64.rb +10 -0
  112. data/spec/filters/bugs/basicobject.rb +12 -0
  113. data/spec/filters/bugs/bigdecimal.rb +248 -0
  114. data/spec/filters/bugs/class.rb +12 -0
  115. data/spec/filters/bugs/complex.rb +7 -0
  116. data/spec/filters/bugs/date.rb +104 -0
  117. data/spec/filters/bugs/encoding.rb +259 -0
  118. data/spec/filters/bugs/enumerable.rb +26 -0
  119. data/spec/filters/bugs/enumerator.rb +48 -0
  120. data/spec/filters/bugs/exception.rb +120 -0
  121. data/spec/filters/bugs/file.rb +48 -0
  122. data/spec/filters/bugs/float.rb +74 -0
  123. data/spec/filters/bugs/hash.rb +60 -0
  124. data/spec/filters/bugs/integer.rb +78 -0
  125. data/spec/filters/bugs/io.rb +9 -0
  126. data/spec/filters/bugs/kernel.rb +401 -0
  127. data/spec/filters/bugs/language.rb +451 -0
  128. data/spec/filters/bugs/marshal.rb +50 -0
  129. data/spec/filters/bugs/math.rb +4 -0
  130. data/spec/filters/bugs/method.rb +79 -0
  131. data/spec/filters/bugs/module.rb +281 -0
  132. data/spec/filters/bugs/nilclass.rb +4 -0
  133. data/spec/filters/bugs/numeric.rb +27 -0
  134. data/spec/filters/bugs/openstruct.rb +8 -0
  135. data/spec/filters/bugs/pack_unpack.rb +138 -0
  136. data/spec/filters/bugs/pathname.rb +9 -0
  137. data/spec/filters/bugs/proc.rb +80 -0
  138. data/spec/filters/bugs/random.rb +20 -0
  139. data/spec/filters/bugs/range.rb +139 -0
  140. data/spec/filters/bugs/rational.rb +12 -0
  141. data/spec/filters/bugs/regexp.rb +91 -0
  142. data/spec/filters/bugs/set.rb +51 -0
  143. data/spec/filters/bugs/singleton.rb +7 -0
  144. data/spec/filters/bugs/string.rb +341 -0
  145. data/spec/filters/bugs/stringscanner.rb +78 -0
  146. data/spec/filters/bugs/struct.rb +26 -0
  147. data/spec/filters/bugs/symbol.rb +7 -0
  148. data/spec/filters/bugs/time.rb +109 -0
  149. data/spec/filters/bugs/unboundmethod.rb +33 -0
  150. data/spec/filters/bugs/warnings.rb +30 -0
  151. data/spec/filters/unsupported/array.rb +168 -0
  152. data/spec/filters/unsupported/basicobject.rb +15 -0
  153. data/spec/filters/unsupported/bignum.rb +55 -0
  154. data/spec/filters/unsupported/class.rb +5 -0
  155. data/spec/filters/unsupported/delegator.rb +6 -0
  156. data/spec/filters/unsupported/enumerable.rb +12 -0
  157. data/spec/filters/unsupported/enumerator.rb +14 -0
  158. data/spec/filters/unsupported/file.rb +4 -0
  159. data/spec/filters/unsupported/fixnum.rb +15 -0
  160. data/spec/filters/unsupported/float.rb +47 -0
  161. data/spec/filters/unsupported/freeze.rb +259 -0
  162. data/spec/filters/unsupported/hash.rb +44 -0
  163. data/spec/filters/unsupported/integer.rb +101 -0
  164. data/spec/filters/unsupported/kernel.rb +35 -0
  165. data/spec/filters/unsupported/language.rb +25 -0
  166. data/spec/filters/unsupported/marshal.rb +44 -0
  167. data/spec/filters/unsupported/matchdata.rb +63 -0
  168. data/spec/filters/unsupported/math.rb +4 -0
  169. data/spec/filters/unsupported/pathname.rb +4 -0
  170. data/spec/filters/unsupported/privacy.rb +287 -0
  171. data/spec/filters/unsupported/proc.rb +4 -0
  172. data/spec/filters/unsupported/random.rb +5 -0
  173. data/spec/filters/unsupported/range.rb +8 -0
  174. data/spec/filters/unsupported/regexp.rb +70 -0
  175. data/spec/filters/unsupported/set.rb +5 -0
  176. data/spec/filters/unsupported/singleton.rb +7 -0
  177. data/spec/filters/unsupported/string.rb +687 -0
  178. data/spec/filters/unsupported/struct.rb +7 -0
  179. data/spec/filters/unsupported/symbol.rb +21 -0
  180. data/spec/filters/unsupported/taint.rb +162 -0
  181. data/spec/filters/unsupported/thread.rb +10 -0
  182. data/spec/filters/unsupported/time.rb +204 -0
  183. data/spec/filters/unsupported/usage_of_files.rb +262 -0
  184. data/spec/lib/builder_processors_spec.rb +44 -0
  185. data/spec/lib/builder_spec.rb +133 -0
  186. data/spec/lib/cli_runners/server_spec.rb +25 -0
  187. data/spec/lib/cli_runners_spec.rb +16 -0
  188. data/spec/lib/cli_spec.rb +256 -0
  189. data/spec/lib/compiler_spec.rb +693 -0
  190. data/spec/lib/config_spec.rb +112 -0
  191. data/spec/lib/dependency_resolver_spec.rb +43 -0
  192. data/spec/lib/deprecations_spec.rb +17 -0
  193. data/spec/lib/fixtures/complex_sprockets.js.rb.erb +4 -0
  194. data/spec/lib/fixtures/file_with_directives.js +2 -0
  195. data/spec/lib/fixtures/jst_file.js.jst +1 -0
  196. data/spec/lib/fixtures/no_requires.rb +1 -0
  197. data/spec/lib/fixtures/opal_file.rb +2 -0
  198. data/spec/lib/fixtures/require_tree_test.rb +3 -0
  199. data/spec/lib/fixtures/required_file.js +1 -0
  200. data/spec/lib/fixtures/required_tree_test/required_file1.rb +1 -0
  201. data/spec/lib/fixtures/required_tree_test/required_file2.rb +1 -0
  202. data/spec/lib/fixtures/requires.rb +7 -0
  203. data/spec/lib/fixtures/source_location_test.rb +7 -0
  204. data/spec/lib/fixtures/source_map.rb +1 -0
  205. data/spec/lib/fixtures/source_map/subfolder/other_file.rb +1 -0
  206. data/spec/lib/fixtures/sprockets_file.js.rb +3 -0
  207. data/spec/lib/fixtures/sprockets_require_tree_test.rb +3 -0
  208. data/spec/lib/path_reader_spec.rb +47 -0
  209. data/spec/lib/paths_spec.rb +18 -0
  210. data/spec/lib/repl_spec.rb +28 -0
  211. data/spec/lib/rewriters/base_spec.rb +68 -0
  212. data/spec/lib/rewriters/binary_operator_assignment_spec.rb +153 -0
  213. data/spec/lib/rewriters/block_to_iter_spec.rb +28 -0
  214. data/spec/lib/rewriters/dot_js_syntax_spec.rb +108 -0
  215. data/spec/lib/rewriters/explicit_writer_return_spec.rb +186 -0
  216. data/spec/lib/rewriters/for_rewriter_spec.rb +92 -0
  217. data/spec/lib/rewriters/hashes/key_duplicates_rewriter_spec.rb +47 -0
  218. data/spec/lib/rewriters/js_reserved_words_spec.rb +119 -0
  219. data/spec/lib/rewriters/logical_operator_assignment_spec.rb +202 -0
  220. data/spec/lib/rewriters/opal_engine_check_spec.rb +84 -0
  221. data/spec/lib/rewriters/returnable_logic_spec.rb +52 -0
  222. data/spec/lib/rewriters/rubyspec/filters_rewriter_spec.rb +59 -0
  223. data/spec/lib/simple_server_spec.rb +56 -0
  224. data/spec/lib/source_map/file_spec.rb +67 -0
  225. data/spec/lib/source_map/index_spec.rb +80 -0
  226. data/spec/lib/spec_helper.rb +105 -0
  227. data/spec/mspec-opal/formatters.rb +197 -0
  228. data/spec/mspec-opal/runner.rb +172 -0
  229. data/spec/opal/compiler/irb_spec.rb +44 -0
  230. data/spec/opal/compiler/unicode_spec.rb +10 -0
  231. data/spec/opal/core/array/dup_spec.rb +23 -0
  232. data/spec/opal/core/array/intersection_spec.rb +38 -0
  233. data/spec/opal/core/array/minus_spec.rb +38 -0
  234. data/spec/opal/core/array/union_spec.rb +38 -0
  235. data/spec/opal/core/array/uniq_spec.rb +49 -0
  236. data/spec/opal/core/class/inherited_spec.rb +18 -0
  237. data/spec/opal/core/enumerable/all_break_spec.rb +5 -0
  238. data/spec/opal/core/enumerable/any_break_spec.rb +5 -0
  239. data/spec/opal/core/enumerable/collect_break_spec.rb +13 -0
  240. data/spec/opal/core/enumerable/count_break_spec.rb +5 -0
  241. data/spec/opal/core/enumerable/detect_break_spec.rb +5 -0
  242. data/spec/opal/core/enumerable/drop_while_break_spec.rb +5 -0
  243. data/spec/opal/core/enumerable/each_slice_break_spec.rb +6 -0
  244. data/spec/opal/core/enumerable/each_with_index_break_spec.rb +5 -0
  245. data/spec/opal/core/enumerable/each_with_object_break_spec.rb +5 -0
  246. data/spec/opal/core/enumerable/find_all_break_spec.rb +5 -0
  247. data/spec/opal/core/enumerable/find_index_break_spec.rb +5 -0
  248. data/spec/opal/core/enumerable/grep_break_spec.rb +5 -0
  249. data/spec/opal/core/enumerable/max_break_spec.rb +5 -0
  250. data/spec/opal/core/enumerable/max_by_break_spec.rb +5 -0
  251. data/spec/opal/core/enumerable/min_break_spec.rb +5 -0
  252. data/spec/opal/core/enumerable/min_by_break_spec.rb +5 -0
  253. data/spec/opal/core/enumerable/none_break_spec.rb +5 -0
  254. data/spec/opal/core/enumerable/one_break_spec.rb +5 -0
  255. data/spec/opal/core/enumerable/reduce_break_spec.rb +5 -0
  256. data/spec/opal/core/enumerable/take_while_break_spec.rb +5 -0
  257. data/spec/opal/core/enumerator/with_index_spec.rb +6 -0
  258. data/spec/opal/core/exception_spec.rb +8 -0
  259. data/spec/opal/core/fixtures/require_tree_files/file 1.rb +1 -0
  260. data/spec/opal/core/fixtures/require_tree_files/file 2.rb +1 -0
  261. data/spec/opal/core/fixtures/require_tree_files/file 3.rb +1 -0
  262. data/spec/opal/core/fixtures/require_tree_files/file 4.rb +1 -0
  263. data/spec/opal/core/fixtures/require_tree_files/file 5.rb +1 -0
  264. data/spec/opal/core/fixtures/require_tree_files/nested/nested 1.rb +1 -0
  265. data/spec/opal/core/fixtures/require_tree_files/nested/nested 2.rb +1 -0
  266. data/spec/opal/core/fixtures/require_tree_files/other/other 1.rb +1 -0
  267. data/spec/opal/core/fixtures/require_tree_with_dot/file 1.rb +1 -0
  268. data/spec/opal/core/fixtures/require_tree_with_dot/file 2.rb +1 -0
  269. data/spec/opal/core/fixtures/require_tree_with_dot/file 3.rb +1 -0
  270. data/spec/opal/core/fixtures/require_tree_with_dot/index.rb +3 -0
  271. data/spec/opal/core/hash/internals_spec.rb +339 -0
  272. data/spec/opal/core/helpers_spec.rb +14 -0
  273. data/spec/opal/core/iterable_props_spec.rb +53 -0
  274. data/spec/opal/core/kernel/at_exit_spec.rb +70 -0
  275. data/spec/opal/core/kernel/freeze_spec.rb +15 -0
  276. data/spec/opal/core/kernel/instance_variables_spec.rb +110 -0
  277. data/spec/opal/core/kernel/methods_spec.rb +25 -0
  278. data/spec/opal/core/kernel/public_methods_spec.rb +25 -0
  279. data/spec/opal/core/kernel/require_tree_spec.rb +18 -0
  280. data/spec/opal/core/kernel/respond_to_spec.rb +15 -0
  281. data/spec/opal/core/language/DATA/characters_support_spec.rb +9 -0
  282. data/spec/opal/core/language/DATA/empty___END___spec.rb +7 -0
  283. data/spec/opal/core/language/DATA/multiple___END___spec.rb +10 -0
  284. data/spec/opal/core/language/arguments/mlhs_arg_spec.rb +18 -0
  285. data/spec/opal/core/language/keyword_arguments_spec.rb +9 -0
  286. data/spec/opal/core/language/numblocks_spec.rb +16 -0
  287. data/spec/opal/core/language/safe_navigator_spec.rb +7 -0
  288. data/spec/opal/core/language/while_spec.rb +31 -0
  289. data/spec/opal/core/language_spec.rb +29 -0
  290. data/spec/opal/core/marshal/dump_spec.rb +81 -0
  291. data/spec/opal/core/marshal/load_spec.rb +13 -0
  292. data/spec/opal/core/module_spec.rb +27 -0
  293. data/spec/opal/core/object_id_spec.rb +56 -0
  294. data/spec/opal/core/regexp/interpolation_spec.rb +40 -0
  295. data/spec/opal/core/regexp/match_spec.rb +78 -0
  296. data/spec/opal/core/runtime/bridged_classes_spec.rb +123 -0
  297. data/spec/opal/core/runtime/constants_spec.rb +16 -0
  298. data/spec/opal/core/runtime/eval_spec.rb +5 -0
  299. data/spec/opal/core/runtime/exit_spec.rb +29 -0
  300. data/spec/opal/core/runtime/is_a_spec.rb +48 -0
  301. data/spec/opal/core/runtime/loaded_spec.rb +20 -0
  302. data/spec/opal/core/runtime/main_methods_spec.rb +39 -0
  303. data/spec/opal/core/runtime/method_missing_spec.rb +68 -0
  304. data/spec/opal/core/runtime/rescue_spec.rb +37 -0
  305. data/spec/opal/core/runtime/string_spec.rb +25 -0
  306. data/spec/opal/core/runtime/truthy_spec.rb +61 -0
  307. data/spec/opal/core/runtime_spec.rb +58 -0
  308. data/spec/opal/core/string/each_byte_spec.rb +19 -0
  309. data/spec/opal/core/string/gsub_spec.rb +35 -0
  310. data/spec/opal/core/string/to_sym_spec.rb +9 -0
  311. data/spec/opal/core/string_spec.rb +28 -0
  312. data/spec/opal/core/struct/dup_spec.rb +11 -0
  313. data/spec/opal/core/time_spec.rb +68 -0
  314. data/spec/opal/stdlib/erb/erb_spec.rb +30 -0
  315. data/spec/opal/stdlib/erb/inline_block.opalerb +3 -0
  316. data/spec/opal/stdlib/erb/quoted.opalerb +1 -0
  317. data/spec/opal/stdlib/erb/simple.opalerb +1 -0
  318. data/spec/opal/stdlib/js_spec.rb +72 -0
  319. data/spec/opal/stdlib/json/ext_spec.rb +55 -0
  320. data/spec/opal/stdlib/json/parse_spec.rb +37 -0
  321. data/spec/opal/stdlib/logger/logger_spec.rb +308 -0
  322. data/spec/opal/stdlib/native/alias_native_spec.rb +27 -0
  323. data/spec/opal/stdlib/native/array_spec.rb +11 -0
  324. data/spec/opal/stdlib/native/date_spec.rb +12 -0
  325. data/spec/opal/stdlib/native/deprecated_include_spec.rb +8 -0
  326. data/spec/opal/stdlib/native/each_spec.rb +13 -0
  327. data/spec/opal/stdlib/native/element_reference_spec.rb +16 -0
  328. data/spec/opal/stdlib/native/exposure_spec.rb +33 -0
  329. data/spec/opal/stdlib/native/ext_spec.rb +19 -0
  330. data/spec/opal/stdlib/native/hash_spec.rb +67 -0
  331. data/spec/opal/stdlib/native/initialize_spec.rb +17 -0
  332. data/spec/opal/stdlib/native/method_missing_spec.rb +51 -0
  333. data/spec/opal/stdlib/native/native_alias_spec.rb +26 -0
  334. data/spec/opal/stdlib/native/native_class_spec.rb +18 -0
  335. data/spec/opal/stdlib/native/native_module_spec.rb +13 -0
  336. data/spec/opal/stdlib/native/native_reader_spec.rb +22 -0
  337. data/spec/opal/stdlib/native/native_writer_spec.rb +30 -0
  338. data/spec/opal/stdlib/native/new_spec.rb +92 -0
  339. data/spec/opal/stdlib/native/struct_spec.rb +12 -0
  340. data/spec/opal/stdlib/pp_spec.rb +5 -0
  341. data/spec/opal/stdlib/promise/always_spec.rb +49 -0
  342. data/spec/opal/stdlib/promise/error_spec.rb +15 -0
  343. data/spec/opal/stdlib/promise/rescue_spec.rb +53 -0
  344. data/spec/opal/stdlib/promise/then_spec.rb +79 -0
  345. data/spec/opal/stdlib/promise/trace_spec.rb +51 -0
  346. data/spec/opal/stdlib/promise/value_spec.rb +15 -0
  347. data/spec/opal/stdlib/promise/when_spec.rb +34 -0
  348. data/spec/opal/stdlib/source_map_spec.rb +8 -0
  349. data/spec/opal/stdlib/strscan/scan_spec.rb +11 -0
  350. data/spec/opal/stdlib/template/paths_spec.rb +10 -0
  351. data/spec/opal/stdlib/thread/mutex_spec.rb +40 -0
  352. data/spec/opal/stdlib/thread/thread_queue_spec.rb +32 -0
  353. data/spec/opal/stdlib/thread/thread_spec.rb +60 -0
  354. data/spec/ruby_specs +183 -0
  355. data/spec/spec_helper.rb +31 -0
  356. data/spec/support/guard_platform.rb +4 -0
  357. data/spec/support/match_helpers.rb +57 -0
  358. data/spec/support/mspec_rspec_adapter.rb +33 -0
  359. data/spec/support/rewriters_helper.rb +54 -0
  360. data/spec/support/source_map_helper.rb +190 -0
  361. data/stdlib/base64.rb +2 -2
  362. data/stdlib/bigdecimal.rb +15 -3
  363. data/stdlib/bigdecimal/bignumber.js.rb +1 -1
  364. data/stdlib/bigdecimal/util.rb +148 -0
  365. data/stdlib/delegate.rb +8 -0
  366. data/stdlib/nodejs/fileutils.rb +1 -1
  367. data/stdlib/nodejs/kernel.rb +0 -13
  368. data/stdlib/nodejs/stacktrace.rb +4 -179
  369. data/stdlib/ostruct.rb +5 -0
  370. data/stdlib/pp.rb +586 -19
  371. data/stdlib/prettyprint.rb +556 -0
  372. data/stdlib/rbconfig/sizeof.rb +2 -0
  373. data/stdlib/securerandom.rb +32 -0
  374. data/stdlib/set.rb +36 -0
  375. data/stdlib/strscan.rb +15 -0
  376. data/tasks/benchmarking.rake +1 -1
  377. data/tasks/linting.rake +3 -5
  378. data/tasks/releasing.rake +2 -5
  379. data/tasks/testing.rake +16 -11
  380. data/tasks/testing/mspec_special_calls.rb +1 -19
  381. data/test/nodejs/fixtures/cat.png +0 -0
  382. data/test/nodejs/fixtures/hello.rb +1 -0
  383. data/test/nodejs/fixtures/iso88591.txt +1 -0
  384. data/test/nodejs/fixtures/utf8.txt +1 -0
  385. data/test/nodejs/fixtures/win1258.txt +1 -0
  386. data/test/nodejs/test_dir.rb +39 -0
  387. data/test/nodejs/test_env.rb +62 -0
  388. data/test/nodejs/test_error.rb +29 -0
  389. data/test/nodejs/test_file.rb +206 -0
  390. data/test/nodejs/test_file_encoding.rb +20 -0
  391. data/test/nodejs/test_io.rb +18 -0
  392. data/test/nodejs/test_opal_builder.rb +12 -0
  393. data/test/nodejs/test_pathname.rb +16 -0
  394. data/test/opal/cat.png +0 -0
  395. data/test/opal/http_server.rb +52 -0
  396. data/test/opal/test_base64.rb +115 -0
  397. data/test/opal/test_keyword.rb +590 -0
  398. data/test/opal/test_matrix.rb +661 -0
  399. data/test/opal/test_openuri.rb +53 -0
  400. data/test/opal/unsupported_and_bugs.rb +39 -0
  401. data/yarn.lock +1355 -0
  402. metadata +905 -40
  403. data/.travis.yml +0 -109
  404. data/appveyor.yml +0 -35
  405. data/lib/opal/nodes/runtime_helpers.rb +0 -51
  406. data/opal/corelib/random/MersenneTwister.js +0 -137
  407. data/opal/corelib/random/mersenne_twister.js.rb +0 -13
  408. data/stdlib/bigdecimal/kernel.rb +0 -5
@@ -0,0 +1,693 @@
1
+ require 'lib/spec_helper'
2
+ require 'support/match_helpers'
3
+
4
+ RSpec.describe Opal::Compiler do
5
+ describe 'regexp' do
6
+ it 'should escape regexp' do
7
+ expect_compiled("%r{^/{4,}$}").to include('/^\/{4,}$/')
8
+ expect_compiled('/\\\\"/').to include('/\\\\"/')
9
+ end
10
+ end
11
+
12
+ describe 'requiring' do
13
+ it 'calls #require' do
14
+ expect_compiled("require 'pippo'").to include('self.$require("pippo")')
15
+ end
16
+ end
17
+
18
+ describe 'requirable' do
19
+ it 'executes the file' do
20
+ expect_compiled("").to include('(function(Opal) {')
21
+ expect_compiled("").to end_with("})(Opal);\n")
22
+ end
23
+
24
+ it 'puts the compiled into "Opal.modules"' do
25
+ options = { :requirable => true, :file => "pippo" }
26
+ expect_compiled("", options).to include('Opal.modules["pippo"] = function(Opal) {')
27
+ expect_compiled("", options).to end_with("};\n")
28
+ end
29
+ end
30
+
31
+ it "should compile simple ruby values" do
32
+ expect_compiled("3.142").to include("return 3.142")
33
+ expect_compiled("123e1").to include("return 1230")
34
+ expect_compiled("123E+10").to include("return 1230000000000")
35
+ expect_compiled("false").to include("return false")
36
+ expect_compiled("true").to include("return true")
37
+ expect_compiled("nil").to include("return nil")
38
+ end
39
+
40
+ it "should compile ruby strings" do
41
+ expect_compiled('"hello world"').to include('return "hello world"')
42
+ expect_compiled('"hello #{100}"').to include('"hello "', '100')
43
+ end
44
+
45
+ it "should compile method calls" do
46
+ expect_compiled("self.inspect").to include("$inspect()")
47
+ expect_compiled("self.map { |a| a + 10 }").to include("$map")
48
+ end
49
+
50
+ it "adds method missing stubs" do
51
+ expect_compiled("self.puts 'hello'").to include("Opal.add_stubs(['$puts'])")
52
+ end
53
+
54
+ it 'adds method missing stubs with operators' do
55
+ expect_compiled("class Foo; end; Foo.new > 5").to include("Opal.add_stubs(['$>', '$new'])")
56
+ end
57
+
58
+ it "should compile constant lookups" do
59
+ expect_compiled("Object").to include("Object")
60
+ expect_compiled("Array").to include("Array")
61
+ end
62
+
63
+ it "should compile undef calls" do
64
+ expect_compiled("undef a").to include("Opal.udef(self, '$' + \"a\")")
65
+ expect_compiled("undef a,b").to match(/Opal.udef\(self, '\$' \+ "a"\);.*Opal.udef\(self, '\$' \+ "b"\);/m)
66
+ end
67
+
68
+ describe "method names" do
69
+ it "generates a named function for method" do
70
+ expect_compiled("def test_method; end").to include("function $$test_method()")
71
+ end
72
+
73
+ context "when function name is reserved" do
74
+ it "generates a valid named function for method" do
75
+ expect_compiled("def Array; end").to include("function $$Array()")
76
+ end
77
+ end
78
+
79
+ context "when function name is not valid" do
80
+ it "skips generating a name" do
81
+ expect_compiled("def test_method?; end").to include("function()")
82
+ end
83
+ end
84
+ end
85
+
86
+ describe "debugger special method" do
87
+ it "generates debugger keyword in javascript" do
88
+ expect_compiled("debugger").to include("debugger")
89
+ expect_compiled("debugger").to_not include("$debugger")
90
+ end
91
+ end
92
+
93
+ describe "extracting __END__ content" do
94
+ shared_examples "it extracts __END__" do |source, expected_eof_content|
95
+ it "extracts #{expected_eof_content.inspect} from #{source.inspect}" do
96
+ compiler = Opal::Compiler.new(source)
97
+ compiler.parse
98
+ expect(compiler.eof_content).to eq(expected_eof_content)
99
+ end
100
+ end
101
+
102
+ include_examples "it extracts __END__", "code", nil
103
+ include_examples "it extracts __END__", "code\n__END_", nil
104
+ include_examples "it extracts __END__", "code\n__END__", ""
105
+ include_examples "it extracts __END__", "code\n\n\n__END__", ""
106
+ include_examples "it extracts __END__", "code\n__END__data", nil
107
+ include_examples "it extracts __END__", "code\n__END__\ndata", "data"
108
+ include_examples "it extracts __END__", "code\n__END__\nline1\nline2", "line1\nline2"
109
+ include_examples "it extracts __END__", "code\n__END__\nline1\nline2\n", "line1\nline2\n"
110
+ include_examples "it extracts __END__", "code\n__END__\nline1\nline2\n", "line1\nline2\n"
111
+ include_examples "it extracts __END__", "code\n __END__\ndata", nil
112
+ include_examples "it extracts __END__", "\"multiline string\n__END__\nwith data separator\"\n__END__\ndata", "data"
113
+ end
114
+
115
+ describe "DATA special variable" do
116
+ it "is not a special case unless __END__ part present in source" do
117
+ expect_compiled("DATA").to include("DATA")
118
+ expect_compiled("DATA\nMALFORMED__END__").to include("DATA")
119
+ expect_compiled("DATA\n__END__").to_not include("DATA")
120
+ end
121
+
122
+ it "DATA gets compiled as a reference to special $__END__ variable" do
123
+ expect_compiled("a = DATA\n__END__").to include("a = $__END__")
124
+ end
125
+
126
+ it "causes the compiler to create a reference to special __END__ variable" do
127
+ expect_compiled("DATA\n__END__\nFord Perfect").to include("$__END__ = ")
128
+ end
129
+
130
+ it "does not create a reference to __END__ vairbale unless __END__ content present" do
131
+ expect_compiled("DATA").to_not include("$__END__ = ")
132
+ end
133
+ end
134
+
135
+ describe "escapes in x-strings" do
136
+ it "compiles the exscapes directly as appearing in x-strings" do
137
+ expect_compiled('`"hello\nworld"`').to include('"hello\nworld"')
138
+ expect_compiled('%x{"hello\nworld"}').to include('"hello\nworld"')
139
+ end
140
+ end
141
+
142
+ describe 'pre-processing require-ish methods' do
143
+ describe '#require' do
144
+ it 'parses and resolve #require argument' do
145
+ compiler = compiler_for(%Q{require "#{__FILE__}"})
146
+ expect(compiler.requires).to eq([__FILE__])
147
+ end
148
+ end
149
+
150
+ describe '#autoload' do
151
+ it 'ignores autoload outside of context class' do
152
+ compiler = compiler_for(%Q{autoload :Whatever, "#{__FILE__}"})
153
+ expect(compiler.requires).to eq([])
154
+ end
155
+
156
+ it 'parses and resolve second #autoload arguments' do
157
+ compiler = compiler_for(%Q{class Foo; autoload :Whatever, "#{__FILE__}"; end})
158
+ expect(compiler.requires).to eq([__FILE__])
159
+ end
160
+ end
161
+
162
+ describe '#require_relative' do
163
+ it 'parses and resolve #require_relative argument' do
164
+ compiler = compiler_for(%Q{require_relative "./#{File.basename(__FILE__)}"}, file: __FILE__)
165
+ expect(compiler.requires).to eq([__FILE__])
166
+ end
167
+ end
168
+
169
+ describe '#require_tree' do
170
+ require 'pathname'
171
+
172
+ it 'parses and resolve #require argument' do
173
+ file = Pathname(__FILE__).join('../fixtures/require_tree_test.rb')
174
+
175
+ compiler = compiler_for(file.read)
176
+ expect(compiler.required_trees).to eq(['../fixtures/required_tree_test'])
177
+ end
178
+ end
179
+ end
180
+
181
+ describe 'truthy check' do
182
+ context 'no parentheses' do
183
+ context 'with operators' do
184
+ it 'excludes nil check for primitives' do
185
+ expect_compiled('foo = 42 if 2 > 3').to include('if ($rb_gt(2, 3))')
186
+ expect_compiled('foo = 42 if 2.5 > 3.5').to include('if ($rb_gt(2.5, 3.5))')
187
+ expect_compiled('foo = 42 if true > false').to include('if ($rb_gt(true, false))')
188
+
189
+ expect_compiled('foo = 42 if 2 == 3').to include("if ((2)['$=='](3))")
190
+ expect_compiled('foo = 42 if 2.5 == 3.5').to include("if ((2.5)['$=='](3.5))")
191
+ expect_compiled('foo = 42 if true == false').to include("if (true['$=='](false))")
192
+ end
193
+
194
+ it 'adds nil check for strings' do
195
+ expect_compiled('foo = 42 if "test" > "bar"').to include('if ($truthy($rb_gt("test", "bar")))')
196
+ end
197
+
198
+ it 'specifically == excludes nil check for strings' do
199
+ expect_compiled('foo = 42 if "test" == "bar"').to include("if (\"test\"['$=='](\"bar\"))")
200
+ end
201
+
202
+ it 'adds nil check for lvars' do
203
+ expect_compiled("bar = 4\nfoo = 42 if bar > 5").to include('if ($truthy($rb_gt(bar, 5)))')
204
+ end
205
+
206
+ it 'specifically == excludes nil check for lvars' do
207
+ expect_compiled("bar = 4\nfoo = 42 if bar == 5").to include("if (bar['$=='](5))")
208
+ end
209
+
210
+ it 'adds nil check for constants' do
211
+ expect_compiled("foo = 42 if Test > 4").to include("if ($truthy($rb_gt($$($nesting, 'Test'), 4))) ")
212
+ end
213
+
214
+ it 'specifically == excludes nil check for constants' do
215
+ expect_compiled("foo = 42 if Test == 4").to include("if ($$($nesting, 'Test')['$=='](4))")
216
+ end
217
+ end
218
+
219
+ context 'without operators' do
220
+ it 'adds nil check for primitives' do
221
+ expect_compiled('foo = 42 if 2').to include('if ($truthy(2))')
222
+ expect_compiled('foo = 42 if 2.5').to include('if ($truthy(2.5))')
223
+ expect_compiled('foo = 42 if true').to include('if ($truthy(true))')
224
+ end
225
+
226
+ it 'adds nil check for boolean method calls' do
227
+ expect_compiled('foo = 42 if true.something').to include('if ($truthy(true.$something()))')
228
+ end
229
+
230
+ it 'adds nil check for strings' do
231
+ expect_compiled('foo = 42 if "test"').to include('if ($truthy("test"))')
232
+ end
233
+
234
+ it 'adds nil check for lvars' do
235
+ expect_compiled("bar = 4\nfoo = 42 if bar").to include('if ($truthy(bar))')
236
+ end
237
+
238
+ it 'adds nil check for constants' do
239
+ expect_compiled("foo = 42 if Test").to include("if ($truthy($$($nesting, 'Test')))")
240
+ end
241
+ end
242
+ end
243
+
244
+ context 'parentheses' do
245
+ context 'with operators' do
246
+ it 'adds nil check for primitives' do
247
+ expect_compiled('foo = 42 if (2 > 3)').to include('if ($truthy($rb_gt(2, 3))')
248
+ expect_compiled('foo = 42 if (2.5 > 3.5)').to include('if ($truthy($rb_gt(2.5, 3.5))')
249
+ expect_compiled('foo = 42 if (true > false)').to include('if ($truthy($rb_gt(true, false))')
250
+
251
+ expect_compiled('foo = 42 if (2 == 3)').to include("if ($truthy((2)['$=='](3))")
252
+ expect_compiled('foo = 42 if (2.5 == 3.5)').to include("if ($truthy((2.5)['$=='](3.5))")
253
+ expect_compiled('foo = 42 if (true == false)').to include("if ($truthy(true['$=='](false)))")
254
+ end
255
+
256
+ it 'adds nil check for strings' do
257
+ expect_compiled('foo = 42 if ("test" > "bar")').to include('if ($truthy($rb_gt("test", "bar"))')
258
+ expect_compiled('foo = 42 if ("test" == "bar")').to include("if ($truthy(\"test\"['$=='](\"bar\"))")
259
+ end
260
+
261
+ it 'adds nil check for lvars' do
262
+ expect_compiled("bar = 4\nfoo = 42 if (bar > 5)").to include('if ($truthy($rb_gt(bar, 5))')
263
+ expect_compiled("bar = 4\nfoo = 42 if (bar == 5)").to include("if ($truthy(bar['$=='](5))) ")
264
+ end
265
+
266
+ it 'adds nil check for constants' do
267
+ expect_compiled("foo = 42 if (Test > 4)").to include("if ($truthy($rb_gt($$($nesting, 'Test'), 4))")
268
+ expect_compiled("foo = 42 if (Test == 4)").to include("if ($truthy($$($nesting, 'Test')['$=='](4))")
269
+ end
270
+ end
271
+
272
+ context 'without operators' do
273
+ it 'adds nil check for primitives' do
274
+ expect_compiled('foo = 42 if (2)').to include('if ($truthy(2)')
275
+ expect_compiled('foo = 42 if (2.5)').to include('if ($truthy(2.5)')
276
+ expect_compiled('foo = 42 if (true)').to include('if ($truthy(true)')
277
+ end
278
+
279
+ it 'adds nil check for boolean method calls' do
280
+ expect_compiled('foo = 42 if (true.something)').to include('if ($truthy(true.$something())')
281
+ end
282
+
283
+ it 'adds nil check for strings' do
284
+ expect_compiled('foo = 42 if ("test")').to include('if ($truthy("test")')
285
+ end
286
+
287
+ it 'adds nil check for lvars' do
288
+ expect_compiled("bar = 4\nfoo = 42 if (bar)").to include('if ($truthy(bar)')
289
+ end
290
+
291
+ it 'adds nil check for constants' do
292
+ expect_compiled("foo = 42 if (Test)").to include("if ($truthy($$($nesting, 'Test'))")
293
+ end
294
+ end
295
+ end
296
+ end
297
+
298
+ describe 'Regexp flags' do
299
+ it 'skips the unsupported ones' do
300
+ expect_compiled("/foobar/nix").to include("/foobar/i")
301
+ end
302
+ end
303
+
304
+ describe 'x-strings' do
305
+ describe 'semicolons handling' do
306
+ def compiling(code, &block)
307
+ compiler = Opal::Compiler.new(code)
308
+ yield compiler
309
+ end
310
+
311
+ it "respects JS returns not doubling the trailing semicolon" do
312
+ expect_compiled(%q{
313
+ def foo
314
+ `return bar(baz)`
315
+ end
316
+ }).to include(" return bar(baz);\n")
317
+
318
+ expect_compiled(%q{
319
+ def foo
320
+ %x{return bar(baz)}
321
+ end
322
+ }).to include(" return bar(baz);\n")
323
+
324
+ expect_compiled(%q{
325
+ def foo
326
+ %x{return bar(baz);}
327
+ end
328
+ }).to include(" return bar(baz);\n")
329
+
330
+ expect_compiled(%q{
331
+ def foo
332
+ %x{
333
+ return bar(baz)
334
+ }
335
+ end
336
+ }).to include(" return bar(baz);\n")
337
+
338
+ expect_compiled(%q{
339
+ def foo
340
+ %x{
341
+ return bar(baz);
342
+ }
343
+ end
344
+ }).to include(" return bar(baz);\n")
345
+
346
+ expect_compiled(%q{
347
+ def foo
348
+ 789
349
+ `#{123 + bar} * 456;`
350
+ end
351
+ }).to include(" return $rb_plus(123, self.$bar()) * 456;\n")
352
+
353
+ expect_compiled(%q{
354
+ def foo
355
+ 789
356
+ `#$bar * 456`
357
+ end
358
+ }).to include(" return $gvars.bar * 456;\n")
359
+
360
+ expect_compiled(%q{
361
+ def foo
362
+ 789
363
+ `#@bar * 456`
364
+ end
365
+ }).to include(" return self.bar * 456;\n")
366
+
367
+ expect_compiled(%q{
368
+ def foo
369
+ 789
370
+ `456 * #@bar`
371
+ end
372
+ }).to include(" return 456 * self.bar;\n")
373
+
374
+ expect_compiled(%q{
375
+ if `compare === nil`
376
+ raise ArgumentError, "comparison of #{a.class} with #{b.class} failed"
377
+ end
378
+ }).to include(" if ($truthy(compare === nil)) {\n")
379
+
380
+ expect_compiled(%q{
381
+ def <<(count)
382
+ count = Opal.coerce_to! count, Integer, :to_int
383
+ `#{count} > 0 ? self << #{count} : self >> -#{count}`
384
+ end
385
+ }).to include(" return count > 0 ? self << count : self >> -count;\n")
386
+
387
+ expect_compiled(%q{
388
+ def self.exist? path
389
+ path = path.path if path.respond_to? :path
390
+ `return executeIOAction(function(){return __fs__.existsSync(#{path})})`
391
+ end
392
+ }).to include(" return executeIOAction(function(){return __fs__.existsSync(path)});\n")
393
+
394
+ expect_compiled(%q{
395
+ def self.exist? path
396
+ path = path.path if path.respond_to? :path
397
+ `executeIOAction(function(){return __fs__.existsSync(#{path})})`
398
+ end
399
+ }).to include(" return executeIOAction(function(){return __fs__.existsSync(path)});\n")
400
+
401
+ expect_compiled(%q{
402
+ def self.exist? path
403
+ path = path.path if path.respond_to? :path
404
+ `return executeIOAction(function(){return __fs__.existsSync(#{path})});`
405
+ end
406
+ }).to include(" return executeIOAction(function(){return __fs__.existsSync(path)});\n")
407
+
408
+ expect_compiled(%q{
409
+ def self.exist? path
410
+ path = path.path if path.respond_to? :path
411
+ `executeIOAction(function(){return __fs__.existsSync(#{path})});`
412
+ end
413
+ }).to include(" return executeIOAction(function(){return __fs__.existsSync(path)});\n")
414
+ end
415
+
416
+ it 'warns if a semicolon is used in a single line' do
417
+ expect_number_of_warnings(%{a = `1;`}).to eq(1)
418
+ expect_number_of_warnings(%{a = `1;`; return}).to eq(1)
419
+ expect_number_of_warnings(%{a = `1;`}).to eq(1)
420
+ expect_number_of_warnings(%{a = ` 1; `}).to eq(1)
421
+ expect_number_of_warnings(%{a = %x{\n 1; \n}}).to eq(1)
422
+ expect_number_of_warnings(%{def foo; ` 1; `; end}).to eq(1)
423
+ end
424
+
425
+ it 'does not warn for statements' do
426
+ expect_number_of_warnings(%{`foo;`; return}).to eq(0)
427
+ expect_number_of_warnings(%{`foo;`; 123}).to eq(0)
428
+ end
429
+
430
+ it 'does not warn for multiline x-strings' do
431
+ expect_number_of_warnings(%{a = `1;\n2;`}).to eq(0)
432
+ expect_number_of_warnings(%{a = `1;\n2;3; `}).to eq(0)
433
+ expect_number_of_warnings(%{def foo;` 1;\n `;end}).to eq(1)
434
+ end
435
+ end
436
+
437
+ specify 'when empty' do
438
+ expect_compiled(%q{
439
+ %x{
440
+ }
441
+ }).to include("return nil\n")
442
+
443
+ expect_compiled(%q{
444
+ %x{}
445
+ }).to include("return nil\n")
446
+
447
+ expect_compiled(%q{
448
+ `
449
+
450
+ `
451
+ }).to include("return nil\n")
452
+
453
+ expect_compiled(%q{
454
+ ``
455
+ }).to include("return nil\n")
456
+ end
457
+
458
+ def expect_number_of_warnings(code)
459
+ warnings_number = 0
460
+ compiler = Opal::Compiler.new(code)
461
+ allow(compiler).to receive(:warning) { warnings_number += 1}
462
+ compiler.compile
463
+ expect(warnings_number)
464
+ end
465
+ end
466
+
467
+ describe '#magic_comments' do
468
+ def expect_magic_comments_for(*lines)
469
+ expect(compiler_for(lines.join("\n")).magic_comments)
470
+ end
471
+
472
+ it 'extracts them in a hash' do
473
+ expect_magic_comments_for("").to eq({})
474
+
475
+ expect_magic_comments_for(
476
+ "",
477
+ "# foo:true",
478
+ "",
479
+ "",
480
+ "",
481
+ "#bar : false",
482
+ "#baz :qux",
483
+ "#biz :boz",
484
+ "baz",
485
+ ).to eq(
486
+ foo: true,
487
+ bar: false,
488
+ baz: "qux",
489
+ biz: "boz"
490
+ )
491
+
492
+ expect_magic_comments_for(
493
+ "#baz :qux",
494
+ "#biz :boz",
495
+ "",
496
+ "baz",
497
+ ).to eq(
498
+ baz: "qux",
499
+ biz: "boz"
500
+ )
501
+
502
+ expect_magic_comments_for(
503
+ "#-*- baz :qux-*-",
504
+ "# -*-biz :boz -*- ",
505
+ ).to eq(
506
+ baz: "qux",
507
+ biz: "boz"
508
+ )
509
+ end
510
+
511
+ it 'accepts complex values' do
512
+ expect_magic_comments_for("").to eq({})
513
+
514
+ expect_magic_comments_for(
515
+ "#baz: qux,naz!",
516
+ "#biz : boz? ,bux, []=",
517
+ "#buz :<<,+,!@",
518
+ ).to eq(
519
+ baz: "qux,naz!",
520
+ biz: "boz? ,bux, []=",
521
+ buz: "<<,+,!@",
522
+ )
523
+ end
524
+ end
525
+
526
+ describe 'magic encoding comment' do
527
+ let(:diagnostics) { [] }
528
+
529
+ around(:each) do |e|
530
+ original_diagnostics_consumer = Opal::Parser.default_parser_class.diagnostics_consumer
531
+ Opal::Parser.default_parser_class.diagnostics_consumer = ->(diagnostic) { diagnostics << diagnostic }
532
+ e.run
533
+ Opal::Parser.default_parser_class.diagnostics_consumer = original_diagnostics_consumer
534
+ end
535
+
536
+ let(:encoding_comment) { '' }
537
+ let(:string) { '' }
538
+
539
+ let(:file) do
540
+ <<-RUBY
541
+ # encoding: #{encoding_comment}
542
+ "#{string}"
543
+ RUBY
544
+ end
545
+
546
+ shared_examples 'it compiles the string as' do |expected|
547
+ it "compiles the string as #{expected}" do
548
+ expect_compiled(file).to include(expected)
549
+ end
550
+ end
551
+
552
+ shared_examples 'it re-encodes the string using $force_encoding' do
553
+ it 'it re-encodes the string using $force_encoding' do
554
+ expect_compiled(file).to include("$force_encoding")
555
+ end
556
+ end
557
+
558
+ shared_examples 'it does not re-encode the string using $force_encoding' do
559
+ it 'it does not re-encode the string using $force_encoding' do
560
+ expect_compiled(file).to_not include("$force_encoding")
561
+ end
562
+ end
563
+
564
+ shared_examples 'it does not print any warnings' do
565
+ it 'does not print any warnings' do
566
+ compile(file)
567
+ expect(diagnostics).to eq([])
568
+ end
569
+ end
570
+
571
+ context 'utf-8 comment' do
572
+ let(:encoding_comment) { 'utf-8' }
573
+
574
+ context 'valid sequence' do
575
+ let(:string) { 'λ' }
576
+
577
+ include_examples 'it compiles the string as', 'λ'.inspect
578
+ include_examples 'it does not re-encode the string using $force_encoding'
579
+ include_examples 'it does not print any warnings'
580
+ end
581
+
582
+ context 'invalid sequence' do
583
+ let(:string) { "\xFF" }
584
+
585
+ it 'raises an error' do
586
+ expect {
587
+ compiled(file)
588
+ }.to raise_error(EncodingError, 'invalid byte sequence in UTF-8')
589
+ end
590
+ end
591
+ end
592
+
593
+ context 'ascii-8bit comment' do
594
+ let(:encoding_comment) { 'ascii-8bit' }
595
+
596
+ context 'valid sequence' do
597
+ let(:string) { "\xFF" }
598
+
599
+ include_examples 'it compiles the string as', '$enc("\xFF", "ASCII-8BIT")'
600
+ include_examples 'it does not print any warnings'
601
+ end
602
+
603
+ context 'unicode sequence' do
604
+ let(:string) { 'λ' }
605
+ encoded_string = 'λ'.force_encoding("ascii-8bit")
606
+
607
+ include_examples 'it compiles the string as', "$enc(#{encoded_string.inspect}, \"ASCII-8BIT\")"
608
+ include_examples 'it does not print any warnings'
609
+ end
610
+ end
611
+ end
612
+
613
+ describe 'a compilation error' do
614
+ context 'at compile time' do
615
+ it 'adds the file and line to the backtrace' do
616
+ error = nil
617
+ begin
618
+ compiled('BEGIN {}', file: 'foobar.js.rb')
619
+ rescue Opal::SyntaxError => syntax_error
620
+ error = syntax_error
621
+ end
622
+
623
+ expect(error.backtrace[0]).to eq("foobar.js.rb:in `BEGIN {}'")
624
+ expect(compiler_backtrace(error)[0]).to end_with(":in `error'")
625
+ expect(compiler_backtrace(error)[-3]).to end_with(":in `block in compile'")
626
+ expect(compiler_backtrace(error)[-1]).to end_with(":in `compile'")
627
+ expect(error.backtrace.size).to be > 1
628
+ end
629
+ end
630
+
631
+ context 'at parse time' do
632
+ it 'adds the file and line to the backtrace' do
633
+ error = nil
634
+ begin
635
+ parsed('def foo', file: 'foobar.js.rb')
636
+ rescue Opal::SyntaxError => syntax_error
637
+ error = syntax_error
638
+ end
639
+ expect(error.backtrace[0]).to eq("foobar.js.rb:1:in `def foo'")
640
+ expect(compiler_backtrace(error)[0]).to end_with(":in `block in parse'")
641
+ expect(error.backtrace.size).to be > 1
642
+ end
643
+ end
644
+
645
+ def compiler_backtrace(error)
646
+ error.backtrace.grep(%r{lib/opal/compiler\.rb})
647
+ end
648
+ end
649
+
650
+ describe '[regressions]' do
651
+ it 'accepts empty rescue within while loop' do
652
+ # found running bm_vm1_rescue.rb
653
+ # was raising: NoMethodError: undefined method `type' for nil:NilClass
654
+ expect{
655
+ compiled <<-RUBY
656
+ while foo
657
+ begin
658
+ rescue
659
+ end
660
+ end
661
+ RUBY
662
+ }.not_to raise_error
663
+ end
664
+
665
+ it 'accepts a comment as the sole body of a for loop' do
666
+ expect{
667
+ compiled <<-RUBY
668
+ for foo in bar
669
+ #
670
+ end
671
+ RUBY
672
+ }.not_to raise_error
673
+ end
674
+ end
675
+
676
+ def compiled(*args)
677
+ Opal::Compiler.new(*args).compile
678
+ end
679
+
680
+ def parsed(*args)
681
+ Opal::Compiler.new(*args).parse
682
+ end
683
+
684
+ alias compile compiled
685
+
686
+ def expect_compiled(*args)
687
+ expect(compiled(*args))
688
+ end
689
+
690
+ def compiler_for(*args)
691
+ Opal::Compiler.new(*args).tap(&:compile)
692
+ end
693
+ end