Skip to content

Commit f8f3041

Browse files
committed
feat(compiler): add ReplexicaBaseScope implementation
1 parent fd3efa6 commit f8f3041

3 files changed

Lines changed: 54 additions & 1 deletion

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import * as t from '@babel/types';
2+
import { attributeExists, getJsxElementName, getJsxParentLine, getStringAttributeValue } from "../../utils/ast";
3+
import { ReplexicaScopeHint } from "../types";
4+
import { NodePath } from '@babel/core';
5+
6+
export abstract class ReplexicaBaseScope {
7+
protected _extractBaseHints(path: NodePath<t.JSXElement | t.JSXFragment>): ReplexicaScopeHint[] {
8+
const result: ReplexicaScopeHint[] = [];
9+
10+
const elementName = getJsxElementName(path);
11+
const elementHint = path.isJSXElement()
12+
? getStringAttributeValue(path, 'data-replexica-hint')
13+
: null;
14+
15+
result.unshift({
16+
type: 'element',
17+
name: elementName,
18+
hint: elementHint,
19+
});
20+
21+
const parentLine = getJsxParentLine(path, []);
22+
for (const parent of parentLine) {
23+
if (!parent.isJSXElement()) { continue; }
24+
25+
const hasHintAttribute = attributeExists(parent, 'data-replexica-hint');
26+
if (!hasHintAttribute) { continue; }
27+
28+
const hintAttributeValue = getStringAttributeValue(parent, 'data-replexica-hint');
29+
if (!hintAttributeValue) { continue; }
30+
31+
const elementName = getJsxElementName(parent);
32+
33+
result.unshift({
34+
type: 'element',
35+
name: elementName,
36+
hint: hintAttributeValue,
37+
});
38+
}
39+
return result;
40+
}
41+
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
export * from './types';
21
export * from './chunk';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as t from '@babel/types';
2+
import { NodePath } from '@babel/core';
3+
import { ReplexicaScopeData, ReplexicaScopeHint } from '../types';
4+
5+
export interface IReplexicaScope {
6+
get id(): string;
7+
injectIntl(fileId: string, isServer: boolean, i18nImportPrefix: string): ReplexicaScopeData;
8+
extractHints(): ReplexicaScopeHint[];
9+
}
10+
11+
export type ReplexicaScopeExtractor = {
12+
fromNode(path: NodePath<t.Node>): IReplexicaScope[];
13+
}

0 commit comments

Comments
 (0)