Skip to content

NullVoxPopuli/ember-estree

Repository files navigation

ember-estree

ESTree-compatible AST parser for Ember's .gjs and .gts files.

Parses <template> tags into Glimmer AST nodes that are embedded directly in the ESTree, so tools like linters and codemods can work with both the JavaScript/TypeScript and template portions of a single file.

Install

pnpm add ember-estree

Usage

Parsing

toTree returns a File node whose .program is a standard ESTree Program, with any <template> regions represented as Glimmer* AST nodes.

import { toTree } from "ember-estree";

let ast = toTree(`
  import Component from "@glimmer/component";

  export default class Demo extends Component {
    <template>Hello, {{this.name}}!</template>
  }
`);

console.log(ast.type); // "File"
console.log(ast.program.body.length); // 2 — ImportDeclaration + ClassDeclaration

parse is a lower-level alternative that returns the Program node directly.

import { parse } from "ember-estree";

let program = parse(`const x = <template>hi</template>;`);
console.log(program.type); // "Program"

Printing

print converts an AST node (ESTree or Glimmer) back to source code.

import { print } from "ember-estree";

print({ type: "Identifier", name: "foo" });
// => "foo"

print({
  type: "GlimmerTemplate",
  body: [{ type: "GlimmerTextNode", chars: "Hello" }],
});
// => "<template>Hello</template>"

Examples

The examples/ directory contains ready-to-run integrations:

Example Description
eslint-parser Custom ESLint parser that understands <template>
zmod Codemod toolkit using zmod

License

MIT

About

ESTree generator for gjs and gts file used by ember

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors