github.com/evanw/esbuild@v0.21.4/internal/js_parser/js_parser_lower_test.go (about) 1 package js_parser 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/evanw/esbuild/internal/compat" 8 ) 9 10 func TestLowerFunctionArgumentScope(t *testing.T) { 11 templates := []string{ 12 "(x = %s) => {\n};\n", 13 "(function(x = %s) {\n});\n", 14 "function foo(x = %s) {\n}\n", 15 16 "({ [%s]: x }) => {\n};\n", 17 "(function({ [%s]: x }) {\n});\n", 18 "function foo({ [%s]: x }) {\n}\n", 19 20 "({ x = %s }) => {\n};\n", 21 "(function({ x = %s }) {\n});\n", 22 "function foo({ x = %s }) {\n}\n", 23 } 24 25 for _, template := range templates { 26 test := func(before string, after string) { 27 expectPrintedTarget(t, 2015, fmt.Sprintf(template, before), fmt.Sprintf(template, after)) 28 } 29 30 test("a() ?? b", "((_a) => (_a = a()) != null ? _a : b)()") 31 test("a()?.b", "((_a) => (_a = a()) == null ? void 0 : _a.b)()") 32 test("a?.b?.()", "((_a) => (_a = a == null ? void 0 : a.b) == null ? void 0 : _a.call(a))()") 33 test("a.b.c?.()", "((_a) => ((_b) => (_b = (_a = a.b).c) == null ? void 0 : _b.call(_a))())()") 34 test("class { static a }", "((_a) => (_a = class {\n}, __publicField(_a, \"a\"), _a))()") 35 } 36 } 37 38 func TestLowerArrowFunction(t *testing.T) { 39 expectPrintedTarget(t, 5, "function foo(a) { arr.forEach(e => this.foo(e)) }", 40 "function foo(a) {\n var _this = this;\n arr.forEach(function(e) {\n return _this.foo(e);\n });\n}\n") 41 expectPrintedTarget(t, 5, "function foo(a) { return () => arguments[0] }", 42 "function foo(a) {\n var _arguments = arguments;\n return function() {\n return _arguments[0];\n };\n}\n") 43 44 expectPrintedTarget(t, 5, "function foo(a) { arr.forEach(function(e) { return this.foo(e) }) }", 45 "function foo(a) {\n arr.forEach(function(e) {\n return this.foo(e);\n });\n}\n") 46 expectPrintedTarget(t, 5, "function foo(a) { return function() { return arguments[0] } }", 47 "function foo(a) {\n return function() {\n return arguments[0];\n };\n}\n") 48 49 // Handling this case isn't implemented yet 50 expectPrintedTarget(t, 5, "var foo = () => this", 51 "var foo = function() {\n return this;\n};\n") 52 } 53 54 func TestLowerNullishCoalescing(t *testing.T) { 55 expectParseError(t, "a ?? b && c", 56 "<stdin>: ERROR: Cannot use \"&&\" with \"??\" without parentheses\n"+ 57 "NOTE: Expressions of the form \"x ?? y && z\" are not allowed in JavaScript. "+ 58 "You must disambiguate between \"(x ?? y) && z\" and \"x ?? (y && z)\" by adding parentheses.\n") 59 expectParseError(t, "a ?? b || c", 60 "<stdin>: ERROR: Cannot use \"||\" with \"??\" without parentheses\n"+ 61 "NOTE: Expressions of the form \"x ?? y || z\" are not allowed in JavaScript. "+ 62 "You must disambiguate between \"(x ?? y) || z\" and \"x ?? (y || z)\" by adding parentheses.\n") 63 expectParseError(t, "a ?? b && c || d", 64 "<stdin>: ERROR: Cannot use \"&&\" with \"??\" without parentheses\n"+ 65 "NOTE: Expressions of the form \"x ?? y && z\" are not allowed in JavaScript. "+ 66 "You must disambiguate between \"(x ?? y) && z\" and \"x ?? (y && z)\" by adding parentheses.\n"+ 67 "<stdin>: ERROR: Cannot use \"||\" with \"??\" without parentheses\n"+ 68 "NOTE: Expressions of the form \"x ?? y || z\" are not allowed in JavaScript. "+ 69 "You must disambiguate between \"(x ?? y) || z\" and \"x ?? (y || z)\" by adding parentheses.\n") 70 expectParseError(t, "a ?? b || c && d", 71 "<stdin>: ERROR: Cannot use \"||\" with \"??\" without parentheses\n"+ 72 "NOTE: Expressions of the form \"x ?? y || z\" are not allowed in JavaScript. "+ 73 "You must disambiguate between \"(x ?? y) || z\" and \"x ?? (y || z)\" by adding parentheses.\n") 74 expectParseError(t, "a && b ?? c", 75 "<stdin>: ERROR: Cannot use \"??\" with \"&&\" without parentheses\n"+ 76 "NOTE: Expressions of the form \"x && y ?? z\" are not allowed in JavaScript. "+ 77 "You must disambiguate between \"(x && y) ?? z\" and \"x && (y ?? z)\" by adding parentheses.\n") 78 expectParseError(t, "a || b ?? c", 79 "<stdin>: ERROR: Cannot use \"??\" with \"||\" without parentheses\n"+ 80 "NOTE: Expressions of the form \"x || y ?? z\" are not allowed in JavaScript. "+ 81 "You must disambiguate between \"(x || y) ?? z\" and \"x || (y ?? z)\" by adding parentheses.\n") 82 expectParseError(t, "a && b || c ?? c", 83 "<stdin>: ERROR: Cannot use \"??\" with \"||\" without parentheses\n"+ 84 "NOTE: Expressions of the form \"x || y ?? z\" are not allowed in JavaScript. "+ 85 "You must disambiguate between \"(x || y) ?? z\" and \"x || (y ?? z)\" by adding parentheses.\n") 86 expectParseError(t, "a || b && c ?? d", 87 "<stdin>: ERROR: Cannot use \"??\" with \"||\" without parentheses\n"+ 88 "NOTE: Expressions of the form \"x || y ?? z\" are not allowed in JavaScript. "+ 89 "You must disambiguate between \"(x || y) ?? z\" and \"x || (y ?? z)\" by adding parentheses.\n") 90 expectPrinted(t, "a ?? b, b && c", "a ?? b, b && c;\n") 91 expectPrinted(t, "a ?? b, b || c", "a ?? b, b || c;\n") 92 expectPrinted(t, "a && b, b ?? c", "a && b, b ?? c;\n") 93 expectPrinted(t, "a || b, b ?? c", "a || b, b ?? c;\n") 94 95 expectPrintedTarget(t, 2020, "a ?? b", "a ?? b;\n") 96 expectPrintedTarget(t, 2019, "a ?? b", "a != null ? a : b;\n") 97 expectPrintedTarget(t, 2019, "a() ?? b()", "var _a;\n(_a = a()) != null ? _a : b();\n") 98 expectPrintedTarget(t, 2019, "function foo() { if (x) { a() ?? b() ?? c() } }", 99 "function foo() {\n var _a, _b;\n if (x) {\n (_b = (_a = a()) != null ? _a : b()) != null ? _b : c();\n }\n}\n") 100 expectPrintedTarget(t, 2019, "() => a ?? b", "() => a != null ? a : b;\n") 101 expectPrintedTarget(t, 2019, "() => a() ?? b()", "() => {\n var _a;\n return (_a = a()) != null ? _a : b();\n};\n") 102 103 // Temporary variables should not come before "use strict" 104 expectPrintedTarget(t, 2019, "function f() { /*! @license */ 'use strict'; a = b.c ?? d }", 105 "function f() {\n /*! @license */\n \"use strict\";\n var _a;\n a = (_a = b.c) != null ? _a : d;\n}\n") 106 } 107 108 func TestLowerNullishCoalescingAssign(t *testing.T) { 109 expectPrinted(t, "a ??= b", "a ??= b;\n") 110 111 expectPrintedTarget(t, 2019, "a ??= b", "a != null ? a : a = b;\n") 112 expectPrintedTarget(t, 2019, "a.b ??= c", "var _a;\n(_a = a.b) != null ? _a : a.b = c;\n") 113 expectPrintedTarget(t, 2019, "a().b ??= c", "var _a, _b;\n(_b = (_a = a()).b) != null ? _b : _a.b = c;\n") 114 expectPrintedTarget(t, 2019, "a[b] ??= c", "var _a;\n(_a = a[b]) != null ? _a : a[b] = c;\n") 115 expectPrintedTarget(t, 2019, "a()[b()] ??= c", "var _a, _b, _c;\n(_c = (_a = a())[_b = b()]) != null ? _c : _a[_b] = c;\n") 116 117 expectPrintedTarget(t, 2019, "class Foo { #x; constructor() { this.#x ??= 2 } }", `var _x; 118 class Foo { 119 constructor() { 120 __privateAdd(this, _x); 121 var _a; 122 (_a = __privateGet(this, _x)) != null ? _a : __privateSet(this, _x, 2); 123 } 124 } 125 _x = new WeakMap(); 126 `) 127 128 expectPrintedTarget(t, 2020, "a ??= b", "a ?? (a = b);\n") 129 expectPrintedTarget(t, 2020, "a.b ??= c", "a.b ?? (a.b = c);\n") 130 expectPrintedTarget(t, 2020, "a().b ??= c", "var _a;\n(_a = a()).b ?? (_a.b = c);\n") 131 expectPrintedTarget(t, 2020, "a[b] ??= c", "a[b] ?? (a[b] = c);\n") 132 expectPrintedTarget(t, 2020, "a()[b()] ??= c", "var _a, _b;\n(_a = a())[_b = b()] ?? (_a[_b] = c);\n") 133 134 expectPrintedTarget(t, 2020, "class Foo { #x; constructor() { this.#x ??= 2 } }", `var _x; 135 class Foo { 136 constructor() { 137 __privateAdd(this, _x); 138 __privateGet(this, _x) ?? __privateSet(this, _x, 2); 139 } 140 } 141 _x = new WeakMap(); 142 `) 143 144 expectPrintedTarget(t, 2021, "a ??= b", "a ??= b;\n") 145 expectPrintedTarget(t, 2021, "a.b ??= c", "a.b ??= c;\n") 146 expectPrintedTarget(t, 2021, "a().b ??= c", "a().b ??= c;\n") 147 expectPrintedTarget(t, 2021, "a[b] ??= c", "a[b] ??= c;\n") 148 expectPrintedTarget(t, 2021, "a()[b()] ??= c", "a()[b()] ??= c;\n") 149 150 expectPrintedTarget(t, 2021, "class Foo { #x; constructor() { this.#x ??= 2 } }", `var _x; 151 class Foo { 152 constructor() { 153 __privateAdd(this, _x); 154 __privateGet(this, _x) ?? __privateSet(this, _x, 2); 155 } 156 } 157 _x = new WeakMap(); 158 `) 159 160 // Temporary variables should not come before "use strict" 161 expectPrintedTarget(t, 2019, "function f() { /*! @license */ 'use strict'; a.b ??= c.d }", 162 "function f() {\n /*! @license */\n \"use strict\";\n var _a;\n (_a = a.b) != null ? _a : a.b = c.d;\n}\n") 163 } 164 165 func TestLowerLogicalAssign(t *testing.T) { 166 expectPrinted(t, "a &&= b", "a &&= b;\n") 167 expectPrinted(t, "a ||= b", "a ||= b;\n") 168 169 expectPrintedTarget(t, 2020, "a &&= b", "a && (a = b);\n") 170 expectPrintedTarget(t, 2020, "a.b &&= c", "a.b && (a.b = c);\n") 171 expectPrintedTarget(t, 2020, "a().b &&= c", "var _a;\n(_a = a()).b && (_a.b = c);\n") 172 expectPrintedTarget(t, 2020, "a[b] &&= c", "a[b] && (a[b] = c);\n") 173 expectPrintedTarget(t, 2020, "a()[b()] &&= c", "var _a, _b;\n(_a = a())[_b = b()] && (_a[_b] = c);\n") 174 175 expectPrintedTarget(t, 2020, "class Foo { #x; constructor() { this.#x &&= 2 } }", `var _x; 176 class Foo { 177 constructor() { 178 __privateAdd(this, _x); 179 __privateGet(this, _x) && __privateSet(this, _x, 2); 180 } 181 } 182 _x = new WeakMap(); 183 `) 184 185 expectPrintedTarget(t, 2021, "a &&= b", "a &&= b;\n") 186 expectPrintedTarget(t, 2021, "a.b &&= c", "a.b &&= c;\n") 187 expectPrintedTarget(t, 2021, "a().b &&= c", "a().b &&= c;\n") 188 expectPrintedTarget(t, 2021, "a[b] &&= c", "a[b] &&= c;\n") 189 expectPrintedTarget(t, 2021, "a()[b()] &&= c", "a()[b()] &&= c;\n") 190 191 expectPrintedTarget(t, 2021, "class Foo { #x; constructor() { this.#x &&= 2 } }", `var _x; 192 class Foo { 193 constructor() { 194 __privateAdd(this, _x); 195 __privateGet(this, _x) && __privateSet(this, _x, 2); 196 } 197 } 198 _x = new WeakMap(); 199 `) 200 201 expectPrintedTarget(t, 2020, "a ||= b", "a || (a = b);\n") 202 expectPrintedTarget(t, 2020, "a.b ||= c", "a.b || (a.b = c);\n") 203 expectPrintedTarget(t, 2020, "a().b ||= c", "var _a;\n(_a = a()).b || (_a.b = c);\n") 204 expectPrintedTarget(t, 2020, "a[b] ||= c", "a[b] || (a[b] = c);\n") 205 expectPrintedTarget(t, 2020, "a()[b()] ||= c", "var _a, _b;\n(_a = a())[_b = b()] || (_a[_b] = c);\n") 206 207 expectPrintedTarget(t, 2020, "class Foo { #x; constructor() { this.#x ||= 2 } }", `var _x; 208 class Foo { 209 constructor() { 210 __privateAdd(this, _x); 211 __privateGet(this, _x) || __privateSet(this, _x, 2); 212 } 213 } 214 _x = new WeakMap(); 215 `) 216 217 expectPrintedTarget(t, 2021, "a ||= b", "a ||= b;\n") 218 expectPrintedTarget(t, 2021, "a.b ||= c", "a.b ||= c;\n") 219 expectPrintedTarget(t, 2021, "a().b ||= c", "a().b ||= c;\n") 220 expectPrintedTarget(t, 2021, "a[b] ||= c", "a[b] ||= c;\n") 221 expectPrintedTarget(t, 2021, "a()[b()] ||= c", "a()[b()] ||= c;\n") 222 223 expectPrintedTarget(t, 2021, "class Foo { #x; constructor() { this.#x ||= 2 } }", `var _x; 224 class Foo { 225 constructor() { 226 __privateAdd(this, _x); 227 __privateGet(this, _x) || __privateSet(this, _x, 2); 228 } 229 } 230 _x = new WeakMap(); 231 `) 232 } 233 234 func TestLowerAsyncFunctions(t *testing.T) { 235 // Lowered non-arrow functions with argument evaluations should merely use 236 // "arguments" rather than allocating a new array when forwarding arguments 237 expectPrintedTarget(t, 2015, "async function foo(a, b = couldThrowErrors()) {console.log(a, b);}", `function foo(_0) { 238 return __async(this, arguments, function* (a, b = couldThrowErrors()) { 239 console.log(a, b); 240 }); 241 } 242 `) 243 // Skip forwarding altogether when parameter evaluation obviously cannot throw 244 expectPrintedTarget(t, 2015, "async (a, b = 123) => {console.log(a, b);}", `(a, b = 123) => __async(this, null, function* () { 245 console.log(a, b); 246 }); 247 `) 248 } 249 250 func TestLowerClassSideEffectOrder(t *testing.T) { 251 // The order of computed property side effects must not change 252 expectPrintedTarget(t, 2015, `class Foo { 253 [a()]() {} 254 [b()]; 255 [c()] = 1; 256 [d()]() {} 257 static [e()]; 258 static [f()] = 1; 259 static [g()]() {} 260 [h()]; 261 } 262 `, `var _a, _b, _c, _d, _e, _f; 263 class Foo { 264 constructor() { 265 __publicField(this, _f); 266 __publicField(this, _e, 1); 267 __publicField(this, _a); 268 } 269 [a()]() { 270 } 271 [(_f = b(), _e = c(), d())]() { 272 } 273 static [(_d = e(), _c = f(), _b = g(), _a = h(), _b)]() { 274 } 275 } 276 __publicField(Foo, _d); 277 __publicField(Foo, _c, 1); 278 `) 279 } 280 281 func TestLowerClassInstance(t *testing.T) { 282 expectPrintedTarget(t, 2015, "class Foo {}", "class Foo {\n}\n") 283 expectPrintedTarget(t, 2015, "class Foo { foo }", "class Foo {\n constructor() {\n __publicField(this, \"foo\");\n }\n}\n") 284 expectPrintedTarget(t, 2015, "class Foo { foo = null }", "class Foo {\n constructor() {\n __publicField(this, \"foo\", null);\n }\n}\n") 285 expectPrintedTarget(t, 2015, "class Foo { 123 }", "class Foo {\n constructor() {\n __publicField(this, 123);\n }\n}\n") 286 expectPrintedTarget(t, 2015, "class Foo { 123 = null }", "class Foo {\n constructor() {\n __publicField(this, 123, null);\n }\n}\n") 287 expectPrintedTarget(t, 2015, "class Foo { [foo] }", "var _a;\n_a = foo;\nclass Foo {\n constructor() {\n __publicField(this, _a);\n }\n}\n") 288 expectPrintedTarget(t, 2015, "class Foo { [foo] = null }", "var _a;\n_a = foo;\nclass Foo {\n constructor() {\n __publicField(this, _a, null);\n }\n}\n") 289 290 expectPrintedTarget(t, 2015, "(class {})", "(class {\n});\n") 291 expectPrintedTarget(t, 2015, "(class { foo })", "(class {\n constructor() {\n __publicField(this, \"foo\");\n }\n});\n") 292 expectPrintedTarget(t, 2015, "(class { foo = null })", "(class {\n constructor() {\n __publicField(this, \"foo\", null);\n }\n});\n") 293 expectPrintedTarget(t, 2015, "(class { 123 })", "(class {\n constructor() {\n __publicField(this, 123);\n }\n});\n") 294 expectPrintedTarget(t, 2015, "(class { 123 = null })", "(class {\n constructor() {\n __publicField(this, 123, null);\n }\n});\n") 295 expectPrintedTarget(t, 2015, "(class { [foo] })", "var _a;\n_a = foo, class {\n constructor() {\n __publicField(this, _a);\n }\n};\n") 296 expectPrintedTarget(t, 2015, "(class { [foo] = null })", "var _a;\n_a = foo, class {\n constructor() {\n __publicField(this, _a, null);\n }\n};\n") 297 298 expectPrintedTarget(t, 2015, "class Foo extends Bar {}", `class Foo extends Bar { 299 } 300 `) 301 expectPrintedTarget(t, 2015, "class Foo extends Bar { bar() {} constructor() { super() } }", `class Foo extends Bar { 302 bar() { 303 } 304 constructor() { 305 super(); 306 } 307 } 308 `) 309 expectPrintedTarget(t, 2015, "class Foo extends Bar { bar() {} foo }", `class Foo extends Bar { 310 constructor() { 311 super(...arguments); 312 __publicField(this, "foo"); 313 } 314 bar() { 315 } 316 } 317 `) 318 expectPrintedTarget(t, 2015, "class Foo extends Bar { bar() {} foo; constructor() { super() } }", `class Foo extends Bar { 319 constructor() { 320 super(); 321 __publicField(this, "foo"); 322 } 323 bar() { 324 } 325 } 326 `) 327 expectPrintedTarget(t, 2015, "class Foo extends Bar { bar() {} foo; constructor({ ...args }) { super() } }", `class Foo extends Bar { 328 constructor(_a) { 329 var args = __objRest(_a, []); 330 super(); 331 __publicField(this, "foo"); 332 } 333 bar() { 334 } 335 } 336 `) 337 } 338 339 func TestLowerClassStatic(t *testing.T) { 340 expectPrintedTarget(t, 2015, "class Foo { static foo }", "class Foo {\n}\n__publicField(Foo, \"foo\");\n") 341 expectPrintedTarget(t, 2015, "class Foo { static foo = null }", "class Foo {\n}\n__publicField(Foo, \"foo\", null);\n") 342 expectPrintedTarget(t, 2015, "class Foo { static foo(a, b) {} }", "class Foo {\n static foo(a, b) {\n }\n}\n") 343 expectPrintedTarget(t, 2015, "class Foo { static get foo() {} }", "class Foo {\n static get foo() {\n }\n}\n") 344 expectPrintedTarget(t, 2015, "class Foo { static set foo(a) {} }", "class Foo {\n static set foo(a) {\n }\n}\n") 345 expectPrintedTarget(t, 2015, "class Foo { static 123 }", "class Foo {\n}\n__publicField(Foo, 123);\n") 346 expectPrintedTarget(t, 2015, "class Foo { static 123 = null }", "class Foo {\n}\n__publicField(Foo, 123, null);\n") 347 expectPrintedTarget(t, 2015, "class Foo { static 123(a, b) {} }", "class Foo {\n static 123(a, b) {\n }\n}\n") 348 expectPrintedTarget(t, 2015, "class Foo { static get 123() {} }", "class Foo {\n static get 123() {\n }\n}\n") 349 expectPrintedTarget(t, 2015, "class Foo { static set 123(a) {} }", "class Foo {\n static set 123(a) {\n }\n}\n") 350 expectPrintedTarget(t, 2015, "class Foo { static [foo] }", "var _a;\n_a = foo;\nclass Foo {\n}\n__publicField(Foo, _a);\n") 351 expectPrintedTarget(t, 2015, "class Foo { static [foo] = null }", "var _a;\n_a = foo;\nclass Foo {\n}\n__publicField(Foo, _a, null);\n") 352 expectPrintedTarget(t, 2015, "class Foo { static [foo](a, b) {} }", "class Foo {\n static [foo](a, b) {\n }\n}\n") 353 expectPrintedTarget(t, 2015, "class Foo { static get [foo]() {} }", "class Foo {\n static get [foo]() {\n }\n}\n") 354 expectPrintedTarget(t, 2015, "class Foo { static set [foo](a) {} }", "class Foo {\n static set [foo](a) {\n }\n}\n") 355 356 expectPrintedTarget(t, 2015, "export default class Foo { static foo }", "export default class Foo {\n}\n__publicField(Foo, \"foo\");\n") 357 expectPrintedTarget(t, 2015, "export default class Foo { static foo = null }", "export default class Foo {\n}\n__publicField(Foo, \"foo\", null);\n") 358 expectPrintedTarget(t, 2015, "export default class Foo { static foo(a, b) {} }", "export default class Foo {\n static foo(a, b) {\n }\n}\n") 359 expectPrintedTarget(t, 2015, "export default class Foo { static get foo() {} }", "export default class Foo {\n static get foo() {\n }\n}\n") 360 expectPrintedTarget(t, 2015, "export default class Foo { static set foo(a) {} }", "export default class Foo {\n static set foo(a) {\n }\n}\n") 361 expectPrintedTarget(t, 2015, "export default class Foo { static 123 }", "export default class Foo {\n}\n__publicField(Foo, 123);\n") 362 expectPrintedTarget(t, 2015, "export default class Foo { static 123 = null }", "export default class Foo {\n}\n__publicField(Foo, 123, null);\n") 363 expectPrintedTarget(t, 2015, "export default class Foo { static 123(a, b) {} }", "export default class Foo {\n static 123(a, b) {\n }\n}\n") 364 expectPrintedTarget(t, 2015, "export default class Foo { static get 123() {} }", "export default class Foo {\n static get 123() {\n }\n}\n") 365 expectPrintedTarget(t, 2015, "export default class Foo { static set 123(a) {} }", "export default class Foo {\n static set 123(a) {\n }\n}\n") 366 expectPrintedTarget(t, 2015, "export default class Foo { static [foo] }", "var _a;\n_a = foo;\nexport default class Foo {\n}\n__publicField(Foo, _a);\n") 367 expectPrintedTarget(t, 2015, "export default class Foo { static [foo] = null }", "var _a;\n_a = foo;\nexport default class Foo {\n}\n__publicField(Foo, _a, null);\n") 368 expectPrintedTarget(t, 2015, "export default class Foo { static [foo](a, b) {} }", "export default class Foo {\n static [foo](a, b) {\n }\n}\n") 369 expectPrintedTarget(t, 2015, "export default class Foo { static get [foo]() {} }", "export default class Foo {\n static get [foo]() {\n }\n}\n") 370 expectPrintedTarget(t, 2015, "export default class Foo { static set [foo](a) {} }", "export default class Foo {\n static set [foo](a) {\n }\n}\n") 371 372 expectPrintedTarget(t, 2015, "export default class { static foo }", 373 "export default class stdin_default {\n}\n__publicField(stdin_default, \"foo\");\n") 374 expectPrintedTarget(t, 2015, "export default class { static foo = null }", 375 "export default class stdin_default {\n}\n__publicField(stdin_default, \"foo\", null);\n") 376 expectPrintedTarget(t, 2015, "export default class { static foo(a, b) {} }", "export default class {\n static foo(a, b) {\n }\n}\n") 377 expectPrintedTarget(t, 2015, "export default class { static get foo() {} }", "export default class {\n static get foo() {\n }\n}\n") 378 expectPrintedTarget(t, 2015, "export default class { static set foo(a) {} }", "export default class {\n static set foo(a) {\n }\n}\n") 379 expectPrintedTarget(t, 2015, "export default class { static 123 }", 380 "export default class stdin_default {\n}\n__publicField(stdin_default, 123);\n") 381 expectPrintedTarget(t, 2015, "export default class { static 123 = null }", 382 "export default class stdin_default {\n}\n__publicField(stdin_default, 123, null);\n") 383 expectPrintedTarget(t, 2015, "export default class { static 123(a, b) {} }", "export default class {\n static 123(a, b) {\n }\n}\n") 384 expectPrintedTarget(t, 2015, "export default class { static get 123() {} }", "export default class {\n static get 123() {\n }\n}\n") 385 expectPrintedTarget(t, 2015, "export default class { static set 123(a) {} }", "export default class {\n static set 123(a) {\n }\n}\n") 386 expectPrintedTarget(t, 2015, "export default class { static [foo] }", 387 "var _a;\n_a = foo;\nexport default class stdin_default {\n}\n__publicField(stdin_default, _a);\n") 388 expectPrintedTarget(t, 2015, "export default class { static [foo] = null }", 389 "var _a;\n_a = foo;\nexport default class stdin_default {\n}\n__publicField(stdin_default, _a, null);\n") 390 expectPrintedTarget(t, 2015, "export default class { static [foo](a, b) {} }", "export default class {\n static [foo](a, b) {\n }\n}\n") 391 expectPrintedTarget(t, 2015, "export default class { static get [foo]() {} }", "export default class {\n static get [foo]() {\n }\n}\n") 392 expectPrintedTarget(t, 2015, "export default class { static set [foo](a) {} }", "export default class {\n static set [foo](a) {\n }\n}\n") 393 394 expectPrintedTarget(t, 2015, "(class Foo { static foo })", "var _a;\n_a = class {\n}, __publicField(_a, \"foo\"), _a;\n") 395 expectPrintedTarget(t, 2015, "(class Foo { static foo = null })", "var _a;\n_a = class {\n}, __publicField(_a, \"foo\", null), _a;\n") 396 expectPrintedTarget(t, 2015, "(class Foo { static foo(a, b) {} })", "(class Foo {\n static foo(a, b) {\n }\n});\n") 397 expectPrintedTarget(t, 2015, "(class Foo { static get foo() {} })", "(class Foo {\n static get foo() {\n }\n});\n") 398 expectPrintedTarget(t, 2015, "(class Foo { static set foo(a) {} })", "(class Foo {\n static set foo(a) {\n }\n});\n") 399 expectPrintedTarget(t, 2015, "(class Foo { static 123 })", "var _a;\n_a = class {\n}, __publicField(_a, 123), _a;\n") 400 expectPrintedTarget(t, 2015, "(class Foo { static 123 = null })", "var _a;\n_a = class {\n}, __publicField(_a, 123, null), _a;\n") 401 expectPrintedTarget(t, 2015, "(class Foo { static 123(a, b) {} })", "(class Foo {\n static 123(a, b) {\n }\n});\n") 402 expectPrintedTarget(t, 2015, "(class Foo { static get 123() {} })", "(class Foo {\n static get 123() {\n }\n});\n") 403 expectPrintedTarget(t, 2015, "(class Foo { static set 123(a) {} })", "(class Foo {\n static set 123(a) {\n }\n});\n") 404 expectPrintedTarget(t, 2015, "(class Foo { static [foo] })", "var _a, _b;\n_a = foo, _b = class {\n}, __publicField(_b, _a), _b;\n") 405 expectPrintedTarget(t, 2015, "(class Foo { static [foo] = null })", "var _a, _b;\n_a = foo, _b = class {\n}, __publicField(_b, _a, null), _b;\n") 406 expectPrintedTarget(t, 2015, "(class Foo { static [foo](a, b) {} })", "(class Foo {\n static [foo](a, b) {\n }\n});\n") 407 expectPrintedTarget(t, 2015, "(class Foo { static get [foo]() {} })", "(class Foo {\n static get [foo]() {\n }\n});\n") 408 expectPrintedTarget(t, 2015, "(class Foo { static set [foo](a) {} })", "(class Foo {\n static set [foo](a) {\n }\n});\n") 409 410 expectPrintedTarget(t, 2015, "(class { static foo })", "var _a;\n_a = class {\n}, __publicField(_a, \"foo\"), _a;\n") 411 expectPrintedTarget(t, 2015, "(class { static foo = null })", "var _a;\n_a = class {\n}, __publicField(_a, \"foo\", null), _a;\n") 412 expectPrintedTarget(t, 2015, "(class { static foo(a, b) {} })", "(class {\n static foo(a, b) {\n }\n});\n") 413 expectPrintedTarget(t, 2015, "(class { static get foo() {} })", "(class {\n static get foo() {\n }\n});\n") 414 expectPrintedTarget(t, 2015, "(class { static set foo(a) {} })", "(class {\n static set foo(a) {\n }\n});\n") 415 expectPrintedTarget(t, 2015, "(class { static 123 })", "var _a;\n_a = class {\n}, __publicField(_a, 123), _a;\n") 416 expectPrintedTarget(t, 2015, "(class { static 123 = null })", "var _a;\n_a = class {\n}, __publicField(_a, 123, null), _a;\n") 417 expectPrintedTarget(t, 2015, "(class { static 123(a, b) {} })", "(class {\n static 123(a, b) {\n }\n});\n") 418 expectPrintedTarget(t, 2015, "(class { static get 123() {} })", "(class {\n static get 123() {\n }\n});\n") 419 expectPrintedTarget(t, 2015, "(class { static set 123(a) {} })", "(class {\n static set 123(a) {\n }\n});\n") 420 expectPrintedTarget(t, 2015, "(class { static [foo] })", "var _a, _b;\n_a = foo, _b = class {\n}, __publicField(_b, _a), _b;\n") 421 expectPrintedTarget(t, 2015, "(class { static [foo] = null })", "var _a, _b;\n_a = foo, _b = class {\n}, __publicField(_b, _a, null), _b;\n") 422 expectPrintedTarget(t, 2015, "(class { static [foo](a, b) {} })", "(class {\n static [foo](a, b) {\n }\n});\n") 423 expectPrintedTarget(t, 2015, "(class { static get [foo]() {} })", "(class {\n static get [foo]() {\n }\n});\n") 424 expectPrintedTarget(t, 2015, "(class { static set [foo](a) {} })", "(class {\n static set [foo](a) {\n }\n});\n") 425 426 expectPrintedTarget(t, 2015, "(class {})", "(class {\n});\n") 427 expectPrintedTarget(t, 2015, "class Foo {}", "class Foo {\n}\n") 428 expectPrintedTarget(t, 2015, "(class Foo {})", "(class Foo {\n});\n") 429 430 // Static field with initializers that access the class expression name must 431 // still work when they are pulled outside of the class body 432 expectPrintedTarget(t, 2015, ` 433 let Bar = class Foo { 434 static foo = 123 435 static bar = Foo.foo 436 } 437 `, `var _a; 438 let Bar = (_a = class { 439 }, __publicField(_a, "foo", 123), __publicField(_a, "bar", _a.foo), _a); 440 `) 441 442 // Generated IIFEs for static class blocks should be appropriately annotated 443 expectPrintedTarget(t, 2015, "class Foo { static { try {} finally { impureCall() } } }", 444 "class Foo {\n}\n(() => {\n try {\n } finally {\n impureCall();\n }\n})();\n") 445 expectPrintedTarget(t, 2015, "(class Foo { static { try {} finally { impureCall() } } })", 446 "var _a;\n_a = class {\n}, (() => {\n try {\n } finally {\n impureCall();\n }\n})(), _a;\n") 447 expectPrintedTarget(t, 2015, "class Foo { static { try {} finally { /* @__PURE__ */ pureCall() } } }", 448 "class Foo {\n}\n/* @__PURE__ */ (() => {\n try {\n } finally {\n /* @__PURE__ */ pureCall();\n }\n})();\n") 449 expectPrintedTarget(t, 2015, "(class Foo { static { try {} finally { /* @__PURE__ */ pureCall() } } })", 450 "var _a;\n_a = class {\n}, /* @__PURE__ */ (() => {\n try {\n } finally {\n /* @__PURE__ */ pureCall();\n }\n})(), _a;\n") 451 } 452 453 func TestLowerClassStaticThis(t *testing.T) { 454 expectPrinted(t, "class Foo { x = this }", "class Foo {\n x = this;\n}\n") 455 expectPrinted(t, "class Foo { static x = this }", "class Foo {\n static x = this;\n}\n") 456 expectPrinted(t, "class Foo { static x = () => this }", "class Foo {\n static x = () => this;\n}\n") 457 expectPrinted(t, "class Foo { static x = function() { return this } }", "class Foo {\n static x = function() {\n return this;\n };\n}\n") 458 expectPrinted(t, "class Foo { static [this.x] }", "class Foo {\n static [this.x];\n}\n") 459 expectPrinted(t, "class Foo { static x = class { y = this } }", "class Foo {\n static x = class {\n y = this;\n };\n}\n") 460 expectPrinted(t, "class Foo { static x = class { [this.y] } }", "class Foo {\n static x = class {\n [this.y];\n };\n}\n") 461 expectPrinted(t, "class Foo { static x = class extends this {} }", "class Foo {\n static x = class extends this {\n };\n}\n") 462 463 expectPrinted(t, "x = class Foo { x = this }", "x = class Foo {\n x = this;\n};\n") 464 expectPrinted(t, "x = class Foo { static x = this }", "x = class Foo {\n static x = this;\n};\n") 465 expectPrinted(t, "x = class Foo { static x = () => this }", "x = class Foo {\n static x = () => this;\n};\n") 466 expectPrinted(t, "x = class Foo { static x = function() { return this } }", "x = class Foo {\n static x = function() {\n return this;\n };\n};\n") 467 expectPrinted(t, "x = class Foo { static [this.x] }", "x = class Foo {\n static [this.x];\n};\n") 468 expectPrinted(t, "x = class Foo { static x = class { y = this } }", "x = class Foo {\n static x = class {\n y = this;\n };\n};\n") 469 expectPrinted(t, "x = class Foo { static x = class { [this.y] } }", "x = class Foo {\n static x = class {\n [this.y];\n };\n};\n") 470 expectPrinted(t, "x = class Foo { static x = class extends this {} }", "x = class Foo {\n static x = class extends this {\n };\n};\n") 471 472 expectPrinted(t, "x = class { x = this }", "x = class {\n x = this;\n};\n") 473 expectPrinted(t, "x = class { static x = this }", "x = class {\n static x = this;\n};\n") 474 expectPrinted(t, "x = class { static x = () => this }", "x = class {\n static x = () => this;\n};\n") 475 expectPrinted(t, "x = class { static x = function() { return this } }", "x = class {\n static x = function() {\n return this;\n };\n};\n") 476 expectPrinted(t, "x = class { static [this.x] }", "x = class {\n static [this.x];\n};\n") 477 expectPrinted(t, "x = class { static x = class { y = this } }", "x = class {\n static x = class {\n y = this;\n };\n};\n") 478 expectPrinted(t, "x = class { static x = class { [this.y] } }", "x = class {\n static x = class {\n [this.y];\n };\n};\n") 479 expectPrinted(t, "x = class { static x = class extends this {} }", "x = class {\n static x = class extends this {\n };\n};\n") 480 481 expectPrintedTarget(t, 2015, "class Foo { x = this }", 482 "class Foo {\n constructor() {\n __publicField(this, \"x\", this);\n }\n}\n") 483 expectPrintedTarget(t, 2015, "class Foo { [this.x] }", 484 "var _a;\n_a = this.x;\nclass Foo {\n constructor() {\n __publicField(this, _a);\n }\n}\n") 485 expectPrintedTarget(t, 2015, "class Foo { static x = this }", 486 "const _Foo = class _Foo {\n};\n__publicField(_Foo, \"x\", _Foo);\nlet Foo = _Foo;\n") 487 expectPrintedTarget(t, 2015, "class Foo { static x = () => this }", 488 "const _Foo = class _Foo {\n};\n__publicField(_Foo, \"x\", () => _Foo);\nlet Foo = _Foo;\n") 489 expectPrintedTarget(t, 2015, "class Foo { static x = function() { return this } }", 490 "class Foo {\n}\n__publicField(Foo, \"x\", function() {\n return this;\n});\n") 491 expectPrintedTarget(t, 2015, "class Foo { static [this.x] }", 492 "var _a;\n_a = this.x;\nclass Foo {\n}\n__publicField(Foo, _a);\n") 493 expectPrintedTarget(t, 2015, "class Foo { static x = class { y = this } }", 494 "class Foo {\n}\n__publicField(Foo, \"x\", class {\n constructor() {\n __publicField(this, \"y\", this);\n }\n});\n") 495 expectPrintedTarget(t, 2015, "class Foo { static x = class { [this.y] } }", 496 "var _a;\nconst _Foo = class _Foo {\n};\n__publicField(_Foo, \"x\", (_a = _Foo.y, class {\n constructor() {\n __publicField(this, _a);\n }\n}));\nlet Foo = _Foo;\n") 497 expectPrintedTarget(t, 2015, "class Foo { static x = class extends this {} }", 498 "const _Foo = class _Foo {\n};\n__publicField(_Foo, \"x\", class extends _Foo {\n});\nlet Foo = _Foo;\n") 499 500 expectPrintedTarget(t, 2015, "x = class Foo { x = this }", 501 "x = class Foo {\n constructor() {\n __publicField(this, \"x\", this);\n }\n};\n") 502 expectPrintedTarget(t, 2015, "x = class Foo { [this.x] }", 503 "var _a;\nx = (_a = this.x, class Foo {\n constructor() {\n __publicField(this, _a);\n }\n});\n") 504 expectPrintedTarget(t, 2015, "x = class Foo { static x = this }", 505 "var _a;\nx = (_a = class {\n}, __publicField(_a, \"x\", _a), _a);\n") 506 expectPrintedTarget(t, 2015, "x = class Foo { static x = () => this }", 507 "var _a;\nx = (_a = class {\n}, __publicField(_a, \"x\", () => _a), _a);\n") 508 expectPrintedTarget(t, 2015, "x = class Foo { static x = function() { return this } }", 509 "var _a;\nx = (_a = class {\n}, __publicField(_a, \"x\", function() {\n return this;\n}), _a);\n") 510 expectPrintedTarget(t, 2015, "x = class Foo { static [this.x] }", 511 "var _a, _b;\nx = (_a = this.x, _b = class {\n}, __publicField(_b, _a), _b);\n") 512 expectPrintedTarget(t, 2015, "x = class Foo { static x = class { y = this } }", 513 "var _a;\nx = (_a = class {\n}, __publicField(_a, \"x\", class {\n constructor() {\n __publicField(this, \"y\", this);\n }\n}), _a);\n") 514 expectPrintedTarget(t, 2015, "x = class Foo { static x = class { [this.y] } }", 515 "var _a, _b;\nx = (_b = class {\n}, __publicField(_b, \"x\", (_a = _b.y, class {\n constructor() {\n __publicField(this, _a);\n }\n})), _b);\n") 516 expectPrintedTarget(t, 2015, "x = class Foo { static x = class extends this {} }", 517 "var _a;\nx = (_a = class {\n}, __publicField(_a, \"x\", class extends _a {\n}), _a);\n") 518 519 expectPrintedTarget(t, 2015, "x = class { x = this }", 520 "x = class {\n constructor() {\n __publicField(this, \"x\", this);\n }\n};\n") 521 expectPrintedTarget(t, 2015, "x = class { [this.x] }", 522 "var _a;\nx = (_a = this.x, class {\n constructor() {\n __publicField(this, _a);\n }\n});\n") 523 expectPrintedTarget(t, 2015, "x = class { static x = this }", 524 "var _a;\nx = (_a = class {\n}, __publicField(_a, \"x\", _a), _a);\n") 525 expectPrintedTarget(t, 2015, "x = class { static x = () => this }", 526 "var _a;\nx = (_a = class {\n}, __publicField(_a, \"x\", () => _a), _a);\n") 527 expectPrintedTarget(t, 2015, "x = class { static x = function() { return this } }", 528 "var _a;\nx = (_a = class {\n}, __publicField(_a, \"x\", function() {\n return this;\n}), _a);\n") 529 expectPrintedTarget(t, 2015, "x = class { static [this.x] }", 530 "var _a, _b;\nx = (_a = this.x, _b = class {\n}, __publicField(_b, _a), _b);\n") 531 expectPrintedTarget(t, 2015, "x = class { static x = class { y = this } }", 532 "var _a;\nx = (_a = class {\n}, __publicField(_a, \"x\", class {\n constructor() {\n __publicField(this, \"y\", this);\n }\n}), _a);\n") 533 expectPrintedTarget(t, 2015, "x = class { static x = class { [this.y] } }", 534 "var _a, _b;\nx = (_b = class {\n}, __publicField(_b, \"x\", (_a = _b.y, class {\n constructor() {\n __publicField(this, _a);\n }\n})), _b);\n") 535 expectPrintedTarget(t, 2015, "x = class Foo { static x = class extends this {} }", 536 "var _a;\nx = (_a = class {\n}, __publicField(_a, \"x\", class extends _a {\n}), _a);\n") 537 } 538 539 func TestLowerClassStaticBlocks(t *testing.T) { 540 expectPrintedTarget(t, 2015, "class Foo { static {} }", "class Foo {\n}\n") 541 expectPrintedTarget(t, 2015, "class Foo { static {} x() {} }", "class Foo {\n x() {\n }\n}\n") 542 expectPrintedTarget(t, 2015, "class Foo { x() {} static {} }", "class Foo {\n x() {\n }\n}\n") 543 expectPrintedTarget(t, 2015, "class Foo { static { x } static {} static { y } }", "class Foo {\n}\nx;\ny;\n") 544 545 expectPrintedMangleTarget(t, 2015, "class Foo { static {} }", "class Foo {\n}\n") 546 expectPrintedMangleTarget(t, 2015, "class Foo { static {} x() {} }", "class Foo {\n x() {\n }\n}\n") 547 expectPrintedMangleTarget(t, 2015, "class Foo { x() {} static {} }", "class Foo {\n x() {\n }\n}\n") 548 expectPrintedMangleTarget(t, 2015, "class Foo { static { x } static {} static { y } }", "class Foo {\n}\nx, y;\n") 549 } 550 551 func TestLowerOptionalChain(t *testing.T) { 552 expectPrintedTarget(t, 2019, "a?.b.c", "a == null ? void 0 : a.b.c;\n") 553 expectPrintedTarget(t, 2019, "(a?.b).c", "(a == null ? void 0 : a.b).c;\n") 554 expectPrintedTarget(t, 2019, "a.b?.c", "var _a;\n(_a = a.b) == null ? void 0 : _a.c;\n") 555 expectPrintedTarget(t, 2019, "this?.x", "this == null ? void 0 : this.x;\n") 556 557 expectPrintedTarget(t, 2019, "a?.[b][c]", "a == null ? void 0 : a[b][c];\n") 558 expectPrintedTarget(t, 2019, "(a?.[b])[c]", "(a == null ? void 0 : a[b])[c];\n") 559 expectPrintedTarget(t, 2019, "a[b]?.[c]", "var _a;\n(_a = a[b]) == null ? void 0 : _a[c];\n") 560 expectPrintedTarget(t, 2019, "this?.[x]", "this == null ? void 0 : this[x];\n") 561 562 expectPrintedTarget(t, 2019, "a?.(b)(c)", "a == null ? void 0 : a(b)(c);\n") 563 expectPrintedTarget(t, 2019, "(a?.(b))(c)", "(a == null ? void 0 : a(b))(c);\n") 564 expectPrintedTarget(t, 2019, "a(b)?.(c)", "var _a;\n(_a = a(b)) == null ? void 0 : _a(c);\n") 565 expectPrintedTarget(t, 2019, "this?.(x)", "this == null ? void 0 : this(x);\n") 566 567 expectPrintedTarget(t, 2019, "delete a?.b.c", "a == null ? true : delete a.b.c;\n") 568 expectPrintedTarget(t, 2019, "delete a?.[b][c]", "a == null ? true : delete a[b][c];\n") 569 expectPrintedTarget(t, 2019, "delete a?.(b)(c)", "a == null ? true : delete a(b)(c);\n") 570 571 expectPrintedTarget(t, 2019, "delete (a?.b).c", "delete (a == null ? void 0 : a.b).c;\n") 572 expectPrintedTarget(t, 2019, "delete (a?.[b])[c]", "delete (a == null ? void 0 : a[b])[c];\n") 573 expectPrintedTarget(t, 2019, "delete (a?.(b))(c)", "delete (a == null ? void 0 : a(b))(c);\n") 574 575 expectPrintedTarget(t, 2019, "(delete a?.b).c", "(a == null ? true : delete a.b).c;\n") 576 expectPrintedTarget(t, 2019, "(delete a?.[b])[c]", "(a == null ? true : delete a[b])[c];\n") 577 expectPrintedTarget(t, 2019, "(delete a?.(b))(c)", "(a == null ? true : delete a(b))(c);\n") 578 579 expectPrintedTarget(t, 2019, "null?.x", "") 580 expectPrintedTarget(t, 2019, "null?.[x]", "") 581 expectPrintedTarget(t, 2019, "null?.(x)", "") 582 583 expectPrintedTarget(t, 2019, "delete null?.x", "") 584 expectPrintedTarget(t, 2019, "delete null?.[x]", "") 585 expectPrintedTarget(t, 2019, "delete null?.(x)", "") 586 587 expectPrintedTarget(t, 2019, "undefined?.x", "") 588 expectPrintedTarget(t, 2019, "undefined?.[x]", "") 589 expectPrintedTarget(t, 2019, "undefined?.(x)", "") 590 591 expectPrintedTarget(t, 2019, "delete undefined?.x", "") 592 expectPrintedTarget(t, 2019, "delete undefined?.[x]", "") 593 expectPrintedTarget(t, 2019, "delete undefined?.(x)", "") 594 595 expectPrintedMangleTarget(t, 2019, "(foo(), null)?.x; y = (bar(), null)?.x", "foo(), y = (bar(), void 0);\n") 596 expectPrintedMangleTarget(t, 2019, "(foo(), null)?.[x]; y = (bar(), null)?.[x]", "foo(), y = (bar(), void 0);\n") 597 expectPrintedMangleTarget(t, 2019, "(foo(), null)?.(x); y = (bar(), null)?.(x)", "foo(), y = (bar(), void 0);\n") 598 599 expectPrintedMangleTarget(t, 2019, "(foo(), void 0)?.x; y = (bar(), void 0)?.x", "foo(), y = (bar(), void 0);\n") 600 expectPrintedMangleTarget(t, 2019, "(foo(), void 0)?.[x]; y = (bar(), void 0)?.[x]", "foo(), y = (bar(), void 0);\n") 601 expectPrintedMangleTarget(t, 2019, "(foo(), void 0)?.(x); y = (bar(), void 0)?.(x)", "foo(), y = (bar(), void 0);\n") 602 603 expectPrintedTarget(t, 2020, "x?.y", "x?.y;\n") 604 expectPrintedTarget(t, 2020, "x?.[y]", "x?.[y];\n") 605 expectPrintedTarget(t, 2020, "x?.(y)", "x?.(y);\n") 606 607 expectPrintedTarget(t, 2020, "null?.x", "") 608 expectPrintedTarget(t, 2020, "null?.[x]", "") 609 expectPrintedTarget(t, 2020, "null?.(x)", "") 610 611 expectPrintedTarget(t, 2020, "undefined?.x", "") 612 expectPrintedTarget(t, 2020, "undefined?.[x]", "") 613 expectPrintedTarget(t, 2020, "undefined?.(x)", "") 614 615 expectPrintedTarget(t, 2020, "(foo(), null)?.x", "(foo(), null)?.x;\n") 616 expectPrintedTarget(t, 2020, "(foo(), null)?.[x]", "(foo(), null)?.[x];\n") 617 expectPrintedTarget(t, 2020, "(foo(), null)?.(x)", "(foo(), null)?.(x);\n") 618 619 expectPrintedTarget(t, 2020, "(foo(), void 0)?.x", "(foo(), void 0)?.x;\n") 620 expectPrintedTarget(t, 2020, "(foo(), void 0)?.[x]", "(foo(), void 0)?.[x];\n") 621 expectPrintedTarget(t, 2020, "(foo(), void 0)?.(x)", "(foo(), void 0)?.(x);\n") 622 623 expectPrintedMangleTarget(t, 2020, "(foo(), null)?.x; y = (bar(), null)?.x", "foo(), y = (bar(), void 0);\n") 624 expectPrintedMangleTarget(t, 2020, "(foo(), null)?.[x]; y = (bar(), null)?.[x]", "foo(), y = (bar(), void 0);\n") 625 expectPrintedMangleTarget(t, 2020, "(foo(), null)?.(x); y = (bar(), null)?.(x)", "foo(), y = (bar(), void 0);\n") 626 627 expectPrintedMangleTarget(t, 2020, "(foo(), void 0)?.x; y = (bar(), void 0)?.x", "foo(), y = (bar(), void 0);\n") 628 expectPrintedMangleTarget(t, 2020, "(foo(), void 0)?.[x]; y = (bar(), void 0)?.[x]", "foo(), y = (bar(), void 0);\n") 629 expectPrintedMangleTarget(t, 2020, "(foo(), void 0)?.(x); y = (bar(), void 0)?.(x)", "foo(), y = (bar(), void 0);\n") 630 631 expectPrintedTarget(t, 2019, "a?.b()", "a == null ? void 0 : a.b();\n") 632 expectPrintedTarget(t, 2019, "a?.[b]()", "a == null ? void 0 : a[b]();\n") 633 expectPrintedTarget(t, 2019, "a?.b.c()", "a == null ? void 0 : a.b.c();\n") 634 expectPrintedTarget(t, 2019, "a?.b[c]()", "a == null ? void 0 : a.b[c]();\n") 635 expectPrintedTarget(t, 2019, "a()?.b()", "var _a;\n(_a = a()) == null ? void 0 : _a.b();\n") 636 expectPrintedTarget(t, 2019, "a()?.[b]()", "var _a;\n(_a = a()) == null ? void 0 : _a[b]();\n") 637 638 expectPrintedTarget(t, 2019, "(a?.b)()", "(a == null ? void 0 : a.b).call(a);\n") 639 expectPrintedTarget(t, 2019, "(a?.[b])()", "(a == null ? void 0 : a[b]).call(a);\n") 640 expectPrintedTarget(t, 2019, "(a?.b.c)()", "var _a;\n(a == null ? void 0 : (_a = a.b).c).call(_a);\n") 641 expectPrintedTarget(t, 2019, "(a?.b[c])()", "var _a;\n(a == null ? void 0 : (_a = a.b)[c]).call(_a);\n") 642 expectPrintedTarget(t, 2019, "(a()?.b)()", "var _a;\n((_a = a()) == null ? void 0 : _a.b).call(_a);\n") 643 expectPrintedTarget(t, 2019, "(a()?.[b])()", "var _a;\n((_a = a()) == null ? void 0 : _a[b]).call(_a);\n") 644 645 // Check multiple levels of nesting 646 expectPrintedTarget(t, 2019, "a?.b?.c?.d", `var _a, _b; 647 (_b = (_a = a == null ? void 0 : a.b) == null ? void 0 : _a.c) == null ? void 0 : _b.d; 648 `) 649 expectPrintedTarget(t, 2019, "a?.[b]?.[c]?.[d]", `var _a, _b; 650 (_b = (_a = a == null ? void 0 : a[b]) == null ? void 0 : _a[c]) == null ? void 0 : _b[d]; 651 `) 652 expectPrintedTarget(t, 2019, "a?.(b)?.(c)?.(d)", `var _a, _b; 653 (_b = (_a = a == null ? void 0 : a(b)) == null ? void 0 : _a(c)) == null ? void 0 : _b(d); 654 `) 655 656 // Check the need to use ".call()" 657 expectPrintedTarget(t, 2019, "a.b?.(c)", `var _a; 658 (_a = a.b) == null ? void 0 : _a.call(a, c); 659 `) 660 expectPrintedTarget(t, 2019, "a[b]?.(c)", `var _a; 661 (_a = a[b]) == null ? void 0 : _a.call(a, c); 662 `) 663 expectPrintedTarget(t, 2019, "a?.[b]?.(c)", `var _a; 664 (_a = a == null ? void 0 : a[b]) == null ? void 0 : _a.call(a, c); 665 `) 666 expectPrintedTarget(t, 2019, "a?.[b]?.(c).d", `var _a; 667 (_a = a == null ? void 0 : a[b]) == null ? void 0 : _a.call(a, c).d; 668 `) 669 expectPrintedTarget(t, 2019, "a?.[b]?.(c).d()", `var _a; 670 (_a = a == null ? void 0 : a[b]) == null ? void 0 : _a.call(a, c).d(); 671 `) 672 expectPrintedTarget(t, 2019, "a?.[b]?.(c)['d']", `var _a; 673 (_a = a == null ? void 0 : a[b]) == null ? void 0 : _a.call(a, c)["d"]; 674 `) 675 expectPrintedTarget(t, 2019, "a?.[b]?.(c)['d']()", `var _a; 676 (_a = a == null ? void 0 : a[b]) == null ? void 0 : _a.call(a, c)["d"](); 677 `) 678 expectPrintedTarget(t, 2019, "a?.[b]?.(c).d['e'](f)['g'].h(i)", `var _a; 679 (_a = a == null ? void 0 : a[b]) == null ? void 0 : _a.call(a, c).d["e"](f)["g"].h(i); 680 `) 681 expectPrintedTarget(t, 2019, "123?.[b]?.(c)", `var _a; 682 (_a = 123 == null ? void 0 : 123[b]) == null ? void 0 : _a.call(123, c); 683 `) 684 expectPrintedTarget(t, 2019, "a?.[b][c]?.(d)", `var _a, _b; 685 (_b = a == null ? void 0 : (_a = a[b])[c]) == null ? void 0 : _b.call(_a, d); 686 `) 687 expectPrintedTarget(t, 2019, "a[b][c]?.(d)", `var _a, _b; 688 (_b = (_a = a[b])[c]) == null ? void 0 : _b.call(_a, d); 689 `) 690 691 // Check that direct eval status is not propagated through optional chaining 692 expectPrintedTarget(t, 2019, "eval?.(x)", "eval == null ? void 0 : (0, eval)(x);\n") 693 expectPrintedMangleTarget(t, 2019, "(1 ? eval : 0)?.(x)", "eval == null || (0, eval)(x);\n") 694 695 // Check super property access 696 expectPrintedTarget(t, 2019, "class Foo extends Bar { foo() { super.bar?.() } }", `class Foo extends Bar { 697 foo() { 698 var _a; 699 (_a = super.bar) == null ? void 0 : _a.call(this); 700 } 701 } 702 `) 703 expectPrintedTarget(t, 2019, "class Foo extends Bar { foo() { super['bar']?.() } }", `class Foo extends Bar { 704 foo() { 705 var _a; 706 (_a = super["bar"]) == null ? void 0 : _a.call(this); 707 } 708 } 709 `) 710 711 expectPrintedTarget(t, 2020, "(x?.y)``", "(x?.y)``;\n") 712 expectPrintedTarget(t, 2019, "(x?.y)``", "var _a;\n(x == null ? void 0 : x.y).call(x, _a || (_a = __template([\"\"])));\n") 713 expectPrintedTarget(t, 5, "(x?.y)``", "var _a;\n(x == null ? void 0 : x.y).call(x, _a || (_a = __template([\"\"])));\n") 714 715 // Temporary variables should not come before "use strict" 716 expectPrintedTarget(t, 2019, "function f() { /*! @license */ 'use strict'; a.b?.c() }", 717 "function f() {\n /*! @license */\n \"use strict\";\n var _a;\n (_a = a.b) == null ? void 0 : _a.c();\n}\n") 718 } 719 720 func TestLowerOptionalCatchBinding(t *testing.T) { 721 expectPrintedTarget(t, 2019, "try {} catch {}", "try {\n} catch {\n}\n") 722 expectPrintedTarget(t, 2018, "try {} catch {}", "try {\n} catch (e) {\n}\n") 723 } 724 725 func TestLowerExportStarAs(t *testing.T) { 726 expectPrintedTarget(t, 2020, "export * as ns from 'path'", "export * as ns from \"path\";\n") 727 expectPrintedTarget(t, 2019, "export * as ns from 'path'", "import * as ns from \"path\";\nexport { ns };\n") 728 } 729 730 func TestAsyncGeneratorFns(t *testing.T) { 731 err := "" 732 expectParseErrorWithUnsupportedFeatures(t, compat.AsyncAwait, "async function gen() {}", err) 733 expectParseErrorWithUnsupportedFeatures(t, compat.AsyncAwait, "(async function () {});", err) 734 expectParseErrorWithUnsupportedFeatures(t, compat.AsyncAwait, "({ async foo() {} });", err) 735 736 err = "<stdin>: ERROR: Transforming generator functions to the configured target environment is not supported yet\n" 737 expectParseErrorWithUnsupportedFeatures(t, compat.Generator, "function* gen() {}", err) 738 expectParseErrorWithUnsupportedFeatures(t, compat.Generator, "(function* () {});", err) 739 expectParseErrorWithUnsupportedFeatures(t, compat.Generator, "({ *foo() {} });", err) 740 741 err = "<stdin>: ERROR: Transforming async functions to the configured target environment is not supported yet\n" 742 expectParseErrorWithUnsupportedFeatures(t, compat.AsyncAwait|compat.Generator, "async function gen() {}", err) 743 expectParseErrorWithUnsupportedFeatures(t, compat.AsyncAwait|compat.Generator, "(async function () {});", err) 744 expectParseErrorWithUnsupportedFeatures(t, compat.AsyncAwait|compat.Generator, "({ async foo() {} });", err) 745 746 err = "" 747 expectParseErrorWithUnsupportedFeatures(t, compat.AsyncGenerator, "async function* gen() {}", err) 748 expectParseErrorWithUnsupportedFeatures(t, compat.AsyncGenerator, "(async function* () {});", err) 749 expectParseErrorWithUnsupportedFeatures(t, compat.AsyncGenerator, "({ async *foo() {} });", err) 750 } 751 752 func TestForAwait(t *testing.T) { 753 err := "" 754 expectParseErrorWithUnsupportedFeatures(t, compat.AsyncAwait, "async function gen() { for await (x of y) ; }", err) 755 expectParseErrorWithUnsupportedFeatures(t, compat.Generator, "async function gen() { for await (x of y) ; }", err) 756 757 // This is ok because for-await can be lowered to await 758 expectParseErrorWithUnsupportedFeatures(t, compat.ForAwait|compat.Generator, "async function gen() { for await (x of y) ; }", err) 759 760 // This is ok because for-await can be lowered to yield 761 expectParseErrorWithUnsupportedFeatures(t, compat.ForAwait|compat.AsyncAwait, "async function gen() { for await (x of y) ; }", err) 762 763 // This is not ok because for-await can't be lowered 764 err = 765 "<stdin>: ERROR: Transforming async functions to the configured target environment is not supported yet\n" + 766 "<stdin>: ERROR: Transforming for-await loops to the configured target environment is not supported yet\n" 767 expectParseErrorWithUnsupportedFeatures(t, compat.ForAwait|compat.AsyncAwait|compat.Generator, "async function gen() { for await (x of y) ; }", err) 768 769 // Can't use for-await at the top-level without top-level await 770 err = "<stdin>: ERROR: Top-level await is not available in the configured target environment\n" 771 expectParseErrorWithUnsupportedFeatures(t, compat.TopLevelAwait, "for await (x of y) ;", err) 772 expectParseErrorWithUnsupportedFeatures(t, compat.TopLevelAwait, "if (true) for await (x of y) ;", err) 773 expectPrintedWithUnsupportedFeatures(t, compat.TopLevelAwait, "if (false) for await (x of y) ;", "if (false) for (x of y) ;\n") 774 expectParseErrorWithUnsupportedFeatures(t, compat.TopLevelAwait, "with (x) y; if (false) for await (x of y) ;", 775 "<stdin>: ERROR: With statements cannot be used in an ECMAScript module\n"+ 776 "<stdin>: NOTE: This file is considered to be an ECMAScript module because of the top-level \"await\" keyword here:\n") 777 } 778 779 func TestLowerAutoAccessors(t *testing.T) { 780 expectPrintedWithUnsupportedFeatures(t, compat.Decorators, "class Foo { accessor x }", 781 "class Foo {\n #x;\n get x() {\n return this.#x;\n }\n set x(_) {\n this.#x = _;\n }\n}\n") 782 expectPrintedWithUnsupportedFeatures(t, compat.Decorators, "class Foo { accessor [x] }", 783 "var _a;\nclass Foo {\n #a;\n get [_a = x]() {\n return this.#a;\n }\n set [_a](_) {\n this.#a = _;\n }\n}\n") 784 expectPrintedWithUnsupportedFeatures(t, compat.Decorators, "class Foo { accessor x = null }", 785 "class Foo {\n #x = null;\n get x() {\n return this.#x;\n }\n set x(_) {\n this.#x = _;\n }\n}\n") 786 expectPrintedWithUnsupportedFeatures(t, compat.Decorators, "class Foo { accessor [x] = null }", 787 "var _a;\nclass Foo {\n #a = null;\n get [_a = x]() {\n return this.#a;\n }\n set [_a](_) {\n this.#a = _;\n }\n}\n") 788 789 expectPrintedWithUnsupportedFeatures(t, compat.Decorators, "class Foo { static accessor x }", 790 "class Foo {\n static #x;\n static get x() {\n return this.#x;\n }\n static set x(_) {\n this.#x = _;\n }\n}\n") 791 expectPrintedWithUnsupportedFeatures(t, compat.Decorators, "class Foo { static accessor [x] }", 792 "var _a;\nclass Foo {\n static #a;\n static get [_a = x]() {\n return this.#a;\n }\n static set [_a](_) {\n this.#a = _;\n }\n}\n") 793 expectPrintedWithUnsupportedFeatures(t, compat.Decorators, "class Foo { static accessor x = null }", 794 "class Foo {\n static #x = null;\n static get x() {\n return this.#x;\n }\n static set x(_) {\n this.#x = _;\n }\n}\n") 795 expectPrintedWithUnsupportedFeatures(t, compat.Decorators, "class Foo { static accessor [x] = null }", 796 "var _a;\nclass Foo {\n static #a = null;\n static get [_a = x]() {\n return this.#a;\n }\n static set [_a](_) {\n this.#a = _;\n }\n}\n") 797 798 // Test various combinations of flags 799 expectPrintedWithUnsupportedFeatures(t, compat.Decorators|compat.ClassPrivateField, "class Foo { accessor x = null }", 800 `var _x; 801 class Foo { 802 constructor() { 803 __privateAdd(this, _x, null); 804 } 805 get x() { 806 return __privateGet(this, _x); 807 } 808 set x(_) { 809 __privateSet(this, _x, _); 810 } 811 } 812 _x = new WeakMap(); 813 `) 814 expectPrintedWithUnsupportedFeatures(t, compat.Decorators|compat.ClassPrivateStaticField, "class Foo { static accessor x = null }", 815 `var _x; 816 class Foo { 817 static get x() { 818 return __privateGet(this, _x); 819 } 820 static set x(_) { 821 __privateSet(this, _x, _); 822 } 823 } 824 _x = new WeakMap(); 825 __privateAdd(Foo, _x, null); 826 `) 827 expectPrintedWithUnsupportedFeatures(t, compat.Decorators|compat.ClassField|compat.ClassPrivateField, "class Foo { accessor x = null }", 828 `var _x; 829 class Foo { 830 constructor() { 831 __privateAdd(this, _x, null); 832 } 833 get x() { 834 return __privateGet(this, _x); 835 } 836 set x(_) { 837 __privateSet(this, _x, _); 838 } 839 } 840 _x = new WeakMap(); 841 `) 842 expectPrintedWithUnsupportedFeatures(t, compat.Decorators|compat.ClassStaticField|compat.ClassPrivateStaticField, "class Foo { static accessor x = null }", 843 `var _x; 844 class Foo { 845 static get x() { 846 return __privateGet(this, _x); 847 } 848 static set x(_) { 849 __privateSet(this, _x, _); 850 } 851 } 852 _x = new WeakMap(); 853 __privateAdd(Foo, _x, null); 854 `) 855 expectPrintedWithUnsupportedFeatures(t, compat.Decorators|compat.ClassField|compat.ClassPrivateField, "class Foo { accessor x = 1; static accessor y = 2 }", 856 `var _x, _y; 857 class Foo { 858 constructor() { 859 __privateAdd(this, _x, 1); 860 } 861 get x() { 862 return __privateGet(this, _x); 863 } 864 set x(_) { 865 __privateSet(this, _x, _); 866 } 867 static get y() { 868 return __privateGet(this, _y); 869 } 870 static set y(_) { 871 __privateSet(this, _y, _); 872 } 873 } 874 _x = new WeakMap(); 875 _y = new WeakMap(); 876 __privateAdd(Foo, _y, 2); 877 `) 878 expectPrintedWithUnsupportedFeatures(t, compat.Decorators|compat.ClassStaticField|compat.ClassPrivateStaticField, "class Foo { accessor x = 1; static accessor y = 2 }", 879 `var _y; 880 class Foo { 881 #x = 1; 882 get x() { 883 return this.#x; 884 } 885 set x(_) { 886 this.#x = _; 887 } 888 static get y() { 889 return __privateGet(this, _y); 890 } 891 static set y(_) { 892 __privateSet(this, _y, _); 893 } 894 } 895 _y = new WeakMap(); 896 __privateAdd(Foo, _y, 2); 897 `) 898 }