[compiler] Emit better error for unsupported syntax this (#34322)

This commit is contained in:
lauren 2025-08-27 17:58:44 -04:00 committed by GitHub
parent 0a1f1fcd50
commit bd5b1b7639
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 2 deletions

View File

@ -323,6 +323,22 @@ export default class HIRBuilder {
],
});
}
if (node.name === 'this') {
CompilerError.throwDiagnostic({
severity: ErrorSeverity.UnsupportedJS,
category: ErrorCategory.UnsupportedSyntax,
reason: '`this` is not supported syntax',
description:
'React Compiler does not support compiling functions that use `this`',
details: [
{
kind: 'error',
message: '`this` was used here',
loc: node.loc ?? GeneratedSource,
},
],
});
}
const originalName = node.name;
let name = originalName;
let index = 0;

View File

@ -24,9 +24,18 @@ function useThing(fn) {
```
Found 1 error:
Error: Expected a non-reserved identifier name
Error: `this` is not supported syntax
`this` is a reserved word in JavaScript and cannot be used as an identifier name.
React Compiler does not support compiling functions that use `this`
error.reserved-words.ts:8:28
6 |
7 | if (ref.current === null) {
> 8 | ref.current = function (this: unknown, ...args) {
| ^^^^^^^^^^^^^ `this` was used here
9 | return fnRef.current.call(this, ...args);
10 | };
11 | }
```