Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Implement more powerful fix methods #656

Open
4 of 5 tasks
HerringtonDarkholme opened this issue Oct 3, 2023 · 2 comments
Open
4 of 5 tasks

[feature] Implement more powerful fix methods #656

HerringtonDarkholme opened this issue Oct 3, 2023 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@HerringtonDarkholme
Copy link
Member

HerringtonDarkholme commented Oct 3, 2023

This is an epic issue to track more powerful fix methods

@HerringtonDarkholme HerringtonDarkholme added the enhancement New feature or request label Oct 3, 2023
@HerringtonDarkholme
Copy link
Member Author

HerringtonDarkholme commented Dec 3, 2023

Expand node selection range

⭐ Suggestion

ast-grep rule can only fix one target node at one time. This works fine for function statement/calls but it has always been problematic for list-item like items in an array, key-value pairs in a dictionary. We cannot delete an item because we also need to delete the surrounding comma.

Similarly, the problem also applies to a statement if it has a semicolon.

💻 Use Cases

Add the expandTarget field to rule.

language: python
rule:
  kind: pair
  has:
   find: regex
expandTarget:
  forward: 1 # forward one byte?
  forward: 
    regex: , # or a rule object
fix: '' # delete

related: #746

The expand struct

expandForward:  # relation
  regex: ','
  stopBy: end

@HerringtonDarkholme
Copy link
Member Author

Add prepend/append statements in fix

⭐ Suggestion

It is usually desirable to prepend/append extra content to a file during a code fix.
For example, setting up a top-level variable, importing a function and etc.

💻 Use Cases

Suppose we want to extract some repetitive code.

const result = () => {
  doSomeRepetitiveCode()
}

to

import { importedUtil } from 'utils' // note this line
const result = importedUtil()

The first line should import the extracted util function from another file.


Change console.log to logger.log

console.log('message')

to

import logger from 'logger'
logger.log('message')

@HerringtonDarkholme HerringtonDarkholme changed the title [feature] add more fix methods to change file [feature] Implement more powerful fix methods Dec 3, 2023
HerringtonDarkholme added a commit that referenced this issue Dec 3, 2023
HerringtonDarkholme added a commit that referenced this issue Dec 4, 2023
@HerringtonDarkholme HerringtonDarkholme self-assigned this Dec 21, 2023
HerringtonDarkholme added a commit to ast-grep/ast-grep.github.io that referenced this issue Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: In Progress
Development

No branches or pull requests

1 participant