How to use the @glimmer/syntax.preprocess function in @glimmer/syntax

To help you get started, we’ve selected a few @glimmer/syntax 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 brodybits / prettierx / src / language-handlebars / parser-glimmer.js View on Github external
function parse(text) {
  try {
    const glimmer = require("@glimmer/syntax").preprocess;
    return glimmer(text, {
      plugins: {
        ast: [removeWhiteSpace]
      }
    });
    /* istanbul ignore next */
  } catch (error) {
    const matches = error.message.match(/on line (\d+)/);
    if (matches) {
      throw createError(error.message, {
        start: { line: Number(matches[1]), column: 0 }
      });
    } else {
      throw error;
    }
  }
}
github cardstack / cardstack / packages / rendering / node-tests / template-helper.js View on Github external
module.exports = function processTemplate(template, opts = {}) {
  let newAst = preprocess(
    template,
    Object.assign(
      {},
      {
        plugins: {
          ast: [
            function(env) {
              return {
                name: 'plugin-under-test',
                visitors: {
                  Program: function(node) {
                    let plugin = new Plugin(env);
                    plugin.syntax = env.syntax;
                    return plugin.transform(node);
                  },
                },
github danakt / handlebars-to-jsx / src / index.ts View on Github external
export function compile(
  hbsCode: string,
  options: boolean | { isComponent?: boolean; isModule?: boolean; includeImport?: boolean } = true
): string {
  if (typeof options === 'boolean') {
    return compile(hbsCode, { isComponent: options })
  }

  const isComponent = !!options.isComponent
  const isModule = !!options.isModule
  const includeImport = !!options.includeImport && isModule

  const glimmerProgram = preprocess(hbsCode)
  const babelProgram: Babel.Program = createProgram(glimmerProgram, isComponent, isModule, includeImport)

  return generate(babelProgram).code
}
github emberwatch / ember-language-server / src / completion-provider / template-completion-provider.ts View on Github external
const uri = params.textDocument.uri;

    if (getExtension(params.textDocument) !== '.hbs') {
      return [];
    }

    const project = this.server.projectRoots.projectForUri(uri);
    if (!project) {
      return [];
    }

    let document = this.server.documents.get(uri);
    let offset = document.offsetAt(params.position);
    let originalText = document.getText();
    let text = originalText.slice(0, offset) + 'ELSCompletionDummy' + originalText.slice(offset);
    let ast = preprocess(text);
    let focusPath = ASTPath.toPosition(ast, toPosition(params.position));
    if (!focusPath) {
      return [];
    }

    const { root } = project;
    let completions: CompletionItem[] = [];

    if (isMustachePath(focusPath)) {
      completions.push(...listComponents(root));
      completions.push(...listHelpers(root));
      completions.push(...emberMustacheItems);
    } else if (isBlockPath(focusPath)) {
      completions.push(...listComponents(root));
      completions.push(...emberBlockItems);
    } else if (isSubExpressionPath(focusPath)) {
github ember-template-lint / ember-template-lint / lib / index.js View on Github external
verify(options) {
    let messages = [];
    let pendingStatus = this.statusForModule('pending', options.moduleId);
    let shouldIgnore = this.statusForModule('ignore', options.moduleId);

    if (shouldIgnore) {
      return messages;
    }

    let source = stripBom(options.source);

    let templateAST;

    try {
      templateAST = preprocess(source, {
        mode: 'codemod',
      });
    } catch (error) {
      let message = buildErrorMessage(options.moduleId, error);
      messages.push(message);
    }

    let rules = this.buildRules({
      results: messages,
      pending: pendingStatus,
      moduleId: options.moduleId,
      moduleName: options.moduleId,
      rawSource: source,
    });

    for (let rule of rules) {
github emberwatch / ember-language-server / src / symbols / hbs-document-symbol-provider.ts View on Github external
process(content: string): SymbolInformation[] {
    let ast = preprocess(content);

    let symbols: SymbolInformation[] = [];

    traverse(ast, {
      BlockStatement(node: any) {
        if (node.program.blockParams.length === 0) return;

        node.program.blockParams.forEach((blockParam: string) => {
          let symbol = SymbolInformation.create(blockParam, SymbolKind.Variable, toLSRange(node.loc));
          symbols.push(symbol);
        });
      }
    });

    return symbols;
  }
github ember-codemods / ember-angle-brackets-codemod / transforms / angle-brackets / angle-brackets-syntax.js View on Github external
module.exports = function(fileInfo, api, options) {
  const config = new Config(options);

  if (shouldSkipFile(fileInfo, config)) {
    return fileInfo.source;
  }

  const ast = glimmer.preprocess(fileInfo.source, {
    mode: 'codemod',
    parseOptions: { ignoreStandalone: true },
  });
  const b = glimmer.builders;

  /**
   * Transform the attributes names & values properly
   */
  const transformAttrs = attrs => {
    return attrs.map(a => {
      let _key = a.key;
      let _valueType = a.value.type;
      let _value;
      if (!isAttribute(a.key)) {
        _key = `@${_key}`;
      }
github emberwatch / ember-language-server / src / definition-provider.ts View on Github external
let uri = params.textDocument.uri;
    let filePath = uriToFilePath(uri);
    if (!filePath) {
      return null;
    }

    const project = this.server.projectRoots.projectForPath(filePath);
    if (!project) {
      return null;
    }

    let extension = getExtension(params.textDocument);

    if (extension === '.hbs') {
      let content = this.server.documents.get(uri).getText();
      let ast = preprocess(content);
      let focusPath = ASTPath.toPosition(ast, toPosition(params.position));
      if (!focusPath) {
        return null;
      }

      if (this.isComponentOrHelperName(focusPath)) {
        const componentOrHelperName = focusPath.node.original;

        const templatePath = path.join(project.root, 'app', 'components', `${componentOrHelperName}.js`);
        const componentPath = path.join(project.root, 'app', 'templates', 'components', `${componentOrHelperName}.hbs`);
        const helperPath = path.join(project.root, 'app', 'helpers', `${componentOrHelperName}.js`);

        return pathsToLocations(templatePath, componentPath, helperPath);
      }
    } else if (extension === '.js') {
      let content = this.server.documents.get(uri).getText();