Skip to content

Commit

Permalink
feat: add assign tag
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Aug 10, 2023
1 parent 5383d69 commit a76e7ac
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 3 deletions.
24 changes: 24 additions & 0 deletions fixtures/let-assign-with-each-scope/compiled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
let out = "";
let $lineNumber = 1;
let $filename = "{{__dirname}}index.edge";
try {
template.loop(state.users, function (user) {
out += "\n";
$lineNumber = 2;
let index = 0;
$lineNumber = 3;
index = index + 1;
out += " ";
$lineNumber = 4;
out += `${template.escape(index)}`;
});
$lineNumber = 6;
state.index = (state.index || 0) + 1;
out += "";
out += "\n";
$lineNumber = 8;
out += `${template.escape(state.index)}`;
} catch (error) {
template.reThrow(error, $filename, $lineNumber);
}
return out;
8 changes: 8 additions & 0 deletions fixtures/let-assign-with-each-scope/index.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@each(user in users)
@let(index = 0)
@assign(index = index + 1)
{{ index }}
@end
@assign(index = (index || 0) + 1)

{{ index }}
3 changes: 3 additions & 0 deletions fixtures/let-assign-with-each-scope/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"users": ["virk"]
}
2 changes: 2 additions & 0 deletions fixtures/let-assign-with-each-scope/index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1
1
48 changes: 48 additions & 0 deletions src/tags/assign.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* edge.js
*
* (c) EdgeJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { expressions } from 'edge-parser'
import lodash from '@poppinss/utils/lodash'

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

/**
* The assign tag is used to re-assign value to an existing variable
*/
export const assignTag: TagContract = {
block: false,
seekable: true,
tagName: 'assign',
noNewLine: true,

/**
* Compiles else block node to Javascript else statement
*/
compile(parser, buffer, token) {
const parsed = parseJsArg(parser, token)

isSubsetOf(parsed, [expressions.AssignmentExpression], () => {
throw unallowedExpression(
`Invalid expression for the @assign tag`,
token.filename,
parser.utils.getExpressionLoc(parsed)
)
})

buffer.writeExpression(parser.utils.stringify(parsed), token.filename, token.loc.start.line)
},

/**
* Add methods to the template for running the loop
*/
boot(template) {
template.macro('setValue', lodash.set)
},
}
4 changes: 1 addition & 3 deletions src/tags/let.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
* file that was distributed with this source code.
*/

import { EdgeError } from 'edge-error'
import { expressions } from 'edge-parser'
import lodash from '@poppinss/utils/lodash'

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

/**
* The let tag is used to set runtime values within the template. The value
Expand Down Expand Up @@ -74,7 +73,6 @@ export const letTag: TagContract = {
)}`

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

/**
Expand Down
1 change: 1 addition & 0 deletions src/tags/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export { eachTag as each } from './each.js'
export { slotTag as slot } from './slot.js'
export { elseTag as else } from './else.js'
export { evalTag as eval } from './eval.js'
export { assignTag as assign } from './assign.js'
export { injectTag as inject } from './inject.js'
export { unlessTag as unless } from './unless.js'
export { elseIfTag as elseif } from './else_if.js'
Expand Down

0 comments on commit a76e7ac

Please sign in to comment.