Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile invariant directly to throw expressions #15071

Merged
merged 4 commits into from
Mar 18, 2019

Commits on Mar 18, 2019

  1. Transform invariant to custom error type

    This transforms calls to the invariant module:
    
    ```js
    invariant(condition, 'A %s message that contains %s', adj, noun);
    ```
    
    Into throw statements:
    
    ```js
    if (!condition) {
      if (__DEV__) {
        throw ReactError(`A ${adj} message that contains ${noun}`);
      } else {
        throw ReactErrorProd(ERR_CODE, [adj, noun]);
      }
    }
    ```
    
    The only thing ReactError does is return an error whose name is set
    to "Invariant Violation" to match the existing behavior.
    
    ReactErrorProd is a special version used in production that throws
    a minified error code, with a link to see to expanded form. This
    replaces the reactProdInvariant module.
    
    As a next step, I would like to replace our use of the invariant module
    for user facing errors by transforming normal Error constructors to
    ReactError and ReactErrorProd. (We can continue using invariant for
    internal React errors that are meant to be unreachable, which was the
    original purpose of invariant.)
    acdlite committed Mar 18, 2019
    Configuration menu
    Copy the full SHA
    8ea8d83 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    cbd7540 View commit details
    Browse the repository at this point in the history
  3. Use arguments instead of an array

    I wasn't sure about this part so I asked Sebastian, and his rationale
    was that using arguments will make ReactErrorProd slightly slower, but
    using an array will likely make all the functions that throw slightly
    slower to compile, so it's hard to say which way is better. But since
    ReactErrorProd is in an error path, and fewer bytes is generally better,
    no array is good.
    acdlite committed Mar 18, 2019
    Configuration menu
    Copy the full SHA
    89083cf View commit details
    Browse the repository at this point in the history
  4. Casing nit

    acdlite committed Mar 18, 2019
    Configuration menu
    Copy the full SHA
    8e7cb77 View commit details
    Browse the repository at this point in the history