/
ClassDeclaration.ts
67 lines (62 loc) · 1.96 KB
/
ClassDeclaration.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
import type MagicString from 'magic-string';
import type { RenderOptions } from '../../utils/renderHelpers';
import { getSystemExportStatement } from '../../utils/systemJsRendering';
import type ChildScope from '../scopes/ChildScope';
import Identifier, { type IdentifierWithVariable } from './Identifier';
import type * as NodeType from './NodeType';
import ClassNode from './shared/ClassNode';
import type { GenericEsTreeNode } from './shared/Node';
export default class ClassDeclaration extends ClassNode {
declare id: IdentifierWithVariable | null;
declare type: NodeType.tClassDeclaration;
initialise(): void {
super.initialise();
if (this.id !== null) {
this.id.variable.isId = true;
}
}
parseNode(esTreeNode: GenericEsTreeNode): void {
if (esTreeNode.id !== null) {
this.id = new Identifier(
esTreeNode.id,
this,
this.scope.parent as ChildScope
) as IdentifierWithVariable;
}
super.parseNode(esTreeNode);
}
render(code: MagicString, options: RenderOptions): void {
const {
exportNamesByVariable,
format,
snippets: { _, getPropertyAccess }
} = options;
if (this.id) {
const { variable, name } = this.id;
if (format === 'system' && exportNamesByVariable.has(variable)) {
code.appendLeft(this.end, `${_}${getSystemExportStatement([variable], options)};`);
}
const renderedVariable = variable.getName(getPropertyAccess);
if (renderedVariable !== name) {
this.superClass?.render(code, options);
this.body.render(code, options);
code.prependRight(this.start, `let ${renderedVariable}${_}=${_}`);
code.prependLeft(this.end, ';');
return;
}
}
super.render(code, options);
}
protected applyDeoptimizations(): void {
super.applyDeoptimizations();
const { id, scope } = this;
if (id) {
const { name, variable } = id;
for (const accessedVariable of scope.accessedOutsideVariables.values()) {
if (accessedVariable !== variable) {
accessedVariable.forbidName(name);
}
}
}
}
}