Files
babel/packages/babel-parser/test/helpers/to-contextual-syntax-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

47 lines
1.5 KiB
JavaScript

import { codeFrameColumns } from "@babel/code-frame";
import { runInThisContext } from "vm";
export default function toContextualSyntaxError(
error,
source,
filename,
options,
) {
if (!(error instanceof SyntaxError)) return error;
const { startLine = 1, startColumn = 0 } = options || {};
const line = error.loc.line - startLine + 1;
const column =
1 + (line === 1 ? error.loc.column - startColumn : error.loc.column);
const frame = codeFrameColumns(
source,
{ start: { line, column } },
{ highlightCode: true },
);
// We make this a lazy property since we don't want pay the price of creating
// this new error unless we are going to display it.
Object.defineProperty(error, "context", {
get() {
const message = JSON.stringify(`${error.message}\n${frame}`);
const originalStackTraceLimit = Error.stackTraceLimit;
// Limit this stack trace to 1. Everything after that is just stuff from
// the test.
Error.stackTraceLimit = 1;
// The only way to manipulate the file in which a SyntaxError reports it
// is coming from is to roundtrip through vm.runInThisContext. If v8
// supported Firefox's non-standard Error.fileName property, that could be
// used instead, but unfortunately it does not.
const context = runInThisContext(`SyntaxError(${message})`, { filename });
Error.stackTraceLimit = originalStackTraceLimit;
return Object.assign(context, { cause: this });
},
});
return error;
}