Skip to content

refactoring: warning revamp part 2 #17081

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

Closed
wants to merge 3 commits into from

Conversation

Jessidhia
Copy link
Contributor

@Jessidhia Jessidhia commented Oct 14, 2019

Part 2 of #16753

Updating the babel plugin to handle the new syntax was a bit tricky.

invariant was also originally not part of the scope, but because the plugin that validated arguments to warning also expected invariant to have the same interface, I had to choose between forking the plugin or also transforming invariant.

Some of the expressions may look funny; they were generated by this codemod:

'use strict';

module.exports = function transformer(file, api) {
  const j = api.jscodeshift;

  return j(file.source)
    .find(j.CallExpression)
    .filter(path => {
      const callee = path.value.callee;
      if (callee.type !== 'Identifier') {
        return false;
      }
      const name = callee.name;
      const scope = path.scope.lookup(name);

      return (
        // "isGlobal" just means file scope, even in Modules
        (scope === null || scope.isGlobal) &&
        (name === 'warning' ||
          name === 'warningWithoutStack' ||
          name === 'lowPriorityWarning' ||
          name === 'lowPriorityWarningWithoutStack' ||
          name === 'invariant')
      );
    })
    .forEach(path => {
      const firstArgument = path.value.arguments[0];
      if (firstArgument.type === 'StringLiteral') {
        // already transformed?
        return;
      }
      if (firstArgument.type === 'Literal') {
        if (firstArgument.value) {
          if (path.parentPath.value.type === 'ExpressionStatement') {
            path.prune();
          } else {
            path.replace(j.identifier('undefined'));
          }
          return;
        }
        path.value.arguments.shift();
        return;
      }

      path.value.arguments.shift();
      if (path.parentPath.value.type === 'ExpressionStatement') {
        return path.parentPath.replace(
          j.ifStatement(
            j.unaryExpression('!', firstArgument),
            j.blockStatement([path.parentPath.node])
          )
        );
      } else {
        return path.replace(
          j.sequenceExpression([
            j.logicalExpression('||', firstArgument, path.node),
            j.identifier('undefined'),
          ])
        );
      }
    })
    .toSource();
};

@Jessidhia Jessidhia changed the title refactoring: run codemod to update invocations refactoring: warning revamp part 2 Oct 14, 2019
@sizebot
Copy link

sizebot commented Oct 14, 2019

React: size: 0.0%, gzip: 0.0%

ReactDOM: size: -0.1%, gzip: -0.2%

Details of bundled changes.

Comparing: 75955bf...78be546

