Home / File/ ValidateNoJSXInTryStatement.ts — react Source File

ValidateNoJSXInTryStatement.ts — react Source File

Architecture documentation for ValidateNoJSXInTryStatement.ts, a typescript file in the react codebase. 8 imports, 1 dependents.

File typescript BabelCompiler Validation 8 imports 1 dependents 1 functions

Entity Profile

Dependency Diagram

graph LR
  e270e40b_ce98_5d39_9c94_07445e9a0ce8["ValidateNoJSXInTryStatement.ts"]
  e96f281e_f381_272d_2359_3e6a091c9a1d["CompilerError.ts"]
  e270e40b_ce98_5d39_9c94_07445e9a0ce8 --> e96f281e_f381_272d_2359_3e6a091c9a1d
  a2b91621_58d3_1d04_4663_00cd808f1034["ErrorCategory"]
  e270e40b_ce98_5d39_9c94_07445e9a0ce8 --> a2b91621_58d3_1d04_4663_00cd808f1034
  0423f759_97e0_9101_4634_ed555abc5ca9["index.ts"]
  e270e40b_ce98_5d39_9c94_07445e9a0ce8 --> 0423f759_97e0_9101_4634_ed555abc5ca9
  494e3425_0b47_293a_1ea4_d4670b0fc0e7["Result.ts"]
  e270e40b_ce98_5d39_9c94_07445e9a0ce8 --> 494e3425_0b47_293a_1ea4_d4670b0fc0e7
  7aace723_0ee1_cff5_b263_aec8e06dd79e["Result"]
  e270e40b_ce98_5d39_9c94_07445e9a0ce8 --> 7aace723_0ee1_cff5_b263_aec8e06dd79e
  eb9d33f9_42c1_205c_93e6_8e1365a31839["utils.ts"]
  e270e40b_ce98_5d39_9c94_07445e9a0ce8 --> eb9d33f9_42c1_205c_93e6_8e1365a31839
  c447b97e_0b8e_b187_e3a8_4be412d6f495["retainWhere"]
  e270e40b_ce98_5d39_9c94_07445e9a0ce8 --> c447b97e_0b8e_b187_e3a8_4be412d6f495
  2ed45bcd_6c82_3ccd_0e20_fa96b5111055[".."]
  e270e40b_ce98_5d39_9c94_07445e9a0ce8 --> 2ed45bcd_6c82_3ccd_0e20_fa96b5111055
  e3cfc07a_10c8_5dcd_e270_e8e14c29309b["Pipeline.ts"]
  e3cfc07a_10c8_5dcd_e270_e8e14c29309b --> e270e40b_ce98_5d39_9c94_07445e9a0ce8
  style e270e40b_ce98_5d39_9c94_07445e9a0ce8 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import {CompilerDiagnostic, CompilerError} from '..';
import {ErrorCategory} from '../CompilerError';
import {BlockId, HIRFunction} from '../HIR';
import {Result} from '../Utils/Result';
import {retainWhere} from '../Utils/utils';

/**
 * Developers may not be aware of error boundaries and lazy evaluation of JSX, leading them
 * to use patterns such as `let el; try { el = <Component /> } catch { ... }` to attempt to
 * catch rendering errors. Such code will fail to catch errors in rendering, but developers
 * may not realize this right away.
 *
 * This validation pass validates against this pattern: specifically, it errors for JSX
 * created within a try block. JSX is allowed within a catch statement, unless that catch
 * is itself nested inside an outer try.
 */
export function validateNoJSXInTryStatement(
  fn: HIRFunction,
): Result<void, CompilerError> {
  const activeTryBlocks: Array<BlockId> = [];
  const errors = new CompilerError();
  for (const [, block] of fn.body.blocks) {
    retainWhere(activeTryBlocks, id => id !== block.id);

    if (activeTryBlocks.length !== 0) {
      for (const instr of block.instructions) {
        const {value} = instr;
        switch (value.kind) {
          case 'JsxExpression':
          case 'JsxFragment': {
            errors.pushDiagnostic(
              CompilerDiagnostic.create({
                category: ErrorCategory.ErrorBoundaries,
                reason: 'Avoid constructing JSX within try/catch',
                description: `React does not immediately render components when JSX is rendered, so any errors from this component will not be caught by the try/catch. To catch errors in rendering a given component, wrap that component in an error boundary. (https://react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary)`,
              }).withDetails({
                kind: 'error',
                loc: value.loc,
                message: 'Avoid constructing JSX within try/catch',
              }),
            );
            break;
          }
        }
      }
    }

    if (block.terminal.kind === 'try') {
      activeTryBlocks.push(block.terminal.handler);
    }
  }
  return errors.asResult();
}

Domain

Subdomains

Frequently Asked Questions

What does ValidateNoJSXInTryStatement.ts do?
ValidateNoJSXInTryStatement.ts is a source file in the react codebase, written in typescript. It belongs to the BabelCompiler domain, Validation subdomain.
What functions are defined in ValidateNoJSXInTryStatement.ts?
ValidateNoJSXInTryStatement.ts defines 1 function(s): validateNoJSXInTryStatement.
What does ValidateNoJSXInTryStatement.ts depend on?
ValidateNoJSXInTryStatement.ts imports 8 module(s): .., CompilerError.ts, ErrorCategory, Result, Result.ts, index.ts, retainWhere, utils.ts.
What files import ValidateNoJSXInTryStatement.ts?
ValidateNoJSXInTryStatement.ts is imported by 1 file(s): Pipeline.ts.
Where is ValidateNoJSXInTryStatement.ts in the architecture?
ValidateNoJSXInTryStatement.ts is located at compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoJSXInTryStatement.ts (domain: BabelCompiler, subdomain: Validation, directory: compiler/packages/babel-plugin-react-compiler/src/Validation).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free