Title: API: add parameter to ts.forEachChild, that is passed through to the callback · Issue #42285 · microsoft/TypeScript · GitHub
Open Graph Title: API: add parameter to ts.forEachChild, that is passed through to the callback · Issue #42285 · microsoft/TypeScript
X Title: API: add parameter to ts.forEachChild, that is passed through to the callback · Issue #42285 · microsoft/TypeScript
Description: Suggestion 🔍 Search Terms forEachChild closure ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime behavior of existing Ja...
Open Graph Description: Suggestion 🔍 Search Terms forEachChild closure ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't ch...
X Description: Suggestion 🔍 Search Terms forEachChild closure ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn&...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/42285
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"API: add parameter to ts.forEachChild, that is passed through to the callback","articleBody":"# Suggestion\r\n\r\n\u003c!--\r\n Please fill in each section completely. Thank you!\r\n-->\r\n\r\n## 🔍 Search Terms\r\n\r\nforEachChild closure\r\n\r\n## ✅ Viability Checklist\r\n\r\n\u003c!--\r\n Suggestions that don't meet all these criteria are very, very unlikely to be accepted.\r\n We always recommend reviewing the TypeScript design goals before investing time writing\r\n a proposal for ideas outside the scope of the project.\r\n-->\r\nMy suggestion meets these guidelines:\r\n\r\n* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code\r\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [x] This could be implemented without emitting different JS based on the types of the expressions\r\n* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)\r\n* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).\r\n\r\n\r\n## ⭐ Suggestion\r\n\r\nAdd an optional parameter to `ts.forEachChild` that is passed through to the callback.\r\n\r\n```ts\r\nexport function forEachChild\u003cT, S>(node: Node, cbNode: (node: Node, state: S) => T | undefined, cbNodes?: (nodes: NodeArray\u003cNode>, state: S) => T | undefined, state: S): T | undefined;\r\nexport function forEachChild\u003cT>(node: Node, cbNode: (node: Node) => T | undefined, cbNodes?: (nodes: NodeArray\u003cNode>) => T | undefined): T | undefined;\r\n```\r\n\r\nThere's already a similar pattern in `ts.forEach{Leading,Trailing}CommentRanges`: https://github.com/microsoft/TypeScript/blob/1ecf22884fcad201e8c13c269f2eb531a595dfab/src/compiler/scanner.ts#L834\r\n\r\n\r\n## 📃 Motivating Example\r\n\r\nThis would help API users and the compiler codebase itself to avoid closures in many cases when using `forEachChild`.\r\n\r\n## 💻 Use Cases\r\n\r\nBefore (needs closure):\r\n```ts\r\nfunction analyzeNode(node: ts.Node) {\r\n const children: ts.Node[] = [];\r\n // the following closure prevents some opimizations of the containing function\r\n // e.g. 'children' is \"context-allocated\", i.e. on the heap instead of the stack,\r\n // a new instance of the closure is created every time the containing function is called,\r\n // ...\r\n ts.forEachChild(node, (child) => void children.push(child));\r\n // now do stuff with children\r\n}\r\n```\r\n\r\nAfter (without closure):\r\n```ts\r\nfunction collectChildren(node: ts.Node, children: ts.Node[]) {\r\n children.push(node);\r\n}\r\n\r\nfunction analyzeNode(node: ts.Node) {\r\n const children: ts.Node[] = [];\r\n ts.forEachChild(node, collectChildren, undefined, children);\r\n // now do stuff with children\r\n}\r\n```\r\n\r\nThere are several functions in the compiler codebase, that could benefit from this:\r\n* forEachReturnStatement\r\n* forEachYieldExpression\r\n* forEachChildRecursively\r\n* getNodeAtPosition\r\n* ...\r\n","author":{"url":"https://github.com/ajafff","@type":"Person","name":"ajafff"},"datePublished":"2021-01-11T18:30:06Z","url":"https://github.com/microsoft/TypeScript/issues/42285"}
| 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:9d3bf663-da2b-e17e-424b-d2cf9df729e9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C17E:2E5652:31C9251:3F77B99:6B01D3CA |
| html-safe-nonce | 1b83eb3e15544a61feba813b905020d3071dde6fad32eb4f2812f15e52d15a3b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMTdFOjJFNTY1MjozMUM5MjUxOjNGNzdCOTk6NkIwMUQzQ0EiLCJ2aXNpdG9yX2lkIjoiMTMzNzIxNTgwNDI5NDQxOTQwMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a8e541c2ad3fb8ba23946bba1b44e51c9409ef13032e37a9a60d628704f60097 |
| hovercard-subject-tag | issue:783600799 |
| 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/42285/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7371502140c93960cc360dfe3aabfb662ed04f8a89588dbbb415d4e2c34dfa58/microsoft/TypeScript/issues/42285 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7371502140c93960cc360dfe3aabfb662ed04f8a89588dbbb415d4e2c34dfa58/microsoft/TypeScript/issues/42285 |
| og:image:alt | Suggestion 🔍 Search Terms forEachChild closure ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't ch... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ajafff |
| 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