Skip to content

Commit e7f4e9f

Browse files
committed
Improve type-safety for Grecha tags
1 parent f653e6a commit e7f4e9f

File tree

9 files changed

+169
-135
lines changed

9 files changed

+169
-135
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"prettier.singleQuote": true,
3+
"typescript.format.semicolons": "ignore",
4+
"typescript.tsdk": "node_modules/typescript/lib"
5+
}

js/eval.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function parse_primary(lexer) {
8181
var expr = parse_expr(lexer);
8282
token = lexer.next();
8383
if (token !== ')') {
84-
throw new Error("Expected ')' but got '" + token + "'");
84+
throw new Error("Expected ')' but got '".concat(token, "'"));
8585
}
8686
return expr;
8787
}
@@ -113,7 +113,7 @@ function parse_primary(lexer) {
113113
next_token = lexer.next();
114114
}
115115
if (next_token !== ')') {
116-
throw Error("Expected ')' but got '" + next_token + "'");
116+
throw Error("Expected ')' but got '".concat(next_token, "'"));
117117
}
118118
return {
119119
"kind": "funcall",
@@ -172,7 +172,7 @@ function compile_expr(src) {
172172
if (token !== null) {
173173
console.log(typeof (token));
174174
console.log(token);
175-
throw new Error("Unexpected token '" + token + "'");
175+
throw new Error("Unexpected token '".concat(token, "'"));
176176
}
177177
return result;
178178
}
@@ -189,7 +189,7 @@ function run_expr(expr, user_context) {
189189
if (user_context.vars && value in user_context.vars) {
190190
return user_context.vars[value];
191191
}
192-
throw new Error("Unknown variable '" + value + "'");
192+
throw new Error("Unknown variable '".concat(value, "'"));
193193
}
194194
else {
195195
return number;
@@ -200,24 +200,24 @@ function run_expr(expr, user_context) {
200200
if (unary_op.op in UNARY_OPS) {
201201
return UNARY_OPS[unary_op.op](run_expr(unary_op.operand, user_context));
202202
}
203-
throw new Error("Unknown unary operator '" + unary_op.op + "'");
203+
throw new Error("Unknown unary operator '".concat(unary_op.op, "'"));
204204
}
205205
case 'binary_op': {
206206
var binary_op = expr.payload;
207207
if (binary_op.op in BINARY_OPS) {
208208
return BINARY_OPS[binary_op.op].func(run_expr(binary_op.lhs, user_context), run_expr(binary_op.rhs, user_context));
209209
}
210-
throw new Error("Unknown binary operator '" + binary_op.op + "'");
210+
throw new Error("Unknown binary operator '".concat(binary_op.op, "'"));
211211
}
212212
case 'funcall': {
213213
var funcall = expr.payload;
214214
if (user_context.funcs && funcall.name in user_context.funcs) {
215215
return (_a = user_context.funcs)[funcall.name].apply(_a, funcall.args.map(function (arg) { return run_expr(arg, user_context); }));
216216
}
217-
throw new Error("Unknown function '" + funcall.name + "'");
217+
throw new Error("Unknown function '".concat(funcall.name, "'"));
218218
}
219219
default: {
220-
throw new Error("Unexpected AST node '" + expr.kind + "'");
220+
throw new Error("Unexpected AST node '".concat(expr.kind, "'"));
221221
}
222222
}
223223
}

