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

Lint rule to detect unminified errors #22429

Closed
wants to merge 4 commits into from

Conversation

acdlite
Copy link
Collaborator

@acdlite acdlite commented Sep 25, 2021

Based on #22428

Adds a lint rule that detects when an Error constructor is used without a corresponding production error code.

We already have this for invariant, but not for regular errors, i.e. throw new Error(msg). There's also nothing that enforces the use of invariant besides convention.

There are some packages where we don't care to minify errors. These are packages that run in environments where bundle size is not a concern, like react-pg. I added an override in the ESLint config to ignore these.

@facebook-github-bot facebook-github-bot added the React Core Team Opened by a member of the React Core Team label Sep 25, 2021
@sizebot
Copy link

sizebot commented Sep 25, 2021

Comparing: 7d38e4f...7d40b8a

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.min.js = 130.22 kB 130.04 kB = 41.45 kB 41.33 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js = 133.05 kB 132.87 kB = 42.42 kB 42.30 kB
facebook-www/ReactDOM-prod.classic.js = 412.61 kB 412.48 kB = 76.37 kB 76.26 kB
facebook-www/ReactDOM-prod.modern.js = 401.23 kB 401.11 kB = 74.67 kB 74.55 kB
facebook-www/ReactDOMForked-prod.classic.js = 412.61 kB 412.48 kB = 76.38 kB 76.27 kB
oss-experimental/react-fetch/cjs/react-fetch.node.production.min.js +15.18% 2.01 kB 2.31 kB +18.95% 0.91 kB 1.09 kB
oss-stable-semver/react-fetch/cjs/react-fetch.node.production.min.js +15.18% 2.01 kB 2.31 kB +18.95% 0.91 kB 1.09 kB
oss-stable/react-fetch/cjs/react-fetch.node.production.min.js +15.18% 2.01 kB 2.31 kB +18.95% 0.91 kB 1.09 kB
oss-stable-semver/react/cjs/react-unstable-shared-subset.development.js +13.10% 0.46 kB 0.52 kB +11.08% 0.33 kB 0.36 kB
oss-stable/react/cjs/react-unstable-shared-subset.development.js +13.10% 0.46 kB 0.52 kB +11.08% 0.33 kB 0.36 kB
facebook-www/ReactFlightDOMRelayClient-prod.classic.js +7.71% 5.83 kB 6.28 kB +12.91% 1.67 kB 1.89 kB
facebook-www/ReactFlightDOMRelayClient-prod.modern.js +7.71% 5.83 kB 6.28 kB +12.91% 1.67 kB 1.89 kB
oss-experimental/react-server-dom-webpack/cjs/react-server-dom-webpack.production.min.js +5.95% 3.82 kB 4.05 kB +7.52% 1.70 kB 1.83 kB
oss-stable-semver/react-server-dom-webpack/cjs/react-server-dom-webpack.production.min.js +5.95% 3.82 kB 4.05 kB +7.52% 1.70 kB 1.83 kB
oss-stable/react-server-dom-webpack/cjs/react-server-dom-webpack.production.min.js +5.95% 3.82 kB 4.05 kB +7.52% 1.70 kB 1.83 kB
oss-experimental/react-client/cjs/react-client-flight.production.min.js +5.72% 3.34 kB 3.53 kB +7.15% 1.50 kB 1.60 kB
oss-stable-semver/react-client/cjs/react-client-flight.production.min.js +5.72% 3.34 kB 3.53 kB +7.15% 1.50 kB 1.60 kB
oss-stable/react-client/cjs/react-client-flight.production.min.js +5.72% 3.34 kB 3.53 kB +7.15% 1.50 kB 1.60 kB
oss-experimental/react-server-dom-webpack/umd/react-server-dom-webpack.production.min.js +5.64% 4.02 kB 4.25 kB +7.09% 1.79 kB 1.92 kB
oss-stable-semver/react-server-dom-webpack/umd/react-server-dom-webpack.production.min.js +5.64% 4.02 kB 4.25 kB +7.09% 1.79 kB 1.92 kB
oss-stable/react-server-dom-webpack/umd/react-server-dom-webpack.production.min.js +5.64% 4.02 kB 4.25 kB +7.09% 1.79 kB 1.92 kB
oss-experimental/react-suspense-test-utils/cjs/react-suspense-test-utils.js +2.34% 2.65 kB 2.71 kB +2.99% 1.10 kB 1.14 kB
oss-stable-semver/react-suspense-test-utils/cjs/react-suspense-test-utils.js +2.34% 2.65 kB 2.71 kB +2.99% 1.10 kB 1.14 kB
oss-stable/react-suspense-test-utils/cjs/react-suspense-test-utils.js +2.34% 2.65 kB 2.71 kB +2.99% 1.10 kB 1.14 kB
oss-experimental/react-fetch/cjs/react-fetch.browser.development.js +2.05% 3.32 kB 3.39 kB +3.18% 1.10 kB 1.14 kB
oss-stable-semver/react-fetch/cjs/react-fetch.browser.development.js +2.05% 3.32 kB 3.39 kB +3.18% 1.10 kB 1.14 kB
oss-stable/react-fetch/cjs/react-fetch.browser.development.js +2.05% 3.32 kB 3.39 kB +3.18% 1.10 kB 1.14 kB
oss-experimental/jest-react/cjs/jest-react.production.min.js = 2.62 kB 2.40 kB = 1.30 kB 1.18 kB
oss-stable-semver/jest-react/cjs/jest-react.production.min.js = 2.62 kB 2.40 kB = 1.30 kB 1.18 kB
oss-stable/jest-react/cjs/jest-react.production.min.js = 2.62 kB 2.40 kB = 1.30 kB 1.18 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
oss-experimental/react-fetch/cjs/react-fetch.node.production.min.js +15.18% 2.01 kB 2.31 kB +18.95% 0.91 kB 1.09 kB
oss-stable-semver/react-fetch/cjs/react-fetch.node.production.min.js +15.18% 2.01 kB 2.31 kB +18.95% 0.91 kB 1.09 kB
oss-stable/react-fetch/cjs/react-fetch.node.production.min.js +15.18% 2.01 kB 2.31 kB +18.95% 0.91 kB 1.09 kB
oss-stable-semver/react/cjs/react-unstable-shared-subset.development.js +13.10% 0.46 kB 0.52 kB +11.08% 0.33 kB 0.36 kB
oss-stable/react/cjs/react-unstable-shared-subset.development.js +13.10% 0.46 kB 0.52 kB +11.08% 0.33 kB 0.36 kB
facebook-www/ReactFlightDOMRelayClient-prod.classic.js +7.71% 5.83 kB 6.28 kB +12.91% 1.67 kB 1.89 kB
facebook-www/ReactFlightDOMRelayClient-prod.modern.js +7.71% 5.83 kB 6.28 kB +12.91% 1.67 kB 1.89 kB
oss-experimental/react-server-dom-webpack/cjs/react-server-dom-webpack.production.min.js +5.95% 3.82 kB 4.05 kB +7.52% 1.70 kB 1.83 kB
oss-stable-semver/react-server-dom-webpack/cjs/react-server-dom-webpack.production.min.js +5.95% 3.82 kB 4.05 kB +7.52% 1.70 kB 1.83 kB
oss-stable/react-server-dom-webpack/cjs/react-server-dom-webpack.production.min.js +5.95% 3.82 kB 4.05 kB +7.52% 1.70 kB 1.83 kB
oss-experimental/react-client/cjs/react-client-flight.production.min.js +5.72% 3.34 kB 3.53 kB +7.15% 1.50 kB 1.60 kB
oss-stable-semver/react-client/cjs/react-client-flight.production.min.js +5.72% 3.34 kB 3.53 kB +7.15% 1.50 kB 1.60 kB
oss-stable/react-client/cjs/react-client-flight.production.min.js +5.72% 3.34 kB 3.53 kB +7.15% 1.50 kB 1.60 kB
oss-experimental/react-server-dom-webpack/umd/react-server-dom-webpack.production.min.js +5.64% 4.02 kB 4.25 kB +7.09% 1.79 kB 1.92 kB
oss-stable-semver/react-server-dom-webpack/umd/react-server-dom-webpack.production.min.js +5.64% 4.02 kB 4.25 kB +7.09% 1.79 kB 1.92 kB
oss-stable/react-server-dom-webpack/umd/react-server-dom-webpack.production.min.js +5.64% 4.02 kB 4.25 kB +7.09% 1.79 kB 1.92 kB
oss-experimental/react-suspense-test-utils/cjs/react-suspense-test-utils.js +2.34% 2.65 kB 2.71 kB +2.99% 1.10 kB 1.14 kB
oss-stable-semver/react-suspense-test-utils/cjs/react-suspense-test-utils.js +2.34% 2.65 kB 2.71 kB +2.99% 1.10 kB 1.14 kB
oss-stable/react-suspense-test-utils/cjs/react-suspense-test-utils.js +2.34% 2.65 kB 2.71 kB +2.99% 1.10 kB 1.14 kB
oss-experimental/react-fetch/cjs/react-fetch.browser.development.js +2.05% 3.32 kB 3.39 kB +3.18% 1.10 kB 1.14 kB
oss-stable-semver/react-fetch/cjs/react-fetch.browser.development.js +2.05% 3.32 kB 3.39 kB +3.18% 1.10 kB 1.14 kB
oss-stable/react-fetch/cjs/react-fetch.browser.development.js +2.05% 3.32 kB 3.39 kB +3.18% 1.10 kB 1.14 kB
oss-experimental/react-cache/cjs/react-cache.development.js +1.71% 8.47 kB 8.61 kB +2.23% 2.83 kB 2.89 kB
oss-stable-semver/react-cache/cjs/react-cache.development.js +1.71% 8.47 kB 8.61 kB +2.23% 2.83 kB 2.89 kB
oss-stable/react-cache/cjs/react-cache.development.js +1.71% 8.47 kB 8.61 kB +2.23% 2.83 kB 2.89 kB
oss-experimental/react-fetch/cjs/react-fetch.node.development.js +1.33% 4.98 kB 5.05 kB +2.05% 1.56 kB 1.60 kB
oss-stable-semver/react-fetch/cjs/react-fetch.node.development.js +1.33% 4.98 kB 5.05 kB +2.05% 1.56 kB 1.60 kB
oss-stable/react-fetch/cjs/react-fetch.node.development.js +1.33% 4.98 kB 5.05 kB +2.05% 1.56 kB 1.60 kB
facebook-relay/flight/ReactFlightNativeRelayClient-dev.js +0.52% 12.01 kB 12.07 kB +0.60% 3.51 kB 3.53 kB
facebook-www/ReactFlightDOMRelayClient-dev.classic.js +0.48% 12.00 kB 12.06 kB +0.60% 3.52 kB 3.54 kB
facebook-www/ReactFlightDOMRelayClient-dev.modern.js +0.48% 12.00 kB 12.06 kB +0.60% 3.52 kB 3.54 kB
oss-experimental/react-client/cjs/react-client-flight.development.js +0.38% 14.63 kB 14.69 kB +0.49% 4.29 kB 4.31 kB
oss-stable-semver/react-client/cjs/react-client-flight.development.js +0.38% 14.63 kB 14.69 kB +0.49% 4.29 kB 4.31 kB
oss-stable/react-client/cjs/react-client-flight.development.js +0.38% 14.63 kB 14.69 kB +0.49% 4.29 kB 4.31 kB
oss-experimental/react-server-dom-webpack/cjs/react-server-dom-webpack.development.js +0.34% 17.18 kB 17.24 kB +0.49% 4.92 kB 4.95 kB
oss-stable-semver/react-server-dom-webpack/cjs/react-server-dom-webpack.development.js +0.34% 17.18 kB 17.24 kB +0.49% 4.92 kB 4.95 kB
oss-stable/react-server-dom-webpack/cjs/react-server-dom-webpack.development.js +0.34% 17.18 kB 17.24 kB +0.49% 4.92 kB 4.95 kB
oss-experimental/react-server-dom-webpack/umd/react-server-dom-webpack.development.js +0.33% 18.42 kB 18.48 kB +0.48% 5.04 kB 5.07 kB
oss-stable-semver/react-server-dom-webpack/umd/react-server-dom-webpack.development.js +0.33% 18.42 kB 18.48 kB +0.48% 5.04 kB 5.07 kB
oss-stable/react-server-dom-webpack/umd/react-server-dom-webpack.development.js +0.33% 18.42 kB 18.48 kB +0.48% 5.04 kB 5.07 kB
oss-experimental/react-dom/cjs/react-dom-test-utils.development.js +0.25% 57.12 kB 57.26 kB = 16.61 kB 16.61 kB
oss-stable-semver/react-dom/cjs/react-dom-test-utils.development.js +0.25% 57.12 kB 57.26 kB = 16.61 kB 16.61 kB
oss-stable/react-dom/cjs/react-dom-test-utils.development.js +0.25% 57.12 kB 57.26 kB = 16.61 kB 16.61 kB
oss-experimental/react-dom/umd/react-dom-test-utils.development.js +0.24% 60.42 kB 60.57 kB +0.14% 16.86 kB 16.88 kB
oss-stable-semver/react-dom/umd/react-dom-test-utils.development.js +0.24% 60.42 kB 60.57 kB +0.14% 16.86 kB 16.88 kB
oss-stable/react-dom/umd/react-dom-test-utils.development.js +0.24% 60.42 kB 60.57 kB +0.14% 16.86 kB 16.88 kB
oss-experimental/react-test-renderer/cjs/react-test-renderer.production.min.js = 82.24 kB 82.06 kB = 25.42 kB 25.31 kB
oss-experimental/react-test-renderer/umd/react-test-renderer.production.min.js = 82.43 kB 82.25 kB = 25.84 kB 25.72 kB
oss-stable-semver/react-test-renderer/umd/react-test-renderer.production.min.js = 80.05 kB 79.87 kB = 24.99 kB 24.88 kB
oss-stable/react-test-renderer/umd/react-test-renderer.production.min.js = 80.05 kB 79.87 kB = 24.99 kB 24.88 kB
oss-stable-semver/react-test-renderer/cjs/react-test-renderer.production.min.js = 79.86 kB 79.68 kB = 24.67 kB 24.56 kB
oss-stable/react-test-renderer/cjs/react-test-renderer.production.min.js = 79.86 kB 79.68 kB = 24.67 kB 24.56 kB
oss-experimental/react-art/cjs/react-art.production.min.js = 85.47 kB 85.28 kB = 26.49 kB 26.36 kB
oss-stable-semver/react-art/cjs/react-art.production.min.js = 83.08 kB 82.88 kB = 25.70 kB 25.57 kB
oss-stable/react-art/cjs/react-art.production.min.js = 83.08 kB 82.88 kB = 25.70 kB 25.57 kB
oss-experimental/react-reconciler/cjs/react-reconciler.profiling.min.js = 102.64 kB 102.39 kB = 31.36 kB 31.20 kB
oss-stable-semver/react-reconciler/cjs/react-reconciler.profiling.min.js = 100.16 kB 99.92 kB = 30.61 kB 30.45 kB
oss-stable/react-reconciler/cjs/react-reconciler.profiling.min.js = 100.16 kB 99.92 kB = 30.61 kB 30.45 kB
oss-experimental/jest-react/cjs/jest-react.production.min.js = 2.62 kB 2.40 kB = 1.30 kB 1.18 kB
oss-stable-semver/jest-react/cjs/jest-react.production.min.js = 2.62 kB 2.40 kB = 1.30 kB 1.18 kB
oss-stable/jest-react/cjs/jest-react.production.min.js = 2.62 kB 2.40 kB = 1.30 kB 1.18 kB

