|
| 1 | +--- |
| 2 | +author: |
| 3 | + - name: Herrington Darkholme |
| 4 | +date: 2025-06-07 |
| 5 | +head: |
| 6 | + - - meta |
| 7 | + - property: og:type |
| 8 | + content: website |
| 9 | + - - meta |
| 10 | + - property: og:title |
| 11 | + content: Interactive Code Fixes with Multiple Options! |
| 12 | + - - meta |
| 13 | + - property: og:url |
| 14 | + content: https://ast-grep.github.io/blog/interactive-demo.html |
| 15 | + - - meta |
| 16 | + - property: og:description |
| 17 | + content: Today, we're thrilled to showcase a game-changing feature, multi-option interactive code fixes! |
| 18 | +--- |
| 19 | + |
| 20 | +# Interactive Code Fixes with Multiple Options! |
| 21 | + |
| 22 | +Today, we're thrilled to showcase a game-changing feature: **multi-option interactive code fixes!** |
| 23 | + |
| 24 | + |
| 25 | +<iframe style="width:100%;aspect-ratio:16/9;" src="https://www.youtube.com/embed/6m0xTwYaM_A?si=-ywQmlg6Wrg0FfGq" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe> |
| 26 | + |
| 27 | +## Beyond Simple Search: Interactive Refactoring |
| 28 | + |
| 29 | +ast-grep's interactive mode, activated with `ast-grep scan --interactive`, transforms code analysis into a dynamic, actionable workflow. When a rule identifies a pattern match, you're presented with: |
| 30 | + |
| 31 | +1. **Clear Diff Views:** Instantly see the problematic code (red) and the proposed change (green), making it easy to understand the impact of the fix. |
| 32 | +2. **Contextual Markdown Notes:** Rules can embed rich Markdown notes, providing instant documentation, best practices, and explanations directly in your terminal – no need to jump to external docs. |
| 33 | + |
| 34 | +## The Power of Choice: Multiple Fix Options |
| 35 | + |
| 36 | +This is where ast-grep truly stands out. Instead of a single, rigid fix, many rules now offer **multiple, intelligent remediation options**. |
| 37 | + |
| 38 | +**How it works:** |
| 39 | + |
| 40 | +* When a match is found, ast-grep displays a list of available fixes. |
| 41 | +* Simply use the **`tab` key** to cycle through the different fix proposals. |
| 42 | +* Once you've found the ideal solution, hit `Enter` to apply it. |
| 43 | + |
| 44 | +This flexibility allows you to choose the fix that best aligns with your project's coding standards or specific refactoring goals. |
| 45 | + |
| 46 | +## Real-World Example: Angular `@Input()` Optionality |
| 47 | + |
| 48 | +Consider a common TypeScript scenario in Angular: an `@Input()` decorator where the component property is typed as `string`, but it's optional by default (meaning it could be `undefined`). |
| 49 | + |
| 50 | +ast-grep's rule for this issue intelligently offers two distinct fixes: |
| 51 | + |
| 52 | +1. **Add `undefined` to Type:** Transforms `test: string;` to `test: string | undefined;`, explicitly acknowledging the optionality in the type system. |
| 53 | +2. **Make Input Required:** Adds `{ required: true }` to the `@Input()` decorator, enforcing that the input must always be provided. |
| 54 | + |
| 55 | +You choose the solution that fits your use case, and ast-grep applies the transformation seamlessly. |
| 56 | + |
| 57 | +## Behind the Scenes: Configurable Fixes |
| 58 | + |
| 59 | +This powerful multi-fix capability is driven by the rule's YAML configuration. Rules can define an array of `fix` templates, each with a unique `title` and `template`, allowing rule authors to provide comprehensive repair options. |
| 60 | + |
| 61 | + |
| 62 | +### Streamline Your Workflow Today! |
| 63 | + |
| 64 | +Ast-grep with its interactive multi-fix feature is a game-changer for maintaining code quality, enforcing standards, and accelerating large-scale code transformations. It puts the power of intelligent, context-aware refactoring directly into your hands. |
| 65 | + |
| 66 | +**Ready to refactor like a pro?** |
| 67 | +Give ast-grep a try and experience the future of code analysis and transformation! |
0 commit comments