react

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react.development.js -0.3% -0.3% 117.33 KB 116.98 KB 29.83 KB 29.75 KB UMD_DEV
react.production.min.js 0.0% 0.0% 12.4 KB 12.4 KB 4.88 KB 4.89 KB UMD_PROD
react.profiling.min.js 0.0% -0.0% 15.92 KB 15.92 KB 5.98 KB 5.98 KB UMD_PROFILING
react.development.js -0.5% -0.4% 72.43 KB 72.07 KB 19.05 KB 18.97 KB NODE_DEV
react.production.min.js 0.0% 0.0% 6.66 KB 6.66 KB 2.77 KB 2.77 KB NODE_PROD
React-dev.js -0.1% -0.1% 70.01 KB 69.96 KB 18.11 KB 18.09 KB FB_WWW_DEV

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.profiling.min.js 0.0% 0.0% 119.46 KB 119.46 KB 37.72 KB 37.72 KB NODE_PROFILING
react-dom-server.browser.development.js -0.9% -0.4% 140.06 KB 138.73 KB 36.8 KB 36.67 KB UMD_DEV
ReactDOM-dev.js -0.6% -0.2% 968.85 KB 962.77 KB 215.29 KB 214.81 KB FB_WWW_DEV
react-dom-server.browser.production.min.js -0.1% -0.2% 19.84 KB 19.81 KB 7.37 KB 7.36 KB UMD_PROD
react-dom-test-utils.development.js -0.7% -0.5% 56.31 KB 55.93 KB 15.59 KB 15.51 KB UMD_DEV
react-dom-unstable-fizz.browser.development.js 0.0% +0.1% 3.78 KB 3.78 KB 1.53 KB 1.53 KB UMD_DEV
react-dom-test-utils.production.min.js 0.0% 0.0% 11.17 KB 11.17 KB 4.14 KB 4.14 KB UMD_PROD
ReactDOMUnstableNativeDependencies-dev.js +0.2% +0.6% 58.19 KB 58.28 KB 14.79 KB 14.88 KB FB_WWW_DEV
react-dom-test-utils.development.js -0.7% -0.5% 54.58 KB 54.2 KB 15.26 KB 15.17 KB NODE_DEV
react-dom-test-utils.production.min.js 0.0% -0.0% 10.94 KB 10.94 KB 4.08 KB 4.08 KB NODE_PROD
react-dom.development.js -0.3% -0.1% 946.68 KB 943.82 KB 214.41 KB 214.09 KB UMD_DEV
react-dom.production.min.js 0.0% 0.0% 115.69 KB 115.69 KB 37.24 KB 37.24 KB UMD_PROD
ReactTestUtils-dev.js +0.3% +0.6% 51.35 KB 51.52 KB 13.94 KB 14.02 KB FB_WWW_DEV
react-dom.profiling.min.js 0.0% 0.0% 119.22 KB 119.22 KB 38.35 KB 38.36 KB UMD_PROFILING
react-dom.development.js -0.3% -0.1% 940.75 KB 937.89 KB 212.84 KB 212.54 KB NODE_DEV
react-dom-server.node.development.js -0.9% -0.3% 137.1 KB 135.82 KB 36.02 KB 35.9 KB NODE_DEV
react-dom.production.min.js 0.0% 0.0% 115.81 KB 115.81 KB 36.7 KB 36.7 KB NODE_PROD
react-dom-server.node.production.min.js -0.2% -0.1% 20.17 KB 20.14 KB 7.5 KB 7.5 KB NODE_PROD
ReactDOM-prod.js -0.1% -0.2% 398.09 KB 397.65 KB 72.46 KB 72.28 KB FB_WWW_PROD
ReactDOM-profiling.js -0.1% -0.2% 398.88 KB 398.44 KB 72.97 KB 72.79 KB FB_WWW_PROFILING
react-dom-server.browser.development.js -1.0% -0.4% 135.99 KB 134.67 KB 35.8 KB 35.67 KB NODE_DEV
react-dom-server.browser.production.min.js -0.2% -0.1% 19.76 KB 19.73 KB 7.35 KB 7.35 KB NODE_PROD
react-dom-unstable-native-dependencies.development.js -0.2% -0.3% 60.13 KB 59.99 KB 15.79 KB 15.74 KB UMD_DEV
react-dom-unstable-native-dependencies.production.min.js 0.0% 0.0% 10.72 KB 10.72 KB 3.67 KB 3.67 KB UMD_PROD
ReactDOMServer-dev.js -1.0% -0.2% 139.29 KB 137.87 KB 35.33 KB 35.25 KB FB_WWW_DEV
ReactDOMServer-prod.js -0.0% 🔺+0.3% 48.5 KB 48.48 KB 11.08 KB 11.11 KB FB_WWW_PROD
react-dom-unstable-native-dependencies.development.js -0.2% -0.3% 59.8 KB 59.66 KB 15.66 KB 15.61 KB NODE_DEV
react-dom-unstable-native-dependencies.production.min.js 0.0% 0.0% 10.46 KB 10.46 KB 3.57 KB 3.57 KB NODE_PROD
react-dom-unstable-fizz.node.production.min.js 0.0% -0.1% 1.1 KB 1.1 KB 668 B 667 B NODE_PROD

react-art

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-art.development.js -0.3% -0.2% 667.62 KB 665.44 KB 145.2 KB 144.97 KB UMD_DEV
react-art.production.min.js 0.0% 0.0% 103.63 KB 103.63 KB 31.51 KB 31.52 KB UMD_PROD
react-art.development.js -0.4% -0.2% 598.25 KB 596.07 KB 127.8 KB 127.57 KB NODE_DEV
react-art.production.min.js 0.0% 0.0% 68.64 KB 68.64 KB 20.81 KB 20.81 KB NODE_PROD
ReactART-dev.js -0.7% -0.2% 611.47 KB 607.43 KB 127.41 KB 127.12 KB FB_WWW_DEV
ReactART-prod.js 0.0% 0.0% 232.21 KB 232.21 KB 39.15 KB 39.15 KB FB_WWW_PROD

react-test-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
ReactTestRenderer-dev.js -0.7% -0.2% 623 KB 618.8 KB 129.95 KB 129.63 KB FB_WWW_DEV
react-test-renderer-shallow.development.js -0.8% -0.5% 38.56 KB 38.26 KB 9.9 KB 9.86 KB UMD_DEV
react-test-renderer-shallow.production.min.js 0.0% 0.0% 11.43 KB 11.43 KB 3.54 KB 3.54 KB UMD_PROD
react-test-renderer-shallow.development.js -0.9% -0.5% 32.53 KB 32.22 KB 8.42 KB 8.38 KB NODE_DEV
react-test-renderer.development.js -0.4% -0.2% 612.33 KB 610.17 KB 130.81 KB 130.58 KB UMD_DEV
react-test-renderer.production.min.js 0.0% 0.0% 70.66 KB 70.66 KB 21.64 KB 21.64 KB UMD_PROD
ReactShallowRenderer-dev.js -0.1% +0.1% 33.44 KB 33.42 KB 8.31 KB 8.32 KB FB_WWW_DEV
react-test-renderer.development.js -0.4% -0.2% 607.6 KB 605.44 KB 129.63 KB 129.4 KB NODE_DEV
react-test-renderer.production.min.js 0.0% 0.0% 70.36 KB 70.36 KB 21.32 KB 21.32 KB NODE_PROD

