Adding typst support to ast-grep (and some package statistics)

Background
ast-grep is a code searching tool that understands AST (abstract syntax tree). I’ve compiled GitHub - uben0/tree-sitter-typst: Tree Sitter grammar for Typst into dynamic libraries (.so/.dll/.wasm) to add typst support to ast-grep.

So now, you can write simple yaml rule to match typst syntax patterns.
The following is an example rule that

  • matches :zombie: show-function rules

    They are hard to override and should be avoided if possible, especially for templates. Please refer to #4591 (comment) for further info.

  • but doesn’t match :smiley_cat: show-set or set rules

    They are best practices.

  • and doesn’t match :see_no_evil: some acceptable one-off hacks.

= 😺 Best practice // Not matched
#show heading: set text(red)
#let template(body) = {
  show heading: set text(red)
  body
}
#show: template

= 🧟 Hard to override // Matched
#show heading: text.with(red)
#show heading: it => { set text(red); it }
#let foo(it) = { set text(red); it }
#show heading: foo

#let template(body) = {
  show emph: repr
  body
}
#show: template

= 🙈 Acceptable one-off hack // Not matched
#show heading: none
#show "badly": "great"
#show heading: [B]

More example rules can be found at GitHub - YDX-2147483647/ast-grep-typst: Typst rules based on ast-grep.


The biggest problem at present: It’s not of much use…

Fun facts

Packages (with versions) sorted by number of imports by other packages

    235 "@preview/i-figured:0.2.4"
    144 "@preview/numbly:0.1.0"
     92 "@preview/cetz:0.2.2"
     89 "@preview/cuti:0.3.0"
     66 "@preview/fontawesome:0.4.0"
     64 "@preview/cetz:0.3.2"
     62 "@preview/cuti:0.2.1"
     60 "@preview/outrageous:0.1.0"
     59 "@preview/cetz:0.3.1"
     58 "@preview/cetz:0.3.4"
     56 "@preview/codly:1.3.0"
     50 "@preview/touying:0.6.1"
     48 "@preview/tidy:0.2.0"
     48 "@preview/tablex:0.0.8"
     40 "@preview/fontawesome:0.2.1"
     40 "@preview/codly:1.0.0"
     38 "@preview/anti-matter:0.0.2"
     34 "@preview/codelst:2.0.1"
     33 "@preview/oxifmt:0.2.0"
     33 "@preview/codly-languages:0.1.8"
     32 "@preview/tidy:0.3.0"
     32 "@preview/codelst:2.0.2"
     32 "@preview/cetz:0.4.0"
     31 "@preview/hydra:0.6.1"
     31 "@preview/cetz:0.1.2"
     30 "@preview/touying:0.5.3"
     30 "@preview/physica:0.9.5"
     30 "@preview/fontawesome:0.5.0"
…
Script
ast-grep scan ../packages/packages/preview/ --filter=deps --json | jq '.[].metaVariables.multi.secondary[0].text' -r | tee deps.txt

nvim deps.txt # Remove the “Tufte” record, which exists because tufte-memo/0.1.0/template/main.typ dropped a quotation mark in `#import "@preview/tufte-memo:0.1.0: *`

sort deps.txt | uniq --count | sort --numeric-sort --reverse

Packages sorted by number of direct show-function rules in all versions

235, scholarly-tauthesis
200, touying
160, modern-sjtu-thesis
145, qooklet
136, modern-uit-thesis
126, cheda-seu-thesis
108, shuosc-shu-bachelor-thesis
108, fletcher
105, supercharged-dhbw
104, unofficial-fhict-document-template
102, ilm
93, modern-cug-thesis
86, shane-hhu-thesis
83, versatile-apa
76, modern-shu-thesis
73, mantys
72, modern-sysu-thesis
65, modern-nju-thesis
64, codly
62, unofficial-sdu-thesis
60, g-exam
58, fh-joanneum-iit-thesis
57, tracl
57, flow
56, universal-hit-thesis
56, modern-ecnu-thesis
55, tntt
55, scholarly-epfl-thesis
53, vienna-tech
53, modern-szu-thesis
52, fuzzy-cnoi-statement
52, ape
47, glossarium
45, thesist
45, quick-minutes
45, modern-hsh-thesis
45, definitely-not-isec-thesis
43, flyingcircus
43, classic-aau-report
42, wenyuan-campaign
42, outrageous
42, orange-book
42, isc-hei-report
42, htlwienwest-da
42, clean-dhbw
41, tuhi-booklet-vuw
41, parcio-thesis
40, silky-report-insa
40, equate
…
Script
ls ../packages/packages/preview/  \
  | xargs -I '{}' sh -c \
    "ast-grep scan ../packages/packages/preview/{} --filter=show-function --json | jq -j '. | length' && echo ', {}'" \
  | tee show-function.csv

cat show-function.csv | sort --numeric-sort --reverse
5 Likes

Somehow, Linter · Issue #151 · typst/typst · GitHub is brought up again. Linting might be a possible usage.

1 Like