Skip to content

Adds a walkthrough for lute transform#948

Open
skberkeley wants to merge 3 commits intoprimaryfrom
transform-docs
Open

Adds a walkthrough for lute transform#948
skberkeley wants to merge 3 commits intoprimaryfrom
transform-docs

Conversation

@skberkeley
Copy link
Copy Markdown
Contributor

No description provided.

@skberkeley skberkeley self-assigned this Apr 7, 2026
@skberkeley skberkeley added the documentation Improvements or additions to documentation label Apr 7, 2026
Copy link
Copy Markdown
Contributor

@hgoldstein hgoldstein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meta note: let's make sure all of the examples type check.

---

# Code Transformations
One of the coolest parts of Lute is that we've exposed many of the Luau internals programmatically. That means that things like types, require-resolution, the Luau AST(Abstract Syntax Tree), Luau bytecode, and so on are exposed with public facing apis that you can use. Of these features, one of the slickest things we've exposed is the ability to write automatic code transformations. Using Lute libraries, you can now programmatically describe how to change Luau source code and `lute transform` will handle running these transformations for you. In the following sections, we'll walk through writing a simple code transformation which doubles any numbers in the source program (we assume some basic level of familiarity with what an [AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree) is). As in the previous library, we'll be creating a new directory, with one file - `transform.luau`. Make sure to run `lute setup` in this directory!
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
One of the coolest parts of Lute is that we've exposed many of the Luau internals programmatically. That means that things like types, require-resolution, the Luau AST(Abstract Syntax Tree), Luau bytecode, and so on are exposed with public facing apis that you can use. Of these features, one of the slickest things we've exposed is the ability to write automatic code transformations. Using Lute libraries, you can now programmatically describe how to change Luau source code and `lute transform` will handle running these transformations for you. In the following sections, we'll walk through writing a simple code transformation which doubles any numbers in the source program (we assume some basic level of familiarity with what an [AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree) is). As in the previous library, we'll be creating a new directory, with one file - `transform.luau`. Make sure to run `lute setup` in this directory!
One of the coolest parts of Lute is that we've exposed parts of Luau like types, require-resolution, the Luau AST (abstract syntax tree), and Luau bytecode through public facing APIs. We've built a tool to write automatic, and deterministic, code transformations on top of these APIs. Using Lute libraries, you can now programmatically describe how to change Luau source code and `lute transform` will handle running these transformations for you. In the following sections, we'll walk through writing a simple code transformation which doubles any numbers in the source program (we assume some basic level of familiarity with what an [AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree) is). As in the previous library, we'll be creating a new directory, with one file - `transform.luau`. Make sure to run `lute setup` in this directory!

}
```

Note that AST's are immutable - rather than mutating them in place, we specify the changes we would like to make using a table of replacements and `lute transform` applies them for us.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I would love to embed a note here about string replacements versus node replacements. Is there a way to do footnotes?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do:

:::info
Your info block here
:::

If you want to highlight this fact inline? We removed node replacements.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I meant: most people when they look at this API think "hey what if I want to build a new node" because that's what you do in other libraries of this kind. Adding a note of why that's not the API and that we may add something like that in the future, but it would be different than actually building the node by hand.


Note that AST's are immutable - rather than mutating them in place, we specify the changes we would like to make using a table of replacements and `lute transform` applies them for us.

Lute provides two ways of writing transforms - a declarative, query based approach, and a visitor based approach. Since we recommend using the query based approach, we'll focus on that, and show how to write a visitor based approach at the end.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Lute provides two ways of writing transforms - a declarative, query based approach, and a visitor based approach. Since we recommend using the query based approach, we'll focus on that, and show how to write a visitor based approach at the end.
Lute provides two ways of writing transforms: a query based approach and a visitor based approach. Most transformations can be done with queries. We'll start with that and later write a visitor based transformation.


### Query Based AST transforms

In this section, we build up an example of a declarative 'query' based implementation which doubles any numbers found in the source program. We begin by selecting every AST node we're interested in - in this case, every number:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In this section, we build up an example of a declarative 'query' based implementation which doubles any numbers found in the source program. We begin by selecting every AST node we're interested in - in this case, every number:
In this section, we build up an example of a query based implementation which doubles any numbers found in the source program. We begin by selecting every AST node we're interested in - in this case, every number:


## [Linting](../../cli/lint/index.md)

lute lint is a programmable linter for Luau code, shipped as part of Lute. It is programmable - you can write custom rules for your repository in Luau. It ships with a number of builtin rules, which you can run on your current directory with `lute lint`. For more information, check out the [lint documentation](../../cli/lint/index.md)!
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
lute lint is a programmable linter for Luau code, shipped as part of Lute. It is programmable - you can write custom rules for your repository in Luau. It ships with a number of builtin rules, which you can run on your current directory with `lute lint`. For more information, check out the [lint documentation](../../cli/lint/index.md)!
`lute lint` is a programmable linter for Luau code, shipped as part of Lute. It is programmable - you can write custom rules for your repository in Luau. It ships with a number of builtin rules, which you can run on your current directory with `lute lint`. For more information, check out the [lint documentation](../../cli/lint/index.md)!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants