Skip to content

How to insert a new AST Node if absent? #280

Answered by ketkarameya
ketkarameya asked this question in Q&A
Discussion options

You must be logged in to vote

The idea would be to match some attribute (which is not attributeA) inside ComponentA that does not contain a attributeA. This can be achieved by using rules.constraints.

[[rules]]
name= "update_componentA"
query = """(
(jsx_attribute (property_identifier) @name ) @attribute
(#not-eq @name "attributeA")
)"""
replace_node = "attribute"
replace = """
attributeA = {a}
@attribute
"""
[[rules.constraints]]
matcher= """(
(jsx_self_closing_element name: (_) @elem_name) @enclosing_jsx_elem
(#eq? @elem_name "ComponentA")
)
"""
queries = ["""(
(jsx_attribute (property_identifier)@p ) @j
(#eq? @p "attributeA")
)"""]

Replies: 3 comments 6 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by ketkarameya
Comment options

You must be logged in to vote
5 replies
@gregintuitaio
Comment options

@ketkarameya
Comment options

@lazaroclapp
Comment options

@ketkarameya
Comment options

@ketkarameya
Comment options

Comment options

You must be logged in to vote
1 reply
@gregintuitaio
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants