Skip to content

Commit bd5b1b7

Browse files
authored
[compiler] Emit better error for unsupported syntax this (facebook#34322)
1 parent 0a1f1fc commit bd5b1b7

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

compiler/packages/babel-plugin-react-compiler/src/HIR/HIRBuilder.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,22 @@ export default class HIRBuilder {
323323
],
324324
});
325325
}
326+
if (node.name === 'this') {
327+
CompilerError.throwDiagnostic({
328+
severity: ErrorSeverity.UnsupportedJS,
329+
category: ErrorCategory.UnsupportedSyntax,
330+
reason: '`this` is not supported syntax',
331+
description:
332+
'React Compiler does not support compiling functions that use `this`',
333+
details: [
334+
{
335+
kind: 'error',
336+
message: '`this` was used here',
337+
loc: node.loc ?? GeneratedSource,
338+
},
339+
],
340+
});
341+
}
326342
const originalName = node.name;
327343
let name = originalName;
328344
let index = 0;

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/ecma/error.reserved-words.expect.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,18 @@ function useThing(fn) {
2424
```
2525
Found 1 error:
2626
27-
Error: Expected a non-reserved identifier name
28-
29-
`this` is a reserved word in JavaScript and cannot be used as an identifier name.
27+
Error: `this` is not supported syntax
28+
29+
React Compiler does not support compiling functions that use `this`
30+
31+
error.reserved-words.ts:8:28
32+
6 |
33+
7 | if (ref.current === null) {
34+
> 8 | ref.current = function (this: unknown, ...args) {
35+
| ^^^^^^^^^^^^^ `this` was used here
36+
9 | return fnRef.current.call(this, ...args);
37+
10 | };
38+
11 | }
3039
```
3140
3241

0 commit comments

Comments
 (0)