js/grecha.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
"use strict";
2-
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
3-
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
4-
to[j] = from[i];
5-
return to;
2+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4+
if (ar || !(i in from)) {
5+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6+
ar[i] = from[i];
7+
}
8+
}
9+
return to.concat(ar || Array.prototype.slice.call(from));
610
};
711
var LOREM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
812
function tag(name) {
@@ -13,7 +17,7 @@ function tag(name) {
1317
var result = document.createElement(name);
1418
for (var _a = 0, children_1 = children; _a < children_1.length; _a++) {
1519
var child = children_1[_a];
16-
if (typeof (child) === 'string') {
20+
if (typeof child === 'string') {
1721
result.appendChild(document.createTextNode(child));
1822
}
1923
else {
@@ -35,63 +39,63 @@ function canvas() {
3539
for (var _i = 0; _i < arguments.length; _i++) {
3640
children[_i] = arguments[_i];
3741
}
38-
return tag.apply(void 0, __spreadArray(["canvas"], children));
42+
return tag.apply(void 0, __spreadArray(["canvas"], children, false));
3943
}
4044
function h1() {
4145
var children = [];
4246
for (var _i = 0; _i < arguments.length; _i++) {
4347
children[_i] = arguments[_i];
4448
}
45-
return tag.apply(void 0, __spreadArray(["h1"], children));
49+
return tag.apply(void 0, __spreadArray(["h1"], children, false));
4650
}
4751
function h2() {
4852
var children = [];
4953
for (var _i = 0; _i < arguments.length; _i++) {
5054
children[_i] = arguments[_i];
5155
}
52-
return tag.apply(void 0, __spreadArray(["h2"], children));
56+
return tag.apply(void 0, __spreadArray(["h2"], children, false));
5357
}
5458
function h3() {
5559
var children = [];
5660
for (var _i = 0; _i < arguments.length; _i++) {
5761
children[_i] = arguments[_i];
5862
}
59-
return tag.apply(void 0, __spreadArray(["h3"], children));
63+
return tag.apply(void 0, __spreadArray(["h3"], children, false));
6064
}
6165
function p() {
6266
var children = [];
6367
for (var _i = 0; _i < arguments.length; _i++) {
6468
children[_i] = arguments[_i];
6569
}
66-
return tag.apply(void 0, __spreadArray(["p"], children));
70+
return tag.apply(void 0, __spreadArray(["p"], children, false));
6771
}
6872
function a() {
6973
var children = [];
7074
for (var _i = 0; _i < arguments.length; _i++) {
7175
children[_i] = arguments[_i];
7276
}
73-
return tag.apply(void 0, __spreadArray(["a"], children));
77+
return tag.apply(void 0, __spreadArray(["a"], children, false));
7478
}
7579
function div() {
7680
var children = [];
7781
for (var _i = 0; _i < arguments.length; _i++) {
7882
children[_i] = arguments[_i];
7983
}
80-
return tag.apply(void 0, __spreadArray(["div"], children));
84+
return tag.apply(void 0, __spreadArray(["div"], children, false));
8185
}
8286
function span() {
8387
var children = [];
8488
for (var _i = 0; _i < arguments.length; _i++) {
8589
children[_i] = arguments[_i];
8690
}
87-
return tag.apply(void 0, __spreadArray(["span"], children));
91+
return tag.apply(void 0, __spreadArray(["span"], children, false));
8892
}
8993
function select() {
9094
var children = [];
9195
for (var _i = 0; _i < arguments.length; _i++) {
9296
children[_i] = arguments[_i];
9397
}
94-
return tag.apply(void 0, __spreadArray(["select"], children));
98+
return tag.apply(void 0, __spreadArray(["select"], children, false));
9599
}
96100
function img(src) {
97101
return tag("img").att$("src", src);
@@ -117,8 +121,7 @@ function router(routes) {
117121
result.appendChild(routes[hashLocation]);
118122
return result;
119123
}
120-
;
121124
syncHash();
122-
window.addEventListener("hashchange", syncHash);
125+
window.addEventListener('hashchange', syncHash);
123126
return result;
124127
}

js/index.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function compileShaderSource(gl, source, shaderType) {
2525
gl.shaderSource(shader, source);
2626
gl.compileShader(shader);
2727
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
28-
throw new Error("Could not compile " + shaderTypeToString() + " shader: " + gl.getShaderInfoLog(shader));
28+
throw new Error("Could not compile ".concat(shaderTypeToString(), " shader: ").concat(gl.getShaderInfoLog(shader)));
2929
}
3030
return shader;
3131
}
@@ -43,7 +43,7 @@ function linkShaderProgram(gl, shaders, vertexAttribs) {
4343
}
4444
gl.linkProgram(program);
4545
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
46-
throw new Error("Could not link shader program: " + gl.getProgramInfoLog(program));
46+
throw new Error("Could not link shader program: ".concat(gl.getProgramInfoLog(program)));
4747
}
4848
return program;
4949
}
@@ -77,36 +77,36 @@ function loadFilterProgram(gl, filter, vertexAttribs) {
7777
var paramsInputs = {};
7878
var _loop_1 = function (paramName) {
7979
if (paramName in uniforms) {
80-
throw new Error("Redefinition of existing uniform parameter " + paramName);
80+
throw new Error("Redefinition of existing uniform parameter ".concat(paramName));
8181
}
8282
switch (filter.params[paramName].type) {
8383
case "float":
8484
{
8585
var valuePreview_1 = span(filter.params[paramName].init.toString());
8686
var valueInput = input("range");
8787
if (filter.params[paramName].min !== undefined) {
88-
valueInput.att$("min", filter.params[paramName].min);
88+
valueInput.att$("min", filter.params[paramName].min.toString());
8989
}
9090
if (filter.params[paramName].max !== undefined) {
91-
valueInput.att$("max", filter.params[paramName].max);
91+
valueInput.att$("max", filter.params[paramName].max.toString());
9292
}
9393
if (filter.params[paramName].step !== undefined) {
94-
valueInput.att$("step", filter.params[paramName].step);
94+
valueInput.att$("step", filter.params[paramName].step.toString());
9595
}
9696
if (filter.params[paramName].init !== undefined) {
97-
valueInput.att$("value", filter.params[paramName].init);
97+
valueInput.att$("value", filter.params[paramName].init.toString());
9898
}
9999
paramsInputs[paramName] = valueInput;
100-
valueInput.oninput = function () {
101-
valuePreview_1.innerText = this.value;
100+
valueInput.oninput = function (e) {
101+
valuePreview_1.innerText = e.currentTarget.value;
102102
paramsPanel.dispatchEvent(new CustomEvent("paramsChanged"));
103103
};
104104
var label = (_a = filter.params[paramName].label) !== null && _a !== void 0 ? _a : paramName;
105-
paramsPanel.appendChild(div(span(label + ": "), valuePreview_1, div(valueInput)));
105+
paramsPanel.appendChild(div(span("".concat(label, ": ")), valuePreview_1, div(valueInput)));
106106
}
107107
break;
108108
default: {
109-
throw new Error("Filter parameters do not support type " + filter.params[paramName].type);
109+
throw new Error("Filter parameters do not support type ".concat(filter.params[paramName].type));
110110
}
111111
}
112112
uniforms[paramName] = gl.getUniformLocation(id, paramName);
@@ -136,7 +136,7 @@ function ImageSelector() {
136136
var imageInput = input("file");
137137
var imagePreview = img("img/tsodinClown.png")
138138
.att$("class", "widget-element")
139-
.att$("width", CANVAS_WIDTH);
139+
.att$("width", String(CANVAS_WIDTH));
140140
var root = div(div(imageInput).att$("class", "widget-element"), imagePreview).att$("class", "widget");
141141
root.selectedImage$ = function () {
142142
return imagePreview;
@@ -155,7 +155,7 @@ function ImageSelector() {
155155
};
156156
root.updateFiles$ = function (files) {
157157
imageInput.files = files;
158-
imageInput.onchange();
158+
imageInput.dispatchEvent(new UIEvent("change", { view: window, bubbles: true }));
159159
};
160160
imagePreview.addEventListener('load', function () {
161161
root.dispatchEvent(new CustomEvent("imageSelected", {
@@ -168,8 +168,8 @@ function ImageSelector() {
168168
imageInput.value = '';
169169
this.src = 'img/error.png';
170170
});
171-
imageInput.onchange = function () {
172-
imagePreview.src = URL.createObjectURL(this.files[0]);
171+
imageInput.onchange = function (e) {
172+
imagePreview.src = URL.createObjectURL(e.currentTarget.files[0]);
173173
};
174174
return root;
175175
}
@@ -196,15 +196,15 @@ function FilterList() {
196196
if (e.deltaY > 0) {
197197
root.selectedIndex = Math.min(root.selectedIndex + 1, root.length - 1);
198198
}
199-
root.onchange();
199+
root.dispatchEvent(new UIEvent("change", { view: window, bubbles: true }));
200200
});
201201
return root;
202202
}
203203
function FilterSelector() {
204204
var filterList_ = FilterList();
205205
var filterPreview = canvas()
206-
.att$("width", CANVAS_WIDTH)
207-
.att$("height", CANVAS_HEIGHT);
206+
.att$("width", String(CANVAS_WIDTH))
207+
.att$("height", String(CANVAS_HEIGHT));
208208
var root = div(div("Filter: ", filterList_)
209209
.att$("class", "widget-element"), filterPreview.att$("class", "widget-element")).att$("class", "widget");
210210
var gl = filterPreview.getContext("webgl", { antialias: false, alpha: false });
@@ -348,7 +348,7 @@ function FilterSelector() {
348348
delay: dt * 1000,
349349
dispose: 2,
350350
});
351-
renderProgress.style.width = (t / duration) * 50 + "%";
351+
renderProgress.style.width = "".concat((t / duration) * 50, "%");
352352
t += dt;
353353
}
354354
gif.on('finished', function (blob) {
@@ -360,7 +360,7 @@ function FilterSelector() {
360360
renderSpinner.style.display = "none";
361361
});
362362
gif.on('progress', function (p) {
363-
renderProgress.style.width = 50 + p * 50 + "%";
363+
renderProgress.style.width = "".concat(50 + p * 50, "%");
364364
});
365365
gif.render();
366366
return gif;
@@ -416,6 +416,6 @@ window.onload = function () {
416416
gif.abort();
417417
}
418418
var fileName = imageSelector.selectedFileName$();
419-
gif = filterSelector.render$(fileName + ".gif");
419+
gif = filterSelector.render$("".concat(fileName, ".gif"));
420420
};
421421
};

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
"homepage": "https://github.com/tsoding/emoteJAM#readme",
2828
"devDependencies": {
2929
"@types/gif.js": "^0.2.1",
30-
"typescript": "^4.3.2"
30+
"typescript": "^4.5.4"
3131
}
3232
}

ts/eval.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,4 @@ function run_expr(expr: Expr, user_context: UserContext = {}): number {
289289
throw new Error(`Unexpected AST node '${expr.kind}'`);
290290
}
291291
}
292-
}
292+
}

0 commit comments

Comments
 (0)