This repository has been archived by the owner on Dec 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ast-cast.js
111 lines (106 loc) · 3.37 KB
/
ast-cast.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
module.exports = function astCast(config, json, fileName) {
if (json === null) return json;
for (const fileNameSelector in config) {
if (!config.hasOwnProperty(fileNameSelector)) continue;
if (fileNameSelector === '*'
|| isFileNameSelected(fileNameSelector, fileName)
) {
const rules = config[fileNameSelector];
for (let i = 0; i < rules.length; i++) {
const rule = rules[i];
if (rule.type === json.type) {
if (rule.match) {
const match = new RegExp(rule.match);
if (match.test(json.raw)) {
json = castWithRule(rule, json, match);
}
} else {
json = castWithRule(rule, json);
}
}
}
}
}
if (json.hasOwnProperty('body')) {
if (Array.isArray(json.body)) {
json.body = json.body.map(bodyPart => astCast(config, bodyPart, fileName));
} else {
json.body = astCast(config, json.body, fileName);
}
}
if (json.hasOwnProperty('argument')) {
json.argument = astCast(config, json.argument, fileName);
}
if (json.hasOwnProperty('arguments')) {
json.arguments = json.arguments.map(part => astCast(config, part, fileName));
}
if (json.hasOwnProperty('callee')) {
json.callee = astCast(config, json.callee, fileName);
}
if (json.hasOwnProperty('consequent')) {
json.consequent = astCast(config, json.consequent, fileName);
}
if (json.hasOwnProperty('declarations')) {
json.declarations = json.declarations.map(part => astCast(config, part, fileName));
}
if (json.hasOwnProperty('expression')) {
json.expression = astCast(config, json.expression, fileName);
}
if (json.hasOwnProperty('init')) {
json.init = astCast(config, json.init, fileName);
}
if (json.hasOwnProperty('left')) {
json.left = astCast(config, json.left, fileName);
}
if (json.hasOwnProperty('object')) {
json.object = astCast(config, json.object, fileName);
}
if (json.hasOwnProperty('right')) {
json.right = astCast(config, json.right, fileName);
}
if (json.hasOwnProperty('test')) {
if (json.test.hasOwnProperty('left')) {
json.test.left = astCast(config, json.test.left, fileName);
json.test.right = astCast(config, json.test.right, fileName);
} else {
json.test = astCast(config, json.test, fileName);
}
}
return json;
};
function castWithRule(rule, json, match) {
const operations = rule.operations;
for (let i = 0; i < operations.length; i++) {
const operation = operations[i];
switch (operation.name) {
case 'substring':
json.raw = json.raw.substring(operation.arguments[0], json.raw.length + operation.arguments[1]);
break;
case 'wrap':
json.raw = operation.arguments[0] + json.raw + operation.arguments[1];
break;
case 'replace':
json.raw = json.raw.replace(match, operation.arguments[0]);
break;
case 'unNestBody':
json = unNestBody(json);
break;
default:
throw new Error(`Unknown operation ${ operation.name }`);
}
}
return json;
}
function isFileNameSelected(fileNameSelector, fileName) {
return fileNameSelector === fileName;
}
function unNestBody(json) {
let result = [];
const body = json.body;
for (let i = 0; i < body.length; i++) {
const bodyItem = body[i];
result = result.concat(bodyItem.body);
}
json.body = result;
return json;
}