-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathJSXFragment.ts
91 lines (81 loc) · 2.72 KB
/
JSXFragment.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import type MagicString from 'magic-string';
import type { RenderOptions } from '../../utils/renderHelpers';
import type { InclusionContext } from '../ExecutionContext';
import type JSXClosingFragment from './JSXClosingFragment';
import type JSXOpeningFragment from './JSXOpeningFragment';
import type * as NodeType from './NodeType';
import JSXElementBase from './shared/JSXElementBase';
import type { JSXChild } from './shared/jsxHelpers';
import type { IncludeChildren } from './shared/Node';
export default class JSXFragment extends JSXElementBase {
type!: NodeType.tJSXElement;
openingFragment!: JSXOpeningFragment;
children!: JSXChild[];
closingFragment!: JSXClosingFragment;
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren) {
super.include(context, includeChildrenRecursively);
this.openingFragment.include(context, includeChildrenRecursively);
this.closingFragment.include(context, includeChildrenRecursively);
}
render(code: MagicString, options: RenderOptions): void {
switch (this.jsxMode.mode) {
case 'classic': {
this.renderClassicMode(code, options);
break;
}
case 'automatic': {
this.renderAutomaticMode(code, options);
break;
}
default: {
super.render(code, options);
}
}
}
private renderClassicMode(code: MagicString, options: RenderOptions) {
const {
snippets: { getPropertyAccess },
useOriginalName
} = options;
const { closingFragment, factory, factoryVariable, openingFragment, start } = this;
const [, ...nestedName] = factory!.split('.');
openingFragment.render(code, options);
code.prependRight(
start,
`/*#__PURE__*/${[
factoryVariable!.getName(getPropertyAccess, useOriginalName),
...nestedName
].join('.')}(`
);
code.appendLeft(openingFragment.end, ', null');
this.renderChildren(code, options, openingFragment.end);
closingFragment.render(code, options);
}
private renderAutomaticMode(code: MagicString, options: RenderOptions) {
const {
snippets: { getPropertyAccess },
useOriginalName
} = options;
const { closingFragment, factoryVariable, openingFragment, start } = this;
openingFragment.render(code, options);
code.prependRight(
start,
`/*#__PURE__*/${factoryVariable!.getName(getPropertyAccess, useOriginalName)}(`
);
const { firstChild, hasMultipleChildren, childrenEnd } = this.renderChildren(
code,
options,
openingFragment.end
);
if (firstChild) {
code.prependRight(firstChild.start, `{ children: ${hasMultipleChildren ? '[' : ''}`);
if (hasMultipleChildren) {
code.appendLeft(closingFragment.start, ']');
}
code.appendLeft(childrenEnd, ' }');
} else {
code.appendLeft(openingFragment.end, ', {}');
}
closingFragment.render(code, options);
}
}