Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed May 29, 2023
1 parent 2430097 commit bfaa030
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 35 deletions.
18 changes: 4 additions & 14 deletions packages/babel-core/src/errors/rewrite-stack-trace.ts
Expand Up @@ -44,12 +44,14 @@

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

const SUPPORTED = !!Error.captureStackTrace && isErrorStackTraceLimitWritable();
const SUPPORTED =
!!Error.captureStackTrace &&
Object.getOwnPropertyDescriptor(Error, "stackTraceLimit")?.writable === true;

const START_HIDING = "startHiding - secret - don't use this - v1";
const STOP_HIDING = "stopHiding - secret - don't use this - v1";

type CallSite = Parameters<typeof Error.prepareStackTrace>[1][number];
type CallSite = NodeJS.CallSite;

const expectedErrors = new WeakSet<Error>();
const virtualFrames = new WeakMap<Error, CallSite[]>();
Expand All @@ -70,18 +72,6 @@ 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
26 changes: 5 additions & 21 deletions packages/babel-core/test/errors-stacks.js
@@ -1,21 +1,10 @@
import * as babel from "../lib/index.js";

import { fileURLToPath } from "url";
import { commonJS, itGte } from "$repo-utils";
import path from "path";
import { spawnSync } from "child_process";
import semver from "semver";
import { readFileSync } from "fs";

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

let USE_ESM = false;
try {
const type = readFileSync(
new URL("../../../.module-type", import.meta.url),
"utf-8",
).trim();
USE_ESM = type === "module";
} catch {}
const { __dirname } = commonJS(import.meta.url);

const replaceAll = "".replaceAll
? Function.call.bind("".replaceAll)
Expand Down Expand Up @@ -84,12 +73,7 @@ function expectError(run) {
throw new Error("It should have thrown an error.");
}

const fixture = name =>
path.join(
path.dirname(fileURLToPath(import.meta.url)),
"fixtures/errors",
name,
);
const fixture = name => path.join(__dirname, "fixtures/errors", name);

describe("@babel/core errors", function () {
it("error inside config function", function () {
Expand Down Expand Up @@ -298,7 +282,7 @@ describe("@babel/core errors", function () {
`);
});

(semver.gte(process.version, "12.0.0") && !USE_ESM ? it : it.skip)(
itGte("12.0.0")(
"should not throw in `node --frozen-intrinsics`",
function () {
expect(
Expand All @@ -309,7 +293,7 @@ describe("@babel/core errors", function () {
"--input-type=module",
"-e",
`
import * as babel from "@babel/core";
import * as babel from "../lib/index.js";
await babel.parseAsync("foo;", {
root: String.raw\`${fixture("valid")}\`,
});
Expand Down

0 comments on commit bfaa030

Please sign in to comment.