Closed NullVoxPopuli closed 7 months ago
Figured it out, this is what I ultimately ended up using (also the latest code at the reproduction):
const source = `import './with-a-class.css';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import TEMPLATE from "./with-a-class.hbs";
import { f, i } from "decorator-transforms/runtime";
import { setComponentTemplate } from "@ember/component";
export default class WithAClass extends Component {
static {
f(this, "greeting", [tracked], function () {
return 'hello there';
});
}
#greeting = (i(this, "greeting"), void 0);
}
setComponentTemplate(TEMPLATE, WithAClass);`;
import recast from 'recast';
import * as acorn from 'acorn';
import babelParser from '@babel/parser';
try {
const ast = recast.parse(source, {
// The default parser doesn't support:
// - static initialization blocks
// - private fields
parser: {
parse(source) {
return babelParser.parse(source, {
sourceType: 'module',
allowImportExportEverywhere: true,
tokens: true,
});
},
},
});
console.log('success');
console.log(ast);
} catch (e) {
console.error(e.message);
console.error(e.stack);
}
It seems the acorns and espreema parsers are fairly incomplete.
I have a reproduction here: https://stackblitz.com/edit/stackblitz-starters-wetowk
code
```js import recast from 'recast'; import babelParser from '@babel/parser'; const source = `import './with-a-class.css'; import Component from '@glimmer/component'; import { tracked } from '@glimmer/tracking'; import TEMPLATE from "./with-a-class.hbs"; import { f, i } from "decorator-transforms/runtime"; import { setComponentTemplate } from "@ember/component"; export default class WithAClass extends Component { static { f(this, "greeting", [tracked], function () { return 'hello there'; }); } #greeting = (i(this, "greeting"), void 0); } setComponentTemplate(TEMPLATE, WithAClass);`; try { const ast = recast.parse(source, { parser: babelParser, }); console.log('success'); console.log(ast); } catch (e) { console.error(e.message); console.error(e.stack); } ```Error:
couple things of note:
#greeting
fieldpast iterations on this issue:
accidentally using runkit
I have a reproduction here, and I'm getting an error: https://runkit.com/nullvoxpopuli/recast-and-private-fieldscode
```js const { parse } = require('recast'); const babel = require('@babel/parser'); const source = `import './with-a-class.css'; import Component from '@glimmer/component'; import { tracked } from '@glimmer/tracking'; import TEMPLATE from "./with-a-class.hbs"; import { f, i } from "decorator-transforms/runtime"; import { setComponentTemplate } from "@ember/component"; export default class WithAClass extends Component { static { f(this, "greeting", [tracked], function () { return 'hello there'; }); } #greeting = (i(this, "greeting"), void 0); } setComponentTemplate(TEMPLATE, WithAClass);` try { const ast = parse(source, { parser: babel }); console.log(ast); } catch (e) { console.error(e.message); console.error(e.stack); } ```