Skip to content

Commit e27b8ff

Browse files
committed
feat(glsl): autodetect type calling function
1 parent 5fbf789 commit e27b8ff

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

src/glsl/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ function handleAlloc(init, typeAnnotation, name) {
337337
case 'vec4':
338338
case 'mat3':
339339
case 'mat4':
340-
if (init.arguments.length) {
340+
if (init.arguments.length || typeAnnotation !== init.callee.name) {
341341
allocation = ` = ${handleNode(init)}`;
342342
} else {
343343
allocation = '';

src/jstree.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,11 @@ function handleNode(node, options) {
187187
node.id = { ...node.id, typeAnnotation, qualifier };
188188
node.init = newInit;
189189

190-
options.scope[node.id.name] = typeAnnotation;
190+
if (typeAnnotation) {
191+
options.scope[node.id.name] = typeAnnotation;
192+
} else if (newInit.returnType) {
193+
options.scope[node.id.name] = newInit.returnType;
194+
}
191195

192196
return node;
193197
}

test/glsl/index.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,4 +362,51 @@ void main() {
362362

363363
assert.equal(glsl.trim(), expected.trim());
364364
});
365+
366+
it('works with glsl autodetect type calling function', () => {
367+
const { glsl } = buildGLSL(() => {
368+
let baz = float(() => {
369+
let foo = float(makeFloat());
370+
return foo;
371+
});
372+
373+
let bar = baz();
374+
});
375+
376+
const expected = `
377+
float baz() {
378+
\tfloat foo = float(makeFloat());
379+
\treturn foo;
380+
}
381+
float bar = baz();
382+
`;
383+
384+
assert.equal(glsl.trim(), expected.trim());
385+
});
386+
387+
// it('works with joining chunks auto detection', () => {
388+
// const one = buildGLSL(() => {
389+
// let foo = uniform(vec2);
390+
// });
391+
//
392+
// const two = buildGLSL(() => {
393+
// let bar = vec2(() => {
394+
// let res = foo;
395+
// return res;
396+
// });
397+
// });
398+
//
399+
// const { glsl } = joinGLSL([one, two]);
400+
//
401+
// const expected = `
402+
// uniform vec2 foo;
403+
// vec2 bar() {
404+
// \tvec2 res = foo;
405+
// \treturn res;
406+
// }
407+
// `;
408+
//
409+
//
410+
// assert.equal(glsl.trim(), expected.trim());
411+
// });
365412
});

test/jstree.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,28 @@ describe('jstree autodetect primitive tests', () => {
455455

456456
});
457457

458+
it('auto detects return type of other functions for inline variables', () => {
459+
const node = parse(`
460+
let bar = float(() => {
461+
return 5.0;
462+
});
463+
464+
let x = Vec2((y = Vec2()) => {
465+
let foo = bar();
466+
return vec2(foo);
467+
});
468+
`);
469+
470+
const [declarator] = node.body[1].declarations[0].init.body.body[0].declarations;
471+
const { id } = declarator;
472+
473+
assert.equal(id.type, 'Identifier');
474+
assert.equal(id.typeAnnotation, 'float');
475+
assert.equal(id.name, 'foo');
476+
assert.equal(id.qualifier, null);
477+
478+
});
479+
458480
it('auto detects type from builtin function', () => {
459481
const node = parse(`let x = Vec2((y = Vec2()) => {
460482
let foo = normalize(y);

0 commit comments

Comments
 (0)