Skip to content

Commit

Permalink
refactor: finalize @include tag
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Mar 26, 2020
1 parent d1a581f commit 202f03b
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 44 deletions.
12 changes: 5 additions & 7 deletions fixtures/include-conditionals/compiled.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
return (function (template, ctx) {
let out = "";
ctx.$lineNumber = 1;
ctx.$filename = "{{__dirname}}index.edge";
let $lineNumber = 1;
let $filename = "{{__dirname}}index.edge";
try {
out += `${template.renderInline(`include-conditionals/${ctx.resolve('username') === "virk" ? "virk.edge" : "guest.edge"}`)(template, ctx)}`;
out += `${template.renderInline(`include-conditionals/${state.username === "virk" ? "virk.edge" : "guest.edge"}`)(template,state,ctx)}`;
} catch (error) {
ctx.reThrow(error);
ctx.reThrow(error, $filename, $lineNumber);
}
return out;
})(template, ctx)
return out;
12 changes: 5 additions & 7 deletions fixtures/include-identifier/compiled.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
return (function (template, ctx) {
let out = "";
ctx.$lineNumber = 1;
ctx.$filename = "{{__dirname}}index.edge";
let $lineNumber = 1;
let $filename = "{{__dirname}}index.edge";
try {
out += `${template.renderInline(ctx.resolve('partial'))(template, ctx)}`;
out += `${template.renderInline(state.partial)(template,state,ctx)}`;
} catch (error) {
ctx.reThrow(error);
ctx.reThrow(error, $filename, $lineNumber);
}
return out;
})(template, ctx)
return out;
12 changes: 5 additions & 7 deletions fixtures/include-literal/compiled.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
return (function (template, ctx) {
let out = "";
ctx.$lineNumber = 1;
ctx.$filename = "{{__dirname}}index.edge";
let $lineNumber = 1;
let $filename = "{{__dirname}}index.edge";
try {
out += `${template.renderInline("include-literal/partial")(template, ctx)}`;
out += `${template.renderInline("include-literal/partial")(template,state,ctx)}`;
} catch (error) {
ctx.reThrow(error);
ctx.reThrow(error, $filename, $lineNumber);
}
return out;
})(template, ctx)
return out;
11 changes: 11 additions & 0 deletions fixtures/include-nested-shared-locals/compiled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
let out = "";
let $lineNumber = 1;
let $filename = "{{__dirname}}index.edge";
try {
let username = 'virk';
$lineNumber = 2;
out += `${template.renderInline("include-nested-shared-locals/partial","username")(template,state,ctx,username)}`;
} catch (error) {
ctx.reThrow(error, $filename, $lineNumber);
}
return out;
2 changes: 2 additions & 0 deletions fixtures/include-nested-shared-locals/index.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@set('username', 'virk')
@include("include-nested-shared-locals/partial")
2 changes: 2 additions & 0 deletions fixtures/include-nested-shared-locals/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
1 change: 1 addition & 0 deletions fixtures/include-nested-shared-locals/index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello virk
1 change: 1 addition & 0 deletions fixtures/include-nested-shared-locals/partial-1.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello {{ username }}
1 change: 1 addition & 0 deletions fixtures/include-nested-shared-locals/partial.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@include("include-nested-shared-locals/partial-1")
12 changes: 5 additions & 7 deletions fixtures/include-nested/compiled.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
return (function (template, ctx) {
let out = "";
ctx.$lineNumber = 1;
ctx.$filename = "{{__dirname}}index.edge";
let $lineNumber = 1;
let $filename = "{{__dirname}}index.edge";
try {
out += `${template.renderInline("include-nested/partial")(template, ctx)}`;
out += `${template.renderInline("include-nested/partial")(template,state,ctx)}`;
} catch (error) {
ctx.reThrow(error);
ctx.reThrow(error, $filename, $lineNumber);
}
return out;
})(template, ctx)
return out;
12 changes: 5 additions & 7 deletions fixtures/include-shared-ctx/compiled.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
return (function (template, ctx) {
let out = "";
ctx.$lineNumber = 1;
ctx.$filename = "{{__dirname}}index.edge";
let $lineNumber = 1;
let $filename = "{{__dirname}}index.edge";
try {
out += `${template.renderInline("include-shared-ctx/partial")(template, ctx)}`;
out += `${template.renderInline("include-shared-ctx/partial")(template,state,ctx)}`;
} catch (error) {
ctx.reThrow(error);
ctx.reThrow(error, $filename, $lineNumber);
}
return out;
})(template, ctx)
return out;
32 changes: 23 additions & 9 deletions src/Tags/Include.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import { expressions } from 'edge-parser'

import { TagContract } from '../Contracts'
import { unallowedExpression, isSubsetOf } from '../utils'
import { unallowedExpression, isSubsetOf, parseJsArg } from '../utils'

/**
* Include tag is used to include partials in the same scope of the parent
Expand All @@ -29,11 +29,11 @@ export const includeTag: TagContract = {
* Compiles else block node to Javascript else statement
*/
compile (parser, buffer, token) {
const parsed = parser.utils.transformAst(
parser.utils.generateAST(token.properties.jsArg, token.loc, token.filename),
token.filename,
)
const parsed = parseJsArg(parser, token)

/**
* Only mentioned expressions are allowed inside `@include` tag
*/
isSubsetOf(
parsed,
[
Expand All @@ -55,12 +55,26 @@ export const includeTag: TagContract = {
)

/**
* Include template. Since the partials can be a runtime value, we cannot inline
* the content right now and have to defer to runtime to get the value of
* the partial and then process it
* We need to pass the local variables to the partial render function
*/
const localVariables = parser.stack.list()

/**
* Arguments for the `renderInline` method
*/
const renderArgs = localVariables.length
? [parser.utils.stringify(parsed), localVariables.map((localVar) => `"${localVar}"`).join(',')]
: [parser.utils.stringify(parsed)]

/**
* Arguments for invoking the output function of `renderInline`
*/
const callFnArgs = localVariables.length
? ['template', 'state', 'ctx', localVariables.map((localVar) => localVar).join(',')]
: ['template', 'state', 'ctx']

buffer.outputExpression(
`template.renderInline(${parser.utils.stringify(parsed)})(template, ctx)`,
`template.renderInline(${renderArgs.join(',')})(${callFnArgs.join(',')})`,
token.filename,
token.loc.start.line,
true,
Expand Down

0 comments on commit 202f03b

Please sign in to comment.