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

Generate @apollo/client/invariantErrorCodes.js for each release. #6665

Merged
merged 3 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

## Improvements

- Errors of the form `Invariant Violation: 42` thrown in production can now be looked up much more easily, by consulting the auto-generated `@apollo/client/invariantErrorCodes.js` file specific to your `@apollo/client` version. <br/>
[@benjamn](https://github.com/benjamn) in [#6665](https://github.com/apollographql/apollo-client/pull/6665)

- Make the `client` field of the `MutationResult` type non-optional, since it is always provided. <br/>
[@glasser](https://github.com/glasser) in [#6617](https://github.com/apollographql/apollo-client/pull/6617)

Expand Down
67 changes: 51 additions & 16 deletions config/processInvariants.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,78 @@
import { readFileSync, writeFileSync } from "fs";
import { resolve, relative } from "path";
import * as fs from "fs";
import * as path from "path";
import glob = require("glob");

const distDir = resolve(__dirname, "..", "dist");
const distDir = path.resolve(__dirname, "..", "dist");

glob(`${distDir}/**/*.js`, (error, files) => {
if (error) throw error;

files.sort().forEach(file => {
const relPath = relative(distDir, file);
const relPath = path.relative(distDir, file);

// Outside the distDir, somehow.
if (relPath.startsWith("../")) return;

// Avoid re-transforming CommonJS bundle files.
if (relPath.endsWith(".cjs.js")) return;

const source = readFileSync(file, "utf8");
const source = fs.readFileSync(file, "utf8");
const output = transform(source, relPath);
if (source !== output) {
console.log("transformed invariants in " + relPath);
writeFileSync(file, output, "utf8");
fs.writeFileSync(file, output, "utf8");
}
});

fs.writeFileSync(
path.join(distDir, "invariantErrorCodes.js"),
recast.print(errorCodeManifest, {
tabWidth: 2,
}).code + "\n",
);
});

import * as recast from "recast";
import * as parser from "recast/parsers/babel";
const b = recast.types.builders;
const n = recast.types.namedTypes;
type Node = recast.types.namedTypes.Node;
type NumericLiteral = recast.types.namedTypes.NumericLiteral;
type CallExpression = recast.types.namedTypes.CallExpression;
type NewExpression = recast.types.namedTypes.NewExpression;
let nextErrorCode = 1;

function transform(code: string, id: string) {
const errorCodeManifest = b.objectExpression([
b.property("init",
b.stringLiteral("@apollo/client version"),
b.stringLiteral(require("../package.json").version),
),
]);

errorCodeManifest.comments = [
b.commentLine(' This file is meant to help with looking up the source of errors like', true),
b.commentLine(' "Invariant Violation: 35" and is automatically generated by the file', true),
b.commentLine(' @apollo/client/config/processInvariants.ts for each @apollo/client', true),
b.commentLine(' release. The numbers may change from release to release, so please', true),
b.commentLine(' consult the @apollo/client/invariantErrorCodes.js file specific to', true),
b.commentLine(' your @apollo/client version. This file is not meant to be imported.', true),
];

function getErrorCode(
file: string,
expr: CallExpression | NewExpression,
): NumericLiteral {
const numLit = b.numericLiteral(nextErrorCode++);
errorCodeManifest.properties.push(
b.property("init", numLit, b.objectExpression([
b.property("init", b.identifier("file"), b.stringLiteral("@apollo/client/" + file)),
b.property("init", b.identifier("node"), expr),
])),
);
return numLit;
}

function transform(code: string, file: string) {
// If the code doesn't seem to contain anything invariant-related, we
// can skip parsing and transforming it.
if (!/invariant/i.test(code)) {
Expand All @@ -50,7 +92,7 @@ function transform(code: string, id: string) {
}

const newArgs = node.arguments.slice(0, 1);
newArgs.push(b.numericLiteral(nextErrorCode++));
newArgs.push(getErrorCode(file, node));

return b.conditionalExpression(
makeNodeEnvTest(),
Expand Down Expand Up @@ -80,9 +122,7 @@ function transform(code: string, id: string) {
return;
}

const newArgs = [
b.numericLiteral(nextErrorCode++),
];
const newArgs = [getErrorCode(file, node)];

return b.conditionalExpression(
makeNodeEnvTest(),
Expand All @@ -99,11 +139,6 @@ function transform(code: string, id: string) {
return recast.print(ast).code;
}

const n = recast.types.namedTypes;
type Node = recast.types.namedTypes.Node;
type CallExpression = recast.types.namedTypes.CallExpression;
type NewExpression = recast.types.namedTypes.NewExpression;

function isIdWithName(node: Node, ...names: string[]) {
return n.Identifier.check(node) &&
names.some(name => name === node.name);
Expand Down