ember-estree 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +70 -0
- package/package.json +1 -1
- package/src/parse.js +9 -1
package/README.md
CHANGED
|
@@ -1 +1,71 @@
|
|
|
1
1
|
# ember-estree
|
|
2
|
+
|
|
3
|
+
ESTree-compatible AST parser for Ember's `.gjs` and `.gts` files.
|
|
4
|
+
|
|
5
|
+
Parses `<template>` tags into [Glimmer](https://github.com/emberjs/ember.js/) 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.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add ember-estree
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### Parsing
|
|
16
|
+
|
|
17
|
+
`toTree` returns a `File` node whose `.program` is a standard ESTree `Program`, with any `<template>` regions represented as `Glimmer*` AST nodes.
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
import { toTree } from "ember-estree";
|
|
21
|
+
|
|
22
|
+
let ast = toTree(`
|
|
23
|
+
import Component from "@glimmer/component";
|
|
24
|
+
|
|
25
|
+
export default class Demo extends Component {
|
|
26
|
+
<template>Hello, {{this.name}}!</template>
|
|
27
|
+
}
|
|
28
|
+
`);
|
|
29
|
+
|
|
30
|
+
console.log(ast.type); // "File"
|
|
31
|
+
console.log(ast.program.body.length); // 2 — ImportDeclaration + ClassDeclaration
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
`parse` is a lower-level alternative that returns the `Program` node directly.
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
import { parse } from "ember-estree";
|
|
38
|
+
|
|
39
|
+
let program = parse(`const x = <template>hi</template>;`);
|
|
40
|
+
console.log(program.type); // "Program"
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Printing
|
|
44
|
+
|
|
45
|
+
`print` converts an AST node (ESTree _or_ Glimmer) back to source code.
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
import { print } from "ember-estree";
|
|
49
|
+
|
|
50
|
+
print({ type: "Identifier", name: "foo" });
|
|
51
|
+
// => "foo"
|
|
52
|
+
|
|
53
|
+
print({
|
|
54
|
+
type: "GlimmerTemplate",
|
|
55
|
+
body: [{ type: "GlimmerTextNode", chars: "Hello" }],
|
|
56
|
+
});
|
|
57
|
+
// => "<template>Hello</template>"
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Examples
|
|
61
|
+
|
|
62
|
+
The [`examples/`](./examples) directory contains ready-to-run integrations:
|
|
63
|
+
|
|
64
|
+
| Example | Description |
|
|
65
|
+
| ------------------------------------------- | -------------------------------------------------------------------- |
|
|
66
|
+
| [`eslint-parser`](./examples/eslint-parser) | Custom ESLint parser that understands `<template>` |
|
|
67
|
+
| [`zmod`](./examples/zmod) | Codemod toolkit using [zmod](https://github.com/nicolo-ribaudo/zmod) |
|
|
68
|
+
|
|
69
|
+
## License
|
|
70
|
+
|
|
71
|
+
MIT
|
package/package.json
CHANGED
package/src/parse.js
CHANGED
|
@@ -57,7 +57,7 @@ export function toTree(source, options = {}) {
|
|
|
57
57
|
|
|
58
58
|
outerAST = walk(outerAST, null, {
|
|
59
59
|
_(node, { next }) {
|
|
60
|
-
if (isExpressionPlaceholder(node)) {
|
|
60
|
+
if (isExpressionPlaceholder(node) || isClassMemberPlaceholder(node)) {
|
|
61
61
|
let parseResult = parseResults.find((r) => {
|
|
62
62
|
// WARNING: these are byte ranges
|
|
63
63
|
return node.start === r.range.start && node.end === r.range.end;
|
|
@@ -109,3 +109,11 @@ function isExpressionPlaceholder(node) {
|
|
|
109
109
|
|
|
110
110
|
return node.callee.name === "TEMPLATE_TEMPLATE";
|
|
111
111
|
}
|
|
112
|
+
|
|
113
|
+
function isClassMemberPlaceholder(node) {
|
|
114
|
+
if (node.type !== "PropertyDefinition") return;
|
|
115
|
+
|
|
116
|
+
return (
|
|
117
|
+
node.computed && node.key?.type === "CallExpression" && node.key.callee?.name === "_TEMPLATE_"
|
|
118
|
+
);
|
|
119
|
+
}
|