Skip to content

Commit 519649e

Browse files
committed
feat(compiler): add ReplexicaChunk implementation
1 parent c138b08 commit 519649e

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import * as t from '@babel/types';
2+
import { NodePath } from '@babel/core';
3+
import { trimSafely } from '../../../utils/text';
4+
import { generateChunkId } from '../../../utils/id';
5+
6+
export class ReplexicaChunk {
7+
public static fromStringLiteral(path: NodePath<t.StringLiteral>) {
8+
const text = trimSafely(path.node.value);
9+
const id = generateChunkId(text, 0);
10+
return new ReplexicaChunk(path, text, id, false);
11+
}
12+
13+
public static fromJsxText(path: NodePath<t.JSXText>) {
14+
let text = path.node.value;
15+
text = trimSafely(text);
16+
const id = generateChunkId(text, 0);
17+
return new ReplexicaChunk(path, text, id, false);
18+
}
19+
20+
public static fromJsxExpressionContainer(path: NodePath<t.JSXExpressionContainer>) {
21+
if (!path.isJSXExpressionContainer()) { throw new Error('Expected a JSXExpressionContainer'); }
22+
23+
if (path.node.expression.type === 'StringLiteral') {
24+
const text = trimSafely(path.node.expression.value);
25+
const id = generateChunkId(text, 0);
26+
return new ReplexicaChunk(path, text, id, false);
27+
} else if (path.node.expression.type === 'Identifier') {
28+
return ReplexicaChunk.fromVariable(path.get('expression') as NodePath<t.Identifier>);
29+
} else if (path.node.expression.type === 'CallExpression') {
30+
return ReplexicaChunk.fromFunctionCall(path.get('expression') as NodePath<t.CallExpression>);
31+
} else {
32+
const text = `{code:${path.node.expression.type}}`;
33+
const id = generateChunkId(text, 0);
34+
return new ReplexicaChunk(path, text, id, true);
35+
}
36+
}
37+
38+
private static fromVariable(path: NodePath<t.Identifier>) {
39+
const text = `{variable:${path.node.name}}`;
40+
const id = generateChunkId(text, 0);
41+
return new ReplexicaChunk(path, text, id, true);
42+
}
43+
44+
private static fromFunctionCall(path: NodePath<t.CallExpression>) {
45+
if (!path.isCallExpression()) { throw new Error('Expected a CallExpression'); }
46+
47+
const functionName = path.node.callee.type === 'Identifier' ? path.node.callee.name : 'unknown';
48+
const text = `{function:${functionName}}`;
49+
const id = generateChunkId(text, 0);
50+
return new ReplexicaChunk(path, text, id, true);
51+
}
52+
53+
private constructor(
54+
public readonly path: NodePath<t.Node>,
55+
public readonly text: string,
56+
public readonly id: string,
57+
public readonly isPlaceholder: boolean,
58+
) {
59+
}
60+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './types';
2+
export * from './chunk';

0 commit comments

Comments
 (0)