|
1 | | -import traverse from '@babel/traverse'; |
2 | | -import type { |
3 | | - BlockStatement, |
4 | | - File, |
5 | | - Node, |
6 | | - ObjectExpression, |
7 | | - StaticBlock, |
8 | | -} from '@babel/types'; |
9 | | -import type { Parser } from 'prettier'; |
10 | | -import { parsers as babelParsers } from 'prettier/plugins/babel.js'; |
11 | | - |
12 | | -import type { PluginOptions } from '../options.js'; |
13 | | -import { assert } from '../utils/assert.js'; |
14 | | -import { PRINTER_NAME } from '../utils/index.js'; |
15 | | -import { preprocess, type Template } from './preprocess.js'; |
16 | | - |
17 | | -const typescript = babelParsers['babel-ts'] as Parser<Node | undefined>; |
18 | | - |
19 | | -/** Converts a node into a GlimmerTemplate node */ |
20 | | -function convertNode( |
21 | | - node: BlockStatement | ObjectExpression | StaticBlock, |
22 | | - rawTemplate: Template, |
23 | | -): void { |
24 | | - node.innerComments = []; |
25 | | - node.extra = Object.assign(node.extra ?? {}, { |
26 | | - isGlimmerTemplate: true as const, |
27 | | - template: rawTemplate, |
28 | | - }); |
29 | | -} |
30 | | - |
31 | | -function findCorrectCommentBlockIndex( |
32 | | - comments: File['comments'], |
33 | | - start: number, |
34 | | - end: number, |
35 | | -): number { |
36 | | - if (!comments) { |
37 | | - return -1; |
38 | | - } |
39 | | - |
40 | | - return comments.findIndex((comment) => { |
41 | | - const { start: commentStart, end: commentEnd } = comment; |
42 | | - |
43 | | - return ( |
44 | | - (commentStart === start && commentEnd === end) || |
45 | | - (commentStart === start + 1 && commentEnd === end - 1) || |
46 | | - (commentStart === start + 7 && commentEnd === end - 1) |
47 | | - ); |
48 | | - }); |
49 | | -} |
50 | | - |
51 | | -/** Traverses the AST and replaces the transformed template parts with other AST */ |
52 | | -function convertAst(ast: File, templates: Template[]): void { |
53 | | - traverse(ast, { |
54 | | - enter(path) { |
55 | | - if (templates.length === 0) { |
56 | | - return null; |
57 | | - } |
58 | | - |
59 | | - const { node } = path; |
60 | | - |
61 | | - switch (node.type) { |
62 | | - case 'BlockStatement': |
63 | | - case 'ObjectExpression': |
64 | | - case 'StaticBlock': { |
65 | | - const [start, end] = [ |
66 | | - typescript.locStart(node), |
67 | | - typescript.locEnd(node), |
68 | | - ]; |
69 | | - |
70 | | - const templateIndex = templates.findIndex((template) => { |
71 | | - const { utf16Range } = template; |
72 | | - |
73 | | - if (utf16Range.start === start && utf16Range.end === end) { |
74 | | - return true; |
75 | | - } |
76 | | - |
77 | | - return ( |
78 | | - node.type === 'ObjectExpression' && |
79 | | - utf16Range.start === start - 1 && |
80 | | - utf16Range.end === end + 1 |
81 | | - ); |
82 | | - }); |
83 | | - |
84 | | - if (templateIndex === -1) { |
85 | | - return null; |
86 | | - } |
87 | | - |
88 | | - const rawTemplate = templates.splice(templateIndex, 1)[0]; |
89 | | - |
90 | | - if (!rawTemplate) { |
91 | | - throw new Error( |
92 | | - 'expected raw template because splice index came from findIndex', |
93 | | - ); |
94 | | - } |
95 | | - |
96 | | - if (ast.comments && ast.comments.length > 0) { |
97 | | - const commentBlockIndex = findCorrectCommentBlockIndex( |
98 | | - ast.comments, |
99 | | - start, |
100 | | - end, |
101 | | - ); |
102 | | - |
103 | | - if (commentBlockIndex !== undefined && commentBlockIndex >= 0) { |
104 | | - ast.comments.splice(commentBlockIndex, 1); |
105 | | - } |
106 | | - } |
107 | | - |
108 | | - convertNode(node, rawTemplate); |
109 | | - } |
110 | | - } |
111 | | - |
112 | | - return null; |
113 | | - }, |
114 | | - }); |
115 | | - |
116 | | - if (templates.length > 0) { |
117 | | - throw new Error( |
118 | | - `failed to process all templates, ${templates.length} remaining`, |
119 | | - ); |
120 | | - } |
121 | | -} |
122 | | - |
123 | | -export const parser: Parser<Node | undefined> = { |
124 | | - ...typescript, |
125 | | - astFormat: PRINTER_NAME, |
126 | | - |
127 | | - async parse(code: string, options: PluginOptions): 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 | | -}; |
| 1 | +export * from './convert-ast.js'; |
| 2 | +export * from './preprocess.js'; |
0 commit comments