github.com/evanw/esbuild@v0.21.4/internal/js_parser/ts_parser_test.go (about) 1 package js_parser 2 3 import ( 4 "testing" 5 6 "github.com/evanw/esbuild/internal/compat" 7 "github.com/evanw/esbuild/internal/config" 8 ) 9 10 func expectParseErrorTS(t *testing.T, contents string, expected string) { 11 t.Helper() 12 expectParseErrorCommon(t, contents, expected, config.Options{ 13 TS: config.TSOptions{ 14 Parse: true, 15 }, 16 }) 17 } 18 19 func expectParseErrorExperimentalDecoratorTS(t *testing.T, contents string, expected string) { 20 t.Helper() 21 expectParseErrorCommon(t, contents, expected, config.Options{ 22 TS: config.TSOptions{ 23 Parse: true, 24 Config: config.TSConfig{ 25 ExperimentalDecorators: config.True, 26 }, 27 }, 28 }) 29 } 30 31 func expectPrintedWithUnsupportedFeaturesTS(t *testing.T, unsupportedJSFeatures compat.JSFeature, contents string, expected string) { 32 t.Helper() 33 expectPrintedCommon(t, contents, expected, config.Options{ 34 TS: config.TSOptions{ 35 Parse: true, 36 }, 37 UnsupportedJSFeatures: unsupportedJSFeatures, 38 }) 39 } 40 41 func expectParseErrorTargetTS(t *testing.T, esVersion int, contents string, expected string) { 42 t.Helper() 43 expectParseErrorCommon(t, contents, expected, config.Options{ 44 TS: config.TSOptions{ 45 Parse: true, 46 }, 47 UnsupportedJSFeatures: compat.UnsupportedJSFeatures(map[compat.Engine]compat.Semver{ 48 compat.ES: {Parts: []int{esVersion}}, 49 }), 50 }) 51 } 52 53 func expectPrintedTS(t *testing.T, contents string, expected string) { 54 t.Helper() 55 expectPrintedCommon(t, contents, expected, config.Options{ 56 TS: config.TSOptions{ 57 Parse: true, 58 }, 59 }) 60 } 61 62 func expectPrintedAssignSemanticsTS(t *testing.T, contents string, expected string) { 63 t.Helper() 64 expectPrintedCommon(t, contents, expected, config.Options{ 65 TS: config.TSOptions{ 66 Parse: true, 67 Config: config.TSConfig{ 68 UseDefineForClassFields: config.False, 69 }, 70 }, 71 }) 72 } 73 74 func expectPrintedAssignSemanticsTargetTS(t *testing.T, esVersion int, contents string, expected string) { 75 t.Helper() 76 expectPrintedCommon(t, contents, expected, config.Options{ 77 TS: config.TSOptions{ 78 Parse: true, 79 Config: config.TSConfig{ 80 UseDefineForClassFields: config.False, 81 }, 82 }, 83 UnsupportedJSFeatures: compat.UnsupportedJSFeatures(map[compat.Engine]compat.Semver{ 84 compat.ES: {Parts: []int{esVersion}}, 85 }), 86 }) 87 } 88 89 func expectPrintedExperimentalDecoratorTS(t *testing.T, contents string, expected string) { 90 t.Helper() 91 expectPrintedCommon(t, contents, expected, config.Options{ 92 TS: config.TSOptions{ 93 Parse: true, 94 Config: config.TSConfig{ 95 ExperimentalDecorators: config.True, 96 }, 97 }, 98 }) 99 } 100 101 func expectPrintedMangleTS(t *testing.T, contents string, expected string) { 102 t.Helper() 103 expectPrintedCommon(t, contents, expected, config.Options{ 104 TS: config.TSOptions{ 105 Parse: true, 106 }, 107 MinifySyntax: true, 108 }) 109 } 110 111 func expectPrintedMangleAssignSemanticsTS(t *testing.T, contents string, expected string) { 112 t.Helper() 113 expectPrintedCommon(t, contents, expected, config.Options{ 114 TS: config.TSOptions{ 115 Parse: true, 116 Config: config.TSConfig{ 117 UseDefineForClassFields: config.False, 118 }, 119 }, 120 MinifySyntax: true, 121 }) 122 } 123 124 func expectPrintedTargetTS(t *testing.T, esVersion int, contents string, expected string) { 125 t.Helper() 126 expectPrintedCommon(t, contents, expected, config.Options{ 127 TS: config.TSOptions{ 128 Parse: true, 129 }, 130 UnsupportedJSFeatures: compat.UnsupportedJSFeatures(map[compat.Engine]compat.Semver{ 131 compat.ES: {Parts: []int{esVersion}}, 132 }), 133 }) 134 } 135 136 func expectPrintedTargetExperimentalDecoratorTS(t *testing.T, esVersion int, contents string, expected string) { 137 t.Helper() 138 expectPrintedCommon(t, contents, expected, config.Options{ 139 TS: config.TSOptions{ 140 Parse: true, 141 Config: config.TSConfig{ 142 ExperimentalDecorators: config.True, 143 }, 144 }, 145 UnsupportedJSFeatures: compat.UnsupportedJSFeatures(map[compat.Engine]compat.Semver{ 146 compat.ES: {Parts: []int{esVersion}}, 147 }), 148 }) 149 } 150 151 func expectParseErrorTSNoAmbiguousLessThan(t *testing.T, contents string, expected string) { 152 t.Helper() 153 expectParseErrorCommon(t, contents, expected, config.Options{ 154 TS: config.TSOptions{ 155 Parse: true, 156 NoAmbiguousLessThan: true, 157 }, 158 }) 159 } 160 161 func expectPrintedTSNoAmbiguousLessThan(t *testing.T, contents string, expected string) { 162 t.Helper() 163 expectPrintedCommon(t, contents, expected, config.Options{ 164 TS: config.TSOptions{ 165 Parse: true, 166 NoAmbiguousLessThan: true, 167 }, 168 }) 169 } 170 171 func expectParseErrorTSX(t *testing.T, contents string, expected string) { 172 t.Helper() 173 expectParseErrorCommon(t, contents, expected, config.Options{ 174 TS: config.TSOptions{ 175 Parse: true, 176 }, 177 JSX: config.JSXOptions{ 178 Parse: true, 179 }, 180 }) 181 } 182 183 func expectPrintedTSX(t *testing.T, contents string, expected string) { 184 t.Helper() 185 expectPrintedCommon(t, contents, expected, config.Options{ 186 TS: config.TSOptions{ 187 Parse: true, 188 }, 189 JSX: config.JSXOptions{ 190 Parse: true, 191 }, 192 }) 193 } 194 195 func TestTSTypes(t *testing.T) { 196 expectPrintedTS(t, "let x: T extends number\n ? T\n : number", "let x;\n") 197 expectPrintedTS(t, "let x: {y: T extends number ? T : number}", "let x;\n") 198 expectPrintedTS(t, "let x: {y: T \n extends: number}", "let x;\n") 199 expectPrintedTS(t, "let x: {y: T \n extends?: number}", "let x;\n") 200 expectPrintedTS(t, "let x: (number | string)[]", "let x;\n") 201 expectPrintedTS(t, "let x: [string[]?]", "let x;\n") 202 expectPrintedTS(t, "let x: [number?, string?]", "let x;\n") 203 expectPrintedTS(t, "let x: [a: number, b?: string, ...c: number[]]", "let x;\n") 204 expectPrintedTS(t, "type x =\n A\n | B\n C", "C;\n") 205 expectPrintedTS(t, "type x =\n | A\n | B\n C", "C;\n") 206 expectPrintedTS(t, "type x =\n A\n & B\n C", "C;\n") 207 expectPrintedTS(t, "type x =\n & A\n & B\n C", "C;\n") 208 expectPrintedTS(t, "type x = [-1, 0, 1]\n[]", "[];\n") 209 expectPrintedTS(t, "type x = [-1n, 0n, 1n]\n[]", "[];\n") 210 expectPrintedTS(t, "type x = {0: number, readonly 1: boolean}\n[]", "[];\n") 211 expectPrintedTS(t, "type x = {'a': number, readonly 'b': boolean}\n[]", "[];\n") 212 expectPrintedTS(t, "type\nFoo = {}", "type;\nFoo = {};\n") 213 expectPrintedTS(t, "export type\n{ Foo } \n x", "x;\n") 214 expectPrintedTS(t, "export type\n* from 'foo' \n x", "x;\n") 215 expectPrintedTS(t, "export type\n* as ns from 'foo' \n x", "x;\n") 216 expectParseErrorTS(t, "export type\nFoo = {}", "<stdin>: ERROR: Unexpected newline after \"type\"\n") 217 expectPrintedTS(t, "let x: {x: 'a', y: false, z: null}", "let x;\n") 218 expectPrintedTS(t, "let x: {foo(): void}", "let x;\n") 219 expectPrintedTS(t, "let x: {['x']: number}", "let x;\n") 220 expectPrintedTS(t, "let x: {['x'](): void}", "let x;\n") 221 expectPrintedTS(t, "let x: {[key: string]: number}", "let x;\n") 222 expectPrintedTS(t, "let x: {[keyof: string]: number}", "let x;\n") 223 expectPrintedTS(t, "let x: {[readonly: string]: number}", "let x;\n") 224 expectPrintedTS(t, "let x: {[infer: string]: number}", "let x;\n") 225 expectPrintedTS(t, "let x: [keyof: string]", "let x;\n") 226 expectPrintedTS(t, "let x: [readonly: string]", "let x;\n") 227 expectPrintedTS(t, "let x: [infer: string]", "let x;\n") 228 expectParseErrorTS(t, "let x: A extends B ? keyof : string", "<stdin>: ERROR: Unexpected \":\"\n") 229 expectParseErrorTS(t, "let x: A extends B ? readonly : string", "<stdin>: ERROR: Unexpected \":\"\n") 230 expectParseErrorTS(t, "let x: A extends B ? infer : string", "<stdin>: ERROR: Expected identifier but found \":\"\n") 231 expectParseErrorTS(t, "let x: {[new: string]: number}", "<stdin>: ERROR: Expected \"(\" but found \":\"\n") 232 expectParseErrorTS(t, "let x: {[import: string]: number}", "<stdin>: ERROR: Expected \"(\" but found \":\"\n") 233 expectParseErrorTS(t, "let x: {[typeof: string]: number}", "<stdin>: ERROR: Expected identifier but found \":\"\n") 234 expectPrintedTS(t, "let x: () => void = Foo", "let x = Foo;\n") 235 expectPrintedTS(t, "let x: new () => void = Foo", "let x = Foo;\n") 236 expectPrintedTS(t, "let x = 'x' as keyof T", "let x = \"x\";\n") 237 expectPrintedTS(t, "let x = [1] as readonly [number]", "let x = [1];\n") 238 expectPrintedTS(t, "let x = 'x' as keyof typeof Foo", "let x = \"x\";\n") 239 expectPrintedTS(t, "let fs: typeof import('fs') = require('fs')", "let fs = require(\"fs\");\n") 240 expectPrintedTS(t, "let fs: typeof import('fs').exists = require('fs').exists", "let fs = require(\"fs\").exists;\n") 241 expectPrintedTS(t, "let fs: typeof import('fs', { assert: { type: 'json' } }) = require('fs')", "let fs = require(\"fs\");\n") 242 expectPrintedTS(t, "let fs: typeof import('fs', { assert: { 'resolution-mode': 'import' } }) = require('fs')", "let fs = require(\"fs\");\n") 243 expectPrintedTS(t, "let x: <T>() => Foo<T>", "let x;\n") 244 expectPrintedTS(t, "let x: new <T>() => Foo<T>", "let x;\n") 245 expectPrintedTS(t, "let x: <T extends object>() => Foo<T>", "let x;\n") 246 expectPrintedTS(t, "let x: new <T extends object>() => Foo<T>", "let x;\n") 247 expectPrintedTS(t, "type Foo<T> = {[P in keyof T]?: T[P]}", "") 248 expectPrintedTS(t, "type Foo<T> = {[P in keyof T]+?: T[P]}", "") 249 expectPrintedTS(t, "type Foo<T> = {[P in keyof T]-?: T[P]}", "") 250 expectPrintedTS(t, "type Foo<T> = {readonly [P in keyof T]: T[P]}", "") 251 expectPrintedTS(t, "type Foo<T> = {-readonly [P in keyof T]: T[P]}", "") 252 expectPrintedTS(t, "type Foo<T> = {+readonly [P in keyof T]: T[P]}", "") 253 expectPrintedTS(t, "type Foo<T> = {[infer in T]?: Foo}", "") 254 expectPrintedTS(t, "type Foo<T> = {[keyof in T]?: Foo}", "") 255 expectPrintedTS(t, "type Foo<T> = {[asserts in T]?: Foo}", "") 256 expectPrintedTS(t, "type Foo<T> = {[abstract in T]?: Foo}", "") 257 expectPrintedTS(t, "type Foo<T> = {[readonly in T]?: Foo}", "") 258 expectPrintedTS(t, "type Foo<T> = {[satisfies in T]?: Foo}", "") 259 expectPrintedTS(t, "let x: number! = y", "let x = y;\n") 260 expectPrintedTS(t, "let x: number \n !y", "let x;\n!y;\n") 261 expectPrintedTS(t, "const x: unique = y", "const x = y;\n") 262 expectPrintedTS(t, "const x: unique<T> = y", "const x = y;\n") 263 expectPrintedTS(t, "const x: unique\nsymbol = y", "const x = y;\n") 264 expectPrintedTS(t, "let x: typeof a = y", "let x = y;\n") 265 expectPrintedTS(t, "let x: typeof a.b = y", "let x = y;\n") 266 expectPrintedTS(t, "let x: typeof a.if = y", "let x = y;\n") 267 expectPrintedTS(t, "let x: typeof if.a = y", "let x = y;\n") 268 expectPrintedTS(t, "let x: typeof readonly = y", "let x = y;\n") 269 expectParseErrorTS(t, "let x: typeof readonly Array", "<stdin>: ERROR: Expected \";\" but found \"Array\"\n") 270 expectPrintedTS(t, "let x: `y`", "let x;\n") 271 expectParseErrorTS(t, "let x: tag`y`", "<stdin>: ERROR: Expected \";\" but found \"`y`\"\n") 272 expectPrintedTS(t, "let x: { <A extends B>(): c.d \n <E extends F>(): g.h }", "let x;\n") 273 expectPrintedTSX(t, "type x = a.b \n <c></c>", "/* @__PURE__ */ React.createElement(\"c\", null);\n") 274 expectPrintedTS(t, "type Foo = a.b \n | c.d", "") 275 expectPrintedTS(t, "type Foo = a.b \n & c.d", "") 276 expectPrintedTS(t, "type Foo = \n | a.b \n | c.d", "") 277 expectPrintedTS(t, "type Foo = \n & a.b \n & c.d", "") 278 expectPrintedTS(t, "type Foo = Bar extends [infer T] ? T : null", "") 279 expectPrintedTS(t, "type Foo = Bar extends [infer T extends string] ? T : null", "") 280 expectPrintedTS(t, "type Foo = {} extends infer T extends {} ? A<T> : never", "") 281 expectPrintedTS(t, "type Foo = {} extends (infer T extends {}) ? A<T> : never", "") 282 expectPrintedTS(t, "type Foo<T> = T extends { a: infer U extends number } | { b: infer U extends number } ? U : never", "") 283 expectPrintedTS(t, "type Foo<T> = T extends { a: infer U extends number } & { b: infer U extends number } ? U : never", "") 284 expectPrintedTS(t, "type Foo<T> = T extends { a: infer U extends number } | infer U extends number ? U : never", "") 285 expectPrintedTS(t, "type Foo<T> = T extends { a: infer U extends number } & infer U extends number ? U : never", "") 286 expectPrintedTS(t, "let x: A extends B<infer C extends D> ? D : never", "let x;\n") 287 expectPrintedTS(t, "let x: A extends B<infer C extends D ? infer C : never> ? D : never", "let x;\n") 288 expectPrintedTS(t, "let x: ([e1, e2, ...es]: any) => any", "let x;\n") 289 expectPrintedTS(t, "let x: (...[e1, e2, es]: any) => any", "let x;\n") 290 expectPrintedTS(t, "let x: (...[e1, e2, ...es]: any) => any", "let x;\n") 291 expectPrintedTS(t, "let x: (y, [e1, e2, ...es]: any) => any", "let x;\n") 292 expectPrintedTS(t, "let x: (y, ...[e1, e2, es]: any) => any", "let x;\n") 293 expectPrintedTS(t, "let x: (y, ...[e1, e2, ...es]: any) => any", "let x;\n") 294 295 expectPrintedTS(t, "let x: A.B<X.Y>", "let x;\n") 296 expectPrintedTS(t, "let x: A.B<X.Y>=2", "let x = 2;\n") 297 expectPrintedTS(t, "let x: A.B<X.Y<Z>>", "let x;\n") 298 expectPrintedTS(t, "let x: A.B<X.Y<Z>>=2", "let x = 2;\n") 299 expectPrintedTS(t, "let x: A.B<X.Y<Z<T>>>", "let x;\n") 300 expectPrintedTS(t, "let x: A.B<X.Y<Z<T>>>=2", "let x = 2;\n") 301 302 expectPrintedTS(t, "(): A<T>=> 0", "() => 0;\n") 303 expectPrintedTS(t, "(): A<B<T>>=> 0", "() => 0;\n") 304 expectPrintedTS(t, "(): A<B<C<T>>>=> 0", "() => 0;\n") 305 306 expectPrintedTS(t, "let foo: any\n<x>y", "let foo;\ny;\n") 307 expectPrintedTSX(t, "let foo: any\n<x>y</x>", "let foo;\n/* @__PURE__ */ React.createElement(\"x\", null, \"y\");\n") 308 expectParseErrorTS(t, "let foo: (any\n<x>y)", "<stdin>: ERROR: Expected \")\" but found \"<\"\n") 309 310 expectPrintedTS(t, "let foo = bar as (null)", "let foo = bar;\n") 311 expectPrintedTS(t, "let foo = bar\nas (null)", "let foo = bar;\nas(null);\n") 312 expectParseErrorTS(t, "let foo = (bar\nas (null))", "<stdin>: ERROR: Expected \")\" but found \"as\"\n") 313 314 expectPrintedTS(t, "a as any ? b : c;", "a ? b : c;\n") 315 expectPrintedTS(t, "a as any ? async () => b : c;", "a ? async () => b : c;\n") 316 expectPrintedTS(t, "foo as number extends Object ? any : any;", "foo;\n") 317 expectPrintedTS(t, "foo as number extends Object ? () => void : any;", "foo;\n") 318 expectPrintedTS(t, "let a = b ? c : d as T extends T ? T extends T ? T : never : never ? e : f;", "let a = b ? c : d ? e : f;\n") 319 expectParseErrorTS(t, "type a = b extends c", "<stdin>: ERROR: Expected \"?\" but found end of file\n") 320 expectParseErrorTS(t, "type a = b extends c extends d", "<stdin>: ERROR: Expected \"?\" but found \"extends\"\n") 321 expectParseErrorTS(t, "type a = b ? c : d", "<stdin>: ERROR: Expected \";\" but found \"?\"\n") 322 323 expectPrintedTS(t, "let foo: keyof Object = 'toString'", "let foo = \"toString\";\n") 324 expectPrintedTS(t, "let foo: keyof\nObject = 'toString'", "let foo = \"toString\";\n") 325 expectPrintedTS(t, "let foo: (keyof\nObject) = 'toString'", "let foo = \"toString\";\n") 326 327 expectPrintedTS(t, "type Foo = Array<<T>(x: T) => T>\n x", "x;\n") 328 expectPrintedTSX(t, "<Foo<<T>(x: T) => T>/>", "/* @__PURE__ */ React.createElement(Foo, null);\n") 329 330 expectPrintedTS(t, "interface Foo<> {}", "") 331 expectPrintedTSX(t, "interface Foo<> {}", "") 332 expectPrintedTS(t, "type Foo<> = {}", "") 333 expectPrintedTSX(t, "type Foo<> = {}", "") 334 expectParseErrorTS(t, "class Foo<> {}", "<stdin>: ERROR: Expected identifier but found \">\"\n") 335 expectParseErrorTSX(t, "class Foo<> {}", "<stdin>: ERROR: Expected identifier but found \">\"\n") 336 expectParseErrorTS(t, "class Foo { foo<>() {} }", "<stdin>: ERROR: Expected identifier but found \">\"\n") 337 expectParseErrorTSX(t, "class Foo { foo<>() {} }", "<stdin>: ERROR: Expected identifier but found \">\"\n") 338 expectParseErrorTS(t, "type Foo = { foo<>(): void }", "<stdin>: ERROR: Expected identifier but found \">\"\n") 339 expectParseErrorTSX(t, "type Foo = { foo<>(): void }", "<stdin>: ERROR: Expected identifier but found \">\"\n") 340 expectParseErrorTS(t, "type Foo = <>() => {}", "<stdin>: ERROR: Expected identifier but found \">\"\n") 341 expectParseErrorTSX(t, "type Foo = <>() => {}", "<stdin>: ERROR: Expected identifier but found \">\"\n") 342 expectParseErrorTS(t, "let Foo = <>() => {}", "<stdin>: ERROR: Unexpected \">\"\n") 343 expectParseErrorTSX(t, "let Foo = <>() => {}", 344 "<stdin>: ERROR: The character \">\" is not valid inside a JSX element\nNOTE: Did you mean to escape it as \"{'>'}\" instead?\n"+ 345 "<stdin>: ERROR: Unexpected end of file before a closing fragment tag\n<stdin>: NOTE: The opening fragment tag is here:\n") 346 347 // Certain built-in types do not accept type parameters 348 expectPrintedTS(t, "x as 1 < 1", "x < 1;\n") 349 expectPrintedTS(t, "x as 1n < 1", "x < 1;\n") 350 expectPrintedTS(t, "x as -1 < 1", "x < 1;\n") 351 expectPrintedTS(t, "x as -1n < 1", "x < 1;\n") 352 expectPrintedTS(t, "x as '' < 1", "x < 1;\n") 353 expectPrintedTS(t, "x as `` < 1", "x < 1;\n") 354 expectPrintedTS(t, "x as any < 1", "x < 1;\n") 355 expectPrintedTS(t, "x as bigint < 1", "x < 1;\n") 356 expectPrintedTS(t, "x as false < 1", "x < 1;\n") 357 expectPrintedTS(t, "x as never < 1", "x < 1;\n") 358 expectPrintedTS(t, "x as null < 1", "x < 1;\n") 359 expectPrintedTS(t, "x as number < 1", "x < 1;\n") 360 expectPrintedTS(t, "x as object < 1", "x < 1;\n") 361 expectPrintedTS(t, "x as string < 1", "x < 1;\n") 362 expectPrintedTS(t, "x as symbol < 1", "x < 1;\n") 363 expectPrintedTS(t, "x as this < 1", "x < 1;\n") 364 expectPrintedTS(t, "x as true < 1", "x < 1;\n") 365 expectPrintedTS(t, "x as undefined < 1", "x < 1;\n") 366 expectPrintedTS(t, "x as unique symbol < 1", "x < 1;\n") 367 expectPrintedTS(t, "x as unknown < 1", "x < 1;\n") 368 expectPrintedTS(t, "x as void < 1", "x < 1;\n") 369 expectParseErrorTS(t, "x as Foo < 1", "<stdin>: ERROR: Expected \">\" but found end of file\n") 370 371 // These keywords are valid tuple labels 372 expectPrintedTS(t, "type _false = [false: string]", "") 373 expectPrintedTS(t, "type _function = [function: string]", "") 374 expectPrintedTS(t, "type _import = [import: string]", "") 375 expectPrintedTS(t, "type _new = [new: string]", "") 376 expectPrintedTS(t, "type _null = [null: string]", "") 377 expectPrintedTS(t, "type _this = [this: string]", "") 378 expectPrintedTS(t, "type _true = [true: string]", "") 379 expectPrintedTS(t, "type _typeof = [typeof: string]", "") 380 expectPrintedTS(t, "type _void = [void: string]", "") 381 382 // These keywords are invalid tuple labels 383 expectParseErrorTS(t, "type _break = [break: string]", "<stdin>: ERROR: Unexpected \"break\"\n") 384 expectParseErrorTS(t, "type _case = [case: string]", "<stdin>: ERROR: Unexpected \"case\"\n") 385 expectParseErrorTS(t, "type _catch = [catch: string]", "<stdin>: ERROR: Unexpected \"catch\"\n") 386 expectParseErrorTS(t, "type _class = [class: string]", "<stdin>: ERROR: Unexpected \"class\"\n") 387 expectParseErrorTS(t, "type _const = [const: string]", "<stdin>: ERROR: Unexpected \"const\"\n") 388 expectParseErrorTS(t, "type _continue = [continue: string]", "<stdin>: ERROR: Unexpected \"continue\"\n") 389 expectParseErrorTS(t, "type _debugger = [debugger: string]", "<stdin>: ERROR: Unexpected \"debugger\"\n") 390 expectParseErrorTS(t, "type _default = [default: string]", "<stdin>: ERROR: Unexpected \"default\"\n") 391 expectParseErrorTS(t, "type _delete = [delete: string]", "<stdin>: ERROR: Unexpected \"delete\"\n") 392 expectParseErrorTS(t, "type _do = [do: string]", "<stdin>: ERROR: Unexpected \"do\"\n") 393 expectParseErrorTS(t, "type _else = [else: string]", "<stdin>: ERROR: Unexpected \"else\"\n") 394 expectParseErrorTS(t, "type _enum = [enum: string]", "<stdin>: ERROR: Unexpected \"enum\"\n") 395 expectParseErrorTS(t, "type _export = [export: string]", "<stdin>: ERROR: Unexpected \"export\"\n") 396 expectParseErrorTS(t, "type _extends = [extends: string]", "<stdin>: ERROR: Unexpected \"extends\"\n") 397 expectParseErrorTS(t, "type _finally = [finally: string]", "<stdin>: ERROR: Unexpected \"finally\"\n") 398 expectParseErrorTS(t, "type _for = [for: string]", "<stdin>: ERROR: Unexpected \"for\"\n") 399 expectParseErrorTS(t, "type _if = [if: string]", "<stdin>: ERROR: Unexpected \"if\"\n") 400 expectParseErrorTS(t, "type _in = [in: string]", "<stdin>: ERROR: Unexpected \"in\"\n") 401 expectParseErrorTS(t, "type _instanceof = [instanceof: string]", "<stdin>: ERROR: Unexpected \"instanceof\"\n") 402 expectParseErrorTS(t, "type _return = [return: string]", "<stdin>: ERROR: Unexpected \"return\"\n") 403 expectParseErrorTS(t, "type _super = [super: string]", "<stdin>: ERROR: Unexpected \"super\"\n") 404 expectParseErrorTS(t, "type _switch = [switch: string]", "<stdin>: ERROR: Unexpected \"switch\"\n") 405 expectParseErrorTS(t, "type _throw = [throw: string]", "<stdin>: ERROR: Unexpected \"throw\"\n") 406 expectParseErrorTS(t, "type _try = [try: string]", "<stdin>: ERROR: Unexpected \"try\"\n") 407 expectParseErrorTS(t, "type _var = [var: string]", "<stdin>: ERROR: Unexpected \"var\"\n") 408 expectParseErrorTS(t, "type _while = [while: string]", "<stdin>: ERROR: Unexpected \"while\"\n") 409 expectParseErrorTS(t, "type _with = [with: string]", "<stdin>: ERROR: Unexpected \"with\"\n") 410 411 // TypeScript 4.1 412 expectPrintedTS(t, "let foo: `${'a' | 'b'}-${'c' | 'd'}` = 'a-c'", "let foo = \"a-c\";\n") 413 414 // TypeScript 4.2 415 expectPrintedTS(t, "let x: abstract new () => void = Foo", "let x = Foo;\n") 416 expectPrintedTS(t, "let x: abstract new <T>() => Foo<T>", "let x;\n") 417 expectPrintedTS(t, "let x: abstract new <T extends object>() => Foo<T>", "let x;\n") 418 expectParseErrorTS(t, "let x: abstract () => void = Foo", "<stdin>: ERROR: Expected \";\" but found \"(\"\n") 419 expectParseErrorTS(t, "let x: abstract <T>() => Foo<T>", "<stdin>: ERROR: Expected \";\" but found \"(\"\n") 420 expectParseErrorTS(t, "let x: abstract <T extends object>() => Foo<T>", "<stdin>: ERROR: Expected \"?\" but found \">\"\n") 421 422 // TypeScript 4.7 423 jsxErrorArrow := "<stdin>: ERROR: The character \">\" is not valid inside a JSX element\n" + 424 "NOTE: Did you mean to escape it as \"{'>'}\" instead?\n" 425 expectPrintedTS(t, "type Foo<in T> = T", "") 426 expectPrintedTS(t, "type Foo<out T> = T", "") 427 expectPrintedTS(t, "type Foo<in out> = T", "") 428 expectPrintedTS(t, "type Foo<out out> = T", "") 429 expectPrintedTS(t, "type Foo<in out out> = T", "") 430 expectPrintedTS(t, "type Foo<in X, out Y> = [X, Y]", "") 431 expectPrintedTS(t, "type Foo<out X, in Y> = [X, Y]", "") 432 expectPrintedTS(t, "type Foo<out X, out Y extends keyof X> = [X, Y]", "") 433 expectParseErrorTS(t, "type Foo<i\\u006E T> = T", "<stdin>: ERROR: Expected identifier but found \"i\\\\u006E\"\n") 434 expectParseErrorTS(t, "type Foo<ou\\u0074 T> = T", "<stdin>: ERROR: Expected \">\" but found \"T\"\n") 435 expectParseErrorTS(t, "type Foo<in in> = T", "<stdin>: ERROR: The modifier \"in\" is not valid here:\n<stdin>: ERROR: Expected identifier but found \">\"\n") 436 expectParseErrorTS(t, "type Foo<out in> = T", "<stdin>: ERROR: The modifier \"in\" is not valid here:\n<stdin>: ERROR: Expected identifier but found \">\"\n") 437 expectParseErrorTS(t, "type Foo<out in T> = T", "<stdin>: ERROR: The modifier \"in\" is not valid here:\n") 438 expectParseErrorTS(t, "type Foo<public T> = T", "<stdin>: ERROR: Expected \">\" but found \"T\"\n") 439 expectParseErrorTS(t, "type Foo<in out in T> = T", "<stdin>: ERROR: The modifier \"in\" is not valid here:\n") 440 expectParseErrorTS(t, "type Foo<in out out T> = T", "<stdin>: ERROR: The modifier \"out\" is not valid here:\n") 441 expectPrintedTS(t, "class Foo<in T> {}", "class Foo {\n}\n") 442 expectPrintedTS(t, "class Foo<out T> {}", "class Foo {\n}\n") 443 expectPrintedTS(t, "export default class Foo<in T> {}", "export default class Foo {\n}\n") 444 expectPrintedTS(t, "export default class Foo<out T> {}", "export default class Foo {\n}\n") 445 expectPrintedTS(t, "export default class <in T> {}", "export default class {\n}\n") 446 expectPrintedTS(t, "export default class <out T> {}", "export default class {\n}\n") 447 expectPrintedTS(t, "interface Foo<in T> {}", "") 448 expectPrintedTS(t, "interface Foo<out T> {}", "") 449 expectPrintedTS(t, "declare class Foo<in T> {}", "") 450 expectPrintedTS(t, "declare class Foo<out T> {}", "") 451 expectPrintedTS(t, "declare interface Foo<in T> {}", "") 452 expectPrintedTS(t, "declare interface Foo<out T> {}", "") 453 expectParseErrorTS(t, "function foo<in T>() {}", "<stdin>: ERROR: The modifier \"in\" is not valid here:\n") 454 expectParseErrorTS(t, "function foo<out T>() {}", "<stdin>: ERROR: The modifier \"out\" is not valid here:\n") 455 expectParseErrorTS(t, "export default function foo<in T>() {}", "<stdin>: ERROR: The modifier \"in\" is not valid here:\n") 456 expectParseErrorTS(t, "export default function foo<out T>() {}", "<stdin>: ERROR: The modifier \"out\" is not valid here:\n") 457 expectParseErrorTS(t, "export default function <in T>() {}", "<stdin>: ERROR: The modifier \"in\" is not valid here:\n") 458 expectParseErrorTS(t, "export default function <out T>() {}", "<stdin>: ERROR: The modifier \"out\" is not valid here:\n") 459 expectParseErrorTS(t, "let foo: Foo<in T>", "<stdin>: ERROR: Unexpected \"in\"\n") 460 expectParseErrorTS(t, "let foo: Foo<out T>", "<stdin>: ERROR: Expected \">\" but found \"T\"\n") 461 expectParseErrorTS(t, "declare function foo<in T>()", "<stdin>: ERROR: The modifier \"in\" is not valid here:\n") 462 expectParseErrorTS(t, "declare function foo<out T>()", "<stdin>: ERROR: The modifier \"out\" is not valid here:\n") 463 expectParseErrorTS(t, "declare let foo: Foo<in T>", "<stdin>: ERROR: Unexpected \"in\"\n") 464 expectParseErrorTS(t, "declare let foo: Foo<out T>", "<stdin>: ERROR: Expected \">\" but found \"T\"\n") 465 expectPrintedTS(t, "Foo = class <in T> {}", "Foo = class {\n};\n") 466 expectPrintedTS(t, "Foo = class <out T> {}", "Foo = class {\n};\n") 467 expectPrintedTS(t, "Foo = class Bar<in T> {}", "Foo = class Bar {\n};\n") 468 expectPrintedTS(t, "Foo = class Bar<out T> {}", "Foo = class Bar {\n};\n") 469 expectParseErrorTS(t, "foo = function <in T>() {}", "<stdin>: ERROR: The modifier \"in\" is not valid here:\n") 470 expectParseErrorTS(t, "foo = function <out T>() {}", "<stdin>: ERROR: The modifier \"out\" is not valid here:\n") 471 expectParseErrorTS(t, "class Foo { foo<in T>(): T {} }", "<stdin>: ERROR: The modifier \"in\" is not valid here:\n") 472 expectParseErrorTS(t, "class Foo { foo<out T>(): T {} }", "<stdin>: ERROR: The modifier \"out\" is not valid here:\n") 473 expectParseErrorTS(t, "foo = { foo<in T>(): T {} }", "<stdin>: ERROR: The modifier \"in\" is not valid here:\n") 474 expectParseErrorTS(t, "foo = { foo<out T>(): T {} }", "<stdin>: ERROR: The modifier \"out\" is not valid here:\n") 475 expectParseErrorTS(t, "<in T>() => {}", "<stdin>: ERROR: The modifier \"in\" is not valid here:\n") 476 expectParseErrorTS(t, "<out T>() => {}", "<stdin>: ERROR: The modifier \"out\" is not valid here:\n") 477 expectParseErrorTS(t, "<in T, out T>() => {}", "<stdin>: ERROR: The modifier \"in\" is not valid here:\n<stdin>: ERROR: The modifier \"out\" is not valid here:\n") 478 expectParseErrorTS(t, "let x: <in T>() => {}", "<stdin>: ERROR: The modifier \"in\" is not valid here:\n") 479 expectParseErrorTS(t, "let x: <out T>() => {}", "<stdin>: ERROR: The modifier \"out\" is not valid here:\n") 480 expectParseErrorTS(t, "let x: <in T, out T>() => {}", "<stdin>: ERROR: The modifier \"in\" is not valid here:\n<stdin>: ERROR: The modifier \"out\" is not valid here:\n") 481 expectParseErrorTS(t, "let x: new <in T>() => {}", "<stdin>: ERROR: The modifier \"in\" is not valid here:\n") 482 expectParseErrorTS(t, "let x: new <out T>() => {}", "<stdin>: ERROR: The modifier \"out\" is not valid here:\n") 483 expectParseErrorTS(t, "let x: new <in T, out T>() => {}", "<stdin>: ERROR: The modifier \"in\" is not valid here:\n<stdin>: ERROR: The modifier \"out\" is not valid here:\n") 484 expectParseErrorTS(t, "let x: { y<in T>(): any }", "<stdin>: ERROR: The modifier \"in\" is not valid here:\n") 485 expectParseErrorTS(t, "let x: { y<out T>(): any }", "<stdin>: ERROR: The modifier \"out\" is not valid here:\n") 486 expectParseErrorTS(t, "let x: { y<in T, out T>(): any }", "<stdin>: ERROR: The modifier \"in\" is not valid here:\n<stdin>: ERROR: The modifier \"out\" is not valid here:\n") 487 expectPrintedTSX(t, "<in T></in>", "/* @__PURE__ */ React.createElement(\"in\", { T: true });\n") 488 expectPrintedTSX(t, "<out T></out>", "/* @__PURE__ */ React.createElement(\"out\", { T: true });\n") 489 expectPrintedTSX(t, "<in out T></in>", "/* @__PURE__ */ React.createElement(\"in\", { out: true, T: true });\n") 490 expectPrintedTSX(t, "<out in T></out>", "/* @__PURE__ */ React.createElement(\"out\", { in: true, T: true });\n") 491 expectPrintedTSX(t, "<in T extends={true}></in>", "/* @__PURE__ */ React.createElement(\"in\", { T: true, extends: true });\n") 492 expectPrintedTSX(t, "<out T extends={true}></out>", "/* @__PURE__ */ React.createElement(\"out\", { T: true, extends: true });\n") 493 expectPrintedTSX(t, "<in out T extends={true}></in>", "/* @__PURE__ */ React.createElement(\"in\", { out: true, T: true, extends: true });\n") 494 expectParseErrorTSX(t, "<in T,>() => {}", "<stdin>: ERROR: Expected \">\" but found \",\"\n") 495 expectParseErrorTSX(t, "<out T,>() => {}", "<stdin>: ERROR: Expected \">\" but found \",\"\n") 496 expectParseErrorTSX(t, "<in out T,>() => {}", "<stdin>: ERROR: Expected \">\" but found \",\"\n") 497 expectParseErrorTSX(t, "<in T extends any>() => {}", jsxErrorArrow+"<stdin>: ERROR: Unexpected end of file before a closing \"in\" tag\n<stdin>: NOTE: The opening \"in\" tag is here:\n") 498 expectParseErrorTSX(t, "<out T extends any>() => {}", jsxErrorArrow+"<stdin>: ERROR: Unexpected end of file before a closing \"out\" tag\n<stdin>: NOTE: The opening \"out\" tag is here:\n") 499 expectParseErrorTSX(t, "<in out T extends any>() => {}", jsxErrorArrow+"<stdin>: ERROR: Unexpected end of file before a closing \"in\" tag\n<stdin>: NOTE: The opening \"in\" tag is here:\n") 500 expectPrintedTS(t, "class Container { get data(): typeof this.#data {} }", "class Container {\n get data() {\n }\n}\n") 501 expectPrintedTS(t, "const a: typeof this.#a = 1;", "const a = 1;\n") 502 expectParseErrorTS(t, "const a: typeof #a = 1;", "<stdin>: ERROR: Expected identifier but found \"#a\"\n") 503 504 // TypeScript 5.0 505 expectPrintedTS(t, "class Foo<const T> {}", "class Foo {\n}\n") 506 expectPrintedTS(t, "class Foo<const T extends X> {}", "class Foo {\n}\n") 507 expectPrintedTS(t, "Foo = class <const T> {}", "Foo = class {\n};\n") 508 expectPrintedTS(t, "Foo = class Bar<const T> {}", "Foo = class Bar {\n};\n") 509 expectPrintedTS(t, "function foo<const T>() {}", "function foo() {\n}\n") 510 expectPrintedTS(t, "foo = function <const T>() {}", "foo = function() {\n};\n") 511 expectPrintedTS(t, "foo = function bar<const T>() {}", "foo = function bar() {\n};\n") 512 expectPrintedTS(t, "class Foo { bar<const T>() {} }", "class Foo {\n bar() {\n }\n}\n") 513 expectPrintedTS(t, "interface Foo { bar<const T>(): T }", "") 514 expectPrintedTS(t, "interface Foo { new bar<const T>(): T }", "") 515 expectPrintedTS(t, "let x: { bar<const T>(): T }", "let x;\n") 516 expectPrintedTS(t, "let x: { new bar<const T>(): T }", "let x;\n") 517 expectPrintedTS(t, "foo = { bar<const T>() {} }", "foo = { bar() {\n} };\n") 518 expectPrintedTS(t, "x = <const>(y)", "x = y;\n") 519 expectPrintedTS(t, "<const T>() => {}", "() => {\n};\n") 520 expectPrintedTS(t, "<const const T>() => {}", "() => {\n};\n") 521 expectPrintedTS(t, "async <const T>() => {}", "async () => {\n};\n") 522 expectPrintedTS(t, "async <const const T>() => {}", "async () => {\n};\n") 523 expectPrintedTS(t, "let x: <const T>() => T = y", "let x = y;\n") 524 expectPrintedTS(t, "let x: <const const T>() => T = y", "let x = y;\n") 525 expectPrintedTS(t, "let x: new <const T>() => T = y", "let x = y;\n") 526 expectPrintedTS(t, "let x: new <const const T>() => T = y", "let x = y;\n") 527 expectParseErrorTS(t, "type Foo<const T> = T", "<stdin>: ERROR: The modifier \"const\" is not valid here:\n") 528 expectParseErrorTS(t, "interface Foo<const T> {}", "<stdin>: ERROR: The modifier \"const\" is not valid here:\n") 529 expectParseErrorTS(t, "let x: <const>() => {}", "<stdin>: ERROR: Expected identifier but found \">\"\n") 530 expectParseErrorTS(t, "let x: new <const>() => {}", "<stdin>: ERROR: Expected identifier but found \">\"\n") 531 expectParseErrorTS(t, "let x: Foo<const T>", "<stdin>: ERROR: Expected \">\" but found \"T\"\n") 532 expectParseErrorTS(t, "x = <T,>(y)", "<stdin>: ERROR: Expected \"=>\" but found end of file\n") 533 expectParseErrorTS(t, "x = <const T>(y)", "<stdin>: ERROR: Expected \"=>\" but found end of file\n") 534 expectParseErrorTS(t, "x = <T extends X>(y)", "<stdin>: ERROR: Expected \"=>\" but found end of file\n") 535 expectParseErrorTS(t, "x = async <T,>(y)", "<stdin>: ERROR: Expected \"=>\" but found end of file\n") 536 expectParseErrorTS(t, "x = async <const T>(y)", "<stdin>: ERROR: Expected \"=>\" but found end of file\n") 537 expectParseErrorTS(t, "x = async <T extends X>(y)", "<stdin>: ERROR: Expected \"=>\" but found end of file\n") 538 expectParseErrorTS(t, "x = <const const>() => {}", "<stdin>: ERROR: Expected \">\" but found \"const\"\n") 539 expectPrintedTS(t, "class Foo<const const const T> {}", "class Foo {\n}\n") 540 expectPrintedTS(t, "class Foo<const in out T> {}", "class Foo {\n}\n") 541 expectPrintedTS(t, "class Foo<in const out T> {}", "class Foo {\n}\n") 542 expectPrintedTS(t, "class Foo<in out const T> {}", "class Foo {\n}\n") 543 expectPrintedTS(t, "class Foo<const in const out const T> {}", "class Foo {\n}\n") 544 expectParseErrorTS(t, "class Foo<in const> {}", "<stdin>: ERROR: Expected identifier but found \">\"\n") 545 expectParseErrorTS(t, "class Foo<out const> {}", "<stdin>: ERROR: Expected identifier but found \">\"\n") 546 expectParseErrorTS(t, "class Foo<in out const> {}", "<stdin>: ERROR: Expected identifier but found \">\"\n") 547 expectPrintedTSX(t, "<const>(x)</const>", "/* @__PURE__ */ React.createElement(\"const\", null, \"(x)\");\n") 548 expectPrintedTSX(t, "<const const/>", "/* @__PURE__ */ React.createElement(\"const\", { const: true });\n") 549 expectPrintedTSX(t, "<const const></const>", "/* @__PURE__ */ React.createElement(\"const\", { const: true });\n") 550 expectPrintedTSX(t, "<const T/>", "/* @__PURE__ */ React.createElement(\"const\", { T: true });\n") 551 expectPrintedTSX(t, "<const T></const>", "/* @__PURE__ */ React.createElement(\"const\", { T: true });\n") 552 expectPrintedTSX(t, "<const T>(y) = {}</const>", "/* @__PURE__ */ React.createElement(\"const\", { T: true }, \"(y) = \");\n") 553 expectPrintedTSX(t, "<const T extends/>", "/* @__PURE__ */ React.createElement(\"const\", { T: true, extends: true });\n") 554 expectPrintedTSX(t, "<const T extends></const>", "/* @__PURE__ */ React.createElement(\"const\", { T: true, extends: true });\n") 555 expectPrintedTSX(t, "<const T extends>(y) = {}</const>", "/* @__PURE__ */ React.createElement(\"const\", { T: true, extends: true }, \"(y) = \");\n") 556 expectPrintedTSX(t, "<const T,>() => {}", "() => {\n};\n") 557 expectPrintedTSX(t, "<const T, X>() => {}", "() => {\n};\n") 558 expectPrintedTSX(t, "<const T, const X>() => {}", "() => {\n};\n") 559 expectPrintedTSX(t, "<const T, const const X>() => {}", "() => {\n};\n") 560 expectPrintedTSX(t, "<const T extends X>() => {}", "() => {\n};\n") 561 expectPrintedTSX(t, "async <const T,>() => {}", "async () => {\n};\n") 562 expectPrintedTSX(t, "async <const T, X>() => {}", "async () => {\n};\n") 563 expectPrintedTSX(t, "async <const T, const X>() => {}", "async () => {\n};\n") 564 expectPrintedTSX(t, "async <const T, const const X>() => {}", "async () => {\n};\n") 565 expectPrintedTSX(t, "async <const T extends X>() => {}", "async () => {\n};\n") 566 expectParseErrorTSX(t, "<const T>() => {}", jsxErrorArrow+"<stdin>: ERROR: Unexpected end of file before a closing \"const\" tag\n<stdin>: NOTE: The opening \"const\" tag is here:\n") 567 expectParseErrorTSX(t, "<const const>() => {}", jsxErrorArrow+"<stdin>: ERROR: Unexpected end of file before a closing \"const\" tag\n<stdin>: NOTE: The opening \"const\" tag is here:\n") 568 expectParseErrorTSX(t, "<const const T,>() => {}", "<stdin>: ERROR: Expected \">\" but found \",\"\n") 569 expectParseErrorTSX(t, "<const const T extends X>() => {}", jsxErrorArrow+"<stdin>: ERROR: Unexpected end of file before a closing \"const\" tag\n<stdin>: NOTE: The opening \"const\" tag is here:\n") 570 expectParseErrorTSX(t, "async <const T>() => {}", "<stdin>: ERROR: Unexpected \"const\"\n") 571 expectParseErrorTSX(t, "async <const const>() => {}", "<stdin>: ERROR: Unexpected \"const\"\n") 572 expectParseErrorTSX(t, "async <const const T,>() => {}", "<stdin>: ERROR: Unexpected \"const\"\n") 573 expectParseErrorTSX(t, "async <const const T extends X>() => {}", "<stdin>: ERROR: Unexpected \"const\"\n") 574 } 575 576 func TestTSAsCast(t *testing.T) { 577 expectPrintedTS(t, "x as any\n(y);", "x;\ny;\n") 578 expectPrintedTS(t, "x as any\n`y`;", "x;\n`y`;\n") 579 expectPrintedTS(t, "x as any\n`${y}`;", "x;\n`${y}`;\n") 580 expectPrintedTS(t, "x as any\n--y;", "x;\n--y;\n") 581 expectPrintedTS(t, "x as any\n++y;", "x;\n++y;\n") 582 expectPrintedTS(t, "x + y as any\n(z as any) + 1;", "x + y;\nz + 1;\n") 583 expectPrintedTS(t, "x + y as any\n(z as any) = 1;", "x + y;\nz = 1;\n") 584 expectPrintedTS(t, "x = y as any\n(z as any) + 1;", "x = y;\nz + 1;\n") 585 expectPrintedTS(t, "x = y as any\n(z as any) = 1;", "x = y;\nz = 1;\n") 586 expectPrintedTS(t, "x * y as any\n['z'];", "x * y;\n[\"z\"];\n") 587 expectPrintedTS(t, "x * y as any\n.z;", "x * y;\n") 588 expectPrintedTS(t, "x as y['x'];", "x;\n") 589 expectPrintedTS(t, "x as y!['x'];", "x;\n") 590 expectPrintedTS(t, "x as y\n['x'];", "x;\n[\"x\"];\n") 591 expectPrintedTS(t, "x as y\n!['x'];", "x;\n![\"x\"];\n") 592 expectParseErrorTS(t, "x = y as any `z`;", "<stdin>: ERROR: Expected \";\" but found \"`z`\"\n") 593 expectParseErrorTS(t, "x = y as any `${z}`;", "<stdin>: ERROR: Expected \";\" but found \"`${\"\n") 594 expectParseErrorTS(t, "x = y as any?.z;", "<stdin>: ERROR: Expected \";\" but found \"?.\"\n") 595 expectParseErrorTS(t, "x = y as any--;", "<stdin>: ERROR: Expected \";\" but found \"--\"\n") 596 expectParseErrorTS(t, "x = y as any++;", "<stdin>: ERROR: Expected \";\" but found \"++\"\n") 597 expectParseErrorTS(t, "x = y as any(z);", "<stdin>: ERROR: Expected \";\" but found \"(\"\n") 598 expectParseErrorTS(t, "x = y as any\n= z;", "<stdin>: ERROR: Unexpected \"=\"\n") 599 expectParseErrorTS(t, "a, x as y `z`;", "<stdin>: ERROR: Expected \";\" but found \"`z`\"\n") 600 expectParseErrorTS(t, "a ? b : x as y `z`;", "<stdin>: ERROR: Expected \";\" but found \"`z`\"\n") 601 expectParseErrorTS(t, "x as any = y;", "<stdin>: ERROR: Expected \";\" but found \"=\"\n") 602 expectParseErrorTS(t, "(x as any = y);", "<stdin>: ERROR: Expected \")\" but found \"=\"\n") 603 expectParseErrorTS(t, "(x = y as any(z));", "<stdin>: ERROR: Expected \")\" but found \"(\"\n") 604 } 605 606 func TestTSSatisfies(t *testing.T) { 607 expectPrintedTS(t, "const t1 = { a: 1 } satisfies I1;", "const t1 = { a: 1 };\n") 608 expectPrintedTS(t, "const t2 = { a: 1, b: 1 } satisfies I1;", "const t2 = { a: 1, b: 1 };\n") 609 expectPrintedTS(t, "const t3 = { } satisfies I1;", "const t3 = {};\n") 610 expectPrintedTS(t, "const t4: T1 = { a: 'a' } satisfies T1;", "const t4 = { a: \"a\" };\n") 611 expectPrintedTS(t, "const t5 = (m => m.substring(0)) satisfies T2;", "const t5 = (m) => m.substring(0);\n") 612 expectPrintedTS(t, "const t6 = [1, 2] satisfies [number, number];", "const t6 = [1, 2];\n") 613 expectPrintedTS(t, "let t7 = { a: 'test' } satisfies A;", "let t7 = { a: \"test\" };\n") 614 expectPrintedTS(t, "let t8 = { a: 'test', b: 'test' } satisfies A;", "let t8 = { a: \"test\", b: \"test\" };\n") 615 expectPrintedTS(t, "export default {} satisfies Foo;", "export default {};\n") 616 expectPrintedTS(t, "export default { a: 1 } satisfies Foo;", "export default { a: 1 };\n") 617 expectPrintedTS(t, 618 "const p = { isEven: n => n % 2 === 0, isOdd: n => n % 2 === 1 } satisfies Predicates;", 619 "const p = { isEven: (n) => n % 2 === 0, isOdd: (n) => n % 2 === 1 };\n") 620 expectPrintedTS(t, 621 "let obj: { f(s: string): void } & Record<string, unknown> = { f(s) { }, g(s) { } } satisfies { g(s: string): void } & Record<string, unknown>;", 622 "let obj = { f(s) {\n}, g(s) {\n} };\n") 623 expectPrintedTS(t, 624 "const car = { start() { }, move(d) { }, stop() { } } satisfies Movable & Record<string, unknown>;", 625 "const car = { start() {\n}, move(d) {\n}, stop() {\n} };\n", 626 ) 627 expectPrintedTS(t, "var v = undefined satisfies 1;", "var v = void 0;\n") 628 expectPrintedTS(t, "const a = { x: 10 } satisfies Partial<Point2d>;", "const a = { x: 10 };\n") 629 expectPrintedTS(t, 630 "const p = { a: 0, b: \"hello\", x: 8 } satisfies Partial<Record<Keys, unknown>>;", 631 "const p = { a: 0, b: \"hello\", x: 8 };\n", 632 ) 633 expectPrintedTS(t, 634 "const p = { a: 0, b: \"hello\", x: 8 } satisfies Record<Keys, unknown>;", 635 "const p = { a: 0, b: \"hello\", x: 8 };\n", 636 ) 637 expectPrintedTS(t, 638 "const x2 = { m: true, s: \"false\" } satisfies Facts;", 639 "const x2 = { m: true, s: \"false\" };\n", 640 ) 641 expectPrintedTS(t, 642 "export const Palette = { white: { r: 255, g: 255, b: 255 }, black: { r: 0, g: 0, d: 0 }, blue: { r: 0, g: 0, b: 255 }, } satisfies Record<string, Color>;", 643 "export const Palette = { white: { r: 255, g: 255, b: 255 }, black: { r: 0, g: 0, d: 0 }, blue: { r: 0, g: 0, b: 255 } };\n", 644 ) 645 expectPrintedTS(t, 646 "const a: \"baz\" = \"foo\" satisfies \"foo\" | \"bar\";", 647 "const a = \"foo\";\n", 648 ) 649 expectPrintedTS(t, 650 "const b: { xyz: \"baz\" } = { xyz: \"foo\" } satisfies { xyz: \"foo\" | \"bar\" };", 651 "const b = { xyz: \"foo\" };\n", 652 ) 653 } 654 655 func TestTSClass(t *testing.T) { 656 expectPrintedTS(t, "export default class Foo {}", "export default class Foo {\n}\n") 657 expectPrintedTS(t, "export default class Foo extends Bar<T> {}", "export default class Foo extends Bar {\n}\n") 658 expectPrintedTS(t, "export default class Foo extends Bar<T>() {}", "export default class Foo extends Bar() {\n}\n") 659 expectPrintedTS(t, "export default class Foo implements Bar<T> {}", "export default class Foo {\n}\n") 660 expectPrintedTS(t, "export default class Foo<T> {}", "export default class Foo {\n}\n") 661 expectPrintedTS(t, "export default class Foo<T> extends Bar<T> {}", "export default class Foo extends Bar {\n}\n") 662 expectPrintedTS(t, "export default class Foo<T> extends Bar<T>() {}", "export default class Foo extends Bar() {\n}\n") 663 expectPrintedTS(t, "export default class Foo<T> implements Bar<T> {}", "export default class Foo {\n}\n") 664 expectPrintedTS(t, "(class Foo<T> {})", "(class Foo {\n});\n") 665 expectPrintedTS(t, "(class Foo<T> extends Bar<T> {})", "(class Foo extends Bar {\n});\n") 666 expectPrintedTS(t, "(class Foo<T> extends Bar<T>() {})", "(class Foo extends Bar() {\n});\n") 667 expectPrintedTS(t, "(class Foo<T> implements Bar<T> {})", "(class Foo {\n});\n") 668 669 expectPrintedTS(t, "export default class {}", "export default class {\n}\n") 670 expectPrintedTS(t, "export default class extends Foo<T> {}", "export default class extends Foo {\n}\n") 671 expectPrintedTS(t, "export default class implements Foo<T> {}", "export default class {\n}\n") 672 expectPrintedTS(t, "export default class <T> {}", "export default class {\n}\n") 673 expectPrintedTS(t, "export default class <T> extends Foo<T> {}", "export default class extends Foo {\n}\n") 674 expectPrintedTS(t, "export default class <T> implements Foo<T> {}", "export default class {\n}\n") 675 expectPrintedTS(t, "(class <T> {})", "(class {\n});\n") 676 expectPrintedTS(t, "(class extends Foo<T> {})", "(class extends Foo {\n});\n") 677 expectPrintedTS(t, "(class extends Foo<T>() {})", "(class extends Foo() {\n});\n") 678 expectPrintedTS(t, "(class implements Foo<T> {})", "(class {\n});\n") 679 expectPrintedTS(t, "(class <T> extends Foo<T> {})", "(class extends Foo {\n});\n") 680 expectPrintedTS(t, "(class <T> extends Foo<T>() {})", "(class extends Foo() {\n});\n") 681 expectPrintedTS(t, "(class <T> implements Foo<T> {})", "(class {\n});\n") 682 683 // Check ASI for "abstract" 684 expectPrintedTS(t, "abstract \n class A {}", "abstract;\nclass A {\n}\n") 685 expectPrintedTS(t, "export default abstract \n class A {}", "export default abstract;\nclass A {\n}\n") 686 expectPrintedTS(t, "abstract class A { abstract \n foo(): void {} }", "class A {\n abstract;\n foo() {\n }\n}\n") 687 688 expectPrintedTS(t, "abstract class A { abstract foo(): void; bar(): void {} }", "class A {\n bar() {\n }\n}\n") 689 expectPrintedTS(t, "export abstract class A { abstract foo(): void; bar(): void {} }", "export class A {\n bar() {\n }\n}\n") 690 expectPrintedTS(t, "export default abstract", "export default abstract;\n") 691 expectPrintedTS(t, "export default abstract - after", "export default abstract - after;\n") 692 expectPrintedTS(t, "export default abstract class { abstract foo(): void; bar(): void {} } - after", "export default class {\n bar() {\n }\n}\n-after;\n") 693 expectPrintedTS(t, "export default abstract class A { abstract foo(): void; bar(): void {} } - after", "export default class A {\n bar() {\n }\n}\n-after;\n") 694 695 expectPrintedTS(t, "class A<T extends number> extends B.C<D, E> {}", "class A extends B.C {\n}\n") 696 expectPrintedTS(t, "class A<T extends number> implements B.C<D, E>, F.G<H, I> {}", "class A {\n}\n") 697 expectPrintedTS(t, "class A<T extends number> extends X implements B.C<D, E>, F.G<H, I> {}", "class A extends X {\n}\n") 698 699 reservedWordError := 700 " is a reserved word and cannot be used in strict mode\n" + 701 "<stdin>: NOTE: All code inside a class is implicitly in strict mode\n" 702 703 expectParseErrorTS(t, "class Foo { constructor(public) {} }", "<stdin>: ERROR: \"public\""+reservedWordError) 704 expectParseErrorTS(t, "class Foo { constructor(protected) {} }", "<stdin>: ERROR: \"protected\""+reservedWordError) 705 expectParseErrorTS(t, "class Foo { constructor(private) {} }", "<stdin>: ERROR: \"private\""+reservedWordError) 706 expectPrintedTS(t, "class Foo { constructor(readonly) {} }", "class Foo {\n constructor(readonly) {\n }\n}\n") 707 expectPrintedTS(t, "class Foo { constructor(override) {} }", "class Foo {\n constructor(override) {\n }\n}\n") 708 expectPrintedTS(t, "class Foo { constructor(public x) {} }", "class Foo {\n constructor(x) {\n this.x = x;\n }\n}\n") 709 expectPrintedTS(t, "class Foo { constructor(protected x) {} }", "class Foo {\n constructor(x) {\n this.x = x;\n }\n}\n") 710 expectPrintedTS(t, "class Foo { constructor(private x) {} }", "class Foo {\n constructor(x) {\n this.x = x;\n }\n}\n") 711 expectPrintedTS(t, "class Foo { constructor(readonly x) {} }", "class Foo {\n constructor(x) {\n this.x = x;\n }\n}\n") 712 expectPrintedTS(t, "class Foo { constructor(override x) {} }", "class Foo {\n constructor(x) {\n this.x = x;\n }\n}\n") 713 expectPrintedTS(t, "class Foo { constructor(public readonly x) {} }", "class Foo {\n constructor(x) {\n this.x = x;\n }\n}\n") 714 expectPrintedTS(t, "class Foo { constructor(protected readonly x) {} }", "class Foo {\n constructor(x) {\n this.x = x;\n }\n}\n") 715 expectPrintedTS(t, "class Foo { constructor(private readonly x) {} }", "class Foo {\n constructor(x) {\n this.x = x;\n }\n}\n") 716 expectPrintedTS(t, "class Foo { constructor(override readonly x) {} }", "class Foo {\n constructor(x) {\n this.x = x;\n }\n}\n") 717 718 expectParseErrorTS(t, "class Foo { constructor(public {x}) {} }", "<stdin>: ERROR: Expected identifier but found \"{\"\n") 719 expectParseErrorTS(t, "class Foo { constructor(protected {x}) {} }", "<stdin>: ERROR: Expected identifier but found \"{\"\n") 720 expectParseErrorTS(t, "class Foo { constructor(private {x}) {} }", "<stdin>: ERROR: Expected identifier but found \"{\"\n") 721 expectParseErrorTS(t, "class Foo { constructor(readonly {x}) {} }", "<stdin>: ERROR: Expected identifier but found \"{\"\n") 722 expectParseErrorTS(t, "class Foo { constructor(override {x}) {} }", "<stdin>: ERROR: Expected identifier but found \"{\"\n") 723 724 expectParseErrorTS(t, "class Foo { constructor(public [x]) {} }", "<stdin>: ERROR: Expected identifier but found \"[\"\n") 725 expectParseErrorTS(t, "class Foo { constructor(protected [x]) {} }", "<stdin>: ERROR: Expected identifier but found \"[\"\n") 726 expectParseErrorTS(t, "class Foo { constructor(private [x]) {} }", "<stdin>: ERROR: Expected identifier but found \"[\"\n") 727 expectParseErrorTS(t, "class Foo { constructor(readonly [x]) {} }", "<stdin>: ERROR: Expected identifier but found \"[\"\n") 728 expectParseErrorTS(t, "class Foo { constructor(override [x]) {} }", "<stdin>: ERROR: Expected identifier but found \"[\"\n") 729 730 expectPrintedAssignSemanticsTS(t, "class Foo { foo: number }", "class Foo {\n}\n") 731 expectPrintedAssignSemanticsTS(t, "class Foo { foo: number = 0 }", "class Foo {\n constructor() {\n this.foo = 0;\n }\n}\n") 732 expectPrintedAssignSemanticsTS(t, "class Foo { ['foo']: number }", "class Foo {\n}\n") 733 expectPrintedAssignSemanticsTS(t, "class Foo { ['foo']: number = 0 }", "class Foo {\n constructor() {\n this[\"foo\"] = 0;\n }\n}\n") 734 expectPrintedAssignSemanticsTS(t, "class Foo { foo(): void {} }", "class Foo {\n foo() {\n }\n}\n") 735 expectPrintedAssignSemanticsTS(t, "class Foo { foo(): void; foo(): void {} }", "class Foo {\n foo() {\n }\n}\n") 736 expectParseErrorTS(t, "class Foo { foo(): void foo(): void {} }", "<stdin>: ERROR: Expected \";\" but found \"foo\"\n") 737 738 expectPrintedAssignSemanticsTS(t, "class Foo { foo?: number }", "class Foo {\n}\n") 739 expectPrintedAssignSemanticsTS(t, "class Foo { foo?: number = 0 }", "class Foo {\n constructor() {\n this.foo = 0;\n }\n}\n") 740 expectPrintedAssignSemanticsTS(t, "class Foo { foo?(): void {} }", "class Foo {\n foo() {\n }\n}\n") 741 expectPrintedAssignSemanticsTS(t, "class Foo { foo?(): void; foo(): void {} }", "class Foo {\n foo() {\n }\n}\n") 742 expectParseErrorTS(t, "class Foo { foo?(): void foo(): void {} }", "<stdin>: ERROR: Expected \";\" but found \"foo\"\n") 743 744 expectPrintedAssignSemanticsTS(t, "class Foo { foo!: number }", "class Foo {\n}\n") 745 expectPrintedAssignSemanticsTS(t, "class Foo { foo!: number = 0 }", "class Foo {\n constructor() {\n this.foo = 0;\n }\n}\n") 746 expectParseErrorTS(t, "class Foo { foo!() {} }", "<stdin>: ERROR: Expected \";\" but found \"(\"\n") 747 expectParseErrorTS(t, "class Foo { *foo!() {} }", "<stdin>: ERROR: Expected \"(\" but found \"!\"\n") 748 expectParseErrorTS(t, "class Foo { get foo!() {} }", "<stdin>: ERROR: Expected \"(\" but found \"!\"\n") 749 expectParseErrorTS(t, "class Foo { set foo!(x) {} }", "<stdin>: ERROR: Expected \"(\" but found \"!\"\n") 750 expectParseErrorTS(t, "class Foo { async foo!() {} }", "<stdin>: ERROR: Expected \"(\" but found \"!\"\n") 751 752 expectPrintedAssignSemanticsTS(t, "class Foo { 'foo' = 0 }", "class Foo {\n constructor() {\n this[\"foo\"] = 0;\n }\n}\n") 753 expectPrintedAssignSemanticsTS(t, "class Foo { ['foo'] = 0 }", "class Foo {\n constructor() {\n this[\"foo\"] = 0;\n }\n}\n") 754 expectPrintedAssignSemanticsTS(t, "class Foo { [foo] = 0 }", "var _a;\n_a = foo;\nclass Foo {\n constructor() {\n this[_a] = 0;\n }\n}\n") 755 expectPrintedMangleAssignSemanticsTS(t, "class Foo { 'foo' = 0 }", "class Foo {\n constructor() {\n this.foo = 0;\n }\n}\n") 756 expectPrintedMangleAssignSemanticsTS(t, "class Foo { ['foo'] = 0 }", "class Foo {\n constructor() {\n this.foo = 0;\n }\n}\n") 757 758 expectPrintedAssignSemanticsTS(t, "class Foo { foo \n ?: number }", "class Foo {\n}\n") 759 expectParseErrorTS(t, "class Foo { foo \n !: number }", "<stdin>: ERROR: Expected identifier but found \"!\"\n") 760 761 expectPrintedAssignSemanticsTS(t, "class Foo { public foo: number }", "class Foo {\n}\n") 762 expectPrintedAssignSemanticsTS(t, "class Foo { private foo: number }", "class Foo {\n}\n") 763 expectPrintedAssignSemanticsTS(t, "class Foo { protected foo: number }", "class Foo {\n}\n") 764 expectPrintedTS(t, "class Foo { declare foo: number }", "class Foo {\n}\n") 765 expectPrintedTS(t, "class Foo { declare public foo: number }", "class Foo {\n}\n") 766 expectPrintedTS(t, "class Foo { public declare foo: number }", "class Foo {\n}\n") 767 expectPrintedAssignSemanticsTS(t, "class Foo { override foo: number }", "class Foo {\n}\n") 768 expectPrintedAssignSemanticsTS(t, "class Foo { override public foo: number }", "class Foo {\n}\n") 769 expectPrintedAssignSemanticsTS(t, "class Foo { public override foo: number }", "class Foo {\n}\n") 770 expectPrintedTS(t, "class Foo { declare override public foo: number }", "class Foo {\n}\n") 771 expectPrintedTS(t, "class Foo { declare foo = 123 }", "class Foo {\n}\n") 772 773 expectPrintedAssignSemanticsTS(t, "class Foo { public static foo: number }", "class Foo {\n}\n") 774 expectPrintedAssignSemanticsTS(t, "class Foo { private static foo: number }", "class Foo {\n}\n") 775 expectPrintedAssignSemanticsTS(t, "class Foo { protected static foo: number }", "class Foo {\n}\n") 776 expectPrintedTS(t, "class Foo { declare static foo: number }", "class Foo {\n}\n") 777 expectPrintedTS(t, "class Foo { declare public static foo: number }", "class Foo {\n}\n") 778 expectPrintedTS(t, "class Foo { public declare static foo: number }", "class Foo {\n}\n") 779 expectPrintedTS(t, "class Foo { public static declare foo: number }", "class Foo {\n}\n") 780 expectPrintedAssignSemanticsTS(t, "class Foo { override static foo: number }", "class Foo {\n}\n") 781 expectPrintedAssignSemanticsTS(t, "class Foo { override public static foo: number }", "class Foo {\n}\n") 782 expectPrintedAssignSemanticsTS(t, "class Foo { public override static foo: number }", "class Foo {\n}\n") 783 expectPrintedAssignSemanticsTS(t, "class Foo { public static override foo: number }", "class Foo {\n}\n") 784 expectPrintedTS(t, "class Foo { declare override public static foo: number }", "class Foo {\n}\n") 785 expectPrintedTS(t, "class Foo { declare static foo = 123 }", "class Foo {\n}\n") 786 expectPrintedTS(t, "class Foo { static declare foo = 123 }", "class Foo {\n}\n") 787 788 expectParseErrorTS(t, "class Foo { declare #foo }", "<stdin>: ERROR: \"declare\" cannot be used with a private identifier\n") 789 expectParseErrorTS(t, "class Foo { declare [foo: string]: number }", "<stdin>: ERROR: \"declare\" cannot be used with an index signature\n") 790 expectParseErrorTS(t, "class Foo { declare foo() }", "<stdin>: ERROR: \"declare\" cannot be used with a method\n") 791 expectParseErrorTS(t, "class Foo { declare get foo() }", "<stdin>: ERROR: \"declare\" cannot be used with a getter\n") 792 expectParseErrorTS(t, "class Foo { declare set foo(x) }", "<stdin>: ERROR: \"declare\" cannot be used with a setter\n") 793 794 expectParseErrorTS(t, "class Foo { declare static #foo }", "<stdin>: ERROR: \"declare\" cannot be used with a private identifier\n") 795 expectParseErrorTS(t, "class Foo { declare static [foo: string]: number }", "<stdin>: ERROR: \"declare\" cannot be used with an index signature\n") 796 expectParseErrorTS(t, "class Foo { declare static foo() }", "<stdin>: ERROR: \"declare\" cannot be used with a method\n") 797 expectParseErrorTS(t, "class Foo { declare static get foo() }", "<stdin>: ERROR: \"declare\" cannot be used with a getter\n") 798 expectParseErrorTS(t, "class Foo { declare static set foo(x) }", "<stdin>: ERROR: \"declare\" cannot be used with a setter\n") 799 800 expectParseErrorTS(t, "class Foo { static declare #foo }", "<stdin>: ERROR: \"declare\" cannot be used with a private identifier\n") 801 expectParseErrorTS(t, "class Foo { static declare [foo: string]: number }", "<stdin>: ERROR: \"declare\" cannot be used with an index signature\n") 802 expectParseErrorTS(t, "class Foo { static declare foo() }", "<stdin>: ERROR: \"declare\" cannot be used with a method\n") 803 expectParseErrorTS(t, "class Foo { static declare get foo() }", "<stdin>: ERROR: \"declare\" cannot be used with a getter\n") 804 expectParseErrorTS(t, "class Foo { static declare set foo(x) }", "<stdin>: ERROR: \"declare\" cannot be used with a setter\n") 805 806 expectPrintedAssignSemanticsTS(t, "class Foo { [key: string]: any\nfoo = 0 }", "class Foo {\n constructor() {\n this.foo = 0;\n }\n}\n") 807 expectPrintedAssignSemanticsTS(t, "class Foo { [key: string]: any; foo = 0 }", "class Foo {\n constructor() {\n this.foo = 0;\n }\n}\n") 808 809 expectParseErrorTS(t, "class Foo<> {}", "<stdin>: ERROR: Expected identifier but found \">\"\n") 810 expectParseErrorTS(t, "class Foo<,> {}", "<stdin>: ERROR: Expected identifier but found \",\"\n") 811 expectParseErrorTS(t, "class Foo<T><T> {}", "<stdin>: ERROR: Expected \"{\" but found \"<\"\n") 812 813 expectPrintedTS(t, "class Foo { foo<T>() {} }", "class Foo {\n foo() {\n }\n}\n") 814 expectPrintedTS(t, "class Foo { foo?<T>() {} }", "class Foo {\n foo() {\n }\n}\n") 815 expectPrintedTS(t, "class Foo { [foo]<T>() {} }", "class Foo {\n [foo]() {\n }\n}\n") 816 expectPrintedTS(t, "class Foo { [foo]?<T>() {} }", "class Foo {\n [foo]() {\n }\n}\n") 817 expectParseErrorTS(t, "class Foo { foo<T> }", "<stdin>: ERROR: Expected \"(\" but found \"}\"\n") 818 expectParseErrorTS(t, "class Foo { foo?<T> }", "<stdin>: ERROR: Expected \"(\" but found \"}\"\n") 819 expectParseErrorTS(t, "class Foo { foo!<T>() {} }", "<stdin>: ERROR: Expected \";\" but found \"<\"\n") 820 expectParseErrorTS(t, "class Foo { [foo]<T> }", "<stdin>: ERROR: Expected \"(\" but found \"}\"\n") 821 expectParseErrorTS(t, "class Foo { [foo]?<T> }", "<stdin>: ERROR: Expected \"(\" but found \"}\"\n") 822 expectParseErrorTS(t, "class Foo { [foo]!<T>() {} }", "<stdin>: ERROR: Expected \";\" but found \"<\"\n") 823 } 824 825 func TestTSAutoAccessors(t *testing.T) { 826 expectPrintedTS(t, "class Foo { accessor }", "class Foo {\n accessor;\n}\n") 827 expectPrintedTS(t, "class Foo { accessor x }", "class Foo {\n accessor x;\n}\n") 828 expectPrintedTS(t, "class Foo { accessor x? }", "class Foo {\n accessor x;\n}\n") 829 expectPrintedTS(t, "class Foo { accessor x! }", "class Foo {\n accessor x;\n}\n") 830 expectPrintedTS(t, "class Foo { accessor x = y }", "class Foo {\n accessor x = y;\n}\n") 831 expectPrintedTS(t, "class Foo { accessor x? = y }", "class Foo {\n accessor x = y;\n}\n") 832 expectPrintedTS(t, "class Foo { accessor x! = y }", "class Foo {\n accessor x = y;\n}\n") 833 expectPrintedTS(t, "class Foo { accessor x: any }", "class Foo {\n accessor x;\n}\n") 834 expectPrintedTS(t, "class Foo { accessor x?: any }", "class Foo {\n accessor x;\n}\n") 835 expectPrintedTS(t, "class Foo { accessor x!: any }", "class Foo {\n accessor x;\n}\n") 836 expectPrintedTS(t, "class Foo { accessor x: any = y }", "class Foo {\n accessor x = y;\n}\n") 837 expectPrintedTS(t, "class Foo { accessor x?: any = y }", "class Foo {\n accessor x = y;\n}\n") 838 expectPrintedTS(t, "class Foo { accessor x!: any = y }", "class Foo {\n accessor x = y;\n}\n") 839 expectPrintedTS(t, "class Foo { accessor [x] }", "class Foo {\n accessor [x];\n}\n") 840 expectPrintedTS(t, "class Foo { accessor [x]? }", "class Foo {\n accessor [x];\n}\n") 841 expectPrintedTS(t, "class Foo { accessor [x]! }", "class Foo {\n accessor [x];\n}\n") 842 expectPrintedTS(t, "class Foo { accessor [x] = y }", "class Foo {\n accessor [x] = y;\n}\n") 843 expectPrintedTS(t, "class Foo { accessor [x]? = y }", "class Foo {\n accessor [x] = y;\n}\n") 844 expectPrintedTS(t, "class Foo { accessor [x]! = y }", "class Foo {\n accessor [x] = y;\n}\n") 845 expectPrintedTS(t, "class Foo { accessor [x]: any }", "class Foo {\n accessor [x];\n}\n") 846 expectPrintedTS(t, "class Foo { accessor [x]?: any }", "class Foo {\n accessor [x];\n}\n") 847 expectPrintedTS(t, "class Foo { accessor [x]!: any }", "class Foo {\n accessor [x];\n}\n") 848 expectPrintedTS(t, "class Foo { accessor [x]: any = y }", "class Foo {\n accessor [x] = y;\n}\n") 849 expectPrintedTS(t, "class Foo { accessor [x]?: any = y }", "class Foo {\n accessor [x] = y;\n}\n") 850 expectPrintedTS(t, "class Foo { accessor [x]!: any = y }", "class Foo {\n accessor [x] = y;\n}\n") 851 852 expectParseErrorTS(t, "class Foo { accessor x<T> }", "<stdin>: ERROR: Expected \";\" but found \"<\"\n") 853 expectParseErrorTS(t, "class Foo { accessor x<T>() {} }", "<stdin>: ERROR: Expected \";\" but found \"<\"\n") 854 855 expectPrintedTS(t, "declare class Foo { accessor x }", "") 856 expectPrintedTS(t, "declare class Foo { accessor #x }", "") 857 expectPrintedTS(t, "declare class Foo { static accessor x }", "") 858 expectPrintedTS(t, "declare class Foo { static accessor #x }", "") 859 860 // TypeScript doesn't allow these combinations, but we shouldn't crash 861 expectPrintedTS(t, "class Foo { declare accessor x }", "class Foo {\n}\n") 862 expectPrintedTS(t, "class Foo { readonly accessor x }", "class Foo {\n accessor x;\n}\n") 863 expectPrintedTS(t, "interface Foo { accessor x }", "") 864 expectPrintedTS(t, "interface Foo { static accessor x }", "") 865 expectPrintedTS(t, "let x: { accessor x }", "let x;\n") 866 expectPrintedTS(t, "let x: { static accessor x }", "let x;\n") 867 expectParseErrorTS(t, "class Foo { accessor declare x }", "<stdin>: ERROR: Expected \";\" but found \"x\"\n") 868 expectParseErrorTS(t, "class Foo { accessor readonly x }", "<stdin>: ERROR: Expected \";\" but found \"x\"\n") 869 } 870 871 func TestTSPrivateIdentifiers(t *testing.T) { 872 // The TypeScript compiler still moves private field initializers into the 873 // constructor, but it has to leave the private field declaration in place so 874 // the private field is still declared. 875 expectPrintedTS(t, "class Foo { #foo }", "class Foo {\n #foo;\n}\n") 876 expectPrintedTS(t, "class Foo { #foo = 1 }", "class Foo {\n #foo = 1;\n}\n") 877 expectPrintedTS(t, "class Foo { #foo() {} }", "class Foo {\n #foo() {\n }\n}\n") 878 expectPrintedTS(t, "class Foo { get #foo() {} }", "class Foo {\n get #foo() {\n }\n}\n") 879 expectPrintedTS(t, "class Foo { set #foo(x) {} }", "class Foo {\n set #foo(x) {\n }\n}\n") 880 881 // The TypeScript compiler doesn't currently support static private fields 882 // because it moves static field initializers to after the class body and 883 // private fields can't be used outside the class body. It remains to be seen 884 // how the TypeScript compiler will transform private static fields once it 885 // finally does support them. For now just leave the initializer in place. 886 expectPrintedTS(t, "class Foo { static #foo }", "class Foo {\n static #foo;\n}\n") 887 expectPrintedTS(t, "class Foo { static #foo = 1 }", "class Foo {\n static #foo = 1;\n}\n") 888 expectPrintedTS(t, "class Foo { static #foo() {} }", "class Foo {\n static #foo() {\n }\n}\n") 889 expectPrintedTS(t, "class Foo { static get #foo() {} }", "class Foo {\n static get #foo() {\n }\n}\n") 890 expectPrintedTS(t, "class Foo { static set #foo(x) {} }", "class Foo {\n static set #foo(x) {\n }\n}\n") 891 892 // Decorators are not valid on private members 893 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec #foo }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 894 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec #foo = 1 }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 895 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec #foo() {} }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 896 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec get #foo() {} }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 897 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec set #foo(x) {x} }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 898 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec accessor #foo }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 899 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static #foo }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 900 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static #foo = 1 }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 901 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static #foo() {} }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 902 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static get #foo() {} }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 903 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static set #foo(x) {x} }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 904 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static accessor #foo }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 905 906 // Decorators are now able to access private names, since the TypeScript 907 // compiler was changed to move them into a "static {}" block within the 908 // class body: https://github.com/microsoft/TypeScript/pull/50074 909 expectParseErrorExperimentalDecoratorTS(t, "class Foo { static #foo; @dec(Foo.#foo) bar }", "") 910 expectParseErrorExperimentalDecoratorTS(t, "class Foo { static #foo; @dec(Foo.#foo) bar() {} }", "") 911 expectParseErrorExperimentalDecoratorTS(t, "class Foo { static #foo; bar(@dec(Foo.#foo) x) {} }", "") 912 } 913 914 func TestTSInterface(t *testing.T) { 915 expectPrintedTS(t, "interface\nA\n{ a }", "interface;\nA;\n{\n a;\n}\n") 916 917 expectPrintedTS(t, "interface A { a } x", "x;\n") 918 expectPrintedTS(t, "interface A { a; b } x", "x;\n") 919 expectPrintedTS(t, "interface A { a() } x", "x;\n") 920 expectPrintedTS(t, "interface A { a(); b } x", "x;\n") 921 expectPrintedTS(t, "interface Foo { foo(): Foo \n is: Bar } x", "x;\n") 922 expectPrintedTS(t, "interface A<T extends number> extends B.C<D, E>, F.G<H, I> {} x", "x;\n") 923 expectPrintedTS(t, "export interface A<T extends number> extends B.C<D, E>, F.G<H, I> {} x", "x;\n") 924 expectPrintedTS(t, "export default interface Foo {} x", "x;\n") 925 expectParseErrorTS(t, "export default interface + x", 926 "<stdin>: ERROR: \"interface\" is a reserved word and cannot be used in an ECMAScript module\n"+ 927 "<stdin>: NOTE: This file is considered to be an ECMAScript module because of the \"export\" keyword here:\n") 928 929 // Check ASI for "interface" 930 expectPrintedTS(t, "interface\nFoo\n{}", "interface;\nFoo;\n{\n}\n") 931 expectPrintedTS(t, "export default interface\nFoo {} x", "x;\n") 932 expectPrintedTS(t, "export default interface\nFoo\n{} x", "x;\n") 933 expectParseErrorTS(t, "interface\nFoo {}", "<stdin>: ERROR: Expected \";\" but found \"{\"\n") 934 expectParseErrorTS(t, "export interface\nFoo {}", "<stdin>: ERROR: Unexpected \"interface\"\n") 935 expectParseErrorTS(t, "export interface\nFoo\n{}", "<stdin>: ERROR: Unexpected \"interface\"\n") 936 expectParseErrorTS(t, "export default interface\nFoo", "<stdin>: ERROR: Expected \"{\" but found end of file\n") 937 } 938 939 func TestTSNamespace(t *testing.T) { 940 expectPrintedTS(t, "namespace\nx\n{ var y }", "namespace;\nx;\n{\n var y;\n}\n") 941 942 // Check ES5 emit 943 expectPrintedTargetTS(t, 5, "namespace x { export var y = 1 }", "var x;\n(function(x) {\n x.y = 1;\n})(x || (x = {}));\n") 944 expectPrintedTargetTS(t, 2015, "namespace x { export var y = 1 }", "var x;\n((x) => {\n x.y = 1;\n})(x || (x = {}));\n") 945 946 // Certain syntax isn't allowed inside a namespace block 947 expectParseErrorTS(t, "namespace x { return }", "<stdin>: ERROR: A return statement cannot be used here:\n") 948 expectParseErrorTS(t, "namespace x { await 1 }", "<stdin>: ERROR: \"await\" can only be used inside an \"async\" function\n") 949 expectParseErrorTS(t, "namespace x { if (y) return }", "<stdin>: ERROR: A return statement cannot be used here:\n") 950 expectParseErrorTS(t, "namespace x { if (y) await 1 }", "<stdin>: ERROR: \"await\" can only be used inside an \"async\" function\n") 951 expectParseErrorTS(t, "namespace x { this }", "<stdin>: ERROR: Cannot use \"this\" here:\n") 952 expectParseErrorTS(t, "namespace x { () => this }", "<stdin>: ERROR: Cannot use \"this\" here:\n") 953 expectParseErrorTS(t, "namespace x { class y { [this] } }", "<stdin>: ERROR: Cannot use \"this\" here:\n") 954 expectParseErrorTS(t, "namespace x { (function() { this }) }", "") 955 expectParseErrorTS(t, "namespace x { function y() { this } }", "") 956 expectParseErrorTS(t, "namespace x { class y { x = this } }", "") 957 expectParseErrorTS(t, "export namespace x { export let yield = 1 }", 958 "<stdin>: ERROR: \"yield\" is a reserved word and cannot be used in an ECMAScript module\n"+ 959 "<stdin>: NOTE: This file is considered to be an ECMAScript module because of the \"export\" keyword here:\n") 960 expectPrintedTS(t, "namespace x { export let await = 1, y = await }", `var x; 961 ((x) => { 962 x.await = 1; 963 x.y = x.await; 964 })(x || (x = {})); 965 `) 966 expectPrintedTS(t, "namespace x { export let yield = 1, y = yield }", `var x; 967 ((x) => { 968 x.yield = 1; 969 x.y = x.yield; 970 })(x || (x = {})); 971 `) 972 973 expectPrintedTS(t, "namespace Foo { 0 }", `var Foo; 974 ((Foo) => { 975 0; 976 })(Foo || (Foo = {})); 977 `) 978 expectPrintedTS(t, "export namespace Foo { 0 }", `export var Foo; 979 ((Foo) => { 980 0; 981 })(Foo || (Foo = {})); 982 `) 983 984 // Namespaces should introduce a scope that prevents name collisions 985 expectPrintedTS(t, "namespace Foo { let x } let x", `var Foo; 986 ((Foo) => { 987 let x; 988 })(Foo || (Foo = {})); 989 let x; 990 `) 991 992 // Exports in namespaces shouldn't collide with module exports 993 expectPrintedTS(t, "namespace Foo { export let x } export let x", `var Foo; 994 ((Foo) => { 995 })(Foo || (Foo = {})); 996 export let x; 997 `) 998 expectPrintedTS(t, "declare namespace Foo { export let x } namespace x { 0 }", `var x; 999 ((x) => { 1000 0; 1001 })(x || (x = {})); 1002 `) 1003 1004 errorText := `<stdin>: ERROR: The symbol "foo" has already been declared 1005 <stdin>: NOTE: The symbol "foo" was originally declared here: 1006 ` 1007 1008 // Namespaces with values are not allowed to merge 1009 expectParseErrorTS(t, "var foo; namespace foo { 0 }", errorText) 1010 expectParseErrorTS(t, "let foo; namespace foo { 0 }", errorText) 1011 expectParseErrorTS(t, "const foo = 0; namespace foo { 0 }", errorText) 1012 expectParseErrorTS(t, "namespace foo { 0 } var foo", errorText) 1013 expectParseErrorTS(t, "namespace foo { 0 } let foo", errorText) 1014 expectParseErrorTS(t, "namespace foo { 0 } const foo = 0", errorText) 1015 1016 // Namespaces without values are allowed to merge 1017 expectPrintedTS(t, "var foo; namespace foo {}", "var foo;\n") 1018 expectPrintedTS(t, "let foo; namespace foo {}", "let foo;\n") 1019 expectPrintedTS(t, "const foo = 0; namespace foo {}", "const foo = 0;\n") 1020 expectPrintedTS(t, "namespace foo {} var foo", "var foo;\n") 1021 expectPrintedTS(t, "namespace foo {} let foo", "let foo;\n") 1022 expectPrintedTS(t, "namespace foo {} const foo = 0", "const foo = 0;\n") 1023 1024 // Namespaces with types but no values are allowed to merge 1025 expectPrintedTS(t, "var foo; namespace foo { export type bar = number }", "var foo;\n") 1026 expectPrintedTS(t, "let foo; namespace foo { export type bar = number }", "let foo;\n") 1027 expectPrintedTS(t, "const foo = 0; namespace foo { export type bar = number }", "const foo = 0;\n") 1028 expectPrintedTS(t, "namespace foo { export type bar = number } var foo", "var foo;\n") 1029 expectPrintedTS(t, "namespace foo { export type bar = number } let foo", "let foo;\n") 1030 expectPrintedTS(t, "namespace foo { export type bar = number } const foo = 0", "const foo = 0;\n") 1031 1032 // Namespaces are allowed to merge with certain symbols 1033 expectPrintedTS(t, "function foo() {} namespace foo { 0 }", `function foo() { 1034 } 1035 ((foo) => { 1036 0; 1037 })(foo || (foo = {})); 1038 `) 1039 expectPrintedTS(t, "function* foo() {} namespace foo { 0 }", `function* foo() { 1040 } 1041 ((foo) => { 1042 0; 1043 })(foo || (foo = {})); 1044 `) 1045 expectPrintedTS(t, "async function foo() {} namespace foo { 0 }", `async function foo() { 1046 } 1047 ((foo) => { 1048 0; 1049 })(foo || (foo = {})); 1050 `) 1051 expectPrintedTS(t, "class foo {} namespace foo { 0 }", `class foo { 1052 } 1053 ((foo) => { 1054 0; 1055 })(foo || (foo = {})); 1056 `) 1057 expectPrintedTS(t, "enum foo { a } namespace foo { 0 }", `var foo = /* @__PURE__ */ ((foo) => { 1058 foo[foo["a"] = 0] = "a"; 1059 return foo; 1060 })(foo || {}); 1061 ((foo) => { 1062 0; 1063 })(foo || (foo = {})); 1064 `) 1065 expectPrintedTS(t, "namespace foo {} namespace foo { 0 }", `var foo; 1066 ((foo) => { 1067 0; 1068 })(foo || (foo = {})); 1069 `) 1070 expectParseErrorTS(t, "namespace foo { 0 } function foo() {}", errorText) 1071 expectParseErrorTS(t, "namespace foo { 0 } function* foo() {}", errorText) 1072 expectParseErrorTS(t, "namespace foo { 0 } async function foo() {}", errorText) 1073 expectParseErrorTS(t, "namespace foo { 0 } class foo {}", errorText) 1074 expectPrintedTS(t, "namespace foo { 0 } enum foo { a }", `((foo) => { 1075 0; 1076 })(foo || (foo = {})); 1077 var foo = /* @__PURE__ */ ((foo) => { 1078 foo[foo["a"] = 0] = "a"; 1079 return foo; 1080 })(foo || {}); 1081 `) 1082 expectPrintedTS(t, "namespace foo { 0 } namespace foo {}", `var foo; 1083 ((foo) => { 1084 0; 1085 })(foo || (foo = {})); 1086 `) 1087 expectPrintedTS(t, "namespace foo { 0 } namespace foo { 0 }", `var foo; 1088 ((foo) => { 1089 0; 1090 })(foo || (foo = {})); 1091 ((foo) => { 1092 0; 1093 })(foo || (foo = {})); 1094 `) 1095 expectPrintedTS(t, "function foo() {} namespace foo { 0 } function foo() {}", `function foo() { 1096 } 1097 ((foo) => { 1098 0; 1099 })(foo || (foo = {})); 1100 function foo() { 1101 } 1102 `) 1103 expectPrintedTS(t, "function* foo() {} namespace foo { 0 } function* foo() {}", `function* foo() { 1104 } 1105 ((foo) => { 1106 0; 1107 })(foo || (foo = {})); 1108 function* foo() { 1109 } 1110 `) 1111 expectPrintedTS(t, "async function foo() {} namespace foo { 0 } async function foo() {}", `async function foo() { 1112 } 1113 ((foo) => { 1114 0; 1115 })(foo || (foo = {})); 1116 async function foo() { 1117 } 1118 `) 1119 1120 // Namespace merging shouldn't allow for other merging 1121 expectParseErrorTS(t, "class foo {} namespace foo { 0 } class foo {}", errorText) 1122 expectParseErrorTS(t, "class foo {} namespace foo { 0 } enum foo {}", errorText) 1123 expectParseErrorTS(t, "enum foo {} namespace foo { 0 } class foo {}", errorText) 1124 expectParseErrorTS(t, "namespace foo { 0 } namespace foo { 0 } let foo", errorText) 1125 expectParseErrorTS(t, "namespace foo { 0 } enum foo {} class foo {}", errorText) 1126 1127 // Test dot nested namespace syntax 1128 expectPrintedTS(t, "namespace foo.bar { foo(bar) }", `var foo; 1129 ((foo) => { 1130 let bar; 1131 ((bar) => { 1132 foo(bar); 1133 })(bar = foo.bar || (foo.bar = {})); 1134 })(foo || (foo = {})); 1135 `) 1136 1137 // "module" is a deprecated alias for "namespace" 1138 expectPrintedTS(t, "module foo { export namespace bar { foo(bar) } }", `var foo; 1139 ((foo) => { 1140 let bar; 1141 ((bar) => { 1142 foo(bar); 1143 })(bar = foo.bar || (foo.bar = {})); 1144 })(foo || (foo = {})); 1145 `) 1146 expectPrintedTS(t, "namespace foo { export module bar { foo(bar) } }", `var foo; 1147 ((foo) => { 1148 let bar; 1149 ((bar) => { 1150 foo(bar); 1151 })(bar = foo.bar || (foo.bar = {})); 1152 })(foo || (foo = {})); 1153 `) 1154 expectPrintedTS(t, "module foo.bar { foo(bar) }", `var foo; 1155 ((foo) => { 1156 let bar; 1157 ((bar) => { 1158 foo(bar); 1159 })(bar = foo.bar || (foo.bar = {})); 1160 })(foo || (foo = {})); 1161 `) 1162 } 1163 1164 func TestTSNamespaceExports(t *testing.T) { 1165 expectPrintedTS(t, ` 1166 namespace A { 1167 export namespace B { 1168 export function fn() {} 1169 } 1170 namespace C { 1171 export function fn() {} 1172 } 1173 namespace D { 1174 function fn() {} 1175 } 1176 } 1177 `, `var A; 1178 ((A) => { 1179 let B; 1180 ((B) => { 1181 function fn() { 1182 } 1183 B.fn = fn; 1184 })(B = A.B || (A.B = {})); 1185 let C; 1186 ((C) => { 1187 function fn() { 1188 } 1189 C.fn = fn; 1190 })(C || (C = {})); 1191 let D; 1192 ((D) => { 1193 function fn() { 1194 } 1195 })(D || (D = {})); 1196 })(A || (A = {})); 1197 `) 1198 1199 expectPrintedTS(t, ` 1200 namespace A { 1201 export namespace B { 1202 export class Class {} 1203 } 1204 namespace C { 1205 export class Class {} 1206 } 1207 namespace D { 1208 class Class {} 1209 } 1210 } 1211 `, `var A; 1212 ((A) => { 1213 let B; 1214 ((B) => { 1215 class Class { 1216 } 1217 B.Class = Class; 1218 })(B = A.B || (A.B = {})); 1219 let C; 1220 ((C) => { 1221 class Class { 1222 } 1223 C.Class = Class; 1224 })(C || (C = {})); 1225 let D; 1226 ((D) => { 1227 class Class { 1228 } 1229 })(D || (D = {})); 1230 })(A || (A = {})); 1231 `) 1232 1233 expectPrintedTS(t, ` 1234 namespace A { 1235 export namespace B { 1236 export enum Enum {} 1237 } 1238 namespace C { 1239 export enum Enum {} 1240 } 1241 namespace D { 1242 enum Enum {} 1243 } 1244 } 1245 `, `var A; 1246 ((A) => { 1247 let B; 1248 ((B) => { 1249 let Enum; 1250 ((Enum) => { 1251 })(Enum = B.Enum || (B.Enum = {})); 1252 })(B = A.B || (A.B = {})); 1253 let C; 1254 ((C) => { 1255 let Enum; 1256 ((Enum) => { 1257 })(Enum = C.Enum || (C.Enum = {})); 1258 })(C || (C = {})); 1259 let D; 1260 ((D) => { 1261 let Enum; 1262 ((Enum) => { 1263 })(Enum || (Enum = {})); 1264 })(D || (D = {})); 1265 })(A || (A = {})); 1266 `) 1267 1268 expectPrintedTS(t, ` 1269 namespace A { 1270 export namespace B { 1271 export let foo = 1 1272 foo += foo 1273 } 1274 namespace C { 1275 export let foo = 1 1276 foo += foo 1277 } 1278 namespace D { 1279 let foo = 1 1280 foo += foo 1281 } 1282 } 1283 `, `var A; 1284 ((A) => { 1285 let B; 1286 ((B) => { 1287 B.foo = 1; 1288 B.foo += B.foo; 1289 })(B = A.B || (A.B = {})); 1290 let C; 1291 ((C) => { 1292 C.foo = 1; 1293 C.foo += C.foo; 1294 })(C || (C = {})); 1295 let D; 1296 ((D) => { 1297 let foo = 1; 1298 foo += foo; 1299 })(D || (D = {})); 1300 })(A || (A = {})); 1301 `) 1302 1303 expectPrintedTS(t, ` 1304 namespace A { 1305 export namespace B { 1306 export const foo = 1 1307 } 1308 namespace C { 1309 export const foo = 1 1310 } 1311 namespace D { 1312 const foo = 1 1313 } 1314 } 1315 `, `var A; 1316 ((A) => { 1317 let B; 1318 ((B) => { 1319 B.foo = 1; 1320 })(B = A.B || (A.B = {})); 1321 let C; 1322 ((C) => { 1323 C.foo = 1; 1324 })(C || (C = {})); 1325 let D; 1326 ((D) => { 1327 const foo = 1; 1328 })(D || (D = {})); 1329 })(A || (A = {})); 1330 `) 1331 1332 expectPrintedTS(t, ` 1333 namespace A { 1334 export namespace B { 1335 export var foo = 1 1336 foo += foo 1337 } 1338 namespace C { 1339 export var foo = 1 1340 foo += foo 1341 } 1342 namespace D { 1343 var foo = 1 1344 foo += foo 1345 } 1346 } 1347 `, `var A; 1348 ((A) => { 1349 let B; 1350 ((B) => { 1351 B.foo = 1; 1352 B.foo += B.foo; 1353 })(B = A.B || (A.B = {})); 1354 let C; 1355 ((C) => { 1356 C.foo = 1; 1357 C.foo += C.foo; 1358 })(C || (C = {})); 1359 let D; 1360 ((D) => { 1361 var foo = 1; 1362 foo += foo; 1363 })(D || (D = {})); 1364 })(A || (A = {})); 1365 `) 1366 1367 expectPrintedTS(t, ` 1368 namespace ns { 1369 export declare const L1 1370 console.log(L1) 1371 1372 export declare let [[L2 = x, { [y]: L3 }]] 1373 console.log(L2, L3) 1374 1375 export declare function F() 1376 console.log(F) 1377 1378 export declare function F2() { } 1379 console.log(F2) 1380 1381 export declare class C { } 1382 console.log(C) 1383 1384 export declare enum E { } 1385 console.log(E) 1386 1387 export declare namespace N { } 1388 console.log(N) 1389 } 1390 `, `var ns; 1391 ((ns) => { 1392 console.log(ns.L1); 1393 console.log(ns.L2, ns.L3); 1394 console.log(F); 1395 console.log(F2); 1396 console.log(C); 1397 console.log(E); 1398 console.log(N); 1399 })(ns || (ns = {})); 1400 `) 1401 1402 expectPrintedTS(t, ` 1403 namespace a { export var a = 123; log(a) } 1404 namespace b { export let b = 123; log(b) } 1405 namespace c { export enum c {} log(c) } 1406 namespace d { export class d {} log(d) } 1407 namespace e { export namespace e {} log(e) } 1408 namespace f { export function f() {} log(f) } 1409 `, `var a; 1410 ((_a) => { 1411 _a.a = 123; 1412 log(_a.a); 1413 })(a || (a = {})); 1414 var b; 1415 ((_b) => { 1416 _b.b = 123; 1417 log(_b.b); 1418 })(b || (b = {})); 1419 var c; 1420 ((_c) => { 1421 let c; 1422 ((c) => { 1423 })(c = _c.c || (_c.c = {})); 1424 log(c); 1425 })(c || (c = {})); 1426 var d; 1427 ((_d) => { 1428 class d { 1429 } 1430 _d.d = d; 1431 log(d); 1432 })(d || (d = {})); 1433 var e; 1434 ((e) => { 1435 log(e); 1436 })(e || (e = {})); 1437 var f; 1438 ((_f) => { 1439 function f() { 1440 } 1441 _f.f = f; 1442 log(f); 1443 })(f || (f = {})); 1444 `) 1445 1446 expectPrintedTS(t, ` 1447 namespace a { export declare var a } 1448 namespace b { export declare let b } 1449 namespace c { export declare enum c {} } 1450 namespace d { export declare class d {} } 1451 namespace e { export declare namespace e {} } 1452 namespace f { export declare function f() {} } 1453 `, `var a; 1454 ((_a) => { 1455 })(a || (a = {})); 1456 var b; 1457 ((_b) => { 1458 })(b || (b = {})); 1459 var c; 1460 ((c) => { 1461 })(c || (c = {})); 1462 var d; 1463 ((d) => { 1464 })(d || (d = {})); 1465 var f; 1466 ((f) => { 1467 })(f || (f = {})); 1468 `) 1469 } 1470 1471 func TestTSNamespaceDestructuring(t *testing.T) { 1472 expectPrintedTS(t, ` 1473 namespace A { 1474 export var [ 1475 a, 1476 [, b = c, ...d], 1477 {[x]: [[y]] = z, ...o}, 1478 ] = ref 1479 } 1480 `, `var A; 1481 ((A) => { 1482 [ 1483 A.a, 1484 [, A.b = c, ...A.d], 1485 { [x]: [[A.y]] = z, ...A.o } 1486 ] = ref; 1487 })(A || (A = {})); 1488 `) 1489 } 1490 1491 func TestTSEnum(t *testing.T) { 1492 expectParseErrorTS(t, "enum x { y z }", "<stdin>: ERROR: Expected \",\" after \"y\" in enum\n") 1493 expectParseErrorTS(t, "enum x { 'y' 'z' }", "<stdin>: ERROR: Expected \",\" after \"y\" in enum\n") 1494 expectParseErrorTS(t, "enum x { y = 0 z }", "<stdin>: ERROR: Expected \",\" before \"z\" in enum\n") 1495 expectParseErrorTS(t, "enum x { 'y' = 0 'z' }", "<stdin>: ERROR: Expected \",\" before \"z\" in enum\n") 1496 1497 // Check ES5 emit 1498 expectPrintedTargetTS(t, 5, "enum x { y = 1 }", "var x = /* @__PURE__ */ function(x) {\n x[x[\"y\"] = 1] = \"y\";\n return x;\n}(x || {});\n") 1499 expectPrintedTargetTS(t, 2015, "enum x { y = 1 }", "var x = /* @__PURE__ */ ((x) => {\n x[x[\"y\"] = 1] = \"y\";\n return x;\n})(x || {});\n") 1500 1501 // Certain syntax isn't allowed inside an enum block 1502 expectParseErrorTS(t, "enum x { y = this }", "<stdin>: ERROR: Cannot use \"this\" here:\n") 1503 expectParseErrorTS(t, "enum x { y = () => this }", "<stdin>: ERROR: Cannot use \"this\" here:\n") 1504 expectParseErrorTS(t, "enum x { y = function() { this } }", "") 1505 1506 expectPrintedTS(t, "enum Foo { A, B }", `var Foo = /* @__PURE__ */ ((Foo) => { 1507 Foo[Foo["A"] = 0] = "A"; 1508 Foo[Foo["B"] = 1] = "B"; 1509 return Foo; 1510 })(Foo || {}); 1511 `) 1512 expectPrintedTS(t, "export enum Foo { A; B }", `export var Foo = /* @__PURE__ */ ((Foo) => { 1513 Foo[Foo["A"] = 0] = "A"; 1514 Foo[Foo["B"] = 1] = "B"; 1515 return Foo; 1516 })(Foo || {}); 1517 `) 1518 expectPrintedTS(t, "enum Foo { A, B, C = 3.3, D, E }", `var Foo = /* @__PURE__ */ ((Foo) => { 1519 Foo[Foo["A"] = 0] = "A"; 1520 Foo[Foo["B"] = 1] = "B"; 1521 Foo[Foo["C"] = 3.3] = "C"; 1522 Foo[Foo["D"] = 4.3] = "D"; 1523 Foo[Foo["E"] = 5.3] = "E"; 1524 return Foo; 1525 })(Foo || {}); 1526 `) 1527 expectPrintedTS(t, "enum Foo { A, B, C = 'x', D, E, F = `y`, G = `${z}`, H = tag`` }", `var Foo = ((Foo) => { 1528 Foo[Foo["A"] = 0] = "A"; 1529 Foo[Foo["B"] = 1] = "B"; 1530 Foo["C"] = "x"; 1531 Foo[Foo["D"] = void 0] = "D"; 1532 Foo[Foo["E"] = void 0] = "E"; 1533 Foo["F"] = `+"`y`"+`; 1534 Foo["G"] = `+"`${z}`"+`; 1535 Foo[Foo["H"] = tag`+"``"+`] = "H"; 1536 return Foo; 1537 })(Foo || {}); 1538 `) 1539 1540 // TypeScript allows splitting an enum into multiple blocks 1541 expectPrintedTS(t, "enum Foo { A = 1 } enum Foo { B = 2 }", `var Foo = /* @__PURE__ */ ((Foo) => { 1542 Foo[Foo["A"] = 1] = "A"; 1543 return Foo; 1544 })(Foo || {}); 1545 var Foo = /* @__PURE__ */ ((Foo) => { 1546 Foo[Foo["B"] = 2] = "B"; 1547 return Foo; 1548 })(Foo || {}); 1549 `) 1550 1551 expectPrintedTS(t, ` 1552 enum Foo { 1553 'a' = 10.01, 1554 'a b' = 100, 1555 c = a + Foo.a + Foo['a b'], 1556 d, 1557 e = a + Foo.a + Foo['a b'] + Math.random(), 1558 f, 1559 } 1560 enum Bar { 1561 a = Foo.a 1562 } 1563 `, `var Foo = ((Foo) => { 1564 Foo[Foo["a"] = 10.01] = "a"; 1565 Foo[Foo["a b"] = 100] = "a b"; 1566 Foo[Foo["c"] = 120.02] = "c"; 1567 Foo[Foo["d"] = 121.02] = "d"; 1568 Foo[Foo["e"] = 120.02 + Math.random()] = "e"; 1569 Foo[Foo["f"] = void 0] = "f"; 1570 return Foo; 1571 })(Foo || {}); 1572 var Bar = /* @__PURE__ */ ((Bar) => { 1573 Bar[Bar["a"] = 10.01 /* a */] = "a"; 1574 return Bar; 1575 })(Bar || {}); 1576 `) 1577 1578 expectPrintedTS(t, ` 1579 enum Foo { A } 1580 x = [Foo.A, Foo?.A, Foo?.A()] 1581 y = [Foo['A'], Foo?.['A'], Foo?.['A']()] 1582 `, `var Foo = /* @__PURE__ */ ((Foo) => { 1583 Foo[Foo["A"] = 0] = "A"; 1584 return Foo; 1585 })(Foo || {}); 1586 x = [0 /* A */, Foo?.A, Foo?.A()]; 1587 y = [0 /* A */, Foo?.["A"], Foo?.["A"]()]; 1588 `) 1589 1590 // Check shadowing 1591 expectPrintedTS(t, "enum Foo { Foo }", `var Foo = /* @__PURE__ */ ((_Foo) => { 1592 _Foo[_Foo["Foo"] = 0] = "Foo"; 1593 return _Foo; 1594 })(Foo || {}); 1595 `) 1596 expectPrintedTS(t, "enum Foo { Bar = Foo }", `var Foo = /* @__PURE__ */ ((Foo) => { 1597 Foo[Foo["Bar"] = Foo] = "Bar"; 1598 return Foo; 1599 })(Foo || {}); 1600 `) 1601 expectPrintedTS(t, "enum Foo { Foo = 1, Bar = Foo }", `var Foo = /* @__PURE__ */ ((_Foo) => { 1602 _Foo[_Foo["Foo"] = 1] = "Foo"; 1603 _Foo[_Foo["Bar"] = 1 /* Foo */] = "Bar"; 1604 return _Foo; 1605 })(Foo || {}); 1606 `) 1607 1608 // Check top-level "var" and nested "let" 1609 expectPrintedTS(t, "enum a { b = 1 }", "var a = /* @__PURE__ */ ((a) => {\n a[a[\"b\"] = 1] = \"b\";\n return a;\n})(a || {});\n") 1610 expectPrintedTS(t, "{ enum a { b = 1 } }", "{\n let a;\n ((a) => {\n a[a[\"b\"] = 1] = \"b\";\n })(a || (a = {}));\n}\n") 1611 1612 // Check "await" and "yield" 1613 expectPrintedTS(t, "enum x { await = 1, y = await }", `var x = /* @__PURE__ */ ((x) => { 1614 x[x["await"] = 1] = "await"; 1615 x[x["y"] = 1 /* await */] = "y"; 1616 return x; 1617 })(x || {}); 1618 `) 1619 expectPrintedTS(t, "enum x { yield = 1, y = yield }", `var x = /* @__PURE__ */ ((x) => { 1620 x[x["yield"] = 1] = "yield"; 1621 x[x["y"] = 1 /* yield */] = "y"; 1622 return x; 1623 })(x || {}); 1624 `) 1625 expectParseErrorTS(t, "enum x { y = await 1 }", "<stdin>: ERROR: \"await\" can only be used inside an \"async\" function\n") 1626 expectParseErrorTS(t, "function *f() { enum x { y = yield 1 } }", "<stdin>: ERROR: Cannot use \"yield\" outside a generator function\n") 1627 expectParseErrorTS(t, "async function f() { enum x { y = await 1 } }", "<stdin>: ERROR: \"await\" can only be used inside an \"async\" function\n") 1628 expectParseErrorTS(t, "export enum x { yield = 1, y = yield }", 1629 "<stdin>: ERROR: \"yield\" is a reserved word and cannot be used in an ECMAScript module\n"+ 1630 "<stdin>: NOTE: This file is considered to be an ECMAScript module because of the \"export\" keyword here:\n") 1631 1632 // Check enum use before declaration 1633 expectPrintedTS(t, "foo = Foo.FOO; enum Foo { FOO } bar = Foo.FOO", `foo = 0 /* FOO */; 1634 var Foo = /* @__PURE__ */ ((Foo) => { 1635 Foo[Foo["FOO"] = 0] = "FOO"; 1636 return Foo; 1637 })(Foo || {}); 1638 bar = 0 /* FOO */; 1639 `) 1640 1641 // https://github.com/evanw/esbuild/issues/3205 1642 expectPrintedTS(t, "(() => { const enum Foo { A } () => Foo.A })", `() => { 1643 let Foo; 1644 ((Foo) => { 1645 Foo[Foo["A"] = 0] = "A"; 1646 })(Foo || (Foo = {})); 1647 () => 0 /* A */; 1648 }; 1649 `) 1650 } 1651 1652 func TestTSEnumConstantFolding(t *testing.T) { 1653 expectPrintedTS(t, ` 1654 enum Foo { 1655 add = 1 + 2, 1656 sub = -1 - 2, 1657 mul = 10 * 20, 1658 1659 div_pos_inf = 1 / 0, 1660 div_neg_inf = 1 / -0, 1661 div_nan = 0 / 0, 1662 div_neg_zero = 1 / (1 / -0), 1663 1664 div0 = 10 / 20, 1665 div1 = 10 / -20, 1666 div2 = -10 / 20, 1667 div3 = -10 / -20, 1668 1669 mod0 = 123 % 100, 1670 mod1 = 123 % -100, 1671 mod2 = -123 % 100, 1672 mod3 = -123 % -100, 1673 1674 fmod0 = 1.375 % 0.75, 1675 fmod1 = 1.375 % -0.75, 1676 fmod2 = -1.375 % 0.75, 1677 fmod3 = -1.375 % -0.75, 1678 1679 pow0 = 2.25 ** 3, 1680 pow1 = 2.25 ** -3, 1681 pow2 = (-2.25) ** 3, 1682 pow3 = (-2.25) ** -3, 1683 } 1684 `, `var Foo = /* @__PURE__ */ ((Foo) => { 1685 Foo[Foo["add"] = 3] = "add"; 1686 Foo[Foo["sub"] = -3] = "sub"; 1687 Foo[Foo["mul"] = 200] = "mul"; 1688 Foo[Foo["div_pos_inf"] = Infinity] = "div_pos_inf"; 1689 Foo[Foo["div_neg_inf"] = -Infinity] = "div_neg_inf"; 1690 Foo[Foo["div_nan"] = NaN] = "div_nan"; 1691 Foo[Foo["div_neg_zero"] = -0] = "div_neg_zero"; 1692 Foo[Foo["div0"] = 0.5] = "div0"; 1693 Foo[Foo["div1"] = -0.5] = "div1"; 1694 Foo[Foo["div2"] = -0.5] = "div2"; 1695 Foo[Foo["div3"] = 0.5] = "div3"; 1696 Foo[Foo["mod0"] = 23] = "mod0"; 1697 Foo[Foo["mod1"] = 23] = "mod1"; 1698 Foo[Foo["mod2"] = -23] = "mod2"; 1699 Foo[Foo["mod3"] = -23] = "mod3"; 1700 Foo[Foo["fmod0"] = 0.625] = "fmod0"; 1701 Foo[Foo["fmod1"] = 0.625] = "fmod1"; 1702 Foo[Foo["fmod2"] = -0.625] = "fmod2"; 1703 Foo[Foo["fmod3"] = -0.625] = "fmod3"; 1704 Foo[Foo["pow0"] = 11.390625] = "pow0"; 1705 Foo[Foo["pow1"] = 0.0877914951989026] = "pow1"; 1706 Foo[Foo["pow2"] = -11.390625] = "pow2"; 1707 Foo[Foo["pow3"] = -0.0877914951989026] = "pow3"; 1708 return Foo; 1709 })(Foo || {}); 1710 `) 1711 1712 expectPrintedTS(t, ` 1713 enum Foo { 1714 pos = +54321012345, 1715 neg = -54321012345, 1716 cpl = ~54321012345, 1717 1718 shl0 = 987654321 << 2, 1719 shl1 = 987654321 << 31, 1720 shl2 = 987654321 << 34, 1721 1722 shr0 = -987654321 >> 2, 1723 shr1 = -987654321 >> 31, 1724 shr2 = -987654321 >> 34, 1725 1726 ushr0 = -987654321 >>> 2, 1727 ushr1 = -987654321 >>> 31, 1728 ushr2 = -987654321 >>> 34, 1729 1730 bitand = 0xDEADF00D & 0xBADCAFE, 1731 bitor = 0xDEADF00D | 0xBADCAFE, 1732 bitxor = 0xDEADF00D ^ 0xBADCAFE, 1733 } 1734 `, `var Foo = /* @__PURE__ */ ((Foo) => { 1735 Foo[Foo["pos"] = 54321012345] = "pos"; 1736 Foo[Foo["neg"] = -54321012345] = "neg"; 1737 Foo[Foo["cpl"] = 1513562502] = "cpl"; 1738 Foo[Foo["shl0"] = -344350012] = "shl0"; 1739 Foo[Foo["shl1"] = -2147483648] = "shl1"; 1740 Foo[Foo["shl2"] = -344350012] = "shl2"; 1741 Foo[Foo["shr0"] = -246913581] = "shr0"; 1742 Foo[Foo["shr1"] = -1] = "shr1"; 1743 Foo[Foo["shr2"] = -246913581] = "shr2"; 1744 Foo[Foo["ushr0"] = 826828243] = "ushr0"; 1745 Foo[Foo["ushr1"] = 1] = "ushr1"; 1746 Foo[Foo["ushr2"] = 826828243] = "ushr2"; 1747 Foo[Foo["bitand"] = 179159052] = "bitand"; 1748 Foo[Foo["bitor"] = -542246145] = "bitor"; 1749 Foo[Foo["bitxor"] = -721405197] = "bitxor"; 1750 return Foo; 1751 })(Foo || {}); 1752 `) 1753 } 1754 1755 func TestTSFunction(t *testing.T) { 1756 expectPrintedTS(t, "function foo(): void; function foo(): void {}", "function foo() {\n}\n") 1757 1758 expectPrintedTS(t, "function foo<A>() {}", "function foo() {\n}\n") 1759 expectPrintedTS(t, "function foo<A extends B<A>>() {}", "function foo() {\n}\n") 1760 expectPrintedTS(t, "function foo<A extends B<C<A>>>() {}", "function foo() {\n}\n") 1761 expectPrintedTS(t, "function foo<A,B,C,>() {}", "function foo() {\n}\n") 1762 expectPrintedTS(t, "function foo<A extends B<C>= B<C>>() {}", "function foo() {\n}\n") 1763 expectPrintedTS(t, "function foo<A extends B<C<D>>= B<C<D>>>() {}", "function foo() {\n}\n") 1764 expectPrintedTS(t, "function foo<A extends B<C<D<E>>>= B<C<D<E>>>>() {}", "function foo() {\n}\n") 1765 1766 expectParseErrorTS(t, "function foo<>() {}", "<stdin>: ERROR: Expected identifier but found \">\"\n") 1767 expectParseErrorTS(t, "function foo<,>() {}", "<stdin>: ERROR: Expected identifier but found \",\"\n") 1768 expectParseErrorTS(t, "function foo<T><T>() {}", "<stdin>: ERROR: Expected \"(\" but found \"<\"\n") 1769 1770 expectPrintedTS(t, "export default function <T>() {}", "export default function() {\n}\n") 1771 expectParseErrorTS(t, "export default function <>() {}", "<stdin>: ERROR: Expected identifier but found \">\"\n") 1772 expectParseErrorTS(t, "export default function <,>() {}", "<stdin>: ERROR: Expected identifier but found \",\"\n") 1773 expectParseErrorTS(t, "export default function <T><T>() {}", "<stdin>: ERROR: Expected \"(\" but found \"<\"\n") 1774 1775 expectPrintedTS(t, ` 1776 export default function foo(); 1777 export default function foo(x); 1778 export default function foo(x?, y?) {} 1779 `, "export default function foo(x, y) {\n}\n") 1780 } 1781 1782 func TestTSDecl(t *testing.T) { 1783 expectPrintedTS(t, "var a!: string, b!: boolean", "var a, b;\n") 1784 expectPrintedTS(t, "let a!: string, b!: boolean", "let a, b;\n") 1785 expectPrintedTS(t, "const a!: string = '', b!: boolean = false", "const a = \"\", b = false;\n") 1786 expectPrintedTS(t, "var a\n!b", "var a;\n!b;\n") 1787 expectPrintedTS(t, "let a\n!b", "let a;\n!b;\n") 1788 expectParseErrorTS(t, "var a!", "<stdin>: ERROR: Expected \":\" but found end of file\n") 1789 expectParseErrorTS(t, "var a! = ", "<stdin>: ERROR: Expected \":\" but found \"=\"\n") 1790 expectParseErrorTS(t, "var a!, b", "<stdin>: ERROR: Expected \":\" but found \",\"\n") 1791 1792 expectPrinted(t, "a ? ({b}) => {} : c", "a ? ({ b }) => {\n} : c;\n") 1793 expectPrinted(t, "a ? (({b}) => {}) : c", "a ? ({ b }) => {\n} : c;\n") 1794 expectPrinted(t, "a ? (({b})) : c", "a ? { b } : c;\n") 1795 expectParseError(t, "a ? (({b})) => {} : c", "<stdin>: ERROR: Invalid binding pattern\n") 1796 expectPrintedTS(t, "a ? ({b}) => {} : c", "a ? ({ b }) => {\n} : c;\n") 1797 expectPrintedTS(t, "a ? (({b}) => {}) : c", "a ? ({ b }) => {\n} : c;\n") 1798 expectPrintedTS(t, "a ? (({b})) : c", "a ? { b } : c;\n") 1799 expectParseErrorTS(t, "a ? (({b})) => {} : c", "<stdin>: ERROR: Invalid binding pattern\n") 1800 } 1801 1802 func TestTSDeclare(t *testing.T) { 1803 expectPrintedTS(t, "declare\nfoo", "declare;\nfoo;\n") 1804 expectPrintedTS(t, "declare\nvar foo", "declare;\nvar foo;\n") 1805 expectPrintedTS(t, "declare\nlet foo", "declare;\nlet foo;\n") 1806 expectPrintedTS(t, "declare\nconst foo = 0", "declare;\nconst foo = 0;\n") 1807 expectPrintedTS(t, "declare\nfunction foo() {}", "declare;\nfunction foo() {\n}\n") 1808 expectPrintedTS(t, "declare\nclass Foo {}", "declare;\nclass Foo {\n}\n") 1809 expectPrintedTS(t, "declare\nenum Foo {}", "declare;\nvar Foo = /* @__PURE__ */ ((Foo) => {\n return Foo;\n})(Foo || {});\n") 1810 expectPrintedTS(t, "class Foo { declare \n foo }", "class Foo {\n declare;\n foo;\n}\n") 1811 1812 expectPrintedTS(t, "declare;", "declare;\n") 1813 expectPrintedTS(t, "declare();", "declare();\n") 1814 expectPrintedTS(t, "declare[x];", "declare[x];\n") 1815 1816 expectPrintedTS(t, "declare var x: number", "") 1817 expectPrintedTS(t, "declare let x: number", "") 1818 expectPrintedTS(t, "declare const x: number", "") 1819 expectPrintedTS(t, "declare var x = function() {}; function scope() {}", "function scope() {\n}\n") 1820 expectPrintedTS(t, "declare let x = function() {}; function scope() {}", "function scope() {\n}\n") 1821 expectPrintedTS(t, "declare const x = function() {}; function scope() {}", "function scope() {\n}\n") 1822 expectPrintedTS(t, "declare function fn(); function scope() {}", "function scope() {\n}\n") 1823 expectPrintedTS(t, "declare function fn()\n function scope() {}", "function scope() {\n}\n") 1824 expectPrintedTS(t, "declare function fn() {} function scope() {}", "function scope() {\n}\n") 1825 expectPrintedTS(t, "declare enum X {} function scope() {}", "function scope() {\n}\n") 1826 expectPrintedTS(t, "declare enum X { x = function() {} } function scope() {}", "function scope() {\n}\n") 1827 expectPrintedTS(t, "declare class X {} function scope() {}", "function scope() {\n}\n") 1828 expectPrintedTS(t, "declare class X { x = function() {} } function scope() {}", "function scope() {\n}\n") 1829 expectPrintedTS(t, "declare interface X {} function scope() {}", "function scope() {\n}\n") 1830 expectPrintedTS(t, "declare namespace X {} function scope() {}", "function scope() {\n}\n") 1831 expectPrintedTS(t, "declare namespace X { export var x = function() {} } function scope() {}", "function scope() {\n}\n") 1832 expectPrintedTS(t, "declare namespace X { export let x = function() {} } function scope() {}", "function scope() {\n}\n") 1833 expectPrintedTS(t, "declare namespace X { export const x = function() {} } function scope() {}", "function scope() {\n}\n") 1834 expectPrintedTS(t, "declare namespace X { export function fn() {} } function scope() {}", "function scope() {\n}\n") 1835 expectPrintedTS(t, "declare module X {} function scope() {}", "function scope() {\n}\n") 1836 expectPrintedTS(t, "declare module 'X' {} function scope() {}", "function scope() {\n}\n") 1837 expectPrintedTS(t, "declare module 'X'; let foo", "let foo;\n") 1838 expectPrintedTS(t, "declare module 'X'\nlet foo", "let foo;\n") 1839 expectPrintedTS(t, "declare module 'X' { let foo }", "") 1840 expectPrintedTS(t, "declare module 'X'\n{ let foo }", "") 1841 expectPrintedTS(t, "declare global { interface Foo {} let foo: any } let bar", "let bar;\n") 1842 expectPrintedTS(t, "declare module M { const x }", "") 1843 expectPrintedTS(t, "declare module M { global { const x } }", "") 1844 expectPrintedTS(t, "declare module M { global { const x } function foo() {} }", "") 1845 expectPrintedTS(t, "declare module M { global \n { const x } }", "") 1846 expectPrintedTS(t, "declare module M { import 'path' }", "") 1847 expectPrintedTS(t, "declare module M { import x from 'path' }", "") 1848 expectPrintedTS(t, "declare module M { import {x} from 'path' }", "") 1849 expectPrintedTS(t, "declare module M { import * as ns from 'path' }", "") 1850 expectPrintedTS(t, "declare module M { import foo = bar }", "") 1851 expectPrintedTS(t, "declare module M { export import foo = bar }", "") 1852 expectPrintedTS(t, "declare module M { export {x} from 'path' }", "") 1853 expectPrintedTS(t, "declare module M { export default 123 }", "") 1854 expectPrintedTS(t, "declare module M { export default function x() {} }", "") 1855 expectPrintedTS(t, "declare module M { export default class X {} }", "") 1856 expectPrintedTS(t, "declare module M { export * as ns from 'path' }", "") 1857 expectPrintedTS(t, "declare module M { export * from 'path' }", "") 1858 expectPrintedTS(t, "declare module M { export = foo }", "") 1859 expectPrintedTS(t, "declare module M { export as namespace ns }", "") 1860 expectPrintedTS(t, "declare module M { export as namespace ns; }", "") 1861 expectParseErrorTS(t, "declare module M { export as namespace ns.foo }", "<stdin>: ERROR: Expected \";\" but found \".\"\n") 1862 expectParseErrorTS(t, "declare module M { export as namespace ns function foo() {} }", "<stdin>: ERROR: Expected \";\" but found \"function\"\n") 1863 expectParseErrorTS(t, "module M { const x }", "<stdin>: ERROR: The constant \"x\" must be initialized\n") 1864 expectParseErrorTS(t, "module M { const [] }", "<stdin>: ERROR: This constant must be initialized\n") 1865 expectParseErrorTS(t, "module M { const {} }", "<stdin>: ERROR: This constant must be initialized\n") 1866 1867 // This is a weird case where "," after a rest parameter is allowed 1868 expectPrintedTS(t, "declare function fn(x: any, ...y, )", "") 1869 expectPrintedTS(t, "declare function fn(x: any, ...y: any, )", "") 1870 expectParseErrorTS(t, "function fn(x: any, ...y, )", "<stdin>: ERROR: Expected \")\" but found \",\"\n") 1871 expectParseErrorTS(t, "function fn(x: any, ...y, ) {}", "<stdin>: ERROR: Expected \")\" but found \",\"\n") 1872 expectParseErrorTS(t, "function fn(x: any, ...y: any, )", "<stdin>: ERROR: Expected \")\" but found \",\"\n") 1873 expectParseErrorTS(t, "function fn(x: any, ...y: any, ) {}", "<stdin>: ERROR: Expected \")\" but found \",\"\n") 1874 1875 // This declares a global module 1876 expectPrintedTS(t, "export as namespace ns", "") 1877 expectParseErrorTS(t, "export as namespace ns.foo", "<stdin>: ERROR: Expected \";\" but found \".\"\n") 1878 1879 // TypeScript 4.4+ technically treats these as valid syntax, but I assume 1880 // this is a bug: https://github.com/microsoft/TypeScript/issues/54602 1881 expectParseErrorTS(t, "declare foo", "<stdin>: ERROR: Unexpected \"foo\"\n") 1882 expectParseErrorTS(t, "declare foo()", "<stdin>: ERROR: Unexpected \"foo\"\n") 1883 expectParseErrorTS(t, "declare {foo}", "<stdin>: ERROR: Unexpected \"{\"\n") 1884 } 1885 1886 func TestTSExperimentalDecorator(t *testing.T) { 1887 // Tests of "declare class" 1888 expectPrintedExperimentalDecoratorTS(t, "@dec(() => 0) declare class Foo {} {let foo}", "{\n let foo;\n}\n") 1889 expectPrintedExperimentalDecoratorTS(t, "@dec(() => 0) declare abstract class Foo {} {let foo}", "{\n let foo;\n}\n") 1890 expectPrintedExperimentalDecoratorTS(t, "@dec(() => 0) export declare class Foo {} {let foo}", "{\n let foo;\n}\n") 1891 expectPrintedExperimentalDecoratorTS(t, "@dec(() => 0) export declare abstract class Foo {} {let foo}", "{\n let foo;\n}\n") 1892 expectPrintedExperimentalDecoratorTS(t, "declare class Foo { @dec(() => 0) foo } {let foo}", "{\n let foo;\n}\n") 1893 expectPrintedExperimentalDecoratorTS(t, "declare class Foo { @dec(() => 0) foo() } {let foo}", "{\n let foo;\n}\n") 1894 expectPrintedExperimentalDecoratorTS(t, "declare class Foo { foo(@dec(() => 0) x) } {let foo}", "{\n let foo;\n}\n") 1895 1896 // Decorators must only work on class statements 1897 expectParseErrorExperimentalDecoratorTS(t, "@dec enum foo {}", "<stdin>: ERROR: Decorators are not valid here\n") 1898 expectParseErrorExperimentalDecoratorTS(t, "@dec namespace foo {}", "<stdin>: ERROR: Decorators are not valid here\n") 1899 expectParseErrorExperimentalDecoratorTS(t, "@dec function foo() {}", "<stdin>: ERROR: Decorators are not valid here\n") 1900 expectParseErrorExperimentalDecoratorTS(t, "@dec abstract", "<stdin>: ERROR: Decorators are not valid here\n") 1901 expectParseErrorExperimentalDecoratorTS(t, "@dec declare: x", "<stdin>: ERROR: Unexpected \":\"\n") 1902 expectParseErrorExperimentalDecoratorTS(t, "@dec declare enum foo {}", "<stdin>: ERROR: Decorators are not valid here\n") 1903 expectParseErrorExperimentalDecoratorTS(t, "@dec declare namespace foo {}", "<stdin>: ERROR: Decorators are not valid here\n") 1904 expectParseErrorExperimentalDecoratorTS(t, "@dec declare function foo()", "<stdin>: ERROR: Decorators are not valid here\n") 1905 expectParseErrorExperimentalDecoratorTS(t, "@dec export {}", "<stdin>: ERROR: Decorators are not valid here\n") 1906 expectParseErrorExperimentalDecoratorTS(t, "@dec export enum foo {}", "<stdin>: ERROR: Decorators are not valid here\n") 1907 expectParseErrorExperimentalDecoratorTS(t, "@dec export namespace foo {}", "<stdin>: ERROR: Decorators are not valid here\n") 1908 expectParseErrorExperimentalDecoratorTS(t, "@dec export function foo() {}", "<stdin>: ERROR: Decorators are not valid here\n") 1909 expectParseErrorExperimentalDecoratorTS(t, "@dec export default abstract", "<stdin>: ERROR: Decorators are not valid here\n") 1910 expectParseErrorExperimentalDecoratorTS(t, "@dec export declare enum foo {}", "<stdin>: ERROR: Decorators are not valid here\n") 1911 expectParseErrorExperimentalDecoratorTS(t, "@dec export declare namespace foo {}", "<stdin>: ERROR: Decorators are not valid here\n") 1912 expectParseErrorExperimentalDecoratorTS(t, "@dec export declare function foo()", "<stdin>: ERROR: Decorators are not valid here\n") 1913 1914 // Decorators must be forbidden outside class statements 1915 note := "<stdin>: NOTE: This is a class expression, not a class declaration:\n" 1916 expectParseErrorExperimentalDecoratorTS(t, "(class { @dec foo })", "<stdin>: ERROR: TypeScript experimental decorators can only be used with class declarations\n"+note) 1917 expectParseErrorExperimentalDecoratorTS(t, "(class { @dec foo() {} })", "<stdin>: ERROR: TypeScript experimental decorators can only be used with class declarations\n"+note) 1918 expectParseErrorExperimentalDecoratorTS(t, "(class { foo(@dec x) {} })", "<stdin>: ERROR: TypeScript experimental decorators can only be used with class declarations\n"+note) 1919 expectParseErrorExperimentalDecoratorTS(t, "({ @dec foo })", "<stdin>: ERROR: Expected identifier but found \"@\"\n") 1920 expectParseErrorExperimentalDecoratorTS(t, "({ @dec foo() {} })", "<stdin>: ERROR: Expected identifier but found \"@\"\n") 1921 expectParseErrorExperimentalDecoratorTS(t, "({ foo(@dec x) {} })", "<stdin>: ERROR: Expected identifier but found \"@\"\n") 1922 1923 // Decorators aren't allowed with private names 1924 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec #foo }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 1925 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec #foo = 1 }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 1926 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec #foo() {} }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 1927 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec *#foo() {} }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 1928 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec async #foo() {} }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 1929 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec async* #foo() {} }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 1930 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec accessor #foo }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 1931 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static #foo }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 1932 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static #foo = 1 }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 1933 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static #foo() {} }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 1934 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static *#foo() {} }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 1935 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static async #foo() {} }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 1936 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static async* #foo() {} }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 1937 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static accessor #foo }", "<stdin>: ERROR: TypeScript experimental decorators cannot be used on private identifiers\n") 1938 1939 // Decorators aren't allowed on class constructors 1940 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec constructor() {} }", "<stdin>: ERROR: Decorators are not allowed on class constructors\n") 1941 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec public constructor() {} }", "<stdin>: ERROR: Decorators are not allowed on class constructors\n") 1942 1943 // Check use of "await" 1944 friendlyAwaitErrorWithNote := "<stdin>: ERROR: \"await\" can only be used inside an \"async\" function\n" + 1945 "<stdin>: NOTE: Consider adding the \"async\" keyword here:\n" 1946 expectPrintedExperimentalDecoratorTS(t, "async function foo() { @dec(await x) class Foo {} }", 1947 "async function foo() {\n let Foo = class {\n };\n Foo = __decorateClass([\n dec(await x)\n ], Foo);\n}\n") 1948 expectPrintedExperimentalDecoratorTS(t, "async function foo() { class Foo { @dec(await x) foo() {} } }", 1949 "async function foo() {\n class Foo {\n foo() {\n }\n }\n __decorateClass([\n dec(await x)\n ], Foo.prototype, \"foo\", 1);\n}\n") 1950 expectPrintedExperimentalDecoratorTS(t, "async function foo() { class Foo { foo(@dec(await x) y) {} } }", 1951 "async function foo() {\n class Foo {\n foo(y) {\n }\n }\n __decorateClass([\n __decorateParam(0, dec(await x))\n ], Foo.prototype, \"foo\", 1);\n}\n") 1952 expectParseErrorExperimentalDecoratorTS(t, "function foo() { @dec(await x) class Foo {} }", friendlyAwaitErrorWithNote) 1953 expectParseErrorExperimentalDecoratorTS(t, "function foo() { class Foo { @dec(await x) foo() {} } }", friendlyAwaitErrorWithNote) 1954 expectParseErrorExperimentalDecoratorTS(t, "function foo() { class Foo { foo(@dec(await x) y) {} } }", friendlyAwaitErrorWithNote) 1955 expectParseErrorExperimentalDecoratorTS(t, "function foo() { class Foo { @dec(await x) async foo() {} } }", friendlyAwaitErrorWithNote) 1956 expectParseErrorExperimentalDecoratorTS(t, "function foo() { class Foo { async foo(@dec(await x) y) {} } }", 1957 "<stdin>: ERROR: The keyword \"await\" cannot be used here:\n<stdin>: ERROR: Expected \")\" but found \"x\"\n") 1958 1959 // Check lowered use of "await" 1960 expectPrintedTargetExperimentalDecoratorTS(t, 2015, "async function foo() { @dec(await x) class Foo {} }", 1961 `function foo() { 1962 return __async(this, null, function* () { 1963 let Foo = class { 1964 }; 1965 Foo = __decorateClass([ 1966 dec(yield x) 1967 ], Foo); 1968 }); 1969 } 1970 `) 1971 expectPrintedTargetExperimentalDecoratorTS(t, 2015, "async function foo() { class Foo { @dec(await x) foo() {} } }", 1972 `function foo() { 1973 return __async(this, null, function* () { 1974 class Foo { 1975 foo() { 1976 } 1977 } 1978 __decorateClass([ 1979 dec(yield x) 1980 ], Foo.prototype, "foo", 1); 1981 }); 1982 } 1983 `) 1984 expectPrintedTargetExperimentalDecoratorTS(t, 2015, "async function foo() { class Foo { foo(@dec(await x) y) {} } }", 1985 `function foo() { 1986 return __async(this, null, function* () { 1987 class Foo { 1988 foo(y) { 1989 } 1990 } 1991 __decorateClass([ 1992 __decorateParam(0, dec(yield x)) 1993 ], Foo.prototype, "foo", 1); 1994 }); 1995 } 1996 `) 1997 1998 // Check use of "yield" 1999 expectPrintedExperimentalDecoratorTS(t, "function *foo() { @dec(yield x) class Foo {} }", // We currently allow this but TypeScript doesn't 2000 "function* foo() {\n let Foo = class {\n };\n Foo = __decorateClass([\n dec(yield x)\n ], Foo);\n}\n") 2001 expectPrintedExperimentalDecoratorTS(t, "function *foo() { class Foo { @dec(yield x) foo() {} } }", // We currently allow this but TypeScript doesn't 2002 "function* foo() {\n class Foo {\n foo() {\n }\n }\n __decorateClass([\n dec(yield x)\n ], Foo.prototype, \"foo\", 1);\n}\n") 2003 expectParseErrorExperimentalDecoratorTS(t, "function *foo() { class Foo { foo(@dec(yield x) y) {} } }", // TypeScript doesn't allow this (although it could because it would work fine) 2004 "<stdin>: ERROR: Cannot use \"yield\" outside a generator function\n") 2005 expectParseErrorExperimentalDecoratorTS(t, "function foo() { @dec(yield x) class Foo {} }", "<stdin>: ERROR: Cannot use \"yield\" outside a generator function\n") 2006 expectParseErrorExperimentalDecoratorTS(t, "function foo() { class Foo { @dec(yield x) foo() {} } }", "<stdin>: ERROR: Cannot use \"yield\" outside a generator function\n") 2007 2008 // Check inline function expressions 2009 expectPrintedExperimentalDecoratorTS(t, "@((x, y) => x + y) class Foo {}", 2010 "let Foo = class {\n};\nFoo = __decorateClass([\n (x, y) => x + y\n], Foo);\n") 2011 expectPrintedExperimentalDecoratorTS(t, "@((x, y) => x + y) export class Foo {}", 2012 "export let Foo = class {\n};\nFoo = __decorateClass([\n (x, y) => x + y\n], Foo);\n") 2013 expectPrintedExperimentalDecoratorTS(t, "@(function(x, y) { return x + y }) class Foo {}", 2014 "let Foo = class {\n};\nFoo = __decorateClass([\n function(x, y) {\n return x + y;\n }\n], Foo);\n") 2015 expectPrintedExperimentalDecoratorTS(t, "@(function(x, y) { return x + y }) export class Foo {}", 2016 "export let Foo = class {\n};\nFoo = __decorateClass([\n function(x, y) {\n return x + y;\n }\n], Foo);\n") 2017 2018 // Don't allow decorators on static blocks 2019 expectPrintedTS(t, "class Foo { static }", "class Foo {\n static;\n}\n") 2020 expectPrintedExperimentalDecoratorTS(t, "class Foo { @dec static }", "class Foo {\n static;\n}\n__decorateClass([\n dec\n], Foo.prototype, \"static\", 2);\n") 2021 expectParseErrorExperimentalDecoratorTS(t, "class Foo { @dec static {} }", "<stdin>: ERROR: Expected \";\" but found \"{\"\n") 2022 2023 // TypeScript experimental decorators allow more expressions than JavaScript decorators 2024 expectPrintedExperimentalDecoratorTS(t, "@x() class Foo {}", "let Foo = class {\n};\nFoo = __decorateClass([\n x()\n], Foo);\n") 2025 expectPrintedExperimentalDecoratorTS(t, "@x.y() class Foo {}", "let Foo = class {\n};\nFoo = __decorateClass([\n x.y()\n], Foo);\n") 2026 expectPrintedExperimentalDecoratorTS(t, "@(() => {}) class Foo {}", "let Foo = class {\n};\nFoo = __decorateClass([\n () => {\n }\n], Foo);\n") 2027 expectPrintedExperimentalDecoratorTS(t, "@123 class Foo {}", "let Foo = class {\n};\nFoo = __decorateClass([\n 123\n], Foo);\n") 2028 expectPrintedExperimentalDecoratorTS(t, "@x?.() class Foo {}", "let Foo = class {\n};\nFoo = __decorateClass([\n x?.()\n], Foo);\n") 2029 expectPrintedExperimentalDecoratorTS(t, "@x?.y() class Foo {}", "let Foo = class {\n};\nFoo = __decorateClass([\n x?.y()\n], Foo);\n") 2030 expectPrintedExperimentalDecoratorTS(t, "@x?.[y]() class Foo {}", "let Foo = class {\n};\nFoo = __decorateClass([\n x?.[y]()\n], Foo);\n") 2031 expectPrintedExperimentalDecoratorTS(t, "@new Function() class Foo {}", "let Foo = class {\n};\nFoo = __decorateClass([\n new Function()\n], Foo);\n") 2032 expectParseErrorExperimentalDecoratorTS(t, "@x[y] class Foo {}", "<stdin>: ERROR: Expected \";\" but found \"class\"\n") 2033 expectParseErrorExperimentalDecoratorTS(t, "@() => {} class Foo {}", "<stdin>: ERROR: Unexpected \")\"\n") 2034 expectParseErrorExperimentalDecoratorTS(t, "x = @y function() {}", 2035 "<stdin>: ERROR: TypeScript experimental decorators cannot be used in expression position\n"+ 2036 "<stdin>: ERROR: Expected \"class\" but found \"function\"\n") 2037 2038 // Check ASI for "abstract" 2039 expectPrintedExperimentalDecoratorTS(t, "@x abstract class Foo {}", "let Foo = class {\n};\nFoo = __decorateClass([\n x\n], Foo);\n") 2040 expectParseErrorExperimentalDecoratorTS(t, "@x abstract\nclass Foo {}", "<stdin>: ERROR: Decorators are not valid here\n") 2041 2042 // Check decorator locations in relation to the "export" keyword 2043 expectPrintedExperimentalDecoratorTS(t, "@x export class Foo {}", "export let Foo = class {\n};\nFoo = __decorateClass([\n x\n], Foo);\n") 2044 expectPrintedExperimentalDecoratorTS(t, "export @x class Foo {}", "export let Foo = class {\n};\nFoo = __decorateClass([\n x\n], Foo);\n") 2045 expectPrintedExperimentalDecoratorTS(t, "@x export default class {}", 2046 "let stdin_default = class {\n};\nstdin_default = __decorateClass([\n x\n], stdin_default);\nexport {\n stdin_default as default\n};\n") 2047 expectPrintedExperimentalDecoratorTS(t, "export default @x class {}", 2048 "let stdin_default = class {\n};\nstdin_default = __decorateClass([\n x\n], stdin_default);\nexport {\n stdin_default as default\n};\n") 2049 expectPrintedExperimentalDecoratorTS(t, "@x export default class Foo {}", "let Foo = class {\n};\nFoo = __decorateClass([\n x\n], Foo);\nexport {\n Foo as default\n};\n") 2050 expectPrintedExperimentalDecoratorTS(t, "export default @x class Foo {}", "let Foo = class {\n};\nFoo = __decorateClass([\n x\n], Foo);\nexport {\n Foo as default\n};\n") 2051 expectParseErrorExperimentalDecoratorTS(t, "export default (@x class {})", "<stdin>: ERROR: TypeScript experimental decorators cannot be used in expression position\n") 2052 expectParseErrorExperimentalDecoratorTS(t, "export default (@x class Foo {})", "<stdin>: ERROR: TypeScript experimental decorators cannot be used in expression position\n") 2053 expectParseErrorExperimentalDecoratorTS(t, "export @x default class {}", "<stdin>: ERROR: Unexpected \"default\"\n") 2054 expectParseErrorExperimentalDecoratorTS(t, "@x export @y class Foo {}", "<stdin>: ERROR: Decorators are not valid here\n") 2055 expectParseErrorExperimentalDecoratorTS(t, "@x export default abstract", "<stdin>: ERROR: Decorators are not valid here\n") 2056 expectParseErrorExperimentalDecoratorTS(t, "@x export @y default class {}", "<stdin>: ERROR: Decorators are not valid here\n<stdin>: ERROR: Unexpected \"default\"\n") 2057 2058 // From the TypeScript team: "We do allow postfix ! because it's TypeScript only." 2059 // https://github.com/microsoft/TypeScript/issues/57756 2060 expectPrintedExperimentalDecoratorTS(t, "@x!.y!.z class Foo {}", "let Foo = class {\n};\nFoo = __decorateClass([\n x.y.z\n], Foo);\n") 2061 2062 // TypeScript experimental decorators are actually allowed on declared and abstract fields 2063 expectPrintedExperimentalDecoratorTS(t, "class Foo { @(() => {}) declare foo: any; @(() => {}) bar: any }", 2064 "class Foo {\n bar;\n}\n__decorateClass([\n () => {\n }\n], Foo.prototype, \"foo\", 2);\n__decorateClass([\n () => {\n }\n], Foo.prototype, \"bar\", 2);\n") 2065 expectPrintedExperimentalDecoratorTS(t, "abstract class Foo { @(() => {}) abstract foo: any; @(() => {}) bar: any }", 2066 "class Foo {\n bar;\n}\n__decorateClass([\n () => {\n }\n], Foo.prototype, \"foo\", 2);\n__decorateClass([\n () => {\n }\n], Foo.prototype, \"bar\", 2);\n") 2067 } 2068 2069 func TestTSDecorators(t *testing.T) { 2070 expectPrintedTS(t, "@x @y class Foo {}", "@x @y class Foo {\n}\n") 2071 expectPrintedTS(t, "@x @y export class Foo {}", "@x @y export class Foo {\n}\n") 2072 expectPrintedTS(t, "@x @y export default class Foo {}", "@x @y export default class Foo {\n}\n") 2073 expectPrintedTS(t, "_ = @x @y class {}", "_ = @x @y class {\n};\n") 2074 2075 expectPrintedTS(t, "class Foo { @x y: any }", "class Foo {\n @x y;\n}\n") 2076 expectPrintedTS(t, "class Foo { @x y(): any {} }", "class Foo {\n @x y() {\n }\n}\n") 2077 expectPrintedTS(t, "class Foo { @x static y: any }", "class Foo {\n @x static y;\n}\n") 2078 expectPrintedTS(t, "class Foo { @x static y(): any {} }", "class Foo {\n @x static y() {\n }\n}\n") 2079 expectPrintedTS(t, "class Foo { @x accessor y: any }", "class Foo {\n @x accessor y;\n}\n") 2080 2081 expectPrintedTS(t, "class Foo { @x #y: any }", "class Foo {\n @x #y;\n}\n") 2082 expectPrintedTS(t, "class Foo { @x #y(): any {} }", "class Foo {\n @x #y() {\n }\n}\n") 2083 expectPrintedTS(t, "class Foo { @x static #y: any }", "class Foo {\n @x static #y;\n}\n") 2084 expectPrintedTS(t, "class Foo { @x static #y(): any {} }", "class Foo {\n @x static #y() {\n }\n}\n") 2085 expectPrintedTS(t, "class Foo { @x accessor #y: any }", "class Foo {\n @x accessor #y;\n}\n") 2086 2087 expectParseErrorTS(t, "class Foo { x(@y z) {} }", "<stdin>: ERROR: Parameter decorators only work when experimental decorators are enabled\n"+ 2088 "NOTE: You can enable experimental decorators by adding \"experimentalDecorators\": true to your \"tsconfig.json\" file.\n") 2089 expectParseErrorTS(t, "class Foo { @x static {} }", "<stdin>: ERROR: Expected \";\" but found \"{\"\n") 2090 2091 expectPrintedTS(t, "@\na\n(\n)\n@\n(\nb\n)\nclass\nFoo\n{\n}\n", "@a()\n@b\nclass Foo {\n}\n") 2092 expectPrintedTS(t, "@(a, b) class Foo {}", "@(a, b) class Foo {\n}\n") 2093 expectPrintedTS(t, "@x() class Foo {}", "@x() class Foo {\n}\n") 2094 expectPrintedTS(t, "@x.y() class Foo {}", "@x.y() class Foo {\n}\n") 2095 expectPrintedTS(t, "@(() => {}) class Foo {}", "@(() => {\n}) class Foo {\n}\n") 2096 expectPrintedTS(t, "class Foo { #x = @y.#x.y.#x class {} }", "class Foo {\n #x = @y.#x.y.#x class {\n };\n}\n") 2097 expectParseErrorTS(t, "@123 class Foo {}", "<stdin>: ERROR: Expected identifier but found \"123\"\n") 2098 expectParseErrorTS(t, "@x[y] class Foo {}", "<stdin>: ERROR: Expected \";\" but found \"class\"\n") 2099 expectParseErrorTS(t, "@x?.() class Foo {}", "<stdin>: ERROR: Expected identifier but found \"(\"\n") 2100 expectParseErrorTS(t, "@x?.y() class Foo {}", 2101 "<stdin>: ERROR: JavaScript decorator syntax does not allow \"?.\" here\n"+ 2102 "<stdin>: NOTE: Wrap this decorator in parentheses to allow arbitrary expressions:\n") 2103 expectParseErrorTS(t, "@x?.[y]() class Foo {}", "<stdin>: ERROR: Expected identifier but found \"[\"\n") 2104 expectParseErrorTS(t, "@new Function() class Foo {}", "<stdin>: ERROR: Expected identifier but found \"new\"\n") 2105 expectParseErrorTS(t, "@() => {} class Foo {}", "<stdin>: ERROR: Unexpected \")\"\n") 2106 expectParseErrorTS(t, "x = @y function() {}", "<stdin>: ERROR: Expected \"class\" but found \"function\"\n") 2107 2108 expectPrintedTS(t, "class Foo { @x<{}> y: any }", "class Foo {\n @x y;\n}\n") 2109 expectPrintedTS(t, "class Foo { @x<{}>() y: any }", "class Foo {\n @x() y;\n}\n") 2110 expectPrintedTS(t, "class Foo { @x<{}> @y<[], () => {}> z: any }", "class Foo {\n @x @y z;\n}\n") 2111 expectPrintedTS(t, "class Foo { @x<{}>() @y<[], () => {}>() z: any }", "class Foo {\n @x() @y() z;\n}\n") 2112 expectPrintedTS(t, "class Foo { @x<{}>.y<[], () => {}> z: any }", "class Foo {\n @x.y z;\n}\n") 2113 2114 // TypeScript 5.0+ allows this but Babel doesn't. I believe this is a bug 2115 // with TypeScript: https://github.com/microsoft/TypeScript/issues/55336 2116 expectParseErrorTS(t, "class Foo { @x<{}>().y<[], () => {}>() z: any }", 2117 "<stdin>: ERROR: JavaScript decorator syntax does not allow \".\" after a call expression\n"+ 2118 "<stdin>: NOTE: Wrap this decorator in parentheses to allow arbitrary expressions:\n") 2119 2120 expectPrintedWithUnsupportedFeaturesTS(t, compat.Decorators, "@dec class Foo {}", 2121 `var _Foo_decorators, _init; 2122 _Foo_decorators = [dec]; 2123 class Foo { 2124 } 2125 _init = __decoratorStart(null); 2126 Foo = __decorateElement(_init, 0, "Foo", _Foo_decorators, Foo); 2127 __runInitializers(_init, 1, Foo); 2128 `) 2129 expectPrintedWithUnsupportedFeaturesTS(t, compat.Decorators, "class Foo { @dec x }", 2130 `var _x_dec, _init; 2131 _x_dec = [dec]; 2132 class Foo { 2133 constructor() { 2134 __publicField(this, "x", __runInitializers(_init, 8, this)), __runInitializers(_init, 11, this); 2135 } 2136 } 2137 _init = __decoratorStart(null); 2138 __decorateElement(_init, 5, "x", _x_dec, Foo); 2139 `) 2140 expectPrintedWithUnsupportedFeaturesTS(t, compat.Decorators, "class Foo { @dec x() {} }", 2141 `var _x_dec, _init; 2142 _x_dec = [dec]; 2143 class Foo { 2144 constructor() { 2145 __runInitializers(_init, 5, this); 2146 } 2147 x() { 2148 } 2149 } 2150 _init = __decoratorStart(null); 2151 __decorateElement(_init, 1, "x", _x_dec, Foo); 2152 `) 2153 expectPrintedWithUnsupportedFeaturesTS(t, compat.Decorators, "class Foo { @dec accessor x }", 2154 `var _x_dec, _init, _x; 2155 _x_dec = [dec]; 2156 class Foo { 2157 constructor() { 2158 __privateAdd(this, _x, __runInitializers(_init, 8, this)), __runInitializers(_init, 11, this); 2159 } 2160 } 2161 _init = __decoratorStart(null); 2162 _x = new WeakMap(); 2163 __decorateElement(_init, 4, "x", _x_dec, Foo, _x); 2164 `) 2165 expectPrintedWithUnsupportedFeaturesTS(t, compat.Decorators, "class Foo { @dec static x }", 2166 `var _x_dec, _init; 2167 _x_dec = [dec]; 2168 class Foo { 2169 } 2170 _init = __decoratorStart(null); 2171 __decorateElement(_init, 13, "x", _x_dec, Foo); 2172 __publicField(Foo, "x", __runInitializers(_init, 8, Foo)), __runInitializers(_init, 11, Foo); 2173 `) 2174 expectPrintedWithUnsupportedFeaturesTS(t, compat.Decorators, "class Foo { @dec static x() {} }", 2175 `var _x_dec, _init; 2176 _x_dec = [dec]; 2177 class Foo { 2178 static x() { 2179 } 2180 } 2181 _init = __decoratorStart(null); 2182 __decorateElement(_init, 9, "x", _x_dec, Foo); 2183 __runInitializers(_init, 3, Foo); 2184 `) 2185 expectPrintedWithUnsupportedFeaturesTS(t, compat.Decorators, "class Foo { @dec static accessor x }", 2186 `var _x_dec, _init, _x; 2187 _x_dec = [dec]; 2188 class Foo { 2189 } 2190 _init = __decoratorStart(null); 2191 _x = new WeakMap(); 2192 __decorateElement(_init, 12, "x", _x_dec, Foo, _x); 2193 __privateAdd(Foo, _x, __runInitializers(_init, 8, Foo)), __runInitializers(_init, 11, Foo); 2194 `) 2195 2196 // Check ASI for "abstract" 2197 expectPrintedTS(t, "@x abstract class Foo {}", "@x class Foo {\n}\n") 2198 expectParseErrorTS(t, "@x abstract\nclass Foo {}", "<stdin>: ERROR: Decorators are not valid here\n") 2199 2200 // Check decorator locations in relation to the "export" keyword 2201 expectPrintedTS(t, "@x export class Foo {}", "@x export class Foo {\n}\n") 2202 expectPrintedTS(t, "export @x class Foo {}", "@x export class Foo {\n}\n") 2203 expectPrintedTS(t, "@x export default class {}", "@x export default class {\n}\n") 2204 expectPrintedTS(t, "export default @x class {}", "@x export default class {\n}\n") 2205 expectPrintedTS(t, "@x export default class Foo {}", "@x export default class Foo {\n}\n") 2206 expectPrintedTS(t, "export default @x class Foo {}", "@x export default class Foo {\n}\n") 2207 expectPrintedTS(t, "export default (@x class {})", "export default (@x class {\n});\n") 2208 expectPrintedTS(t, "export default (@x class Foo {})", "export default (@x class Foo {\n});\n") 2209 expectParseErrorTS(t, "export @x default class {}", "<stdin>: ERROR: Unexpected \"default\"\n") 2210 expectParseErrorTS(t, "@x export @y class Foo {}", "<stdin>: ERROR: Decorators are not valid here\n") 2211 expectParseErrorTS(t, "@x export default abstract", "<stdin>: ERROR: Decorators are not valid here\n") 2212 expectParseErrorTS(t, "@x export @y default class {}", "<stdin>: ERROR: Decorators are not valid here\n<stdin>: ERROR: Unexpected \"default\"\n") 2213 2214 // From the TypeScript team: "We do allow postfix ! because it's TypeScript only." 2215 // https://github.com/microsoft/TypeScript/issues/57756 2216 expectPrintedTS(t, "@x!.y!.z class Foo {}", "@x.y.z class Foo {\n}\n") 2217 2218 // JavaScript decorators are not allowed on declared or abstract fields 2219 expectParseErrorTS(t, "class Foo { @(() => {}) declare foo: any; @(() => {}) bar: any }", 2220 "<stdin>: ERROR: Decorators are not valid here\n") 2221 expectParseErrorTS(t, "abstract class Foo { @(() => {}) abstract foo: any; @(() => {}) bar: any }", 2222 "<stdin>: ERROR: Decorators are not valid here\n") 2223 } 2224 2225 func TestTSTry(t *testing.T) { 2226 expectPrintedTS(t, "try {} catch (x: any) {}", "try {\n} catch (x) {\n}\n") 2227 expectPrintedTS(t, "try {} catch (x: unknown) {}", "try {\n} catch (x) {\n}\n") 2228 expectPrintedTS(t, "try {} catch (x: number) {}", "try {\n} catch (x) {\n}\n") 2229 2230 expectPrintedTS(t, "try {} catch ({x}: any) {}", "try {\n} catch ({ x }) {\n}\n") 2231 expectPrintedTS(t, "try {} catch ({x}: unknown) {}", "try {\n} catch ({ x }) {\n}\n") 2232 expectPrintedTS(t, "try {} catch ({x}: number) {}", "try {\n} catch ({ x }) {\n}\n") 2233 2234 expectPrintedTS(t, "try {} catch ([x]: any) {}", "try {\n} catch ([x]) {\n}\n") 2235 expectPrintedTS(t, "try {} catch ([x]: unknown) {}", "try {\n} catch ([x]) {\n}\n") 2236 expectPrintedTS(t, "try {} catch ([x]: number) {}", "try {\n} catch ([x]) {\n}\n") 2237 2238 expectParseErrorTS(t, "try {} catch (x!) {}", "<stdin>: ERROR: Expected \")\" but found \"!\"\n") 2239 expectParseErrorTS(t, "try {} catch (x!: any) {}", "<stdin>: ERROR: Expected \")\" but found \"!\"\n") 2240 expectParseErrorTS(t, "try {} catch (x!: unknown) {}", "<stdin>: ERROR: Expected \")\" but found \"!\"\n") 2241 } 2242 2243 func TestTSArrow(t *testing.T) { 2244 expectPrintedTS(t, "(a?) => {}", "(a) => {\n};\n") 2245 expectPrintedTS(t, "(a?: number) => {}", "(a) => {\n};\n") 2246 expectPrintedTS(t, "(a?: number = 0) => {}", "(a = 0) => {\n};\n") 2247 expectParseErrorTS(t, "(a? = 0) => {}", "<stdin>: ERROR: Unexpected \"=\"\n") 2248 2249 expectPrintedTS(t, "(a?, b) => {}", "(a, b) => {\n};\n") 2250 expectPrintedTS(t, "(a?: number, b) => {}", "(a, b) => {\n};\n") 2251 expectPrintedTS(t, "(a?: number = 0, b) => {}", "(a = 0, b) => {\n};\n") 2252 expectParseErrorTS(t, "(a? = 0, b) => {}", "<stdin>: ERROR: Unexpected \"=\"\n") 2253 2254 expectPrintedTS(t, "(a: number) => {}", "(a) => {\n};\n") 2255 expectPrintedTS(t, "(a: number = 0) => {}", "(a = 0) => {\n};\n") 2256 expectPrintedTS(t, "(a: number, b) => {}", "(a, b) => {\n};\n") 2257 2258 expectPrintedTS(t, "(): void => {}", "() => {\n};\n") 2259 expectPrintedTS(t, "(a): void => {}", "(a) => {\n};\n") 2260 expectParseErrorTS(t, "x: void => {}", "<stdin>: ERROR: Unexpected \"=>\"\n") 2261 expectPrintedTS(t, "a ? (1 + 2) : (3 + 4)", "a ? 1 + 2 : 3 + 4;\n") 2262 expectPrintedTS(t, "(foo) ? (foo as Bar) : null;", "foo ? foo : null;\n") 2263 expectPrintedTS(t, "((foo) ? (foo as Bar) : null)", "foo ? foo : null;\n") 2264 expectPrintedTS(t, "let x = a ? (b, c) : (d, e)", "let x = a ? (b, c) : (d, e);\n") 2265 2266 expectPrintedTS(t, "let x: () => void = () => {}", "let x = () => {\n};\n") 2267 expectPrintedTS(t, "let x: (y) => void = () => {}", "let x = () => {\n};\n") 2268 expectPrintedTS(t, "let x: (this) => void = () => {}", "let x = () => {\n};\n") 2269 expectPrintedTS(t, "let x: (this: any) => void = () => {}", "let x = () => {\n};\n") 2270 expectPrintedTS(t, "let x = (y: any): (() => {}) => {};", "let x = (y) => {\n};\n") 2271 expectPrintedTS(t, "let x = (y: any): () => {} => {};", "let x = (y) => {\n};\n") 2272 expectPrintedTS(t, "let x = (y: any): (y) => {} => {};", "let x = (y) => {\n};\n") 2273 expectPrintedTS(t, "let x = (y: any): ([,[b]]) => {} => {};", "let x = (y) => {\n};\n") 2274 expectPrintedTS(t, "let x = (y: any): ([a,[b]]) => {} => {};", "let x = (y) => {\n};\n") 2275 expectPrintedTS(t, "let x = (y: any): ([a,[b],]) => {} => {};", "let x = (y) => {\n};\n") 2276 expectPrintedTS(t, "let x = (y: any): ({a}) => {} => {};", "let x = (y) => {\n};\n") 2277 expectPrintedTS(t, "let x = (y: any): ({a,}) => {} => {};", "let x = (y) => {\n};\n") 2278 expectPrintedTS(t, "let x = (y: any): ({a:{b}}) => {} => {};", "let x = (y) => {\n};\n") 2279 expectPrintedTS(t, "let x = (y: any): ({0:{b}}) => {} => {};", "let x = (y) => {\n};\n") 2280 expectPrintedTS(t, "let x = (y: any): ({'a':{b}}) => {} => {};", "let x = (y) => {\n};\n") 2281 expectPrintedTS(t, "let x = (y: any): ({if:{b}}) => {} => {};", "let x = (y) => {\n};\n") 2282 expectPrintedTS(t, "let x = (y: any): ({...a}) => {} => {};", "let x = (y) => {\n};\n") 2283 expectPrintedTS(t, "let x = (y: any): ({a,...b}) => {} => {};", "let x = (y) => {\n};\n") 2284 expectPrintedTS(t, "let x = (y: any): (y[]) => {};", "let x = (y) => {\n};\n") 2285 expectPrintedTS(t, "let x = (y: any): (a | b) => {};", "let x = (y) => {\n};\n") 2286 expectPrintedTS(t, "type x = ({...fi}) => {};", "") 2287 expectParseErrorTS(t, "let x = (y: any): (y) => {};", "<stdin>: ERROR: Unexpected \":\"\n") 2288 expectParseErrorTS(t, "let x = (y: any): (y) => {return 0};", "<stdin>: ERROR: Unexpected \":\"\n") 2289 expectParseErrorTS(t, "let x = (y: any): asserts y is (y) => {};", "<stdin>: ERROR: Unexpected \":\"\n") 2290 expectParseErrorTS(t, "type x = ({...if}) => {};", "<stdin>: ERROR: Unexpected \"...\"\n") 2291 2292 expectPrintedTS(t, "async (): void => {}", "async () => {\n};\n") 2293 expectPrintedTS(t, "async (a): void => {}", "async (a) => {\n};\n") 2294 expectParseErrorTS(t, "async x: void => {}", "<stdin>: ERROR: Expected \"=>\" but found \":\"\n") 2295 2296 expectPrintedTS(t, "function foo(x: boolean): asserts x", "") 2297 expectPrintedTS(t, "function foo(x: boolean): asserts<T>", "") 2298 expectPrintedTS(t, "function foo(x: boolean): asserts\nx", "x;\n") 2299 expectPrintedTS(t, "function foo(x: boolean): asserts<T>\nx", "x;\n") 2300 expectParseErrorTS(t, "function foo(x: boolean): asserts<T> x", "<stdin>: ERROR: Expected \";\" but found \"x\"\n") 2301 expectPrintedTS(t, "(x: boolean): asserts x => {}", "(x) => {\n};\n") 2302 expectPrintedTS(t, "(x: boolean): asserts this is object => {}", "(x) => {\n};\n") 2303 expectPrintedTS(t, "(x: T): asserts x is NonNullable<T> => {}", "(x) => {\n};\n") 2304 expectPrintedTS(t, "(x: any): x is number => {}", "(x) => {\n};\n") 2305 expectPrintedTS(t, "(x: any): this is object => {}", "(x) => {\n};\n") 2306 expectPrintedTS(t, "(x: any): (() => void) => {}", "(x) => {\n};\n") 2307 expectPrintedTS(t, "(x: any): ((y: any) => void) => {}", "(x) => {\n};\n") 2308 expectPrintedTS(t, "function foo(this: any): this is number {}", "function foo() {\n}\n") 2309 expectPrintedTS(t, "function foo(this: any): asserts this is number {}", "function foo() {\n}\n") 2310 expectPrintedTS(t, "(symbol: any): symbol is number => {}", "(symbol) => {\n};\n") 2311 2312 expectPrintedTS(t, "let x: () => {} | ({y: z});", "let x;\n") 2313 expectPrintedTS(t, "function x(): ({y: z}) {}", "function x() {\n}\n") 2314 2315 expectParseErrorTargetTS(t, 5, "return check ? (hover = 2, bar) : baz()", "") 2316 expectParseErrorTargetTS(t, 5, "return check ? (hover = 2, bar) => 0 : baz()", 2317 "<stdin>: ERROR: Transforming default arguments to the configured target environment is not supported yet\n") 2318 } 2319 2320 func TestTSSuperCall(t *testing.T) { 2321 expectPrintedAssignSemanticsTS(t, "class A extends B { x = 1 }", 2322 `class A extends B { 2323 constructor() { 2324 super(...arguments); 2325 this.x = 1; 2326 } 2327 } 2328 `) 2329 2330 expectPrintedAssignSemanticsTS(t, "class A extends B { x }", 2331 `class A extends B { 2332 } 2333 `) 2334 2335 expectPrintedAssignSemanticsTS(t, "class A extends B { x = 1; constructor() { foo() } }", 2336 `class A extends B { 2337 constructor() { 2338 this.x = 1; 2339 foo(); 2340 } 2341 } 2342 `) 2343 2344 expectPrintedAssignSemanticsTS(t, "class A extends B { x; constructor() { foo() } }", 2345 `class A extends B { 2346 constructor() { 2347 foo(); 2348 } 2349 } 2350 `) 2351 2352 expectPrintedAssignSemanticsTS(t, "class A extends B { x = 1; constructor() { foo(); super(1); } }", 2353 `class A extends B { 2354 constructor() { 2355 foo(); 2356 super(1); 2357 this.x = 1; 2358 } 2359 } 2360 `) 2361 2362 expectPrintedAssignSemanticsTS(t, "class A extends B { x = 1; constructor() { foo(); y ||= super(1); } }", 2363 `class A extends B { 2364 constructor() { 2365 var __super = (...args) => { 2366 super(...args); 2367 this.x = 1; 2368 return this; 2369 }; 2370 foo(); 2371 y ||= __super(1); 2372 } 2373 } 2374 `) 2375 2376 expectPrintedAssignSemanticsTS(t, "class A extends B { x; constructor() { foo(); super(1); } }", 2377 `class A extends B { 2378 constructor() { 2379 foo(); 2380 super(1); 2381 } 2382 } 2383 `) 2384 2385 expectPrintedAssignSemanticsTS(t, "class A extends B { x; constructor() { foo(); y ||= super(1); } }", 2386 `class A extends B { 2387 constructor() { 2388 foo(); 2389 y ||= super(1); 2390 } 2391 } 2392 `) 2393 2394 expectPrintedAssignSemanticsTS(t, "class A extends B { [x] = 1; constructor() { foo(); super(1); } }", 2395 `var _a, _b; 2396 class A extends (_b = B, _a = x, _b) { 2397 constructor() { 2398 foo(); 2399 super(1); 2400 this[_a] = 1; 2401 } 2402 } 2403 `) 2404 2405 expectPrintedAssignSemanticsTS(t, "class A extends B { [x] = 1; constructor() { foo(); y ||= super(1); } }", 2406 `var _a, _b; 2407 class A extends (_b = B, _a = x, _b) { 2408 constructor() { 2409 var __super = (...args) => { 2410 super(...args); 2411 this[_a] = 1; 2412 return this; 2413 }; 2414 foo(); 2415 y ||= __super(1); 2416 } 2417 } 2418 `) 2419 2420 expectPrintedAssignSemanticsTS(t, "class A extends B { [x]; constructor() { foo(); super(1); } }", 2421 `var _a; 2422 class A extends (_a = B, x, _a) { 2423 constructor() { 2424 foo(); 2425 super(1); 2426 } 2427 } 2428 `) 2429 2430 expectPrintedAssignSemanticsTS(t, "class A extends B { [x]; constructor() { foo(); y ||= super(1); } }", 2431 `var _a; 2432 class A extends (_a = B, x, _a) { 2433 constructor() { 2434 foo(); 2435 y ||= super(1); 2436 } 2437 } 2438 `) 2439 2440 expectPrintedAssignSemanticsTS(t, "class A extends B { constructor(public x = 1) { foo(); super(1); } }", 2441 `class A extends B { 2442 constructor(x = 1) { 2443 foo(); 2444 super(1); 2445 this.x = x; 2446 } 2447 } 2448 `) 2449 2450 expectPrintedAssignSemanticsTS(t, "class A extends B { constructor(public x = 1) { foo(); super(1); super(2); } }", 2451 `class A extends B { 2452 constructor(x = 1) { 2453 var __super = (...args) => { 2454 super(...args); 2455 this.x = x; 2456 return this; 2457 }; 2458 foo(); 2459 __super(1); 2460 __super(2); 2461 } 2462 } 2463 `) 2464 2465 expectPrintedAssignSemanticsTS(t, "class A extends B { constructor(public x = 1) { if (false) super(1); super(2); } }", `class A extends B { 2466 constructor(x = 1) { 2467 if (false) __super(1); 2468 super(2); 2469 this.x = x; 2470 } 2471 } 2472 `) 2473 2474 expectPrintedAssignSemanticsTS(t, "class A extends B { constructor(public x = 1) { if (foo) super(1); super(2); } }", `class A extends B { 2475 constructor(x = 1) { 2476 var __super = (...args) => { 2477 super(...args); 2478 this.x = x; 2479 return this; 2480 }; 2481 if (foo) __super(1); 2482 __super(2); 2483 } 2484 } 2485 `) 2486 2487 expectPrintedAssignSemanticsTS(t, "class A extends B { constructor(public x = 1) { if (foo) super(1); else super(2); } }", `class A extends B { 2488 constructor(x = 1) { 2489 var __super = (...args) => { 2490 super(...args); 2491 this.x = x; 2492 return this; 2493 }; 2494 if (foo) __super(1); 2495 else __super(2); 2496 } 2497 } 2498 `) 2499 2500 expectPrintedAssignSemanticsTS(t, "class A extends B { #x; y; constructor() { super() } }", 2501 `class A extends B { 2502 #x; 2503 constructor() { 2504 super(); 2505 } 2506 } 2507 `) 2508 2509 expectPrintedAssignSemanticsTS(t, "class A extends B { #x = 1; y; constructor() { super() } }", 2510 `class A extends B { 2511 #x = 1; 2512 constructor() { 2513 super(); 2514 } 2515 } 2516 `) 2517 2518 expectPrintedAssignSemanticsTS(t, "class A extends B { #x; y = 1; constructor() { super() } }", 2519 `class A extends B { 2520 constructor() { 2521 super(); 2522 this.y = 1; 2523 } 2524 #x; 2525 } 2526 `) 2527 2528 expectPrintedAssignSemanticsTS(t, "class A extends B { #x = 1; y = 2; constructor() { super() } }", 2529 `class A extends B { 2530 constructor() { 2531 super(); 2532 this.#x = 1; 2533 this.y = 2; 2534 } 2535 #x; 2536 } 2537 `) 2538 } 2539 2540 func TestTSCall(t *testing.T) { 2541 expectPrintedTS(t, "foo()", "foo();\n") 2542 expectPrintedTS(t, "foo<number>()", "foo();\n") 2543 expectPrintedTS(t, "foo<number, boolean>()", "foo();\n") 2544 } 2545 2546 func TestTSNew(t *testing.T) { 2547 expectPrintedTS(t, "new Foo()", "new Foo();\n") 2548 expectPrintedTS(t, "new Foo<number>()", "new Foo();\n") 2549 expectPrintedTS(t, "new Foo<number, boolean>()", "new Foo();\n") 2550 expectPrintedTS(t, "new Foo<number>", "new Foo();\n") 2551 expectPrintedTS(t, "new Foo<number, boolean>", "new Foo();\n") 2552 2553 expectPrintedTS(t, "new Foo!()", "new Foo();\n") 2554 expectPrintedTS(t, "new Foo!<number>()", "new Foo();\n") 2555 expectPrintedTS(t, "new Foo!.Bar()", "new Foo.Bar();\n") 2556 expectPrintedTS(t, "new Foo!.Bar<number>()", "new Foo.Bar();\n") 2557 expectPrintedTS(t, "new Foo!['Bar']()", "new Foo[\"Bar\"]();\n") 2558 expectPrintedTS(t, "new Foo\n!(x)", "new Foo();\n!x;\n") 2559 expectPrintedTS(t, "new Foo<number>!(x)", "new Foo() < number > !x;\n") 2560 expectParseErrorTS(t, "new Foo<number>!()", "<stdin>: ERROR: Unexpected \")\"\n") 2561 expectParseErrorTS(t, "new Foo\n!.Bar()", "<stdin>: ERROR: Unexpected \".\"\n") 2562 expectParseError(t, "new Foo!()", "<stdin>: ERROR: Unexpected \"!\"\n") 2563 } 2564 2565 func TestTSInstantiationExpression(t *testing.T) { 2566 expectPrintedTS(t, "f<number>", "f;\n") 2567 expectPrintedTS(t, "f<number, boolean>", "f;\n") 2568 expectPrintedTS(t, "f.g<number>", "f.g;\n") 2569 expectPrintedTS(t, "f<number>.g", "f.g;\n") 2570 expectPrintedTS(t, "f<number>.g<number>", "f.g;\n") 2571 expectPrintedTS(t, "f['g']<number>", "f[\"g\"];\n") 2572 expectPrintedTS(t, "(f<number>)<number>", "f;\n") 2573 2574 // Function call 2575 expectPrintedTS(t, "const x1 = f<true>\n(true);", "const x1 = f(true);\n") 2576 // Relational expression 2577 expectPrintedTS(t, "const x1 = f<true>\ntrue;", "const x1 = f;\ntrue;\n") 2578 // Instantiation expression 2579 expectPrintedTS(t, "const x1 = f<true>;\n(true);", "const x1 = f;\ntrue;\n") 2580 2581 // Trailing commas are not allowed 2582 expectPrintedTS(t, "const x = Array<number>\n(0);", "const x = Array(0);\n") 2583 expectPrintedTS(t, "const x = Array<number>;\n(0);", "const x = Array;\n0;\n") 2584 expectParseErrorTS(t, "const x = Array<number,>\n(0);", "<stdin>: ERROR: Expected identifier but found \">\"\n") 2585 expectParseErrorTS(t, "const x = Array<number,>;\n(0);", "<stdin>: ERROR: Expected identifier but found \">\"\n") 2586 2587 expectPrintedTS(t, "f<number>?.();", "f?.();\n") 2588 expectPrintedTS(t, "f?.<number>();", "f?.();\n") 2589 expectPrintedTS(t, "f<<T>() => T>?.();", "f?.();\n") 2590 expectPrintedTS(t, "f?.<<T>() => T>();", "f?.();\n") 2591 2592 expectPrintedTS(t, "f<number>['g'];", "f < number > [\"g\"];\n") 2593 2594 expectPrintedTS(t, "type T21 = typeof Array<string>; f();", "f();\n") 2595 expectPrintedTS(t, "type T22 = typeof Array<string, number>; f();", "f();\n") 2596 2597 expectPrintedTS(t, "f<x>, g<y>;", "f, g;\n") 2598 expectPrintedTS(t, "f<<T>() => T>;", "f;\n") 2599 expectPrintedTS(t, "f.x<<T>() => T>;", "f.x;\n") 2600 expectPrintedTS(t, "f['x']<<T>() => T>;", "f[\"x\"];\n") 2601 expectPrintedTS(t, "f<x>g<y>;", "f < x > g;\n") 2602 expectPrintedTS(t, "f<x>=g<y>;", "f < x >= g;\n") 2603 expectPrintedTS(t, "f<x>>g<y>;", "f < x >> g;\n") 2604 expectPrintedTS(t, "f<x>>>g<y>;", "f < x >>> g;\n") 2605 expectParseErrorTS(t, "f<x>>=g<y>;", "<stdin>: ERROR: Invalid assignment target\n") 2606 expectParseErrorTS(t, "f<x>>>=g<y>;", "<stdin>: ERROR: Invalid assignment target\n") 2607 expectPrintedTS(t, "f<x,y>g<y>;", "f < x, y > g;\n") 2608 expectPrintedTS(t, "f<x,y>=g<y>;", "f < x, y >= g;\n") 2609 expectPrintedTS(t, "f<x,y>>g<y>;", "f < x, y >> g;\n") 2610 expectPrintedTS(t, "f<x,y>>>g<y>;", "f < x, y >>> g;\n") 2611 expectPrintedTS(t, "f<x,y>>=g<y>;", "f < x, y >>= g;\n") 2612 expectPrintedTS(t, "f<x,y>>>=g<y>;", "f < x, y >>>= g;\n") 2613 expectPrintedTS(t, "f<x> = g<y>;", "f = g;\n") 2614 expectParseErrorTS(t, "f<x> > g<y>;", "<stdin>: ERROR: Unexpected \">\"\n") 2615 expectParseErrorTS(t, "f<x> >> g<y>;", "<stdin>: ERROR: Unexpected \">>\"\n") 2616 expectParseErrorTS(t, "f<x> >>> g<y>;", "<stdin>: ERROR: Unexpected \">>>\"\n") 2617 expectParseErrorTS(t, "f<x> >= g<y>;", "<stdin>: ERROR: Unexpected \">=\"\n") 2618 expectParseErrorTS(t, "f<x> >>= g<y>;", "<stdin>: ERROR: Unexpected \">>=\"\n") 2619 expectParseErrorTS(t, "f<x> >>>= g<y>;", "<stdin>: ERROR: Unexpected \">>>=\"\n") 2620 expectPrintedTS(t, "f<x,y> = g<y>;", "f = g;\n") 2621 expectParseErrorTS(t, "f<x,y> > g<y>;", "<stdin>: ERROR: Unexpected \">\"\n") 2622 expectParseErrorTS(t, "f<x,y> >> g<y>;", "<stdin>: ERROR: Unexpected \">>\"\n") 2623 expectParseErrorTS(t, "f<x,y> >>> g<y>;", "<stdin>: ERROR: Unexpected \">>>\"\n") 2624 expectParseErrorTS(t, "f<x,y> >= g<y>;", "<stdin>: ERROR: Unexpected \">=\"\n") 2625 expectParseErrorTS(t, "f<x,y> >>= g<y>;", "<stdin>: ERROR: Unexpected \">>=\"\n") 2626 expectParseErrorTS(t, "f<x,y> >>>= g<y>;", "<stdin>: ERROR: Unexpected \">>>=\"\n") 2627 expectPrintedTS(t, "[f<x>];", "[f];\n") 2628 expectPrintedTS(t, "f<x> ? g<y> : h<z>;", "f ? g : h;\n") 2629 expectPrintedTS(t, "{ f<x> }", "{\n f;\n}\n") 2630 expectPrintedTS(t, "f<x> + g<y>;", "f < x > +g;\n") 2631 expectPrintedTS(t, "f<x> - g<y>;", "f < x > -g;\n") 2632 expectPrintedTS(t, "f<x> * g<y>;", "f * g;\n") 2633 expectPrintedTS(t, "f<x> *= g<y>;", "f *= g;\n") 2634 expectPrintedTS(t, "f<x> == g<y>;", "f == g;\n") 2635 expectPrintedTS(t, "f<x> ?? g<y>;", "f ?? g;\n") 2636 expectPrintedTS(t, "f<x> in g<y>;", "f in g;\n") 2637 expectPrintedTS(t, "f<x> instanceof g<y>;", "f instanceof g;\n") 2638 expectPrintedTS(t, "f<x> as g<y>;", "f;\n") 2639 expectPrintedTS(t, "f<x> satisfies g<y>;", "f;\n") 2640 expectPrintedTS(t, "class A extends B { f() { super.f<x>=y } }", "class A extends B {\n f() {\n super.f < x >= y;\n }\n}\n") 2641 expectPrintedTS(t, "class A extends B { f() { super.f<x,y>=z } }", "class A extends B {\n f() {\n super.f < x, y >= z;\n }\n}\n") 2642 2643 expectParseErrorTS(t, "const a8 = f<number><number>;", "<stdin>: ERROR: Unexpected \";\"\n") 2644 expectParseErrorTS(t, "const b1 = f?.<number>;", "<stdin>: ERROR: Expected \"(\" but found \";\"\n") 2645 2646 // See: https://github.com/microsoft/TypeScript/issues/48711 2647 expectPrintedTS(t, "type x = y\n<number>\nz", "z;\n") 2648 expectPrintedTSX(t, "type x = y\n<number>\nz\n</number>", "/* @__PURE__ */ React.createElement(\"number\", null, \"z\");\n") 2649 expectPrintedTS(t, "type x = typeof y\n<number>\nz", "z;\n") 2650 expectPrintedTSX(t, "type x = typeof y\n<number>\nz\n</number>", "/* @__PURE__ */ React.createElement(\"number\", null, \"z\");\n") 2651 expectPrintedTS(t, "interface Foo { \n (a: number): a \n <T>(): void \n }", "") 2652 expectPrintedTSX(t, "interface Foo { \n (a: number): a \n <T>(): void \n }", "") 2653 expectPrintedTS(t, "interface Foo { \n (a: number): typeof a \n <T>(): void \n }", "") 2654 expectPrintedTSX(t, "interface Foo { \n (a: number): typeof a \n <T>(): void \n }", "") 2655 expectParseErrorTS(t, "type x = y\n<number>\nz\n</number>", "<stdin>: ERROR: Unterminated regular expression\n") 2656 expectParseErrorTSX(t, "type x = y\n<number>\nz", "<stdin>: ERROR: Unexpected end of file before a closing \"number\" tag\n<stdin>: NOTE: The opening \"number\" tag is here:\n") 2657 expectParseErrorTS(t, "type x = typeof y\n<number>\nz\n</number>", "<stdin>: ERROR: Unterminated regular expression\n") 2658 expectParseErrorTSX(t, "type x = typeof y\n<number>\nz", "<stdin>: ERROR: Unexpected end of file before a closing \"number\" tag\n<stdin>: NOTE: The opening \"number\" tag is here:\n") 2659 2660 // See: https://github.com/microsoft/TypeScript/issues/48654 2661 expectPrintedTS(t, "x<true> y", "x < true > y;\n") 2662 expectPrintedTS(t, "x<true>\ny", "x;\ny;\n") 2663 expectPrintedTS(t, "x<true>\nif (y) {}", "x;\nif (y) {\n}\n") 2664 expectPrintedTS(t, "x<true>\nimport 'y'", "x;\nimport \"y\";\n") 2665 expectPrintedTS(t, "x<true>\nimport('y')", "x;\nimport(\"y\");\n") 2666 expectPrintedTS(t, "x<true>\nimport.meta", "x;\nimport.meta;\n") 2667 expectPrintedTS(t, "x<true> import('y')", "x < true > import(\"y\");\n") 2668 expectPrintedTS(t, "x<true> import.meta", "x < true > import.meta;\n") 2669 expectPrintedTS(t, "new x<number> y", "new x() < number > y;\n") 2670 expectPrintedTS(t, "new x<number>\ny", "new x();\ny;\n") 2671 expectPrintedTS(t, "new x<number>\nif (y) {}", "new x();\nif (y) {\n}\n") 2672 expectPrintedTS(t, "new x<true>\nimport 'y'", "new x();\nimport \"y\";\n") 2673 expectPrintedTS(t, "new x<true>\nimport('y')", "new x();\nimport(\"y\");\n") 2674 expectPrintedTS(t, "new x<true>\nimport.meta", "new x();\nimport.meta;\n") 2675 expectPrintedTS(t, "new x<true> import('y')", "new x() < true > import(\"y\");\n") 2676 expectPrintedTS(t, "new x<true> import.meta", "new x() < true > import.meta;\n") 2677 2678 // See: https://github.com/microsoft/TypeScript/issues/48759 2679 expectParseErrorTS(t, "x<true>\nimport<T>('y')", "<stdin>: ERROR: Unexpected \"<\"\n") 2680 expectParseErrorTS(t, "new x<true>\nimport<T>('y')", "<stdin>: ERROR: Unexpected \"<\"\n") 2681 2682 // See: https://github.com/evanw/esbuild/issues/2201 2683 expectParseErrorTS(t, "return Array < ;", "<stdin>: ERROR: Unexpected \";\"\n") 2684 expectParseErrorTS(t, "return Array < > ;", "<stdin>: ERROR: Unexpected \">\"\n") 2685 expectParseErrorTS(t, "return Array < , > ;", "<stdin>: ERROR: Unexpected \",\"\n") 2686 expectPrintedTS(t, "return Array < number > ;", "return Array;\n") 2687 expectPrintedTS(t, "return Array < number > 1;", "return Array < number > 1;\n") 2688 expectPrintedTS(t, "return Array < number > +1;", "return Array < number > 1;\n") 2689 expectPrintedTS(t, "return Array < number > (1);", "return Array(1);\n") 2690 expectPrintedTS(t, "return Array < number >> 1;", "return Array < number >> 1;\n") 2691 expectPrintedTS(t, "return Array < number >>> 1;", "return Array < number >>> 1;\n") 2692 expectPrintedTS(t, "return Array < Array < number >> ;", "return Array;\n") 2693 expectPrintedTS(t, "return Array < Array < number > > ;", "return Array;\n") 2694 expectParseErrorTS(t, "return Array < Array < number > > 1;", "<stdin>: ERROR: Unexpected \">\"\n") 2695 expectPrintedTS(t, "return Array < Array < number >> 1;", "return Array < Array < number >> 1;\n") 2696 expectParseErrorTS(t, "return Array < Array < number > > +1;", "<stdin>: ERROR: Unexpected \">\"\n") 2697 expectPrintedTS(t, "return Array < Array < number >> +1;", "return Array < Array < number >> 1;\n") 2698 expectPrintedTS(t, "return Array < Array < number >> (1);", "return Array(1);\n") 2699 expectPrintedTS(t, "return Array < Array < number > > (1);", "return Array(1);\n") 2700 expectPrintedTS(t, "return Array < number > in x;", "return Array in x;\n") 2701 expectPrintedTS(t, "return Array < Array < number >> in x;", "return Array in x;\n") 2702 expectPrintedTS(t, "return Array < Array < number > > in x;", "return Array in x;\n") 2703 expectPrintedTS(t, "for (var x = Array < number > in y) ;", "x = Array;\nfor (var x in y) ;\n") 2704 expectPrintedTS(t, "for (var x = Array < Array < number >> in y) ;", "x = Array;\nfor (var x in y) ;\n") 2705 expectPrintedTS(t, "for (var x = Array < Array < number > > in y) ;", "x = Array;\nfor (var x in y) ;\n") 2706 2707 // See: https://github.com/microsoft/TypeScript/pull/49353 2708 expectPrintedTS(t, "F<{}> 0", "F < {} > 0;\n") 2709 expectPrintedTS(t, "F<{}> class F<T> {}", "F < {} > class F {\n};\n") 2710 expectPrintedTS(t, "f<{}> function f<T>() {}", "f < {} > function f() {\n};\n") 2711 expectPrintedTS(t, "F<{}>\n0", "F;\n0;\n") 2712 expectPrintedTS(t, "F<{}>\nclass F<T> {}", "F;\nclass F {\n}\n") 2713 expectPrintedTS(t, "f<{}>\nfunction f<T>() {}", "f;\nfunction f() {\n}\n") 2714 } 2715 2716 func TestTSExponentiation(t *testing.T) { 2717 // More info: https://github.com/microsoft/TypeScript/issues/41755 2718 expectParseErrorTS(t, "await x! ** 2", "<stdin>: ERROR: Unexpected \"**\"\n") 2719 expectPrintedTS(t, "await x as any ** 2", "(await x) ** 2;\n") 2720 } 2721 2722 func TestTSImport(t *testing.T) { 2723 expectPrintedTS(t, "import {x} from 'foo'", "") 2724 expectPrintedTS(t, "import {x} from 'foo'; log(x)", "import { x } from \"foo\";\nlog(x);\n") 2725 expectPrintedTS(t, "import {x, y as z} from 'foo'; log(z)", "import { y as z } from \"foo\";\nlog(z);\n") 2726 2727 expectPrintedTS(t, "import x from 'foo'", "") 2728 expectPrintedTS(t, "import x from 'foo'; log(x)", "import x from \"foo\";\nlog(x);\n") 2729 2730 expectPrintedTS(t, "import * as ns from 'foo'", "") 2731 expectPrintedTS(t, "import * as ns from 'foo'; log(ns)", "import * as ns from \"foo\";\nlog(ns);\n") 2732 2733 // Dead control flow must not affect usage tracking 2734 expectPrintedTS(t, "import {x} from 'foo'; if (false) log(x)", "import \"foo\";\nif (false) log(x);\n") 2735 expectPrintedTS(t, "import x from 'foo'; if (false) log(x)", "import \"foo\";\nif (false) log(x);\n") 2736 expectPrintedTS(t, "import * as ns from 'foo'; if (false) log(ns)", "import \"foo\";\nif (false) log(ns);\n") 2737 } 2738 2739 // This is TypeScript-specific export syntax 2740 func TestTSExportEquals(t *testing.T) { 2741 // This use of the "export" keyword should not trigger strict mode because 2742 // this syntax works in CommonJS modules, not in ECMAScript modules 2743 expectPrintedTS(t, "export = []", "module.exports = [];\n") 2744 expectPrintedTS(t, "export = []; with ({}) ;", "with ({}) ;\nmodule.exports = [];\n") 2745 } 2746 2747 // This is TypeScript-specific import syntax 2748 func TestTSImportEquals(t *testing.T) { 2749 // This use of the "export" keyword should not trigger strict mode because 2750 // this syntax works in CommonJS modules, not in ECMAScript modules 2751 expectPrintedTS(t, "import x = require('y')", "const x = require(\"y\");\n") 2752 expectPrintedTS(t, "import x = require('y'); with ({}) ;", "const x = require(\"y\");\nwith ({}) ;\n") 2753 2754 expectPrintedTS(t, "import x = require('foo'); x()", "const x = require(\"foo\");\nx();\n") 2755 expectPrintedTS(t, "import x = require('foo')\nx()", "const x = require(\"foo\");\nx();\n") 2756 expectPrintedTS(t, "import x = require\nx()", "const x = require;\nx();\n") 2757 expectPrintedTS(t, "import x = foo.bar; x()", "const x = foo.bar;\nx();\n") 2758 expectPrintedTS(t, "import x = foo.bar\nx()", "const x = foo.bar;\nx();\n") 2759 expectParseErrorTS(t, "import x = foo()", "<stdin>: ERROR: Expected \";\" but found \"(\"\n") 2760 expectParseErrorTS(t, "import x = foo<T>.bar", "<stdin>: ERROR: Expected \";\" but found \"<\"\n") 2761 expectParseErrorTS(t, "{ import x = foo.bar }", "<stdin>: ERROR: Unexpected \"x\"\n") 2762 2763 expectPrintedTS(t, "export import x = require('foo'); x()", "export const x = require(\"foo\");\nx();\n") 2764 expectPrintedTS(t, "export import x = require('foo')\nx()", "export const x = require(\"foo\");\nx();\n") 2765 expectPrintedTS(t, "export import x = foo.bar; x()", "export const x = foo.bar;\nx();\n") 2766 expectPrintedTS(t, "export import x = foo.bar\nx()", "export const x = foo.bar;\nx();\n") 2767 2768 expectParseError(t, "export import foo = bar", "<stdin>: ERROR: Unexpected \"import\"\n") 2769 expectParseErrorTS(t, "export import {foo} from 'bar'", "<stdin>: ERROR: Expected identifier but found \"{\"\n") 2770 expectParseErrorTS(t, "export import foo from 'bar'", "<stdin>: ERROR: Expected \"=\" but found \"from\"\n") 2771 expectParseErrorTS(t, "export import foo = bar; var x; export {x as foo}", 2772 `<stdin>: ERROR: Multiple exports with the same name "foo" 2773 <stdin>: NOTE: The name "foo" was originally exported here: 2774 `) 2775 expectParseErrorTS(t, "{ export import foo = bar }", "<stdin>: ERROR: Unexpected \"export\"\n") 2776 2777 errorText := `<stdin>: WARNING: This assignment will throw because "x" is a constant 2778 <stdin>: NOTE: The symbol "x" was declared a constant here: 2779 ` 2780 expectParseErrorTS(t, "import x = require('y'); x = require('z')", errorText) 2781 expectParseErrorTS(t, "import x = y.z; x = z.y", errorText) 2782 } 2783 2784 func TestTSImportEqualsInNamespace(t *testing.T) { 2785 expectPrintedTS(t, "namespace ns { import foo = bar }", "") 2786 expectPrintedTS(t, "namespace ns { import foo = bar; type x = foo.x }", "") 2787 expectPrintedTS(t, "namespace ns { import foo = bar.x; foo }", `var ns; 2788 ((ns) => { 2789 const foo = bar.x; 2790 foo; 2791 })(ns || (ns = {})); 2792 `) 2793 expectPrintedTS(t, "namespace ns { export import foo = bar }", `var ns; 2794 ((ns) => { 2795 ns.foo = bar; 2796 })(ns || (ns = {})); 2797 `) 2798 expectPrintedTS(t, "namespace ns { export import foo = bar.x; foo }", `var ns; 2799 ((ns) => { 2800 ns.foo = bar.x; 2801 ns.foo; 2802 })(ns || (ns = {})); 2803 `) 2804 expectParseErrorTS(t, "namespace ns { import {foo} from 'bar' }", "<stdin>: ERROR: Expected identifier but found \"{\"\n") 2805 expectParseErrorTS(t, "namespace ns { import foo from 'bar' }", "<stdin>: ERROR: Expected \"=\" but found \"from\"\n") 2806 expectParseErrorTS(t, "namespace ns { export import {foo} from 'bar' }", "<stdin>: ERROR: Expected identifier but found \"{\"\n") 2807 expectParseErrorTS(t, "namespace ns { export import foo from 'bar' }", "<stdin>: ERROR: Expected \"=\" but found \"from\"\n") 2808 expectParseErrorTS(t, "namespace ns { { import foo = bar } }", "<stdin>: ERROR: Unexpected \"foo\"\n") 2809 expectParseErrorTS(t, "namespace ns { { export import foo = bar } }", "<stdin>: ERROR: Unexpected \"export\"\n") 2810 } 2811 2812 func TestTSTypeOnlyImport(t *testing.T) { 2813 expectPrintedTS(t, "import type foo from 'bar'; x", "x;\n") 2814 expectPrintedTS(t, "import type foo from 'bar'\nx", "x;\n") 2815 expectPrintedTS(t, "import type from from 'bar'; x", "x;\n") 2816 expectPrintedTS(t, "import type * as foo from 'bar'; x", "x;\n") 2817 expectPrintedTS(t, "import type * as foo from 'bar'\nx", "x;\n") 2818 expectPrintedTS(t, "import type {foo, bar as baz} from 'bar'; x", "x;\n") 2819 expectPrintedTS(t, "import type {'foo' as bar} from 'bar'\nx", "x;\n") 2820 expectPrintedTS(t, "import type foo = require('bar'); x", "x;\n") 2821 expectPrintedTS(t, "import type foo = bar.baz; x", "x;\n") 2822 expectPrintedTS(t, "import type from = require('bar'); x", "x;\n") 2823 2824 expectPrintedTS(t, "import type = bar; type", "const type = bar;\ntype;\n") 2825 expectPrintedTS(t, "import type = foo.bar; type", "const type = foo.bar;\ntype;\n") 2826 expectPrintedTS(t, "import type = require('type'); type", "const type = require(\"type\");\ntype;\n") 2827 expectPrintedTS(t, "import type from 'bar'; type", "import type from \"bar\";\ntype;\n") 2828 2829 expectPrintedTS(t, "import { type } from 'mod'; type", "import { type } from \"mod\";\ntype;\n") 2830 expectPrintedTS(t, "import { x, type foo } from 'mod'; x", "import { x } from \"mod\";\nx;\n") 2831 expectPrintedTS(t, "import { x, type as } from 'mod'; x", "import { x } from \"mod\";\nx;\n") 2832 expectPrintedTS(t, "import { x, type foo as bar } from 'mod'; x", "import { x } from \"mod\";\nx;\n") 2833 expectPrintedTS(t, "import { x, type foo as as } from 'mod'; x", "import { x } from \"mod\";\nx;\n") 2834 expectPrintedTS(t, "import { type as as } from 'mod'; as", "import { type as as } from \"mod\";\nas;\n") 2835 expectPrintedTS(t, "import { type as foo } from 'mod'; foo", "import { type as foo } from \"mod\";\nfoo;\n") 2836 expectPrintedTS(t, "import { type as type } from 'mod'; type", "import { type } from \"mod\";\ntype;\n") 2837 expectPrintedTS(t, "import { x, type as as foo } from 'mod'; x", "import { x } from \"mod\";\nx;\n") 2838 expectPrintedTS(t, "import { x, type as as as } from 'mod'; x", "import { x } from \"mod\";\nx;\n") 2839 expectPrintedTS(t, "import { x, type type as as } from 'mod'; x", "import { x } from \"mod\";\nx;\n") 2840 expectPrintedTS(t, "import { x, \\u0074ype y } from 'mod'; x, y", "import { x } from \"mod\";\nx, y;\n") 2841 expectPrintedTS(t, "import { x, type if as y } from 'mod'; x, y", "import { x } from \"mod\";\nx, y;\n") 2842 2843 expectPrintedTS(t, "import a = b; import c = a.c", "") 2844 expectPrintedTS(t, "import c = a.c; import a = b", "") 2845 expectPrintedTS(t, "import a = b; import c = a.c; c()", "const a = b;\nconst c = a.c;\nc();\n") 2846 expectPrintedTS(t, "import c = a.c; import a = b; c()", "const c = a.c;\nconst a = b;\nc();\n") 2847 2848 expectParseErrorTS(t, "import type", "<stdin>: ERROR: Expected \"from\" but found end of file\n") 2849 expectParseErrorTS(t, "import type * foo", "<stdin>: ERROR: Expected \"as\" but found \"foo\"\n") 2850 expectParseErrorTS(t, "import type * as 'bar'", "<stdin>: ERROR: Expected identifier but found \"'bar'\"\n") 2851 expectParseErrorTS(t, "import type { 'bar' }", "<stdin>: ERROR: Expected \"as\" but found \"}\"\n") 2852 2853 expectParseErrorTS(t, "import type foo, * as foo from 'bar'", "<stdin>: ERROR: Expected \"from\" but found \",\"\n") 2854 expectParseErrorTS(t, "import type foo, {foo} from 'bar'", "<stdin>: ERROR: Expected \"from\" but found \",\"\n") 2855 expectParseErrorTS(t, "import type from, * as foo from 'bar'", "<stdin>: ERROR: Expected \"from\" but found \",\"\n") 2856 expectParseErrorTS(t, "import type from, {foo} from 'bar'", "<stdin>: ERROR: Expected \"from\" but found \",\"\n") 2857 expectParseErrorTS(t, "import type * as foo = require('bar')", "<stdin>: ERROR: Expected \"from\" but found \"=\"\n") 2858 expectParseErrorTS(t, "import type {foo} = require('bar')", "<stdin>: ERROR: Expected \"from\" but found \"=\"\n") 2859 2860 expectParseErrorTS(t, "import { type as export } from 'mod'", "<stdin>: ERROR: Expected \"}\" but found \"export\"\n") 2861 expectParseErrorTS(t, "import { type as as export } from 'mod'", "<stdin>: ERROR: Expected \"}\" but found \"export\"\n") 2862 expectParseErrorTS(t, "import { type import } from 'mod'", "<stdin>: ERROR: Expected \"as\" but found \"}\"\n") 2863 expectParseErrorTS(t, "import { type foo bar } from 'mod'", "<stdin>: ERROR: Expected \"}\" but found \"bar\"\n") 2864 expectParseErrorTS(t, "import { type foo as } from 'mod'", "<stdin>: ERROR: Expected identifier but found \"}\"\n") 2865 expectParseErrorTS(t, "import { type foo as bar baz } from 'mod'", "<stdin>: ERROR: Expected \"}\" but found \"baz\"\n") 2866 expectParseErrorTS(t, "import { type as as as as } from 'mod'", "<stdin>: ERROR: Expected \"}\" but found \"as\"\n") 2867 expectParseErrorTS(t, "import { type \\u0061s x } from 'mod'", "<stdin>: ERROR: Expected \"}\" but found \"x\"\n") 2868 expectParseErrorTS(t, "import { type x \\u0061s y } from 'mod'", "<stdin>: ERROR: Expected \"}\" but found \"\\\\u0061s\"\n") 2869 expectParseErrorTS(t, "import { type x as if } from 'mod'", "<stdin>: ERROR: Expected identifier but found \"if\"\n") 2870 expectParseErrorTS(t, "import { type as if } from 'mod'", "<stdin>: ERROR: Expected \"}\" but found \"if\"\n") 2871 2872 // Forbidden names 2873 expectParseErrorTS(t, "import { type as eval } from 'mod'", "<stdin>: ERROR: Cannot use \"eval\" as an identifier here:\n") 2874 expectParseErrorTS(t, "import { type as arguments } from 'mod'", "<stdin>: ERROR: Cannot use \"arguments\" as an identifier here:\n") 2875 2876 // Arbitrary module namespace identifier names 2877 expectPrintedTS(t, "import { x, type 'y' as z } from 'mod'; x, z", "import { x } from \"mod\";\nx, z;\n") 2878 expectParseErrorTS(t, "import { x, type 'y' } from 'mod'", "<stdin>: ERROR: Expected \"as\" but found \"}\"\n") 2879 expectParseErrorTS(t, "import { x, type 'y' as } from 'mod'", "<stdin>: ERROR: Expected identifier but found \"}\"\n") 2880 expectParseErrorTS(t, "import { x, type 'y' as 'z' } from 'mod'", "<stdin>: ERROR: Expected identifier but found \"'z'\"\n") 2881 expectParseErrorTS(t, "import { x, type as 'y' } from 'mod'", "<stdin>: ERROR: Expected \"}\" but found \"'y'\"\n") 2882 expectParseErrorTS(t, "import { x, type y as 'z' } from 'mod'", "<stdin>: ERROR: Expected identifier but found \"'z'\"\n") 2883 } 2884 2885 func TestTSTypeOnlyExport(t *testing.T) { 2886 expectPrintedTS(t, "export type {foo, bar as baz} from 'bar'", "") 2887 expectPrintedTS(t, "export type {foo, bar as baz}", "") 2888 expectPrintedTS(t, "export type {foo} from 'bar'; x", "x;\n") 2889 expectPrintedTS(t, "export type {foo} from 'bar'\nx", "x;\n") 2890 expectPrintedTS(t, "export type {default} from 'bar'", "") 2891 expectParseErrorTS(t, "export type {default}", "<stdin>: ERROR: Expected identifier but found \"default\"\n") 2892 2893 expectPrintedTS(t, "export { type } from 'mod'; type", "export { type } from \"mod\";\ntype;\n") 2894 expectPrintedTS(t, "export { type, as } from 'mod'", "export { type, as } from \"mod\";\n") 2895 expectPrintedTS(t, "export { x, type foo } from 'mod'; x", "export { x } from \"mod\";\nx;\n") 2896 expectPrintedTS(t, "export { x, type as } from 'mod'; x", "export { x } from \"mod\";\nx;\n") 2897 expectPrintedTS(t, "export { x, type foo as bar } from 'mod'; x", "export { x } from \"mod\";\nx;\n") 2898 expectPrintedTS(t, "export { x, type foo as as } from 'mod'; x", "export { x } from \"mod\";\nx;\n") 2899 expectPrintedTS(t, "export { type as as } from 'mod'; as", "export { type as as } from \"mod\";\nas;\n") 2900 expectPrintedTS(t, "export { type as foo } from 'mod'; foo", "export { type as foo } from \"mod\";\nfoo;\n") 2901 expectPrintedTS(t, "export { type as type } from 'mod'; type", "export { type } from \"mod\";\ntype;\n") 2902 expectPrintedTS(t, "export { x, type as as foo } from 'mod'; x", "export { x } from \"mod\";\nx;\n") 2903 expectPrintedTS(t, "export { x, type as as as } from 'mod'; x", "export { x } from \"mod\";\nx;\n") 2904 expectPrintedTS(t, "export { x, type type as as } from 'mod'; x", "export { x } from \"mod\";\nx;\n") 2905 expectPrintedTS(t, "export { x, \\u0074ype y }; let x, y", "export { x };\nlet x, y;\n") 2906 expectPrintedTS(t, "export { x, \\u0074ype y } from 'mod'", "export { x } from \"mod\";\n") 2907 expectPrintedTS(t, "export { x, type if } from 'mod'", "export { x } from \"mod\";\n") 2908 expectPrintedTS(t, "export { x, type y as if }; let x", "export { x };\nlet x;\n") 2909 2910 expectParseErrorTS(t, "export { type foo bar } from 'mod'", "<stdin>: ERROR: Expected \"}\" but found \"bar\"\n") 2911 expectParseErrorTS(t, "export { type foo as } from 'mod'", "<stdin>: ERROR: Expected identifier but found \"}\"\n") 2912 expectParseErrorTS(t, "export { type foo as bar baz } from 'mod'", "<stdin>: ERROR: Expected \"}\" but found \"baz\"\n") 2913 expectParseErrorTS(t, "export { type as as as as } from 'mod'", "<stdin>: ERROR: Expected \"}\" but found \"as\"\n") 2914 expectParseErrorTS(t, "export { type \\u0061s x } from 'mod'", "<stdin>: ERROR: Expected \"}\" but found \"x\"\n") 2915 expectParseErrorTS(t, "export { type x \\u0061s y } from 'mod'", "<stdin>: ERROR: Expected \"}\" but found \"\\\\u0061s\"\n") 2916 expectParseErrorTS(t, "export { x, type if }", "<stdin>: ERROR: Expected identifier but found \"if\"\n") 2917 2918 // Arbitrary module namespace identifier names 2919 expectPrintedTS(t, "export { type as \"\" } from 'mod'", "export { type as \"\" } from \"mod\";\n") 2920 expectPrintedTS(t, "export { x, type as as \"\" } from 'mod'", "export { x } from \"mod\";\n") 2921 expectPrintedTS(t, "export { x, type x as \"\" } from 'mod'", "export { x } from \"mod\";\n") 2922 expectPrintedTS(t, "export { x, type \"\" as x } from 'mod'", "export { x } from \"mod\";\n") 2923 expectPrintedTS(t, "export { x, type \"\" as \" \" } from 'mod'", "export { x } from \"mod\";\n") 2924 expectPrintedTS(t, "export { x, type \"\" } from 'mod'", "export { x } from \"mod\";\n") 2925 expectParseErrorTS(t, "export { type \"\" }", "<stdin>: ERROR: Expected identifier but found \"\\\"\\\"\"\n") 2926 expectParseErrorTS(t, "export { type \"\" as x }", "<stdin>: ERROR: Expected identifier but found \"\\\"\\\"\"\n") 2927 expectParseErrorTS(t, "export { type \"\" as \" \" }", "<stdin>: ERROR: Expected identifier but found \"\\\"\\\"\"\n") 2928 2929 // Named exports should be removed if they don't refer to a local symbol 2930 expectPrintedTS(t, "const Foo = {}; export {Foo}", "const Foo = {};\nexport { Foo };\n") 2931 expectPrintedTS(t, "type Foo = {}; export {Foo}", "export {};\n") 2932 expectPrintedTS(t, "const Foo = {}; export {Foo as Bar}", "const Foo = {};\nexport { Foo as Bar };\n") 2933 expectPrintedTS(t, "type Foo = {}; export {Foo as Bar}", "export {};\n") 2934 expectPrintedTS(t, "import Foo from 'foo'; export {Foo}", "import Foo from \"foo\";\nexport { Foo };\n") 2935 expectPrintedTS(t, "import {Foo} from 'foo'; export {Foo}", "import { Foo } from \"foo\";\nexport { Foo };\n") 2936 expectPrintedTS(t, "import * as Foo from 'foo'; export {Foo}", "import * as Foo from \"foo\";\nexport { Foo };\n") 2937 expectPrintedTS(t, "{ var Foo; } export {Foo}", "{\n var Foo;\n}\nexport { Foo };\n") 2938 expectPrintedTS(t, "{ let Foo; } export {Foo}", "{\n let Foo;\n}\nexport {};\n") 2939 expectPrintedTS(t, "export {Foo}", "export {};\n") 2940 expectParseError(t, "export {Foo}", "<stdin>: ERROR: \"Foo\" is not declared in this file\n") 2941 2942 // This is a syntax error in TypeScript, but we parse it anyway because 2943 // people blame esbuild when it doesn't parse. It's silently discarded 2944 // because we always discard all type annotations (even invalid ones). 2945 expectPrintedTS(t, "export type * from 'foo'\nbar", "bar;\n") 2946 expectPrintedTS(t, "export type * as foo from 'bar'; foo", "foo;\n") 2947 expectPrintedTS(t, "export type * as 'f o' from 'bar'; foo", "foo;\n") 2948 } 2949 2950 func TestTSOptionalChain(t *testing.T) { 2951 expectParseError(t, "a?.<T>()", "<stdin>: ERROR: Expected identifier but found \"<\"\n") 2952 expectParseError(t, "a?.<<T>() => T>()", "<stdin>: ERROR: Expected identifier but found \"<<\"\n") 2953 expectPrintedTS(t, "a?.<T>()", "a?.();\n") 2954 expectPrintedTS(t, "a?.<<T>() => T>()", "a?.();\n") 2955 expectParseErrorTS(t, "a?.<T>b", "<stdin>: ERROR: Expected \"(\" but found \"b\"\n") 2956 expectParseErrorTS(t, "a?.<T>[b]", "<stdin>: ERROR: Expected \"(\" but found \"[\"\n") 2957 expectParseErrorTS(t, "a?.<<T>() => T>b", "<stdin>: ERROR: Expected \"(\" but found \"b\"\n") 2958 expectParseErrorTS(t, "a?.<<T>() => T>[b]", "<stdin>: ERROR: Expected \"(\" but found \"[\"\n") 2959 2960 expectPrintedTS(t, "a?.b.c", "a?.b.c;\n") 2961 expectPrintedTS(t, "(a?.b).c", "(a?.b).c;\n") 2962 expectPrintedTS(t, "a?.b!.c", "a?.b.c;\n") 2963 2964 expectPrintedTS(t, "a?.b[c]", "a?.b[c];\n") 2965 expectPrintedTS(t, "(a?.b)[c]", "(a?.b)[c];\n") 2966 expectPrintedTS(t, "a?.b![c]", "a?.b[c];\n") 2967 2968 expectPrintedTS(t, "a?.b(c)", "a?.b(c);\n") 2969 expectPrintedTS(t, "(a?.b)(c)", "(a?.b)(c);\n") 2970 expectPrintedTS(t, "a?.b!(c)", "a?.b(c);\n") 2971 2972 expectPrintedTS(t, "a?.b<T>(c)", "a?.b(c);\n") 2973 expectPrintedTS(t, "a?.b<+T>(c)", "a?.b < +T > c;\n") 2974 expectPrintedTS(t, "a?.b<<T>() => T>(c)", "a?.b(c);\n") 2975 } 2976 2977 func TestTSJSX(t *testing.T) { 2978 expectParseErrorTSX(t, "<div>></div>", 2979 "<stdin>: ERROR: The character \">\" is not valid inside a JSX element\n"+ 2980 "NOTE: Did you mean to escape it as \"{'>'}\" instead?\n") 2981 expectParseErrorTSX(t, "<div>{1}}</div>", 2982 "<stdin>: ERROR: The character \"}\" is not valid inside a JSX element\n"+ 2983 "NOTE: Did you mean to escape it as \"{'}'}\" instead?\n") 2984 2985 expectPrintedTS(t, "const x = <number>1", "const x = 1;\n") 2986 expectPrintedTSX(t, "const x = <number>1</number>", "const x = /* @__PURE__ */ React.createElement(\"number\", null, \"1\");\n") 2987 expectParseErrorTSX(t, "const x = <number>1", "<stdin>: ERROR: Unexpected end of file before a closing \"number\" tag\n<stdin>: NOTE: The opening \"number\" tag is here:\n") 2988 2989 expectPrintedTSX(t, "<x>a{}c</x>", "/* @__PURE__ */ React.createElement(\"x\", null, \"a\", \"c\");\n") 2990 expectPrintedTSX(t, "<x>a{/* comment */}c</x>", "/* @__PURE__ */ React.createElement(\"x\", null, \"a\", \"c\");\n") 2991 expectPrintedTSX(t, "<x>a{b}c</x>", "/* @__PURE__ */ React.createElement(\"x\", null, \"a\", b, \"c\");\n") 2992 expectPrintedTSX(t, "<x>a{...b}c</x>", "/* @__PURE__ */ React.createElement(\"x\", null, \"a\", ...b, \"c\");\n") 2993 2994 expectPrintedTSX(t, "const x = <Foo<T>></Foo>", "const x = /* @__PURE__ */ React.createElement(Foo, null);\n") 2995 expectPrintedTSX(t, "const x = <Foo<T> data-foo></Foo>", "const x = /* @__PURE__ */ React.createElement(Foo, { \"data-foo\": true });\n") 2996 expectParseErrorTSX(t, "const x = <Foo<T>=>", "<stdin>: ERROR: Expected \">\" but found \"=>\"\n") 2997 2998 expectPrintedTS(t, "const x = <T>() => {}", "const x = () => {\n};\n") 2999 expectPrintedTS(t, "const x = <T>(y)", "const x = y;\n") 3000 expectPrintedTS(t, "const x = <T>(y, z)", "const x = (y, z);\n") 3001 expectPrintedTS(t, "const x = <T>(y: T) => {}", "const x = (y) => {\n};\n") 3002 expectPrintedTS(t, "const x = <T>(y, z) => {}", "const x = (y, z) => {\n};\n") 3003 expectPrintedTS(t, "const x = <T = X>(y: T) => {}", "const x = (y) => {\n};\n") 3004 expectPrintedTS(t, "const x = <T = X>(y, z) => {}", "const x = (y, z) => {\n};\n") 3005 expectPrintedTS(t, "const x = <T extends X>(y: T) => {}", "const x = (y) => {\n};\n") 3006 expectPrintedTS(t, "const x = <T extends X>(y, z) => {}", "const x = (y, z) => {\n};\n") 3007 expectPrintedTS(t, "const x = <T extends X = Y>(y: T) => {}", "const x = (y) => {\n};\n") 3008 expectPrintedTS(t, "const x = <T extends X = Y>(y, z) => {}", "const x = (y, z) => {\n};\n") 3009 3010 expectPrintedTS(t, "const x = async <T>() => {}", "const x = async () => {\n};\n") 3011 expectPrintedTS(t, "const x = async <T>(y)", "const x = async(y);\n") 3012 expectPrintedTS(t, "const x = async\n<T>(y)", "const x = async(y);\n") 3013 expectPrintedTS(t, "const x = async <T>(y, z)", "const x = async(y, z);\n") 3014 expectPrintedTS(t, "const x = async <T>(y: T) => {}", "const x = async (y) => {\n};\n") 3015 expectPrintedTS(t, "const x = async <T>(y, z) => {}", "const x = async (y, z) => {\n};\n") 3016 expectPrintedTS(t, "const x = async <T = X>(y: T) => {}", "const x = async (y) => {\n};\n") 3017 expectPrintedTS(t, "const x = async <T = X>(y, z) => {}", "const x = async (y, z) => {\n};\n") 3018 expectPrintedTS(t, "const x = async <T extends X>(y: T) => {}", "const x = async (y) => {\n};\n") 3019 expectPrintedTS(t, "const x = async <T extends X>(y, z) => {}", "const x = async (y, z) => {\n};\n") 3020 expectPrintedTS(t, "const x = async <T extends X = Y>(y: T) => {}", "const x = async (y) => {\n};\n") 3021 expectPrintedTS(t, "const x = async <T extends X = Y>(y, z) => {}", "const x = async (y, z) => {\n};\n") 3022 expectPrintedTS(t, "const x = (async <T, X> y)", "const x = (async < T, X > y);\n") 3023 expectPrintedTS(t, "const x = (async <T, X>(y))", "const x = async(y);\n") 3024 expectParseErrorTS(t, "const x = async <T,>(y)", "<stdin>: ERROR: Expected \"=>\" but found end of file\n") 3025 expectParseErrorTS(t, "const x = async <T>(y: T)", "<stdin>: ERROR: Unexpected \":\"\n") 3026 expectParseErrorTS(t, "const x = async\n<T>() => {}", "<stdin>: ERROR: Expected \";\" but found \"=>\"\n") 3027 expectParseErrorTS(t, "const x = async\n<T>(x) => {}", "<stdin>: ERROR: Expected \";\" but found \"=>\"\n") 3028 3029 expectPrintedTS(t, "const x = <{}>() => {}", "const x = () => {\n};\n") 3030 expectPrintedTS(t, "const x = <{}>(y)", "const x = y;\n") 3031 expectPrintedTS(t, "const x = <{}>(y, z)", "const x = (y, z);\n") 3032 expectPrintedTS(t, "const x = <{}>(y, z) => {}", "const x = (y, z) => {\n};\n") 3033 3034 expectPrintedTS(t, "const x = <[]>() => {}", "const x = () => {\n};\n") 3035 expectPrintedTS(t, "const x = <[]>(y)", "const x = y;\n") 3036 expectPrintedTS(t, "const x = <[]>(y, z)", "const x = (y, z);\n") 3037 expectPrintedTS(t, "const x = <[]>(y, z) => {}", "const x = (y, z) => {\n};\n") 3038 3039 invalid := "<stdin>: ERROR: The character \">\" is not valid inside a JSX element\nNOTE: Did you mean to escape it as \"{'>'}\" instead?\n" 3040 invalidWithHint := "<stdin>: ERROR: The character \">\" is not valid inside a JSX element\n<stdin>: NOTE: TypeScript's TSX syntax interprets " + 3041 "arrow functions with a single generic type parameter as an opening JSX element. If you want it to be interpreted as an arrow function instead, " + 3042 "you need to add a trailing comma after the type parameter to disambiguate:\n" 3043 expectPrintedTSX(t, "<T extends/>", "/* @__PURE__ */ React.createElement(T, { extends: true });\n") 3044 expectPrintedTSX(t, "<T extends>(y) = {}</T>", "/* @__PURE__ */ React.createElement(T, { extends: true }, \"(y) = \");\n") 3045 expectParseErrorTSX(t, "<T extends X/>", "<stdin>: ERROR: Expected \">\" but found \"/\"\n") 3046 expectParseErrorTSX(t, "<T extends X>(y) = {}</T>", "<stdin>: ERROR: Expected \"=>\" but found \"=\"\n") 3047 expectParseErrorTSX(t, "(<T>(y) => {}</T>)", invalidWithHint) 3048 expectParseErrorTSX(t, "(<T>(x: X<Y>) => {}</Y></T>)", invalidWithHint) 3049 expectParseErrorTSX(t, "(<T extends>(y) => {}</T>)", invalid) 3050 expectParseErrorTSX(t, "(<T extends={false}>(y) => {}</T>)", invalid) 3051 expectPrintedTSX(t, "(<T = X>(y) => {})", "(y) => {\n};\n") 3052 expectPrintedTSX(t, "(<T extends X>(y) => {})", "(y) => {\n};\n") 3053 expectPrintedTSX(t, "(<T extends X = Y>(y) => {})", "(y) => {\n};\n") 3054 expectPrintedTSX(t, "(<T,>() => {})", "() => {\n};\n") 3055 expectPrintedTSX(t, "(<T, X>(y) => {})", "(y) => {\n};\n") 3056 expectPrintedTSX(t, "(<T, X>(y): (() => {}) => {})", "(y) => {\n};\n") 3057 expectParseErrorTSX(t, "(<T>() => {})", invalidWithHint+"<stdin>: ERROR: Unexpected end of file before a closing \"T\" tag\n<stdin>: NOTE: The opening \"T\" tag is here:\n") 3058 expectParseErrorTSX(t, "(<T>(x: X<Y>) => {})", invalidWithHint+"<stdin>: ERROR: Unexpected end of file before a closing \"Y\" tag\n<stdin>: NOTE: The opening \"Y\" tag is here:\n") 3059 expectParseErrorTSX(t, "(<T>(x: X<Y>) => {})</Y>", invalidWithHint+"<stdin>: ERROR: Unexpected end of file before a closing \"T\" tag\n<stdin>: NOTE: The opening \"T\" tag is here:\n") 3060 expectParseErrorTSX(t, "(<[]>(y))", "<stdin>: ERROR: Expected identifier but found \"[\"\n") 3061 expectParseErrorTSX(t, "(<T[]>(y))", "<stdin>: ERROR: Expected \">\" but found \"[\"\n") 3062 expectParseErrorTSX(t, "(<T = X>(y))", "<stdin>: ERROR: Expected \"=>\" but found \")\"\n") 3063 expectParseErrorTSX(t, "(<T, X>(y))", "<stdin>: ERROR: Expected \"=>\" but found \")\"\n") 3064 expectParseErrorTSX(t, "(<T, X>y => {})", "<stdin>: ERROR: Expected \"(\" but found \"y\"\n") 3065 3066 // TypeScript doesn't currently parse these even though it seems unambiguous 3067 expectPrintedTSX(t, "async <T,>() => {}", "async () => {\n};\n") 3068 expectPrintedTSX(t, "async <T extends X>() => {}", "async () => {\n};\n") 3069 expectPrintedTSX(t, "async <T>()", "async();\n") 3070 expectParseErrorTSX(t, "async <T>() => {}", "<stdin>: ERROR: Expected \";\" but found \"=>\"\n") 3071 expectParseErrorTSX(t, "async <T extends>() => {}", "<stdin>: ERROR: Expected \";\" but found \"extends\"\n") 3072 } 3073 3074 func TestTSNoAmbiguousLessThan(t *testing.T) { 3075 expectPrintedTSNoAmbiguousLessThan(t, "(<T,>() => {})", "() => {\n};\n") 3076 expectPrintedTSNoAmbiguousLessThan(t, "(<T, X>() => {})", "() => {\n};\n") 3077 expectPrintedTSNoAmbiguousLessThan(t, "(<T extends X>() => {})", "() => {\n};\n") 3078 expectParseErrorTSNoAmbiguousLessThan(t, "(<T>x)", 3079 "<stdin>: ERROR: This syntax is not allowed in files with the \".mts\" or \".cts\" extension\n") 3080 expectParseErrorTSNoAmbiguousLessThan(t, "(<T>() => {})", 3081 "<stdin>: ERROR: This syntax is not allowed in files with the \".mts\" or \".cts\" extension\n") 3082 expectParseErrorTSNoAmbiguousLessThan(t, "(<T>(x) => {})", 3083 "<stdin>: ERROR: This syntax is not allowed in files with the \".mts\" or \".cts\" extension\n") 3084 expectParseErrorTSNoAmbiguousLessThan(t, "<x>y</x>", 3085 "<stdin>: ERROR: This syntax is not allowed in files with the \".mts\" or \".cts\" extension\n"+ 3086 "<stdin>: ERROR: Unterminated regular expression\n") 3087 expectParseErrorTSNoAmbiguousLessThan(t, "<x extends></x>", 3088 "<stdin>: ERROR: This syntax is not allowed in files with the \".mts\" or \".cts\" extension\n"+ 3089 "<stdin>: ERROR: Unexpected \">\"\n") 3090 expectParseErrorTSNoAmbiguousLessThan(t, "<x extends={y}></x>", 3091 "<stdin>: ERROR: This syntax is not allowed in files with the \".mts\" or \".cts\" extension\n"+ 3092 "<stdin>: ERROR: Unexpected \"=\"\n") 3093 } 3094 3095 func TestTSClassSideEffectOrder(t *testing.T) { 3096 // The order of computed property side effects must not change 3097 expectPrintedAssignSemanticsTS(t, `class Foo { 3098 [a()]() {} 3099 [b()]; 3100 [c()] = 1; 3101 [d()]() {} 3102 static [e()]; 3103 static [f()] = 1; 3104 static [g()]() {} 3105 [h()]; 3106 } 3107 `, `var _a, _b, _c; 3108 class Foo { 3109 constructor() { 3110 this[_c] = 1; 3111 } 3112 [a()]() { 3113 } 3114 [(b(), _c = c(), d())]() { 3115 } 3116 static { 3117 this[_b] = 1; 3118 } 3119 static [(e(), _b = f(), _a = g(), h(), _a)]() { 3120 } 3121 } 3122 `) 3123 expectPrintedAssignSemanticsTS(t, `class Foo { 3124 static [x()] = 1; 3125 } 3126 `, `var _a; 3127 _a = x(); 3128 class Foo { 3129 static { 3130 this[_a] = 1; 3131 } 3132 } 3133 `) 3134 expectPrintedAssignSemanticsTargetTS(t, 2021, `class Foo { 3135 [a()]() {} 3136 [b()]; 3137 [c()] = 1; 3138 [d()]() {} 3139 static [e()]; 3140 static [f()] = 1; 3141 static [g()]() {} 3142 [h()]; 3143 } 3144 `, `var _a, _b, _c; 3145 class Foo { 3146 constructor() { 3147 this[_c] = 1; 3148 } 3149 [a()]() { 3150 } 3151 [(b(), _c = c(), d())]() { 3152 } 3153 static [(e(), _b = f(), _a = g(), h(), _a)]() { 3154 } 3155 } 3156 Foo[_b] = 1; 3157 `) 3158 } 3159 3160 func TestTSMangleStringEnumLength(t *testing.T) { 3161 expectPrintedTS(t, "enum x { y = '' } z = x.y.length", 3162 "var x = /* @__PURE__ */ ((x) => {\n x[\"y\"] = \"\";\n return x;\n})(x || {});\nz = \"\" /* y */.length;\n") 3163 3164 expectPrintedMangleTS(t, "enum x { y = '' } x.y.length++", 3165 "var x = /* @__PURE__ */ ((x) => (x.y = \"\", x))(x || {});\n\"\" /* y */.length++;\n") 3166 3167 expectPrintedMangleTS(t, "enum x { y = '' } x.y.length = z", 3168 "var x = /* @__PURE__ */ ((x) => (x.y = \"\", x))(x || {});\n\"\" /* y */.length = z;\n") 3169 3170 expectPrintedMangleTS(t, "enum x { y = '' } z = x.y.length", 3171 "var x = /* @__PURE__ */ ((x) => (x.y = \"\", x))(x || {});\nz = 0;\n") 3172 3173 expectPrintedMangleTS(t, "enum x { y = 'abc' } z = x.y.length", 3174 "var x = /* @__PURE__ */ ((x) => (x.y = \"abc\", x))(x || {});\nz = 3;\n") 3175 3176 expectPrintedMangleTS(t, "enum x { y = 'ȧḃċ' } z = x.y.length", 3177 "var x = /* @__PURE__ */ ((x) => (x.y = \"ȧḃċ\", x))(x || {});\nz = 3;\n") 3178 3179 expectPrintedMangleTS(t, "enum x { y = '👯♂️' } z = x.y.length", 3180 "var x = /* @__PURE__ */ ((x) => (x.y = \"👯♂️\", x))(x || {});\nz = 5;\n") 3181 } 3182 3183 func TestTSES5(t *testing.T) { 3184 // Errors from lowering hypothetical arrow function arguments to ES5 should 3185 // not leak out when backtracking. This comes up when parentheses are followed 3186 // by a colon in TypeScript because the colon could deliminate an arrow 3187 // function return type. See: https://github.com/evanw/esbuild/issues/2375. 3188 expectPrintedTargetTS(t, 2015, "0 ? ([]) : 0", "0 ? [] : 0;\n") 3189 expectPrintedTargetTS(t, 2015, "0 ? ({}) : 0", "0 ? {} : 0;\n") 3190 expectPrintedTargetTS(t, 5, "0 ? ([]) : 0", "0 ? [] : 0;\n") 3191 expectPrintedTargetTS(t, 5, "0 ? ({}) : 0", "0 ? {} : 0;\n") 3192 expectPrintedTargetTS(t, 2015, "0 ? ([]): 0 => 0 : 0", "0 ? ([]) => 0 : 0;\n") 3193 expectPrintedTargetTS(t, 2015, "0 ? ({}): 0 => 0 : 0", "0 ? ({}) => 0 : 0;\n") 3194 expectParseErrorTargetTS(t, 5, "0 ? ([]): 0 => 0 : 0", "<stdin>: ERROR: Transforming destructuring to the configured target environment is not supported yet\n") 3195 expectParseErrorTargetTS(t, 5, "0 ? ({}): 0 => 0 : 0", "<stdin>: ERROR: Transforming destructuring to the configured target environment is not supported yet\n") 3196 } 3197 3198 func TestTSUsing(t *testing.T) { 3199 expectPrintedTS(t, "using x = y", "using x = y;\n") 3200 expectPrintedTS(t, "using x: any = y", "using x = y;\n") 3201 expectPrintedTS(t, "using x: any = y, z: any = _", "using x = y, z = _;\n") 3202 expectParseErrorTS(t, "export using x: any = y", "<stdin>: ERROR: Unexpected \"using\"\n") 3203 expectParseErrorTS(t, "namespace ns { export using x: any = y }", "<stdin>: ERROR: Unexpected \"using\"\n") 3204 }