Skip to content

Commit

Permalink
refactor: implement let tag
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Aug 10, 2023
1 parent 0d94e30 commit 5383d69
Show file tree
Hide file tree
Showing 47 changed files with 250 additions and 1 deletion.
13 changes: 13 additions & 0 deletions fixtures/let-tag-array-destructure/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, age] = state.user;
$lineNumber = 2;
out += `${template.escape(username)}`;
out += ", ";
out += `${template.escape(age)}`;
} catch (error) {
template.reThrow(error, $filename, $lineNumber);
}
return out;
2 changes: 2 additions & 0 deletions fixtures/let-tag-array-destructure/index.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@let([username, age] = user)
{{ username }}, {{ age }}
3 changes: 3 additions & 0 deletions fixtures/let-tag-array-destructure/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"user": ["virk", 32]
}
1 change: 1 addition & 0 deletions fixtures/let-tag-array-destructure/index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
virk, 32
15 changes: 15 additions & 0 deletions fixtures/let-tag-destructure-array-spread/compiled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let out = "";
let $lineNumber = 1;
let $filename = "{{__dirname}}index.edge";
try {
let [username, age, ...rest] = state.user;
$lineNumber = 2;
out += `${template.escape(username)}`;
out += ", ";
out += `${template.escape(age)}`;
out += " ";
out += `${template.escape(rest)}`;
} catch (error) {
template.reThrow(error, $filename, $lineNumber);
}
return out;
2 changes: 2 additions & 0 deletions fixtures/let-tag-destructure-array-spread/index.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@let([username, age, ...rest] = user)
{{ username }}, {{ age }} {{ rest }}
3 changes: 3 additions & 0 deletions fixtures/let-tag-destructure-array-spread/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"user": ["virk", 32]
}
1 change: 1 addition & 0 deletions fixtures/let-tag-destructure-array-spread/index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
virk, 32
13 changes: 13 additions & 0 deletions fixtures/let-tag-destructure-reassign/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, age: userAge} = state.user;
$lineNumber = 2;
out += `${template.escape(username)}`;
out += ", ";
out += `${template.escape(userAge)}`;
} catch (error) {
template.reThrow(error, $filename, $lineNumber);
}
return out;
2 changes: 2 additions & 0 deletions fixtures/let-tag-destructure-reassign/index.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@let({ username, age: userAge } = user)
{{ username }}, {{ userAge }}
6 changes: 6 additions & 0 deletions fixtures/let-tag-destructure-reassign/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"user": {
"username": "virk",
"age": 32
}
}
1 change: 1 addition & 0 deletions fixtures/let-tag-destructure-reassign/index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
virk, 32
15 changes: 15 additions & 0 deletions fixtures/let-tag-destructure-spread/compiled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let out = "";
let $lineNumber = 1;
let $filename = "{{__dirname}}index.edge";
try {
let {username, age, ...rest} = state.user;
$lineNumber = 2;
out += `${template.escape(username)}`;
out += ", ";
out += `${template.escape(age)}`;
out += " ";
out += `${template.escape(rest)}`;
} catch (error) {
template.reThrow(error, $filename, $lineNumber);
}
return out;
2 changes: 2 additions & 0 deletions fixtures/let-tag-destructure-spread/index.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@let({ username, age, ...rest } = user)
{{ username }}, {{ age }} {{ rest }}
6 changes: 6 additions & 0 deletions fixtures/let-tag-destructure-spread/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"user": {
"username": "virk",
"age": 32
}
}
1 change: 1 addition & 0 deletions fixtures/let-tag-destructure-spread/index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
virk, 32 [object Object]
13 changes: 13 additions & 0 deletions fixtures/let-tag-destructure/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, age} = state.user;
$lineNumber = 2;
out += `${template.escape(username)}`;
out += ", ";
out += `${template.escape(age)}`;
} catch (error) {
template.reThrow(error, $filename, $lineNumber);
}
return out;
2 changes: 2 additions & 0 deletions fixtures/let-tag-destructure/index.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@let({ username, age } = user)
{{ username }}, {{ age }}
6 changes: 6 additions & 0 deletions fixtures/let-tag-destructure/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"user": {
"username": "virk",
"age": 32
}
}
1 change: 1 addition & 0 deletions fixtures/let-tag-destructure/index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
virk, 32
24 changes: 24 additions & 0 deletions fixtures/let-tag-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-tag-scope/index.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@each(user in users)
@let(index = 0)
@eval(index = index + 1)
{{ index }}
@end
@eval(index = (index || 0) + 1)

{{ index }}
3 changes: 3 additions & 0 deletions fixtures/let-tag-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-tag-scope/index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1
1
11 changes: 11 additions & 0 deletions fixtures/let-tag/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 = 'nikk';
$lineNumber = 2;
out += `${template.escape(username)}`;
} catch (error) {
template.reThrow(error, $filename, $lineNumber);
}
return out;
2 changes: 2 additions & 0 deletions fixtures/let-tag/index.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@let(username = 'nikk')
{{ username }}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions fixtures/set-tag-compat/index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nikk
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions fixtures/set-tag-update-value-compat/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"username": "virk"
}
File renamed without changes.
86 changes: 86 additions & 0 deletions src/tags/let.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* edge.js
*
* (c) EdgeJS
*
* For the full copyright and license information, please view the LICENSE
* 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'

/**
* The let tag is used to set runtime values within the template. The value
* is set inside the current scope of the template.
*/
export const letTag: TagContract = {
block: false,
seekable: true,
tagName: 'let',
noNewLine: true,

/**
* Compiles else block node to Javascript else statement
*/
compile(parser, buffer, token) {
const parsed = parser.utils.generateAST(
`let ${token.properties.jsArg}`,
token.loc,
token.filename
).declarations[0]

const key = parsed.id
const value = parsed.init

/**
* The variable name has to be an identifier or the destructuring
* operator.
*/
isSubsetOf(key, ['ObjectPattern', expressions.Identifier, 'ArrayPattern'], () => {
throw unallowedExpression(
`Invalid variable name for the @let tag`,
token.filename,
parser.utils.getExpressionLoc(key)
)
})

/**
* Define local variables based upon the expression
*/
if (key.type === 'Identifier') {
parser.stack.defineVariable(key.name)
} else if (key.type === 'ObjectPattern') {
key.properties.forEach((property: any) => {
parser.stack.defineVariable(
property.argument ? property.argument.name : property.value.name
)
})
} else if (key.type === 'ArrayPattern') {
key.elements.forEach((element: any) => {
parser.stack.defineVariable(element.argument ? element.argument.name : element.name)
})
}

/**
* Declare let variable
*/
const expression = `let ${parser.utils.stringify(key)} = ${parser.utils.stringify(
parser.utils.transformAst(value, token.filename, parser)
)}`

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

/**
* Add methods to the template for running the loop
*/
boot(template) {
template.macro('setValue', lodash.set)
},
}
1 change: 1 addition & 0 deletions src/tags/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

export { ifTag as if } from './if.js'
export { letTag as let } from './let.js'
export { eachTag as each } from './each.js'
export { slotTag as slot } from './slot.js'
export { elseTag as else } from './else.js'
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { TagToken } from 'edge-lexer/types'
import { find, html } from 'property-information'
import { expressions as expressionsList, Parser } from 'edge-parser'

type ExpressionList = readonly (keyof typeof expressionsList)[]
type ExpressionList = readonly (keyof typeof expressionsList | 'ObjectPattern' | 'ArrayPattern')[]

/**
* Function to register custom properties
Expand Down

0 comments on commit 5383d69

Please sign in to comment.