5 releases

0.1.1 Apr 3, 2023
0.1.1-rc.3 Apr 1, 2023
0.1.0 Mar 27, 2023

#822 in Programming languages

Download history 26/week @ 2024-03-11 35/week @ 2024-03-18 48/week @ 2024-03-25 55/week @ 2024-04-01 21/week @ 2024-04-08 27/week @ 2024-04-15 33/week @ 2024-04-22 26/week @ 2024-04-29 35/week @ 2024-05-06 22/week @ 2024-05-13 27/week @ 2024-05-20 28/week @ 2024-05-27 22/week @ 2024-06-03 23/week @ 2024-06-10 25/week @ 2024-06-17 33/week @ 2024-06-24

103 downloads per month
Used in 11 crates

MIT license

56KB
1.5K SLoC

Mogglo

Mogglo is a multi-language AST-based code search and rewriting tool. Mogglo supports embedding Lua code in search patterns and replacements.

Mogglo focuses on the following goals:

  • Minimal setup: Mogglo will work right away on any codebase in a supported language.
  • Many languages: 12 and counting!
  • High-quality grammars: Mogglo is based on tree-sitter grammars.
  • Lua: Mogglo exposes a rich API to embedded Lua snippets.

Introduction

The following examples give a taste of Mogglo. Here's how to find pointless assignments of an expression to itself:

mogglo-rust --detail 'let $x = $x;' ./**/*.rs

Lua code is wrapped in braces. Lua can recursively match patterns with rec. Here's a pattern to detect out-of-bounds array accesses:

mogglo-rust 'while $i <= $buf.len() { ${{ rec("$buf.get($i)") }} }' ./**/*.rs

Here's how to unroll a simple loop:

mogglo-rust \
  'for $i in 0..$h { $b; }' \
  --where 'h_num = tonumber(h); return h_num ~= nil and h_num % 4 == 0' \
  --replace 'for $i in 0..${{ string.format("%.0f", h / 4) }} { $b; $b; $b; $b; }' \
  ./*/**.rs

This transformation demonstrates the power of using Lua: it can't be done with regular expression substitutions and would be very difficult with other codemod tools.

See the documentation for more details!

Dependencies

~6–8.5MB
~177K SLoC