Title: Using createVariableStatement in a compiler transformer breaks the compiler · Issue #22372 · microsoft/TypeScript · GitHub
Open Graph Title: Using createVariableStatement in a compiler transformer breaks the compiler · Issue #22372 · microsoft/TypeScript
X Title: Using createVariableStatement in a compiler transformer breaks the compiler · Issue #22372 · microsoft/TypeScript
Description: TypeScript Version: 2.8.0-dev.20180307 Search Terms: createVariableStatement const Code (compiler) // A *self-contained* demonstration of the problem follows... import * as ts from 'typescript'; function transformer(program: ts.Program):...
Open Graph Description: TypeScript Version: 2.8.0-dev.20180307 Search Terms: createVariableStatement const Code (compiler) // A *self-contained* demonstration of the problem follows... import * as ts from 'typescript'; fu...
X Description: TypeScript Version: 2.8.0-dev.20180307 Search Terms: createVariableStatement const Code (compiler) // A *self-contained* demonstration of the problem follows... import * as ts from 'typescript&...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/22372
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Using createVariableStatement in a compiler transformer breaks the compiler","articleBody":"**TypeScript Version:** 2.8.0-dev.20180307\r\n\r\n**Search Terms:** createVariableStatement const\r\n\r\n**Code (compiler)**\r\n\r\n```ts\r\n// A *self-contained* demonstration of the problem follows...\r\nimport * as ts from 'typescript';\r\n\r\nfunction transformer(program: ts.Program): ts.TransformerFactory\u003cts.SourceFile> {\r\n return (context: ts.TransformationContext) => (file: ts.SourceFile) => transformFile(program, context, file);\r\n}\r\n\r\nfunction transformFile(program: ts.Program, context: ts.TransformationContext, file: ts.SourceFile): ts.SourceFile {\r\n const transformedFile = ts.visitEachChild(file, child => visit(child, context, file), context);\r\n return transformedFile;\r\n}\r\nfunction visit(node: ts.Node, context: ts.TransformationContext, file: ts.SourceFile): ts.Node {\r\n if (ts.isMethodDeclaration(node)) {\r\n const newNode = ts.createMethod(\r\n [ts.createToken(ts.SyntaxKind.StaticKeyword)], \r\n [], \r\n null, \r\n node.name, \r\n null, \r\n [], \r\n node.parameters, \r\n node.type, \r\n ts.createBlock([ \r\n ts.createVariableStatement(\r\n [ts.createToken(ts.SyntaxKind.ConstKeyword)], \r\n [ts.createVariableDeclaration('myConst', null, ts.createLiteral('value'))]\r\n ),\r\n ...(node.body ? node.body.statements : [])\r\n ])\r\n );\r\n console.log(ts.createPrinter().printNode(ts.EmitHint.Unspecified, newNode, file));\r\n return newNode;\r\n }\r\n return ts.visitEachChild(node, child => visit(child, context, file), context);\r\n}\r\n\r\nconst program = ts.createProgram([\r\n '../transformer-issue-src/src/A.ts'\r\n], {\r\n target: ts.ScriptTarget.ES5,\r\n module: ts.ModuleKind.CommonJS,\r\n moduleResolution: ts.ModuleResolutionKind.NodeJs,\r\n importHelpers: true,\r\n alwaysStrict: true,\r\n noImplicitAny: true,\r\n noImplicitThis: true,\r\n removeComments: true,\r\n sourceMap: true,\r\n outDir: \"../transformer-issue-src/lib\",\r\n declaration: true,\r\n declarationDir: \"../transformer-issue-src/lib\",\r\n lib: [\r\n \"lib.es2017.d.ts\",\r\n ],\r\n experimentalDecorators: true,\r\n noEmitOnError: true,\r\n});\r\n\r\nconst transformers = {\r\n before: [\r\n transformer(program),\r\n ]\r\n}\r\nconst result = program.emit(undefined, undefined, undefined, false, transformers);\r\n```\r\n\r\n**Code (test program, ../transformer-issue-src/src/A.ts)**\r\n\r\n```ts\r\nexport class A {\r\n static myMethod() {\r\n return 'value';\r\n }\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\nThe .js file should be emitted.\r\nThe `console.log` statement should print the following:\r\n\r\n```ts\r\nstatic myMethod() { const myConst = \"value\"; return 'value'; }\r\n```\r\n\r\n**Actual behavior:**\r\nThe .js file is not emitted due to a compiler exception. Message and stack trace:\r\n\r\n```\r\nTypeError: Cannot read property 'transformFlags' of null\r\n at aggregateTransformFlagsForNode (d:\\solutions\\transformer-issue-compiler\\node_modules\\typescript\\lib\\typescript.js:54626:18)\r\n at Object.aggregateTransformFlags (d:\\solutions\\transformer-issue-compiler\\node_modules\\typescript\\lib\\typescript.js:54611:9)\r\n at visitNode (d:\\solutions\\transformer-issue-compiler\\node_modules\\typescript\\lib\\typescript.js:53754:12)\r\n at Object.visitEachChild (d:\\solutions\\transformer-issue-compiler\\node_modules\\typescript\\lib\\typescript.js:54053:108)\r\n at visitVariableDeclaration (d:\\solutions\\transformer-issue-compiler\\node_modules\\typescript\\lib\\typescript.js:61625:30)\r\n at Object.flatMap (d:\\solutions\\transformer-issue-compiler\\node_modules\\typescript\\lib\\typescript.js:2047:25)\r\n at visitVariableDeclarationList (d:\\solutions\\transformer-issue-compiler\\node_modules\\typescript\\lib\\typescript.js:61510:39)\r\n at visitJavaScript (d:\\solutions\\transformer-issue-compiler\\node_modules\\typescript\\lib\\typescript.js:60282:28)\r\n at visitor (d:\\solutions\\transformer-issue-compiler\\node_modules\\typescript\\lib\\typescript.js:60243:24)\r\n at visitNode (d:\\solutions\\transformer-issue-compiler\\node_modules\\typescript\\lib\\typescript.js:53755:23)\r\n```\r\n\r\nEven if I remove the `ts.createToken(ts.SyntaxKind.ConstKeyword)` token, this exception is still thrown.\r\n\r\nAlso, the `console.log` statement prints the following (the var keyword is left there):\r\n\r\n```ts\r\nstatic myMethod() { const var myConst = \"value\"; return 'value'; }\r\n```","author":{"url":"https://github.com/pedro-pedrosa","@type":"Person","name":"pedro-pedrosa"},"datePublished":"2018-03-07T11:15:49Z","url":"https://github.com/microsoft/TypeScript/issues/22372"}
| 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:08f3aece-81e2-3a3f-33ca-e5fea0f1da76 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AC6E:C8FAB:BE6DF8:1038E77:6B04896E |
| html-safe-nonce | d3e75ef1f0c39c95fa51f6de37013ae02d837cb951a92d50cc9ab8ddff3d90e8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQzZFOkM4RkFCOkJFNkRGODoxMDM4RTc3OjZCMDQ4OTZFIiwidmlzaXRvcl9pZCI6IjE3NDExOTI3MjcyNzk5OTUyNDYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 153d504f605367781a178a0a2469b41378a474ad45596ae0ed0079816a5d5c1a |
| hovercard-subject-tag | issue:303058340 |
| 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/22372/issue_layout |
| twitter:image | https://opengraph.githubassets.com/da4e685edb7a43f749dce21a84712423684e0c5e85383b86204e3647e8afc027/microsoft/TypeScript/issues/22372 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/da4e685edb7a43f749dce21a84712423684e0c5e85383b86204e3647e8afc027/microsoft/TypeScript/issues/22372 |
| og:image:alt | TypeScript Version: 2.8.0-dev.20180307 Search Terms: createVariableStatement const Code (compiler) // A *self-contained* demonstration of the problem follows... import * as ts from 'typescript'; fu... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pedro-pedrosa |
| hostname | github.com |
| expected-hostname | github.com |
| None | 1d5c152376e38b7d2e37f3d5179be29a73bee93b85110da28a4eebe1159cdc70 |
| 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 | f5017e8ff4ea25a8011fcf0f2a0e430c60dc86c8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width