Skip to content

Commit a0be704

Browse files
committed
Report a semantic error for an arrow function with a "this" parameter.
Fixes #9744.
1 parent 93722c8 commit a0be704

8 files changed

+599
-464
lines changed

src/compiler/checker.ts

+3
Original file line numberDiff line numberDiff line change
@@ -21857,6 +21857,9 @@ namespace ts {
2185721857
if (func.kind === SyntaxKind.Constructor || func.kind === SyntaxKind.ConstructSignature || func.kind === SyntaxKind.ConstructorType) {
2185821858
error(node, Diagnostics.A_constructor_cannot_have_a_this_parameter);
2185921859
}
21860+
if (func.kind === SyntaxKind.ArrowFunction) {
21861+
error(node, Diagnostics.An_arrow_function_cannot_have_a_this_parameter);
21862+
}
2186021863
}
2186121864

2186221865
// Only check rest parameter type if it's not a binding pattern. Since binding patterns are

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -2417,6 +2417,10 @@
24172417
"category": "Error",
24182418
"code": 2729
24192419
},
2420+
"An arrow function cannot have a 'this' parameter.": {
2421+
"category": "Error",
2422+
"code": 2730
2423+
},
24202424

24212425
"Import declaration '{0}' is using private name '{1}'.": {
24222426
"category": "Error",

src/compiler/parser.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -3537,8 +3537,9 @@ namespace ts {
35373537
}
35383538

35393539
// If we had "(" followed by something that's not an identifier,
3540-
// then this definitely doesn't look like a lambda.
3541-
if (!isIdentifier()) {
3540+
// then this definitely doesn't look like a lambda. "this" is not
3541+
// valid, but we want to parse it and then give a semantic error.
3542+
if (!isIdentifier() && second !== SyntaxKind.ThisKeyword) {
35423543
return Tristate.False;
35433544
}
35443545

tests/baselines/reference/thisTypeInFunctionsNegative.errors.txt

+79-71
Large diffs are not rendered by default.

tests/baselines/reference/thisTypeInFunctionsNegative.js

+49-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//// [thisTypeInFunctionsNegative.ts]
2+
// @target es6
3+
24
class C {
35
n: number;
46
explicitThis(this: this, m: number): number {
@@ -174,9 +176,13 @@ function initializer(this: C = new C()): number { return this.n; }
174176

175177
// can't name parameters 'this' in a lambda.
176178
c.explicitProperty = (this, m) => m + this.n;
179+
const f2 = <T>(this: {n: number}, m: number) => m + this.n;
180+
const f3 = async (this: {n: number}, m: number) => m + this.n;
181+
const f4 = async <T>(this: {n: number}, m: number) => m + this.n;
177182

178183

179184
//// [thisTypeInFunctionsNegative.js]
185+
// @target es6
180186
var __extends = (this && this.__extends) || (function () {
181187
var extendStatics = function (d, b) {
182188
extendStatics = Object.setPrototypeOf ||
@@ -190,6 +196,41 @@ var __extends = (this && this.__extends) || (function () {
190196
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
191197
};
192198
})();
199+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
200+
return new (P || (P = Promise))(function (resolve, reject) {
201+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
202+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
203+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
204+
step((generator = generator.apply(thisArg, _arguments || [])).next());
205+
});
206+
};
207+
var __generator = (this && this.__generator) || function (thisArg, body) {
208+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
209+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
210+
function verb(n) { return function (v) { return step([n, v]); }; }
211+
function step(op) {
212+
if (f) throw new TypeError("Generator is already executing.");
213+
while (_) try {
214+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
215+
if (y = 0, t) op = [op[0] & 2, t.value];
216+
switch (op[0]) {
217+
case 0: case 1: t = op; break;
218+
case 4: _.label++; return { value: op[1], done: false };
219+
case 5: _.label++; y = op[1]; op = [0]; continue;
220+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
221+
default:
222+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
223+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
224+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
225+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
226+
if (t[2]) _.ops.pop();
227+
_.trys.pop(); continue;
228+
}
229+
op = body.call(thisArg, _);
230+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
231+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
232+
}
233+
};
193234
var _this = this;
194235
var C = /** @class */ (function () {
195236
function C() {
@@ -360,5 +401,11 @@ number;
360401
return this.n;
361402
}
362403
// can't name parameters 'this' in a lambda.
363-
c.explicitProperty = (this, m);
364-
m + this.n;
404+
c.explicitProperty = function (m) { return m + _this.n; };
405+
var f2 = function (m) { return m + _this.n; };
406+
var f3 = function (m) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
407+
return [2 /*return*/, m + this.n];
408+
}); }); };
409+
var f4 = function (m) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
410+
return [2 /*return*/, m + this.n];
411+
}); }); };

0 commit comments

Comments
 (0)