Skip to content

Commit b4d3bca

Browse files
committed
Object rest emit for method and accessor parameters
I missed these before, so emit was incorrect for object rest in a method or accessor parameter.
1 parent f437c8f commit b4d3bca

File tree

2 files changed

+69
-16
lines changed

2 files changed

+69
-16
lines changed

src/compiler/binder.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2771,6 +2771,11 @@ namespace ts {
27712771
transformFlags |= TransformFlags.AssertTypeScript;
27722772
}
27732773

2774+
// a method declaration with object rest destructuring is ES Next syntax
2775+
if (subtreeFlags & TransformFlags.ContainsSpreadExpression) {
2776+
transformFlags |= TransformFlags.AssertESNext;
2777+
}
2778+
27742779
// An async method declaration is ES2017 syntax.
27752780
if (hasModifier(node, ModifierFlags.Async)) {
27762781
transformFlags |= TransformFlags.AssertES2017;
@@ -2797,6 +2802,11 @@ namespace ts {
27972802
transformFlags |= TransformFlags.AssertTypeScript;
27982803
}
27992804

2805+
// a method declaration with object rest destructuring is ES Next syntax
2806+
if (subtreeFlags & TransformFlags.ContainsSpreadExpression) {
2807+
transformFlags |= TransformFlags.AssertESNext;
2808+
}
2809+
28002810
node.transformFlags = transformFlags | TransformFlags.HasComputedFlags;
28012811
return transformFlags & ~TransformFlags.MethodOrAccessorExcludes;
28022812
}

src/compiler/transformers/esnext.ts

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ namespace ts {
4646
return visitFunctionExpression(node as FunctionExpression);
4747
case SyntaxKind.ArrowFunction:
4848
return visitArrowFunction(node as ArrowFunction);
49+
case SyntaxKind.MethodDeclaration:
50+
return visitMethodDeclaration(node as MethodDeclaration);
51+
case SyntaxKind.GetAccessor:
52+
case SyntaxKind.SetAccessor:
53+
return visitAccessorDeclaration(node as AccessorDeclaration);
4954
case SyntaxKind.Parameter:
5055
return visitParameter(node as ParameterDeclaration);
5156
default:
@@ -208,11 +213,6 @@ namespace ts {
208213
}
209214

210215
function visitFunctionDeclaration(node: FunctionDeclaration): FunctionDeclaration {
211-
const hasRest = forEach(node.parameters, isObjectRestParameter);
212-
const body = hasRest ?
213-
transformFunctionBody(node, visitor, currentSourceFile, context, noop, /*convertObjectRest*/ true) as Block :
214-
visitEachChild(node.body, visitor, context);
215-
216216
return setOriginalNode(
217217
createFunctionDeclaration(
218218
/*decorators*/ undefined,
@@ -222,25 +222,21 @@ namespace ts {
222222
/*typeParameters*/ undefined,
223223
visitNodes(node.parameters, visitor, isParameter),
224224
/*type*/ undefined,
225-
body,
225+
transformFunctionBodyIfNeeded(node),
226226
/*location*/ node
227227
),
228228
/*original*/ node);
229229
}
230230

231231
function visitArrowFunction(node: ArrowFunction) {
232-
const hasRest = forEach(node.parameters, isObjectRestParameter);
233-
const body = hasRest ?
234-
transformFunctionBody(node, visitor, currentSourceFile, context, noop, /*convertObjectRest*/ true) as Block :
235-
visitEachChild(node.body, visitor, context);
236232
const func = setOriginalNode(
237233
createArrowFunction(
238234
/*modifiers*/ undefined,
239235
/*typeParameters*/ undefined,
240236
visitNodes(node.parameters, visitor, isParameter),
241237
/*type*/ undefined,
242238
node.equalsGreaterThanToken,
243-
body,
239+
transformFunctionBodyIfNeeded(node),
244240
/*location*/ node
245241
),
246242
/*original*/ node
@@ -250,10 +246,6 @@ namespace ts {
250246
}
251247

252248
function visitFunctionExpression(node: FunctionExpression): Expression {
253-
const hasRest = forEach(node.parameters, isObjectRestParameter);
254-
const body = hasRest ?
255-
transformFunctionBody(node, visitor, currentSourceFile, context, noop, /*convertObjectRest*/ true) as Block :
256-
visitEachChild(node.body, visitor, context);
257249
return setOriginalNode(
258250
createFunctionExpression(
259251
/*modifiers*/ undefined,
@@ -262,11 +254,62 @@ namespace ts {
262254
/*typeParameters*/ undefined,
263255
visitNodes(node.parameters, visitor, isParameter),
264256
/*type*/ undefined,
265-
body,
257+
transformFunctionBodyIfNeeded(node),
266258
/*location*/ node
267259
),
268260
/*original*/ node
269261
);
270262
}
263+
264+
function visitMethodDeclaration(node: MethodDeclaration): MethodDeclaration {
265+
return setOriginalNode(
266+
createMethod(
267+
/*decorators*/ undefined,
268+
node.modifiers,
269+
node.asteriskToken,
270+
node.name,
271+
/*typeParameters*/ undefined,
272+
visitNodes(node.parameters, visitor, isParameter),
273+
/*type*/ undefined,
274+
transformFunctionBodyIfNeeded(node),
275+
/*location*/ node
276+
),
277+
/*original*/ node);
278+
}
279+
280+
function visitAccessorDeclaration(node: AccessorDeclaration): AccessorDeclaration {
281+
if (node.kind === SyntaxKind.GetAccessor) {
282+
return setOriginalNode(
283+
createGetAccessor(
284+
/*decorators*/ undefined,
285+
node.modifiers,
286+
node.name,
287+
visitNodes(node.parameters, visitor, isParameter),
288+
/*type*/ undefined,
289+
transformFunctionBodyIfNeeded(node),
290+
/*location*/ node
291+
),
292+
/*original*/ node);
293+
}
294+
else {
295+
return setOriginalNode(
296+
createSetAccessor(
297+
/*decorators*/ undefined,
298+
node.modifiers,
299+
node.name,
300+
visitNodes(node.parameters, visitor, isParameter),
301+
transformFunctionBodyIfNeeded(node),
302+
/*location*/ node
303+
),
304+
/*original*/ node);
305+
}
306+
}
307+
308+
function transformFunctionBodyIfNeeded(node: FunctionLikeDeclaration): Block {
309+
const hasRest = forEach(node.parameters, isObjectRestParameter);
310+
return hasRest ?
311+
transformFunctionBody(node, visitor, currentSourceFile, context, noop, /*convertObjectRest*/ true) :
312+
visitEachChild(node.body, visitor, context) as Block;
313+
}
271314
}
272315
}

0 commit comments

Comments
 (0)