mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Address code review; preserve users non-parenthesis
This commit is contained in:
+20
-2
@@ -3289,6 +3289,21 @@ module ts {
|
||||
decreaseIndent();
|
||||
}
|
||||
|
||||
function emitSignatureParametersES6(node: FunctionLikeDeclaration) {
|
||||
// Check the node's parameters whether it contains flags indicating that it has no parenthesis around the parameters
|
||||
// Preserver no-parenthesis
|
||||
if (node && node.flags & NodeFlags.SimpleArrowFunction) {
|
||||
increaseIndent();
|
||||
var parameters = node.parameters;
|
||||
var omitCount = languageVersion < ScriptTarget.ES6 && hasRestParameters(node) ? 1 : 0;
|
||||
emitList(parameters, 0, parameters.length - omitCount, /*multiLine*/ false, /*trailingComma*/ false);
|
||||
decreaseIndent();
|
||||
}
|
||||
else {
|
||||
emitSignatureParameters(node);
|
||||
}
|
||||
}
|
||||
|
||||
function emitSignatureAndBody(node: FunctionLikeDeclaration) {
|
||||
var saveTempCount = tempCount;
|
||||
var saveTempVariables = tempVariables;
|
||||
@@ -3296,12 +3311,15 @@ module ts {
|
||||
tempCount = 0;
|
||||
tempVariables = undefined;
|
||||
tempParameters = undefined;
|
||||
emitSignatureParameters(node);
|
||||
|
||||
// When targeting ES6, emit arrow function natively in ES6
|
||||
if (isES6ArrowFunction(node)) {
|
||||
write(" =>");
|
||||
emitSignatureParametersES6(node);
|
||||
write(" =>");
|
||||
}
|
||||
else {
|
||||
emitSignatureParameters(node);
|
||||
}
|
||||
|
||||
write(" {");
|
||||
scopeEmitStart(node);
|
||||
|
||||
@@ -2883,6 +2883,9 @@ module ts {
|
||||
node.parameters = <NodeArray<ParameterDeclaration>>[parameter];
|
||||
node.parameters.pos = parameter.pos;
|
||||
node.parameters.end = parameter.end;
|
||||
// Add node flags for simple arrow function(no parenthesis around parameters)
|
||||
// so that in emit state we can check this flag and preserve users original text
|
||||
node.flags |= NodeFlags.SimpleArrowFunction;
|
||||
|
||||
parseExpected(SyntaxKind.EqualsGreaterThanToken);
|
||||
node.body = parseArrowFunctionExpressionBody();
|
||||
|
||||
@@ -293,6 +293,8 @@ module ts {
|
||||
Const = 0x00001000, // Variable declaration
|
||||
OctalLiteral = 0x00002000,
|
||||
|
||||
SimpleArrowFunction = 0x00004000, // Arrow function without parenthesized parameters
|
||||
|
||||
Modifier = Export | Ambient | Public | Private | Protected | Static,
|
||||
AccessibilityModifier = Public | Private | Protected,
|
||||
BlockScoped = Let | Const
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
//// [emitArrowFunctionsAsIs.ts]
|
||||
var arrow1 = a => { };
|
||||
var arrow2 = (a) => { };
|
||||
|
||||
var arrow3 = (a, b) => { };
|
||||
|
||||
//// [emitArrowFunctionsAsIs.js]
|
||||
var arrow1 = function (a) {
|
||||
};
|
||||
var arrow2 = function (a) {
|
||||
};
|
||||
var arrow3 = function (a, b) {
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
=== tests/cases/conformance/es6/arrowFunction/emitArrowFunctionsAsIs.ts ===
|
||||
var arrow1 = a => { };
|
||||
>arrow1 : (a: any) => void
|
||||
>a => { } : (a: any) => void
|
||||
>a : any
|
||||
|
||||
var arrow2 = (a) => { };
|
||||
>arrow2 : (a: any) => void
|
||||
>(a) => { } : (a: any) => void
|
||||
>a : any
|
||||
|
||||
var arrow3 = (a, b) => { };
|
||||
>arrow3 : (a: any, b: any) => void
|
||||
>(a, b) => { } : (a: any, b: any) => void
|
||||
>a : any
|
||||
>b : any
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
//// [emitArrowFunctionsAsIsES6.ts]
|
||||
var arrow1 = a => { };
|
||||
var arrow2 = (a) => { };
|
||||
|
||||
var arrow3 = (a, b) => { };
|
||||
|
||||
//// [emitArrowFunctionsAsIsES6.js]
|
||||
var arrow1 = a => {
|
||||
};
|
||||
var arrow2 = (a) => {
|
||||
};
|
||||
var arrow3 = (a, b) => {
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
=== tests/cases/conformance/es6/arrowFunction/emitArrowFunctionsAsIsES6.ts ===
|
||||
var arrow1 = a => { };
|
||||
>arrow1 : (a: any) => void
|
||||
>a => { } : (a: any) => void
|
||||
>a : any
|
||||
|
||||
var arrow2 = (a) => { };
|
||||
>arrow2 : (a: any) => void
|
||||
>(a) => { } : (a: any) => void
|
||||
>a : any
|
||||
|
||||
var arrow3 = (a, b) => { };
|
||||
>arrow3 : (a: any, b: any) => void
|
||||
>(a, b) => { } : (a: any, b: any) => void
|
||||
>a : any
|
||||
>b : any
|
||||
|
||||
@@ -25,11 +25,11 @@ var h = tempFun `${ (x => x) } ${ (((x => x))) } ${ undefined }`
|
||||
function tempFun(tempStrs, g, x) {
|
||||
return g(x);
|
||||
}
|
||||
var a = tempFun `${(x) => { return x; }} ${10}`;
|
||||
var b = tempFun `${((x) => { return x; })} ${10}`;
|
||||
var c = tempFun `${(((x) => { return x; }))} ${10}`;
|
||||
var d = tempFun `${(x) => { return x; }} ${(x) => { return x; }} ${10}`;
|
||||
var e = tempFun `${(x) => { return x; }} ${((x) => { return x; })} ${10}`;
|
||||
var f = tempFun `${(x) => { return x; }} ${(((x) => { return x; }))} ${10}`;
|
||||
var g = tempFun `${((x) => { return x; })} ${((((x) => { return x; })))} ${10}`;
|
||||
var h = tempFun `${((x) => { return x; })} ${((((x) => { return x; })))} ${undefined}`;
|
||||
var a = tempFun `${x => { return x; }} ${10}`;
|
||||
var b = tempFun `${(x => { return x; })} ${10}`;
|
||||
var c = tempFun `${((x => { return x; }))} ${10}`;
|
||||
var d = tempFun `${x => { return x; }} ${x => { return x; }} ${10}`;
|
||||
var e = tempFun `${x => { return x; }} ${(x => { return x; })} ${10}`;
|
||||
var f = tempFun `${x => { return x; }} ${((x => { return x; }))} ${10}`;
|
||||
var g = tempFun `${(x => { return x; })} ${(((x => { return x; })))} ${10}`;
|
||||
var h = tempFun `${(x => { return x; })} ${(((x => { return x; })))} ${undefined}`;
|
||||
|
||||
@@ -26,18 +26,18 @@ function tempTag1(...rest) {
|
||||
// Otherwise, the arrow functions' parameters will be typed as 'any',
|
||||
// and it is an error to invoke an any-typed value with type arguments,
|
||||
// so this test will error.
|
||||
tempTag1 `${(x) => {
|
||||
tempTag1 `${x => {
|
||||
x(undefined);
|
||||
return x;
|
||||
}}${10}`;
|
||||
tempTag1 `${(x) => {
|
||||
tempTag1 `${x => {
|
||||
x(undefined);
|
||||
return x;
|
||||
}}${(y) => {
|
||||
}}${y => {
|
||||
y(undefined);
|
||||
return y;
|
||||
}}${10}`;
|
||||
tempTag1 `${(x) => {
|
||||
tempTag1 `${x => {
|
||||
x(undefined);
|
||||
return x;
|
||||
}}${(y) => {
|
||||
@@ -47,7 +47,7 @@ tempTag1 `${(x) => {
|
||||
tempTag1 `${(x) => {
|
||||
x(undefined);
|
||||
return x;
|
||||
}}${(y) => {
|
||||
}}${y => {
|
||||
y(undefined);
|
||||
return y;
|
||||
}}${undefined}`;
|
||||
|
||||
@@ -25,18 +25,18 @@ function tempTag2(...rest) {
|
||||
// Otherwise, the arrow functions' parameters will be typed as 'any',
|
||||
// and it is an error to invoke an any-typed value with type arguments,
|
||||
// so this test will error.
|
||||
tempTag2 `${(x) => {
|
||||
tempTag2 `${x => {
|
||||
x(undefined);
|
||||
return x;
|
||||
}}${0}`;
|
||||
tempTag2 `${(x) => {
|
||||
tempTag2 `${x => {
|
||||
x(undefined);
|
||||
return x;
|
||||
}}${(y) => {
|
||||
}}${y => {
|
||||
y(null);
|
||||
return y;
|
||||
}}${"hello"}`;
|
||||
tempTag2 `${(x) => {
|
||||
tempTag2 `${x => {
|
||||
x(undefined);
|
||||
return x;
|
||||
}}${undefined}${"hello"}`;
|
||||
|
||||
@@ -136,14 +136,14 @@ someGenerics5 `${null}${null}`;
|
||||
// Generic tag with multiple arguments of function types that each have parameters of the same generic type
|
||||
function someGenerics6(strs, a, b, c) {
|
||||
}
|
||||
someGenerics6 `${(n) => { return n; }}${(n) => { return n; }}${(n) => { return n; }}`;
|
||||
someGenerics6 `${(n) => { return n; }}${(n) => { return n; }}${(n) => { return n; }}`;
|
||||
someGenerics6 `${n => { return n; }}${n => { return n; }}${n => { return n; }}`;
|
||||
someGenerics6 `${n => { return n; }}${n => { return n; }}${n => { return n; }}`;
|
||||
someGenerics6 `${(n) => { return n; }}${(n) => { return n; }}${(n) => { return n; }}`;
|
||||
// Generic tag with multiple arguments of function types that each have parameters of different generic type
|
||||
function someGenerics7(strs, a, b, c) {
|
||||
}
|
||||
someGenerics7 `${(n) => { return n; }}${(n) => { return n; }}${(n) => { return n; }}`;
|
||||
someGenerics7 `${(n) => { return n; }}${(n) => { return n; }}${(n) => { return n; }}`;
|
||||
someGenerics7 `${n => { return n; }}${n => { return n; }}${n => { return n; }}`;
|
||||
someGenerics7 `${n => { return n; }}${n => { return n; }}${n => { return n; }}`;
|
||||
someGenerics7 `${(n) => { return n; }}${(n) => { return n; }}${(n) => { return n; }}`;
|
||||
// Generic tag with argument of generic function type
|
||||
function someGenerics8(strs, n) {
|
||||
|
||||
@@ -2,4 +2,4 @@
|
||||
var x = x => `abc${ x }def`;
|
||||
|
||||
//// [templateStringInArrowFunctionES6.js]
|
||||
var x = (x) => { return `abc${x}def`; };
|
||||
var x = x => { return `abc${x}def`; };
|
||||
|
||||
@@ -2,4 +2,4 @@
|
||||
var x = `abc${ x => x }def`;
|
||||
|
||||
//// [templateStringWithEmbeddedArrowFunctionES6.js]
|
||||
var x = `abc${(x) => { return x; }}def`;
|
||||
var x = `abc${x => { return x; }}def`;
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
// @target:ES5
|
||||
var arrow1 = a => { };
|
||||
var arrow2 = (a) => { };
|
||||
|
||||
var arrow3 = (a, b) => { };
|
||||
@@ -0,0 +1,5 @@
|
||||
// @target:ES6
|
||||
var arrow1 = a => { };
|
||||
var arrow2 = (a) => { };
|
||||
|
||||
var arrow3 = (a, b) => { };
|
||||
Reference in New Issue
Block a user