-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
New with spread #3066
New with spread #3066
Changes from 7 commits
e4f416e
fdfbcf6
d130816
a97ea4b
1be34f1
b88d542
fb63aea
e9f8a6f
629b4bb
78a36c0
ccd3de3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1886,11 +1886,44 @@ var __param = (this && this.__param) || function (paramIndex, decorator) { | |
|
||
function emitNewExpression(node: NewExpression) { | ||
write("new "); | ||
emit(node.expression); | ||
if (node.arguments) { | ||
|
||
// Spread operator logic can be supported in new expressions in ES5 using a combination | ||
// of Function.prototype.bind() and Function.prototype.apply(). | ||
// | ||
// Example: | ||
// | ||
// var arguments = [1, 2, 3, 4, 5]; | ||
// new Array(...arguments); | ||
// | ||
// Could be transpiled into ES5: | ||
// | ||
// var arguments = [1, 2, 3, 4, 5]; | ||
// new (Array.bind.apply(Array, [void 0].concat(arguments))); | ||
// | ||
// `[void 0]` is the first argument which represents `thisArg` to the bind method above. | ||
// And `thisArg` will be set to the return value of the constructor when instantiated | ||
// with the new operator — regardless of any value we set `thisArg` to. Thus, we set it | ||
// to an undefined, `void 0`. | ||
if (languageVersion === ScriptTarget.ES5 && | ||
node.arguments && | ||
hasSpreadElement(node.arguments)) { | ||
|
||
write("("); | ||
emitCommaList(node.arguments); | ||
write(")"); | ||
let target = emitCallTarget(node.expression); | ||
write(".bind.apply("); | ||
emit(target); | ||
write(", [void 0].concat("); | ||
emitListWithSpread(node.arguments, /*alwaysCopy*/true, /*multiline*/false, /*trailingComma*/false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you actually rename There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I can rename it. |
||
write(")))"); | ||
write("()"); | ||
} | ||
else { | ||
emit(node.expression); | ||
if (node.arguments) { | ||
write("("); | ||
emitCommaList(node.arguments); | ||
write(")"); | ||
} | ||
} | ||
} | ||
|
||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
=== tests/cases/conformance/expressions/functionCalls/callWithSpread.ts === | ||
interface X { | ||
>X : Symbol(X, Decl(callWithSpread.ts, 0, 0)) | ||
|
||
foo(x: number, y: number, ...z: string[]); | ||
>foo : Symbol(foo, Decl(callWithSpread.ts, 0, 13)) | ||
>x : Symbol(x, Decl(callWithSpread.ts, 1, 8)) | ||
>y : Symbol(y, Decl(callWithSpread.ts, 1, 18)) | ||
>z : Symbol(z, Decl(callWithSpread.ts, 1, 29)) | ||
} | ||
|
||
function foo(x: number, y: number, ...z: string[]) { | ||
>foo : Symbol(foo, Decl(callWithSpread.ts, 2, 1)) | ||
>x : Symbol(x, Decl(callWithSpread.ts, 4, 13)) | ||
>y : Symbol(y, Decl(callWithSpread.ts, 4, 23)) | ||
>z : Symbol(z, Decl(callWithSpread.ts, 4, 34)) | ||
} | ||
|
||
var a: string[]; | ||
>a : Symbol(a, Decl(callWithSpread.ts, 7, 3)) | ||
|
||
var z: number[]; | ||
>z : Symbol(z, Decl(callWithSpread.ts, 8, 3)) | ||
|
||
var obj: X; | ||
>obj : Symbol(obj, Decl(callWithSpread.ts, 9, 3)) | ||
>X : Symbol(X, Decl(callWithSpread.ts, 0, 0)) | ||
|
||
var xa: X[]; | ||
>xa : Symbol(xa, Decl(callWithSpread.ts, 10, 3)) | ||
>X : Symbol(X, Decl(callWithSpread.ts, 0, 0)) | ||
|
||
foo(1, 2, "abc"); | ||
>foo : Symbol(foo, Decl(callWithSpread.ts, 2, 1)) | ||
|
||
foo(1, 2, ...a); | ||
>foo : Symbol(foo, Decl(callWithSpread.ts, 2, 1)) | ||
>a : Symbol(a, Decl(callWithSpread.ts, 7, 3)) | ||
|
||
foo(1, 2, ...a, "abc"); | ||
>foo : Symbol(foo, Decl(callWithSpread.ts, 2, 1)) | ||
>a : Symbol(a, Decl(callWithSpread.ts, 7, 3)) | ||
|
||
obj.foo(1, 2, "abc"); | ||
>obj.foo : Symbol(X.foo, Decl(callWithSpread.ts, 0, 13)) | ||
>obj : Symbol(obj, Decl(callWithSpread.ts, 9, 3)) | ||
>foo : Symbol(X.foo, Decl(callWithSpread.ts, 0, 13)) | ||
|
||
obj.foo(1, 2, ...a); | ||
>obj.foo : Symbol(X.foo, Decl(callWithSpread.ts, 0, 13)) | ||
>obj : Symbol(obj, Decl(callWithSpread.ts, 9, 3)) | ||
>foo : Symbol(X.foo, Decl(callWithSpread.ts, 0, 13)) | ||
>a : Symbol(a, Decl(callWithSpread.ts, 7, 3)) | ||
|
||
obj.foo(1, 2, ...a, "abc"); | ||
>obj.foo : Symbol(X.foo, Decl(callWithSpread.ts, 0, 13)) | ||
>obj : Symbol(obj, Decl(callWithSpread.ts, 9, 3)) | ||
>foo : Symbol(X.foo, Decl(callWithSpread.ts, 0, 13)) | ||
>a : Symbol(a, Decl(callWithSpread.ts, 7, 3)) | ||
|
||
(obj.foo)(1, 2, "abc"); | ||
>obj.foo : Symbol(X.foo, Decl(callWithSpread.ts, 0, 13)) | ||
>obj : Symbol(obj, Decl(callWithSpread.ts, 9, 3)) | ||
>foo : Symbol(X.foo, Decl(callWithSpread.ts, 0, 13)) | ||
|
||
(obj.foo)(1, 2, ...a); | ||
>obj.foo : Symbol(X.foo, Decl(callWithSpread.ts, 0, 13)) | ||
>obj : Symbol(obj, Decl(callWithSpread.ts, 9, 3)) | ||
>foo : Symbol(X.foo, Decl(callWithSpread.ts, 0, 13)) | ||
>a : Symbol(a, Decl(callWithSpread.ts, 7, 3)) | ||
|
||
(obj.foo)(1, 2, ...a, "abc"); | ||
>obj.foo : Symbol(X.foo, Decl(callWithSpread.ts, 0, 13)) | ||
>obj : Symbol(obj, Decl(callWithSpread.ts, 9, 3)) | ||
>foo : Symbol(X.foo, Decl(callWithSpread.ts, 0, 13)) | ||
>a : Symbol(a, Decl(callWithSpread.ts, 7, 3)) | ||
|
||
xa[1].foo(1, 2, "abc"); | ||
>xa[1].foo : Symbol(X.foo, Decl(callWithSpread.ts, 0, 13)) | ||
>xa : Symbol(xa, Decl(callWithSpread.ts, 10, 3)) | ||
>foo : Symbol(X.foo, Decl(callWithSpread.ts, 0, 13)) | ||
|
||
xa[1].foo(1, 2, ...a); | ||
>xa[1].foo : Symbol(X.foo, Decl(callWithSpread.ts, 0, 13)) | ||
>xa : Symbol(xa, Decl(callWithSpread.ts, 10, 3)) | ||
>foo : Symbol(X.foo, Decl(callWithSpread.ts, 0, 13)) | ||
>a : Symbol(a, Decl(callWithSpread.ts, 7, 3)) | ||
|
||
xa[1].foo(1, 2, ...a, "abc"); | ||
>xa[1].foo : Symbol(X.foo, Decl(callWithSpread.ts, 0, 13)) | ||
>xa : Symbol(xa, Decl(callWithSpread.ts, 10, 3)) | ||
>foo : Symbol(X.foo, Decl(callWithSpread.ts, 0, 13)) | ||
>a : Symbol(a, Decl(callWithSpread.ts, 7, 3)) | ||
|
||
(<Function>xa[1].foo)(...[1, 2, "abc"]); | ||
>Function : Symbol(Function, Decl(lib.d.ts, 223, 38), Decl(lib.d.ts, 269, 11)) | ||
>xa[1].foo : Symbol(X.foo, Decl(callWithSpread.ts, 0, 13)) | ||
>xa : Symbol(xa, Decl(callWithSpread.ts, 10, 3)) | ||
>foo : Symbol(X.foo, Decl(callWithSpread.ts, 0, 13)) | ||
|
||
class C { | ||
>C : Symbol(C, Decl(callWithSpread.ts, 28, 40)) | ||
|
||
constructor(x: number, y: number, ...z: string[]) { | ||
>x : Symbol(x, Decl(callWithSpread.ts, 31, 16)) | ||
>y : Symbol(y, Decl(callWithSpread.ts, 31, 26)) | ||
>z : Symbol(z, Decl(callWithSpread.ts, 31, 37)) | ||
|
||
this.foo(x, y); | ||
>this.foo : Symbol(foo, Decl(callWithSpread.ts, 34, 5)) | ||
>this : Symbol(C, Decl(callWithSpread.ts, 28, 40)) | ||
>foo : Symbol(foo, Decl(callWithSpread.ts, 34, 5)) | ||
>x : Symbol(x, Decl(callWithSpread.ts, 31, 16)) | ||
>y : Symbol(y, Decl(callWithSpread.ts, 31, 26)) | ||
|
||
this.foo(x, y, ...z); | ||
>this.foo : Symbol(foo, Decl(callWithSpread.ts, 34, 5)) | ||
>this : Symbol(C, Decl(callWithSpread.ts, 28, 40)) | ||
>foo : Symbol(foo, Decl(callWithSpread.ts, 34, 5)) | ||
>x : Symbol(x, Decl(callWithSpread.ts, 31, 16)) | ||
>y : Symbol(y, Decl(callWithSpread.ts, 31, 26)) | ||
>z : Symbol(z, Decl(callWithSpread.ts, 31, 37)) | ||
} | ||
foo(x: number, y: number, ...z: string[]) { | ||
>foo : Symbol(foo, Decl(callWithSpread.ts, 34, 5)) | ||
>x : Symbol(x, Decl(callWithSpread.ts, 35, 8)) | ||
>y : Symbol(y, Decl(callWithSpread.ts, 35, 18)) | ||
>z : Symbol(z, Decl(callWithSpread.ts, 35, 29)) | ||
} | ||
} | ||
|
||
class D extends C { | ||
>D : Symbol(D, Decl(callWithSpread.ts, 37, 1)) | ||
>C : Symbol(C, Decl(callWithSpread.ts, 28, 40)) | ||
|
||
constructor() { | ||
super(1, 2); | ||
>super : Symbol(C, Decl(callWithSpread.ts, 28, 40)) | ||
|
||
super(1, 2, ...a); | ||
>super : Symbol(C, Decl(callWithSpread.ts, 28, 40)) | ||
>a : Symbol(a, Decl(callWithSpread.ts, 7, 3)) | ||
} | ||
foo() { | ||
>foo : Symbol(foo, Decl(callWithSpread.ts, 43, 5)) | ||
|
||
super.foo(1, 2); | ||
>super.foo : Symbol(C.foo, Decl(callWithSpread.ts, 34, 5)) | ||
>super : Symbol(C, Decl(callWithSpread.ts, 28, 40)) | ||
>foo : Symbol(C.foo, Decl(callWithSpread.ts, 34, 5)) | ||
|
||
super.foo(1, 2, ...a); | ||
>super.foo : Symbol(C.foo, Decl(callWithSpread.ts, 34, 5)) | ||
>super : Symbol(C, Decl(callWithSpread.ts, 28, 40)) | ||
>foo : Symbol(C.foo, Decl(callWithSpread.ts, 34, 5)) | ||
>a : Symbol(a, Decl(callWithSpread.ts, 7, 3)) | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why was this added to the .gitignore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got multiple files outputted there every time I run
jake runtests
:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CyrusNajmabadi i think this is a fair one to add, ppl have made this mistake multiple times already. mainly because of a mocha issue on node 0.10.*
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah I don't think there's any real harm in having it