@@ -2,19 +2,18 @@ import traverse from '@babel/traverse';
22import type {
33 BlockStatement ,
44 File ,
5- Node ,
65 ObjectExpression ,
76 StaticBlock ,
87} from '@babel/types' ;
98import type { Parser } from 'prettier' ;
10- import { parsers as babelParsers } from 'prettier/plugins/babel.js' ;
119
12- import { PRINTER_NAME } from '../config.js' ;
13- import type { Options } from '../options.js' ;
14- import { assert } from '../utils/assert.js' ;
15- import { preprocess , type Template } from './preprocess.js' ;
10+ import type { NodeType } from '../utils/index.js' ;
11+ import type { Template } from './preprocess.js' ;
1612
17- const typescript = babelParsers [ 'babel-ts' ] as Parser < Node | undefined > ;
13+ type Data = {
14+ parser : Parser < NodeType > ;
15+ templates : Template [ ] ;
16+ } ;
1817
1918/** Converts a node into a GlimmerTemplate node */
2019function convertNode (
@@ -49,7 +48,9 @@ function findCorrectCommentBlockIndex(
4948}
5049
5150/** Traverses the AST and replaces the transformed template parts with other AST */
52- function convertAst ( ast : File , templates : Template [ ] ) : void {
51+ export function convertAst ( ast : File , data : Data ) : void {
52+ const { parser, templates } = data ;
53+
5354 traverse ( ast , {
5455 enter ( path ) {
5556 if ( templates . length === 0 ) {
@@ -62,10 +63,7 @@ function convertAst(ast: File, templates: Template[]): void {
6263 case 'BlockStatement' :
6364 case 'ObjectExpression' :
6465 case 'StaticBlock' : {
65- const [ start , end ] = [
66- typescript . locStart ( node ) ,
67- typescript . locEnd ( node ) ,
68- ] ;
66+ const [ start , end ] = [ parser . locStart ( node ) , parser . locEnd ( node ) ] ;
6967
7068 const templateIndex = templates . findIndex ( ( template ) => {
7169 const { utf16Range } = template ;
@@ -119,18 +117,3 @@ function convertAst(ast: File, templates: Template[]): void {
119117 ) ;
120118 }
121119}
122-
123- export const parser : Parser < Node | undefined > = {
124- ...typescript ,
125- astFormat : PRINTER_NAME ,
126-
127- async parse ( code : string , options : Options ) : Promise < Node > {
128- const preprocessed = preprocess ( code , options . filepath ) ;
129-
130- const ast = await typescript . parse ( preprocessed . code , options ) ;
131- assert ( 'expected ast' , ast ) ;
132- convertAst ( ast as File , preprocessed . templates ) ;
133-
134- return ast ;
135- } ,
136- } ;
0 commit comments