Title: Invalid emit for declaring a class that references itself inside of a loop · Issue #31330 · microsoft/TypeScript · GitHub
Open Graph Title: Invalid emit for declaring a class that references itself inside of a loop · Issue #31330 · microsoft/TypeScript
X Title: Invalid emit for declaring a class that references itself inside of a loop · Issue #31330 · microsoft/TypeScript
Description: TypeScript Version: Version 3.5.0-dev.20190509 Search Terms: Class inside loop Uncaught SyntaxError: Illegal return statement Code const classesByRow: Record
Open Graph Description: TypeScript Version: Version 3.5.0-dev.20190509 Search Terms: Class inside loop Uncaught SyntaxError: Illegal return statement Code const classesByRow: Record
X Description: TypeScript Version: Version 3.5.0-dev.20190509 Search Terms: Class inside loop Uncaught SyntaxError: Illegal return statement Code const classesByRow: Record<string, object> = {}; for (const ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/31330
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Invalid emit for declaring a class that references itself inside of a loop ","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Even if you think you've found a *bug*, please read the FAQ first, especially the Common \"Bugs\" That Aren't Bugs section!\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\nPlease fill in the *entire* template below.\r\n-->\r\n\r\n\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** Version 3.5.0-dev.20190509\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:**\r\nClass inside loop\r\nUncaught SyntaxError: Illegal return statement\r\n\r\n**Code**\r\n```ts\r\nconst classesByRow: Record\u003cstring, object> = {};\r\nfor (const row of ['1', '2', '3', '4', '5']) {\r\n class RowClass {\r\n row = row;\r\n static factory = () => new RowClass();\r\n }\r\n\r\n classesByRow[row] = RowClass;\r\n}\r\n\r\nconsole.log(classesByRow)\r\n```\r\n\r\n**Expected behavior:**\r\nWhen downleveled to ES5 the compiled code runs and logs something like:\r\n```\r\n{1: ƒ, 2: ƒ, 3: ƒ, 4: ƒ, 5: ƒ}\r\n 1: class RowClass\r\n 2: class RowClass\r\n 3: class RowClass\r\n 4: class RowClass\r\n 5: class RowClass\r\n```\r\n\r\n**Actual behavior:**\r\nWhen downleveled to ES5 the compiled code does not run and throws a syntax error:\r\n```\r\nUncaught SyntaxError: Illegal return statement\r\n```\r\n\r\n**Playground Link:** \u003c!-- A link to a TypeScript Playground \"Share\" link which demonstrates this behavior -->\r\nhttps://www.typescriptlang.org/play/index.html#src=const%20classesByRow%3A%20Record%3Cstring%2C%20object%3E%20%3D%20%7B%7D%3B%0Afor%20(const%20row%20of%20%5B'1'%2C%20'2'%2C%20'3'%2C%20'4'%2C%20'5'%5D)%20%7B%0A%20%20class%20RowClass%20%7B%0A%20%20%20%20row%20%3D%20row%3B%0A%20%20%20%20static%20factory%20%3D%20()%20%3D%3E%20new%20RowClass()%3B%0A%20%20%7D%0A%0A%20%20classesByRow%5Brow%5D%20%3D%20RowClass%3B%0A%7D%0A%0Aconsole.log(classesByRow)\r\n\r\n**Related Issues:** \u003c!-- Did you find other bugs that looked similar? -->\r\nI could not find any.\r\n\r\n---\r\nWhen downleveling the ES5 in this weird situation the TS compiler produces JS with either a syntax error or incorrect runtime semantics by placing a `return` statement where it does not belong. This error _seems_ to happen when a class declared inside of a loop has a property that references itself. The error does not happen when the self-referencing property is removed. To test this, remove line 5 from the above example. This issue is reproducable for every kind of loop, not just `for ... of` loops, it also comes up when using `while` loops and `for` loops.\r\n\r\nThe bug does not happen when targeting ES2015 or above.\r\n\r\nI was able to reproduce this on TS 3.3 as well.","author":{"url":"https://github.com/rrdelaney","@type":"Person","name":"rrdelaney"},"datePublished":"2019-05-09T20:44:53Z","url":"https://github.com/microsoft/TypeScript/issues/31330"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:b6248134-d65b-139e-1583-d6dab3f0b566 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 819C:E3C4F:2701DDA:31B836D:6B019D86 |
| html-safe-nonce | b4803caccbd053bce36a3a55b4d3d8023233b397d1900dca89bed6043889138d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MTlDOkUzQzRGOjI3MDFEREE6MzFCODM2RDo2QjAxOUQ4NiIsInZpc2l0b3JfaWQiOiI2OTQ2OTI2MzU4Mjk1NTg0MTM1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 9f6d69a6d57d928cecf03e44b0a0e2c180531375669cc690f1b8598b48957507 |
| hovercard-subject-tag | issue:442425250 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/31330/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f813e5def77c398e167197cb0712493c00552213197eabf79262ae32a7f8bce8/microsoft/TypeScript/issues/31330 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f813e5def77c398e167197cb0712493c00552213197eabf79262ae32a7f8bce8/microsoft/TypeScript/issues/31330 |
| og:image:alt | TypeScript Version: Version 3.5.0-dev.20190509 Search Terms: Class inside loop Uncaught SyntaxError: Illegal return statement Code const classesByRow: Record |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rrdelaney |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width