diff --git a/fixtures/set-tag-identifier/compiled.js b/fixtures/set-tag-identifier/compiled.js index 91ce72d..3bef27c 100644 --- a/fixtures/set-tag-identifier/compiled.js +++ b/fixtures/set-tag-identifier/compiled.js @@ -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) \ No newline at end of file +return out; \ No newline at end of file diff --git a/fixtures/set-tag-update-value/compiled.js b/fixtures/set-tag-update-value/compiled.js new file mode 100644 index 0000000..864f5f8 --- /dev/null +++ b/fixtures/set-tag-update-value/compiled.js @@ -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; \ No newline at end of file diff --git a/fixtures/set-tag-update-value/index.edge b/fixtures/set-tag-update-value/index.edge new file mode 100644 index 0000000..798115b --- /dev/null +++ b/fixtures/set-tag-update-value/index.edge @@ -0,0 +1,3 @@ +@set("username", "nikk") +@set("username", "virk") +{{ username }} \ No newline at end of file diff --git a/fixtures/set-tag-update-value/index.json b/fixtures/set-tag-update-value/index.json new file mode 100644 index 0000000..d5f5540 --- /dev/null +++ b/fixtures/set-tag-update-value/index.json @@ -0,0 +1,3 @@ +{ + "username": "virk" +} \ No newline at end of file diff --git a/fixtures/set-tag-update-value/index.txt b/fixtures/set-tag-update-value/index.txt new file mode 100644 index 0000000..b7acc9a --- /dev/null +++ b/fixtures/set-tag-update-value/index.txt @@ -0,0 +1 @@ +virk \ No newline at end of file diff --git a/fixtures/set-tag/compiled.js b/fixtures/set-tag/compiled.js index f266228..dcf7897 100644 --- a/fixtures/set-tag/compiled.js +++ b/fixtures/set-tag/compiled.js @@ -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) \ No newline at end of file +return out; \ No newline at end of file diff --git a/src/Tags/Set.ts b/src/Tags/Set.ts index 8803b9a..7b6a6e3 100644 --- a/src/Tags/Set.ts +++ b/src/Tags/Set.ts @@ -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 @@ -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. @@ -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 @@ -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) }, }