-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
params.js
156 lines (130 loc) 路 4.16 KB
/
params.js
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import callDelegate from "@babel/helper-call-delegate";
import { template, types as t } from "@babel/core";
const buildDefaultParam = template(`
let VARIABLE_NAME =
arguments.length > ARGUMENT_KEY && arguments[ARGUMENT_KEY] !== undefined ?
arguments[ARGUMENT_KEY]
:
DEFAULT_VALUE;
`);
const buildLooseDefaultParam = template(`
if (ASSIGNMENT_IDENTIFIER === UNDEFINED) {
ASSIGNMENT_IDENTIFIER = DEFAULT_VALUE;
}
`);
const buildLooseDestructuredDefaultParam = template(`
let ASSIGNMENT_IDENTIFIER = PARAMETER_NAME === UNDEFINED ? DEFAULT_VALUE : PARAMETER_NAME ;
`);
const buildSafeArgumentsAccess = template(`
let $0 = arguments.length > $1 ? arguments[$1] : undefined;
`);
function isSafeBinding(scope, node) {
if (!scope.hasOwnBinding(node.name)) return true;
const { kind } = scope.getOwnBinding(node.name);
return kind === "param" || kind === "local";
}
const iifeVisitor = {
ReferencedIdentifier(path, state) {
const { scope, node } = path;
if (
node.name === "eval" ||
!isSafeBinding(scope, node) ||
!isSafeBinding(state.scope, node)
) {
state.iife = true;
path.stop();
}
},
Scope(path) {
// different bindings
path.skip();
},
};
export default function convertFunctionParams(path, loose) {
const { node, scope } = path;
const state = {
iife: false,
scope: scope,
};
const body = [];
const params = path.get("params");
let firstOptionalIndex = null;
for (let i = 0; i < params.length; i++) {
const param = params[i];
const paramIsAssignmentPattern = param.isAssignmentPattern();
if (paramIsAssignmentPattern && (loose || node.kind === "set")) {
const left = param.get("left");
const right = param.get("right");
const undefinedNode = scope.buildUndefinedNode();
if (left.isIdentifier()) {
body.push(
buildLooseDefaultParam({
ASSIGNMENT_IDENTIFIER: t.cloneNode(left.node),
DEFAULT_VALUE: right.node,
UNDEFINED: undefinedNode,
}),
);
param.replaceWith(left.node);
} else if (left.isObjectPattern() || left.isArrayPattern()) {
const paramName = scope.generateUidIdentifier();
body.push(
buildLooseDestructuredDefaultParam({
ASSIGNMENT_IDENTIFIER: left.node,
DEFAULT_VALUE: right.node,
PARAMETER_NAME: t.cloneNode(paramName),
UNDEFINED: undefinedNode,
}),
);
param.replaceWith(paramName);
}
} else if (paramIsAssignmentPattern) {
if (firstOptionalIndex === null) firstOptionalIndex = i;
const left = param.get("left");
const right = param.get("right");
if (!state.iife) {
if (right.isIdentifier() && !isSafeBinding(scope, right.node)) {
// the right hand side references a parameter
state.iife = true;
} else {
right.traverse(iifeVisitor, state);
}
}
const defNode = buildDefaultParam({
VARIABLE_NAME: left.node,
DEFAULT_VALUE: right.node,
ARGUMENT_KEY: t.numericLiteral(i),
});
body.push(defNode);
} else if (firstOptionalIndex !== null) {
const defNode = buildSafeArgumentsAccess([
param.node,
t.numericLiteral(i),
]);
body.push(defNode);
} else if (param.isObjectPattern() || param.isArrayPattern()) {
const uid = path.scope.generateUidIdentifier("ref");
const defNode = t.variableDeclaration("let", [
t.variableDeclarator(param.node, uid),
]);
body.push(defNode);
param.replaceWith(t.cloneNode(uid));
}
if (!state.iife && !param.isIdentifier()) {
param.traverse(iifeVisitor, state);
}
}
if (body.length === 0) return false;
// we need to cut off all trailing parameters
if (firstOptionalIndex !== null) {
node.params = node.params.slice(0, firstOptionalIndex);
}
// ensure it's a block, useful for arrow functions
path.ensureBlock();
if (state.iife) {
body.push(callDelegate(path, scope));
path.set("body", t.blockStatement(body));
} else {
path.get("body").unshiftContainer("body", body);
}
return true;
}