Skip to content

Commit ddee426

Browse files
committed
refactor: Separated concerns
1 parent 0ae7f92 commit ddee426

3 files changed

Lines changed: 44 additions & 46 deletions

File tree

src/parse/index.ts

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,15 @@ import type {
66
ObjectExpression,
77
StaticBlock,
88
} from '@babel/types';
9-
import { Preprocessor } from 'content-tag';
109
import type { Parser } from 'prettier';
1110
import { parsers as babelParsers } from 'prettier/plugins/babel.js';
1211

1312
import { PRINTER_NAME } from '../config.js';
1413
import type { Options } from '../options.js';
1514
import { assert } from '../utils/assert.js';
16-
import {
17-
byteToCharIndex,
18-
preprocessTemplateRange,
19-
type Template,
20-
} from './preprocess.js';
15+
import { preprocess, type Template } from './preprocess.js';
2116

2217
const typescript = babelParsers['babel-ts'] as Parser<Node | undefined>;
23-
const p = new Preprocessor();
2418

2519
/** Converts a node into a GlimmerTemplate node */
2620
function convertNode(
@@ -87,27 +81,6 @@ function convertAst(ast: File, templates: Template[]): void {
8781
}
8882
}
8983

90-
/**
91-
* Pre-processes the template info, parsing the template content to Glimmer AST,
92-
* fixing the offsets and locations of all nodes also calculates the block
93-
* params locations & ranges and adding it to the info
94-
*/
95-
export function preprocess(
96-
code: string,
97-
fileName: string,
98-
): {
99-
code: string;
100-
templates: Template[];
101-
} {
102-
const templates = codeToGlimmerAst(code, fileName);
103-
104-
for (const template of templates) {
105-
code = preprocessTemplateRange(template, code);
106-
}
107-
108-
return { templates, code };
109-
}
110-
11184
export const parser: Parser<Node | undefined> = {
11285
...typescript,
11386
astFormat: PRINTER_NAME,
@@ -120,20 +93,3 @@ export const parser: Parser<Node | undefined> = {
12093
return ast;
12194
},
12295
};
123-
124-
/** Pre-processes the template info, parsing the template content to Glimmer AST. */
125-
export function codeToGlimmerAst(code: string, filename: string): Template[] {
126-
const rawTemplates = p.parse(code, { filename });
127-
const templates: Template[] = rawTemplates.map((r) => ({
128-
type: r.type,
129-
range: r.range,
130-
contentRange: r.contentRange,
131-
contents: r.contents,
132-
utf16Range: {
133-
start: byteToCharIndex(code, r.range.start),
134-
end: byteToCharIndex(code, r.range.end),
135-
},
136-
}));
137-
138-
return templates;
139-
}

src/parse/preprocess.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Preprocessor } from 'content-tag';
2+
13
export interface Template {
24
contents: string;
35
type: string;
@@ -87,3 +89,43 @@ export function preprocessTemplateRange(
8789
const total = prefix + content + ' '.repeat(spaces) + suffix;
8890
return replaceRange(code, template.range.start, template.range.end, total);
8991
}
92+
93+
const p = new Preprocessor();
94+
95+
/** Pre-processes the template info, parsing the template content to Glimmer AST. */
96+
export function codeToGlimmerAst(code: string, filename: string): Template[] {
97+
const rawTemplates = p.parse(code, { filename });
98+
const templates: Template[] = rawTemplates.map((r) => ({
99+
type: r.type,
100+
range: r.range,
101+
contentRange: r.contentRange,
102+
contents: r.contents,
103+
utf16Range: {
104+
start: byteToCharIndex(code, r.range.start),
105+
end: byteToCharIndex(code, r.range.end),
106+
},
107+
}));
108+
109+
return templates;
110+
}
111+
112+
/**
113+
* Pre-processes the template info, parsing the template content to Glimmer AST,
114+
* fixing the offsets and locations of all nodes also calculates the block
115+
* params locations & ranges and adding it to the info
116+
*/
117+
export function preprocess(
118+
code: string,
119+
fileName: string,
120+
): {
121+
code: string;
122+
templates: Template[];
123+
} {
124+
const templates = codeToGlimmerAst(code, fileName);
125+
126+
for (const template of templates) {
127+
code = preprocessTemplateRange(template, code);
128+
}
129+
130+
return { templates, code };
131+
}

tests/unit-tests/preprocess.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, test } from 'vitest';
22

3-
import { codeToGlimmerAst } from '../../src/parse/index.js';
43
import {
4+
codeToGlimmerAst,
55
PLACEHOLDER,
66
preprocessTemplateRange,
77
} from '../../src/parse/preprocess.js';

0 commit comments

Comments
 (0)