{"id":22686906,"url":"https://github.com/dcronqvist/inspectree","last_synced_at":"2025-03-29T15:41:12.286Z","repository":{"id":264999954,"uuid":"853896708","full_name":"dcronqvist/InspecTree","owner":"dcronqvist","description":"🔎 A .NET library for inspecting entire syntax trees of methods in C#","archived":false,"fork":false,"pushed_at":"2024-10-02T18:23:36.000Z","size":49,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-28T12:56:56.278Z","etag":null,"topics":["csharp","roslyn","source-generator"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dcronqvist.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-09-07T20:50:46.000Z","updated_at":"2024-10-02T18:33:30.000Z","dependencies_parsed_at":"2024-11-27T05:38:17.739Z","dependency_job_id":null,"html_url":"https://github.com/dcronqvist/InspecTree","commit_stats":null,"previous_names":["dcronqvist/inspectree"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcronqvist%2FInspecTree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcronqvist%2FInspecTree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcronqvist%2FInspecTree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcronqvist%2FInspecTree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dcronqvist","download_url":"https://codeload.github.com/dcronqvist/InspecTree/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246207488,"owners_count":20740723,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","roslyn","source-generator"],"created_at":"2024-12-09T23:16:02.360Z","updated_at":"2025-03-29T15:41:12.257Z","avatar_url":"https://github.com/dcronqvist.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🔎 InspecTree\n\nInspecTree is a library for inspecting entire syntax trees of lambda functions in C#. [`Expression\u003cT\u003e`](https://docs.microsoft.com/en-us/dotnet/api/system.linq.expressions.expression-1) is commonly used for this purpose, but it is limited to single expressions unless you manually construct a [`BlockExpression`](https://learn.microsoft.com/en-us/dotnet/api/system.linq.expressions.blockexpression). InspecTree allows you to inspect entire syntax trees of lambda functions, including entire statement bodies.\n\n## Quick example\n\nThis library was initially created with the intention of transpiling C# code to GLSL. Here's a small example of how to access the syntax tree of a lambda function to transpile it to GLSL.\n\n```csharp\n// Partial required due to source generator\n// creating an overload in the same class\npublic partial class Program\n{\n  public static void Main(string[] args)\n  {\n    var glslCode = TranspileToGLSL(() =\u003e \n    {\n      var x = 2 + 5f;\n      var color = new Vector4(1.0f);\n\n      if (x \u003e 4)\n      {\n        color = new Vector4(0.2f, 0.3f, x, 1.0f);\n      }\n\n      return color;\n    });\n  }\n\n  // Overload of this method is generated by source generator that\n  // accepts a Func\u003cVector4\u003e, and therefore accepts a lambda function\n  public static string TranspileToGLSL(InspecTree\u003cFunc\u003cVector4\u003e\u003e shader)\n  {\n    var transpiler = new GLSLTranspiler();\n    return transpiler.Transpile(shader.SyntaxTree);\n  }\n}\n```\n\nIf you want a more complete example of where this is done, check out the [InspecTree.Example](src/InspecTree.Example/Program.cs) project.\n\n## How it works\n\nInspecTree uses two source generators to make it possible to access both the syntax tree and semantic model of a lambda.\n\n- For each method that accepts an `InspecTree\u003cFunc\u003cT\u003e` parameter, it generates an overload that accepts a `Func\u003cT\u003e` parameter instead. This allows you to pass a lambda function to your method since there is no way to implicitly convert a lambda to a user-defined type.\n\n- For each invocation to one of the generated overloads, it generates an interceptor method that will be invoked instead. This interceptor method will create an `InspecTree\u003cFunc\u003cT\u003e\u003e` object from the `Func\u003cT\u003e` parameter and store the lambda function's syntax tree and semantic model.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdcronqvist%2Finspectree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdcronqvist%2Finspectree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdcronqvist%2Finspectree/lists"}