Skip to content

Commit 58487a4

Browse files
committed
refactor: refactor parser-script
1 parent 41948b6 commit 58487a4

File tree

3 files changed

+145
-144
lines changed

3 files changed

+145
-144
lines changed

packages/core/parser/__test__/parser-script-binding.spec.ts

Whitespace-only changes.
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
export const astGrepRules = {
2+
IDENT_NAME: {
3+
has: {
4+
kind: 'identifier',
5+
field: 'name',
6+
},
7+
},
8+
FN_CALL: {
9+
has: {
10+
kind: 'variable_declarator',
11+
has: {
12+
kind: 'call_expression',
13+
},
14+
},
15+
},
16+
ARROW_FN: {
17+
has: {
18+
kind: 'variable_declarator',
19+
has: {
20+
kind: 'arrow_function',
21+
},
22+
},
23+
},
24+
NOR_FN_VAR: {
25+
has: {
26+
kind: 'variable_declarator',
27+
has: {
28+
kind: 'function',
29+
},
30+
},
31+
},
32+
OBJ_VAR: {
33+
has: {
34+
kind: 'variable_declarator',
35+
has: {
36+
kind: 'object',
37+
field: 'value',
38+
},
39+
},
40+
},
41+
NOR_FN: {
42+
has: {
43+
kind: 'function_declaration',
44+
has: {
45+
kind: 'identifier',
46+
field: 'name',
47+
},
48+
},
49+
},
50+
PROPS_DEFAULT_CALL: {
51+
has: {
52+
kind: 'call_expression',
53+
has: {
54+
kind: 'identifier',
55+
field: 'function',
56+
},
57+
},
58+
},
59+
PROPS_DEFAULT_ARG: {
60+
kind: 'property_identifier',
61+
inside: {
62+
kind: 'pair',
63+
inside: {
64+
kind: 'object',
65+
inside: {
66+
kind: 'arguments',
67+
},
68+
},
69+
},
70+
},
71+
PROPS_DEFAULT_VAL: {
72+
kind: 'object',
73+
inside: {
74+
kind: 'arguments',
75+
},
76+
},
77+
CONST_VAR: {
78+
any: [
79+
{
80+
pattern: 'const $VAR',
81+
},
82+
],
83+
},
84+
LET_VAR: {
85+
any: [
86+
{
87+
pattern: 'let $VAR',
88+
},
89+
],
90+
},
91+
CONST_REF_VAR: {
92+
any: [
93+
{
94+
pattern: 'const $VAR = ref($VAL)',
95+
},
96+
],
97+
},
98+
CONST_REACTIVE_VAR: {
99+
any: [
100+
{
101+
pattern: 'const $VAR = reactive($VAL)',
102+
},
103+
],
104+
},
105+
CONST_PROPS_VAR: {
106+
any: [
107+
{
108+
pattern: 'const $VAR = defineProps($VAL)',
109+
},
110+
],
111+
},
112+
CONST_NOR_FN: {
113+
has:
114+
{
115+
kind: 'function_declaration',
116+
},
117+
},
118+
OBJ_PATTERN: {
119+
has:
120+
{
121+
kind: 'object_pattern',
122+
},
123+
},
124+
OBJ_PATTERN_VAL: {
125+
any:
126+
[
127+
{
128+
kind: 'object_pattern',
129+
},
130+
{
131+
kind: 'shorthand_property_identifier_pattern',
132+
},
133+
],
134+
},
135+
}
136+
137+
export const getRules = (name: string) => {
138+
return {
139+
rule: {
140+
matches: name,
141+
},
142+
utils: astGrepRules,
143+
}
144+
}

packages/core/parser/parser-script-bindings.ts

Lines changed: 1 addition & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CSSVarsBindingTypes } from './utils'
33
import type { SgNode } from '@ast-grep/napi'
44
import type { SFCDescriptor } from '@vue/compiler-sfc'
55
import type { BindingMetadata } from '@vue/compiler-dom'
6+
import {getRules} from "./ast-grep-rules";
67

