-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathworkflow.ts
More file actions
73 lines (56 loc) · 2 KB
/
workflow.ts
File metadata and controls
73 lines (56 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import type Js from '@codemod.com/jssg-types/src/langs/javascript'
import type { Edit, SgNode, SgRoot } from '@codemod.com/jssg-types/src/main'
function getStringLiteralValue(node: SgNode<Js>): string | null {
if (!node.is('string')) return null
const fragments = node.findAll({ rule: { kind: 'string_fragment' } })
if (fragments.length !== 1) return null
return fragments[0]?.text() ?? null
}
function findParentFunctionParameters(node: SgNode<Js>): SgNode<Js, 'formal_parameters'> | null {
let parent = node.parent()
while (parent) {
if (parent.is('formal_parameters')) return parent
parent = parent.parent()
}
return null
}
async function transform(root: SgRoot<Js>): Promise<string | null> {
const rootNode = root.root()
const nodes = rootNode.findAll({
rule: {
pattern: '$OBJ.$METHOD($ARG)',
},
constraints: {
METHOD: { regex: '^(param)$' },
},
})
if (!nodes.length) return null
const edits: Edit[] = []
for (const call of nodes) {
const arg = call.getMatch('ARG')
const obj = call.getMatch('OBJ')
if (!arg || !obj) continue
const objDef = obj.definition({ resolveExternal: false })
if (!objDef) continue
const isParameter = objDef.node.matches({
rule: { inside: { kind: 'formal_parameters', stopBy: 'end' } },
})
if (!isParameter) continue
const parameters = findParentFunctionParameters(objDef.node)
if (!parameters) continue
const firstParameter = parameters.find({ rule: { kind: 'identifier' } })
if (!firstParameter) continue
const requestName = firstParameter.text()
const argValue = getStringLiteralValue(arg)
if (argValue === 'body') {
edits.push(call.replace(`${requestName}.body`))
} else if (argValue === 'query') {
edits.push(call.replace(`${requestName}.query`))
} else if (argValue) {
edits.push(call.replace(`${requestName}.params.${argValue}`))
}
}
if (!edits.length) return null
return rootNode.commitEdits(edits)
}
export default transform