Skip to content

Commit

Permalink
refactor: complete @set tag
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Mar 26, 2020
1 parent fb5471f commit b082e67
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 33 deletions.
16 changes: 7 additions & 9 deletions fixtures/set-tag-identifier/compiled.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
return (function (template, ctx) {
let out = "";
ctx.$lineNumber = 1;
ctx.$filename = "{{__dirname}}index.edge";
let $lineNumber = 1;
let $filename = "{{__dirname}}index.edge";
try {
ctx.set("username", ctx.resolve('username').split("")[0], false);
ctx.$lineNumber = 2;
out += `${ctx.escape(ctx.resolve('username'))}`;
let username = state.username.split("")[0];
$lineNumber = 2;
out += `${ctx.escape(username)}`;
} catch (error) {
ctx.reThrow(error);
ctx.reThrow(error, $filename, $lineNumber);
}
return out;
})(template, ctx)
return out;
13 changes: 13 additions & 0 deletions fixtures/set-tag-update-value/compiled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let out = "";
let $lineNumber = 1;
let $filename = "{{__dirname}}index.edge";
try {
let username = "nikk";
$lineNumber = 2;
username = "virk";
$lineNumber = 3;
out += `${ctx.escape(username)}`;
} catch (error) {
ctx.reThrow(error, $filename, $lineNumber);
}
return out;
3 changes: 3 additions & 0 deletions fixtures/set-tag-update-value/index.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@set("username", "nikk")
@set("username", "virk")
{{ username }}
3 changes: 3 additions & 0 deletions fixtures/set-tag-update-value/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"username": "virk"
}
1 change: 1 addition & 0 deletions fixtures/set-tag-update-value/index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
virk
16 changes: 7 additions & 9 deletions fixtures/set-tag/compiled.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
return (function (template, ctx) {
let out = "";
ctx.$lineNumber = 1;
ctx.$filename = "{{__dirname}}index.edge";
let $lineNumber = 1;
let $filename = "{{__dirname}}index.edge";
try {
ctx.set("username", "nikk", false);
ctx.$lineNumber = 2;
out += `${ctx.escape(ctx.resolve('username'))}`;
let username = "nikk";
$lineNumber = 2;
out += `${ctx.escape(username)}`;
} catch (error) {
ctx.reThrow(error);
ctx.reThrow(error, $filename, $lineNumber);
}
return out;
})(template, ctx)
return out;
31 changes: 16 additions & 15 deletions src/Tags/Set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { EdgeError } from 'edge-error'
import { expressions } from 'edge-parser'

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

/**
* The set tag is used to set runtime values within the template. The value
Expand Down Expand Up @@ -40,10 +40,7 @@ export const setTag: 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)

/**
* The set tag only accepts a sequence expression.
Expand All @@ -63,15 +60,15 @@ export const setTag: TagContract = {
/**
* Disallow more than 2 values for the sequence expression
*/
if (parsed.expressions.length > 3) {
throw new EdgeError('maximum of 3 arguments are allowed for the @set tag', 'E_MAX_ARGUMENTS', {
if (parsed.expressions.length > 2) {
throw new EdgeError('maximum of 2 arguments are allowed for the @set tag', 'E_MAX_ARGUMENTS', {
line: parsed.loc.start.line,
col: parsed.loc.start.column,
filename: token.filename,
})
}

const [key, value, isolated] = parsed.expressions
const [key, value] = parsed.expressions

/**
* The key has to be a literal value
Expand All @@ -82,19 +79,23 @@ export const setTag: TagContract = {
() => {
throw unallowedExpression(
'The first argument for @set tag must be a string literal',
parsed,
key,
token.filename,
)
},
)

/**
* Write statement to mutate the key
* Write statement to mutate the key. If the variable has already been
* defined, then just update it's value.
*
* We do not allow re-declaring a variable as of now
*/
buffer.writeExpression(
`ctx.set(${key.raw}, ${parser.utils.stringify(value)}, ${!!isolated})`,
token.filename,
token.loc.start.line,
)
const expression = parser.stack.has(key.value)
? `${key.value} = ${parser.utils.stringify(value)}`
: `let ${key.value} = ${parser.utils.stringify(value)}`

buffer.writeExpression(expression, token.filename, token.loc.start.line)
parser.stack.defineVariable(key.value)
},
}

0 comments on commit b082e67

Please sign in to comment.