78
// TODO: unit test
89
export function analyzeScriptBindings(descriptor: SFCDescriptor): BindingMetadata {
@@ -18,150 +19,6 @@ export function analyzeScriptBindings(descriptor: SFCDescriptor): BindingMetadat
1819
return bindings
1920
}
2021

21-
const getRules = (name: string) => {
22-
return {
23-
rule: {
24-
matches: name,
25-
},
26-
utils: {
27-
IDENT_NAME: {
28-
has: {
29-
kind: 'identifier',
30-
field: 'name',
31-
},
32-
},
33-
FN_CALL: {
34-
has: {
35-
kind: 'variable_declarator',
36-
has: {
37-
kind: 'call_expression',
38-
},
39-
},
40-
},
41-
ARROW_FN: {
42-
has: {
43-
kind: 'variable_declarator',
44-
has: {
45-
kind: 'arrow_function',
46-
},
47-
},
48-
},
49-
NOR_FN_VAR: {
50-
has: {
51-
kind: 'variable_declarator',
52-
has: {
53-
kind: 'function',
54-
},
55-
},
56-
},
57-
OBJ_VAR: {
58-
has: {
59-
kind: 'variable_declarator',
60-
has: {
61-
kind: 'object',
62-
field: 'value',
63-
},
64-
},
65-
},
66-
NOR_FN: {
67-
has: {
68-
kind: 'function_declaration',
69-
has: {
70-
kind: 'identifier',
71-
field: 'name',
72-
},
73-
},
74-
},
75-
PROPS_DEFAULT_CALL: {
76-
has: {
77-
kind: 'call_expression',
78-
has: {
79-
kind: 'identifier',
80-
field: 'function',
81-
},
82-
},
83-
},
84-
PROPS_DEFAULT_ARG: {
85-
kind: 'property_identifier',
86-
inside: {
87-
kind: 'pair',
88-
inside: {
89-
kind: 'object',
90-
inside: {
91-
kind: 'arguments',
92-
},
93-
},
94-
},
95-
},
96-
PROPS_DEFAULT_VAL: {
97-
kind: 'object',
98-
inside: {
99-
kind: 'arguments',
100-
},
101-
},
102-
CONST_VAR: {
103-
any: [
104-
{
105-
pattern: 'const $VAR',
106-
},
107-
],
108-
},
109-
LET_VAR: {
110-
any: [
111-
{
112-
pattern: 'let $VAR',
113-
},
114-
],
115-
},
116-
CONST_REF_VAR: {
117-
any: [
118-
{
119-
pattern: 'const $VAR = ref($VAL)',
120-
},
121-
],
122-
},
123-
CONST_REACTIVE_VAR: {
124-
any: [
125-
{
126-
pattern: 'const $VAR = reactive($VAL)',
127-
},
128-
],
129-
},
130-
CONST_PROPS_VAR: {
131-
any: [
132-
{
133-
pattern: 'const $VAR = defineProps($VAL)',
134-
},
135-
],
136-
},
137-
CONST_NOR_FN: {
138-
has:
139-
{
140-
kind: 'function_declaration',
141-
},
142-
},
143-
OBJ_PATTERN: {
144-
has:
145-
{
146-
kind: 'object_pattern',
147-
},
148-
},
149-
OBJ_PATTERN_VAL: {
150-
any:
151-
[
152-
{
153-
kind: 'object_pattern',
154-
},
155-
{
156-
kind: 'shorthand_property_identifier_pattern',
157-
},
158-
],
159-
},
160-
161-
},
162-
}
163-
}
164-
16522
function walkSgNodeToGetBindings(node: SgNode, bindings: BindingMetadata) {
16623
node.findAll(getRules('LET_VAR')).map((n) => {
16724
const key = n.getMatch('VAR')?.text() || ''

0 commit comments

Comments
 (0)