react-reconciler

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-reconciler.development.js -0.4% -0.2% 599.23 KB 597 KB 126.97 KB 126.74 KB NODE_DEV
react-reconciler-reflection.development.js -1.2% -0.7% 19.19 KB 18.96 KB 6.26 KB 6.22 KB NODE_DEV
react-reconciler-persistent.development.js -0.4% -0.2% 596.58 KB 594.36 KB 125.86 KB 125.62 KB NODE_DEV
react-reconciler-persistent.production.min.js 0.0% 0.0% 71.37 KB 71.37 KB 21.1 KB 21.1 KB NODE_PROD

react-is

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-is.development.js -0.7% -1.0% 8.77 KB 8.7 KB 2.63 KB 2.6 KB UMD_DEV
react-is.development.js -0.7% -1.1% 8.58 KB 8.52 KB 2.58 KB 2.55 KB NODE_DEV
react-is.production.min.js 0.0% 🔺+0.1% 2.5 KB 2.5 KB 903 B 904 B NODE_PROD
ReactIs-dev.js +4.0% +6.6% 7.01 KB 7.29 KB 1.88 KB 2.01 KB FB_WWW_DEV

react-native-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
ReactNativeRenderer-profiling.js 0.0% -0.0% 281.91 KB 281.91 KB 48.47 KB 48.47 KB RN_OSS_PROFILING
ReactFabric-dev.js -0.5% -0.3% 746.85 KB 743.49 KB 158.29 KB 157.86 KB RN_FB_DEV
ReactFabric-prod.js 0.0% -0.0% 264.1 KB 264.1 KB 45.37 KB 45.37 KB RN_FB_PROD
ReactNativeRenderer-dev.js -0.4% -0.3% 741.19 KB 737.9 KB 157.37 KB 156.95 KB RN_OSS_DEV
ReactNativeRenderer-dev.js -0.4% -0.3% 741.35 KB 738.06 KB 157.46 KB 157.03 KB RN_FB_DEV
ReactNativeRenderer-prod.js 0.0% 0.0% 272.32 KB 272.32 KB 46.73 KB 46.73 KB RN_FB_PROD
ReactNativeRenderer-profiling.js 0.0% -0.0% 281.9 KB 281.9 KB 48.49 KB 48.49 KB RN_FB_PROFILING
ReactFabric-dev.js -0.5% -0.3% 746.68 KB 743.32 KB 158.22 KB 157.78 KB RN_OSS_DEV

react-cache

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-cache.development.js -1.3% -1.4% 9.18 KB 9.07 KB 3.1 KB 3.06 KB UMD_DEV
react-cache.production.min.js 0.0% 🔺+0.1% 2.38 KB 2.38 KB 1.21 KB 1.21 KB UMD_PROD
react-cache.development.js -1.3% -1.4% 8.96 KB 8.84 KB 3.03 KB 2.99 KB NODE_DEV
react-cache.production.min.js 0.0% 🔺+0.1% 2.19 KB 2.19 KB 1.13 KB 1.13 KB NODE_PROD
ReactCache-dev.js +3.7% +5.2% 7.39 KB 7.66 KB 2.39 KB 2.51 KB FB_WWW_DEV

Generated by 🚫 dangerJS against 78be546

@gaearon
Copy link
Collaborator

gaearon commented Oct 14, 2019

Seems like something's up in prod environment — prod tests are failing?

@acdlite
Copy link
Collaborator

acdlite commented Oct 14, 2019

invariant was also originally not part of the scope, but because the plugin that validated arguments to warning also expected invariant to have the same interface, I had to choose between forking the plugin or also transforming invariant.

Can you please fork the plugin instead? We have separate plans to replace those with throw Error expressions in the source and I'd rather not update those callsites twice.

@acdlite
Copy link
Collaborator

acdlite commented Oct 14, 2019

I personally hate the wrap-warning-with-env-check transform. Can we make an eslint rule instead of relying on a babel plugin to do it for us? The end game is to get to writing console.error directly.

@gaearon
Copy link
Collaborator

gaearon commented Dec 11, 2019

Thanks for starting this! @walaura picked up your script and we got to completion in #17568. ❤️

@gaearon gaearon closed this Dec 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants