|
| 1 | +# Config Cheat Sheet |
| 2 | + |
| 3 | +This cheat sheet provides a concise overview of ast-grep's linter rule YAML configuration. It's designed as a handy reference for common usage. |
| 4 | + |
| 5 | +<script setup> |
| 6 | +import CheatSheet from '../src/cheatsheet/SheetTable.vue' |
| 7 | +import Item from '../src/cheatsheet/Item.vue' |
| 8 | +</script> |
| 9 | + |
| 10 | + |
| 11 | +## Basic Information |
| 12 | + |
| 13 | +Core details that identify and define your rule and miscellaneous keys for documentation and custom data. |
| 14 | + |
| 15 | +<CheatSheet title="ℹ️ Basic Information" variant="info"> |
| 16 | +<Item> |
| 17 | + |
| 18 | +```yaml |
| 19 | +id: no-console-log |
| 20 | +``` |
| 21 | +
|
| 22 | +🆔 A unique, descriptive identifier for the rule. |
| 23 | +
|
| 24 | +</Item> |
| 25 | +
|
| 26 | +<Item> |
| 27 | +
|
| 28 | +```yaml |
| 29 | +language: JavaScript |
| 30 | +``` |
| 31 | +
|
| 32 | +🌐 The programming language the rule applies to. |
| 33 | +
|
| 34 | +</Item> |
| 35 | +
|
| 36 | +
|
| 37 | +<Item> |
| 38 | +
|
| 39 | +```yaml |
| 40 | +url: 'https://doc.link/' |
| 41 | +``` |
| 42 | +🔗 A URL to the rule's documentation. |
| 43 | +
|
| 44 | +</Item> |
| 45 | +
|
| 46 | +
|
| 47 | +<Item> |
| 48 | +
|
| 49 | +```yaml |
| 50 | +metadata: { author: 'John Doe' } |
| 51 | +``` |
| 52 | +
|
| 53 | +📓 metadata A dictionary for custom data related to the rule. |
| 54 | +
|
| 55 | +</Item> |
| 56 | +
|
| 57 | +</CheatSheet> |
| 58 | +
|
| 59 | +## Finding |
| 60 | +
|
| 61 | +Keys for specifying what code to search for. |
| 62 | +
|
| 63 | +<CheatSheet title="🔍 Finding Code" variant="danger"> |
| 64 | +
|
| 65 | +<Item> |
| 66 | +
|
| 67 | +```yaml |
| 68 | +rule: |
| 69 | + pattern: 'console.log($$$ARGS)' |
| 70 | +``` |
| 71 | +
|
| 72 | +🎯 The core `rule` to find matching AST nodes. |
| 73 | + |
| 74 | +</Item> |
| 75 | + |
| 76 | +<Item> |
| 77 | + |
| 78 | +```yaml |
| 79 | +constraints: |
| 80 | + ARG: { kind: 'string' } } |
| 81 | +``` |
| 82 | + |
| 83 | +⚙️ Additional `constraints` rules to filter meta-variable matches. |
| 84 | + |
| 85 | +</Item> |
| 86 | + |
| 87 | +<Item> |
| 88 | + |
| 89 | +```yaml |
| 90 | +utils: |
| 91 | + is-react: |
| 92 | + kind: function_declaration |
| 93 | + has: { kind: jsx_element } |
| 94 | +``` |
| 95 | + |
| 96 | +🛠️ A dictionary of reusable utility rules. Use them in `matches` to modularize your rules. |
| 97 | + |
| 98 | +</Item> |
| 99 | + |
| 100 | +</CheatSheet> |
| 101 | + |
| 102 | + |
| 103 | +## Patching |
| 104 | + |
| 105 | +Keys for defining how to automatically fix the found code. |
| 106 | + |
| 107 | + |
| 108 | +<CheatSheet title="🛠️ Patching Code" variant="tip"> |
| 109 | + |
| 110 | +<Item> |
| 111 | + |
| 112 | +```yaml |
| 113 | +transform: |
| 114 | + NEW_VAR: |
| 115 | + substring: {endChar: 1, source: $V} |
| 116 | +``` |
| 117 | + |
| 118 | +🎩 `transform` meta-variables before they are used in `fix`. |
| 119 | + |
| 120 | +</Item> |
| 121 | + |
| 122 | +<Item> |
| 123 | + |
| 124 | +```yaml |
| 125 | +transform: |
| 126 | + NEW_VAR: substring($V, endChar=1) |
| 127 | +``` |
| 128 | + |
| 129 | +🎩 `transform` also accepts string form. |
| 130 | + |
| 131 | +</Item> |
| 132 | + |
| 133 | +<Item> |
| 134 | + |
| 135 | +```yaml |
| 136 | +fix: "logger.log($$$ARGS)" |
| 137 | +``` |
| 138 | + |
| 139 | +🔧 A `fix` string to auto-fix the matched code. |
| 140 | + |
| 141 | +</Item> |
| 142 | + |
| 143 | +<Item> |
| 144 | + |
| 145 | +```yaml |
| 146 | +fix: |
| 147 | + template: "logger.log($$$ARGS)" |
| 148 | + expandEnd: rule |
| 149 | +``` |
| 150 | + |
| 151 | +🔧 Fix also accepts `FixConfig` object. |
| 152 | + |
| 153 | +</Item> |
| 154 | + |
| 155 | +<Item> |
| 156 | + |
| 157 | +```yaml |
| 158 | +rewriters: |
| 159 | +- id: remove-quotes |
| 160 | + rule: { pattern: "'$A'" } |
| 161 | + fix: "$A" |
| 162 | +``` |
| 163 | + |
| 164 | +✍️ A list of `rewriters` for complex transformations. |
| 165 | + |
| 166 | +</Item> |
| 167 | + |
| 168 | +</CheatSheet> |
| 169 | + |
| 170 | +## Linting |
| 171 | + |
| 172 | +Keys for configuring the messages and severity of reported issues. |
| 173 | + |
| 174 | +<CheatSheet title="🚦 Linting" variant="warning"> |
| 175 | + |
| 176 | +<Item> |
| 177 | + |
| 178 | +```yaml |
| 179 | +severity: warning |
| 180 | +``` |
| 181 | + |
| 182 | +⚠️ The `severity` level of the linting message. |
| 183 | + |
| 184 | +</Item> |
| 185 | + |
| 186 | +<Item> |
| 187 | + |
| 188 | +```yaml |
| 189 | +message: "Avoid using $MATCH in production." |
| 190 | +``` |
| 191 | + |
| 192 | +💬 A concise `message` explaining the rule. Matched $VAR can be used. |
| 193 | + |
| 194 | +</Item> |
| 195 | + |
| 196 | +<Item> |
| 197 | + |
| 198 | +```yaml |
| 199 | +note: |
| 200 | + Use a _logger_ instead of `console` |
| 201 | +``` |
| 202 | + |
| 203 | +📌 More detailed `note`. It supports Markdown format. |
| 204 | + |
| 205 | +</Item> |
| 206 | + |
| 207 | +<Item> |
| 208 | + |
| 209 | +```yaml |
| 210 | +labels: |
| 211 | + ARG: |
| 212 | + style: 'primary' |
| 213 | + message: 'The argument to log' |
| 214 | +``` |
| 215 | +
|
| 216 | +🎨 Customized `labels` for highlighting parts of the matched code. |
| 217 | + |
| 218 | +</Item> |
| 219 | + |
| 220 | +<Item> |
| 221 | + |
| 222 | +```yaml |
| 223 | +files: ['src/**/*.js'] |
| 224 | +``` |
| 225 | + |
| 226 | +✅ Glob `files` patterns to include files for the rule. |
| 227 | + |
| 228 | +</Item> |
| 229 | + |
| 230 | +<Item> |
| 231 | + |
| 232 | +```yaml |
| 233 | +ignores: ['test/**/*.js'] |
| 234 | +``` |
| 235 | + |
| 236 | +❌ Glob patterns to exclude files from the rule. |
| 237 | + |
| 238 | +</Item> |
| 239 | + |
| 240 | +</CheatSheet> |
0 commit comments