From bad577be00807498f6dce29128d34b55ec599583 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Tue, 19 Apr 2016 14:35:14 -0700 Subject: [PATCH 1/3] Skip emit of this parameters --- src/compiler/utilities.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 7e393c5a35a..fef23ebdcf1 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -3331,7 +3331,7 @@ namespace ts { } export function isParameter(node: Node): node is ParameterDeclaration { - return node.kind === SyntaxKind.Parameter; + return node.kind === SyntaxKind.Parameter && ((node as ParameterDeclaration).name as Identifier).text !== "this"; } export function isDecorator(node: Node): node is Decorator { From 9797c9c5123a62e05124079f78c71d459a09736e Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Tue, 19 Apr 2016 16:18:56 -0700 Subject: [PATCH 2/3] Mark 'this' parameters in the binder. This lets the TypeScript visitor correctly omit them. --- src/compiler/binder.ts | 5 +++++ src/compiler/transformers/ts.ts | 5 +++++ src/compiler/utilities.ts | 2 +- tests/baselines/reference/thisTypeInFunctions.js | 3 ++- tests/baselines/reference/thisTypeInFunctionsNegative.js | 5 +++-- 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 0d9002a1ddb..4027539334e 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -2267,6 +2267,11 @@ namespace ts { transformFlags |= TransformFlags.AssertTypeScript; } + // If the parameter's name is 'this, then it is TypeScript syntax. + if ((node.name as Identifier).text === "this") { + transformFlags |= TransformFlags.AssertTypeScript; + } + // If a parameter has an accessibility modifier, then it is TypeScript syntax. if (hasModifier(node, ModifierFlags.AccessibilityModifier)) { transformFlags |= TransformFlags.AssertTypeScript | TransformFlags.ContainsParameterPropertyAssignments; diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 9bcd5344ea8..0f96c65a738 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -359,6 +359,7 @@ namespace ts { // - accessibility modifiers // - the question mark (?) token for optional parameters // - type annotations + // - this parameters return visitParameter(node); case SyntaxKind.ParenthesizedExpression: @@ -2114,10 +2115,14 @@ namespace ts { * This function will be called when one of the following conditions are met: * - The node has an accessibility modifier. * - The node has a questionToken. + * - The node's text is "this". * * @param node The parameter declaration node. */ function visitParameter(node: ParameterDeclaration) { + if ((node.name as Identifier).text === "this") { + return undefined; + } const clone = getMutableClone(node); clone.decorators = undefined; clone.modifiers = undefined; diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index fef23ebdcf1..7e393c5a35a 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -3331,7 +3331,7 @@ namespace ts { } export function isParameter(node: Node): node is ParameterDeclaration { - return node.kind === SyntaxKind.Parameter && ((node as ParameterDeclaration).name as Identifier).text !== "this"; + return node.kind === SyntaxKind.Parameter; } export function isDecorator(node: Node): node is Decorator { diff --git a/tests/baselines/reference/thisTypeInFunctions.js b/tests/baselines/reference/thisTypeInFunctions.js index cfe1706ae99..ccec3c563e8 100644 --- a/tests/baselines/reference/thisTypeInFunctions.js +++ b/tests/baselines/reference/thisTypeInFunctions.js @@ -243,7 +243,8 @@ function implicitThis(n) { var impl = { a: 12, explicitVoid2: function () { return _this.a; }, - explicitVoid1: function () { return 12; }, + explicitVoid1: // ok, this: any because it refers to some outer object (window?) + function () { return 12; }, explicitStructural: function () { return this.a; }, diff --git a/tests/baselines/reference/thisTypeInFunctionsNegative.js b/tests/baselines/reference/thisTypeInFunctionsNegative.js index 124a7c88013..93c65031212 100644 --- a/tests/baselines/reference/thisTypeInFunctionsNegative.js +++ b/tests/baselines/reference/thisTypeInFunctionsNegative.js @@ -341,18 +341,19 @@ var ThisConstructor = (function () { return ThisConstructor; }()); var thisConstructorType; -function notFirst(a, this) { return this.n; } +function notFirst(a) { return this.n; } ///// parse errors ///// function modifiers(, C) { if ( === void 0) { = this; } return this.n; } -function restParam(, C) { return this.n; } +function restParam(C) { return this.n; } function optional(C) { return this.n; } function decorated(, C) { if ( === void 0) { = this; } return this.n; } +function initializer() { } new C(); number; { From c55de357b78c074e96e3504467c491056bf68d57 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Wed, 20 Apr 2016 09:04:34 -0700 Subject: [PATCH 3/3] Address PR comments 1. Test name.originalKeywordKind: SyntaxKind instead of name.text: string. 2. Revert regression in comment placement when emitting methods as function properties. --- src/compiler/binder.ts | 4 ++-- src/compiler/transformers/ts.ts | 4 ++-- tests/baselines/reference/thisTypeInFunctions.js | 3 +-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 4027539334e..fc08a862007 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -2267,8 +2267,8 @@ namespace ts { transformFlags |= TransformFlags.AssertTypeScript; } - // If the parameter's name is 'this, then it is TypeScript syntax. - if ((node.name as Identifier).text === "this") { + // If the parameter's name is 'this', then it is TypeScript syntax. + if (node.name && (node.name as Identifier).originalKeywordKind === SyntaxKind.ThisKeyword) { transformFlags |= TransformFlags.AssertTypeScript; } diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 0f96c65a738..f930b7bc93a 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -2115,12 +2115,12 @@ namespace ts { * This function will be called when one of the following conditions are met: * - The node has an accessibility modifier. * - The node has a questionToken. - * - The node's text is "this". + * - The node's kind is ThisKeyword. * * @param node The parameter declaration node. */ function visitParameter(node: ParameterDeclaration) { - if ((node.name as Identifier).text === "this") { + if (node.name && (node.name as Identifier).originalKeywordKind === SyntaxKind.ThisKeyword) { return undefined; } const clone = getMutableClone(node); diff --git a/tests/baselines/reference/thisTypeInFunctions.js b/tests/baselines/reference/thisTypeInFunctions.js index ccec3c563e8..cfe1706ae99 100644 --- a/tests/baselines/reference/thisTypeInFunctions.js +++ b/tests/baselines/reference/thisTypeInFunctions.js @@ -243,8 +243,7 @@ function implicitThis(n) { var impl = { a: 12, explicitVoid2: function () { return _this.a; }, - explicitVoid1: // ok, this: any because it refers to some outer object (window?) - function () { return 12; }, + explicitVoid1: function () { return 12; }, explicitStructural: function () { return this.a; },