Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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: parser.options.filename,
})
}
const [key, value] = parsed.expressions
/**
* The key has to be a literal value
*/
allowExpressions(
key,
[expressions.Literal],
parser.options.filename,
'The first argument for @set tag must be a string literal',
)
/**
* Write statement to mutate the key
*/
buffer.writeStatement(`ctx.set(${key.raw}, ${parser.stringifyExpression(value)});`)
},
}
compile (parser, buffer, token) {
const parsed = parser.generateEdgeExpression(token.properties.jsArg, token.loc)
allowExpressions(
parsed,
[
expressions.Identifier,
expressions.Literal,
expressions.LogicalExpression,
expressions.MemberExpression,
expressions.ConditionalExpression,
expressions.CallExpression,
expressions.TemplateLiteral,
],
parser.options.filename,
`{${token.properties.jsArg}} is not a valid argument type for the @include tag`,
)
/**
* Include template. Since the partials can be a runtime value, we cannot inline
* the content right now and have to defer to runtime to get the value of
* the partial and then process it
*/
buffer.writeLine(`template.renderInline(${parser.stringifyExpression(parsed)})(template, ctx)`)
* file that was distributed with this source code.
*/
import { EdgeError } from 'edge-error'
import { EdgeBuffer, expressions, Parser } from 'edge-parser'
import { TagContract } from '../Contracts'
import { StringifiedObject } from '../StringifiedObject'
import { expressionsToStringifyObject, isBlockToken, allowExpressions } from '../utils'
/**
* A list of allowed expressions for the component name
*/
const componentNameAllowedExpressions: (keyof typeof expressions)[] = [
expressions.Identifier,
expressions.Literal,
expressions.LogicalExpression,
expressions.MemberExpression,
expressions.ConditionalExpression,
expressions.CallExpression,
expressions.TemplateLiteral,
]
/**
* Returns the component name and props by parsing the component jsArg expression
*/
function getComponentNameAndProps (expression: any, parser: Parser): [string, string] {
let name
/**
* Use the first expression inside the sequence expression as the name
* of the component
/**
* Validating the slot name to be a literal value, since slot names cannot be dynamic
*/
allowExpressions(
name,
[expressions.Literal],
parser.options.filename,
'slot name must be a valid string literal',
)
/**
* Return the slot name with empty props, when the expression is a literal
* value.
*/
if (parsed.type === expressions.Literal) {
return [name.raw, null]
}
/**
* Make sure the sequence expression has only 2 arguments in it. Though it doesn't hurt
* the rendering of component, we must not run code with false expectations.
*/
if (parsed.expressions.length > 2) {
throw new EdgeError('maximum of 2 arguments are allowed for @slot tag', 'E_MAX_ARGUMENTS', {
line: parsed.loc.start.line,
col: parsed.loc.start.column,
filename: parser.options.filename,
})
}
allowExpressions(