From ccac9f71dd49545a223428bcf41dc0b1ddef71d1 Mon Sep 17 00:00:00 2001 From: Ben Gubler Date: Mon, 21 Sep 2020 12:54:19 -0600 Subject: [PATCH] Add support for layout parameters --- deno_dist/compile-string.ts | 8 ++++---- src/compile-string.ts | 8 ++++---- test/async.spec.ts | 12 ++++++------ test/compile-string.spec.ts | 28 ++++++++++++++-------------- test/compile.spec.ts | 6 +++--- test/file-handlers.spec.ts | 18 +++++++++--------- 6 files changed, 40 insertions(+), 40 deletions(-) diff --git a/deno_dist/compile-string.ts b/deno_dist/compile-string.ts index 899d3d8..3d5b79f 100644 --- a/deno_dist/compile-string.ts +++ b/deno_dist/compile-string.ts @@ -24,20 +24,20 @@ export default function compileToString( ): string { var buffer: Array = Parse(str, config); - var res = "var tR='',__l" + + var res = "var tR='',__l,__lP" + (config.include ? ",include=E.include.bind(E)" : "") + (config.includeFile ? ",includeFile=E.includeFile.bind(E)" : "") + - "\nfunction layout(p){__l=p}\n" + + "\nfunction layout(p,d){__l=p;__lP=d}\n" + (config.useWith ? "with(" + config.varName + "||{}){" : "") + compileScope(buffer, config) + (config.includeFile ? "if(__l)tR=" + (config.async ? "await " : "") + - `includeFile(__l,Object.assign(${config.varName},{body:tR}))\n` + `includeFile(__l,Object.assign(__lP||${config.varName},{body:tR}))\n` : config.include ? "if(__l)tR=" + (config.async ? "await " : "") + - `include(__l,Object.assign(${config.varName},{body:tR}))\n` + `include(__l,Object.assign(__lP||${config.varName},{body:tR}))\n` : "") + "if(cb){cb(null,tR)} return tR" + (config.useWith ? "}" : ""); diff --git a/src/compile-string.ts b/src/compile-string.ts index 8dca6a3..76e8018 100644 --- a/src/compile-string.ts +++ b/src/compile-string.ts @@ -22,20 +22,20 @@ export default function compileToString(str: string, config: EtaConfig): string var buffer: Array = Parse(str, config) var res = - "var tR='',__l" + + "var tR='',__l,__lP" + (config.include ? ',include=E.include.bind(E)' : '') + (config.includeFile ? ',includeFile=E.includeFile.bind(E)' : '') + - '\nfunction layout(p){__l=p}\n' + + '\nfunction layout(p,d){__l=p;__lP=d}\n' + (config.useWith ? 'with(' + config.varName + '||{}){' : '') + compileScope(buffer, config) + (config.includeFile ? 'if(__l)tR=' + (config.async ? 'await ' : '') + - `includeFile(__l,Object.assign(${config.varName},{body:tR}))\n` + `includeFile(__l,Object.assign(__lP||${config.varName},{body:tR}))\n` : config.include ? 'if(__l)tR=' + (config.async ? 'await ' : '') + - `include(__l,Object.assign(${config.varName},{body:tR}))\n` + `include(__l,Object.assign(__lP||${config.varName},{body:tR}))\n` : '') + 'if(cb){cb(null,tR)} return tR' + (config.useWith ? '}' : '') diff --git a/test/async.spec.ts b/test/async.spec.ts index ef4d1c0..dabb681 100644 --- a/test/async.spec.ts +++ b/test/async.spec.ts @@ -47,10 +47,10 @@ describe('Async Render checks', () => { await Eta.render('<%= @#$%^ %>', {}, { async: true }) }).rejects.toThrow( buildRegEx(` -var tR='',__l,include=E.include.bind(E),includeFile=E.includeFile.bind(E) -function layout(p){__l=p} +var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E) +function layout(p,d){__l=p;__lP=d} tR+=E.e(@#$%^) -if(__l)tR=await includeFile(__l,Object.assign(it,{body:tR})) +if(__l)tR=await includeFile(__l,Object.assign(__lP||it,{body:tR})) if(cb){cb(null,tR)} return tR `) ) @@ -61,10 +61,10 @@ if(cb){cb(null,tR)} return tR expect(err).toBeTruthy() expect((err as Error).message).toMatch( buildRegEx(` -var tR='',__l,include=E.include.bind(E),includeFile=E.includeFile.bind(E) -function layout(p){__l=p} +var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E) +function layout(p,d){__l=p;__lP=d} tR+=E.e(@#$%^) -if(__l)tR=await includeFile(__l,Object.assign(it,{body:tR})) +if(__l)tR=await includeFile(__l,Object.assign(__lP||it,{body:tR})) if(cb){cb(null,tR)} return tR `) ) diff --git a/test/compile-string.spec.ts b/test/compile-string.spec.ts index c2dcb21..9d18d1b 100644 --- a/test/compile-string.spec.ts +++ b/test/compile-string.spec.ts @@ -10,11 +10,11 @@ const complexTemplate = fs.readFileSync(filePath, 'utf8') describe('Compile to String test', () => { it('parses a simple template', () => { var str = compileToString('hi <%= hey %>', defaultConfig) - expect(str).toEqual(`var tR='',__l,include=E.include.bind(E),includeFile=E.includeFile.bind(E) -function layout(p){__l=p} + expect(str).toEqual(`var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E) +function layout(p,d){__l=p;__lP=d} tR+='hi ' tR+=E.e(hey) -if(__l)tR=includeFile(__l,Object.assign(it,{body:tR})) +if(__l)tR=includeFile(__l,Object.assign(__lP||it,{body:tR})) if(cb){cb(null,tR)} return tR`) }) @@ -23,8 +23,8 @@ if(cb){cb(null,tR)} return tR`) 'hi <%= hey %>', getConfig({ include: undefined, includeFile: undefined }) ) - expect(str).toEqual(`var tR='',__l -function layout(p){__l=p} + expect(str).toEqual(`var tR='',__l,__lP +function layout(p,d){__l=p;__lP=d} tR+='hi ' tR+=E.e(hey) if(cb){cb(null,tR)} return tR`) @@ -32,30 +32,30 @@ if(cb){cb(null,tR)} return tR`) it('parses a simple template with raw tag', () => { var str = compileToString('hi <%~ hey %>', defaultConfig) - expect(str).toEqual(`var tR='',__l,include=E.include.bind(E),includeFile=E.includeFile.bind(E) -function layout(p){__l=p} + expect(str).toEqual(`var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E) +function layout(p,d){__l=p;__lP=d} tR+='hi ' tR+=hey -if(__l)tR=includeFile(__l,Object.assign(it,{body:tR})) +if(__l)tR=includeFile(__l,Object.assign(__lP||it,{body:tR})) if(cb){cb(null,tR)} return tR`) }) it('works with whitespace trimming', () => { var str = compileToString('hi\n<%- = hey-%>\n<%_ = hi_%>', defaultConfig) - expect(str).toEqual(`var tR='',__l,include=E.include.bind(E),includeFile=E.includeFile.bind(E) -function layout(p){__l=p} + expect(str).toEqual(`var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E) +function layout(p,d){__l=p;__lP=d} tR+='hi' tR+=E.e(hey) tR+=E.e(hi) -if(__l)tR=includeFile(__l,Object.assign(it,{body:tR})) +if(__l)tR=includeFile(__l,Object.assign(__lP||it,{body:tR})) if(cb){cb(null,tR)} return tR`) }) it('compiles complex template', () => { var str = compileToString(complexTemplate, defaultConfig) expect(str).toEqual( - `var tR='',__l,include=E.include.bind(E),includeFile=E.includeFile.bind(E) -function layout(p){__l=p} + `var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E) +function layout(p,d){__l=p;__lP=d} tR+='Hi\\n' console.log("Hope you like Eta!") tR+=E.e(it.htmlstuff) @@ -79,7 +79,7 @@ tR+=' \\n ' } tR+='\\nThis is a partial: ' tR+=include("mypartial") -if(__l)tR=includeFile(__l,Object.assign(it,{body:tR})) +if(__l)tR=includeFile(__l,Object.assign(__lP||it,{body:tR})) if(cb){cb(null,tR)} return tR` ) }) diff --git a/test/compile.spec.ts b/test/compile.spec.ts index d9b3d00..4af2b71 100644 --- a/test/compile.spec.ts +++ b/test/compile.spec.ts @@ -31,10 +31,10 @@ describe('Compile test', () => { compile('<% hi (=h) %>') }).toThrow( buildRegEx(` -var tR='',__l,include=E.include.bind(E),includeFile=E.includeFile.bind(E) -function layout(p){__l=p} +var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E) +function layout(p,d){__l=p;__lP=d} hi (=h) -if(__l)tR=includeFile(__l,Object.assign(it,{body:tR})) +if(__l)tR=includeFile(__l,Object.assign(__lP||it,{body:tR})) if(cb){cb(null,tR)} return tR `) ) diff --git a/test/file-handlers.spec.ts b/test/file-handlers.spec.ts index b7fdd5b..ca5e1a0 100644 --- a/test/file-handlers.spec.ts +++ b/test/file-handlers.spec.ts @@ -107,11 +107,11 @@ describe('renderFile error tests', () => { expect(err).toBeTruthy() expect(err.message).toMatch( buildRegEx(` -var tR='',__l,include=E.include.bind(E),includeFile=E.includeFile.bind(E) -function layout(p){__l=p} +var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E) +function layout(p,d){__l=p;__lP=d} tR+='Hi ' tR+=E.e(badSyntax(=!) -if(__l)tR=await includeFile(__l,Object.assign(it,{body:tR})) +if(__l)tR=await includeFile(__l,Object.assign(__lP||it,{body:tR})) if(cb){cb(null,tR)} return tR `) ) @@ -126,11 +126,11 @@ if(cb){cb(null,tR)} return tR await renderFile(errFilePath, {}) }).rejects.toThrow( buildRegEx(` -var tR='',__l,include=E.include.bind(E),includeFile=E.includeFile.bind(E) -function layout(p){__l=p} +var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E) +function layout(p,d){__l=p;__lP=d} tR+='Hi ' tR+=E.e(badSyntax(=!) -if(__l)tR=includeFile(__l,Object.assign(it,{body:tR})) +if(__l)tR=includeFile(__l,Object.assign(__lP||it,{body:tR})) if(cb){cb(null,tR)} return tR `) ) @@ -146,11 +146,11 @@ Bad template syntax Unexpected token '=' ==================== -var tR='',__l,include=E.include.bind(E),includeFile=E.includeFile.bind(E) -function layout(p){__l=p} +var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E) +function layout(p,d){__l=p;__lP=d} tR+='Hi ' tR+=E.e(badSyntax(=!) -if(__l)tR=includeFile(__l,Object.assign(it,{body:tR})) +if(__l)tR=includeFile(__l,Object.assign(__lP||it,{body:tR})) if(cb){cb(null,tR)} return tR */