This repository was archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtransform.js
More file actions
81 lines (75 loc) · 2.67 KB
/
Copy pathtransform.js
File metadata and controls
81 lines (75 loc) · 2.67 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
78
79
80
81
/**
* npm i -g jscodeshift
* jscodeshift -t transform.js elm.js
*/
const glslx = require('glslx').compile;
module.exports = function (file, api, options) {
const j = api.jscodeshift;
const airity = new Map(); // Map(functionName, airity)
const tree = j(file.source);
// Build the initial airity map
tree.find(j.VariableDeclarator).forEach(path => {
if (
path.node.id.type === "Identifier" &&
path.node.init &&
path.node.init.type === "CallExpression" &&
path.node.init.callee.type === "Identifier" &&
path.node.init.callee.name.match(/^F\d$/)
) {
airity.set(
path.node.id.name,
Number(path.node.init.callee.name.substr(1))
);
}
});
// Add re-declarations of the existing functions
tree.find(j.VariableDeclarator).forEach(path => {
if (
path.node.id.type === "Identifier" &&
path.node.init &&
path.node.init.type === "Identifier" &&
airity.has(path.node.init.name)
) {
airity.set(path.node.id.name, airity.get(path.node.init.name));
}
});
// Transform the A1..n calls
return tree
.find(j.CallExpression)
.forEach(path => {
if (
path.node.callee.type === "Identifier" &&
path.node.callee.name.match(/^A\d$/) &&
path.node.arguments.length > 1 &&
path.node.arguments[0].type === "Identifier" &&
airity.get(path.node.arguments[0].name) ===
path.node.arguments.length - 1 &&
airity.get(path.node.arguments[0].name) ===
Number(path.node.callee.name.substr(1))
) {
path.node.callee = {
type: "MemberExpression",
object: {
type: "Identifier",
name: path.node.arguments[0].name
},
property: {
type: "Identifier",
name: "f"
}
};
path.node.arguments.shift();
}
})
.find(j.Literal)
.filter((path) => path.value && path.value.value && path.value.value.length > 100)
.replaceWith(nodePath => {
const { node } = nodePath;
const newValue = glslx(node.value, { renaming: 'none', format: "json", });
if (newValue.log === "") {
node.value = JSON.parse(newValue.output).shaders[0].contents;
}
return node;
})
.toSource();
};