Files
babel/packages/babel-parser/test/helpers/fixture-error.js
Francisco Ryan Tolmasky IandGitHub 93c84126bb Fuzz test location-related parser options (#14201)
* Autogenerate tests to make sure startLine and startColumn work everywhere. Also improve the performance of tests so that the fact that we have ~88,000 tests now doesn't really cause the tests to run any slower.

* Only deserialize JSON files that are serialized.

* Don't fuzz test in node 8, since for whatever reason Jest 23 on node 8 takes forever after a certain number of tests, even if those tests are empty.

* Rename runFixtureTests.js to run-fixture-tests.js.

* Fix for showing code snippet again.

* Fix some linter errors.

* Fix stack traces and only generate the context error when we need it.

* Create errors in deserialization step instead of using adjust to convert them.

* Fix only storing cause if it's an error.

* Fix UnexpectedSuccess error.

* Better DifferentError and cleaned up the cause stuff a bit.

* Fix linter errors.

* First pass at serialization.

* Better errors and serialization.

* Fix saving output.

* Fix saving options.

* Fix linter errors and incorrect removal.

* Add FUZZ environment variable.

* Fix location undefined problem when saving.

* Read the actual options file in since we don't have a true copy of the original options.

* Fix also compacting start and end outside of loc.

* Fix linter error.

* Address a few style issues.

* Use environment variable for TEST_FUZZ, and disable fuzz testing for Node 6, 8, and 10.

* Address more change requests.

* Fix lint error.

* Move logic into FixtureError base class and add comments.

* Throw early if we are in CI, and make sure runFixtureText has its JSDocs comments.

* Fix JSDocs.

* Change fuzz testing to be opt-in while we only do line changes.

* Don't check if file exists before deleting it, and only overwite options if there's a throw property.

* Put a newline at the end of JSON files.

* Only refrain from throwing if the error is ENOENT. Also, clean up the error message when there is no cause.

* Fix linter error.
2022-01-29 08:48:12 +01:00

96 lines
2.9 KiB
JavaScript

import { inspect } from "util";
import Difference from "./difference.js";
import "./polyfill.js";
const { isArray } = Array;
const { defineProperty, entries, fromEntries } = Object;
const named = (name, object) => defineProperty(object, "name", { value: name });
const mapEntries = (object, f) => fromEntries(entries(object).map(f));
// eslint-disable-next-line no-confusing-arrow
const toContextError = error =>
isArray(error) ? error.map(toContextError) : error.context || error;
export default class FixtureError extends Error {
constructor(difference, { cause } = {}) {
super();
// Sigh, still have to manually set the name unfortunately...
named(this.constructor.name, this);
this.difference = difference;
// Set cause ourselves, since node < 17 has a bug where it won't show it
// otherwise. Technically, we display it ourselves, but best to be defensive
// in case we modify this implementation later.
if (cause) this.cause = cause;
}
static toMessage() {
return this.constructor.name;
}
get message() {
return this.constructor.toMessage(this.difference, this.cause);
}
// Don't show the stack of FixtureErrors, it's irrelevant.
// Instead, show the cause, if present.
[inspect.custom](depth, options) {
return this.cause
? `${this.message.replace(/(?<=error(s?))\.$/, ":\n")}\n${inspect(
toContextError(this.cause),
options,
)}`.replace(/\n/g, "\n ")
: this.message;
}
static fromDifference(difference, actual) {
return difference === Difference.None
? false
: difference.path[0] !== "threw"
? new FixtureError.DifferentAST(difference)
: !difference.expected
? new FixtureError.UnexpectedError(difference, { cause: actual.threw })
: difference.actual
? new FixtureError.DifferentError(difference, { cause: actual.threw })
: actual.ast && actual.ast.errors
? new FixtureError.UnexpectedRecovery(difference, {
cause: actual.ast.errors,
})
: new FixtureError.UnexpectedSuccess(difference);
}
}
Object.assign(
FixtureError,
mapEntries(
{
DifferentError: ({ expected }) =>
`Expected unrecoverable error: \n\n${expected}\n\n` +
`But instead encountered different unrecoverable error.`,
DifferentAST: ({ message }) => message,
UnexpectedError: () => `Encountered unexpected unrecoverable error.`,
UnexpectedSuccess: ({ expected }) =>
`Expected unrecoverable error:\n\n ${expected}\n\n` +
`But parsing succeeded without errors.`,
UnexpectedRecovery: ({ expected }, errors) =>
`Expected unrecoverable error:\n\n ${expected}\n\n` +
`But instead parsing recovered from ${errors.length} errors.`,
},
([name, toMessage]) => [
name,
named(
`FixtureError.${name}`,
class extends FixtureError {
static toMessage = toMessage;
},
),
],
),
);