Pattern match on multiple language document (e.g., Vue SFC file) #978
-
I am sorry for another question in a short time. After some investigation, I found there is a tree-sitter feature called How can I match patterns inside script tag syntactically in Vue SFC files? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
thanks for using ast-grep! Any idea or suggestion is welcome! |
Beta Was this translation helpful? Give feedback.
-
You can comment <style> and <template> block first. id: setup-sfc start comment
language: html
rule:
kind: script_element
has:
kind: start_tag
pattern: $START
precedes:
kind: raw_text
pattern: $RAW
precedes:
kind: end_tag
pattern: $END
inside:
kind: fragment
fix: |-
$START*/
$RAW/*$END id: setup-sfc end comment
language: html
rule:
kind: fragment
pattern: $A
has:
kind: script_element
fix: /*$A*/ Result/*<script setup>*/
defineProps<{
foo: string
}>()
/*</script>
<template>
<div>{foo}<div>
</template>*/ After conversion, clean these comments. id: setup-sfc clean comment
language: tsx
rule:
kind: comment
regex: /\*</?script|<script.*>\*/$
pattern: $A
inside:
kind: program
transform:
B:
substring:
startChar: 2
endChar: -2
source: $A
C:
replace:
replace: \*\\/
by: '*/'
source: $B
fix: $C Example |
Beta Was this translation helpful? Give feedback.
You can comment <style> and <template> block first.
Result
After conversion, clean these comments.