forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionExpression.ts
48 lines (43 loc) · 1.58 KB
/
FunctionExpression.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
import type MagicString from 'magic-string';
import { BLANK } from '../../utils/blank';
import type { NodeRenderOptions, RenderOptions } from '../../utils/renderHelpers';
import ChildScope from '../scopes/ChildScope';
import type CallExpression from './CallExpression';
import type { IdentifierWithVariable } from './Identifier';
import Identifier from './Identifier';
import * as NodeType from './NodeType';
import FunctionNode from './shared/FunctionNode';
import type { GenericEsTreeNode } from './shared/Node';
export default class FunctionExpression extends FunctionNode {
declare type: NodeType.tFunctionExpression;
declare idScope: ChildScope;
createScope(parentScope: ChildScope) {
super.createScope((this.idScope = new ChildScope(parentScope, parentScope.context)));
}
parseNode(esTreeNode: GenericEsTreeNode): this {
if (esTreeNode.id !== null) {
this.id = new Identifier(this, this.idScope).parseNode(
esTreeNode.id
) as IdentifierWithVariable;
}
return super.parseNode(esTreeNode);
}
protected onlyFunctionCallUsed(): boolean {
const isIIFE =
this.parent.type === NodeType.CallExpression &&
(this.parent as CallExpression).callee === this &&
(this.id === null || this.id.variable.getOnlyFunctionCallUsed());
return isIIFE || super.onlyFunctionCallUsed();
}
render(
code: MagicString,
options: RenderOptions,
{ renderedSurroundingElement }: NodeRenderOptions = BLANK
): void {
super.render(code, options);
if (renderedSurroundingElement === NodeType.ExpressionStatement) {
code.appendRight(this.start, '(');
code.prependLeft(this.end, ')');
}
}
}