index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. var test = require('tape')
  2. var acorn = require('../')
  3. var walk = require('../walk')
  4. var baseAcorn = require('acorn')
  5. test('parses object spread syntax', function (t) {
  6. var ast = acorn.parse('var a = { ...b }')
  7. t.equal(ast.body[0].declarations[0].init.type, 'ObjectExpression')
  8. t.equal(ast.body[0].declarations[0].init.properties[0].type, 'SpreadElement')
  9. ast = acorn.parse('function a ({ ...b }) {}')
  10. t.equal(ast.body[0].params[0].type, 'ObjectPattern')
  11. t.equal(ast.body[0].params[0].properties[0].type, 'RestElement')
  12. t.end()
  13. })
  14. test('does not change main acorn module', function (t) {
  15. t.throws(function () {
  16. baseAcorn.parse('var a = 10n')
  17. })
  18. t.end()
  19. })
  20. test('tokenizes object spread syntax', function (t) {
  21. var tokenizer = acorn.tokenizer('var a = { ...b }')
  22. t.doesNotThrow(function (t) {
  23. while (tokenizer.getToken().type !== acorn.tokTypes.eof) {}
  24. })
  25. t.end()
  26. })
  27. test('allows hashbangs by default', function (t) {
  28. t.doesNotThrow(function () {
  29. acorn.parse('#!/usr/bin/env node\nconsole.log("ok")')
  30. })
  31. t.end()
  32. })
  33. test('allows top level return by default', function (t) {
  34. t.doesNotThrow(function () {
  35. acorn.parse('console.log("ok"); return; console.log("not ok")')
  36. })
  37. t.end()
  38. })
  39. test('supports async generators', function (t) {
  40. t.doesNotThrow(function () {
  41. acorn.parse('async function* a () { await x; yield 1 }')
  42. })
  43. t.end()
  44. })
  45. test('supports async iteration', function (t) {
  46. t.doesNotThrow(function () {
  47. acorn.parse('async function l (y) { for await (const x of y) {} }')
  48. })
  49. t.end()
  50. })
  51. test('supports optional catch', function (t) {
  52. t.doesNotThrow(function () {
  53. acorn.parse('try { throw null } catch {}')
  54. })
  55. t.end()
  56. })
  57. test('supports bigint', function (t) {
  58. t.doesNotThrow(function () {
  59. acorn.parse('50n ** 50n')
  60. })
  61. t.end()
  62. })
  63. test('supports numeric separators', function (t) {
  64. t.doesNotThrow(function () {
  65. acorn.parse('50_000_000n ** 1n')
  66. })
  67. t.end()
  68. })
  69. test('supports import.meta with sourceType: module', function (t) {
  70. t.doesNotThrow(function () {
  71. acorn.parse('console.log(import.meta.url)', { sourceType: 'module' })
  72. })
  73. t.end()
  74. })
  75. test('supports dynamic import() with sourceType: module', function (t) {
  76. t.doesNotThrow(function () {
  77. acorn.parse('import("./whatever.mjs")', { sourceType: 'module' })
  78. })
  79. t.end()
  80. })
  81. test('supports dynamic import() with sourceType: script', function (t) {
  82. t.doesNotThrow(function () {
  83. acorn.parse('import("./whatever.mjs")', { sourceType: 'script' })
  84. })
  85. t.end()
  86. })
  87. test('supports class instance properties', function (t) {
  88. t.doesNotThrow(function () {
  89. acorn.parse('class X { x = y }', { sourceType: 'script' })
  90. })
  91. t.end()
  92. })
  93. test('supports private class instance properties', function (t) {
  94. t.doesNotThrow(function () {
  95. acorn.parse('class X { #x = y }', { sourceType: 'script' })
  96. })
  97. t.end()
  98. })
  99. test('supports class static properties', function (t) {
  100. t.doesNotThrow(function () {
  101. acorn.parse('class X { static x = y }', { sourceType: 'script' })
  102. })
  103. t.end()
  104. })
  105. test('supports private class static properties', function (t) {
  106. t.doesNotThrow(function () {
  107. acorn.parse('class X { static #x = y }', { sourceType: 'script' })
  108. })
  109. t.end()
  110. })
  111. test('supports namespace export syntax with sourceType: module', function (t) {
  112. t.doesNotThrow(function () {
  113. acorn.parse('export * as x from "./x.mjs";', { sourceType: 'module' })
  114. })
  115. t.end()
  116. })
  117. test('walk supports plugin syntax', function (t) {
  118. var ast = acorn.parse(
  119. 'async function* a() { try { await import(xyz); } catch { for await (x of null) {} } yield import.meta.url }',
  120. { sourceType: 'module' }
  121. )
  122. t.plan(2)
  123. walk.simple(ast, {
  124. Import: function () {
  125. t.pass('import()')
  126. },
  127. MetaProperty: function () {
  128. t.pass('import.meta')
  129. }
  130. })
  131. t.end()
  132. })