Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed May 13, 2023
1 parent f347e9f commit 96ebec3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
14 changes: 13 additions & 1 deletion packages/babel-core/src/errors/rewrite-stack-trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

const ErrorToString = Function.call.bind(Error.prototype.toString);

const SUPPORTED = !!Error.captureStackTrace;
const SUPPORTED = !!Error.captureStackTrace && isErrorStackTraceLimitWritable();

const START_HIDING = "startHiding - secret - don't use this - v1";
const STOP_HIDING = "stopHiding - secret - don't use this - v1";
Expand All @@ -70,6 +70,18 @@ function CallSite(filename: string): CallSite {
} as CallSite);
}

// https://github.com/nodejs/node/pull/38215/files#diff-670bf55805b781d9a3579f8bca9104c04d94af87cc33220149fd7d37b095ca1cR210
function isErrorStackTraceLimitWritable() {
const desc = Object.getOwnPropertyDescriptor(Error, "stackTraceLimit");
if (desc === undefined) {
return Object.isExtensible(Error);
}

return Object.prototype.hasOwnProperty.call(desc, "writable")
? desc.writable
: desc.set !== undefined;
}

export function injectVirtualStackFrame(error: Error, filename: string) {
if (!SUPPORTED) return;

Expand Down
29 changes: 29 additions & 0 deletions packages/babel-core/test/errors-stacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import * as babel from "../lib/index.js";

import { fileURLToPath } from "url";
import path from "path";
import { spawnSync } from "child_process";
import semver from "semver";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const replaceAll = "".replaceAll
? Function.call.bind("".replaceAll)
Expand Down Expand Up @@ -283,4 +287,29 @@ describe("@babel/core errors", function () {
at ... internal jest frames ..."
`);
});

(semver.gte(process.version, "12.0.0") ? it : it.skip)(
"should not throw in `node --frozen-intrinsics`",
function () {
expect(
spawnSync(
process.execPath,
[
"--frozen-intrinsics",
"-e",
`
const babel = require("../lib/index.js");
babel.parseSync("foo;", {
root: String.raw\`${fixture("valid")}\`,
});
console.log("%done%");
`,
],
{
cwd: __dirname,
},
).output + "",
).toContain("%done%");
},
);
});

0 comments on commit 96ebec3

Please sign in to comment.