tsc-ts 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (584) hide show
  1. package/README.md +33 -1
  2. package/dist/api/compiler/ast-utilities.d.ts +82 -0
  3. package/dist/api/compiler/ast-utilities.js +462 -0
  4. package/dist/api/compiler/ast.d.ts +2350 -0
  5. package/dist/api/compiler/ast.js +11206 -0
  6. package/dist/api/compiler/astnav.d.ts +37 -0
  7. package/dist/api/compiler/astnav.js +767 -0
  8. package/dist/api/compiler/binder-utilities.d.ts +182 -0
  9. package/dist/api/compiler/binder-utilities.js +1240 -0
  10. package/dist/api/compiler/binder.d.ts +33 -0
  11. package/dist/api/compiler/binder.js +2358 -0
  12. package/dist/api/compiler/character-codes.d.ts +161 -0
  13. package/dist/api/compiler/character-codes.js +160 -0
  14. package/dist/api/compiler/checker-calls.d.ts +46 -0
  15. package/dist/api/compiler/checker-calls.js +1640 -0
  16. package/dist/api/compiler/checker-classes.d.ts +30 -0
  17. package/dist/api/compiler/checker-classes.js +582 -0
  18. package/dist/api/compiler/checker-contextual.d.ts +77 -0
  19. package/dist/api/compiler/checker-contextual.js +1136 -0
  20. package/dist/api/compiler/checker-declaration-order.d.ts +12 -0
  21. package/dist/api/compiler/checker-declaration-order.js +356 -0
  22. package/dist/api/compiler/checker-declarations.d.ts +33 -0
  23. package/dist/api/compiler/checker-declarations.js +888 -0
  24. package/dist/api/compiler/checker-declared-types.d.ts +48 -0
  25. package/dist/api/compiler/checker-declared-types.js +787 -0
  26. package/dist/api/compiler/checker-decorators.d.ts +26 -0
  27. package/dist/api/compiler/checker-decorators.js +562 -0
  28. package/dist/api/compiler/checker-deprecation.d.ts +33 -0
  29. package/dist/api/compiler/checker-deprecation.js +315 -0
  30. package/dist/api/compiler/checker-destructuring.d.ts +34 -0
  31. package/dist/api/compiler/checker-destructuring.js +584 -0
  32. package/dist/api/compiler/checker-elaboration.d.ts +15 -0
  33. package/dist/api/compiler/checker-elaboration.js +290 -0
  34. package/dist/api/compiler/checker-emit-helpers.d.ts +58 -0
  35. package/dist/api/compiler/checker-emit-helpers.js +154 -0
  36. package/dist/api/compiler/checker-emit-resolver.d.ts +75 -0
  37. package/dist/api/compiler/checker-emit-resolver.js +579 -0
  38. package/dist/api/compiler/checker-evaluator.d.ts +27 -0
  39. package/dist/api/compiler/checker-evaluator.js +188 -0
  40. package/dist/api/compiler/checker-expressions.d.ts +67 -0
  41. package/dist/api/compiler/checker-expressions.js +1351 -0
  42. package/dist/api/compiler/checker-features.d.ts +6 -0
  43. package/dist/api/compiler/checker-features.js +354 -0
  44. package/dist/api/compiler/checker-flow.d.ts +86 -0
  45. package/dist/api/compiler/checker-flow.js +169 -0
  46. package/dist/api/compiler/checker-functions.d.ts +43 -0
  47. package/dist/api/compiler/checker-functions.js +1002 -0
  48. package/dist/api/compiler/checker-generated-code.d.ts +25 -0
  49. package/dist/api/compiler/checker-generated-code.js +168 -0
  50. package/dist/api/compiler/checker-globals.d.ts +14 -0
  51. package/dist/api/compiler/checker-globals.js +37 -0
  52. package/dist/api/compiler/checker-grammar.d.ts +35 -0
  53. package/dist/api/compiler/checker-grammar.js +2573 -0
  54. package/dist/api/compiler/checker-heritage.d.ts +28 -0
  55. package/dist/api/compiler/checker-heritage.js +900 -0
  56. package/dist/api/compiler/checker-inference.d.ts +179 -0
  57. package/dist/api/compiler/checker-inference.js +2105 -0
  58. package/dist/api/compiler/checker-iteration.d.ts +115 -0
  59. package/dist/api/compiler/checker-iteration.js +857 -0
  60. package/dist/api/compiler/checker-javascript.d.ts +42 -0
  61. package/dist/api/compiler/checker-javascript.js +441 -0
  62. package/dist/api/compiler/checker-jsdoc.d.ts +30 -0
  63. package/dist/api/compiler/checker-jsdoc.js +320 -0
  64. package/dist/api/compiler/checker-jsx.d.ts +94 -0
  65. package/dist/api/compiler/checker-jsx.js +1433 -0
  66. package/dist/api/compiler/checker-literals.d.ts +6 -0
  67. package/dist/api/compiler/checker-literals.js +43 -0
  68. package/dist/api/compiler/checker-members.d.ts +198 -0
  69. package/dist/api/compiler/checker-members.js +2126 -0
  70. package/dist/api/compiler/checker-modules.d.ts +85 -0
  71. package/dist/api/compiler/checker-modules.js +2141 -0
  72. package/dist/api/compiler/checker-narrowing.d.ts +120 -0
  73. package/dist/api/compiler/checker-narrowing.js +2797 -0
  74. package/dist/api/compiler/checker-object-literals.d.ts +14 -0
  75. package/dist/api/compiler/checker-object-literals.js +479 -0
  76. package/dist/api/compiler/checker-operators.d.ts +33 -0
  77. package/dist/api/compiler/checker-operators.js +1040 -0
  78. package/dist/api/compiler/checker-overloads.d.ts +7 -0
  79. package/dist/api/compiler/checker-overloads.js +270 -0
  80. package/dist/api/compiler/checker-property-access.d.ts +34 -0
  81. package/dist/api/compiler/checker-property-access.js +672 -0
  82. package/dist/api/compiler/checker-services.d.ts +49 -0
  83. package/dist/api/compiler/checker-services.js +995 -0
  84. package/dist/api/compiler/checker-signatures.d.ts +55 -0
  85. package/dist/api/compiler/checker-signatures.js +788 -0
  86. package/dist/api/compiler/checker-source-file.d.ts +27 -0
  87. package/dist/api/compiler/checker-source-file.js +619 -0
  88. package/dist/api/compiler/checker-statements.d.ts +18 -0
  89. package/dist/api/compiler/checker-statements.js +226 -0
  90. package/dist/api/compiler/checker-super.d.ts +13 -0
  91. package/dist/api/compiler/checker-super.js +241 -0
  92. package/dist/api/compiler/checker-symbol-types.d.ts +67 -0
  93. package/dist/api/compiler/checker-symbol-types.js +1885 -0
  94. package/dist/api/compiler/checker-symbols.d.ts +181 -0
  95. package/dist/api/compiler/checker-symbols.js +1782 -0
  96. package/dist/api/compiler/checker-type-factory.d.ts +142 -0
  97. package/dist/api/compiler/checker-type-factory.js +2501 -0
  98. package/dist/api/compiler/checker-type-nodes.d.ts +26 -0
  99. package/dist/api/compiler/checker-type-nodes.js +828 -0
  100. package/dist/api/compiler/checker-type-operators.d.ts +109 -0
  101. package/dist/api/compiler/checker-type-operators.js +1842 -0
  102. package/dist/api/compiler/checker-types.d.ts +1027 -0
  103. package/dist/api/compiler/checker-types.js +1697 -0
  104. package/dist/api/compiler/checker-unused.d.ts +19 -0
  105. package/dist/api/compiler/checker-unused.js +524 -0
  106. package/dist/api/compiler/checker-utilities.d.ts +33 -0
  107. package/dist/api/compiler/checker-utilities.js +141 -0
  108. package/dist/api/compiler/checker.d.ts +491 -0
  109. package/dist/api/compiler/checker.js +912 -0
  110. package/dist/api/compiler/command-line.d.ts +32 -0
  111. package/dist/api/compiler/command-line.js +383 -0
  112. package/dist/api/compiler/compile-program.d.ts +118 -0
  113. package/dist/api/compiler/compile-program.js +384 -0
  114. package/dist/api/compiler/diagnostic-locales.d.ts +22 -0
  115. package/dist/api/compiler/diagnostic-locales.js +64 -0
  116. package/dist/api/compiler/diagnostic-messages.d.ts +2166 -0
  117. package/dist/api/compiler/diagnostic-messages.js +184 -0
  118. package/dist/api/compiler/diagnostics.d.ts +167 -0
  119. package/dist/api/compiler/diagnostics.js +820 -0
  120. package/dist/api/compiler/emitter.d.ts +71 -0
  121. package/dist/api/compiler/emitter.js +327 -0
  122. package/dist/api/compiler/file-include.d.ts +117 -0
  123. package/dist/api/compiler/file-include.js +536 -0
  124. package/dist/api/compiler/file-loader.d.ts +261 -0
  125. package/dist/api/compiler/file-loader.js +1257 -0
  126. package/dist/api/compiler/file-system.d.ts +51 -0
  127. package/dist/api/compiler/file-system.js +132 -0
  128. package/dist/api/compiler/flow.d.ts +60 -0
  129. package/dist/api/compiler/flow.js +45 -0
  130. package/dist/api/compiler/instantiation.d.ts +74 -0
  131. package/dist/api/compiler/instantiation.js +799 -0
  132. package/dist/api/compiler/jsdoc.d.ts +75 -0
  133. package/dist/api/compiler/jsdoc.js +1229 -0
  134. package/dist/api/compiler/link-tables.d.ts +36 -0
  135. package/dist/api/compiler/link-tables.js +83 -0
  136. package/dist/api/compiler/locale-data.d.ts +24 -0
  137. package/dist/api/compiler/locale-data.js +183 -0
  138. package/dist/api/compiler/locale.d.ts +4 -0
  139. package/dist/api/compiler/locale.js +317 -0
  140. package/dist/api/compiler/locales/cs-CZ.d.ts +2 -0
  141. package/dist/api/compiler/locales/cs-CZ.js +3 -0
  142. package/dist/api/compiler/locales/de-DE.d.ts +2 -0
  143. package/dist/api/compiler/locales/de-DE.js +3 -0
  144. package/dist/api/compiler/locales/es-ES.d.ts +2 -0
  145. package/dist/api/compiler/locales/es-ES.js +3 -0
  146. package/dist/api/compiler/locales/fr-FR.d.ts +2 -0
  147. package/dist/api/compiler/locales/fr-FR.js +3 -0
  148. package/dist/api/compiler/locales/it-IT.d.ts +2 -0
  149. package/dist/api/compiler/locales/it-IT.js +3 -0
  150. package/dist/api/compiler/locales/ja-JP.d.ts +2 -0
  151. package/dist/api/compiler/locales/ja-JP.js +3 -0
  152. package/dist/api/compiler/locales/ko-KR.d.ts +2 -0
  153. package/dist/api/compiler/locales/ko-KR.js +3 -0
  154. package/dist/api/compiler/locales/pl-PL.d.ts +2 -0
  155. package/dist/api/compiler/locales/pl-PL.js +3 -0
  156. package/dist/api/compiler/locales/pt-BR.d.ts +2 -0
  157. package/dist/api/compiler/locales/pt-BR.js +3 -0
  158. package/dist/api/compiler/locales/ru-RU.d.ts +2 -0
  159. package/dist/api/compiler/locales/ru-RU.js +3 -0
  160. package/dist/api/compiler/locales/tr-TR.d.ts +2 -0
  161. package/dist/api/compiler/locales/tr-TR.js +3 -0
  162. package/dist/api/compiler/locales/zh-CN.d.ts +2 -0
  163. package/dist/api/compiler/locales/zh-CN.js +3 -0
  164. package/dist/api/compiler/locales/zh-TW.d.ts +2 -0
  165. package/dist/api/compiler/locales/zh-TW.js +3 -0
  166. package/dist/api/compiler/module-resolution-types.d.ts +83 -0
  167. package/dist/api/compiler/module-resolution-types.js +176 -0
  168. package/dist/api/compiler/module-resolution.d.ts +71 -0
  169. package/dist/api/compiler/module-resolution.js +1598 -0
  170. package/dist/api/compiler/module-specifiers.d.ts +64 -0
  171. package/dist/api/compiler/module-specifiers.js +1101 -0
  172. package/dist/api/compiler/name-resolver.d.ts +43 -0
  173. package/dist/api/compiler/name-resolver.js +509 -0
  174. package/dist/api/compiler/node-builder-names.d.ts +32 -0
  175. package/dist/api/compiler/node-builder-names.js +854 -0
  176. package/dist/api/compiler/node-builder-pseudo.d.ts +36 -0
  177. package/dist/api/compiler/node-builder-pseudo.js +744 -0
  178. package/dist/api/compiler/node-builder-reuse.d.ts +4 -0
  179. package/dist/api/compiler/node-builder-reuse.js +647 -0
  180. package/dist/api/compiler/node-builder.d.ts +270 -0
  181. package/dist/api/compiler/node-builder.js +2645 -0
  182. package/dist/api/compiler/option-declarations.d.ts +90 -0
  183. package/dist/api/compiler/option-declarations.js +1460 -0
  184. package/dist/api/compiler/options.d.ts +289 -0
  185. package/dist/api/compiler/options.js +1197 -0
  186. package/dist/api/compiler/output-paths.d.ts +42 -0
  187. package/dist/api/compiler/output-paths.js +195 -0
  188. package/dist/api/compiler/package-json.d.ts +175 -0
  189. package/dist/api/compiler/package-json.js +1015 -0
  190. package/dist/api/compiler/parallel-parse.d.ts +219 -0
  191. package/dist/api/compiler/parallel-parse.js +537 -0
  192. package/dist/api/compiler/parser.d.ts +587 -0
  193. package/dist/api/compiler/parser.js +6407 -0
  194. package/dist/api/compiler/path.d.ts +91 -0
  195. package/dist/api/compiler/path.js +705 -0
  196. package/dist/api/compiler/printer/async-helpers.d.ts +22 -0
  197. package/dist/api/compiler/printer/async-helpers.js +91 -0
  198. package/dist/api/compiler/printer/auto-generate.d.ts +13 -0
  199. package/dist/api/compiler/printer/auto-generate.js +20 -0
  200. package/dist/api/compiler/printer/decorator-helpers.d.ts +24 -0
  201. package/dist/api/compiler/printer/decorator-helpers.js +118 -0
  202. package/dist/api/compiler/printer/emit-context.d.ts +154 -0
  203. package/dist/api/compiler/printer/emit-context.js +576 -0
  204. package/dist/api/compiler/printer/emit-flags.d.ts +60 -0
  205. package/dist/api/compiler/printer/emit-flags.js +2 -0
  206. package/dist/api/compiler/printer/factory-base.d.ts +171 -0
  207. package/dist/api/compiler/printer/factory-base.js +1505 -0
  208. package/dist/api/compiler/printer/factory.d.ts +147 -0
  209. package/dist/api/compiler/printer/factory.js +542 -0
  210. package/dist/api/compiler/printer/helpers.d.ts +27 -0
  211. package/dist/api/compiler/printer/helpers.js +164 -0
  212. package/dist/api/compiler/printer/name-generator.d.ts +41 -0
  213. package/dist/api/compiler/printer/name-generator.js +283 -0
  214. package/dist/api/compiler/printer/printer.d.ts +411 -0
  215. package/dist/api/compiler/printer/printer.js +4517 -0
  216. package/dist/api/compiler/printer/text-writer.d.ts +46 -0
  217. package/dist/api/compiler/printer/text-writer.js +172 -0
  218. package/dist/api/compiler/printer/utilities.d.ts +82 -0
  219. package/dist/api/compiler/printer/utilities.js +817 -0
  220. package/dist/api/compiler/program-checker.d.ts +156 -0
  221. package/dist/api/compiler/program-checker.js +526 -0
  222. package/dist/api/compiler/program-utilities.d.ts +52 -0
  223. package/dist/api/compiler/program-utilities.js +365 -0
  224. package/dist/api/compiler/program.d.ts +180 -0
  225. package/dist/api/compiler/program.js +1011 -0
  226. package/dist/api/compiler/pseudo-bigint.d.ts +5 -0
  227. package/dist/api/compiler/pseudo-bigint.js +61 -0
  228. package/dist/api/compiler/regexp-checker.d.ts +38 -0
  229. package/dist/api/compiler/regexp-checker.js +942 -0
  230. package/dist/api/compiler/relations.d.ts +263 -0
  231. package/dist/api/compiler/relations.js +4002 -0
  232. package/dist/api/compiler/reparser.d.ts +48 -0
  233. package/dist/api/compiler/reparser.js +1323 -0
  234. package/dist/api/compiler/scanner.d.ts +256 -0
  235. package/dist/api/compiler/scanner.js +2853 -0
  236. package/dist/api/compiler/side-state.d.ts +16 -0
  237. package/dist/api/compiler/side-state.js +89 -0
  238. package/dist/api/compiler/sourcemap.d.ts +55 -0
  239. package/dist/api/compiler/sourcemap.js +340 -0
  240. package/dist/api/compiler/spelling.d.ts +14 -0
  241. package/dist/api/compiler/spelling.js +128 -0
  242. package/dist/api/compiler/symbol-accessibility.d.ts +57 -0
  243. package/dist/api/compiler/symbol-accessibility.js +894 -0
  244. package/dist/api/compiler/symbol.d.ts +240 -0
  245. package/dist/api/compiler/symbol.js +228 -0
  246. package/dist/api/compiler/symlinks.d.ts +32 -0
  247. package/dist/api/compiler/symlinks.js +114 -0
  248. package/dist/api/compiler/syntax-kind.d.ts +389 -0
  249. package/dist/api/compiler/syntax-kind.js +2 -0
  250. package/dist/api/compiler/timeline.d.ts +9 -0
  251. package/dist/api/compiler/timeline.js +54 -0
  252. package/dist/api/compiler/transformers/alias-references.d.ts +39 -0
  253. package/dist/api/compiler/transformers/alias-references.js +676 -0
  254. package/dist/api/compiler/transformers/async-utilities.d.ts +51 -0
  255. package/dist/api/compiler/transformers/async-utilities.js +260 -0
  256. package/dist/api/compiler/transformers/async.d.ts +53 -0
  257. package/dist/api/compiler/transformers/async.js +645 -0
  258. package/dist/api/compiler/transformers/class-fields.d.ts +206 -0
  259. package/dist/api/compiler/transformers/class-fields.js +2418 -0
  260. package/dist/api/compiler/transformers/class-utilities.d.ts +46 -0
  261. package/dist/api/compiler/transformers/class-utilities.js +261 -0
  262. package/dist/api/compiler/transformers/commonjs-module.d.ts +103 -0
  263. package/dist/api/compiler/transformers/commonjs-module.js +1180 -0
  264. package/dist/api/compiler/transformers/const-enum-inliner.d.ts +11 -0
  265. package/dist/api/compiler/transformers/const-enum-inliner.js +36 -0
  266. package/dist/api/compiler/transformers/declarations-diagnostics.d.ts +52 -0
  267. package/dist/api/compiler/transformers/declarations-diagnostics.js +538 -0
  268. package/dist/api/compiler/transformers/declarations-pseudo.d.ts +160 -0
  269. package/dist/api/compiler/transformers/declarations-pseudo.js +797 -0
  270. package/dist/api/compiler/transformers/declarations-resolver.d.ts +152 -0
  271. package/dist/api/compiler/transformers/declarations-resolver.js +35 -0
  272. package/dist/api/compiler/transformers/declarations-syntactic.d.ts +119 -0
  273. package/dist/api/compiler/transformers/declarations-syntactic.js +1582 -0
  274. package/dist/api/compiler/transformers/declarations-tracker.d.ts +46 -0
  275. package/dist/api/compiler/transformers/declarations-tracker.js +197 -0
  276. package/dist/api/compiler/transformers/declarations.d.ts +175 -0
  277. package/dist/api/compiler/transformers/declarations.js +2380 -0
  278. package/dist/api/compiler/transformers/decorator-metadata.d.ts +54 -0
  279. package/dist/api/compiler/transformers/decorator-metadata.js +554 -0
  280. package/dist/api/compiler/transformers/decorator-utilities.d.ts +38 -0
  281. package/dist/api/compiler/transformers/decorator-utilities.js +238 -0
  282. package/dist/api/compiler/transformers/destructuring.d.ts +25 -0
  283. package/dist/api/compiler/transformers/destructuring.js +626 -0
  284. package/dist/api/compiler/transformers/downlevel.d.ts +6 -0
  285. package/dist/api/compiler/transformers/downlevel.js +45 -0
  286. package/dist/api/compiler/transformers/emit-resolver.d.ts +61 -0
  287. package/dist/api/compiler/transformers/emit-resolver.js +197 -0
  288. package/dist/api/compiler/transformers/es-decorators.d.ts +118 -0
  289. package/dist/api/compiler/transformers/es-decorators.js +1901 -0
  290. package/dist/api/compiler/transformers/exponentiation.d.ts +11 -0
  291. package/dist/api/compiler/transformers/exponentiation.js +76 -0
  292. package/dist/api/compiler/transformers/external-module-info.d.ts +35 -0
  293. package/dist/api/compiler/transformers/external-module-info.js +351 -0
  294. package/dist/api/compiler/transformers/for-await.d.ts +43 -0
  295. package/dist/api/compiler/transformers/for-await.js +469 -0
  296. package/dist/api/compiler/transformers/import-elision.d.ts +16 -0
  297. package/dist/api/compiler/transformers/import-elision.js +124 -0
  298. package/dist/api/compiler/transformers/jsx-entities.d.ts +1 -0
  299. package/dist/api/compiler/transformers/jsx-entities.js +256 -0
  300. package/dist/api/compiler/transformers/jsx.d.ts +71 -0
  301. package/dist/api/compiler/transformers/jsx.js +700 -0
  302. package/dist/api/compiler/transformers/legacy-decorators.d.ts +56 -0
  303. package/dist/api/compiler/transformers/legacy-decorators.js +564 -0
  304. package/dist/api/compiler/transformers/logical-assignment.d.ts +14 -0
  305. package/dist/api/compiler/transformers/logical-assignment.js +62 -0
  306. package/dist/api/compiler/transformers/module-utilities.d.ts +9 -0
  307. package/dist/api/compiler/transformers/module-utilities.js +36 -0
  308. package/dist/api/compiler/transformers/module.d.ts +49 -0
  309. package/dist/api/compiler/transformers/module.js +296 -0
  310. package/dist/api/compiler/transformers/named-evaluation.d.ts +26 -0
  311. package/dist/api/compiler/transformers/named-evaluation.js +266 -0
  312. package/dist/api/compiler/transformers/nullish-coalescing.d.ts +9 -0
  313. package/dist/api/compiler/transformers/nullish-coalescing.js +26 -0
  314. package/dist/api/compiler/transformers/object-rest-spread.d.ts +34 -0
  315. package/dist/api/compiler/transformers/object-rest-spread.js +411 -0
  316. package/dist/api/compiler/transformers/optional-catch.d.ts +9 -0
  317. package/dist/api/compiler/transformers/optional-catch.js +20 -0
  318. package/dist/api/compiler/transformers/optional-chain.d.ts +14 -0
  319. package/dist/api/compiler/transformers/optional-chain.js +188 -0
  320. package/dist/api/compiler/transformers/reference-resolver.d.ts +128 -0
  321. package/dist/api/compiler/transformers/reference-resolver.js +1155 -0
  322. package/dist/api/compiler/transformers/runtime-syntax.d.ts +71 -0
  323. package/dist/api/compiler/transformers/runtime-syntax.js +753 -0
  324. package/dist/api/compiler/transformers/subtree-facts.d.ts +9 -0
  325. package/dist/api/compiler/transformers/subtree-facts.js +203 -0
  326. package/dist/api/compiler/transformers/syntax-facts.d.ts +51 -0
  327. package/dist/api/compiler/transformers/syntax-facts.js +254 -0
  328. package/dist/api/compiler/transformers/tagged-template.d.ts +17 -0
  329. package/dist/api/compiler/transformers/tagged-template.js +148 -0
  330. package/dist/api/compiler/transformers/transformer.d.ts +17 -0
  331. package/dist/api/compiler/transformers/transformer.js +54 -0
  332. package/dist/api/compiler/transformers/type-eraser.d.ts +21 -0
  333. package/dist/api/compiler/transformers/type-eraser.js +346 -0
  334. package/dist/api/compiler/transformers/type-serialization.d.ts +20 -0
  335. package/dist/api/compiler/transformers/type-serialization.js +347 -0
  336. package/dist/api/compiler/transformers/using.d.ts +56 -0
  337. package/dist/api/compiler/transformers/using.js +453 -0
  338. package/dist/api/compiler/transformers/utilities.d.ts +50 -0
  339. package/dist/api/compiler/transformers/utilities.js +347 -0
  340. package/dist/api/compiler/transformers/visitor-base.d.ts +6 -0
  341. package/dist/api/compiler/transformers/visitor-base.js +817 -0
  342. package/dist/api/compiler/transformers/visitor-hooks.d.ts +29 -0
  343. package/dist/api/compiler/transformers/visitor-hooks.js +189 -0
  344. package/dist/api/compiler/tsconfig.d.ts +177 -0
  345. package/dist/api/compiler/tsconfig.js +1311 -0
  346. package/dist/api/compiler/type-printer.d.ts +15 -0
  347. package/dist/api/compiler/type-printer.js +74 -0
  348. package/dist/api/compiler/types.d.ts +87 -0
  349. package/dist/api/compiler/types.js +23 -0
  350. package/dist/api/compiler/unicode-identifiers.d.ts +2 -0
  351. package/dist/api/compiler/unicode-identifiers.js +209 -0
  352. package/dist/api/compiler/unicode-properties.d.ts +8 -0
  353. package/dist/api/compiler/unicode-properties.js +531 -0
  354. package/dist/api/compiler/vfs-match.d.ts +24 -0
  355. package/dist/api/compiler/vfs-match.js +587 -0
  356. package/dist/api/library/ast.d.ts +3 -2
  357. package/dist/api/library/ast.generated.d.ts +127 -139
  358. package/dist/api/library/checker.d.ts +51 -0
  359. package/dist/api/library/checker.js +318 -0
  360. package/dist/api/library/diagnostics.d.ts +13 -0
  361. package/dist/api/library/diagnostics.js +50 -0
  362. package/dist/api/library/disk.d.ts +13 -0
  363. package/dist/api/library/disk.js +66 -0
  364. package/dist/api/library/errors.d.ts +13 -0
  365. package/dist/api/library/errors.js +24 -0
  366. package/dist/api/library/host.d.ts +24 -0
  367. package/dist/api/library/host.js +52 -0
  368. package/dist/api/library/index.d.ts +5 -1
  369. package/dist/api/library/index.js +4 -0
  370. package/dist/api/library/nodes.d.ts +51 -0
  371. package/dist/api/library/nodes.js +196 -0
  372. package/dist/api/library/options.d.ts +144 -0
  373. package/dist/api/library/options.js +308 -0
  374. package/dist/api/library/parse.d.ts +19 -0
  375. package/dist/api/library/parse.js +41 -0
  376. package/dist/api/library/program.d.ts +21 -0
  377. package/dist/api/library/program.js +234 -0
  378. package/dist/api/library/types.d.ts +14 -1
  379. package/package.json +12 -8
  380. package/src/compiler/ast-utilities.ts +548 -0
  381. package/src/compiler/ast.ts +12862 -0
  382. package/src/compiler/astnav.ts +799 -0
  383. package/src/compiler/binder-utilities.ts +1392 -0
  384. package/src/compiler/binder.ts +2703 -0
  385. package/src/compiler/build-info.ts +1197 -0
  386. package/src/compiler/character-codes.ts +323 -0
  387. package/src/compiler/checker-calls.ts +2155 -0
  388. package/src/compiler/checker-classes.ts +706 -0
  389. package/src/compiler/checker-contextual.ts +1457 -0
  390. package/src/compiler/checker-declaration-order.ts +438 -0
  391. package/src/compiler/checker-declarations.ts +1194 -0
  392. package/src/compiler/checker-declared-types.ts +919 -0
  393. package/src/compiler/checker-decorators.ts +694 -0
  394. package/src/compiler/checker-deprecation.ts +359 -0
  395. package/src/compiler/checker-destructuring.ts +805 -0
  396. package/src/compiler/checker-elaboration.ts +477 -0
  397. package/src/compiler/checker-emit-helpers.ts +224 -0
  398. package/src/compiler/checker-emit-resolver.ts +769 -0
  399. package/src/compiler/checker-evaluator.ts +243 -0
  400. package/src/compiler/checker-expressions.ts +1831 -0
  401. package/src/compiler/checker-features.ts +361 -0
  402. package/src/compiler/checker-flow.ts +394 -0
  403. package/src/compiler/checker-functions.ts +1296 -0
  404. package/src/compiler/checker-generated-code.ts +227 -0
  405. package/src/compiler/checker-globals.ts +52 -0
  406. package/src/compiler/checker-grammar.ts +3584 -0
  407. package/src/compiler/checker-heritage.ts +1233 -0
  408. package/src/compiler/checker-inference.ts +2576 -0
  409. package/src/compiler/checker-iteration.ts +1206 -0
  410. package/src/compiler/checker-javascript.ts +502 -0
  411. package/src/compiler/checker-jsdoc.ts +360 -0
  412. package/src/compiler/checker-jsx.ts +1924 -0
  413. package/src/compiler/checker-literals.ts +61 -0
  414. package/src/compiler/checker-members.ts +2436 -0
  415. package/src/compiler/checker-modules.ts +2886 -0
  416. package/src/compiler/checker-narrowing.ts +3230 -0
  417. package/src/compiler/checker-object-literals.ts +575 -0
  418. package/src/compiler/checker-operators.ts +1422 -0
  419. package/src/compiler/checker-overloads.ts +328 -0
  420. package/src/compiler/checker-property-access.ts +906 -0
  421. package/src/compiler/checker-services.ts +1149 -0
  422. package/src/compiler/checker-signatures.ts +1032 -0
  423. package/src/compiler/checker-source-file.ts +732 -0
  424. package/src/compiler/checker-statements.ts +292 -0
  425. package/src/compiler/checker-super.ts +261 -0
  426. package/src/compiler/checker-symbol-types.ts +2107 -0
  427. package/src/compiler/checker-symbols.ts +2150 -0
  428. package/src/compiler/checker-type-factory.ts +2579 -0
  429. package/src/compiler/checker-type-nodes.ts +1033 -0
  430. package/src/compiler/checker-type-operators.ts +2502 -0
  431. package/src/compiler/checker-types.ts +2340 -0
  432. package/src/compiler/checker-unused.ts +642 -0
  433. package/src/compiler/checker-utilities.ts +161 -0
  434. package/src/compiler/checker.ts +1116 -0
  435. package/src/compiler/command-line.ts +436 -0
  436. package/src/compiler/compile-program.ts +439 -0
  437. package/src/compiler/decoder.ts +1209 -0
  438. package/src/compiler/diagnostic-locales.ts +86 -0
  439. package/src/compiler/diagnostic-messages.ts +2349 -0
  440. package/src/compiler/diagnostics.ts +897 -0
  441. package/src/compiler/emitter.ts +434 -0
  442. package/src/compiler/encoder.ts +1945 -0
  443. package/src/compiler/file-include.ts +673 -0
  444. package/src/compiler/file-loader.ts +1528 -0
  445. package/src/compiler/file-system.ts +143 -0
  446. package/src/compiler/flow.ts +89 -0
  447. package/src/compiler/help.ts +473 -0
  448. package/src/compiler/incremental.ts +2784 -0
  449. package/src/compiler/init.ts +141 -0
  450. package/src/compiler/instantiation.ts +894 -0
  451. package/src/compiler/jsdoc.ts +1386 -0
  452. package/src/compiler/link-tables.ts +98 -0
  453. package/src/compiler/locale-data.ts +203 -0
  454. package/src/compiler/locale.ts +307 -0
  455. package/src/compiler/locales/cs-CZ.ts +5 -0
  456. package/src/compiler/locales/de-DE.ts +5 -0
  457. package/src/compiler/locales/es-ES.ts +5 -0
  458. package/src/compiler/locales/fr-FR.ts +5 -0
  459. package/src/compiler/locales/it-IT.ts +5 -0
  460. package/src/compiler/locales/ja-JP.ts +5 -0
  461. package/src/compiler/locales/ko-KR.ts +5 -0
  462. package/src/compiler/locales/pl-PL.ts +5 -0
  463. package/src/compiler/locales/pt-BR.ts +5 -0
  464. package/src/compiler/locales/ru-RU.ts +5 -0
  465. package/src/compiler/locales/tr-TR.ts +5 -0
  466. package/src/compiler/locales/zh-CN.ts +5 -0
  467. package/src/compiler/locales/zh-TW.ts +5 -0
  468. package/src/compiler/module-resolution-types.ts +243 -0
  469. package/src/compiler/module-resolution.ts +2072 -0
  470. package/src/compiler/module-specifiers.ts +1491 -0
  471. package/src/compiler/name-resolver.ts +646 -0
  472. package/src/compiler/node-builder-names.ts +1069 -0
  473. package/src/compiler/node-builder-pseudo.ts +892 -0
  474. package/src/compiler/node-builder-reuse.ts +764 -0
  475. package/src/compiler/node-builder.ts +3425 -0
  476. package/src/compiler/option-declarations.ts +1560 -0
  477. package/src/compiler/options.ts +1576 -0
  478. package/src/compiler/output-paths.ts +334 -0
  479. package/src/compiler/package-json.ts +1083 -0
  480. package/src/compiler/parallel-check.ts +1951 -0
  481. package/src/compiler/parallel-parse.ts +590 -0
  482. package/src/compiler/parse-thread.ts +140 -0
  483. package/src/compiler/parser.ts +7716 -0
  484. package/src/compiler/path.ts +717 -0
  485. package/src/compiler/printer/async-helpers.ts +183 -0
  486. package/src/compiler/printer/auto-generate.ts +27 -0
  487. package/src/compiler/printer/decorator-helpers.ts +218 -0
  488. package/src/compiler/printer/emit-context.ts +679 -0
  489. package/src/compiler/printer/emit-flags.ts +64 -0
  490. package/src/compiler/printer/factory-base.ts +2577 -0
  491. package/src/compiler/printer/factory.ts +764 -0
  492. package/src/compiler/printer/helpers.ts +291 -0
  493. package/src/compiler/printer/name-generator.ts +372 -0
  494. package/src/compiler/printer/printer.ts +5072 -0
  495. package/src/compiler/printer/text-writer.ts +183 -0
  496. package/src/compiler/printer/utilities.ts +884 -0
  497. package/src/compiler/program-checker.ts +592 -0
  498. package/src/compiler/program-utilities.ts +435 -0
  499. package/src/compiler/program.ts +1419 -0
  500. package/src/compiler/pseudo-bigint.ts +62 -0
  501. package/src/compiler/regexp-checker.ts +1142 -0
  502. package/src/compiler/relations.ts +5149 -0
  503. package/src/compiler/reparser.ts +1566 -0
  504. package/src/compiler/scanner.ts +2951 -0
  505. package/src/compiler/show-config.ts +239 -0
  506. package/src/compiler/side-state.ts +125 -0
  507. package/src/compiler/sourcemap.ts +366 -0
  508. package/src/compiler/spelling.ts +122 -0
  509. package/src/compiler/symbol-accessibility.ts +1071 -0
  510. package/src/compiler/symbol.ts +439 -0
  511. package/src/compiler/symlinks.ts +137 -0
  512. package/src/compiler/syntax-kind.ts +391 -0
  513. package/src/compiler/thread-entry.ts +191 -0
  514. package/src/compiler/timeline.ts +53 -0
  515. package/src/compiler/transformers/alias-references.ts +757 -0
  516. package/src/compiler/transformers/async-utilities.ts +335 -0
  517. package/src/compiler/transformers/async.ts +838 -0
  518. package/src/compiler/transformers/class-fields.ts +2967 -0
  519. package/src/compiler/transformers/class-utilities.ts +324 -0
  520. package/src/compiler/transformers/commonjs-module.ts +1621 -0
  521. package/src/compiler/transformers/const-enum-inliner.ts +50 -0
  522. package/src/compiler/transformers/declarations-diagnostics.ts +832 -0
  523. package/src/compiler/transformers/declarations-pseudo.ts +966 -0
  524. package/src/compiler/transformers/declarations-resolver.ts +249 -0
  525. package/src/compiler/transformers/declarations-syntactic.ts +1859 -0
  526. package/src/compiler/transformers/declarations-tracker.ts +269 -0
  527. package/src/compiler/transformers/declarations.ts +3120 -0
  528. package/src/compiler/transformers/decorator-metadata.ts +697 -0
  529. package/src/compiler/transformers/decorator-utilities.ts +283 -0
  530. package/src/compiler/transformers/destructuring.ts +708 -0
  531. package/src/compiler/transformers/downlevel.ts +50 -0
  532. package/src/compiler/transformers/emit-resolver.ts +258 -0
  533. package/src/compiler/transformers/es-decorators.ts +2682 -0
  534. package/src/compiler/transformers/exponentiation.ts +80 -0
  535. package/src/compiler/transformers/external-module-info.ts +412 -0
  536. package/src/compiler/transformers/for-await.ts +687 -0
  537. package/src/compiler/transformers/import-elision.ts +152 -0
  538. package/src/compiler/transformers/jsx-entities.ts +256 -0
  539. package/src/compiler/transformers/jsx.ts +824 -0
  540. package/src/compiler/transformers/legacy-decorators.ts +726 -0
  541. package/src/compiler/transformers/logical-assignment.ts +75 -0
  542. package/src/compiler/transformers/module-utilities.ts +42 -0
  543. package/src/compiler/transformers/module.ts +474 -0
  544. package/src/compiler/transformers/named-evaluation.ts +389 -0
  545. package/src/compiler/transformers/nullish-coalescing.ts +35 -0
  546. package/src/compiler/transformers/object-rest-spread.ts +577 -0
  547. package/src/compiler/transformers/optional-catch.ts +29 -0
  548. package/src/compiler/transformers/optional-chain.ts +246 -0
  549. package/src/compiler/transformers/reference-resolver.ts +1201 -0
  550. package/src/compiler/transformers/runtime-syntax.ts +942 -0
  551. package/src/compiler/transformers/subtree-facts.ts +245 -0
  552. package/src/compiler/transformers/syntax-facts.ts +353 -0
  553. package/src/compiler/transformers/tagged-template.ts +167 -0
  554. package/src/compiler/transformers/transformer.ts +56 -0
  555. package/src/compiler/transformers/type-eraser.ts +520 -0
  556. package/src/compiler/transformers/type-serialization.ts +374 -0
  557. package/src/compiler/transformers/using.ts +613 -0
  558. package/src/compiler/transformers/utilities.ts +419 -0
  559. package/src/compiler/transformers/visitor-base.ts +1601 -0
  560. package/src/compiler/transformers/visitor-hooks.ts +252 -0
  561. package/src/compiler/tsc-build.ts +1418 -0
  562. package/src/compiler/tsc-cli.ts +666 -0
  563. package/src/compiler/tsc-compile.ts +437 -0
  564. package/src/compiler/tsc-watch.ts +854 -0
  565. package/src/compiler/tsconfig.ts +1574 -0
  566. package/src/compiler/type-printer.ts +110 -0
  567. package/src/compiler/types.ts +115 -0
  568. package/src/compiler/unicode-identifiers.ts +212 -0
  569. package/src/compiler/unicode-properties.ts +537 -0
  570. package/src/compiler/vfs-match.ts +597 -0
  571. package/src/compiler/xxh3.ts +650 -0
  572. package/src/library/ast.generated.ts +141 -167
  573. package/src/library/ast.ts +4 -1
  574. package/src/library/checker.ts +384 -0
  575. package/src/library/diagnostics.ts +72 -0
  576. package/src/library/disk.ts +73 -0
  577. package/src/library/errors.ts +26 -0
  578. package/src/library/host.ts +63 -0
  579. package/src/library/index.ts +21 -0
  580. package/src/library/nodes.ts +232 -0
  581. package/src/library/options.ts +449 -0
  582. package/src/library/parse.ts +63 -0
  583. package/src/library/program.ts +293 -0
  584. package/src/library/types.ts +14 -1