Generated by 🚫 dangerJS against 7d40b8a

When this code was written, the error codes map (`codes.json`) was
created on-the-fly, so we had to lazily require from inside the visitor.

Because `codes.json` is now checked into source, we can import it a
single time in module scope.
We use a script to minify our error messages in production. Each message
is assigned an error code, defined in `scripts/error-codes/codes.json`.
Then our build script replaces the messages with a link to our
error decoder page, e.g. https://reactjs.org/docs/error-decoder.html/?invariant=92

This enables us to write helpful error messages without increasing the
bundle size.

Right now, the script only works for `invariant` calls. It does not work
if you throw an Error object. This is an old Facebookism that we don't
really need, other than the fact that our error minification script
relies on it.

So, I've updated the script to minify error constructors, too:

Input:
  Error(`A ${adj} message that contains ${noun}`);
Output:
  Error(formatProdErrorMessage(ERR_CODE, adj, noun));

It only works for constructors that are literally named Error, though we
could add support for other names, too.

As a next step, I will add a lint rule to enforce that errors written
this way must have a corresponding error code.
This error message wasn't being minified because it doesn't use
invariant. The reason it didn't use invariant is because this particular
error is created without begin thrown — it doesn't need to be thrown
because it's located inside the error handling part of the runtime.

Now that the error minification script supports Error constructors, we
can minify it by assigning it a production error code in
`scripts/error-codes/codes.json`.

