@@ -4,12 +4,18 @@ import generate from './generator.js';
44import { renameBindings , renameFunctions , renameTypes } from './utils.js' ;
55import { UNKNOWN_TYPE } from './grammar.js' ;
66import { buildParser , nextWarn } from './test-helpers.js' ;
7-
8- let c ! : ReturnType < typeof buildParser > ;
9- beforeAll ( ( ) => ( c = buildParser ( ) ) ) ;
7+ import { Program } from '../ast/index.js' ;
8+ import { ParserOptions } from './parser.js' ;
9+
10+ let c ! : Awaited < ReturnType < typeof buildParser > > ;
11+ let parse : ( src : string , options ?: ParserOptions ) => Program ;
12+ beforeAll ( async ( ) => {
13+ c = await buildParser ( ) ;
14+ parse = c . parse ;
15+ } ) ;
1016
1117test ( 'scope bindings and type names' , ( ) => {
12- const ast = c . parseSrc ( `
18+ const ast = parse ( `
1319float selfref, b = 1.0, c = selfref;
1420vec2 texcoord1, texcoord2;
1521vec3 position;
@@ -49,7 +55,7 @@ coherent buffer Block {
4955} ) ;
5056
5157test ( 'scope references' , ( ) => {
52- const ast = c . parseSrc (
58+ const ast = parse (
5359 `
5460float selfref, b = 1.0, c = selfref;
5561mat2x2 myMat = mat2( vec2( 1.0, 0.0 ), vec2( 0.0, 1.0 ) );
@@ -126,7 +132,7 @@ vec3 fnName(float arg1, vec3 arg2) {
126132} ) ;
127133
128134test ( 'scope binding declarations' , ( ) => {
129- const ast = c . parseSrc (
135+ const ast = parse (
130136 `
131137float selfref, b = 1.0, c = selfref;
132138void main() {
@@ -146,7 +152,7 @@ void main() {
146152} ) ;
147153
148154test ( 'struct constructor identified in scope' , ( ) => {
149- const ast = c . parseSrc ( `
155+ const ast = parse ( `
150156struct light {
151157 float intensity;
152158 vec3 position;
@@ -157,7 +163,7 @@ light lightVar = light(3.0, vec3(1.0, 2.0, 3.0));
157163} ) ;
158164
159165test ( 'function overloaded scope' , ( ) => {
160- const ast = c . parseSrc ( `
166+ const ast = parse ( `
161167vec4 overloaded(vec4 x) {
162168 return x;
163169}
@@ -169,7 +175,7 @@ float overloaded(float x) {
169175
170176test ( 'overriding glsl builtin function' , ( ) => {
171177 // "noise" is a built-in GLSL function that should be identified and renamed
172- const ast = c . parseSrc ( `
178+ const ast = parse ( `
173179float noise() {}
174180float fn() {
175181 vec2 uv;
@@ -192,7 +198,7 @@ float fn_FUNCTION() {
192198} ) ;
193199
194200test ( 'rename bindings and functions' , ( ) => {
195- const ast = c . parseSrc (
201+ const ast = parse (
196202 `
197203float selfref, b = 1.0, c = selfref;
198204mat2x2 myMat = mat2( vec2( 1.0, 0.0 ), vec2( 0.0, 1.0 ) );
@@ -292,7 +298,7 @@ vec4 linearToOutputTexel_FUNCTION( vec4 value ) { return LinearToLinear_FUNCTION
292298} ) ;
293299
294300test ( 'detecting struct scope and usage' , ( ) => {
295- const ast = c . parseSrc ( `
301+ const ast = parse ( `
296302struct StructName {
297303 vec3 color;
298304};
@@ -364,7 +370,7 @@ StructName_x main(in StructName_x x, StructName_x[3] y) {
364370} ) ;
365371
366372test ( 'fn args shadowing global scope identified as separate bindings' , ( ) => {
367- const ast = c . parseSrc ( `
373+ const ast = parse ( `
368374attribute vec3 position;
369375vec3 func(vec3 position) {
370376 return position;
@@ -381,7 +387,7 @@ vec3 func(vec3 position) {
381387} ) ;
382388
383389test ( 'I do not yet know what to do with layout()' , ( ) => {
384- const ast = c . parseSrc ( `
390+ const ast = parse ( `
385391layout(std140,column_major) uniform;
386392float a;
387393uniform Material
@@ -404,14 +410,14 @@ uniform vec2 vProp;
404410} ) ;
405411
406412test ( `(regression) ensure self-referenced variables don't appear as types` , ( ) => {
407- const ast = c . parseSrc ( `
413+ const ast = parse ( `
408414float a = 1.0, c = a;
409415` ) ;
410416 expect ( Object . keys ( ast . scopes [ 0 ] . types ) ) . toEqual ( [ ] ) ;
411417} ) ;
412418
413419test ( 'identifies a declared function with references' , ( ) => {
414- const ast = c . parseSrc ( `
420+ const ast = parse ( `
415421vec4[3] main(float a, vec3 b) {}
416422void x() {
417423 float a = 1.0;
@@ -432,7 +438,7 @@ void x() {
432438} ) ;
433439
434440test ( 'does not match function overload with different argument length' , ( ) => {
435- const ast = c . parseSrc (
441+ const ast = parse (
436442 `
437443float main(float a, float b) {}
438444void x() {
@@ -460,7 +466,7 @@ void x() {
460466} ) ;
461467
462468test ( 'handles declared, undeclared, and unknown function cases' , ( ) => {
463- const ast = c . parseSrc (
469+ const ast = parse (
464470 `
465471// Prototype for undeclared function
466472float main(float, float, float[3]);
@@ -509,7 +515,7 @@ void x() {
509515test ( 'warns on undeclared functions and structs' , ( ) => {
510516 const next = nextWarn ( ) ;
511517
512- c . parseSrc ( `
518+ parse ( `
513519MyStruct x = MyStruct();
514520void main() {
515521 a();
@@ -530,7 +536,7 @@ struct MyStruct { float y; };
530536test ( 'warns on duplicate declarations' , ( ) => {
531537 const next = nextWarn ( ) ;
532538
533- c . parseSrc ( `
539+ parse ( `
534540struct MyStruct { float y; };
535541struct MyStruct { float y; };
536542float dupefloat = 1.0;
@@ -548,7 +554,7 @@ void dupefn() {}
548554} ) ;
549555
550556test ( 'undeclared variables are added to the expected scope' , ( ) => {
551- const ast = c . parseSrc (
557+ const ast = parse (
552558 `
553559void a() {
554560 MyStruct x;
@@ -565,7 +571,7 @@ void a() {
565571} ) ;
566572
567573test ( 'postfix is added to scope' , ( ) => {
568- const ast = c . parseSrc ( `
574+ const ast = parse ( `
569575void a() {}
570576void main() {
571577 float y = a().xyz;
@@ -576,7 +582,7 @@ void main() {
576582} ) ;
577583
578584test ( 'rename function prototypes' , ( ) => {
579- const ast = c . parseSrc (
585+ const ast = parse (
580586 `vec3 hash3(vec3 p3);
581587vec3 hash3(vec3 p3) {}`
582588 ) ;
0 commit comments