Skip to content

Object rest emit for method and accessor parameters #12272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2771,6 +2771,11 @@ namespace ts {
transformFlags |= TransformFlags.AssertTypeScript;
}

// a method declaration with object rest destructuring is ES Next syntax
if (subtreeFlags & TransformFlags.ContainsSpreadExpression) {
transformFlags |= TransformFlags.AssertESNext;
}

// An async method declaration is ES2017 syntax.
if (hasModifier(node, ModifierFlags.Async)) {
transformFlags |= TransformFlags.AssertES2017;
Expand All @@ -2797,6 +2802,11 @@ namespace ts {
transformFlags |= TransformFlags.AssertTypeScript;
}

// a method declaration with object rest destructuring is ES Next syntax
if (subtreeFlags & TransformFlags.ContainsSpreadExpression) {
transformFlags |= TransformFlags.AssertESNext;
}

node.transformFlags = transformFlags | TransformFlags.HasComputedFlags;
return transformFlags & ~TransformFlags.MethodOrAccessorExcludes;
}
Expand Down
75 changes: 59 additions & 16 deletions src/compiler/transformers/esnext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ namespace ts {
return visitFunctionExpression(node as FunctionExpression);
case SyntaxKind.ArrowFunction:
return visitArrowFunction(node as ArrowFunction);
case SyntaxKind.MethodDeclaration:
return visitMethodDeclaration(node as MethodDeclaration);
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
return visitAccessorDeclaration(node as AccessorDeclaration);
case SyntaxKind.Parameter:
return visitParameter(node as ParameterDeclaration);
default:
Expand Down Expand Up @@ -208,11 +213,6 @@ namespace ts {
}

function visitFunctionDeclaration(node: FunctionDeclaration): FunctionDeclaration {
const hasRest = forEach(node.parameters, isObjectRestParameter);
const body = hasRest ?
transformFunctionBody(node, visitor, currentSourceFile, context, noop, /*convertObjectRest*/ true) as Block :
visitEachChild(node.body, visitor, context);

return setOriginalNode(
createFunctionDeclaration(
/*decorators*/ undefined,
Expand All @@ -222,25 +222,21 @@ namespace ts {
/*typeParameters*/ undefined,
visitNodes(node.parameters, visitor, isParameter),
/*type*/ undefined,
body,
transformFunctionBodyIfNeeded(node),
/*location*/ node
),
/*original*/ node);
}

function visitArrowFunction(node: ArrowFunction) {
const hasRest = forEach(node.parameters, isObjectRestParameter);
const body = hasRest ?
transformFunctionBody(node, visitor, currentSourceFile, context, noop, /*convertObjectRest*/ true) as Block :
visitEachChild(node.body, visitor, context);
const func = setOriginalNode(
createArrowFunction(
/*modifiers*/ undefined,
/*typeParameters*/ undefined,
visitNodes(node.parameters, visitor, isParameter),
/*type*/ undefined,
node.equalsGreaterThanToken,
body,
transformFunctionBodyIfNeeded(node),
/*location*/ node
),
/*original*/ node
Expand All @@ -250,10 +246,6 @@ namespace ts {
}

function visitFunctionExpression(node: FunctionExpression): Expression {
const hasRest = forEach(node.parameters, isObjectRestParameter);
const body = hasRest ?
transformFunctionBody(node, visitor, currentSourceFile, context, noop, /*convertObjectRest*/ true) as Block :
visitEachChild(node.body, visitor, context);
return setOriginalNode(
createFunctionExpression(
/*modifiers*/ undefined,
Expand All @@ -262,11 +254,62 @@ namespace ts {
/*typeParameters*/ undefined,
visitNodes(node.parameters, visitor, isParameter),
/*type*/ undefined,
body,
transformFunctionBodyIfNeeded(node),
/*location*/ node
),
/*original*/ node
);
}

function visitMethodDeclaration(node: MethodDeclaration): MethodDeclaration {
return setOriginalNode(
createMethod(
/*decorators*/ undefined,
node.modifiers,
node.asteriskToken,
node.name,
/*typeParameters*/ undefined,
visitNodes(node.parameters, visitor, isParameter),
/*type*/ undefined,
transformFunctionBodyIfNeeded(node),
/*location*/ node
),
/*original*/ node);
}

function visitAccessorDeclaration(node: AccessorDeclaration): AccessorDeclaration {
if (node.kind === SyntaxKind.GetAccessor) {
return setOriginalNode(
createGetAccessor(
/*decorators*/ undefined,
node.modifiers,
node.name,
visitNodes(node.parameters, visitor, isParameter),
/*type*/ undefined,
transformFunctionBodyIfNeeded(node),
/*location*/ node
),
/*original*/ node);
}
else {
return setOriginalNode(
createSetAccessor(
/*decorators*/ undefined,
node.modifiers,
node.name,
visitNodes(node.parameters, visitor, isParameter),
transformFunctionBodyIfNeeded(node),
/*location*/ node
),
/*original*/ node);
}
}

function transformFunctionBodyIfNeeded(node: FunctionLikeDeclaration): Block {
const hasRest = forEach(node.parameters, isObjectRestParameter);
return hasRest ?
transformFunctionBody(node, visitor, currentSourceFile, context, noop, /*convertObjectRest*/ true) :
visitEachChild(node.body, visitor, context) as Block;
}
}
}
19 changes: 19 additions & 0 deletions tests/baselines/reference/objectRestParameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ declare function suddenly(f: (a: { x: { z, ka }, y: string }) => void);
suddenly(({ x: a, ...rest }) => rest.y);
suddenly(({ x: { z = 12, ...nested }, ...rest } = { x: { z: 1, ka: 1 }, y: 'noo' }) => rest.y + nested.ka);

class C {
m({ a, ...clone }: { a: number, b: string}): void {
// actually, never mind, don't clone
}
set p({ a, ...clone }: { a: number, b: string}) {
// actually, never mind, don't clone
}
}



//// [objectRestParameter.js]
Expand All @@ -26,3 +35,13 @@ suddenly((_a = { x: { z: 1, ka: 1 }, y: 'noo' }) => {
var _b = _a.x, { z = 12 } = _b, nested = __rest(_b, ["z"]), rest = __rest(_a, ["x"]);
return rest.y + nested.ka;
});
class C {
m(_a) {
var { a } = _a, clone = __rest(_a, ["a"]);
// actually, never mind, don't clone
}
set p(_a) {
var { a } = _a, clone = __rest(_a, ["a"]);
// actually, never mind, don't clone
}
}
23 changes: 23 additions & 0 deletions tests/baselines/reference/objectRestParameter.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,27 @@ suddenly(({ x: { z = 12, ...nested }, ...rest } = { x: { z: 1, ka: 1 }, y: 'noo'
>nested : Symbol(nested, Decl(objectRestParameter.ts, 5, 24))
>ka : Symbol(ka, Decl(objectRestParameter.ts, 3, 42))

class C {
>C : Symbol(C, Decl(objectRestParameter.ts, 5, 107))

m({ a, ...clone }: { a: number, b: string}): void {
>m : Symbol(C.m, Decl(objectRestParameter.ts, 7, 9))
>a : Symbol(a, Decl(objectRestParameter.ts, 8, 7))
>clone : Symbol(clone, Decl(objectRestParameter.ts, 8, 10))
>a : Symbol(a, Decl(objectRestParameter.ts, 8, 24))
>b : Symbol(b, Decl(objectRestParameter.ts, 8, 35))

// actually, never mind, don't clone
}
set p({ a, ...clone }: { a: number, b: string}) {
>p : Symbol(C.p, Decl(objectRestParameter.ts, 10, 5))
>a : Symbol(a, Decl(objectRestParameter.ts, 11, 11))
>clone : Symbol(clone, Decl(objectRestParameter.ts, 11, 14))
>a : Symbol(a, Decl(objectRestParameter.ts, 11, 28))
>b : Symbol(b, Decl(objectRestParameter.ts, 11, 39))

// actually, never mind, don't clone
}
}


23 changes: 23 additions & 0 deletions tests/baselines/reference/objectRestParameter.types
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,27 @@ suddenly(({ x: { z = 12, ...nested }, ...rest } = { x: { z: 1, ka: 1 }, y: 'noo'
>nested : { ka: any; }
>ka : any

class C {
>C : C

m({ a, ...clone }: { a: number, b: string}): void {
>m : ({a, ...clone}: { a: number; b: string; }) => void
>a : number
>clone : { b: string; }
>a : number
>b : string

// actually, never mind, don't clone
}
set p({ a, ...clone }: { a: number, b: string}) {
>p : { a: number; b: string; }
>a : number
>clone : { b: string; }
>a : number
>b : string

// actually, never mind, don't clone
}
}


9 changes: 9 additions & 0 deletions tests/cases/conformance/types/rest/objectRestParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@ declare function suddenly(f: (a: { x: { z, ka }, y: string }) => void);
suddenly(({ x: a, ...rest }) => rest.y);
suddenly(({ x: { z = 12, ...nested }, ...rest } = { x: { z: 1, ka: 1 }, y: 'noo' }) => rest.y + nested.ka);

class C {
m({ a, ...clone }: { a: number, b: string}): void {
// actually, never mind, don't clone
}
set p({ a, ...clone }: { a: number, b: string}) {
// actually, never mind, don't clone
}
}