@@ -0,0 +1,3584 @@
1
+ // Grammar checks: errors for syntax the parser accepts but the language disallows.
2
+ //
3
+ // `checkGrammarSourceFile` walks a file once after it is type checked and runs the grammar checks
4
+ // of each node in the order tsgo's checker runs them. Checks that need types read the checker's
5
+ // caches. Errors reported with `grammarError*` are dropped when the file has parse errors.
6
+ import {
7
+ type ArrowFunction,
8
+ type BinaryExpression,
9
+ type BindingElement,
10
+ type CallExpression,
11
+ type CatchClause,
12
+ type ComputedPropertyName,
13
+ type ConditionalTypeNode,
14
+ type ConstructorDeclaration,
15
+ type Decorator,
16
+ ElementAccessExpression,
17
+ type ExportAssignment,
18
+ type ExportDeclaration,
19
+ type ExportSpecifier,
20
+ type ExpressionWithTypeArguments,
21
+ type ForInOrOfStatement,
22
+ type ForStatement,
23
+ type HeritageClause,
24
+ Identifier,
25
+ type ImportAttribute,
26
+ type ImportAttributes,
27
+ type ImportClause,
28
+ type ImportDeclaration,
29
+ type ImportEqualsDeclaration,
30
+ type ImportTypeNode,
31
+ type IndexSignatureDeclaration,
32
+ type LabeledStatement,
33
+ type MappedTypeNode,
34
+ type MetaProperty,
35
+ ModifierFlags,
36
+ type ModifierList,
37
+ type ModuleDeclaration,
38
+ type NamedTupleMember,
39
+ type Node,
40
+ NodeFlags,
41
+ type NodeList,
42
+ NodeVisitor,
43
+ type ObjectLiteralExpression,
44
+ type ParameterDeclaration,
45
+ type PrefixUnaryExpression,
46
+ type PropertyAccessExpression,
47
+ type PropertyDeclaration,
48
+ type ShorthandPropertyAssignment,
49
+ type SourceFile,
50
+ StringLiteral,
51
+ type SwitchStatement,
52
+ type TaggedTemplateExpression,
53
+ type ThrowStatement,
54
+ type TupleTypeNode,
55
+ type TypeOperatorNode,
56
+ type TypeParameterDeclaration,
57
+ type TypeReferenceNode,
58
+ type VariableDeclaration,
59
+ type VariableDeclarationList,
60
+ type VariableStatement,
61
+ type WithStatement,
62
+ getAsteriskToken,
63
+ getBody,
64
+ getElements,
65
+ getExpression,
66
+ getHeritageClauses,
67
+ getInitializer,
68
+ getLabel,
69
+ getIsTypeOnly,
70
+ getLocals,
71
+ getMembers,
72
+ getModifiers,
73
+ getName,
74
+ getNodeId,
75
+ getParameters,
76
+ getPostfixToken,
77
+ getStatements,
78
+ getText,
79
+ getType,
80
+ getTypeArguments,
81
+ getTypeParameters,
82
+ isDeclarationNode,
83
+ } from './ast.js';
84
+ import {
85
+ canHaveDecorators,
86
+ canHaveIllegalDecorators,
87
+ getExternalModuleName,
88
+ getTextOfNodeFromSourceText,
89
+ isAmbientModule,
90
+ isFunctionLike,
91
+ isFunctionLikeDeclarationKind,
92
+ isModifierKind,
93
+ isStringLiteralLike,
94
+ modifierToFlag,
95
+ nodeIsPresent,
96
+ isInJSFile,
97
+ } from './ast-utilities.js';
98
+ import {
99
+ getCombinedModifierFlags,
100
+ getCombinedNodeFlags,
101
+ getContainingClass,
102
+ getRangeOfTokenAtPosition,
103
+ getSourceFileOfNode,
104
+ hasSyntacticModifier,
105
+ isAutoAccessorPropertyDeclaration,
106
+ isClassLike,
107
+ isDynamicName,
108
+ isEntityNameExpression,
109
+ isExternalModule,
110
+ isInTopLevelContext,
111
+ isModuleAugmentationExternal,
112
+ isPrivateIdentifierClassElementDeclaration,
113
+ isStatic,
114
+ skipParentheses,
115
+ } from './binder-utilities.js';
116
+ import { findUseStrictPrologue } from './binder.js';
117
+ import { type Checker, createDiagnosticForNode } from './checker.js';
118
+ import { ElementFlags, type Type, TypeFlags, type UnionType } from './checker-types.js';
119
+ import { checkComputedPropertyName, checkExpressionCached, getTypeOfExpression } from './checker-expressions.js';
120
+ import { isArrayLikeType } from './checker-members.js';
121
+ import { getNullableType } from './checker-symbol-types.js';
122
+ import { getTupleElementFlags, getTypeFromTypeNode } from './checker-type-nodes.js';
123
+ import { isArrayType, isTupleType, tupleTargetOf } from './checker-type-factory.js';
124
+ import { isGenericType } from './checker-type-operators.js';
125
+ import { getPropertyNameFromType, isTypeUsableAsPropertyName, isValidIndexKeyType } from './checker-utilities.js';
126
+ import { getSymbolOfDeclaration, resolveAlias } from './checker-symbols.js';
127
+ import { Diagnostics } from './diagnostic-messages.js';
128
+ import { Diagnostic, DiagnosticCategory, type DiagnosticMessage, messageCategory } from './diagnostics.js';
129
+ import { ModuleKind, ScriptTarget, getEmitModuleKind } from './options.js';
130
+ import { fileExtensionIsOneOf, isExternalModuleNameRelative } from './path.js';
131
+ import { SourceFileMetadata, getEmitModuleFormatOfFile, getImpliedNodeFormatForEmit } from './program-utilities.js';
132
+ import { CharacterCodes } from './character-codes.js';
133
+ import { Scanner, ScannerErrorReporter, computeLineOfPosition, skipTrivia, tokenToString } from './scanner.js';
134
+ import { SymbolFlags } from './symbol.js';
135
+ import { typeToString } from './type-printer.js';
136
+ import type { ScriptTarget as LanguageTarget } from './types.js';
137
+ import { SyntaxKind } from './syntax-kind.js';
138
+ import { findMismatchedAugmentsTag } from './checker-jsdoc.js';
139
+
140
+ /** Runs the grammar checks of every node of `file`. */
141
+ export function checkGrammarSourceFile(c: Checker, file: SourceFile): void {
142
+ const g = new GrammarChecker(c, file);
143
+ if ((file.flags & NodeFlags.Ambient) !== 0) checkGrammarTopLevelElementsForRequiredDeclareModifier(g, file);
144
+ file.forEachChild(g);
145
+ }
146
+
147
+ /** The grammar checker of the file of `node`, for grammar checks run while checking expressions. */
148
+ function grammarCheckerOf(c: Checker, node: Node): GrammarChecker {
149
+ const file = getSourceFileOfNode(node);
150
+ const cached = c.grammarChecker;
151
+ if (cached !== undefined && cached.file === file) return cached;
152
+ const g = new GrammarChecker(c, file);
153
+ c.grammarChecker = g;
154
+ return g;
155
+ }
156
+
157
+ /** Checks the grammar of an object literal each time it is checked, as tsgo does. */
158
+ export function checkGrammarObjectLiteralOfExpression(
159
+ c: Checker,
160
+ node: ObjectLiteralExpression,
161
+ inDestructuring: boolean,
162
+ ): void {
163
+ checkGrammarObjectLiteralExpression(grammarCheckerOf(c, node), node, inDestructuring);
164
+ }
165
+
166
+ /** Reports a trailing comma after the rest element of a destructuring assignment. */
167
+ export function checkGrammarRestTrailingComma(c: Checker, node: Node, list: NodeList): void {
168
+ checkGrammarForDisallowedTrailingComma(
169
+ grammarCheckerOf(c, node),
170
+ list,
171
+ Diagnostics.A_rest_parameter_or_binding_pattern_may_not_have_a_trailing_comma,
172
+ );
173
+ }
174
+
175
+ /** Checks the grammar of a function expression or arrow function each time it is checked. */
176
+ export function checkGrammarFunctionExpression(c: Checker, node: Node): void {
177
+ const g = grammarCheckerOf(c, node);
178
+ if (!checkGrammarFunctionLikeDeclaration(g, node) && node.kind === SyntaxKind.FunctionExpression)
179
+ checkGrammarForGenerator(g, node);
180
+ }
181
+
182
+ /** Checks the grammar of an `await` expression each time it is checked. */
183
+ export function checkGrammarAwaitExpression(c: Checker, node: Node): void {
184
+ checkGrammarAwaitOrAwaitUsing(grammarCheckerOf(c, node), node);
185
+ }
186
+
187
+ /** Checks the grammar of a `yield` expression each time it is checked. */
188
+ export function checkGrammarYieldOfExpression(c: Checker, node: Node): void {
189
+ checkGrammarYieldExpression(grammarCheckerOf(c, node), node);
190
+ }
191
+
192
+ /** Per-file state of the grammar walk. */
193
+ export class GrammarChecker extends NodeVisitor {
194
+ readonly c: Checker;
195
+ readonly file: SourceFile;
196
+ /** Grammar errors are only reported in files without parse errors. */
197
+ readonly hasParseErrors: boolean;
198
+ readonly moduleKind: ModuleKind;
199
+ readonly metadata: SourceFileMetadata;
200
+ readonly legacyDecorators: boolean;
201
+ /** Blocks and statements that already reported a statement in an ambient context. */
202
+ readonly reportedAmbientStatements = new Set<number>();
203
+ private scanner: Scanner | undefined = undefined;
204
+
205
+ constructor(c: Checker, file: SourceFile) {
206
+ super();
207
+ this.c = c;
208
+ this.file = file;
209
+ this.hasParseErrors = file.diagnostics.length > 0;
210
+ this.moduleKind = getEmitModuleKind(c.compilerOptions);
211
+ this.metadata = c.host.getSourceFileMetadata(file) ?? new SourceFileMetadata('', '', ModuleKind.None);
212
+ this.legacyDecorators = c.compilerOptions.experimentalDecorators === true;
213
+ }
214
+
215
+ visitNode(node: Node): boolean {
216
+ if (checkGrammarNode(this, node)) node.forEachChild(this);
217
+ return false;
218
+ }
219
+
220
+ /** A scanner over the file's text, for rescanning tokens. */
221
+ getScanner(): Scanner {
222
+ let scanner = this.scanner;
223
+ if (scanner === undefined) {
224
+ scanner = new Scanner(this.file.text);
225
+ scanner.setLanguageVariant(this.file.languageVariant);
226
+ // The scanner's target enum has the same values as the option's.
227
+ scanner.setScriptTarget(this.c.languageVersion as number as LanguageTarget);
228
+ this.scanner = scanner;
229
+ }
230
+ return scanner;
231
+ }
232
+ }
233
+
234
+ // ---- Reporting
235
+
236
+ function grammarErrorOnNode(g: GrammarChecker, node: Node, message: DiagnosticMessage, ...args: string[]): boolean {
237
+ if (g.hasParseErrors) return false;
238
+ g.c.error(node, message, ...args);
239
+ return true;
240
+ }
241
+
242
+ function grammarErrorOnFirstToken(
243
+ g: GrammarChecker,
244
+ node: Node,
245
+ message: DiagnosticMessage,
246
+ ...args: string[]
247
+ ): boolean {
248
+ if (g.hasParseErrors) return false;
249
+ const range = getRangeOfTokenAtPosition(g.file, node.pos);
250
+ g.c.addDiagnostic(new Diagnostic(g.file.diagnosticFile(), range.pos, range.end, message, args));
251
+ return true;
252
+ }
253
+
254
+ function grammarErrorAtPos(
255
+ g: GrammarChecker,
256
+ start: number,
257
+ length: number,
258
+ message: DiagnosticMessage,
259
+ ...args: string[]
260
+ ): boolean {
261
+ if (g.hasParseErrors) return false;
262
+ g.c.addDiagnostic(new Diagnostic(g.file.diagnosticFile(), start, start + length, message, args));
263
+ return true;
264
+ }
265
+
266
+ /** A diagnostic at the first token of `node`. */
267
+ function diagnosticAtFirstToken(g: GrammarChecker, node: Node, message: DiagnosticMessage): Diagnostic {
268
+ const range = getRangeOfTokenAtPosition(g.file, node.pos);
269
+ return new Diagnostic(g.file.diagnosticFile(), range.pos, range.end, message);
270
+ }
271
+
272
+ function addRelated(diagnostic: Diagnostic, node: Node, message: DiagnosticMessage): void {
273
+ diagnostic.relatedInformation.push(createDiagnosticForNode(node, message, []));
274
+ }
275
+
276
+ // ---- Dispatch
277
+
278
+ /** Runs the grammar checks of `node`; returns whether its children are checked too. */
279
+ function checkGrammarNode(g: GrammarChecker, node: Node): boolean {
280
+ switch (node.kind) {
281
+ // Declarations and members
282
+ case SyntaxKind.TypeParameter: {
283
+ checkGrammarModifiers(g, node);
284
+ const expression = (node as TypeParameterDeclaration).expression;
285
+ if (expression !== undefined) grammarErrorOnFirstToken(g, expression, Diagnostics.Type_expected);
286
+ return true;
287
+ }
288
+ case SyntaxKind.Parameter:
289
+ checkGrammarModifiers(g, node);
290
+ checkGrammarDecorators(g, node);
291
+ return true;
292
+ case SyntaxKind.PropertySignature:
293
+ checkGrammarPropertyDeclaration(g, node);
294
+ return true;
295
+ case SyntaxKind.PropertyDeclaration:
296
+ checkGrammarPropertyDeclaration(g, node);
297
+ checkGrammarDecorators(g, node);
298
+ return true;
299
+ case SyntaxKind.ConstructorType:
300
+ case SyntaxKind.FunctionType:
301
+ case SyntaxKind.CallSignature:
302
+ case SyntaxKind.ConstructSignature:
303
+ checkGrammarFunctionLikeDeclaration(g, node);
304
+ return true;
305
+ case SyntaxKind.IndexSignature:
306
+ if (!checkGrammarModifiers(g, node)) checkGrammarIndexSignatureParameters(g, node as IndexSignatureDeclaration);
307
+ return true;
308
+ case SyntaxKind.MethodDeclaration:
309
+ case SyntaxKind.MethodSignature:
310
+ checkGrammarMethodDeclaration(g, node);
311
+ return true;
312
+ case SyntaxKind.ClassStaticBlockDeclaration:
313
+ checkGrammarModifiers(g, node);
314
+ return true;
315
+ case SyntaxKind.Constructor:
316
+ checkGrammarFunctionLikeDeclaration(g, node);
317
+ if (!checkGrammarConstructorTypeParameters(g, node as ConstructorDeclaration)) {
318
+ const type = (node as ConstructorDeclaration).type;
319
+ if (type !== undefined)
320
+ grammarErrorOnNode(g, type, Diagnostics.Type_annotation_cannot_appear_on_a_constructor_declaration);
321
+ }
322
+ return true;
323
+ case SyntaxKind.GetAccessor:
324
+ case SyntaxKind.SetAccessor:
325
+ checkGrammarAccessorDeclaration(g, node);
326
+ return true;
327
+ case SyntaxKind.FunctionDeclaration:
328
+ checkGrammarDecorators(g, node);
329
+ checkGrammarFunctionLikeDeclaration(g, node);
330
+ checkGrammarForGenerator(g, node);
331
+ return true;
332
+ case SyntaxKind.ClassDeclaration:
333
+ checkGrammarClassDeclaration(g, node);
334
+ return true;
335
+ case SyntaxKind.ClassExpression:
336
+ checkGrammarClassLikeDeclaration(g, node);
337
+ return true;
338
+ case SyntaxKind.InterfaceDeclaration:
339
+ checkGrammarInterfaceDeclaration(g, node);
340
+ return true;
341
+ case SyntaxKind.TypeAliasDeclaration:
342
+ checkGrammarModifiers(g, node);
343
+ if (!containerAllowsBlockScopedVariable(node.parent!))
344
+ grammarErrorOnNode(g, node, Diagnostics.X_0_declarations_can_only_be_declared_inside_a_block, 'type');
345
+ return true;
346
+ case SyntaxKind.EnumDeclaration:
347
+ checkGrammarModifiers(g, node);
348
+ return true;
349
+ case SyntaxKind.ModuleDeclaration:
350
+ return checkGrammarModuleDeclaration(g, node as ModuleDeclaration);
351
+ case SyntaxKind.ImportDeclaration:
352
+ case SyntaxKind.JSImportDeclaration:
353
+ checkGrammarImportDeclaration(g, node as ImportDeclaration);
354
+ return false;
355
+ case SyntaxKind.ImportEqualsDeclaration:
356
+ checkGrammarImportEqualsDeclaration(g, node as ImportEqualsDeclaration);
357
+ return false;
358
+ case SyntaxKind.ExportDeclaration:
359
+ checkGrammarExportDeclaration(g, node as ExportDeclaration);
360
+ return false;
361
+ case SyntaxKind.ExportAssignment:
362
+ return checkGrammarExportAssignment(g, node as ExportAssignment);
363
+ case SyntaxKind.VariableDeclaration:
364
+ // Catch clause variables are checked with the clause.
365
+ if (node.parent!.kind !== SyntaxKind.CatchClause) checkGrammarVariableDeclaration(g, node as VariableDeclaration);
366
+ return true;
367
+ case SyntaxKind.BindingElement:
368
+ checkGrammarBindingElement(g, node as BindingElement);
369
+ return true;
370
+ // Statements
371
+ case SyntaxKind.Block:
372
+ case SyntaxKind.EmptyStatement:
373
+ case SyntaxKind.DebuggerStatement:
374
+ case SyntaxKind.ExpressionStatement:
375
+ case SyntaxKind.DoStatement:
376
+ case SyntaxKind.WhileStatement:
377
+ case SyntaxKind.TryStatement:
378
+ checkGrammarStatementInAmbientContext(g, node);
379
+ return true;
380
+ case SyntaxKind.IfStatement: {
381
+ checkGrammarStatementInAmbientContext(g, node);
382
+ return true;
383
+ }
384
+ case SyntaxKind.VariableStatement:
385
+ checkGrammarVariableStatement(g, node as VariableStatement);
386
+ return true;
387
+ case SyntaxKind.ForStatement: {
388
+ const initializer = (node as ForStatement).initializer;
389
+ if (
390
+ !checkGrammarStatementInAmbientContext(g, node) &&
391
+ initializer !== undefined &&
392
+ initializer.kind === SyntaxKind.VariableDeclarationList
393
+ )
394
+ checkGrammarVariableDeclarationList(g, initializer as VariableDeclarationList);
395
+ return true;
396
+ }
397
+ case SyntaxKind.ForInStatement:
398
+ case SyntaxKind.ForOfStatement:
399
+ checkGrammarForInOrForOfStatement(g, node as ForInOrOfStatement);
400
+ return true;
401
+ case SyntaxKind.BreakStatement:
402
+ case SyntaxKind.ContinueStatement:
403
+ if (!checkGrammarStatementInAmbientContext(g, node)) checkGrammarBreakOrContinueStatement(g, node);
404
+ return true;
405
+ case SyntaxKind.ReturnStatement:
406
+ return checkGrammarReturnStatement(g, node);
407
+ case SyntaxKind.WithStatement:
408
+ checkGrammarWithStatement(g, node as WithStatement);
409
+ return false;
410
+ case SyntaxKind.SwitchStatement:
411
+ checkGrammarSwitchStatement(g, node as SwitchStatement);
412
+ return true;
413
+ case SyntaxKind.LabeledStatement:
414
+ checkGrammarLabeledStatement(g, node as LabeledStatement);
415
+ return true;
416
+ case SyntaxKind.ThrowStatement: {
417
+ const expression = (node as ThrowStatement).expression;
418
+ if (
419
+ !checkGrammarStatementInAmbientContext(g, node) &&
420
+ expression instanceof Identifier &&
421
+ (expression as Identifier).text.length === 0
422
+ )
423
+ grammarErrorAtPos(g, expression.pos, 0, Diagnostics.Line_break_not_permitted_here);
424
+ return true;
425
+ }
426
+ case SyntaxKind.CatchClause:
427
+ checkGrammarCatchClause(g, node as CatchClause);
428
+ return true;
429
+ // Type nodes
430
+ case SyntaxKind.TypeReference:
431
+ checkGrammarTypeReference(g, node as TypeReferenceNode);
432
+ return true;
433
+ case SyntaxKind.TypeQuery:
434
+ checkGrammarTypeArguments(g, getTypeArguments(node));
435
+ return true;
436
+ case SyntaxKind.TypeOperator:
437
+ checkGrammarTypeOperatorNode(g, node as TypeOperatorNode);
438
+ return true;
439
+ case SyntaxKind.InferType:
440
+ checkGrammarInferType(g, node);
441
+ return true;
442
+ case SyntaxKind.ImportType: {
443
+ const attributes = (node as ImportTypeNode).attributes;
444
+ if (attributes !== undefined) getResolutionModeOverride(g, attributes, true);
445
+ return true;
446
+ }
447
+ case SyntaxKind.TupleType:
448
+ checkGrammarTupleType(g, node as TupleTypeNode);
449
+ return true;
450
+ case SyntaxKind.NamedTupleMember:
451
+ checkGrammarNamedTupleMember(g, node as NamedTupleMember);
452
+ return true;
453
+ case SyntaxKind.JSDocNonNullableType:
454
+ case SyntaxKind.JSDocNullableType:
455
+ case SyntaxKind.JSDocAllType:
456
+ case SyntaxKind.JSDocTypeLiteral:
457
+ checkGrammarJSDocTypeInTypeScript(g, node);
458
+ return true;
459
+ case SyntaxKind.MappedType: {
460
+ const members = (node as MappedTypeNode).members;
461
+ if (members !== undefined && members.nodes.length > 0)
462
+ grammarErrorOnNode(g, members.nodes[0]!, Diagnostics.A_mapped_type_may_not_declare_properties_or_methods);
463
+ return true;
464
+ }
465
+ // Expressions
466
+ case SyntaxKind.ExpressionWithTypeArguments:
467
+ // Heritage clause elements are checked with their class or interface.
468
+ if (node.parent!.kind !== SyntaxKind.HeritageClause) checkGrammarExpressionWithTypeArguments(g, node);
469
+ return true;
470
+ case SyntaxKind.FunctionExpression:
471
+ case SyntaxKind.ArrowFunction:
472
+ // Checked by the checker each time the function is checked.
473
+ return true;
474
+ case SyntaxKind.ObjectLiteralExpression:
475
+ // Checked by the checker each time the literal is checked.
476
+ return true;
477
+ case SyntaxKind.CallExpression:
478
+ checkGrammarCallExpression(g, node as CallExpression);
479
+ return true;
480
+ case SyntaxKind.NewExpression:
481
+ checkGrammarTypeArguments(g, getTypeArguments(node));
482
+ return true;
483
+ case SyntaxKind.TaggedTemplateExpression:
484
+ if (!checkGrammarTaggedTemplateChain(g, node as TaggedTemplateExpression))
485
+ checkGrammarTypeArguments(g, getTypeArguments(node));
486
+ return true;
487
+ case SyntaxKind.MetaProperty:
488
+ checkGrammarMetaProperty(g, node as MetaProperty);
489
+ return true;
490
+ case SyntaxKind.AwaitExpression:
491
+ case SyntaxKind.YieldExpression:
492
+ case SyntaxKind.BinaryExpression:
493
+ // Checked by the checker each time the expression is checked.
494
+ return true;
495
+ case SyntaxKind.PrivateIdentifier:
496
+ checkGrammarPrivateIdentifierExpression(g, node);
497
+ return false;
498
+ case SyntaxKind.BigIntLiteral:
499
+ checkGrammarBigIntLiteral(g, node);
500
+ return false;
501
+ case SyntaxKind.RegularExpressionLiteral:
502
+ checkGrammarRegularExpressionLiteral(g, node);
503
+ return false;
504
+ case SyntaxKind.Decorator: {
505
+ // Decorators are only checked on valid targets.
506
+ const target = node.parent!;
507
+ return (
508
+ canHaveDecorators(target) &&
509
+ nodeCanBeDecorated(g.legacyDecorators, target, target.parent, target.parent!.parent)
510
+ );
511
+ }
512
+ }
513
+ return true;
514
+ }
515
+
516
+ // ---- Modifiers and decorators
517
+
518
+ function checkGrammarModifiers(g: GrammarChecker, node: Node): boolean {
519
+ const modifiers = getModifiers(node);
520
+ if (modifiers === undefined) return false;
521
+ if (reportObviousDecoratorErrors(g, node, modifiers) || reportObviousModifierErrors(g, node, modifiers)) return true;
522
+ if (isThisParameter(node))
523
+ return grammarErrorOnFirstToken(
524
+ g,
525
+ node,
526
+ Diagnostics.Neither_decorators_nor_modifiers_may_be_applied_to_this_parameters,
527
+ );
528
+ const blockScopeKind =
529
+ node.kind === SyntaxKind.VariableStatement
530
+ ? (node as VariableStatement).declarationList.flags & NodeFlags.BlockScoped
531
+ : NodeFlags.None;
532
+ const parent = node.parent!;
533
+ let lastStatic: Node | undefined;
534
+ let lastDeclare: Node | undefined;
535
+ let lastAsync: Node | undefined;
536
+ let lastOverride: Node | undefined;
537
+ let firstDecorator: Node | undefined;
538
+ let flags = ModifierFlags.None;
539
+ let sawExportBeforeDecorators = false;
540
+ // Decorators and modifiers come in four chunks: leading decorators, leading modifiers,
541
+ // trailing decorators and trailing modifiers. Leading and trailing decorators can't both appear.
542
+ let hasLeadingDecorators = false;
543
+ for (const modifier of modifiers.nodes) {
544
+ if (modifier.kind === SyntaxKind.Decorator) {
545
+ if (!nodeCanBeDecorated(g.legacyDecorators, node, parent, parent.parent)) {
546
+ if (node.kind === SyntaxKind.MethodDeclaration && !nodeIsPresent(getBody(node)))
547
+ return grammarErrorOnFirstToken(
548
+ g,
549
+ node,
550
+ Diagnostics.A_decorator_can_only_decorate_a_method_implementation_not_an_overload,
551
+ );
552
+ return grammarErrorOnFirstToken(g, node, Diagnostics.Decorators_are_not_valid_here);
553
+ } else if (g.legacyDecorators && (node.kind === SyntaxKind.GetAccessor || node.kind === SyntaxKind.SetAccessor)) {
554
+ const first = getFirstAccessorOfPair(g.c, node);
555
+ if (first !== node && hasDecorators(first))
556
+ return grammarErrorOnFirstToken(
557
+ g,
558
+ node,
559
+ Diagnostics.Decorators_cannot_be_applied_to_multiple_get_Slashset_accessors_of_the_same_name,
560
+ );
561
+ }
562
+ // Decorators can only follow `export` and `default`.
563
+ if ((flags & ~(ModifierFlags.ExportDefault | ModifierFlags.Decorator)) !== 0)
564
+ return grammarErrorOnNode(g, modifier, Diagnostics.Decorators_are_not_valid_here);
565
+ if (hasLeadingDecorators && (flags & ModifierFlags.Modifier) !== 0) {
566
+ if (g.hasParseErrors) return false;
567
+ const diagnostic = g.c.error(
568
+ modifier,
569
+ Diagnostics.Decorators_may_not_appear_after_export_or_export_default_if_they_also_appear_before_export,
570
+ );
571
+ addRelated(diagnostic, firstDecorator!, Diagnostics.Decorator_used_before_export_here);
572
+ return true;
573
+ }
574
+ flags |= ModifierFlags.Decorator;
575
+ if ((flags & ModifierFlags.Modifier) === 0) hasLeadingDecorators = true;
576
+ else if ((flags & ModifierFlags.Export) !== 0) sawExportBeforeDecorators = true;
577
+ if (firstDecorator === undefined) firstDecorator = modifier;
578
+ continue;
579
+ }
580
+ const kind = modifier.kind;
581
+ const reparsed = (modifier.flags & NodeFlags.Reparsed) !== 0;
582
+ if (kind !== SyntaxKind.ReadonlyKeyword) {
583
+ if (node.kind === SyntaxKind.PropertySignature || node.kind === SyntaxKind.MethodSignature)
584
+ return grammarErrorOnNode(
585
+ g,
586
+ modifier,
587
+ Diagnostics.X_0_modifier_cannot_appear_on_a_type_member,
588
+ tokenToString(kind),
589
+ );
590
+ if (node.kind === SyntaxKind.IndexSignature && (kind !== SyntaxKind.StaticKeyword || !isClassLike(parent)))
591
+ return grammarErrorOnNode(
592
+ g,
593
+ modifier,
594
+ Diagnostics.X_0_modifier_cannot_appear_on_an_index_signature,
595
+ tokenToString(kind),
596
+ );
597
+ }
598
+ if (
599
+ kind !== SyntaxKind.InKeyword &&
600
+ kind !== SyntaxKind.OutKeyword &&
601
+ kind !== SyntaxKind.ConstKeyword &&
602
+ node.kind === SyntaxKind.TypeParameter
603
+ )
604
+ return grammarErrorOnNode(
605
+ g,
606
+ modifier,
607
+ Diagnostics.X_0_modifier_cannot_appear_on_a_type_parameter,
608
+ tokenToString(kind),
609
+ );
610
+ switch (kind) {
611
+ case SyntaxKind.ConstKeyword:
612
+ if (node.kind !== SyntaxKind.EnumDeclaration && node.kind !== SyntaxKind.TypeParameter)
613
+ return grammarErrorOnNode(g, node, Diagnostics.A_class_member_cannot_have_the_0_keyword, tokenToString(kind));
614
+ if (node.kind === SyntaxKind.TypeParameter && !isConstTypeParameterContainer(parent))
615
+ return grammarErrorOnNode(
616
+ g,
617
+ modifier,
618
+ Diagnostics.X_0_modifier_can_only_appear_on_a_type_parameter_of_a_function_method_or_class,
619
+ tokenToString(kind),
620
+ );
621
+ break;
622
+ case SyntaxKind.OverrideKeyword:
623
+ // Parameters that aren't parameter properties are reported by the parameter check.
624
+ if ((flags & ModifierFlags.Override) !== 0)
625
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_already_seen, 'override');
626
+ if ((flags & ModifierFlags.Ambient) !== 0)
627
+ return grammarErrorOnNode(
628
+ g,
629
+ modifier,
630
+ Diagnostics.X_0_modifier_cannot_be_used_with_1_modifier,
631
+ 'override',
632
+ 'declare',
633
+ );
634
+ if ((flags & ModifierFlags.Readonly) !== 0 && !reparsed)
635
+ return grammarErrorOnNode(
636
+ g,
637
+ modifier,
638
+ Diagnostics.X_0_modifier_must_precede_1_modifier,
639
+ 'override',
640
+ 'readonly',
641
+ );
642
+ if ((flags & ModifierFlags.Accessor) !== 0 && !reparsed)
643
+ return grammarErrorOnNode(
644
+ g,
645
+ modifier,
646
+ Diagnostics.X_0_modifier_must_precede_1_modifier,
647
+ 'override',
648
+ 'accessor',
649
+ );
650
+ if ((flags & ModifierFlags.Async) !== 0 && !reparsed)
651
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_must_precede_1_modifier, 'override', 'async');
652
+ flags |= ModifierFlags.Override;
653
+ lastOverride = modifier;
654
+ break;
655
+ case SyntaxKind.PublicKeyword:
656
+ case SyntaxKind.ProtectedKeyword:
657
+ case SyntaxKind.PrivateKeyword: {
658
+ const text = tokenToString(kind);
659
+ if ((flags & ModifierFlags.AccessibilityModifier) !== 0)
660
+ return grammarErrorOnNode(g, modifier, Diagnostics.Accessibility_modifier_already_seen);
661
+ if ((flags & ModifierFlags.Override) !== 0 && !reparsed)
662
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_must_precede_1_modifier, text, 'override');
663
+ if ((flags & ModifierFlags.Static) !== 0 && !reparsed)
664
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_must_precede_1_modifier, text, 'static');
665
+ if ((flags & ModifierFlags.Accessor) !== 0 && !reparsed)
666
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_must_precede_1_modifier, text, 'accessor');
667
+ if ((flags & ModifierFlags.Readonly) !== 0 && !reparsed)
668
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_must_precede_1_modifier, text, 'readonly');
669
+ if ((flags & ModifierFlags.Async) !== 0 && !reparsed)
670
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_must_precede_1_modifier, text, 'async');
671
+ if (parent.kind === SyntaxKind.ModuleBlock || parent.kind === SyntaxKind.SourceFile)
672
+ return grammarErrorOnNode(
673
+ g,
674
+ modifier,
675
+ Diagnostics.X_0_modifier_cannot_appear_on_a_module_or_namespace_element,
676
+ text,
677
+ );
678
+ if ((flags & ModifierFlags.Abstract) !== 0) {
679
+ if (kind === SyntaxKind.PrivateKeyword)
680
+ return grammarErrorOnNode(
681
+ g,
682
+ modifier,
683
+ Diagnostics.X_0_modifier_cannot_be_used_with_1_modifier,
684
+ text,
685
+ 'abstract',
686
+ );
687
+ if (!reparsed)
688
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_must_precede_1_modifier, text, 'abstract');
689
+ } else if (isPrivateIdentifierClassElementDeclaration(node))
690
+ return grammarErrorOnNode(
691
+ g,
692
+ modifier,
693
+ Diagnostics.An_accessibility_modifier_cannot_be_used_with_a_private_identifier,
694
+ );
695
+ flags |= modifierToFlag(kind);
696
+ break;
697
+ }
698
+ case SyntaxKind.StaticKeyword:
699
+ if ((flags & ModifierFlags.Static) !== 0)
700
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_already_seen, 'static');
701
+ if ((flags & ModifierFlags.Readonly) !== 0 && !reparsed)
702
+ return grammarErrorOnNode(
703
+ g,
704
+ modifier,
705
+ Diagnostics.X_0_modifier_must_precede_1_modifier,
706
+ 'static',
707
+ 'readonly',
708
+ );
709
+ if ((flags & ModifierFlags.Async) !== 0 && !reparsed)
710
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_must_precede_1_modifier, 'static', 'async');
711
+ if ((flags & ModifierFlags.Accessor) !== 0 && !reparsed)
712
+ return grammarErrorOnNode(
713
+ g,
714
+ modifier,
715
+ Diagnostics.X_0_modifier_must_precede_1_modifier,
716
+ 'static',
717
+ 'accessor',
718
+ );
719
+ if (parent.kind === SyntaxKind.ModuleBlock || parent.kind === SyntaxKind.SourceFile)
720
+ return grammarErrorOnNode(
721
+ g,
722
+ modifier,
723
+ Diagnostics.X_0_modifier_cannot_appear_on_a_module_or_namespace_element,
724
+ 'static',
725
+ );
726
+ if (node.kind === SyntaxKind.Parameter)
727
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_cannot_appear_on_a_parameter, 'static');
728
+ if ((flags & ModifierFlags.Abstract) !== 0)
729
+ return grammarErrorOnNode(
730
+ g,
731
+ modifier,
732
+ Diagnostics.X_0_modifier_cannot_be_used_with_1_modifier,
733
+ 'static',
734
+ 'abstract',
735
+ );
736
+ if ((flags & ModifierFlags.Override) !== 0 && !reparsed)
737
+ return grammarErrorOnNode(
738
+ g,
739
+ modifier,
740
+ Diagnostics.X_0_modifier_must_precede_1_modifier,
741
+ 'static',
742
+ 'override',
743
+ );
744
+ flags |= ModifierFlags.Static;
745
+ lastStatic = modifier;
746
+ break;
747
+ case SyntaxKind.AccessorKeyword:
748
+ if ((flags & ModifierFlags.Accessor) !== 0)
749
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_already_seen, 'accessor');
750
+ if ((flags & ModifierFlags.Readonly) !== 0)
751
+ return grammarErrorOnNode(
752
+ g,
753
+ modifier,
754
+ Diagnostics.X_0_modifier_cannot_be_used_with_1_modifier,
755
+ 'accessor',
756
+ 'readonly',
757
+ );
758
+ if ((flags & ModifierFlags.Ambient) !== 0)
759
+ return grammarErrorOnNode(
760
+ g,
761
+ modifier,
762
+ Diagnostics.X_0_modifier_cannot_be_used_with_1_modifier,
763
+ 'accessor',
764
+ 'declare',
765
+ );
766
+ if (node.kind !== SyntaxKind.PropertyDeclaration)
767
+ return grammarErrorOnNode(
768
+ g,
769
+ modifier,
770
+ Diagnostics.X_accessor_modifier_can_only_appear_on_a_property_declaration,
771
+ );
772
+ flags |= ModifierFlags.Accessor;
773
+ break;
774
+ case SyntaxKind.ReadonlyKeyword:
775
+ if ((flags & ModifierFlags.Readonly) !== 0)
776
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_already_seen, 'readonly');
777
+ // Parameters that aren't parameter properties are reported by the parameter check.
778
+ if (
779
+ node.kind !== SyntaxKind.PropertyDeclaration &&
780
+ node.kind !== SyntaxKind.PropertySignature &&
781
+ node.kind !== SyntaxKind.IndexSignature &&
782
+ node.kind !== SyntaxKind.Parameter
783
+ )
784
+ return grammarErrorOnNode(
785
+ g,
786
+ modifier,
787
+ Diagnostics.X_readonly_modifier_can_only_appear_on_a_property_declaration_or_index_signature,
788
+ );
789
+ if ((flags & ModifierFlags.Accessor) !== 0)
790
+ return grammarErrorOnNode(
791
+ g,
792
+ modifier,
793
+ Diagnostics.X_0_modifier_cannot_be_used_with_1_modifier,
794
+ 'readonly',
795
+ 'accessor',
796
+ );
797
+ flags |= ModifierFlags.Readonly;
798
+ break;
799
+ case SyntaxKind.ExportKeyword:
800
+ if (
801
+ g.c.compilerOptions.verbatimModuleSyntax === true &&
802
+ (node.flags & NodeFlags.Ambient) === 0 &&
803
+ node.kind !== SyntaxKind.TypeAliasDeclaration &&
804
+ node.kind !== SyntaxKind.InterfaceDeclaration &&
805
+ node.kind !== SyntaxKind.ModuleDeclaration &&
806
+ parent.kind === SyntaxKind.SourceFile &&
807
+ getEmitModuleFormat(g) === ModuleKind.CommonJS
808
+ )
809
+ return grammarErrorOnNode(
810
+ g,
811
+ modifier,
812
+ Diagnostics.A_top_level_export_modifier_cannot_be_used_on_value_declarations_in_a_CommonJS_module_when_verbatimModuleSyntax_is_enabled,
813
+ );
814
+ if ((flags & ModifierFlags.Export) !== 0)
815
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_already_seen, 'export');
816
+ if ((flags & ModifierFlags.Ambient) !== 0 && !reparsed)
817
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_must_precede_1_modifier, 'export', 'declare');
818
+ if ((flags & ModifierFlags.Abstract) !== 0 && !reparsed)
819
+ return grammarErrorOnNode(
820
+ g,
821
+ modifier,
822
+ Diagnostics.X_0_modifier_must_precede_1_modifier,
823
+ 'export',
824
+ 'abstract',
825
+ );
826
+ if ((flags & ModifierFlags.Async) !== 0 && !reparsed)
827
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_must_precede_1_modifier, 'export', 'async');
828
+ if (isClassLike(parent) && node.kind !== SyntaxKind.JSTypeAliasDeclaration)
829
+ return grammarErrorOnNode(
830
+ g,
831
+ modifier,
832
+ Diagnostics.X_0_modifier_cannot_appear_on_class_elements_of_this_kind,
833
+ 'export',
834
+ );
835
+ if (node.kind === SyntaxKind.Parameter)
836
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_cannot_appear_on_a_parameter, 'export');
837
+ if (blockScopeKind === NodeFlags.Using)
838
+ return grammarErrorOnNode(
839
+ g,
840
+ modifier,
841
+ Diagnostics.X_0_modifier_cannot_appear_on_a_using_declaration,
842
+ 'export',
843
+ );
844
+ if (blockScopeKind === NodeFlags.AwaitUsing)
845
+ return grammarErrorOnNode(
846
+ g,
847
+ modifier,
848
+ Diagnostics.X_0_modifier_cannot_appear_on_an_await_using_declaration,
849
+ 'export',
850
+ );
851
+ flags |= ModifierFlags.Export;
852
+ break;
853
+ case SyntaxKind.DefaultKeyword: {
854
+ const container = parent.kind === SyntaxKind.SourceFile ? parent : parent.parent!;
855
+ if (container.kind === SyntaxKind.ModuleDeclaration && !isAmbientModule(container))
856
+ return grammarErrorOnNode(
857
+ g,
858
+ modifier,
859
+ Diagnostics.A_default_export_can_only_be_used_in_an_ECMAScript_style_module,
860
+ );
861
+ if (blockScopeKind === NodeFlags.Using)
862
+ return grammarErrorOnNode(
863
+ g,
864
+ modifier,
865
+ Diagnostics.X_0_modifier_cannot_appear_on_a_using_declaration,
866
+ 'default',
867
+ );
868
+ if (blockScopeKind === NodeFlags.AwaitUsing)
869
+ return grammarErrorOnNode(
870
+ g,
871
+ modifier,
872
+ Diagnostics.X_0_modifier_cannot_appear_on_an_await_using_declaration,
873
+ 'default',
874
+ );
875
+ if ((flags & ModifierFlags.Export) === 0 && !reparsed)
876
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_must_precede_1_modifier, 'export', 'default');
877
+ if (sawExportBeforeDecorators)
878
+ return grammarErrorOnNode(g, firstDecorator!, Diagnostics.Decorators_are_not_valid_here);
879
+ flags |= ModifierFlags.Default;
880
+ break;
881
+ }
882
+ case SyntaxKind.DeclareKeyword:
883
+ if ((flags & ModifierFlags.Ambient) !== 0)
884
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_already_seen, 'declare');
885
+ if ((flags & ModifierFlags.Async) !== 0)
886
+ return grammarErrorOnNode(
887
+ g,
888
+ modifier,
889
+ Diagnostics.X_0_modifier_cannot_be_used_in_an_ambient_context,
890
+ 'async',
891
+ );
892
+ if ((flags & ModifierFlags.Override) !== 0)
893
+ return grammarErrorOnNode(
894
+ g,
895
+ modifier,
896
+ Diagnostics.X_0_modifier_cannot_be_used_in_an_ambient_context,
897
+ 'override',
898
+ );
899
+ if (isClassLike(parent) && node.kind !== SyntaxKind.PropertyDeclaration)
900
+ return grammarErrorOnNode(
901
+ g,
902
+ modifier,
903
+ Diagnostics.X_0_modifier_cannot_appear_on_class_elements_of_this_kind,
904
+ 'declare',
905
+ );
906
+ if (node.kind === SyntaxKind.Parameter)
907
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_cannot_appear_on_a_parameter, 'declare');
908
+ if (blockScopeKind === NodeFlags.Using)
909
+ return grammarErrorOnNode(
910
+ g,
911
+ modifier,
912
+ Diagnostics.X_0_modifier_cannot_appear_on_a_using_declaration,
913
+ 'declare',
914
+ );
915
+ if (blockScopeKind === NodeFlags.AwaitUsing)
916
+ return grammarErrorOnNode(
917
+ g,
918
+ modifier,
919
+ Diagnostics.X_0_modifier_cannot_appear_on_an_await_using_declaration,
920
+ 'declare',
921
+ );
922
+ if ((parent.flags & NodeFlags.Ambient) !== 0 && parent.kind === SyntaxKind.ModuleBlock)
923
+ return grammarErrorOnNode(
924
+ g,
925
+ modifier,
926
+ Diagnostics.A_declare_modifier_cannot_be_used_in_an_already_ambient_context,
927
+ );
928
+ if (isPrivateIdentifierClassElementDeclaration(node))
929
+ return grammarErrorOnNode(
930
+ g,
931
+ modifier,
932
+ Diagnostics.X_0_modifier_cannot_be_used_with_a_private_identifier,
933
+ 'declare',
934
+ );
935
+ if ((flags & ModifierFlags.Accessor) !== 0)
936
+ return grammarErrorOnNode(
937
+ g,
938
+ modifier,
939
+ Diagnostics.X_0_modifier_cannot_be_used_with_1_modifier,
940
+ 'declare',
941
+ 'accessor',
942
+ );
943
+ flags |= ModifierFlags.Ambient;
944
+ lastDeclare = modifier;
945
+ break;
946
+ case SyntaxKind.AbstractKeyword: {
947
+ if ((flags & ModifierFlags.Abstract) !== 0)
948
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_already_seen, 'abstract');
949
+ if (node.kind !== SyntaxKind.ClassDeclaration && node.kind !== SyntaxKind.ConstructorType) {
950
+ if (
951
+ node.kind !== SyntaxKind.MethodDeclaration &&
952
+ node.kind !== SyntaxKind.PropertyDeclaration &&
953
+ node.kind !== SyntaxKind.GetAccessor &&
954
+ node.kind !== SyntaxKind.SetAccessor
955
+ )
956
+ return grammarErrorOnNode(
957
+ g,
958
+ modifier,
959
+ Diagnostics.X_abstract_modifier_can_only_appear_on_a_class_method_or_property_declaration,
960
+ );
961
+ if (!(parent.kind === SyntaxKind.ClassDeclaration && hasSyntacticModifier(parent, ModifierFlags.Abstract)))
962
+ return grammarErrorOnNode(
963
+ g,
964
+ modifier,
965
+ node.kind === SyntaxKind.PropertyDeclaration
966
+ ? Diagnostics.Abstract_properties_can_only_appear_within_an_abstract_class
967
+ : Diagnostics.Abstract_methods_can_only_appear_within_an_abstract_class,
968
+ );
969
+ if ((flags & ModifierFlags.Static) !== 0)
970
+ return grammarErrorOnNode(
971
+ g,
972
+ modifier,
973
+ Diagnostics.X_0_modifier_cannot_be_used_with_1_modifier,
974
+ 'static',
975
+ 'abstract',
976
+ );
977
+ if ((flags & ModifierFlags.Private) !== 0)
978
+ return grammarErrorOnNode(
979
+ g,
980
+ modifier,
981
+ Diagnostics.X_0_modifier_cannot_be_used_with_1_modifier,
982
+ 'private',
983
+ 'abstract',
984
+ );
985
+ if ((flags & ModifierFlags.Async) !== 0 && lastAsync !== undefined)
986
+ return grammarErrorOnNode(
987
+ g,
988
+ lastAsync,
989
+ Diagnostics.X_0_modifier_cannot_be_used_with_1_modifier,
990
+ 'async',
991
+ 'abstract',
992
+ );
993
+ if ((flags & ModifierFlags.Override) !== 0 && !reparsed)
994
+ return grammarErrorOnNode(
995
+ g,
996
+ modifier,
997
+ Diagnostics.X_0_modifier_must_precede_1_modifier,
998
+ 'abstract',
999
+ 'override',
1000
+ );
1001
+ if ((flags & ModifierFlags.Accessor) !== 0 && !reparsed)
1002
+ return grammarErrorOnNode(
1003
+ g,
1004
+ modifier,
1005
+ Diagnostics.X_0_modifier_must_precede_1_modifier,
1006
+ 'abstract',
1007
+ 'accessor',
1008
+ );
1009
+ }
1010
+ const name = getName(node);
1011
+ if (name !== undefined && name.kind === SyntaxKind.PrivateIdentifier)
1012
+ return grammarErrorOnNode(
1013
+ g,
1014
+ modifier,
1015
+ Diagnostics.X_0_modifier_cannot_be_used_with_a_private_identifier,
1016
+ 'abstract',
1017
+ );
1018
+ flags |= ModifierFlags.Abstract;
1019
+ break;
1020
+ }
1021
+ case SyntaxKind.AsyncKeyword:
1022
+ if ((flags & ModifierFlags.Async) !== 0)
1023
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_already_seen, 'async');
1024
+ if ((flags & ModifierFlags.Ambient) !== 0 || (parent.flags & NodeFlags.Ambient) !== 0)
1025
+ return grammarErrorOnNode(
1026
+ g,
1027
+ modifier,
1028
+ Diagnostics.X_0_modifier_cannot_be_used_in_an_ambient_context,
1029
+ 'async',
1030
+ );
1031
+ if (node.kind === SyntaxKind.Parameter)
1032
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_cannot_appear_on_a_parameter, 'async');
1033
+ if ((flags & ModifierFlags.Abstract) !== 0)
1034
+ return grammarErrorOnNode(
1035
+ g,
1036
+ modifier,
1037
+ Diagnostics.X_0_modifier_cannot_be_used_with_1_modifier,
1038
+ 'async',
1039
+ 'abstract',
1040
+ );
1041
+ flags |= ModifierFlags.Async;
1042
+ lastAsync = modifier;
1043
+ break;
1044
+ case SyntaxKind.InKeyword:
1045
+ case SyntaxKind.OutKeyword: {
1046
+ const inOutFlag = kind === SyntaxKind.InKeyword ? ModifierFlags.In : ModifierFlags.Out;
1047
+ const inOutText = kind === SyntaxKind.InKeyword ? 'in' : 'out';
1048
+ if (
1049
+ node.kind !== SyntaxKind.TypeParameter ||
1050
+ !(
1051
+ parent.kind === SyntaxKind.InterfaceDeclaration ||
1052
+ isClassLike(parent) ||
1053
+ parent.kind === SyntaxKind.TypeAliasDeclaration ||
1054
+ parent.kind === SyntaxKind.JSTypeAliasDeclaration
1055
+ )
1056
+ )
1057
+ return grammarErrorOnNode(
1058
+ g,
1059
+ modifier,
1060
+ Diagnostics.X_0_modifier_can_only_appear_on_a_type_parameter_of_a_class_interface_or_type_alias,
1061
+ inOutText,
1062
+ );
1063
+ if ((flags & inOutFlag) !== 0)
1064
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_already_seen, inOutText);
1065
+ if ((inOutFlag & ModifierFlags.In) !== 0 && (flags & ModifierFlags.Out) !== 0)
1066
+ return grammarErrorOnNode(g, modifier, Diagnostics.X_0_modifier_must_precede_1_modifier, 'in', 'out');
1067
+ flags |= inOutFlag;
1068
+ break;
1069
+ }
1070
+ }
1071
+ }
1072
+ if (node.kind === SyntaxKind.Constructor) {
1073
+ if ((flags & ModifierFlags.Static) !== 0)
1074
+ return grammarErrorOnNode(
1075
+ g,
1076
+ lastStatic!,
1077
+ Diagnostics.X_0_modifier_cannot_appear_on_a_constructor_declaration,
1078
+ 'static',
1079
+ );
1080
+ if ((flags & ModifierFlags.Override) !== 0)
1081
+ return grammarErrorOnNode(
1082
+ g,
1083
+ lastOverride!,
1084
+ Diagnostics.X_0_modifier_cannot_appear_on_a_constructor_declaration,
1085
+ 'override',
1086
+ );
1087
+ if ((flags & ModifierFlags.Async) !== 0)
1088
+ return grammarErrorOnNode(
1089
+ g,
1090
+ lastAsync!,
1091
+ Diagnostics.X_0_modifier_cannot_appear_on_a_constructor_declaration,
1092
+ 'async',
1093
+ );
1094
+ return false;
1095
+ }
1096
+ if (
1097
+ (node.kind === SyntaxKind.ImportDeclaration ||
1098
+ node.kind === SyntaxKind.JSImportDeclaration ||
1099
+ node.kind === SyntaxKind.ImportEqualsDeclaration) &&
1100
+ (flags & ModifierFlags.Ambient) !== 0
1101
+ )
1102
+ return grammarErrorOnNode(
1103
+ g,
1104
+ lastDeclare!,
1105
+ Diagnostics.A_0_modifier_cannot_be_used_with_an_import_declaration,
1106
+ 'declare',
1107
+ );
1108
+ if (node.kind === SyntaxKind.Parameter && (flags & ModifierFlags.ParameterPropertyModifier) !== 0) {
1109
+ const parameter = node as ParameterDeclaration;
1110
+ if (isBindingPattern(parameter.name))
1111
+ return grammarErrorOnNode(g, node, Diagnostics.A_parameter_property_may_not_be_declared_using_a_binding_pattern);
1112
+ if (parameter.dotDotDotToken !== undefined)
1113
+ return grammarErrorOnNode(g, node, Diagnostics.A_parameter_property_cannot_be_declared_using_a_rest_parameter);
1114
+ }
1115
+ if ((flags & ModifierFlags.Async) !== 0) return checkGrammarAsyncModifier(g, node, lastAsync!);
1116
+ return false;
1117
+ }
1118
+
1119
+ function isConstTypeParameterContainer(parent: Node): boolean {
1120
+ switch (parent.kind) {
1121
+ case SyntaxKind.FunctionType:
1122
+ case SyntaxKind.ConstructorType:
1123
+ case SyntaxKind.CallSignature:
1124
+ case SyntaxKind.ConstructSignature:
1125
+ case SyntaxKind.MethodSignature:
1126
+ return true;
1127
+ }
1128
+ return isFunctionLikeDeclarationKind(parent.kind) || isClassLike(parent);
1129
+ }
1130
+
1131
+ function reportObviousModifierErrors(g: GrammarChecker, node: Node, modifiers: ModifierList): boolean {
1132
+ const modifier = findFirstIllegalModifier(node, modifiers);
1133
+ return modifier !== undefined && grammarErrorOnFirstToken(g, modifier, Diagnostics.Modifiers_cannot_appear_here);
1134
+ }
1135
+
1136
+ function findFirstModifier(modifiers: ModifierList): Node | undefined {
1137
+ for (const modifier of modifiers.nodes) if (isModifierKind(modifier.kind)) return modifier;
1138
+ return undefined;
1139
+ }
1140
+
1141
+ function findFirstModifierExcept(modifiers: ModifierList, allowedModifier: SyntaxKind): Node | undefined {
1142
+ const modifier = findFirstModifier(modifiers);
1143
+ return modifier !== undefined && modifier.kind !== allowedModifier ? modifier : undefined;
1144
+ }
1145
+
1146
+ function findFirstIllegalModifier(node: Node, modifiers: ModifierList): Node | undefined {
1147
+ switch (node.kind) {
1148
+ case SyntaxKind.GetAccessor:
1149
+ case SyntaxKind.SetAccessor:
1150
+ case SyntaxKind.Constructor:
1151
+ case SyntaxKind.PropertyDeclaration:
1152
+ case SyntaxKind.PropertySignature:
1153
+ case SyntaxKind.MethodDeclaration:
1154
+ case SyntaxKind.MethodSignature:
1155
+ case SyntaxKind.IndexSignature:
1156
+ case SyntaxKind.ModuleDeclaration:
1157
+ case SyntaxKind.ImportDeclaration:
1158
+ case SyntaxKind.JSImportDeclaration:
1159
+ case SyntaxKind.ImportEqualsDeclaration:
1160
+ case SyntaxKind.ExportDeclaration:
1161
+ case SyntaxKind.ExportAssignment:
1162
+ case SyntaxKind.FunctionExpression:
1163
+ case SyntaxKind.ArrowFunction:
1164
+ case SyntaxKind.Parameter:
1165
+ case SyntaxKind.TypeParameter:
1166
+ case SyntaxKind.JSTypeAliasDeclaration:
1167
+ return undefined;
1168
+ case SyntaxKind.ClassStaticBlockDeclaration:
1169
+ case SyntaxKind.PropertyAssignment:
1170
+ case SyntaxKind.ShorthandPropertyAssignment:
1171
+ case SyntaxKind.NamespaceExportDeclaration:
1172
+ case SyntaxKind.MissingDeclaration:
1173
+ return findFirstModifier(modifiers);
1174
+ }
1175
+ const parent = node.parent!;
1176
+ if (parent.kind === SyntaxKind.ModuleBlock || parent.kind === SyntaxKind.SourceFile) return undefined;
1177
+ switch (node.kind) {
1178
+ case SyntaxKind.FunctionDeclaration:
1179
+ return findFirstModifierExcept(modifiers, SyntaxKind.AsyncKeyword);
1180
+ case SyntaxKind.ClassDeclaration:
1181
+ case SyntaxKind.ConstructorType:
1182
+ return findFirstModifierExcept(modifiers, SyntaxKind.AbstractKeyword);
1183
+ case SyntaxKind.VariableStatement:
1184
+ if (((node as VariableStatement).declarationList.flags & NodeFlags.Using) !== 0)
1185
+ return findFirstModifierExcept(modifiers, SyntaxKind.AwaitKeyword);
1186
+ return findFirstModifier(modifiers);
1187
+ case SyntaxKind.EnumDeclaration:
1188
+ return findFirstModifierExcept(modifiers, SyntaxKind.ConstKeyword);
1189
+ }
1190
+ // Class expressions, interfaces and type aliases.
1191
+ return findFirstModifier(modifiers);
1192
+ }
1193
+
1194
+ function reportObviousDecoratorErrors(g: GrammarChecker, node: Node, modifiers: ModifierList): boolean {
1195
+ if (!canHaveIllegalDecorators(node)) return false;
1196
+ for (const modifier of modifiers.nodes)
1197
+ if (modifier.kind === SyntaxKind.Decorator)
1198
+ return grammarErrorOnFirstToken(g, modifier, Diagnostics.Decorators_are_not_valid_here);
1199
+ return false;
1200
+ }
1201
+
1202
+ function checkGrammarAsyncModifier(g: GrammarChecker, node: Node, asyncModifier: Node): boolean {
1203
+ switch (node.kind) {
1204
+ case SyntaxKind.MethodDeclaration:
1205
+ case SyntaxKind.FunctionDeclaration:
1206
+ case SyntaxKind.FunctionExpression:
1207
+ case SyntaxKind.ArrowFunction:
1208
+ return false;
1209
+ }
1210
+ return grammarErrorOnNode(g, asyncModifier, Diagnostics.X_0_modifier_cannot_be_used_here, 'async');
1211
+ }
1212
+
1213
+ function hasDecorators(node: Node): boolean {
1214
+ const modifiers = getModifiers(node);
1215
+ if (modifiers === undefined) return false;
1216
+ for (const modifier of modifiers.nodes) if (modifier.kind === SyntaxKind.Decorator) return true;
1217
+ return false;
1218
+ }
1219
+
1220
+ /** Whether `node` is a valid decorator target. */
1221
+ function nodeCanBeDecorated(
1222
+ legacy: boolean,
1223
+ node: Node,
1224
+ parent: Node | undefined,
1225
+ grandparent: Node | undefined,
1226
+ ): boolean {
1227
+ const name = getName(node);
1228
+ // Legacy decorators don't support private names.
1229
+ if (legacy && name !== undefined && name.kind === SyntaxKind.PrivateIdentifier) return false;
1230
+ switch (node.kind) {
1231
+ case SyntaxKind.ClassDeclaration:
1232
+ return true;
1233
+ case SyntaxKind.ClassExpression:
1234
+ return !legacy;
1235
+ case SyntaxKind.PropertyDeclaration:
1236
+ return (
1237
+ parent !== undefined &&
1238
+ (legacy
1239
+ ? parent.kind === SyntaxKind.ClassDeclaration
1240
+ : isClassLike(parent) && !hasSyntacticModifier(node, ModifierFlags.Abstract | ModifierFlags.Ambient))
1241
+ );
1242
+ case SyntaxKind.GetAccessor:
1243
+ case SyntaxKind.SetAccessor:
1244
+ case SyntaxKind.MethodDeclaration:
1245
+ return (
1246
+ parent !== undefined &&
1247
+ getBody(node) !== undefined &&
1248
+ (legacy ? parent.kind === SyntaxKind.ClassDeclaration : isClassLike(parent))
1249
+ );
1250
+ case SyntaxKind.Parameter:
1251
+ if (!legacy || parent === undefined || getBody(parent) === undefined) return false;
1252
+ return (
1253
+ (parent.kind === SyntaxKind.Constructor ||
1254
+ parent.kind === SyntaxKind.MethodDeclaration ||
1255
+ parent.kind === SyntaxKind.SetAccessor) &&
1256
+ getThisParameter(parent) !== node &&
1257
+ grandparent !== undefined &&
1258
+ grandparent.kind === SyntaxKind.ClassDeclaration
1259
+ );
1260
+ }
1261
+ return false;
1262
+ }
1263
+
1264
+ /** The first of the get/set accessor pair `node` belongs to. */
1265
+ function getFirstAccessorOfPair(c: Checker, node: Node): Node {
1266
+ const symbol = getSymbolOfDeclaration(c, node);
1267
+ if (symbol === undefined) return node;
1268
+ const otherKind = node.kind === SyntaxKind.GetAccessor ? SyntaxKind.SetAccessor : SyntaxKind.GetAccessor;
1269
+ for (const declaration of symbol.declarations)
1270
+ if (declaration.kind === otherKind && declaration.pos < node.pos) return declaration;
1271
+ return node;
1272
+ }
1273
+
1274
+ /** The decorator checks of `checkDecorators`. */
1275
+ function checkGrammarDecorators(g: GrammarChecker, node: Node): void {
1276
+ if (!canHaveDecorators(node) || !hasDecorators(node)) return;
1277
+ if (!nodeCanBeDecorated(g.legacyDecorators, node, node.parent, node.parent!.parent)) return;
1278
+ for (const modifier of getModifiers(node)!.nodes)
1279
+ if (modifier.kind === SyntaxKind.Decorator) checkGrammarDecorator(g, modifier as Decorator);
1280
+ }
1281
+
1282
+ function checkGrammarDecorator(g: GrammarChecker, decorator: Decorator): boolean {
1283
+ if (g.hasParseErrors) return false;
1284
+ let node: Node = decorator.expression;
1285
+ // `@(expression)`
1286
+ if (node.kind === SyntaxKind.ParenthesizedExpression) return false;
1287
+ let canHaveCallExpression = true;
1288
+ let errorNode: Node | undefined;
1289
+ for (;;) {
1290
+ // Non-null assertions and instantiation expressions are allowed.
1291
+ if (node.kind === SyntaxKind.ExpressionWithTypeArguments || node.kind === SyntaxKind.NonNullExpression) {
1292
+ node = getExpression(node)!;
1293
+ continue;
1294
+ }
1295
+ // `@member.expression(arguments)`
1296
+ if (node.kind === SyntaxKind.CallExpression) {
1297
+ const call = node as CallExpression;
1298
+ if (!canHaveCallExpression) errorNode = node;
1299
+ // A `?.` token comes first, so it wins over an earlier error node.
1300
+ if (call.questionDotToken !== undefined) errorNode = call.questionDotToken;
1301
+ node = call.expression;
1302
+ canHaveCallExpression = false;
1303
+ continue;
1304
+ }
1305
+ // `@identifier`, `@member.name` and `@member.#name`
1306
+ if (node.kind === SyntaxKind.PropertyAccessExpression) {
1307
+ const access = node as PropertyAccessExpression;
1308
+ if (access.questionDotToken !== undefined) errorNode = access.questionDotToken;
1309
+ node = access.expression;
1310
+ canHaveCallExpression = false;
1311
+ continue;
1312
+ }
1313
+ if (node.kind !== SyntaxKind.Identifier) errorNode = node;
1314
+ break;
1315
+ }
1316
+ if (errorNode === undefined) return false;
1317
+ const diagnostic = g.c.error(
1318
+ decorator.expression,
1319
+ Diagnostics.Expression_must_be_enclosed_in_parentheses_to_be_used_as_a_decorator,
1320
+ );
1321
+ addRelated(diagnostic, errorNode, Diagnostics.Invalid_syntax_in_decorator);
1322
+ return true;
1323
+ }
1324
+
1325
+ // ---- Functions and parameters
1326
+
1327
+ function checkGrammarFunctionLikeDeclaration(g: GrammarChecker, node: Node): boolean {
1328
+ // Short-circuits to prevent cascading errors.
1329
+ return (
1330
+ checkGrammarModifiers(g, node) ||
1331
+ checkGrammarTypeParameterList(g, getTypeParameters(node)) ||
1332
+ checkGrammarParameterList(g, getParameters(node)!) ||
1333
+ checkGrammarArrowFunction(g, node) ||
1334
+ (isFunctionLikeDeclarationKind(node.kind) && checkGrammarForUseStrictSimpleParameterList(g, node))
1335
+ );
1336
+ }
1337
+
1338
+ function checkGrammarForDisallowedTrailingComma(
1339
+ g: GrammarChecker,
1340
+ list: NodeList | undefined,
1341
+ message: DiagnosticMessage,
1342
+ ): boolean {
1343
+ return list !== undefined && list.hasTrailingComma() && grammarErrorAtPos(g, list.end - 1, 1, message);
1344
+ }
1345
+
1346
+ function checkGrammarTypeParameterList(g: GrammarChecker, typeParameters: NodeList | undefined): boolean {
1347
+ if (typeParameters === undefined || typeParameters.nodes.length !== 0) return false;
1348
+ const start = typeParameters.pos - 1;
1349
+ const end = skipTrivia(g.file.text, typeParameters.end) + 1;
1350
+ return grammarErrorAtPos(g, start, end - start, Diagnostics.Type_parameter_list_cannot_be_empty);
1351
+ }
1352
+
1353
+ function checkGrammarParameterList(g: GrammarChecker, parameters: NodeList): boolean {
1354
+ let seenOptionalParameter = false;
1355
+ const count = parameters.nodes.length;
1356
+ for (let i = 0; i < count; i++) {
1357
+ const parameter = parameters.nodes[i]! as ParameterDeclaration;
1358
+ if (parameter.dotDotDotToken !== undefined) {
1359
+ if (i !== count - 1)
1360
+ return grammarErrorOnNode(
1361
+ g,
1362
+ parameter.dotDotDotToken,
1363
+ Diagnostics.A_rest_parameter_must_be_last_in_a_parameter_list,
1364
+ );
1365
+ if ((parameter.flags & NodeFlags.Ambient) === 0)
1366
+ checkGrammarForDisallowedTrailingComma(
1367
+ g,
1368
+ parameters,
1369
+ Diagnostics.A_rest_parameter_or_binding_pattern_may_not_have_a_trailing_comma,
1370
+ );
1371
+ if (parameter.questionToken !== undefined)
1372
+ return grammarErrorOnNode(g, parameter.questionToken, Diagnostics.A_rest_parameter_cannot_be_optional);
1373
+ if (parameter.initializer !== undefined)
1374
+ return grammarErrorOnNode(g, parameter.name, Diagnostics.A_rest_parameter_cannot_have_an_initializer);
1375
+ } else if (parameter.questionToken !== undefined) {
1376
+ seenOptionalParameter = true;
1377
+ // A reparsed `?` comes from a bracketed `@param` name.
1378
+ if ((parameter.questionToken.flags & NodeFlags.Reparsed) === 0 && parameter.initializer !== undefined)
1379
+ return grammarErrorOnNode(g, parameter.name, Diagnostics.Parameter_cannot_have_question_mark_and_initializer);
1380
+ } else if (seenOptionalParameter && parameter.initializer === undefined) {
1381
+ return grammarErrorOnNode(
1382
+ g,
1383
+ parameter.name,
1384
+ Diagnostics.A_required_parameter_cannot_follow_an_optional_parameter,
1385
+ );
1386
+ }
1387
+ }
1388
+ return false;
1389
+ }
1390
+
1391
+ function checkGrammarForUseStrictSimpleParameterList(g: GrammarChecker, node: Node): boolean {
1392
+ if (g.c.languageVersion < ScriptTarget.ES2016) return false;
1393
+ const body = getBody(node);
1394
+ if (body === undefined || body.kind !== SyntaxKind.Block) return false;
1395
+ const useStrictDirective = findUseStrictPrologue(g.file, getStatements(body)!.nodes);
1396
+ if (useStrictDirective === undefined) return false;
1397
+ const nonSimpleParameters: Node[] = [];
1398
+ for (const parameter of getParameters(node)!.nodes) {
1399
+ const p = parameter as ParameterDeclaration;
1400
+ if (p.initializer !== undefined || isBindingPattern(p.name) || p.dotDotDotToken !== undefined)
1401
+ nonSimpleParameters.push(parameter);
1402
+ }
1403
+ if (nonSimpleParameters.length === 0) return false;
1404
+ for (const parameter of nonSimpleParameters) {
1405
+ const diagnostic = g.c.error(parameter, Diagnostics.This_parameter_is_not_allowed_with_use_strict_directive);
1406
+ addRelated(diagnostic, useStrictDirective, Diagnostics.X_use_strict_directive_used_here);
1407
+ }
1408
+ const diagnostic = g.c.error(
1409
+ useStrictDirective,
1410
+ Diagnostics.X_use_strict_directive_cannot_be_used_with_non_simple_parameter_list,
1411
+ );
1412
+ for (let i = 0; i < nonSimpleParameters.length; i++)
1413
+ addRelated(
1414
+ diagnostic,
1415
+ nonSimpleParameters[i]!,
1416
+ i === 0 ? Diagnostics.Non_simple_parameter_declared_here : Diagnostics.X_and_here,
1417
+ );
1418
+ return true;
1419
+ }
1420
+
1421
+ function checkGrammarArrowFunction(g: GrammarChecker, node: Node): boolean {
1422
+ if (node.kind !== SyntaxKind.ArrowFunction) return false;
1423
+ const arrow = node as ArrowFunction;
1424
+ const typeParameters = arrow.typeParameters;
1425
+ if (typeParameters !== undefined) {
1426
+ const nodes = typeParameters.nodes;
1427
+ const hasConstraint = nodes.length > 0 && (nodes[0]! as TypeParameterDeclaration).constraint !== undefined;
1428
+ if (
1429
+ !(nodes.length > 1 || typeParameters.hasTrailingComma() || hasConstraint) &&
1430
+ fileExtensionIsOneOf(g.file.fileName, ['.mts', '.cts'])
1431
+ )
1432
+ grammarErrorOnNode(
1433
+ g,
1434
+ nodes[0]!,
1435
+ Diagnostics.This_syntax_is_reserved_in_files_with_the_mts_or_cts_extension_Add_a_trailing_comma_or_explicit_constraint,
1436
+ );
1437
+ }
1438
+ const arrowToken = arrow.equalsGreaterThanToken;
1439
+ if (arrowToken === undefined) return false;
1440
+ const lineStarts = g.file.lineStarts();
1441
+ return (
1442
+ computeLineOfPosition(lineStarts, arrowToken.pos) !== computeLineOfPosition(lineStarts, arrowToken.end) &&
1443
+ grammarErrorOnNode(g, arrowToken, Diagnostics.Line_terminator_not_permitted_before_arrow)
1444
+ );
1445
+ }
1446
+
1447
+ function checkGrammarForGenerator(g: GrammarChecker, node: Node): boolean {
1448
+ const asteriskToken = getAsteriskToken(node);
1449
+ if (asteriskToken === undefined) return false;
1450
+ if ((node.flags & NodeFlags.Ambient) !== 0)
1451
+ return grammarErrorOnNode(g, asteriskToken, Diagnostics.Generators_are_not_allowed_in_an_ambient_context);
1452
+ if (getBody(node) === undefined)
1453
+ return grammarErrorOnNode(g, asteriskToken, Diagnostics.An_overload_signature_cannot_be_declared_as_a_generator);
1454
+ return false;
1455
+ }
1456
+
1457
+ function isThisParameter(node: Node): boolean {
1458
+ if (node.kind !== SyntaxKind.Parameter) return false;
1459
+ const name = (node as ParameterDeclaration).name;
1460
+ return name instanceof Identifier && name.text === 'this';
1461
+ }
1462
+
1463
+ function getThisParameter(signature: Node): Node | undefined {
1464
+ const parameters = getParameters(signature);
1465
+ if (parameters === undefined || parameters.nodes.length === 0) return undefined;
1466
+ const first = parameters.nodes[0]!;
1467
+ return isThisParameter(first) ? first : undefined;
1468
+ }
1469
+
1470
+ function isBindingPattern(node: Node | undefined): boolean {
1471
+ return (
1472
+ node !== undefined &&
1473
+ (node.kind === SyntaxKind.ObjectBindingPattern || node.kind === SyntaxKind.ArrayBindingPattern)
1474
+ );
1475
+ }
1476
+
1477
+ // ---- Signatures and members
1478
+
1479
+ function checkGrammarIndexSignatureParameters(g: GrammarChecker, node: IndexSignatureDeclaration): boolean {
1480
+ const parameters = node.parameters.nodes;
1481
+ if (parameters.length === 0)
1482
+ return grammarErrorOnNode(g, node, Diagnostics.An_index_signature_must_have_exactly_one_parameter);
1483
+ const parameter = parameters[0]! as ParameterDeclaration;
1484
+ if (parameters.length !== 1)
1485
+ return grammarErrorOnNode(g, parameter.name, Diagnostics.An_index_signature_must_have_exactly_one_parameter);
1486
+ checkGrammarForDisallowedTrailingComma(
1487
+ g,
1488
+ node.parameters,
1489
+ Diagnostics.An_index_signature_cannot_have_a_trailing_comma,
1490
+ );
1491
+ if (parameter.dotDotDotToken !== undefined)
1492
+ return grammarErrorOnNode(g, parameter.dotDotDotToken, Diagnostics.An_index_signature_cannot_have_a_rest_parameter);
1493
+ if (parameter.modifiers !== undefined)
1494
+ return grammarErrorOnNode(
1495
+ g,
1496
+ parameter.name,
1497
+ Diagnostics.An_index_signature_parameter_cannot_have_an_accessibility_modifier,
1498
+ );
1499
+ if (parameter.questionToken !== undefined)
1500
+ return grammarErrorOnNode(
1501
+ g,
1502
+ parameter.questionToken,
1503
+ Diagnostics.An_index_signature_parameter_cannot_have_a_question_mark,
1504
+ );
1505
+ if (parameter.initializer !== undefined)
1506
+ return grammarErrorOnNode(g, parameter.name, Diagnostics.An_index_signature_parameter_cannot_have_an_initializer);
1507
+ if (parameter.type === undefined)
1508
+ return grammarErrorOnNode(g, parameter.name, Diagnostics.An_index_signature_parameter_must_have_a_type_annotation);
1509
+ const t = getTypeFromTypeNode(g.c, parameter.type);
1510
+ if (someTypeHasFlags(t, TypeFlags.StringOrNumberLiteralOrUnique) || isGenericType(g.c, t))
1511
+ return grammarErrorOnNode(
1512
+ g,
1513
+ parameter.name,
1514
+ Diagnostics.An_index_signature_parameter_type_cannot_be_a_literal_type_or_generic_type_Consider_using_a_mapped_object_type_instead,
1515
+ );
1516
+ if (!everyTypeIsValidIndexKey(g.c, t))
1517
+ return grammarErrorOnNode(
1518
+ g,
1519
+ parameter.name,
1520
+ Diagnostics.An_index_signature_parameter_type_must_be_string_number_symbol_or_a_template_literal_type,
1521
+ );
1522
+ if (node.type === undefined)
1523
+ return grammarErrorOnNode(g, node, Diagnostics.An_index_signature_must_have_a_type_annotation);
1524
+ return false;
1525
+ }
1526
+
1527
+ function someTypeHasFlags(t: Type, flags: TypeFlags): boolean {
1528
+ if ((t.flags & TypeFlags.Union) === 0) return (t.flags & flags) !== 0;
1529
+ for (const member of (t as UnionType).types) if ((member.flags & flags) !== 0) return true;
1530
+ return false;
1531
+ }
1532
+
1533
+ function everyTypeIsValidIndexKey(c: Checker, t: Type): boolean {
1534
+ if ((t.flags & TypeFlags.Union) === 0) return isValidIndexKeyType(c, t);
1535
+ for (const member of (t as UnionType).types) if (!isValidIndexKeyType(c, member)) return false;
1536
+ return true;
1537
+ }
1538
+
1539
+ function checkGrammarTypeArguments(g: GrammarChecker, typeArguments: NodeList | undefined): boolean {
1540
+ if (checkGrammarForDisallowedTrailingComma(g, typeArguments, Diagnostics.Trailing_comma_not_allowed)) return true;
1541
+ if (typeArguments === undefined || typeArguments.nodes.length !== 0) return false;
1542
+ const start = typeArguments.pos - 1;
1543
+ const end = skipTrivia(g.file.text, typeArguments.end) + 1;
1544
+ return grammarErrorAtPos(g, start, end - start, Diagnostics.Type_argument_list_cannot_be_empty);
1545
+ }
1546
+
1547
+ function checkGrammarExpressionWithTypeArguments(g: GrammarChecker, node: Node): boolean {
1548
+ const typeArguments = getTypeArguments(node);
1549
+ if (
1550
+ node.kind === SyntaxKind.ExpressionWithTypeArguments &&
1551
+ (node as ExpressionWithTypeArguments).expression.kind === SyntaxKind.ImportKeyword &&
1552
+ typeArguments !== undefined
1553
+ )
1554
+ return grammarErrorOnNode(
1555
+ g,
1556
+ node,
1557
+ Diagnostics.This_use_of_import_is_invalid_import_calls_can_be_written_but_they_must_have_parentheses_and_cannot_have_type_arguments,
1558
+ );
1559
+ return checkGrammarTypeArguments(g, typeArguments);
1560
+ }
1561
+
1562
+ function checkGrammarHeritageClause(g: GrammarChecker, clause: HeritageClause): boolean {
1563
+ const types = clause.types;
1564
+ if (checkGrammarForDisallowedTrailingComma(g, types, Diagnostics.Trailing_comma_not_allowed)) return true;
1565
+ if (types.nodes.length === 0)
1566
+ return grammarErrorAtPos(g, types.pos, 0, Diagnostics.X_0_list_cannot_be_empty, tokenToString(clause.token));
1567
+ for (const type of types.nodes) if (checkGrammarExpressionWithTypeArguments(g, type)) return true;
1568
+ return false;
1569
+ }
1570
+
1571
+ function checkGrammarClassDeclaration(g: GrammarChecker, node: Node): void {
1572
+ const modifiers = getModifiers(node);
1573
+ if (g.legacyDecorators && modifiers !== undefined) {
1574
+ let firstDecorator: Node | undefined;
1575
+ for (const modifier of modifiers.nodes)
1576
+ if (modifier.kind === SyntaxKind.Decorator) {
1577
+ firstDecorator = modifier;
1578
+ break;
1579
+ }
1580
+ if (firstDecorator !== undefined)
1581
+ for (const member of getMembers(node)!.nodes)
1582
+ if (hasSyntacticModifier(member, ModifierFlags.Static) && isPrivateIdentifierClassElementDeclaration(member)) {
1583
+ grammarErrorOnNode(
1584
+ g,
1585
+ firstDecorator,
1586
+ Diagnostics.Class_decorators_can_t_be_used_with_static_private_identifier_Consider_removing_the_experimental_decorator,
1587
+ );
1588
+ break;
1589
+ }
1590
+ }
1591
+ if (getName(node) === undefined && !hasSyntacticModifier(node, ModifierFlags.Default))
1592
+ grammarErrorOnFirstToken(g, node, Diagnostics.A_class_declaration_without_the_default_modifier_must_have_a_name);
1593
+ checkGrammarClassLikeDeclaration(g, node);
1594
+ }
1595
+
1596
+ function checkGrammarClassLikeDeclaration(g: GrammarChecker, node: Node): void {
1597
+ if (!checkGrammarClassDeclarationHeritageClauses(g, node)) checkGrammarTypeParameterList(g, getTypeParameters(node));
1598
+ checkGrammarDecorators(g, node);
1599
+ // `implements` clause elements are checked again as type references.
1600
+ const clauses = getHeritageClauses(node);
1601
+ if (clauses !== undefined)
1602
+ for (const clause of clauses.nodes)
1603
+ if ((clause as HeritageClause).token === SyntaxKind.ImplementsKeyword)
1604
+ for (const type of (clause as HeritageClause).types.nodes) checkGrammarTypeArguments(g, getTypeArguments(type));
1605
+ }
1606
+
1607
+ function checkGrammarClassDeclarationHeritageClauses(g: GrammarChecker, node: Node): boolean {
1608
+ let seenExtendsClause = false;
1609
+ let seenImplementsClause = false;
1610
+ const clauses = getHeritageClauses(node);
1611
+ if (checkGrammarModifiers(g, node) || clauses === undefined) return false;
1612
+ for (const clauseNode of clauses.nodes) {
1613
+ const clause = clauseNode as HeritageClause;
1614
+ if (clause.token === SyntaxKind.ExtendsKeyword) {
1615
+ if (seenExtendsClause) return grammarErrorOnFirstToken(g, clause, Diagnostics.X_extends_clause_already_seen);
1616
+ if (seenImplementsClause)
1617
+ return grammarErrorOnFirstToken(g, clause, Diagnostics.X_extends_clause_must_precede_implements_clause);
1618
+ if (clause.types.nodes.length > 1)
1619
+ return grammarErrorOnFirstToken(g, clause.types.nodes[1]!, Diagnostics.Classes_can_only_extend_a_single_class);
1620
+ if (clause.types.nodes.length > 0) {
1621
+ const mismatch = findMismatchedAugmentsTag(node, clause.types.nodes[0]! as ExpressionWithTypeArguments);
1622
+ if (mismatch !== undefined)
1623
+ return grammarErrorOnNode(
1624
+ g,
1625
+ mismatch.name,
1626
+ Diagnostics.JSDoc_0_1_does_not_match_the_extends_2_clause,
1627
+ mismatch.tagName,
1628
+ mismatch.className,
1629
+ mismatch.extendsName,
1630
+ );
1631
+ }
1632
+ seenExtendsClause = true;
1633
+ } else {
1634
+ if (seenImplementsClause)
1635
+ return grammarErrorOnFirstToken(g, clause, Diagnostics.X_implements_clause_already_seen);
1636
+ seenImplementsClause = true;
1637
+ }
1638
+ checkGrammarHeritageClause(g, clause);
1639
+ }
1640
+ return false;
1641
+ }
1642
+
1643
+ function checkGrammarInterfaceDeclaration(g: GrammarChecker, node: Node): void {
1644
+ const clauses = getHeritageClauses(node);
1645
+ if (!checkGrammarModifiers(g, node) && clauses !== undefined) {
1646
+ let seenExtendsClause = false;
1647
+ for (const clauseNode of clauses.nodes) {
1648
+ const clause = clauseNode as HeritageClause;
1649
+ if (clause.token === SyntaxKind.ExtendsKeyword) {
1650
+ if (seenExtendsClause) {
1651
+ grammarErrorOnFirstToken(g, clause, Diagnostics.X_extends_clause_already_seen);
1652
+ break;
1653
+ }
1654
+ seenExtendsClause = true;
1655
+ } else {
1656
+ grammarErrorOnFirstToken(g, clause, Diagnostics.Interface_declaration_cannot_have_implements_clause);
1657
+ break;
1658
+ }
1659
+ checkGrammarHeritageClause(g, clause);
1660
+ }
1661
+ }
1662
+ if (!containerAllowsBlockScopedVariable(node.parent!))
1663
+ grammarErrorOnNode(g, node, Diagnostics.X_0_declarations_can_only_be_declared_inside_a_block, 'interface');
1664
+ // `extends` clause elements are checked again as type references.
1665
+ if (clauses !== undefined)
1666
+ for (const clause of clauses.nodes)
1667
+ if ((clause as HeritageClause).token === SyntaxKind.ExtendsKeyword)
1668
+ for (const type of (clause as HeritageClause).types.nodes) checkGrammarTypeArguments(g, getTypeArguments(type));
1669
+ }
1670
+
1671
+ function checkGrammarComputedPropertyName(g: GrammarChecker, node: Node): boolean {
1672
+ if (node.kind !== SyntaxKind.ComputedPropertyName) return false;
1673
+ const expression = (node as ComputedPropertyName).expression;
1674
+ return (
1675
+ expression.kind === SyntaxKind.BinaryExpression &&
1676
+ (expression as BinaryExpression).operatorToken.kind === SyntaxKind.CommaToken &&
1677
+ grammarErrorOnNode(g, expression, Diagnostics.A_comma_expression_is_not_allowed_in_a_computed_property_name)
1678
+ );
1679
+ }
1680
+
1681
+ function checkGrammarForInvalidQuestionMark(
1682
+ g: GrammarChecker,
1683
+ postfixToken: Node | undefined,
1684
+ message: DiagnosticMessage,
1685
+ ): boolean {
1686
+ return (
1687
+ postfixToken !== undefined &&
1688
+ postfixToken.kind === SyntaxKind.QuestionToken &&
1689
+ grammarErrorOnNode(g, postfixToken, message)
1690
+ );
1691
+ }
1692
+
1693
+ function checkGrammarForInvalidExclamationToken(
1694
+ g: GrammarChecker,
1695
+ postfixToken: Node | undefined,
1696
+ message: DiagnosticMessage,
1697
+ ): boolean {
1698
+ return (
1699
+ postfixToken !== undefined &&
1700
+ postfixToken.kind === SyntaxKind.ExclamationToken &&
1701
+ grammarErrorOnNode(g, postfixToken, message)
1702
+ );
1703
+ }
1704
+
1705
+ /** Whether a name is dynamic and can't be late bound: its type isn't a literal or unique symbol. */
1706
+ function isNonBindableDynamicName(c: Checker, node: Node): boolean {
1707
+ if (!isDynamicName(node)) return false;
1708
+ let expression: Node | undefined;
1709
+ if (node.kind === SyntaxKind.ComputedPropertyName) expression = getExpression(node);
1710
+ else if (node instanceof ElementAccessExpression) expression = node.argumentExpression;
1711
+ if (expression === undefined || !isEntityNameExpression(expression, false)) return true;
1712
+ const t =
1713
+ node.kind === SyntaxKind.ComputedPropertyName
1714
+ ? checkComputedPropertyName(c, node)
1715
+ : checkExpressionCached(c, expression);
1716
+ return !isTypeUsableAsPropertyName(t);
1717
+ }
1718
+
1719
+ function checkGrammarForInvalidDynamicName(g: GrammarChecker, node: Node, message: DiagnosticMessage): boolean {
1720
+ if (!isNonBindableDynamicName(g.c, node)) return false;
1721
+ const expression =
1722
+ node instanceof ElementAccessExpression ? skipParentheses(node.argumentExpression) : getExpression(node)!;
1723
+ return !isEntityNameExpression(expression, false) && grammarErrorOnNode(g, node, message);
1724
+ }
1725
+
1726
+ function checkGrammarMethodDeclaration(g: GrammarChecker, node: Node): void {
1727
+ if (node.parent!.kind === SyntaxKind.ObjectLiteralExpression) {
1728
+ // `checkObjectLiteralMethod` checks the method, then checks it again as a function expression.
1729
+ checkGrammarMethod(g, node);
1730
+ checkGrammarFunctionLikeDeclaration(g, node);
1731
+ return;
1732
+ }
1733
+ const name = getName(node)!;
1734
+ if (!checkGrammarMethod(g, node)) {
1735
+ checkGrammarComputedPropertyName(g, name);
1736
+ if (getAsteriskToken(node) !== undefined && name instanceof Identifier && name.text === 'constructor')
1737
+ g.c.error(name, Diagnostics.Class_constructor_may_not_be_a_generator);
1738
+ }
1739
+ checkGrammarDecorators(g, node);
1740
+ if (name.kind === SyntaxKind.PrivateIdentifier && getContainingClass(node) === undefined)
1741
+ g.c.error(node, Diagnostics.Private_identifiers_are_not_allowed_outside_class_bodies);
1742
+ }
1743
+
1744
+ function checkGrammarMethod(g: GrammarChecker, node: Node): boolean {
1745
+ if (checkGrammarFunctionLikeDeclaration(g, node)) return true;
1746
+ const parent = node.parent!;
1747
+ if (node.kind === SyntaxKind.MethodDeclaration) {
1748
+ if (parent.kind === SyntaxKind.ObjectLiteralExpression) {
1749
+ // Only `async` is allowed on object literal methods.
1750
+ const modifiers = getModifiers(node);
1751
+ if (
1752
+ modifiers !== undefined &&
1753
+ !(modifiers.nodes.length === 1 && modifiers.nodes[0]!.kind === SyntaxKind.AsyncKeyword)
1754
+ )
1755
+ return grammarErrorOnFirstToken(g, node, Diagnostics.Modifiers_cannot_appear_here);
1756
+ const postfixToken = getPostfixToken(node);
1757
+ if (checkGrammarForInvalidQuestionMark(g, postfixToken, Diagnostics.An_object_member_cannot_be_declared_optional))
1758
+ return true;
1759
+ if (
1760
+ checkGrammarForInvalidExclamationToken(
1761
+ g,
1762
+ postfixToken,
1763
+ Diagnostics.A_definite_assignment_assertion_is_not_permitted_in_this_context,
1764
+ )
1765
+ )
1766
+ return true;
1767
+ if (getBody(node) === undefined) return grammarErrorAtPos(g, node.end - 1, 1, Diagnostics.X_0_expected, '{');
1768
+ }
1769
+ if (checkGrammarForGenerator(g, node)) return true;
1770
+ }
1771
+ const name = getName(node)!;
1772
+ if (isClassLike(parent)) {
1773
+ // Computed names are disallowed in ambient contexts for properties and accessors too, but
1774
+ // those report other errors first.
1775
+ if ((node.flags & NodeFlags.Ambient) !== 0)
1776
+ return checkGrammarForInvalidDynamicName(
1777
+ g,
1778
+ name,
1779
+ Diagnostics.A_computed_property_name_in_an_ambient_context_must_refer_to_an_expression_whose_type_is_a_literal_type_or_a_unique_symbol_type,
1780
+ );
1781
+ if (node.kind === SyntaxKind.MethodDeclaration && getBody(node) === undefined)
1782
+ return checkGrammarForInvalidDynamicName(
1783
+ g,
1784
+ name,
1785
+ Diagnostics.A_computed_property_name_in_a_method_overload_must_refer_to_an_expression_whose_type_is_a_literal_type_or_a_unique_symbol_type,
1786
+ );
1787
+ } else if (parent.kind === SyntaxKind.InterfaceDeclaration) {
1788
+ return checkGrammarForInvalidDynamicName(
1789
+ g,
1790
+ name,
1791
+ Diagnostics.A_computed_property_name_in_an_interface_must_refer_to_an_expression_whose_type_is_a_literal_type_or_a_unique_symbol_type,
1792
+ );
1793
+ } else if (parent.kind === SyntaxKind.TypeLiteral) {
1794
+ return checkGrammarForInvalidDynamicName(
1795
+ g,
1796
+ name,
1797
+ Diagnostics.A_computed_property_name_in_a_type_literal_must_refer_to_an_expression_whose_type_is_a_literal_type_or_a_unique_symbol_type,
1798
+ );
1799
+ }
1800
+ return false;
1801
+ }
1802
+
1803
+ function checkGrammarConstructorTypeParameters(g: GrammarChecker, node: ConstructorDeclaration): boolean {
1804
+ const range = node.typeParameters;
1805
+ if (range === undefined) return false;
1806
+ const pos = range.pos === range.end ? range.pos : skipTrivia(g.file.text, range.pos);
1807
+ return grammarErrorAtPos(
1808
+ g,
1809
+ pos,
1810
+ range.end - pos,
1811
+ Diagnostics.Type_parameters_cannot_appear_on_a_constructor_declaration,
1812
+ );
1813
+ }
1814
+
1815
+ function checkGrammarAccessorDeclaration(g: GrammarChecker, node: Node): void {
1816
+ const name = getName(node)!;
1817
+ if (!checkGrammarFunctionLikeDeclaration(g, node) && !checkGrammarAccessor(g, node))
1818
+ checkGrammarComputedPropertyName(g, name);
1819
+ checkGrammarDecorators(g, node);
1820
+ }
1821
+
1822
+ function checkGrammarAccessor(g: GrammarChecker, accessor: Node): boolean {
1823
+ const body = getBody(accessor);
1824
+ const parent = accessor.parent!;
1825
+ const inTypeMember = parent.kind === SyntaxKind.TypeLiteral || parent.kind === SyntaxKind.InterfaceDeclaration;
1826
+ const isAbstract = hasSyntacticModifier(accessor, ModifierFlags.Abstract);
1827
+ if ((accessor.flags & NodeFlags.Ambient) === 0 && !inTypeMember && body === undefined && !isAbstract)
1828
+ return grammarErrorAtPos(g, accessor.end - 1, 1, Diagnostics.X_0_expected, '{');
1829
+ if (body !== undefined) {
1830
+ if (isAbstract)
1831
+ return grammarErrorOnNode(g, accessor, Diagnostics.An_abstract_accessor_cannot_have_an_implementation);
1832
+ if (inTypeMember)
1833
+ return grammarErrorOnNode(g, body, Diagnostics.An_implementation_cannot_be_declared_in_ambient_contexts);
1834
+ }
1835
+ const name = getName(accessor)!;
1836
+ if (getTypeParameters(accessor) !== undefined)
1837
+ return grammarErrorOnNode(g, name, Diagnostics.An_accessor_cannot_have_type_parameters);
1838
+ const parameters = getParameters(accessor)!.nodes;
1839
+ const isGetter = accessor.kind === SyntaxKind.GetAccessor;
1840
+ // A `this` parameter only counts with the right number of other parameters.
1841
+ const hasThisParameter = parameters.length === (isGetter ? 1 : 2) && isThisParameter(parameters[0]!);
1842
+ if (!hasThisParameter && parameters.length !== (isGetter ? 0 : 1))
1843
+ return grammarErrorOnNode(
1844
+ g,
1845
+ name,
1846
+ isGetter
1847
+ ? Diagnostics.A_get_accessor_cannot_have_parameters
1848
+ : Diagnostics.A_set_accessor_must_have_exactly_one_parameter,
1849
+ );
1850
+ if (!isGetter) {
1851
+ if (getType(accessor) !== undefined)
1852
+ return grammarErrorOnNode(g, name, Diagnostics.A_set_accessor_cannot_have_a_return_type_annotation);
1853
+ const parameter = parameters[hasThisParameter ? 1 : 0]! as ParameterDeclaration;
1854
+ if (parameter.dotDotDotToken !== undefined)
1855
+ return grammarErrorOnNode(g, parameter.dotDotDotToken, Diagnostics.A_set_accessor_cannot_have_rest_parameter);
1856
+ if (parameter.questionToken !== undefined)
1857
+ return grammarErrorOnNode(
1858
+ g,
1859
+ parameter.questionToken,
1860
+ Diagnostics.A_set_accessor_cannot_have_an_optional_parameter,
1861
+ );
1862
+ if (parameter.initializer !== undefined)
1863
+ return grammarErrorOnNode(g, name, Diagnostics.A_set_accessor_parameter_cannot_have_an_initializer);
1864
+ }
1865
+ return false;
1866
+ }
1867
+
1868
+ function checkGrammarPropertyDeclaration(g: GrammarChecker, node: Node): void {
1869
+ if (!checkGrammarModifiers(g, node) && !checkGrammarProperty(g, node))
1870
+ checkGrammarComputedPropertyName(g, getName(node)!);
1871
+ }
1872
+
1873
+ function checkGrammarProperty(g: GrammarChecker, node: Node): boolean {
1874
+ const name = getName(node)!;
1875
+ const parent = node.parent!;
1876
+ if (name.kind === SyntaxKind.ComputedPropertyName) {
1877
+ const expression = (name as ComputedPropertyName).expression;
1878
+ if (
1879
+ expression.kind === SyntaxKind.BinaryExpression &&
1880
+ (expression as BinaryExpression).operatorToken.kind === SyntaxKind.InKeyword
1881
+ )
1882
+ return grammarErrorOnNode(
1883
+ g,
1884
+ getMembers(parent)!.nodes[0]!,
1885
+ Diagnostics.A_mapped_type_may_not_declare_properties_or_methods,
1886
+ );
1887
+ }
1888
+ if (isClassLike(parent)) {
1889
+ if (name instanceof StringLiteral && name.text === 'constructor')
1890
+ return grammarErrorOnNode(g, name, Diagnostics.Classes_may_not_have_a_field_named_constructor);
1891
+ if (
1892
+ checkGrammarForInvalidDynamicName(
1893
+ g,
1894
+ name,
1895
+ Diagnostics.A_computed_property_name_in_a_class_property_declaration_must_have_a_simple_literal_type_or_a_unique_symbol_type,
1896
+ )
1897
+ )
1898
+ return true;
1899
+ if (
1900
+ isAutoAccessorPropertyDeclaration(node) &&
1901
+ checkGrammarForInvalidQuestionMark(
1902
+ g,
1903
+ getPostfixToken(node),
1904
+ Diagnostics.An_accessor_property_cannot_be_declared_optional,
1905
+ )
1906
+ )
1907
+ return true;
1908
+ } else if (parent.kind === SyntaxKind.InterfaceDeclaration) {
1909
+ if (
1910
+ checkGrammarForInvalidDynamicName(
1911
+ g,
1912
+ name,
1913
+ Diagnostics.A_computed_property_name_in_an_interface_must_refer_to_an_expression_whose_type_is_a_literal_type_or_a_unique_symbol_type,
1914
+ )
1915
+ )
1916
+ return true;
1917
+ const initializer = getInitializer(node);
1918
+ if (initializer !== undefined)
1919
+ return grammarErrorOnNode(g, initializer, Diagnostics.An_interface_property_cannot_have_an_initializer);
1920
+ } else if (parent.kind === SyntaxKind.TypeLiteral) {
1921
+ if (
1922
+ checkGrammarForInvalidDynamicName(
1923
+ g,
1924
+ name,
1925
+ Diagnostics.A_computed_property_name_in_a_type_literal_must_refer_to_an_expression_whose_type_is_a_literal_type_or_a_unique_symbol_type,
1926
+ )
1927
+ )
1928
+ return true;
1929
+ const initializer = getInitializer(node);
1930
+ if (initializer !== undefined)
1931
+ return grammarErrorOnNode(g, initializer, Diagnostics.A_type_literal_property_cannot_have_an_initializer);
1932
+ }
1933
+ if ((node.flags & NodeFlags.Ambient) !== 0) checkAmbientInitializer(g, node);
1934
+ if (node.kind === SyntaxKind.PropertyDeclaration) {
1935
+ const property = node as PropertyDeclaration;
1936
+ const postfixToken = property.postfixToken;
1937
+ if (postfixToken !== undefined && postfixToken.kind === SyntaxKind.ExclamationToken) {
1938
+ if (property.initializer !== undefined)
1939
+ return grammarErrorOnNode(
1940
+ g,
1941
+ postfixToken,
1942
+ Diagnostics.Declarations_with_initializers_cannot_also_have_definite_assignment_assertions,
1943
+ );
1944
+ if (property.type === undefined)
1945
+ return grammarErrorOnNode(
1946
+ g,
1947
+ postfixToken,
1948
+ Diagnostics.Declarations_with_definite_assignment_assertions_must_also_have_type_annotations,
1949
+ );
1950
+ if (
1951
+ !isClassLike(parent) ||
1952
+ (node.flags & NodeFlags.Ambient) !== 0 ||
1953
+ isStatic(node) ||
1954
+ hasSyntacticModifier(node, ModifierFlags.Abstract)
1955
+ )
1956
+ return grammarErrorOnNode(
1957
+ g,
1958
+ postfixToken,
1959
+ Diagnostics.A_definite_assignment_assertion_is_not_permitted_in_this_context,
1960
+ );
1961
+ }
1962
+ }
1963
+ return false;
1964
+ }
1965
+
1966
+ function checkAmbientInitializer(g: GrammarChecker, node: Node): boolean {
1967
+ const initializer = getInitializer(node);
1968
+ if (initializer === undefined) return false;
1969
+ const isInvalidInitializer = !(
1970
+ isInitializerStringOrNumberLiteralExpression(initializer) ||
1971
+ isInitializerSimpleLiteralEnumReference(g.c, initializer) ||
1972
+ initializer.kind === SyntaxKind.TrueKeyword ||
1973
+ initializer.kind === SyntaxKind.FalseKeyword ||
1974
+ isInitializerBigIntLiteralExpression(initializer)
1975
+ );
1976
+ const isConstOrReadonly =
1977
+ isDeclarationReadonly(node) || (node.kind === SyntaxKind.VariableDeclaration && isVarConstLike(node));
1978
+ if (!isConstOrReadonly || getType(node) !== undefined)
1979
+ return grammarErrorOnNode(g, initializer, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts);
1980
+ if (isInvalidInitializer)
1981
+ return grammarErrorOnNode(
1982
+ g,
1983
+ initializer,
1984
+ Diagnostics.A_const_initializer_in_an_ambient_context_must_be_a_string_or_numeric_literal_or_literal_enum_reference,
1985
+ );
1986
+ return false;
1987
+ }
1988
+
1989
+ function isDeclarationReadonly(node: Node): boolean {
1990
+ return (
1991
+ (getCombinedModifierFlags(node) & ModifierFlags.Readonly) !== 0 &&
1992
+ !(node.kind === SyntaxKind.Parameter && node.parent!.kind === SyntaxKind.Constructor)
1993
+ );
1994
+ }
1995
+
1996
+ function isVarConstLike(node: Node): boolean {
1997
+ const blockScopeKind = getCombinedNodeFlags(node) & NodeFlags.BlockScoped;
1998
+ return (
1999
+ blockScopeKind === NodeFlags.Const || blockScopeKind === NodeFlags.Using || blockScopeKind === NodeFlags.AwaitUsing
2000
+ );
2001
+ }
2002
+
2003
+ function isInitializerStringOrNumberLiteralExpression(expression: Node): boolean {
2004
+ switch (expression.kind) {
2005
+ case SyntaxKind.StringLiteral:
2006
+ case SyntaxKind.NoSubstitutionTemplateLiteral:
2007
+ case SyntaxKind.NumericLiteral:
2008
+ return true;
2009
+ case SyntaxKind.PrefixUnaryExpression: {
2010
+ const unary = expression as PrefixUnaryExpression;
2011
+ return unary.operator === SyntaxKind.MinusToken && unary.operand.kind === SyntaxKind.NumericLiteral;
2012
+ }
2013
+ }
2014
+ return false;
2015
+ }
2016
+
2017
+ function isInitializerBigIntLiteralExpression(expression: Node): boolean {
2018
+ if (expression.kind === SyntaxKind.BigIntLiteral) return true;
2019
+ if (expression.kind !== SyntaxKind.PrefixUnaryExpression) return false;
2020
+ const unary = expression as PrefixUnaryExpression;
2021
+ return unary.operator === SyntaxKind.MinusToken && unary.operand.kind === SyntaxKind.BigIntLiteral;
2022
+ }
2023
+
2024
+ function isInitializerSimpleLiteralEnumReference(c: Checker, expression: Node): boolean {
2025
+ if (expression.kind === SyntaxKind.PropertyAccessExpression)
2026
+ return (checkExpressionCached(c, expression).flags & TypeFlags.EnumLike) !== 0;
2027
+ if (expression instanceof ElementAccessExpression)
2028
+ return (
2029
+ isInitializerStringOrNumberLiteralExpression(expression.argumentExpression) &&
2030
+ isEntityNameExpression(expression.expression, false) &&
2031
+ (checkExpressionCached(c, expression).flags & TypeFlags.EnumLike) !== 0
2032
+ );
2033
+ return false;
2034
+ }
2035
+
2036
+ // ---- Statements
2037
+
2038
+ function checkGrammarStatementInAmbientContext(g: GrammarChecker, node: Node): boolean {
2039
+ if ((node.flags & NodeFlags.Ambient) === 0) return false;
2040
+ const parent = node.parent!;
2041
+ const id = getNodeId(node);
2042
+ if (!g.reportedAmbientStatements.has(id) && (isFunctionLike(parent) || isAccessor(parent))) {
2043
+ const reported = grammarErrorOnFirstToken(
2044
+ g,
2045
+ node,
2046
+ Diagnostics.An_implementation_cannot_be_declared_in_ambient_contexts,
2047
+ );
2048
+ if (reported) g.reportedAmbientStatements.add(id);
2049
+ return reported;
2050
+ }
2051
+ // Report once per block; statements nested in other statements are covered by their parent.
2052
+ if (
2053
+ parent.kind === SyntaxKind.Block ||
2054
+ parent.kind === SyntaxKind.ModuleBlock ||
2055
+ parent.kind === SyntaxKind.SourceFile
2056
+ ) {
2057
+ const parentId = getNodeId(parent);
2058
+ if (!g.reportedAmbientStatements.has(parentId)) {
2059
+ const reported = grammarErrorOnFirstToken(g, node, Diagnostics.Statements_are_not_allowed_in_ambient_contexts);
2060
+ if (reported) g.reportedAmbientStatements.add(parentId);
2061
+ return reported;
2062
+ }
2063
+ }
2064
+ return false;
2065
+ }
2066
+
2067
+ function isAccessor(node: Node): boolean {
2068
+ return node.kind === SyntaxKind.GetAccessor || node.kind === SyntaxKind.SetAccessor;
2069
+ }
2070
+
2071
+ function checkGrammarVariableStatement(g: GrammarChecker, node: VariableStatement): void {
2072
+ if (!checkGrammarModifiers(g, node) && !checkGrammarVariableDeclarationList(g, node.declarationList))
2073
+ checkGrammarForDisallowedBlockScopedVariableStatement(g, node);
2074
+ }
2075
+
2076
+ function checkGrammarForDisallowedBlockScopedVariableStatement(g: GrammarChecker, node: VariableStatement): void {
2077
+ if (containerAllowsBlockScopedVariable(node.parent!)) return;
2078
+ const blockScopeKind = getCombinedNodeFlags(node.declarationList) & NodeFlags.BlockScoped;
2079
+ if (blockScopeKind === 0) return;
2080
+ let keyword: string;
2081
+ if (blockScopeKind === NodeFlags.Let) keyword = 'let';
2082
+ else if (blockScopeKind === NodeFlags.Const) keyword = 'const';
2083
+ else if (blockScopeKind === NodeFlags.Using) keyword = 'using';
2084
+ else keyword = 'await using';
2085
+ g.c.error(node, Diagnostics.X_0_declarations_can_only_be_declared_inside_a_block, keyword);
2086
+ }
2087
+
2088
+ function containerAllowsBlockScopedVariable(parent: Node): boolean {
2089
+ switch (parent.kind) {
2090
+ case SyntaxKind.IfStatement:
2091
+ case SyntaxKind.DoStatement:
2092
+ case SyntaxKind.WhileStatement:
2093
+ case SyntaxKind.WithStatement:
2094
+ case SyntaxKind.ForStatement:
2095
+ case SyntaxKind.ForInStatement:
2096
+ case SyntaxKind.ForOfStatement:
2097
+ return false;
2098
+ case SyntaxKind.LabeledStatement:
2099
+ return containerAllowsBlockScopedVariable(parent.parent!);
2100
+ }
2101
+ return true;
2102
+ }
2103
+
2104
+ function checkGrammarVariableDeclarationList(g: GrammarChecker, list: VariableDeclarationList): boolean {
2105
+ const declarations = list.declarations;
2106
+ if (checkGrammarForDisallowedTrailingComma(g, declarations, Diagnostics.Trailing_comma_not_allowed)) return true;
2107
+ if (declarations.nodes.length === 0)
2108
+ return grammarErrorAtPos(
2109
+ g,
2110
+ declarations.pos,
2111
+ declarations.end - declarations.pos,
2112
+ Diagnostics.Variable_declaration_list_cannot_be_empty,
2113
+ );
2114
+ const blockScopeFlags = list.flags & NodeFlags.BlockScoped;
2115
+ const parent = list.parent!;
2116
+ if (blockScopeFlags === NodeFlags.Using || blockScopeFlags === NodeFlags.AwaitUsing) {
2117
+ const isUsing = blockScopeFlags === NodeFlags.Using;
2118
+ if (parent.kind === SyntaxKind.ForInStatement)
2119
+ return grammarErrorOnNode(
2120
+ g,
2121
+ list,
2122
+ isUsing
2123
+ ? Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_a_using_declaration
2124
+ : Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_an_await_using_declaration,
2125
+ );
2126
+ if ((list.flags & NodeFlags.Ambient) !== 0)
2127
+ return grammarErrorOnNode(
2128
+ g,
2129
+ list,
2130
+ isUsing
2131
+ ? Diagnostics.X_using_declarations_are_not_allowed_in_ambient_contexts
2132
+ : Diagnostics.X_await_using_declarations_are_not_allowed_in_ambient_contexts,
2133
+ );
2134
+ if (
2135
+ parent.kind === SyntaxKind.VariableStatement &&
2136
+ (parent.parent!.kind === SyntaxKind.CaseClause || parent.parent!.kind === SyntaxKind.DefaultClause)
2137
+ )
2138
+ return grammarErrorOnNode(
2139
+ g,
2140
+ list,
2141
+ isUsing
2142
+ ? Diagnostics.X_using_declarations_are_not_allowed_in_case_or_default_clauses_unless_contained_within_a_block
2143
+ : Diagnostics.X_await_using_declarations_are_not_allowed_in_case_or_default_clauses_unless_contained_within_a_block,
2144
+ );
2145
+ }
2146
+ if (blockScopeFlags === NodeFlags.AwaitUsing) return checkGrammarAwaitOrAwaitUsing(g, list);
2147
+ return false;
2148
+ }
2149
+
2150
+ function checkGrammarVariableDeclaration(g: GrammarChecker, node: VariableDeclaration): boolean {
2151
+ const nodeFlags = getCombinedNodeFlags(node);
2152
+ const blockScopeKind = nodeFlags & NodeFlags.BlockScoped;
2153
+ const name = node.name;
2154
+ if (isBindingPattern(name)) {
2155
+ if (blockScopeKind === NodeFlags.AwaitUsing)
2156
+ return grammarErrorOnNode(g, node, Diagnostics.X_0_declarations_may_not_have_binding_patterns, 'await using');
2157
+ if (blockScopeKind === NodeFlags.Using)
2158
+ return grammarErrorOnNode(g, node, Diagnostics.X_0_declarations_may_not_have_binding_patterns, 'using');
2159
+ }
2160
+ const statement = node.parent!.parent!;
2161
+ if (statement.kind !== SyntaxKind.ForInStatement && statement.kind !== SyntaxKind.ForOfStatement) {
2162
+ if ((nodeFlags & NodeFlags.Ambient) !== 0) checkAmbientInitializer(g, node);
2163
+ else if (node.initializer === undefined) {
2164
+ if (isBindingPattern(name) && !isBindingPattern(node.parent))
2165
+ return grammarErrorOnNode(g, node, Diagnostics.A_destructuring_declaration_must_have_an_initializer);
2166
+ if (blockScopeKind === NodeFlags.AwaitUsing)
2167
+ return grammarErrorOnNode(g, node, Diagnostics.X_0_declarations_must_be_initialized, 'await using');
2168
+ if (blockScopeKind === NodeFlags.Using)
2169
+ return grammarErrorOnNode(g, node, Diagnostics.X_0_declarations_must_be_initialized, 'using');
2170
+ if (blockScopeKind === NodeFlags.Const)
2171
+ return grammarErrorOnNode(g, node, Diagnostics.X_0_declarations_must_be_initialized, 'const');
2172
+ }
2173
+ }
2174
+ const exclamationToken = node.exclamationToken;
2175
+ if (
2176
+ exclamationToken !== undefined &&
2177
+ (statement.kind !== SyntaxKind.VariableStatement ||
2178
+ node.type === undefined ||
2179
+ node.initializer !== undefined ||
2180
+ (nodeFlags & NodeFlags.Ambient) !== 0)
2181
+ ) {
2182
+ const message =
2183
+ node.initializer !== undefined
2184
+ ? Diagnostics.Declarations_with_initializers_cannot_also_have_definite_assignment_assertions
2185
+ : node.type === undefined
2186
+ ? Diagnostics.Declarations_with_definite_assignment_assertions_must_also_have_type_annotations
2187
+ : Diagnostics.A_definite_assignment_assertion_is_not_permitted_in_this_context;
2188
+ return grammarErrorOnNode(g, exclamationToken, message);
2189
+ }
2190
+ if (
2191
+ getEmitModuleFormat(g) < ModuleKind.System &&
2192
+ (statement.flags & NodeFlags.Ambient) === 0 &&
2193
+ hasSyntacticModifier(statement, ModifierFlags.Export)
2194
+ )
2195
+ checkGrammarForEsModuleMarkerInBindingName(g, name);
2196
+ // `let` can't be a name in `let` or `const` declarations.
2197
+ return blockScopeKind !== 0 && checkGrammarNameInLetOrConstDeclarations(g, name);
2198
+ }
2199
+
2200
+ function checkGrammarForEsModuleMarkerInBindingName(g: GrammarChecker, name: Node): boolean {
2201
+ if (name instanceof Identifier)
2202
+ return (
2203
+ name.text === '__esModule' &&
2204
+ grammarErrorOnNode(
2205
+ g,
2206
+ name,
2207
+ Diagnostics.Identifier_expected_esModule_is_reserved_as_an_exported_marker_when_transforming_ECMAScript_modules,
2208
+ )
2209
+ );
2210
+ for (const element of getElements(name)!.nodes) {
2211
+ const elementName = getName(element);
2212
+ if (elementName !== undefined) return checkGrammarForEsModuleMarkerInBindingName(g, elementName);
2213
+ }
2214
+ return false;
2215
+ }
2216
+
2217
+ function checkGrammarNameInLetOrConstDeclarations(g: GrammarChecker, name: Node): boolean {
2218
+ if (name instanceof Identifier) {
2219
+ const identifier: Identifier = name;
2220
+ if (identifier.text === 'let')
2221
+ return grammarErrorOnNode(
2222
+ g,
2223
+ name,
2224
+ Diagnostics.X_let_is_not_allowed_to_be_used_as_a_name_in_let_or_const_declarations,
2225
+ );
2226
+ return false;
2227
+ }
2228
+ for (const element of getElements(name)!.nodes) {
2229
+ const elementName = getName(element);
2230
+ if (elementName !== undefined) checkGrammarNameInLetOrConstDeclarations(g, elementName);
2231
+ }
2232
+ return false;
2233
+ }
2234
+
2235
+ function checkGrammarBindingElement(g: GrammarChecker, node: BindingElement): boolean {
2236
+ if (node.dotDotDotToken !== undefined) {
2237
+ const elements = getElements(node.parent!)!;
2238
+ if (node !== elements.nodes[elements.nodes.length - 1]!)
2239
+ return grammarErrorOnNode(g, node, Diagnostics.A_rest_element_must_be_last_in_a_destructuring_pattern);
2240
+ checkGrammarForDisallowedTrailingComma(
2241
+ g,
2242
+ elements,
2243
+ Diagnostics.A_rest_parameter_or_binding_pattern_may_not_have_a_trailing_comma,
2244
+ );
2245
+ if (node.propertyName !== undefined)
2246
+ return grammarErrorOnNode(g, node.name!, Diagnostics.A_rest_element_cannot_have_a_property_name);
2247
+ // Report on the `=` before the initializer.
2248
+ if (node.initializer !== undefined)
2249
+ return grammarErrorAtPos(g, node.initializer.pos - 1, 1, Diagnostics.A_rest_element_cannot_have_an_initializer);
2250
+ }
2251
+ return false;
2252
+ }
2253
+
2254
+ function checkGrammarForInOrForOfStatement(g: GrammarChecker, node: ForInOrOfStatement): boolean {
2255
+ checkGrammarForInOrForOfStatementWorker(g, node);
2256
+ // `checkForOfStatement` reports `for await` in static blocks after the statement's grammar.
2257
+ const awaitModifier = node.awaitModifier;
2258
+ if (node.kind === SyntaxKind.ForOfStatement && awaitModifier !== undefined) {
2259
+ const container = getContainingFunctionOrClassStaticBlock(node);
2260
+ if (container !== undefined && container.kind === SyntaxKind.ClassStaticBlockDeclaration)
2261
+ grammarErrorOnNode(g, awaitModifier, Diagnostics.X_for_await_loops_cannot_be_used_inside_a_class_static_block);
2262
+ }
2263
+ return false;
2264
+ }
2265
+
2266
+ function checkGrammarForInOrForOfStatementWorker(g: GrammarChecker, node: ForInOrOfStatement): boolean {
2267
+ if (checkGrammarStatementInAmbientContext(g, node)) return true;
2268
+ const awaitModifier = node.awaitModifier;
2269
+ if (
2270
+ node.kind === SyntaxKind.ForOfStatement &&
2271
+ awaitModifier !== undefined &&
2272
+ (node.flags & NodeFlags.AwaitContext) === 0
2273
+ ) {
2274
+ if (isInTopLevelContext(node)) {
2275
+ if (!g.hasParseErrors) {
2276
+ if (!isEffectiveExternalModule(g))
2277
+ g.c.addDiagnostic(
2278
+ createDiagnosticForNode(
2279
+ awaitModifier,
2280
+ Diagnostics.X_for_await_loops_are_only_allowed_at_the_top_level_of_a_file_when_that_file_is_a_module_but_this_file_has_no_imports_or_exports_Consider_adding_an_empty_export_to_make_this_file_a_module,
2281
+ [],
2282
+ ),
2283
+ );
2284
+ const kind = checkTopLevelAwaitModule(g);
2285
+ if (kind === TopLevelAwaitSupport.CommonJS)
2286
+ g.c.addDiagnostic(
2287
+ createDiagnosticForNode(
2288
+ awaitModifier,
2289
+ Diagnostics.The_current_file_is_a_CommonJS_module_and_cannot_use_await_at_the_top_level,
2290
+ [],
2291
+ ),
2292
+ );
2293
+ else if (kind === TopLevelAwaitSupport.Unsupported)
2294
+ g.c.addDiagnostic(
2295
+ createDiagnosticForNode(
2296
+ awaitModifier,
2297
+ Diagnostics.Top_level_for_await_loops_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_node16_node18_node20_nodenext_or_preserve_and_the_target_option_is_set_to_es2017_or_higher,
2298
+ [],
2299
+ ),
2300
+ );
2301
+ }
2302
+ } else if (!g.hasParseErrors) {
2303
+ // `for await` in a function that isn't async.
2304
+ const diagnostic = createDiagnosticForNode(
2305
+ awaitModifier,
2306
+ Diagnostics.X_for_await_loops_are_only_allowed_within_async_functions_and_at_the_top_levels_of_modules,
2307
+ [],
2308
+ );
2309
+ const containingFunction = getContainingFunction(node);
2310
+ if (containingFunction !== undefined && containingFunction.kind !== SyntaxKind.Constructor)
2311
+ addRelated(diagnostic, containingFunction, Diagnostics.Did_you_mean_to_mark_this_function_as_async);
2312
+ g.c.addDiagnostic(diagnostic);
2313
+ return true;
2314
+ }
2315
+ }
2316
+ const initializer = node.initializer;
2317
+ if (
2318
+ node.kind === SyntaxKind.ForOfStatement &&
2319
+ (node.flags & NodeFlags.AwaitContext) === 0 &&
2320
+ initializer instanceof Identifier &&
2321
+ initializer.text === 'async'
2322
+ ) {
2323
+ grammarErrorOnNode(g, initializer, Diagnostics.The_left_hand_side_of_a_for_of_statement_may_not_be_async);
2324
+ return false;
2325
+ }
2326
+ if (initializer.kind !== SyntaxKind.VariableDeclarationList) return false;
2327
+ const list = initializer as VariableDeclarationList;
2328
+ if (checkGrammarVariableDeclarationList(g, list)) return false;
2329
+ // Declarations can be empty after a parse error, as in `for (let of x)`.
2330
+ const declarations = list.declarations.nodes;
2331
+ if (declarations.length === 0) return false;
2332
+ const isForIn = node.kind === SyntaxKind.ForInStatement;
2333
+ if (declarations.length > 1)
2334
+ return grammarErrorOnFirstToken(
2335
+ g,
2336
+ declarations[1]!,
2337
+ isForIn
2338
+ ? Diagnostics.Only_a_single_variable_declaration_is_allowed_in_a_for_in_statement
2339
+ : Diagnostics.Only_a_single_variable_declaration_is_allowed_in_a_for_of_statement,
2340
+ );
2341
+ const first = declarations[0]! as VariableDeclaration;
2342
+ if (first.initializer !== undefined)
2343
+ return grammarErrorOnNode(
2344
+ g,
2345
+ first.name,
2346
+ isForIn
2347
+ ? Diagnostics.The_variable_declaration_of_a_for_in_statement_cannot_have_an_initializer
2348
+ : Diagnostics.The_variable_declaration_of_a_for_of_statement_cannot_have_an_initializer,
2349
+ );
2350
+ if (first.type !== undefined)
2351
+ return grammarErrorOnNode(
2352
+ g,
2353
+ first,
2354
+ isForIn
2355
+ ? Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_use_a_type_annotation
2356
+ : Diagnostics.The_left_hand_side_of_a_for_of_statement_cannot_use_a_type_annotation,
2357
+ );
2358
+ return false;
2359
+ }
2360
+
2361
+ const enum TopLevelAwaitSupport {
2362
+ Supported,
2363
+ CommonJS,
2364
+ Unsupported,
2365
+ }
2366
+
2367
+ /** Whether the module and target options allow top-level `await`. */
2368
+ function checkTopLevelAwaitModule(g: GrammarChecker): TopLevelAwaitSupport {
2369
+ const moduleKind = g.moduleKind;
2370
+ if (moduleKind >= ModuleKind.Node16 && moduleKind <= ModuleKind.NodeNext) {
2371
+ if (g.metadata.impliedNodeFormat === ModuleKind.CommonJS) return TopLevelAwaitSupport.CommonJS;
2372
+ } else if (
2373
+ moduleKind !== ModuleKind.ES2022 &&
2374
+ moduleKind !== ModuleKind.ESNext &&
2375
+ moduleKind !== ModuleKind.Preserve &&
2376
+ moduleKind !== ModuleKind.System
2377
+ )
2378
+ return TopLevelAwaitSupport.Unsupported;
2379
+ return g.c.languageVersion >= ScriptTarget.ES2017 ? TopLevelAwaitSupport.Supported : TopLevelAwaitSupport.Unsupported;
2380
+ }
2381
+
2382
+ function checkGrammarBreakOrContinueStatement(g: GrammarChecker, node: Node): boolean {
2383
+ const targetLabel = getLabel(node);
2384
+ const isBreak = node.kind === SyntaxKind.BreakStatement;
2385
+ for (let current: Node | undefined = node; current !== undefined; current = current.parent) {
2386
+ if (isFunctionLike(current) || current.kind === SyntaxKind.ClassStaticBlockDeclaration)
2387
+ return grammarErrorOnNode(g, node, Diagnostics.Jump_target_cannot_cross_function_boundary);
2388
+ if (current.kind === SyntaxKind.LabeledStatement) {
2389
+ const labeled = current as LabeledStatement;
2390
+ if (targetLabel !== undefined && labeled.label.text === getText(targetLabel)) {
2391
+ // `continue` can only target labels of iteration statements.
2392
+ if (!isBreak && !isIterationStatement(labeled.statement, true))
2393
+ return grammarErrorOnNode(
2394
+ g,
2395
+ node,
2396
+ Diagnostics.A_continue_statement_can_only_jump_to_a_label_of_an_enclosing_iteration_statement,
2397
+ );
2398
+ return false;
2399
+ }
2400
+ } else if (current.kind === SyntaxKind.SwitchStatement) {
2401
+ if (isBreak && targetLabel === undefined) return false;
2402
+ } else if (isIterationStatement(current, false) && targetLabel === undefined) {
2403
+ return false;
2404
+ }
2405
+ }
2406
+ if (targetLabel !== undefined)
2407
+ return grammarErrorOnNode(
2408
+ g,
2409
+ node,
2410
+ isBreak
2411
+ ? Diagnostics.A_break_statement_can_only_jump_to_a_label_of_an_enclosing_statement
2412
+ : Diagnostics.A_continue_statement_can_only_jump_to_a_label_of_an_enclosing_iteration_statement,
2413
+ );
2414
+ return grammarErrorOnNode(
2415
+ g,
2416
+ node,
2417
+ isBreak
2418
+ ? Diagnostics.A_break_statement_can_only_be_used_within_an_enclosing_iteration_or_switch_statement
2419
+ : Diagnostics.A_continue_statement_can_only_be_used_within_an_enclosing_iteration_statement,
2420
+ );
2421
+ }
2422
+
2423
+ function isIterationStatement(node: Node, lookInLabeledStatements: boolean): boolean {
2424
+ switch (node.kind) {
2425
+ case SyntaxKind.ForStatement:
2426
+ case SyntaxKind.ForInStatement:
2427
+ case SyntaxKind.ForOfStatement:
2428
+ case SyntaxKind.DoStatement:
2429
+ case SyntaxKind.WhileStatement:
2430
+ return true;
2431
+ case SyntaxKind.LabeledStatement:
2432
+ return lookInLabeledStatements && isIterationStatement((node as LabeledStatement).statement, true);
2433
+ }
2434
+ return false;
2435
+ }
2436
+
2437
+ /** Returns whether the statement's expression is checked. */
2438
+ function checkGrammarReturnStatement(g: GrammarChecker, node: Node): boolean {
2439
+ if (checkGrammarStatementInAmbientContext(g, node)) return false;
2440
+ const container = getContainingFunctionOrClassStaticBlock(node);
2441
+ if (container !== undefined && container.kind === SyntaxKind.ClassStaticBlockDeclaration) {
2442
+ grammarErrorOnFirstToken(g, node, Diagnostics.A_return_statement_cannot_be_used_inside_a_class_static_block);
2443
+ return false;
2444
+ }
2445
+ if (container === undefined) {
2446
+ grammarErrorOnFirstToken(g, node, Diagnostics.A_return_statement_can_only_be_used_within_a_function_body);
2447
+ return false;
2448
+ }
2449
+ return true;
2450
+ }
2451
+
2452
+ function checkGrammarWithStatement(g: GrammarChecker, node: WithStatement): void {
2453
+ if (!checkGrammarStatementInAmbientContext(g, node) && (node.flags & NodeFlags.AwaitContext) !== 0)
2454
+ grammarErrorOnFirstToken(g, node, Diagnostics.X_with_statements_are_not_allowed_in_an_async_function_block);
2455
+ // The body of a `with` statement isn't checked.
2456
+ g.visitNode(node.expression);
2457
+ const start = skipTrivia(g.file.text, node.pos);
2458
+ grammarErrorAtPos(
2459
+ g,
2460
+ start,
2461
+ node.statement.pos - start,
2462
+ Diagnostics.The_with_statement_is_not_supported_All_symbols_in_a_with_block_will_have_type_any,
2463
+ );
2464
+ }
2465
+
2466
+ function checkGrammarSwitchStatement(g: GrammarChecker, node: SwitchStatement): void {
2467
+ checkGrammarStatementInAmbientContext(g, node);
2468
+ let seenDefaultClause = false;
2469
+ for (const clause of node.caseBlock.clauses.nodes) {
2470
+ if (clause.kind !== SyntaxKind.DefaultClause) continue;
2471
+ if (!seenDefaultClause) seenDefaultClause = true;
2472
+ else {
2473
+ grammarErrorOnNode(g, clause, Diagnostics.A_default_clause_cannot_appear_more_than_once_in_a_switch_statement);
2474
+ return;
2475
+ }
2476
+ }
2477
+ }
2478
+
2479
+ function checkGrammarLabeledStatement(g: GrammarChecker, node: LabeledStatement): void {
2480
+ if (checkGrammarStatementInAmbientContext(g, node)) return;
2481
+ const label = node.label;
2482
+ for (let current = node.parent; current !== undefined && !isFunctionLike(current); current = current.parent)
2483
+ if (current.kind === SyntaxKind.LabeledStatement && (current as LabeledStatement).label.text === label.text) {
2484
+ grammarErrorOnNode(g, label, Diagnostics.Duplicate_label_0, label.text);
2485
+ return;
2486
+ }
2487
+ }
2488
+
2489
+ function checkGrammarCatchClause(g: GrammarChecker, node: CatchClause): void {
2490
+ const declaration = node.variableDeclaration;
2491
+ if (declaration === undefined) return;
2492
+ const typeNode = declaration.type;
2493
+ if (typeNode !== undefined) {
2494
+ const t = getTypeFromTypeNode(g.c, typeNode);
2495
+ if ((t.flags & TypeFlags.AnyOrUnknown) === 0)
2496
+ grammarErrorOnFirstToken(
2497
+ g,
2498
+ typeNode,
2499
+ Diagnostics.Catch_clause_variable_type_annotation_must_be_any_or_unknown_if_specified,
2500
+ );
2501
+ } else if (declaration.initializer !== undefined) {
2502
+ grammarErrorOnFirstToken(g, declaration.initializer, Diagnostics.Catch_clause_variable_cannot_have_an_initializer);
2503
+ } else {
2504
+ const blockLocals = getLocals(node.block);
2505
+ const locals = getLocals(node);
2506
+ if (blockLocals === undefined || locals === undefined) return;
2507
+ for (const caughtName of locals.keys()) {
2508
+ const blockLocal = blockLocals.get(caughtName);
2509
+ if (
2510
+ blockLocal !== undefined &&
2511
+ blockLocal.valueDeclaration !== undefined &&
2512
+ (blockLocal.flags & SymbolFlags.BlockScopedVariable) !== 0
2513
+ )
2514
+ grammarErrorOnNode(
2515
+ g,
2516
+ blockLocal.valueDeclaration,
2517
+ Diagnostics.Cannot_redeclare_identifier_0_in_catch_clause,
2518
+ caughtName,
2519
+ );
2520
+ }
2521
+ }
2522
+ }
2523
+
2524
+ function getContainingFunction(node: Node): Node | undefined {
2525
+ let current = node.parent;
2526
+ while (current !== undefined && !isFunctionLike(current)) current = current.parent;
2527
+ return current;
2528
+ }
2529
+
2530
+ export function getContainingFunctionOrClassStaticBlock(node: Node): Node | undefined {
2531
+ let current = node.parent;
2532
+ while (current !== undefined && !isFunctionLike(current) && current.kind !== SyntaxKind.ClassStaticBlockDeclaration)
2533
+ current = current.parent;
2534
+ return current;
2535
+ }
2536
+
2537
+ // ---- Modules, imports and exports
2538
+
2539
+ function checkGrammarTopLevelElementsForRequiredDeclareModifier(g: GrammarChecker, file: SourceFile): boolean {
2540
+ for (const statement of file.statements.nodes) {
2541
+ if (!isDeclarationNode(statement) && statement.kind !== SyntaxKind.VariableStatement) continue;
2542
+ // Everything but interfaces, type aliases, imports and exports needs `declare` or `export`.
2543
+ switch (statement.kind) {
2544
+ case SyntaxKind.InterfaceDeclaration:
2545
+ case SyntaxKind.TypeAliasDeclaration:
2546
+ case SyntaxKind.ImportDeclaration:
2547
+ case SyntaxKind.JSImportDeclaration:
2548
+ case SyntaxKind.ImportEqualsDeclaration:
2549
+ case SyntaxKind.ExportDeclaration:
2550
+ case SyntaxKind.ExportAssignment:
2551
+ case SyntaxKind.NamespaceExportDeclaration:
2552
+ continue;
2553
+ }
2554
+ if (hasSyntacticModifier(statement, ModifierFlags.Ambient | ModifierFlags.Export | ModifierFlags.Default)) continue;
2555
+ if (
2556
+ grammarErrorOnFirstToken(
2557
+ g,
2558
+ statement,
2559
+ Diagnostics.Top_level_declarations_in_d_ts_files_must_start_with_either_a_declare_or_export_modifier,
2560
+ )
2561
+ )
2562
+ return true;
2563
+ }
2564
+ return false;
2565
+ }
2566
+
2567
+ function checkGrammarModuleElementContext(g: GrammarChecker, node: Node, message: DiagnosticMessage): boolean {
2568
+ const parent = node.parent!;
2569
+ const isInAppropriateContext =
2570
+ parent.kind === SyntaxKind.SourceFile ||
2571
+ parent.kind === SyntaxKind.ModuleBlock ||
2572
+ parent.kind === SyntaxKind.ModuleDeclaration;
2573
+ if (!isInAppropriateContext) grammarErrorOnFirstToken(g, node, message);
2574
+ return !isInAppropriateContext;
2575
+ }
2576
+
2577
+ function checkGrammarModuleDeclaration(g: GrammarChecker, node: ModuleDeclaration): boolean {
2578
+ // The declaration check reports an illegal context and skips the rest.
2579
+ const parent = node.parent!.kind;
2580
+ if (parent !== SyntaxKind.SourceFile && parent !== SyntaxKind.ModuleBlock && parent !== SyntaxKind.ModuleDeclaration)
2581
+ return true;
2582
+ if (
2583
+ !checkGrammarModifiers(g, node) &&
2584
+ (node.flags & NodeFlags.Ambient) === 0 &&
2585
+ node.name.kind === SyntaxKind.StringLiteral
2586
+ )
2587
+ grammarErrorOnNode(g, node.name, Diagnostics.Only_ambient_modules_can_use_quoted_names);
2588
+ return true;
2589
+ }
2590
+
2591
+ /** Checks an import or export with a module specifier; reports and returns `false` if it is invalid. */
2592
+ function checkExternalImportOrExportDeclaration(g: GrammarChecker, node: Node): boolean {
2593
+ const moduleName = getExternalModuleName(node);
2594
+ // A missing name is a parse error.
2595
+ if (moduleName === undefined || moduleName.pos === moduleName.end) return false;
2596
+ if (moduleName.kind !== SyntaxKind.StringLiteral) {
2597
+ g.c.error(moduleName, Diagnostics.String_literal_expected);
2598
+ return false;
2599
+ }
2600
+ const parent = node.parent!;
2601
+ const inAmbientExternalModule = parent.kind === SyntaxKind.ModuleBlock && isAmbientModule(parent.parent!);
2602
+ if (parent.kind !== SyntaxKind.SourceFile && !inAmbientExternalModule) {
2603
+ g.c.error(
2604
+ moduleName,
2605
+ node.kind === SyntaxKind.ExportDeclaration
2606
+ ? Diagnostics.Export_declarations_are_not_permitted_in_a_namespace
2607
+ : Diagnostics.Import_declarations_in_a_namespace_cannot_reference_a_module,
2608
+ );
2609
+ return false;
2610
+ }
2611
+ if (
2612
+ inAmbientExternalModule &&
2613
+ isExternalModuleNameRelative((moduleName as StringLiteral).text) &&
2614
+ !isModuleAugmentationExternal(parent.parent!)
2615
+ ) {
2616
+ // Module augmentations report relative names when checking the augmentation.
2617
+ g.c.error(
2618
+ node,
2619
+ Diagnostics.Import_or_export_declaration_in_an_ambient_module_declaration_cannot_reference_module_through_relative_module_name,
2620
+ );
2621
+ return false;
2622
+ }
2623
+ if (node.kind === SyntaxKind.ImportEqualsDeclaration) return true;
2624
+ const attributes = getImportAttributes(node);
2625
+ if (attributes === undefined) return true;
2626
+ let hasError = false;
2627
+ for (const attribute of attributes.attributes.nodes) {
2628
+ const value = (attribute as ImportAttribute).value;
2629
+ if (value.kind !== SyntaxKind.StringLiteral) {
2630
+ hasError = true;
2631
+ g.c.error(value, Diagnostics.Import_attribute_values_must_be_string_literal_expressions);
2632
+ }
2633
+ }
2634
+ return !hasError;
2635
+ }
2636
+
2637
+ function getImportAttributes(node: Node): ImportAttributes | undefined {
2638
+ if (node.kind === SyntaxKind.ImportDeclaration || node.kind === SyntaxKind.JSImportDeclaration)
2639
+ return (node as ImportDeclaration).attributes;
2640
+ if (node.kind === SyntaxKind.ExportDeclaration) return (node as ExportDeclaration).attributes;
2641
+ return undefined;
2642
+ }
2643
+
2644
+ function checkGrammarImportDeclaration(g: GrammarChecker, node: ImportDeclaration): void {
2645
+ if (
2646
+ checkGrammarModuleElementContext(
2647
+ g,
2648
+ node,
2649
+ isInJSFile(node)
2650
+ ? Diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_module
2651
+ : Diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_namespace_or_module,
2652
+ )
2653
+ )
2654
+ return;
2655
+ if (!checkGrammarModifiers(g, node) && node.modifiers !== undefined)
2656
+ grammarErrorOnFirstToken(g, node, Diagnostics.An_import_declaration_cannot_have_modifiers);
2657
+ const clause = node.importClause;
2658
+ if (checkExternalImportOrExportDeclaration(g, node) && clause !== undefined) checkGrammarImportClause(g, clause);
2659
+ checkGrammarImportAttributes(g, node);
2660
+ }
2661
+
2662
+ function checkGrammarImportClause(g: GrammarChecker, node: ImportClause): boolean {
2663
+ const namedBindings = node.namedBindings;
2664
+ if (node.phaseModifier === SyntaxKind.TypeKeyword) {
2665
+ if ((node.flags & NodeFlags.JSDoc) === 0 && node.name !== undefined && namedBindings !== undefined)
2666
+ return grammarErrorOnNode(
2667
+ g,
2668
+ node,
2669
+ Diagnostics.A_type_only_import_can_specify_a_default_import_or_named_bindings_but_not_both,
2670
+ );
2671
+ if (namedBindings !== undefined && namedBindings.kind === SyntaxKind.NamedImports)
2672
+ return checkGrammarTypeOnlyNamedImportsOrExports(g, namedBindings);
2673
+ } else if (node.phaseModifier === SyntaxKind.DeferKeyword) {
2674
+ if (node.name !== undefined)
2675
+ return grammarErrorOnNode(g, node, Diagnostics.Default_imports_are_not_allowed_in_a_deferred_import);
2676
+ if (namedBindings !== undefined && namedBindings.kind === SyntaxKind.NamedImports)
2677
+ return grammarErrorOnNode(g, node, Diagnostics.Named_imports_are_not_allowed_in_a_deferred_import);
2678
+ if (g.moduleKind !== ModuleKind.ESNext && g.moduleKind !== ModuleKind.Preserve)
2679
+ return grammarErrorOnNode(
2680
+ g,
2681
+ node,
2682
+ Diagnostics.Deferred_imports_are_only_supported_when_the_module_flag_is_set_to_esnext_or_preserve,
2683
+ );
2684
+ }
2685
+ return false;
2686
+ }
2687
+
2688
+ function checkGrammarTypeOnlyNamedImportsOrExports(g: GrammarChecker, namedBindings: Node): boolean {
2689
+ const isImport = namedBindings.kind === SyntaxKind.NamedImports;
2690
+ for (const specifier of getElements(namedBindings)!.nodes)
2691
+ if (getIsTypeOnly(specifier))
2692
+ return grammarErrorOnFirstToken(
2693
+ g,
2694
+ specifier,
2695
+ isImport
2696
+ ? Diagnostics.The_type_modifier_cannot_be_used_on_a_named_import_when_import_type_is_used_on_its_import_statement
2697
+ : Diagnostics.The_type_modifier_cannot_be_used_on_a_named_export_when_export_type_is_used_on_its_export_statement,
2698
+ );
2699
+ return false;
2700
+ }
2701
+
2702
+ function checkGrammarImportEqualsDeclaration(g: GrammarChecker, node: ImportEqualsDeclaration): void {
2703
+ if (
2704
+ checkGrammarModuleElementContext(
2705
+ g,
2706
+ node,
2707
+ isInJSFile(node)
2708
+ ? Diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_module
2709
+ : Diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_namespace_or_module,
2710
+ )
2711
+ )
2712
+ return;
2713
+ checkGrammarModifiers(g, node);
2714
+ const isExternal = node.moduleReference.kind === SyntaxKind.ExternalModuleReference;
2715
+ if (!isExternal) {
2716
+ if (node.isTypeOnly) {
2717
+ const symbol = getSymbolOfDeclaration(g.c, node);
2718
+ if (symbol !== undefined && resolveAlias(g.c, symbol) !== g.c.unknownSymbol)
2719
+ grammarErrorOnNode(g, node, Diagnostics.An_import_alias_cannot_use_import_type);
2720
+ }
2721
+ return;
2722
+ }
2723
+ if (
2724
+ checkExternalImportOrExportDeclaration(g, node) &&
2725
+ g.moduleKind >= ModuleKind.ES2015 &&
2726
+ g.moduleKind <= ModuleKind.ESNext &&
2727
+ !node.isTypeOnly &&
2728
+ (node.flags & NodeFlags.Ambient) === 0
2729
+ )
2730
+ // Import assignments can't be emitted as ECMAScript modules.
2731
+ grammarErrorOnNode(
2732
+ g,
2733
+ node,
2734
+ Diagnostics.Import_assignment_cannot_be_used_when_targeting_ECMAScript_modules_Consider_using_import_Asterisk_as_ns_from_mod_import_a_from_mod_import_d_from_mod_or_another_module_format_instead,
2735
+ );
2736
+ }
2737
+
2738
+ function checkGrammarExportDeclaration(g: GrammarChecker, node: ExportDeclaration): void {
2739
+ if (
2740
+ checkGrammarModuleElementContext(
2741
+ g,
2742
+ node,
2743
+ Diagnostics.An_export_declaration_can_only_be_used_at_the_top_level_of_a_namespace_or_module,
2744
+ )
2745
+ )
2746
+ return;
2747
+ if (!checkGrammarModifiers(g, node) && node.modifiers !== undefined)
2748
+ grammarErrorOnFirstToken(g, node, Diagnostics.An_export_declaration_cannot_have_modifiers);
2749
+ const clause = node.exportClause;
2750
+ if (node.isTypeOnly && clause !== undefined && clause.kind === SyntaxKind.NamedExports)
2751
+ checkGrammarTypeOnlyNamedImportsOrExports(g, clause);
2752
+ const hasModuleSpecifier = node.moduleSpecifier !== undefined;
2753
+ if (
2754
+ (!hasModuleSpecifier || checkExternalImportOrExportDeclaration(g, node)) &&
2755
+ clause !== undefined &&
2756
+ clause.kind === SyntaxKind.NamedExports
2757
+ ) {
2758
+ for (const specifier of getElements(clause)!.nodes) {
2759
+ checkGrammarModuleExportName(g, (specifier as ExportSpecifier).propertyName, hasModuleSpecifier);
2760
+ checkGrammarModuleExportName(g, (specifier as ExportSpecifier).name, true);
2761
+ }
2762
+ const parent = node.parent!;
2763
+ const inAmbientExternalModule = parent.kind === SyntaxKind.ModuleBlock && isAmbientModule(parent.parent!);
2764
+ const inAmbientNamespaceDeclaration =
2765
+ !inAmbientExternalModule &&
2766
+ parent.kind === SyntaxKind.ModuleBlock &&
2767
+ !hasModuleSpecifier &&
2768
+ (node.flags & NodeFlags.Ambient) !== 0;
2769
+ if (parent.kind !== SyntaxKind.SourceFile && !inAmbientExternalModule && !inAmbientNamespaceDeclaration)
2770
+ g.c.error(node, Diagnostics.Export_declarations_are_not_permitted_in_a_namespace);
2771
+ }
2772
+ checkGrammarImportAttributes(g, node);
2773
+ }
2774
+
2775
+ function checkGrammarModuleExportName(g: GrammarChecker, name: Node | undefined, allowStringLiteral: boolean): void {
2776
+ if (name === undefined || name.kind !== SyntaxKind.StringLiteral) return;
2777
+ if (!allowStringLiteral) grammarErrorOnNode(g, name, Diagnostics.Identifier_expected);
2778
+ else if ((g.moduleKind === ModuleKind.ES2015 || g.moduleKind === ModuleKind.ES2020) && !g.file.isDeclarationFile)
2779
+ grammarErrorOnNode(
2780
+ g,
2781
+ name,
2782
+ Diagnostics.String_literal_import_and_export_names_are_not_supported_when_the_module_flag_is_set_to_es2015_or_es2020,
2783
+ );
2784
+ }
2785
+
2786
+ /** Returns whether the export's expression is checked. */
2787
+ function checkGrammarExportAssignment(g: GrammarChecker, node: ExportAssignment): boolean {
2788
+ const isExportEquals = node.isExportEquals;
2789
+ if (
2790
+ checkGrammarModuleElementContext(
2791
+ g,
2792
+ node,
2793
+ isExportEquals
2794
+ ? Diagnostics.An_export_assignment_must_be_at_the_top_level_of_a_file_or_module_declaration
2795
+ : Diagnostics.A_default_export_must_be_at_the_top_level_of_a_file_or_module_declaration,
2796
+ )
2797
+ )
2798
+ return false;
2799
+ const parent = node.parent!;
2800
+ const container = parent.kind === SyntaxKind.SourceFile ? parent : parent.parent!;
2801
+ if (container.kind === SyntaxKind.ModuleDeclaration && !isAmbientModule(container)) {
2802
+ g.c.error(
2803
+ node,
2804
+ isExportEquals
2805
+ ? Diagnostics.An_export_assignment_cannot_be_used_in_a_namespace
2806
+ : Diagnostics.A_default_export_can_only_be_used_in_an_ECMAScript_style_module,
2807
+ );
2808
+ return false;
2809
+ }
2810
+ if (!checkGrammarModifiers(g, node) && node.modifiers !== undefined)
2811
+ grammarErrorOnFirstToken(g, node, Diagnostics.An_export_assignment_cannot_have_modifiers);
2812
+ const isAmbient = (node.flags & NodeFlags.Ambient) !== 0;
2813
+ if (isAmbient && !isEntityNameExpression(node.expression, false))
2814
+ grammarErrorOnNode(
2815
+ g,
2816
+ node.expression,
2817
+ Diagnostics.The_expression_of_an_export_assignment_must_be_an_identifier_or_qualified_name_in_an_ambient_context,
2818
+ );
2819
+ if (isExportEquals) {
2820
+ const impliedFormat = getImpliedNodeFormatForEmit(g.file.fileName, g.moduleKind, g.metadata);
2821
+ // Export assignments aren't supported in ECMAScript modules.
2822
+ if (
2823
+ g.moduleKind >= ModuleKind.ES2015 &&
2824
+ g.moduleKind !== ModuleKind.Preserve &&
2825
+ (isAmbient ? impliedFormat === ModuleKind.ESNext : impliedFormat !== ModuleKind.CommonJS)
2826
+ )
2827
+ grammarErrorOnNode(
2828
+ g,
2829
+ node,
2830
+ Diagnostics.Export_assignment_cannot_be_used_when_targeting_ECMAScript_modules_Consider_using_export_default_or_another_module_format_instead,
2831
+ );
2832
+ else if (g.moduleKind === ModuleKind.System && !isAmbient)
2833
+ grammarErrorOnNode(g, node, Diagnostics.Export_assignment_is_not_supported_when_module_flag_is_system);
2834
+ }
2835
+ return true;
2836
+ }
2837
+
2838
+ function getEmitModuleFormat(g: GrammarChecker): ModuleKind {
2839
+ return getEmitModuleFormatOfFile(g.file.fileName, g.c.compilerOptions, g.metadata);
2840
+ }
2841
+
2842
+ function isEffectiveExternalModule(g: GrammarChecker): boolean {
2843
+ if (isExternalModule(g.file)) return true;
2844
+ const moduleKind = g.moduleKind;
2845
+ const commonJSContaining =
2846
+ moduleKind === ModuleKind.CommonJS || (moduleKind >= ModuleKind.Node16 && moduleKind <= ModuleKind.NodeNext);
2847
+ return commonJSContaining && g.file.commonJSModuleIndicator !== undefined;
2848
+ }
2849
+
2850
+ // ---- Type nodes
2851
+
2852
+ function checkGrammarTypeReference(g: GrammarChecker, node: TypeReferenceNode): void {
2853
+ checkGrammarTypeArguments(g, node.typeArguments);
2854
+ const typeArguments = node.typeArguments;
2855
+ if ((node.flags & NodeFlags.JSDoc) !== 0 || typeArguments === undefined || node.typeName.end === typeArguments.pos)
2856
+ return;
2857
+ // `Foo.<T>` is JSDoc syntax. A token starting with `<`, as in `Foo<T>`, is not a dot, which
2858
+ // saves creating a scanner over the file.
2859
+ if (g.file.text.charCodeAt(node.typeName.end) === CharacterCodes.lessThan) return;
2860
+ const scanner = g.getScanner();
2861
+ scanner.resetTokenState(node.typeName.end);
2862
+ if (scanner.scan() === SyntaxKind.DotToken)
2863
+ grammarErrorAtPos(
2864
+ g,
2865
+ skipTrivia(g.file.text, node.typeName.end),
2866
+ 1,
2867
+ Diagnostics.JSDoc_types_can_only_be_used_inside_documentation_comments,
2868
+ );
2869
+ }
2870
+
2871
+ function checkGrammarTypeOperatorNode(g: GrammarChecker, node: TypeOperatorNode): boolean {
2872
+ const innerType = node.type;
2873
+ if (node.operator === SyntaxKind.UniqueKeyword) {
2874
+ if (innerType.kind !== SyntaxKind.SymbolKeyword)
2875
+ return grammarErrorOnNode(g, innerType, Diagnostics.X_0_expected, tokenToString(SyntaxKind.SymbolKeyword));
2876
+ let parent = node.parent!;
2877
+ while (parent.kind === SyntaxKind.ParenthesizedType) parent = parent.parent!;
2878
+ switch (parent.kind) {
2879
+ case SyntaxKind.VariableDeclaration: {
2880
+ const declaration = parent as VariableDeclaration;
2881
+ if (declaration.name.kind !== SyntaxKind.Identifier)
2882
+ return grammarErrorOnNode(
2883
+ g,
2884
+ node,
2885
+ Diagnostics.X_unique_symbol_types_may_not_be_used_on_a_variable_declaration_with_a_binding_name,
2886
+ );
2887
+ if (
2888
+ !(
2889
+ declaration.parent!.kind === SyntaxKind.VariableDeclarationList &&
2890
+ declaration.parent!.parent!.kind === SyntaxKind.VariableStatement
2891
+ )
2892
+ )
2893
+ return grammarErrorOnNode(
2894
+ g,
2895
+ node,
2896
+ Diagnostics.X_unique_symbol_types_are_only_allowed_on_variables_in_a_variable_statement,
2897
+ );
2898
+ if ((declaration.parent!.flags & NodeFlags.Const) === 0)
2899
+ return grammarErrorOnNode(
2900
+ g,
2901
+ declaration.name,
2902
+ Diagnostics.A_variable_whose_type_is_a_unique_symbol_type_must_be_const,
2903
+ );
2904
+ return false;
2905
+ }
2906
+ case SyntaxKind.PropertyDeclaration:
2907
+ if (!isStatic(parent) || !hasSyntacticModifier(parent, ModifierFlags.Readonly))
2908
+ return grammarErrorOnNode(
2909
+ g,
2910
+ getName(parent)!,
2911
+ Diagnostics.A_property_of_a_class_whose_type_is_a_unique_symbol_type_must_be_both_static_and_readonly,
2912
+ );
2913
+ return false;
2914
+ case SyntaxKind.PropertySignature:
2915
+ if (!hasSyntacticModifier(parent, ModifierFlags.Readonly))
2916
+ return grammarErrorOnNode(
2917
+ g,
2918
+ getName(parent)!,
2919
+ Diagnostics.A_property_of_an_interface_or_type_literal_whose_type_is_a_unique_symbol_type_must_be_readonly,
2920
+ );
2921
+ return false;
2922
+ }
2923
+ return grammarErrorOnNode(g, node, Diagnostics.X_unique_symbol_types_are_not_allowed_here);
2924
+ }
2925
+ if (
2926
+ node.operator === SyntaxKind.ReadonlyKeyword &&
2927
+ innerType.kind !== SyntaxKind.ArrayType &&
2928
+ innerType.kind !== SyntaxKind.TupleType
2929
+ )
2930
+ return grammarErrorOnFirstToken(
2931
+ g,
2932
+ node,
2933
+ Diagnostics.X_readonly_type_modifier_is_only_permitted_on_array_and_tuple_literal_types,
2934
+ tokenToString(SyntaxKind.SymbolKeyword),
2935
+ );
2936
+ return false;
2937
+ }
2938
+
2939
+ function checkGrammarInferType(g: GrammarChecker, node: Node): void {
2940
+ for (let current: Node | undefined = node; current !== undefined; current = current.parent) {
2941
+ const parent: Node | undefined = current.parent;
2942
+ if (
2943
+ parent !== undefined &&
2944
+ parent.kind === SyntaxKind.ConditionalType &&
2945
+ (parent as ConditionalTypeNode).extendsType === current
2946
+ )
2947
+ return;
2948
+ }
2949
+ grammarErrorOnNode(
2950
+ g,
2951
+ node,
2952
+ Diagnostics.X_infer_declarations_are_only_permitted_in_the_extends_clause_of_a_conditional_type,
2953
+ );
2954
+ }
2955
+
2956
+ function checkGrammarTupleType(g: GrammarChecker, node: TupleTypeNode): void {
2957
+ let seenOptionalElement = false;
2958
+ let seenRestElement = false;
2959
+ for (const element of node.elements.nodes) {
2960
+ let flags = getTupleElementFlags(g.c, element);
2961
+ if ((flags & ElementFlags.Variadic) !== 0) {
2962
+ const t = getTypeFromTypeNode(g.c, getType(element)!);
2963
+ // `checkTupleType` reports non-array rest types and stops.
2964
+ if (!isArrayLikeType(g.c, t)) break;
2965
+ if (isArrayType(g.c, t) || (isTupleType(t) && (tupleTargetOf(t).combinedFlags & ElementFlags.Rest) !== 0))
2966
+ flags |= ElementFlags.Rest;
2967
+ }
2968
+ if ((flags & ElementFlags.Rest) !== 0) {
2969
+ if (seenRestElement) {
2970
+ grammarErrorOnNode(g, element, Diagnostics.A_rest_element_cannot_follow_another_rest_element);
2971
+ break;
2972
+ }
2973
+ seenRestElement = true;
2974
+ } else if ((flags & ElementFlags.Optional) !== 0) {
2975
+ if (seenRestElement) {
2976
+ grammarErrorOnNode(g, element, Diagnostics.An_optional_element_cannot_follow_a_rest_element);
2977
+ break;
2978
+ }
2979
+ seenOptionalElement = true;
2980
+ } else if ((flags & ElementFlags.Required) !== 0 && seenOptionalElement) {
2981
+ grammarErrorOnNode(g, element, Diagnostics.A_required_element_cannot_follow_an_optional_element);
2982
+ break;
2983
+ }
2984
+ }
2985
+ }
2986
+
2987
+ function checkGrammarNamedTupleMember(g: GrammarChecker, node: NamedTupleMember): void {
2988
+ if (node.dotDotDotToken !== undefined && node.questionToken !== undefined)
2989
+ grammarErrorOnNode(g, node, Diagnostics.A_tuple_member_cannot_be_both_optional_and_rest);
2990
+ if (node.type.kind === SyntaxKind.OptionalType)
2991
+ grammarErrorOnNode(
2992
+ g,
2993
+ node.type,
2994
+ Diagnostics.A_labeled_tuple_element_is_declared_as_optional_with_a_question_mark_after_the_name_and_before_the_colon_rather_than_after_the_type,
2995
+ );
2996
+ if (node.type.kind === SyntaxKind.RestType)
2997
+ grammarErrorOnNode(
2998
+ g,
2999
+ node.type,
3000
+ Diagnostics.A_labeled_tuple_element_is_declared_as_rest_with_a_before_the_name_rather_than_before_the_type,
3001
+ );
3002
+ }
3003
+
3004
+ /** JSDoc-only type syntax in TypeScript code. */
3005
+ function checkGrammarJSDocTypeInTypeScript(g: GrammarChecker, node: Node): void {
3006
+ if ((node.flags & NodeFlags.JavaScriptFile) !== 0) return;
3007
+ if (node.kind !== SyntaxKind.JSDocNonNullableType && node.kind !== SyntaxKind.JSDocNullableType) {
3008
+ grammarErrorOnNode(g, node, Diagnostics.JSDoc_types_can_only_be_used_inside_documentation_comments);
3009
+ return;
3010
+ }
3011
+ const isNullable = node.kind === SyntaxKind.JSDocNullableType;
3012
+ const typeNode = getType(node)!;
3013
+ const postfix = node.pos === typeNode.pos;
3014
+ let t = getTypeFromTypeNode(g.c, typeNode);
3015
+ if (isNullable && t !== g.c.neverType && t !== g.c.voidType)
3016
+ t = getNullableType(g.c, t, postfix ? TypeFlags.Undefined : TypeFlags.Nullable);
3017
+ grammarErrorOnNode(
3018
+ g,
3019
+ node,
3020
+ postfix
3021
+ ? Diagnostics.X_0_at_the_end_of_a_type_is_not_valid_TypeScript_syntax_Did_you_mean_to_write_1
3022
+ : Diagnostics.X_0_at_the_start_of_a_type_is_not_valid_TypeScript_syntax_Did_you_mean_to_write_1,
3023
+ isNullable ? '?' : '!',
3024
+ typeToString(g.c, t),
3025
+ );
3026
+ }
3027
+
3028
+ /** The `resolution-mode` of import type attributes, as tsgo's `getResolutionModeOverride`. */
3029
+ function getResolutionModeOverride(g: GrammarChecker, node: ImportAttributes, reportErrors: boolean): ModuleKind {
3030
+ const attributes = node.attributes.nodes;
3031
+ if (attributes.length !== 1) {
3032
+ if (reportErrors)
3033
+ grammarErrorOnNode(
3034
+ g,
3035
+ node,
3036
+ Diagnostics.Type_import_attributes_should_have_exactly_one_key_resolution_mode_with_value_import_or_require,
3037
+ );
3038
+ return ModuleKind.None;
3039
+ }
3040
+ const attribute = attributes[0]! as ImportAttribute;
3041
+ const name = attribute.name;
3042
+ if (name === undefined || !isStringLiteralLike(name)) return ModuleKind.None;
3043
+ if (getText(name) !== 'resolution-mode') {
3044
+ if (reportErrors)
3045
+ grammarErrorOnNode(g, name, Diagnostics.X_resolution_mode_is_the_only_valid_key_for_type_import_attributes);
3046
+ return ModuleKind.None;
3047
+ }
3048
+ const value = attribute.value;
3049
+ if (!isStringLiteralLike(value)) return ModuleKind.None;
3050
+ const text = getText(value);
3051
+ if (text === 'import') return ModuleKind.ESNext;
3052
+ if (text === 'require') return ModuleKind.CommonJS;
3053
+ if (reportErrors) grammarErrorOnNode(g, value, Diagnostics.X_resolution_mode_should_be_either_require_or_import);
3054
+ return ModuleKind.None;
3055
+ }
3056
+
3057
+ /** The grammar of `checkImportAttributes` for import and export declarations. */
3058
+ function checkGrammarImportAttributes(g: GrammarChecker, declaration: Node): void {
3059
+ const node = getImportAttributes(declaration);
3060
+ if (node === undefined) return;
3061
+ const isTypeOnly = isExclusivelyTypeOnlyImportOrExport(declaration);
3062
+ const override = getResolutionModeOverride(g, node, isTypeOnly);
3063
+ // Other checks don't apply to type-only imports with a resolution mode.
3064
+ if (isTypeOnly && override !== ModuleKind.None) return;
3065
+ const moduleKind = g.moduleKind;
3066
+ if (
3067
+ !(
3068
+ (moduleKind >= ModuleKind.Node18 && moduleKind <= ModuleKind.NodeNext) ||
3069
+ moduleKind === ModuleKind.Preserve ||
3070
+ moduleKind === ModuleKind.ESNext
3071
+ )
3072
+ ) {
3073
+ grammarErrorOnNode(
3074
+ g,
3075
+ node,
3076
+ Diagnostics.Import_attributes_are_only_supported_when_the_module_option_is_set_to_esnext_node18_node20_nodenext_or_preserve,
3077
+ );
3078
+ return;
3079
+ }
3080
+ const moduleSpecifier = getExternalModuleName(declaration);
3081
+ if (
3082
+ moduleSpecifier !== undefined &&
3083
+ isStringLiteralLike(moduleSpecifier) &&
3084
+ getEmitSyntaxOfImportOrExport(g) === ModuleKind.CommonJS
3085
+ ) {
3086
+ grammarErrorOnNode(
3087
+ g,
3088
+ node,
3089
+ Diagnostics.Import_attributes_are_not_allowed_on_statements_that_compile_to_CommonJS_require_calls,
3090
+ );
3091
+ return;
3092
+ }
3093
+ if (isTypeOnly) {
3094
+ grammarErrorOnNode(g, node, Diagnostics.Import_attributes_cannot_be_used_with_type_only_imports_or_exports);
3095
+ return;
3096
+ }
3097
+ if (override !== ModuleKind.None)
3098
+ grammarErrorOnNode(g, node, Diagnostics.X_resolution_mode_can_only_be_set_for_type_only_imports);
3099
+ }
3100
+
3101
+ function isExclusivelyTypeOnlyImportOrExport(node: Node): boolean {
3102
+ if (node.kind === SyntaxKind.ExportDeclaration) return (node as ExportDeclaration).isTypeOnly;
3103
+ if (node.kind !== SyntaxKind.ImportDeclaration && node.kind !== SyntaxKind.JSImportDeclaration) return false;
3104
+ const clause = (node as ImportDeclaration).importClause;
3105
+ return clause !== undefined && clause.phaseModifier === SyntaxKind.TypeKeyword;
3106
+ }
3107
+
3108
+ /** The module syntax an import or export declaration is emitted as. */
3109
+ function getEmitSyntaxOfImportOrExport(g: GrammarChecker): ModuleKind {
3110
+ const format = getEmitModuleFormat(g);
3111
+ if (format === ModuleKind.CommonJS) return ModuleKind.CommonJS;
3112
+ if ((format >= ModuleKind.ES2015 && format <= ModuleKind.ESNext) || format === ModuleKind.Preserve)
3113
+ return ModuleKind.ESNext;
3114
+ return ModuleKind.None;
3115
+ }
3116
+
3117
+ // ---- Expressions
3118
+
3119
+ const enum DeclarationMeaning {
3120
+ GetAccessor = 1 << 0,
3121
+ SetAccessor = 1 << 1,
3122
+ PropertyAssignment = 1 << 2,
3123
+ Method = 1 << 3,
3124
+ GetOrSetAccessor = GetAccessor | SetAccessor,
3125
+ }
3126
+
3127
+ function checkGrammarObjectLiteralExpression(
3128
+ g: GrammarChecker,
3129
+ node: ObjectLiteralExpression,
3130
+ inDestructuring: boolean,
3131
+ ): boolean {
3132
+ const seen = new Map<string, DeclarationMeaning>();
3133
+ for (const property of node.properties.nodes) {
3134
+ if (property.kind === SyntaxKind.SpreadAssignment) {
3135
+ if (inDestructuring) {
3136
+ // A rest property can't be destructured further.
3137
+ const expression = skipParentheses(getExpression(property)!);
3138
+ if (
3139
+ expression.kind === SyntaxKind.ArrayLiteralExpression ||
3140
+ expression.kind === SyntaxKind.ObjectLiteralExpression
3141
+ )
3142
+ return grammarErrorOnNode(
3143
+ g,
3144
+ getExpression(property)!,
3145
+ Diagnostics.A_rest_element_cannot_contain_a_binding_pattern,
3146
+ );
3147
+ }
3148
+ continue;
3149
+ }
3150
+ const name = getName(property)!;
3151
+ if (name.kind === SyntaxKind.ComputedPropertyName) checkGrammarComputedPropertyName(g, name);
3152
+ if (property.kind === SyntaxKind.ShorthandPropertyAssignment && !inDestructuring) {
3153
+ // An initializer is only valid in a destructuring pattern; report on the `=`.
3154
+ const shorthand = property as ShorthandPropertyAssignment;
3155
+ const equalsToken: Node | undefined = shorthand.equalsToken;
3156
+ if (shorthand.objectAssignmentInitializer !== undefined)
3157
+ grammarErrorOnFirstToken(
3158
+ g,
3159
+ equalsToken !== undefined ? equalsToken : shorthand.name,
3160
+ Diagnostics.Did_you_mean_to_use_a_Colon_An_can_only_follow_a_property_name_when_the_containing_object_literal_is_part_of_a_destructuring_pattern,
3161
+ );
3162
+ }
3163
+ if (name.kind === SyntaxKind.PrivateIdentifier)
3164
+ grammarErrorOnNode(g, name, Diagnostics.Private_identifiers_are_not_allowed_outside_class_bodies);
3165
+ // Only `async` is allowed, and only on methods.
3166
+ const modifiers = getModifiers(property);
3167
+ if (modifiers !== undefined)
3168
+ for (const modifier of modifiers.nodes)
3169
+ if (
3170
+ isModifierKind(modifier.kind) &&
3171
+ (modifier.kind !== SyntaxKind.AsyncKeyword || property.kind !== SyntaxKind.MethodDeclaration)
3172
+ )
3173
+ grammarErrorOnNode(
3174
+ g,
3175
+ modifier,
3176
+ Diagnostics.X_0_modifier_cannot_be_used_here,
3177
+ getTextOfNodeFromSourceText(g.file.text, modifier),
3178
+ );
3179
+ // Duplicate data properties are errors in strict mode only, but accessors and data
3180
+ // properties never mix, and get/set accessors can't repeat (ECMA-262 11.1.5).
3181
+ let currentKind: DeclarationMeaning;
3182
+ switch (property.kind) {
3183
+ case SyntaxKind.ShorthandPropertyAssignment:
3184
+ case SyntaxKind.PropertyAssignment: {
3185
+ const postfixToken = getPostfixToken(property);
3186
+ checkGrammarForInvalidExclamationToken(
3187
+ g,
3188
+ postfixToken,
3189
+ Diagnostics.A_definite_assignment_assertion_is_not_permitted_in_this_context,
3190
+ );
3191
+ checkGrammarForInvalidQuestionMark(g, postfixToken, Diagnostics.An_object_member_cannot_be_declared_optional);
3192
+ if (name.kind === SyntaxKind.BigIntLiteral)
3193
+ g.c.addDiagnostic(
3194
+ createDiagnosticForNode(name, Diagnostics.A_bigint_literal_cannot_be_used_as_a_property_name, []),
3195
+ );
3196
+ currentKind = DeclarationMeaning.PropertyAssignment;
3197
+ break;
3198
+ }
3199
+ case SyntaxKind.MethodDeclaration:
3200
+ currentKind = DeclarationMeaning.Method;
3201
+ break;
3202
+ case SyntaxKind.GetAccessor:
3203
+ currentKind = DeclarationMeaning.GetAccessor;
3204
+ break;
3205
+ default:
3206
+ currentKind = DeclarationMeaning.SetAccessor;
3207
+ break;
3208
+ }
3209
+ if (inDestructuring) continue;
3210
+ const effectiveName = getEffectivePropertyNameForPropertyNameNode(g.c, name);
3211
+ if (effectiveName === undefined) continue;
3212
+ const existingKind = seen.get(effectiveName);
3213
+ if (existingKind === undefined) {
3214
+ seen.set(effectiveName, currentKind);
3215
+ continue;
3216
+ }
3217
+ if ((currentKind & DeclarationMeaning.Method) !== 0 && (existingKind & DeclarationMeaning.Method) !== 0)
3218
+ grammarErrorOnNode(g, name, Diagnostics.Duplicate_identifier_0, getTextOfNodeFromSourceText(g.file.text, name));
3219
+ else if (
3220
+ (currentKind & DeclarationMeaning.PropertyAssignment) !== 0 &&
3221
+ (existingKind & DeclarationMeaning.PropertyAssignment) !== 0
3222
+ )
3223
+ grammarErrorOnNode(
3224
+ g,
3225
+ name,
3226
+ Diagnostics.An_object_literal_cannot_have_multiple_properties_with_the_same_name,
3227
+ getTextOfNodeFromSourceText(g.file.text, name),
3228
+ );
3229
+ else if (
3230
+ (currentKind & DeclarationMeaning.GetOrSetAccessor) !== 0 &&
3231
+ (existingKind & DeclarationMeaning.GetOrSetAccessor) !== 0
3232
+ ) {
3233
+ if (existingKind !== DeclarationMeaning.GetOrSetAccessor && currentKind !== existingKind)
3234
+ seen.set(effectiveName, currentKind | existingKind);
3235
+ else
3236
+ return grammarErrorOnNode(
3237
+ g,
3238
+ name,
3239
+ Diagnostics.An_object_literal_cannot_have_multiple_get_Slashset_accessors_with_the_same_name,
3240
+ );
3241
+ } else {
3242
+ return grammarErrorOnNode(
3243
+ g,
3244
+ name,
3245
+ Diagnostics.An_object_literal_cannot_have_property_and_accessor_with_the_same_name,
3246
+ );
3247
+ }
3248
+ }
3249
+ return false;
3250
+ }
3251
+
3252
+ /** The property name a literal or computed name denotes, if known. */
3253
+ function getEffectivePropertyNameForPropertyNameNode(c: Checker, name: Node): string | undefined {
3254
+ switch (name.kind) {
3255
+ case SyntaxKind.Identifier:
3256
+ case SyntaxKind.PrivateIdentifier:
3257
+ case SyntaxKind.StringLiteral:
3258
+ case SyntaxKind.NoSubstitutionTemplateLiteral:
3259
+ case SyntaxKind.NumericLiteral:
3260
+ case SyntaxKind.BigIntLiteral:
3261
+ return getText(name);
3262
+ case SyntaxKind.ComputedPropertyName: {
3263
+ const expression = (name as ComputedPropertyName).expression;
3264
+ switch (expression.kind) {
3265
+ case SyntaxKind.StringLiteral:
3266
+ case SyntaxKind.NoSubstitutionTemplateLiteral:
3267
+ case SyntaxKind.NumericLiteral:
3268
+ return getText(expression);
3269
+ case SyntaxKind.PrefixUnaryExpression: {
3270
+ const unary = expression as PrefixUnaryExpression;
3271
+ if (
3272
+ unary.operand.kind === SyntaxKind.NumericLiteral &&
3273
+ (unary.operator === SyntaxKind.MinusToken || unary.operator === SyntaxKind.PlusToken)
3274
+ )
3275
+ return unary.operator === SyntaxKind.MinusToken ? '-' + getText(unary.operand) : getText(unary.operand);
3276
+ break;
3277
+ }
3278
+ }
3279
+ const t = getTypeOfExpression(c, expression);
3280
+ return (t.flags & (TypeFlags.UniqueESSymbol | TypeFlags.StringLiteral | TypeFlags.NumberLiteral)) !== 0
3281
+ ? getPropertyNameFromType(t)
3282
+ : undefined;
3283
+ }
3284
+ }
3285
+ return undefined;
3286
+ }
3287
+
3288
+ function checkGrammarCallExpression(g: GrammarChecker, node: CallExpression): void {
3289
+ const callee = node.expression;
3290
+ const isImportCall =
3291
+ callee.kind === SyntaxKind.ImportKeyword ||
3292
+ (callee.kind === SyntaxKind.MetaProperty &&
3293
+ (callee as MetaProperty).keywordToken === SyntaxKind.ImportKeyword &&
3294
+ (callee as MetaProperty).name.text === 'defer');
3295
+ if (isImportCall) checkGrammarImportCallExpression(g, node);
3296
+ else checkGrammarTypeArguments(g, node.typeArguments);
3297
+ }
3298
+
3299
+ function checkGrammarImportCallExpression(g: GrammarChecker, node: CallExpression): boolean {
3300
+ const moduleKind = g.moduleKind;
3301
+ if (g.c.compilerOptions.verbatimModuleSyntax === true && moduleKind === ModuleKind.CommonJS)
3302
+ return grammarErrorOnNode(
3303
+ g,
3304
+ node,
3305
+ fileExtensionIsOneOf(g.file.fileName, ['.cts', '.cjs'])
3306
+ ? Diagnostics.ECMAScript_imports_and_exports_cannot_be_written_in_a_CommonJS_file_under_verbatimModuleSyntax
3307
+ : Diagnostics.ECMAScript_imports_and_exports_cannot_be_written_in_a_CommonJS_file_under_verbatimModuleSyntax_Adjust_the_type_field_in_the_nearest_package_json_to_make_this_file_an_ECMAScript_module_or_adjust_your_verbatimModuleSyntax_module_and_moduleResolution_settings_in_TypeScript,
3308
+ );
3309
+ if (node.expression.kind === SyntaxKind.MetaProperty) {
3310
+ if (moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.Preserve)
3311
+ return grammarErrorOnNode(
3312
+ g,
3313
+ node,
3314
+ Diagnostics.Deferred_imports_are_only_supported_when_the_module_flag_is_set_to_esnext_or_preserve,
3315
+ );
3316
+ } else if (moduleKind === ModuleKind.ES2015)
3317
+ return grammarErrorOnNode(
3318
+ g,
3319
+ node,
3320
+ Diagnostics.Dynamic_imports_are_only_supported_when_the_module_flag_is_set_to_es2020_es2022_esnext_commonjs_amd_system_umd_node16_node18_node20_or_nodenext,
3321
+ );
3322
+ if (node.typeArguments !== undefined)
3323
+ return grammarErrorOnNode(
3324
+ g,
3325
+ node,
3326
+ Diagnostics.This_use_of_import_is_invalid_import_calls_can_be_written_but_they_must_have_parentheses_and_cannot_have_type_arguments,
3327
+ );
3328
+ const args = node.arguments;
3329
+ const argumentNodes = args.nodes;
3330
+ if (
3331
+ !(moduleKind >= ModuleKind.Node16 && moduleKind <= ModuleKind.NodeNext) &&
3332
+ moduleKind !== ModuleKind.ESNext &&
3333
+ moduleKind !== ModuleKind.Preserve
3334
+ ) {
3335
+ // Import attributes allow a trailing comma.
3336
+ checkGrammarForDisallowedTrailingComma(g, args, Diagnostics.Trailing_comma_not_allowed);
3337
+ if (argumentNodes.length > 1)
3338
+ return grammarErrorOnNode(
3339
+ g,
3340
+ argumentNodes[1]!,
3341
+ Diagnostics.Dynamic_imports_only_support_a_second_argument_when_the_module_option_is_set_to_esnext_node16_node18_node20_nodenext_or_preserve,
3342
+ );
3343
+ }
3344
+ if (argumentNodes.length === 0 || argumentNodes.length > 2)
3345
+ return grammarErrorOnNode(
3346
+ g,
3347
+ node,
3348
+ Diagnostics.Dynamic_imports_can_only_accept_a_module_specifier_and_an_optional_set_of_attributes_as_arguments,
3349
+ );
3350
+ // Arguments are parsed like array elements, which allows spreads.
3351
+ for (const argument of argumentNodes)
3352
+ if (argument.kind === SyntaxKind.SpreadElement)
3353
+ return grammarErrorOnNode(g, argument, Diagnostics.Argument_of_dynamic_import_cannot_be_spread_element);
3354
+ return false;
3355
+ }
3356
+
3357
+ function checkGrammarTaggedTemplateChain(g: GrammarChecker, node: TaggedTemplateExpression): boolean {
3358
+ if (node.questionDotToken === undefined && (node.flags & NodeFlags.OptionalChain) === 0) return false;
3359
+ return grammarErrorOnNode(
3360
+ g,
3361
+ node.template,
3362
+ Diagnostics.Tagged_template_expressions_are_not_permitted_in_an_optional_chain,
3363
+ );
3364
+ }
3365
+
3366
+ function checkGrammarMetaProperty(g: GrammarChecker, node: MetaProperty): boolean {
3367
+ const name = node.name;
3368
+ const nameText = name.text;
3369
+ if (node.keywordToken === SyntaxKind.NewKeyword) {
3370
+ if (nameText !== 'target')
3371
+ return grammarErrorOnNode(
3372
+ g,
3373
+ name,
3374
+ Diagnostics.X_0_is_not_a_valid_meta_property_for_keyword_1_Did_you_mean_2,
3375
+ nameText,
3376
+ tokenToString(node.keywordToken),
3377
+ 'target',
3378
+ );
3379
+ } else if (node.keywordToken === SyntaxKind.ImportKeyword && nameText !== 'meta') {
3380
+ const parent = node.parent!;
3381
+ const isCallee = parent.kind === SyntaxKind.CallExpression && (parent as CallExpression).expression === node;
3382
+ if (nameText === 'defer') {
3383
+ if (!isCallee) return grammarErrorAtPos(g, node.end, 0, Diagnostics.X_0_expected, '(');
3384
+ } else {
3385
+ if (isCallee)
3386
+ return grammarErrorOnNode(
3387
+ g,
3388
+ name,
3389
+ Diagnostics.X_0_is_not_a_valid_meta_property_for_keyword_import_Did_you_mean_meta_or_defer,
3390
+ nameText,
3391
+ );
3392
+ return grammarErrorOnNode(
3393
+ g,
3394
+ name,
3395
+ Diagnostics.X_0_is_not_a_valid_meta_property_for_keyword_1_Did_you_mean_2,
3396
+ nameText,
3397
+ tokenToString(node.keywordToken),
3398
+ 'meta',
3399
+ );
3400
+ }
3401
+ }
3402
+ return false;
3403
+ }
3404
+
3405
+ function checkGrammarAwaitOrAwaitUsing(g: GrammarChecker, node: Node): boolean {
3406
+ const isAwaitExpression = node.kind === SyntaxKind.AwaitExpression;
3407
+ let hasError = false;
3408
+ const container = getContainingFunctionOrClassStaticBlock(node);
3409
+ if (container !== undefined && container.kind === SyntaxKind.ClassStaticBlockDeclaration) {
3410
+ // Reported even with parse errors.
3411
+ g.c.error(
3412
+ node,
3413
+ isAwaitExpression
3414
+ ? Diagnostics.X_await_expression_cannot_be_used_inside_a_class_static_block
3415
+ : Diagnostics.X_await_using_statements_cannot_be_used_inside_a_class_static_block,
3416
+ );
3417
+ hasError = true;
3418
+ } else if ((node.flags & NodeFlags.AwaitContext) === 0) {
3419
+ if (isInTopLevelContext(node)) {
3420
+ if (!g.hasParseErrors) {
3421
+ if (!isEffectiveExternalModule(g)) {
3422
+ g.c.addDiagnostic(
3423
+ diagnosticAtFirstToken(
3424
+ g,
3425
+ node,
3426
+ isAwaitExpression
3427
+ ? Diagnostics.X_await_expressions_are_only_allowed_at_the_top_level_of_a_file_when_that_file_is_a_module_but_this_file_has_no_imports_or_exports_Consider_adding_an_empty_export_to_make_this_file_a_module
3428
+ : Diagnostics.X_await_using_statements_are_only_allowed_at_the_top_level_of_a_file_when_that_file_is_a_module_but_this_file_has_no_imports_or_exports_Consider_adding_an_empty_export_to_make_this_file_a_module,
3429
+ ),
3430
+ );
3431
+ hasError = true;
3432
+ }
3433
+ const support = checkTopLevelAwaitModule(g);
3434
+ if (support === TopLevelAwaitSupport.CommonJS) {
3435
+ g.c.addDiagnostic(
3436
+ diagnosticAtFirstToken(
3437
+ g,
3438
+ node,
3439
+ Diagnostics.The_current_file_is_a_CommonJS_module_and_cannot_use_await_at_the_top_level,
3440
+ ),
3441
+ );
3442
+ hasError = true;
3443
+ } else if (support === TopLevelAwaitSupport.Unsupported) {
3444
+ g.c.addDiagnostic(
3445
+ diagnosticAtFirstToken(
3446
+ g,
3447
+ node,
3448
+ isAwaitExpression
3449
+ ? Diagnostics.Top_level_await_expressions_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_node16_node18_node20_nodenext_or_preserve_and_the_target_option_is_set_to_es2017_or_higher
3450
+ : Diagnostics.Top_level_await_using_statements_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_node16_node18_node20_nodenext_or_preserve_and_the_target_option_is_set_to_es2017_or_higher,
3451
+ ),
3452
+ );
3453
+ hasError = true;
3454
+ }
3455
+ }
3456
+ } else if (!g.hasParseErrors) {
3457
+ // `await` in a function that isn't async.
3458
+ const diagnostic = diagnosticAtFirstToken(
3459
+ g,
3460
+ node,
3461
+ isAwaitExpression
3462
+ ? Diagnostics.X_await_expressions_are_only_allowed_within_async_functions_and_at_the_top_levels_of_modules
3463
+ : Diagnostics.X_await_using_statements_are_only_allowed_within_async_functions_and_at_the_top_levels_of_modules,
3464
+ );
3465
+ if (
3466
+ container !== undefined &&
3467
+ container.kind !== SyntaxKind.Constructor &&
3468
+ !hasSyntacticModifier(container, ModifierFlags.Async)
3469
+ )
3470
+ addRelated(diagnostic, container, Diagnostics.Did_you_mean_to_mark_this_function_as_async);
3471
+ g.c.addDiagnostic(diagnostic);
3472
+ hasError = true;
3473
+ }
3474
+ }
3475
+ if (isAwaitExpression && isInParameterInitializerBeforeContainingFunction(node)) {
3476
+ // Reported even with parse errors.
3477
+ g.c.error(node, Diagnostics.X_await_expressions_cannot_be_used_in_a_parameter_initializer);
3478
+ hasError = true;
3479
+ }
3480
+ return hasError;
3481
+ }
3482
+
3483
+ function checkGrammarYieldExpression(g: GrammarChecker, node: Node): boolean {
3484
+ let hasError = false;
3485
+ if ((node.flags & NodeFlags.YieldContext) === 0) {
3486
+ grammarErrorOnFirstToken(g, node, Diagnostics.A_yield_expression_is_only_allowed_in_a_generator_body);
3487
+ hasError = true;
3488
+ }
3489
+ if (isInParameterInitializerBeforeContainingFunction(node)) {
3490
+ g.c.error(node, Diagnostics.X_yield_expressions_cannot_be_used_in_a_parameter_initializer);
3491
+ hasError = true;
3492
+ }
3493
+ return hasError;
3494
+ }
3495
+
3496
+ function isInParameterInitializerBeforeContainingFunction(node: Node): boolean {
3497
+ let inBindingInitializer = false;
3498
+ let current = node;
3499
+ while (current.parent !== undefined && !isFunctionLike(current.parent)) {
3500
+ const parent = current.parent;
3501
+ if (parent.kind === SyntaxKind.Parameter && (inBindingInitializer || getInitializer(parent) === current))
3502
+ return true;
3503
+ if (parent.kind === SyntaxKind.BindingElement && getInitializer(parent) === current) inBindingInitializer = true;
3504
+ current = parent;
3505
+ }
3506
+ return false;
3507
+ }
3508
+
3509
+ /** A private name in an expression, which is only valid as `#x in obj`. */
3510
+ function checkGrammarPrivateIdentifierExpression(g: GrammarChecker, node: Node): boolean {
3511
+ const parent = node.parent!;
3512
+ // Names of members and property accesses aren't expressions.
3513
+ if (parent.kind === SyntaxKind.PropertyAccessExpression || getName(parent) === node) return false;
3514
+ const isForIn = parent.kind === SyntaxKind.ForInStatement;
3515
+ if (getContainingClass(node) === undefined)
3516
+ return grammarErrorOnNode(g, node, Diagnostics.Private_identifiers_are_not_allowed_outside_class_bodies);
3517
+ if (isForIn) return false;
3518
+ const isLeftOfIn =
3519
+ parent.kind === SyntaxKind.BinaryExpression &&
3520
+ (parent as BinaryExpression).left === node &&
3521
+ (parent as BinaryExpression).operatorToken.kind === SyntaxKind.InKeyword;
3522
+ if (!isLeftOfIn)
3523
+ return grammarErrorOnNode(
3524
+ g,
3525
+ node,
3526
+ Diagnostics.Private_identifiers_are_only_allowed_in_class_bodies_and_may_only_be_used_as_part_of_a_class_member_declaration_property_access_or_on_the_left_hand_side_of_an_in_expression,
3527
+ );
3528
+ return false;
3529
+ }
3530
+
3531
+ function checkGrammarBigIntLiteral(g: GrammarChecker, node: Node): boolean {
3532
+ const parent = node.parent!;
3533
+ // Like tsgo, only BigInt literals checked as expressions are reported, not property or member names.
3534
+ if (getName(parent) === node) return false;
3535
+ const isLiteralType =
3536
+ parent.kind === SyntaxKind.LiteralType ||
3537
+ (parent.kind === SyntaxKind.PrefixUnaryExpression && parent.parent!.kind === SyntaxKind.LiteralType);
3538
+ // BigInt literals are fine in types and ambient contexts.
3539
+ return (
3540
+ !isLiteralType &&
3541
+ (node.flags & NodeFlags.Ambient) === 0 &&
3542
+ g.c.languageVersion < ScriptTarget.ES2020 &&
3543
+ grammarErrorOnNode(g, node, Diagnostics.BigInt_literals_are_not_available_when_targeting_lower_than_ES2020)
3544
+ );
3545
+ }
3546
+
3547
+ /** Reports the errors of rescanning a regular expression; spelling suggestions become related information. */
3548
+ class RegularExpressionErrorReporter extends ScannerErrorReporter {
3549
+ readonly g: GrammarChecker;
3550
+ lastError: Diagnostic | undefined = undefined;
3551
+
3552
+ constructor(g: GrammarChecker) {
3553
+ super();
3554
+ this.g = g;
3555
+ }
3556
+
3557
+ onError(message: DiagnosticMessage, start: number, length: number, args: readonly string[]): void {
3558
+ const lastError = this.lastError;
3559
+ if (
3560
+ messageCategory(message) === DiagnosticCategory.Message &&
3561
+ lastError !== undefined &&
3562
+ start === lastError.pos &&
3563
+ length === lastError.end - lastError.pos
3564
+ ) {
3565
+ lastError.relatedInformation.push(new Diagnostic(undefined, start, start + length, message, args));
3566
+ } else if (lastError === undefined || start !== lastError.pos) {
3567
+ const diagnostic = new Diagnostic(this.g.file.diagnosticFile(), start, start + length, message, args);
3568
+ this.lastError = diagnostic;
3569
+ this.g.c.addDiagnostic(diagnostic);
3570
+ }
3571
+ }
3572
+ }
3573
+
3574
+ function checkGrammarRegularExpressionLiteral(g: GrammarChecker, node: Node): boolean {
3575
+ if (g.hasParseErrors) return false;
3576
+ const scanner = g.getScanner();
3577
+ const reporter = new RegularExpressionErrorReporter(g);
3578
+ scanner.setOnError(reporter);
3579
+ scanner.resetTokenState(node.pos);
3580
+ scanner.scan();
3581
+ scanner.reScanSlashToken(true);
3582
+ scanner.setOnError(undefined);
3583
+ return reporter.lastError !== undefined;
3584
+ }