@samitouri / QOS-React / commits / 4666bf8c17

[dx] Improve BuildHIR error messages

ghstack-source-id: cbf62aec4e768249a8c7f44fe6f3852183127415 Pull Request resolved: https://github.com/facebook/react-forget/pull/2858

Joe Savona committed Apr 17, 2024 at 18:16 UTC 4666bf8c17b7af893157d91e7eeafed0d1a87b61
1 file changed +15 -11
compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts
+15 -11
@@ -184,8 +184,9 @@ export function lower(
184 directives = body.get("directives").map((d) => d.node.value.value);
185 } else {
186 builder.errors.push({
187 - reason: `Unexpected function body kind: ${body.type}}. This error is likely caused by a bug in React Compiler. Please file an issue`,
187 severity: ErrorSeverity.InvalidJS,
188 + reason: `Unexpected function body kind`,
189 + description: `Expected function body to be an expression or a block statement, got '${body.type}'`,
190 loc: body.node.loc ?? null,
191 suggestions: null,
192 });
@@ -738,7 +739,7 @@ function lowerStatement(
739 if (testExpr.node == null) {
740 if (hasDefault) {
741 builder.errors.push({
741 - reason: `Expected at most one \`default\` branch in SwitchStatement, this code should have failed to parse. This error is likely caused by a bug in React Compiler. Please file an issue`,
742 + reason: `Expected at most one \`default\` branch in a switch statement, this code should have failed to parse`,
743 severity: ErrorSeverity.InvalidJS,
744 loc: case_.node.loc ?? null,
745 suggestions: null,
@@ -855,12 +856,12 @@ function lowerStatement(
856 if (kind === InstructionKind.Const) {
857 const declRangeStart = declaration.parentPath.node.start!;
858 builder.errors.push({
858 - reason: `Invalid declaration kind (const), this variable is reassigned later`,
859 + reason: `Expect \`const\` declaration not to be reassigned`,
860 severity: ErrorSeverity.InvalidJS,
861 loc: id.node.loc ?? null,
862 suggestions: [
863 {
863 - description: "Change to let",
864 + description: "Change to a `let` declaration",
865 op: CompilerSuggestionOperation.Replace,
866 range: [declRangeStart, declRangeStart + 5], // "const".length
867 text: "let",
@@ -901,7 +902,8 @@ function lowerStatement(
902 }
903 } else {
904 builder.errors.push({
904 - reason: `Expected variable declaration to be an identifier if no initializer was provided. This error is likely caused by a bug in React Compiler. Please file an issue`,
905 + reason: `Expected variable declaration to be an identifier if no initializer was provided`,
906 + description: `Got a \`${id.type}\``,
907 severity: ErrorSeverity.InvalidJS,
908 loc: stmt.node.loc ?? null,
909 suggestions: null,
@@ -1550,7 +1552,8 @@ function lowerExpression(
1552 const calleePath = expr.get("callee");
1553 if (!calleePath.isExpression()) {
1554 builder.errors.push({
1553 - reason: `Expected Expression, got ${calleePath.type} in NewExpression (v8 intrinsics not supported): ${calleePath.type}. This error is likely caused by a bug in React Compiler. Please file an issue`,
1555 + reason: `Expected an expression as the \`new\` expression receiver (v8 intrinsics are not supported)`,
1556 + description: `Got a \`${calleePath.node.type}\``,
1557 severity: ErrorSeverity.InvalidJS,
1558 loc: calleePath.node.loc ?? null,
1559 suggestions: null,
@@ -1642,7 +1645,7 @@ function lowerExpression(
1645 }
1646 if (last === null) {
1647 builder.errors.push({
1645 - reason: `Expected SequenceExpression to have at least one expression. This error is likely caused by a bug in React Compiler. Please file an issue`,
1648 + reason: `Expected sequence expression to have at least one expression`,
1649 severity: ErrorSeverity.InvalidJS,
1650 loc: expr.node.loc ?? null,
1651 suggestions: null,
@@ -2187,7 +2190,7 @@ function lowerExpression(
2190
2191 if (subexprs.length !== quasis.length - 1) {
2192 builder.errors.push({
2190 - reason: `Unexpected quasi and subexpression lengths in TemplateLiteral. This error is likely caused by a bug in React Compiler. Please file an issue`,
2193 + reason: `Unexpected quasi and subexpression lengths in template literal`,
2194 severity: ErrorSeverity.InvalidJS,
2195 loc: exprPath.node.loc ?? null,
2196 suggestions: null,
@@ -2239,7 +2242,7 @@ function lowerExpression(
2242 }
2243 } else {
2244 builder.errors.push({
2242 - reason: `Deleting a non-member expression has no semantic meaning`,
2245 + reason: `Only object properties can be deleted`,
2246 severity: ErrorSeverity.InvalidJS,
2247 loc: expr.node.loc ?? null,
2248 suggestions: [
@@ -2945,7 +2948,8 @@ function lowerJsxElementName(
2948 const tag = `${namespace}:${name}`;
2949 if (namespace.indexOf(":") !== -1 || name.indexOf(":") !== -1) {
2950 builder.errors.push({
2948 - reason: `Expected JSXNamespacedName to have no colons in the namespace or name, got '${namespace}' : '${name}'. This error is likely caused by a bug in React Compiler. Please file an issue`,
2951 + reason: `Expected JSXNamespacedName to have no colons in the namespace or name`,
2952 + description: `Got \`${namespace}\` : \`${name}\``,
2953 severity: ErrorSeverity.InvalidJS,
2954 loc: exprPath.node.loc ?? null,
2955 suggestions: null,
@@ -3327,7 +3331,7 @@ function lowerAssignment(
3331 if (kind !== InstructionKind.Reassign && !isHoistedIdentifier) {
3332 if (kind === InstructionKind.Const) {
3333 builder.errors.push({
3330 - reason: `[lowerAssignment] Invalid declaration kind (const), this variable is reassigned later`,
3334 + reason: `Expected \`const\` declaration not to be reassigned`,
3335 severity: ErrorSeverity.InvalidJS,
3336 loc: lvalue.node.loc ?? null,
3337 suggestions: null,