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

Allow to use meta variable captured outside of rewrite rule inside the rewriter #1072

Closed
ptorbynskyy opened this issue Apr 21, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@ptorbynskyy
Copy link

⭐ Suggestion

I would like to be able to use meta variable that captured outside of rewrite rule inside it.

💻 Use Cases

Here is my case
I tried to replace spread with set of calls.

Source:

createService(
    ...c.resolve("A", "B", "С")
)

Goal:

createService(
    c.resolve("A"),
    c.resolve("B"),
    c.resolve("C")
)

Rule:

# YAML Rule is more powerful!
# https://ast-grep.github.io/guide/rule-config.html#rule
rule:
   kind: spread_element
   has: 
      kind: call_expression
      pattern: $NAME.resolve($$$IDENTS)
rewriters:
  - id: rewrite-identifer
    rule:
      pattern: $IDENT
      kind: string
    fix: $NAME.resolve($IDENT)
transform:
  RESOLVES:
    rewrite:
      rewriters: [rewrite-identifer]
      source: $$$IDENTS
      joinBy: "\n"
fix: $RESOLVES
image

$NAME is empty inside the rewriters.
I suggest to make external context available.

Additional Links:

@ptorbynskyy ptorbynskyy added the enhancement New feature or request label Apr 21, 2024
@HerringtonDarkholme
Copy link
Member

thanks for reporting. I will try to implement it in 0.21.x releases

@HerringtonDarkholme
Copy link
Member

HerringtonDarkholme commented Apr 30, 2024

  • Pass variables from outside to rewriters
  • Pass captured AST nodes to rewriters fix

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
None yet
Development

No branches or pull requests

2 participants