To support the use of Error constructors more generally, I will add a
lint rule that enforces each message has a corresponding error code.
Adds a lint rule that detects when an Error constructor is used without
a corresponding production error code.

We already have this for `invariant`, but not for regular errors, i.e.
`throw new Error(msg)`. There's also nothing that enforces the use of
`invariant` besides convention.

There are some packages where we don't care to minify errors. These are
packages that run in environments where bundle size is not a concern,
like react-pg. I added an override in the ESLint config to ignore these.
Copy link
Member

@rickhanlonii rickhanlonii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking, but I feel like needing to disable this many instances is a smell. Why are so many disabled?

@@ -127,6 +127,41 @@ module.exports = {
},

overrides: [
{
// By default, anything error message that appears the packages directory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// By default, anything error message that appears the packages directory
// By default, any error message that appears the packages directory

@@ -397,6 +397,7 @@ export function resolveError(
message: string,
stack: string,
): void {
// eslint-disable-next-line react-internal/prod-error-codes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe include a reason why?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto elsewhere

@@ -12,19 +12,22 @@ export type StringDecoder = void;
export const supportsBinaryStreams = false;

export function createStringDecoder(): void {
// eslint-disable-next-line react-internal/prod-error-codes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about defining something like an NotImplementedError or UnminifiedError which is automatically ignored by the lint rule and parser?

@acdlite
Copy link
Collaborator Author

acdlite commented Sep 30, 2021

Not blocking, but I feel like needing to disable this many instances is a smell. Why are so many disabled?

It reflects that we're not super consistent about which errors we minify and which ones we don't, because there's no existing lint rule that enforces it either way.

The first step should be to decide which packages should have minified error messages. I think probably everything that runs in the browser, but not stuff that runs elsewhere. For RN we don't minify currently, which I suppose makes sense since React itself is never downloaded OTA (I think?).

If there are exceptions after that we can consider adding an escape hatch, though I would still prefer an inline comment directive (like // no-minify-error-message or something) since it doesn't have runtime implications and we can adapt it to work with any Error subclass, like TypeError for example.

@acdlite acdlite closed this Sep 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants