@shaderfrog/core 0.0.2 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/README.md +184 -1
  2. package/dist/ast/manipulate.js +328 -0
  3. package/dist/ast/shader-sections.js +256 -0
  4. package/dist/context.js +230 -0
  5. package/dist/engine.js +209 -0
  6. package/dist/evaluate.js +27 -0
  7. package/dist/graph-types.js +7 -0
  8. package/dist/graph.js +381 -0
  9. package/dist/graph.test.js +168 -0
  10. package/dist/nodes/code-nodes.js +18 -0
  11. package/dist/nodes/core-node.js +9 -0
  12. package/dist/nodes/data-nodes.js +123 -0
  13. package/dist/nodes/edge.js +1 -0
  14. package/dist/nodes/engine-node.js +189 -0
  15. package/dist/parsers.js +213 -0
  16. package/dist/plugins/babylon/bablyengine.js +582 -0
  17. package/dist/plugins/babylon/importers.js +64 -0
  18. package/{src/plugins/babylon/index.ts → dist/plugins/babylon/index.js} +0 -1
  19. package/dist/plugins/playcanvas/importers.js +28 -0
  20. package/dist/plugins/playcanvas/index.js +2 -0
  21. package/dist/plugins/playcanvas/playengine.js +510 -0
  22. package/dist/plugins/three/importers.js +15 -0
  23. package/{src/plugins/three/index.ts → dist/plugins/three/index.js} +0 -1
  24. package/dist/plugins/three/threngine.js +495 -0
  25. package/dist/strategy/assignemntTo.js +26 -0
  26. package/dist/strategy/declarationOf.js +23 -0
  27. package/dist/strategy/hardCode.js +23 -0
  28. package/dist/strategy/index.js +38 -0
  29. package/dist/strategy/inject.js +122 -0
  30. package/dist/strategy/namedAttribute.js +48 -0
  31. package/dist/strategy/texture2D.js +83 -0
  32. package/dist/strategy/uniform.js +190 -0
  33. package/dist/strategy/variable.js +80 -0
  34. package/dist/stratgies.test.js +164 -0
  35. package/dist/util/ast.js +9 -0
  36. package/dist/util/ensure.js +7 -0
  37. package/dist/util/id.js +2 -0
  38. package/package.json +12 -4
  39. package/src/ast/manipulate.ts +0 -392
  40. package/src/ast/shader-sections.ts +0 -323
  41. package/src/core/engine.ts +0 -214
  42. package/src/core/file.js +0 -53
  43. package/src/core/graph.ts +0 -1007
  44. package/src/core/nodes/code-nodes.ts +0 -66
  45. package/src/core/nodes/core-node.ts +0 -48
  46. package/src/core/nodes/data-nodes.ts +0 -344
  47. package/src/core/nodes/edge.ts +0 -23
  48. package/src/core/nodes/engine-node.ts +0 -266
  49. package/src/core/strategy.ts +0 -520
  50. package/src/core.test.ts +0 -312
  51. package/src/plugins/babylon/bablyengine.ts +0 -670
  52. package/src/plugins/babylon/importers.ts +0 -69
  53. package/src/plugins/three/importers.ts +0 -18
  54. package/src/plugins/three/threngine.tsx +0 -571
  55. package/src/util/ensure.ts +0 -10
  56. package/src/util/id.ts +0 -2
@@ -0,0 +1,122 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ var __read = (this && this.__read) || function (o, n) {
13
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
14
+ if (!m) return o;
15
+ var i = m.call(o), r, ar = [], e;
16
+ try {
17
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
18
+ }
19
+ catch (error) { e = { error: error }; }
20
+ finally {
21
+ try {
22
+ if (r && !r.done && (m = i["return"])) m.call(i);
23
+ }
24
+ finally { if (e) throw e.error; }
25
+ }
26
+ return ar;
27
+ };
28
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
29
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
30
+ if (ar || !(i in from)) {
31
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
32
+ ar[i] = from[i];
33
+ }
34
+ }
35
+ return to.concat(ar || Array.prototype.slice.call(from));
36
+ };
37
+ import { generate } from '@shaderfrog/glsl-parser';
38
+ import { nodeInput } from '../nodes/core-node';
39
+ import { StrategyType } from '.';
40
+ /**
41
+ * Inject source code into an AST, in a find-and-replace style. This only
42
+ * operates on statements right now
43
+ */
44
+ export var injectStrategy = function (config) { return ({
45
+ type: StrategyType.INJECT,
46
+ config: config,
47
+ }); };
48
+ // Typescript fucked up flat https://stackoverflow.com/a/61420611/743464
49
+ var combineWs = function (a, b) {
50
+ return [a, b].flat(Infinity);
51
+ };
52
+ // Move whitespace from one node to another. Since whitespace is trailing, if a
53
+ // node is injected after a previous node, move the whitespace from the earlier
54
+ // node to the later one. This keeps comments in the same place
55
+ var transferWhitespace = function (to, from) {
56
+ return 'semi' in to && 'semi' in from
57
+ ? [
58
+ __assign(__assign({}, to), { semi: __assign(__assign({}, to.semi), { whitespace: from.semi.whitespace }) }),
59
+ __assign(__assign({}, from), { semi: __assign(__assign({}, from.semi), { whitespace: '\n' }) }),
60
+ ]
61
+ : 'whitespace' in to && 'semi' in from
62
+ ? [
63
+ __assign(__assign({}, to), { whitespace: combineWs(to.whitespace, from.semi.whitespace) }),
64
+ __assign(__assign({}, from), { semi: __assign(__assign({}, from.semi), { whitespace: '\n' }) }),
65
+ ]
66
+ : [to, from];
67
+ };
68
+ export var applyInjectStrategy = function (graphNode, ast, strategy) {
69
+ var program = ast.program;
70
+ var _a = strategy.config, find = _a.find, count = _a.count, insert = _a.insert;
71
+ // Total hack for now. Search each function body for statements that when
72
+ // generated, match the user's "find" string.
73
+ var matchedStatements = program.reduce(function (matches, statement) {
74
+ var newMatches = [];
75
+ if (statement.type === 'function') {
76
+ var bodyStatements_1 = statement.body.statements;
77
+ newMatches = bodyStatements_1.reduce(function (innerMatches, bodyStmt, index) {
78
+ var generated = generate(bodyStmt);
79
+ // console.log(`"${generated}"`);
80
+ var triple = [
81
+ bodyStatements_1,
82
+ bodyStmt,
83
+ index,
84
+ ];
85
+ return __spreadArray(__spreadArray([], __read(innerMatches), false), __read((generated.includes(find) ? [triple] : [])), false);
86
+ }, []);
87
+ }
88
+ return __spreadArray(__spreadArray([], __read(matches), false), __read(newMatches), false);
89
+ }, []);
90
+ var name = "Inject ".concat(strategy.config.insert);
91
+ return [
92
+ [
93
+ nodeInput(name, "filler_".concat(name), 'filler', undefined, // Data type for what plugs into this filler
94
+ new Set(['code', 'data']), false),
95
+ function (fillerAst) {
96
+ var toInsert = Array.isArray(fillerAst)
97
+ ? fillerAst
98
+ : [fillerAst];
99
+ // Loop backwards through the matches because when we inject code into
100
+ // parent nodes, it modifies the statement list arrays
101
+ for (
102
+ // Only go as many count replacements as specified in the config
103
+ var i = Math.min(matchedStatements.length - 1, count); i >= 0; i--) {
104
+ var _a = __read(matchedStatements[i], 3), parent = _a[0], stmt = _a[1], index = _a[2];
105
+ if (insert === 'after') {
106
+ var _b = __read(toInsert), first = _b[0], rest = _b.slice(1);
107
+ var _c = __read(transferWhitespace(first, stmt), 2), newFirst = _c[0], updatedStmt = _c[1];
108
+ var newStatements = __spreadArray([newFirst], __read(rest), false);
109
+ parent.splice.apply(parent, __spreadArray([index, 1, updatedStmt], __read(newStatements), false));
110
+ }
111
+ else if (insert === 'before') {
112
+ parent.splice.apply(parent, __spreadArray([index - 1, 0], __read(toInsert), false));
113
+ }
114
+ else if (insert === 'replace') {
115
+ throw new Error('not yet implemented');
116
+ }
117
+ }
118
+ return ast;
119
+ },
120
+ ],
121
+ ];
122
+ };
@@ -0,0 +1,48 @@
1
+ var __read = (this && this.__read) || function (o, n) {
2
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
3
+ if (!m) return o;
4
+ var i = m.call(o), r, ar = [], e;
5
+ try {
6
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
7
+ }
8
+ catch (error) { e = { error: error }; }
9
+ finally {
10
+ try {
11
+ if (r && !r.done && (m = i["return"])) m.call(i);
12
+ }
13
+ finally { if (e) throw e.error; }
14
+ }
15
+ return ar;
16
+ };
17
+ import { nodeInput } from '../nodes/core-node';
18
+ import { StrategyType } from '.';
19
+ import { generateFiller } from '../util/ast';
20
+ export var namedAttributeStrategy = function (attributeName) { return ({
21
+ type: StrategyType.NAMED_ATTRIBUTE,
22
+ config: { attributeName: attributeName },
23
+ }); };
24
+ export var applyNamedAttributeStrategy = function (node, ast, strategy) {
25
+ var program = ast;
26
+ var attributeName = strategy.config.attributeName;
27
+ return [
28
+ [
29
+ nodeInput(attributeName, "filler_".concat(attributeName), 'filler', undefined, // Data type for what plugs into this filler
30
+ new Set(['code', 'data']), true),
31
+ function (fillerAst) {
32
+ Object.entries(program.scopes[0].bindings).forEach(function (_a) {
33
+ var _b = __read(_a, 2), name = _b[0], binding = _b[1];
34
+ binding.references.forEach(function (ref) {
35
+ // Rename the variable usage only if it's not the identifier, to
36
+ // avoid filling in `in vec3 replaceMe;` with `replacer()`
37
+ if (ref.type === 'identifier' &&
38
+ ref !== binding.declaration &&
39
+ ref.identifier === attributeName) {
40
+ ref.identifier = generateFiller(fillerAst);
41
+ }
42
+ });
43
+ });
44
+ return ast;
45
+ },
46
+ ],
47
+ ];
48
+ };
@@ -0,0 +1,83 @@
1
+ var __read = (this && this.__read) || function (o, n) {
2
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
3
+ if (!m) return o;
4
+ var i = m.call(o), r, ar = [], e;
5
+ try {
6
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
7
+ }
8
+ catch (error) { e = { error: error }; }
9
+ finally {
10
+ try {
11
+ if (r && !r.done && (m = i["return"])) m.call(i);
12
+ }
13
+ finally { if (e) throw e.error; }
14
+ }
15
+ return ar;
16
+ };
17
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
18
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
19
+ if (ar || !(i in from)) {
20
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
21
+ ar[i] = from[i];
22
+ }
23
+ }
24
+ return to.concat(ar || Array.prototype.slice.call(from));
25
+ };
26
+ import { generate } from '@shaderfrog/glsl-parser';
27
+ import { visit, } from '@shaderfrog/glsl-parser/ast';
28
+ import { nodeInput } from '../nodes/core-node';
29
+ import { StrategyType } from '.';
30
+ export var texture2DStrategy = function () { return ({
31
+ type: StrategyType.TEXTURE_2D,
32
+ config: {},
33
+ }); };
34
+ export var applyTexture2DStrategy = function (node, ast, strategy) {
35
+ var texture2Dcalls = [];
36
+ var seen = {};
37
+ var visitors = {
38
+ function_call: {
39
+ enter: function (path) {
40
+ var identifier = path.node.identifier;
41
+ if (
42
+ // TODO: 100 vs 300
43
+ ((identifier === null || identifier === void 0 ? void 0 : identifier.identifier) === 'texture2D' ||
44
+ (identifier === null || identifier === void 0 ? void 0 : identifier.identifier) === 'texture') &&
45
+ path.key) {
46
+ if (!path.parent) {
47
+ throw new Error('This error is impossible. A function call always has a parent.');
48
+ }
49
+ var name = generate(path.node.args[0]);
50
+ seen[name] = (seen[name] || 0) + 1;
51
+ texture2Dcalls.push([
52
+ name,
53
+ path.parent,
54
+ path.key,
55
+ // Remove the first argument and comma
56
+ path.node.args.slice(2),
57
+ ]);
58
+ }
59
+ },
60
+ },
61
+ };
62
+ visit(ast, visitors);
63
+ var names = new Set(Object.entries(seen).reduce(function (arr, _a) {
64
+ var _b = __read(_a, 2), name = _b[0], count = _b[1];
65
+ return __spreadArray(__spreadArray([], __read(arr), false), __read((count > 1 ? [name] : [])), false);
66
+ }, []));
67
+ var inputs = texture2Dcalls.map(function (_a, index) {
68
+ var _b = __read(_a, 4), name = _b[0], parent = _b[1], key = _b[2], texture2dArgs = _b[3];
69
+ // Suffix input name if it's used more than once
70
+ var iName = names.has(name) ? "".concat(name, "_").concat(index) : name;
71
+ return [
72
+ nodeInput(iName, "filler_".concat(iName), 'filler', 'vector4', // Data type for what plugs into this filler
73
+ new Set(['code', 'data']), false),
74
+ function (fillerAst) {
75
+ // @ts-ignore
76
+ parent[key] = fillerAst;
77
+ return ast;
78
+ },
79
+ texture2dArgs,
80
+ ];
81
+ });
82
+ return inputs;
83
+ };
@@ -0,0 +1,190 @@
1
+ var __read = (this && this.__read) || function (o, n) {
2
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
3
+ if (!m) return o;
4
+ var i = m.call(o), r, ar = [], e;
5
+ try {
6
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
7
+ }
8
+ catch (error) { e = { error: error }; }
9
+ finally {
10
+ try {
11
+ if (r && !r.done && (m = i["return"])) m.call(i);
12
+ }
13
+ finally { if (e) throw e.error; }
14
+ }
15
+ return ar;
16
+ };
17
+ import { mangleName } from '../graph';
18
+ import { nodeInput } from '../nodes/core-node';
19
+ import { StrategyType } from '.';
20
+ import { generateFiller } from '../util/ast';
21
+ export var uniformStrategy = function () { return ({
22
+ type: StrategyType.UNIFORM,
23
+ config: {},
24
+ }); };
25
+ var DATA_TYPE_MAP = [
26
+ ['vector2', new Set(['bvec2', 'dvec2', 'ivec2', 'uvec2', 'vec2'])],
27
+ ['number', new Set(['float', 'double', 'int', 'uint', 'atomic_uint'])],
28
+ ['vector3', new Set(['bvec3', 'dvec3', 'ivec3', 'uvec3', 'vec3'])],
29
+ ['vector4', new Set(['bvec4', 'dvec4', 'ivec4', 'uvec4', 'vec4'])],
30
+ ['texture', new Set(['sampler2D'])],
31
+ ['mat2', new Set(['mat2', 'dmat2'])],
32
+ ['mat3', new Set(['mat3', 'dmat3'])],
33
+ ['mat4', new Set(['mat4', 'dmat4'])],
34
+ ['mat2x2', new Set(['mat2x2', 'dmat2x2'])],
35
+ ['mat2x3', new Set(['mat2x3', 'dmat2x3'])],
36
+ ['mat2x4', new Set(['mat2x4', 'dmat2x4'])],
37
+ ['mat3x2', new Set(['mat3x2', 'dmat3x2'])],
38
+ ['mat3x3', new Set(['mat3x3', 'dmat3x3'])],
39
+ ['mat3x4', new Set(['mat3x4', 'dmat3x4'])],
40
+ ['mat4x2', new Set(['mat4x2', 'dmat4x2'])],
41
+ ['mat4x3', new Set(['mat4x3', 'dmat4x3'])],
42
+ ['mat4x4', new Set(['mat4x4', 'dmat4x4'])],
43
+ ];
44
+ /**
45
+ * Uncategorized:
46
+ *
47
+ "sampler1D"
48
+ "sampler3D"
49
+ "samplerCube"
50
+ "sampler1DShadow"
51
+ "sampler2DShadow"
52
+ "samplerCubeShadow"
53
+ "sampler1DArray"
54
+ "sampler2DArray"
55
+ "sampler1DArrayShadow"
56
+ "sampler2DArrayshadow"
57
+ "isampler1D"
58
+ "isampler2D"
59
+ "isampler3D"
60
+ "isamplerCube"
61
+ "isampler1Darray"
62
+ "isampler2DArray"
63
+ "usampler1D"
64
+ "usampler2D"
65
+ "usampler3D"
66
+ "usamplerCube"
67
+ "usampler1DArray"
68
+ "usampler2DArray"
69
+ "sampler2DRect"
70
+ "sampler2DRectshadow"
71
+ "isampler2DRect"
72
+ "usampler2DRect"
73
+ "samplerBuffer"
74
+ "isamplerBuffer"
75
+ "usamplerBuffer"
76
+ "samplerCubeArray"
77
+ "samplerCubeArrayShadow"
78
+ "isamplerCubeArray"
79
+ "usamplerCubeArray"
80
+ "sampler2DMS"
81
+ "isampler2DMS"
82
+ "usampler2DMS"
83
+ "sampler2DMSArray"
84
+ "isampler2DMSArray"
85
+ "usampler2DMSArray"
86
+ "image1D"
87
+ "iimage1D"
88
+ "uimage1D"
89
+ "image2D"
90
+ "iimage2D"
91
+ "uimage2D"
92
+ "image3D"
93
+ "iimage3D"
94
+ "uimage3D"
95
+ "image2DRect"
96
+ "iimage2DRect"
97
+ "uimage2DRect"
98
+ "imageCube"
99
+ "iimageCube"
100
+ "uimageCube"
101
+ "imageBuffer"
102
+ "iimageBuffer"
103
+ "uimageBuffer"
104
+ "image1DArray"
105
+ "iimage1DArray"
106
+ "uimage1DArray"
107
+ "image2DArray"
108
+ "iimage2DArray"
109
+ "uimage2DArray"
110
+ "imageCubeArray"
111
+ "iimageCubeArray"
112
+ "uimageCubeArray"
113
+ "image2DMS"
114
+ "iimage2DMS"
115
+ "uimage2DMS"
116
+ "image2DMArray"
117
+ "iimage2DMSArray"
118
+ "uimage2DMSArray"
119
+ "struct"
120
+ */
121
+ var mapUniformType = function (type) {
122
+ var found = DATA_TYPE_MAP.find(function (_a) {
123
+ var _b = __read(_a, 2), _ = _b[0], set = _b[1];
124
+ return set.has(type);
125
+ });
126
+ if (found) {
127
+ return found[0];
128
+ }
129
+ // console.log(`Unknown uniform type, can't map to graph: ${type}`);
130
+ };
131
+ export var applyUniformStrategy = function (graphNode, ast, strategy) {
132
+ var program = ast;
133
+ return (program.program || []).flatMap(function (node) {
134
+ var _a, _b, _c, _d, _e, _f, _g;
135
+ // The uniform declaration type, like vec4
136
+ var uniformType = (_d = (_c = (_b = (_a = node.declaration) === null || _a === void 0 ? void 0 : _a.specified_type) === null || _b === void 0 ? void 0 : _b.specifier) === null || _c === void 0 ? void 0 : _c.specifier) === null || _d === void 0 ? void 0 : _d.token;
137
+ var graphDataType = mapUniformType(uniformType);
138
+ // If this is a uniform declaration line
139
+ if (node.type === 'declaration_statement' &&
140
+ node.declaration.type === 'declarator_list' &&
141
+ ((_g = (_f = (_e = node.declaration) === null || _e === void 0 ? void 0 : _e.specified_type) === null || _f === void 0 ? void 0 : _f.qualifiers) === null || _g === void 0 ? void 0 : _g.find(function (n) { return n.token === 'uniform'; }))
142
+ // commented this out to allow for sampler2D uniforms to appear as inputs
143
+ // && uniformType !== 'sampler2D'
144
+ ) {
145
+ // Capture all the declared names, removing mangling suffix
146
+ var declarations_1 = node.declaration.declarations;
147
+ var names = declarations_1.map(function (d) { return d.identifier.identifier; });
148
+ // Tricky code warning: The flow of preparing a node for the graph is:
149
+ // 1. Produce/mangle the AST (with unmangled names)
150
+ // 2. findInputs() (with unmangled names)
151
+ // 3. The AST is *then* mangled in graph.ts
152
+ // 4. Later, the inputs are filled in, and now, we have an input with
153
+ // the name "x" but the ast now has the mangled name "x_1". So
154
+ // here, we look for the *mangled* name in the strategy runner
155
+ return names.map(function (name) { return [
156
+ nodeInput(name, "uniform_".concat(name), 'uniform', graphDataType, new Set(['code', 'data']), true),
157
+ function (filler) {
158
+ var mangledName = mangleName(name, graphNode);
159
+ // Remove the declaration line, or the declared uniform
160
+ if (declarations_1.length === 1) {
161
+ program.program.splice(program.program.indexOf(node), 1);
162
+ }
163
+ else {
164
+ var decl = node.declaration;
165
+ decl.declarations = decl.declarations.filter(function (d) { return d.identifier.identifier !== mangledName; });
166
+ }
167
+ // And rename all the references to said uniform
168
+ program.scopes[0].bindings[name].references.forEach(function (ref) {
169
+ if (ref.type === 'identifier' && ref.identifier === mangledName) {
170
+ ref.identifier = generateFiller(filler);
171
+ }
172
+ else if (ref.type === 'parameter_declaration' &&
173
+ 'identifier' in ref &&
174
+ ref.identifier.identifier === mangledName) {
175
+ ref.identifier.identifier = generateFiller(filler);
176
+ }
177
+ else if ('identifier' in ref) {
178
+ ref.identifier = generateFiller(filler);
179
+ }
180
+ else {
181
+ console.warn('Unknown uniform reference for', graphNode.name, 'ref');
182
+ }
183
+ });
184
+ return ast;
185
+ },
186
+ ]; });
187
+ }
188
+ return [];
189
+ });
190
+ };
@@ -0,0 +1,80 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ var __read = (this && this.__read) || function (o, n) {
13
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
14
+ if (!m) return o;
15
+ var i = m.call(o), r, ar = [], e;
16
+ try {
17
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
18
+ }
19
+ catch (error) { e = { error: error }; }
20
+ finally {
21
+ try {
22
+ if (r && !r.done && (m = i["return"])) m.call(i);
23
+ }
24
+ finally { if (e) throw e.error; }
25
+ }
26
+ return ar;
27
+ };
28
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
29
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
30
+ if (ar || !(i in from)) {
31
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
32
+ ar[i] = from[i];
33
+ }
34
+ }
35
+ return to.concat(ar || Array.prototype.slice.call(from));
36
+ };
37
+ import { nodeInput } from '../nodes/core-node';
38
+ import { StrategyType } from '.';
39
+ import { generateFiller } from '../util/ast';
40
+ export var variableStrategy = function () { return ({
41
+ type: StrategyType.VARIABLE,
42
+ config: {},
43
+ }); };
44
+ export var applyVariableStrategy = function (node, ast, strategy) {
45
+ var program = ast;
46
+ return Object.values(program.scopes.reduce(function (acc, scope) { return (__assign(__assign({}, acc), scope.bindings)); }, {})).flatMap(function (binding) {
47
+ return binding.references.reduce(function (acc, ref) {
48
+ var identifier, replacer;
49
+ if (ref.type === 'declaration') {
50
+ identifier = ref.identifier.identifier;
51
+ replacer = function (fillerAst) {
52
+ ref.identifier.identifier = generateFiller(fillerAst);
53
+ return ast;
54
+ };
55
+ }
56
+ else if (ref.type === 'identifier') {
57
+ identifier = ref.identifier;
58
+ replacer = function (fillerAst) {
59
+ ref.identifier = generateFiller(fillerAst);
60
+ return ast;
61
+ };
62
+ // } else if (ref.type === 'parameter_declaration') {
63
+ // identifier = ref.declaration.identifier.identifier;
64
+ // replacer = (fillerAst: AstNode) => {
65
+ // ref.declaration.identifier.identifier = generate(fillerAst);
66
+ // };
67
+ }
68
+ else {
69
+ return acc;
70
+ }
71
+ return __spreadArray(__spreadArray([], __read(acc), false), [
72
+ [
73
+ nodeInput(identifier, "filler_".concat(identifier), 'filler', undefined, // Data type for what plugs into this filler
74
+ new Set(['code', 'data']), false),
75
+ replacer,
76
+ ],
77
+ ], false);
78
+ }, []);
79
+ });
80
+ };