How to use the edge-parser.expressions.Literal function in edge-parser

To help you get started, we’ve selected a few edge-parser examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github edge-js / edge / src / Tags / Set.ts View on Github external
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)});`)
  },
}
github edge-js / edge / src / Tags / Include.ts View on Github external
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)`)
github edge-js / edge / src / Tags / Component.ts View on Github external
* 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
github edge-js / edge / src / Tags / Component.ts View on Github external
/**
   * 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(