diff --git a/fixtures/let-assign-with-each-scope/compiled.js b/fixtures/let-assign-with-each-scope/compiled.js new file mode 100644 index 0000000..cc77270 --- /dev/null +++ b/fixtures/let-assign-with-each-scope/compiled.js @@ -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; \ No newline at end of file diff --git a/fixtures/let-assign-with-each-scope/index.edge b/fixtures/let-assign-with-each-scope/index.edge new file mode 100644 index 0000000..2892d48 --- /dev/null +++ b/fixtures/let-assign-with-each-scope/index.edge @@ -0,0 +1,8 @@ +@each(user in users) + @let(index = 0) + @assign(index = index + 1) + {{ index }} +@end +@assign(index = (index || 0) + 1) + +{{ index }} \ No newline at end of file diff --git a/fixtures/let-assign-with-each-scope/index.json b/fixtures/let-assign-with-each-scope/index.json new file mode 100644 index 0000000..9621a8b --- /dev/null +++ b/fixtures/let-assign-with-each-scope/index.json @@ -0,0 +1,3 @@ +{ + "users": ["virk"] +} \ No newline at end of file diff --git a/fixtures/let-assign-with-each-scope/index.txt b/fixtures/let-assign-with-each-scope/index.txt new file mode 100644 index 0000000..0bc999a --- /dev/null +++ b/fixtures/let-assign-with-each-scope/index.txt @@ -0,0 +1,2 @@ +1 +1 \ No newline at end of file diff --git a/src/tags/assign.ts b/src/tags/assign.ts new file mode 100644 index 0000000..db5455d --- /dev/null +++ b/src/tags/assign.ts @@ -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) + }, +} diff --git a/src/tags/let.ts b/src/tags/let.ts index 9db16b4..5cb83ad 100644 --- a/src/tags/let.ts +++ b/src/tags/let.ts @@ -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 @@ -74,7 +73,6 @@ export const letTag: TagContract = { )}` buffer.writeExpression(expression, token.filename, token.loc.start.line) - parser.stack.defineVariable(key.value) }, /** diff --git a/src/tags/main.ts b/src/tags/main.ts index 54d3182..da7ee26 100644 --- a/src/tags/main.ts +++ b/src/tags/main.ts @@ -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'