-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-query.js
More file actions
77 lines (66 loc) · 1.95 KB
/
test-query.js
File metadata and controls
77 lines (66 loc) · 1.95 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
74
75
76
77
const { tsquery } = require('@phenomnomnominal/tsquery');
const files = tsquery.project('./functions/tsconfig.json');
const sourceFiles = files.filter(file =>
file.fileName.startsWith('/Users/eldad/projects/feedfarm/functions/src/'),
);
sourceFiles.forEach(ast => {
if (
ast.fileName !==
'/Users/eldad/projects/feedfarm/functions/src/functions/create-feed.ts'
) {
return;
}
let newSource;
const functions = tsquery(ast, 'ExportAssignment FunctionExpression');
const arrowFunctions = tsquery(ast, 'ExportAssignment ArrowFunction');
const functionDeclaration = functions[0] || arrowFunctions[0];
if (functionDeclaration) {
newSource = tsquery.replace(
ast.getText(),
'ExportAssignment > CallExpression',
node => {
const children = node.getChildren().map(child => child.getText());
if (children[0] === 'functions.https.onCall') {
children[0] = '';
if (children[2].slice(-1) === ',') {
children[2] = children[2].slice(0, -1);
}
return children.join('');
} else {
throw new Error(
`[firebase-functions-declarations] Could not parse file "${ast.fileName}"`,
);
}
},
);
} else {
const defaultExports = tsquery(
ast,
'ExportAssignment CallExpression Identifier',
);
if (
defaultExports
.map(call => call.getText())
.join('.')
.startsWith('functions.https.onCall')
) {
newSource = tsquery.replace(
ast.getText(),
'ExportAssignment CallExpression',
node => {
const children = node.getChildren();
if (
children
.map(child => child.getText())
.slice(0)
.join('')
.match(/^functions\.https\.onCall\(.*?\)/)
) {
return node.getChildren()[2].getText();
}
},
);
}
}
console.log(newSource);
});