Skip to content

Commit 442feea

Browse files
authored
Merge pull request #487 from XmiliaH/fix-485
Fix 485
2 parents 9624295 + 12790ca commit 442feea

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

lib/transformer.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ function transformer(args, body, isAsync, isGenerator, filename) {
103103
const TO_RIGHT = 100;
104104

105105
let internStateValiable = undefined;
106+
let tmpname = 'VM2_INTERNAL_TMPNAME';
106107

107108
acornWalkFull(ast, (node, state, type) => {
108109
if (type === 'Function') {
@@ -112,15 +113,30 @@ function transformer(args, body, isAsync, isGenerator, filename) {
112113
if (nodeType === 'CatchClause') {
113114
const param = node.param;
114115
if (param) {
115-
const name = assertType(param, 'Identifier').name;
116-
const cBody = assertType(node.body, 'BlockStatement');
117-
if (cBody.body.length > 0) {
116+
if (param.type === 'ObjectPattern') {
118117
insertions.push({
119118
__proto__: null,
120-
pos: cBody.body[0].start,
119+
pos: node.start,
120+
order: TO_RIGHT,
121+
code: `catch($tmpname){try{throw ${INTERNAL_STATE_NAME}.handleException($tmpname);}`
122+
});
123+
insertions.push({
124+
__proto__: null,
125+
pos: node.body.end,
121126
order: TO_LEFT,
122-
code: `${name}=${INTERNAL_STATE_NAME}.handleException(${name});`
127+
code: `}`
123128
});
129+
} else {
130+
const name = assertType(param, 'Identifier').name;
131+
const cBody = assertType(node.body, 'BlockStatement');
132+
if (cBody.body.length > 0) {
133+
insertions.push({
134+
__proto__: null,
135+
pos: cBody.body[0].start,
136+
order: TO_LEFT,
137+
code: `${name}=${INTERNAL_STATE_NAME}.handleException(${name});`
138+
});
139+
}
124140
}
125141
}
126142
} else if (nodeType === 'WithStatement') {
@@ -141,6 +157,8 @@ function transformer(args, body, isAsync, isGenerator, filename) {
141157
if (internStateValiable === undefined || internStateValiable.start > node.start) {
142158
internStateValiable = node;
143159
}
160+
} else if (node.name.startsWith(tmpname)) {
161+
tmpname = node.name + '_UNIQUE';
144162
}
145163
} else if (nodeType === 'ImportExpression') {
146164
insertions.push({
@@ -168,7 +186,7 @@ function transformer(args, body, isAsync, isGenerator, filename) {
168186
let curr = 0;
169187
for (let i = 0; i < insertions.length; i++) {
170188
const change = insertions[i];
171-
ncode += code.substring(curr, change.pos) + change.code;
189+
ncode += code.substring(curr, change.pos) + change.code.replace(/\$tmpname/g, tmpname);
172190
curr = change.pos;
173191
}
174192
ncode += code.substring(curr);

0 commit comments

Comments
 (0)