Authoring Custom Patterns

We recommend authoring Grit patterns in .md files, as this format allows you to combine documentation, GritQL, and source code examples in a single file.

VS Code

We recommend installing the Grit extension to get syntax highlighting and authoring assistance for Grit patterns.

You can watch this video to see the extension in action.

Authoring Walkthrough

Markdown Format

Patterns can be stored in *.md files within a .grit/patterns folder at the root of your repo.

  • The name of the file is used as the name of the pattern.
  • The first fenced code block in the file is used as the GritQL pattern body.
  • Subheadings are used for test cases, where each test case should have a before and after code block.
  • Cases which shouldn't be transformed should only have a single code block.
  • Within the sample patterns, you can use // @filename: example.js to represent multiple input/output files that should be tested as a group - like this example.

Example

You can find many examples in the Grit standard library.

Here is an example of a markdown pattern file:

YAML .grit/patterns/remove_console_log.md
# Remove console.log

Remove console.log in production code.

```grit
`console.log($_)` => .
```

## Test case one

```typescript
console.error("keep this");
console.log('remove this!');
```

```typescript
console.error("keep this");

```