Skip to content

Commit

Permalink
Skip node deprecation warnings when used by an old @babel package (#…
Browse files Browse the repository at this point in the history
…15484)

Skip node deprecation warnings when used by a `@babel` package
  • Loading branch information
nicolo-ribaudo committed Mar 12, 2023
1 parent 15bd88c commit 48089fb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
@@ -0,0 +1,3 @@
export function callFromAtBabelPackage(fn, arg) {
return fn(arg);
}
20 changes: 19 additions & 1 deletion packages/babel-traverse/test/traverse.js
Expand Up @@ -4,6 +4,8 @@ import * as t from "@babel/types";
import _traverse from "../lib/index.js";
const traverse = _traverse.default || _traverse;

import { callFromAtBabelPackage } from "./helpers/@babel/fake-babel-package/index.js";

describe("traverse", function () {
const code = `
var foo = "bar";
Expand Down Expand Up @@ -342,9 +344,12 @@ describe("traverse", function () {
beforeAll(() => {
jest.spyOn(console, "warn").mockImplementation(() => {});
});
afterAll(() => {
afterEach(() => {
console.warn.mockClear();
});
afterAll(() => {
console.warn.mockRestore();
});
it("should warn for deprecated node types", function testFn1() {
const visitNumericLiteral = () => {};
const visitor = {
Expand All @@ -360,6 +365,7 @@ describe("traverse", function () {
visitNumericLiteral,
]);
});

it("should warn for deprecated aliases", function testFn2() {
const visitImportOrExportDeclaration = () => {};
const visitor = {
Expand All @@ -375,6 +381,18 @@ describe("traverse", function () {
visitImportOrExportDeclaration,
]);
});

it("should not warn deprecations if usage comes from a @babel/* package", () => {
const visitImportOrExportDeclaration = () => {};
const visitor = {
ModuleDeclaration: visitImportOrExportDeclaration,
};
callFromAtBabelPackage(traverse.explode, visitor);
expect(console.warn).not.toHaveBeenCalled();
expect(visitor).toHaveProperty("ImportDeclaration.enter", [
visitImportOrExportDeclaration,
]);
});
});
});
});
22 changes: 16 additions & 6 deletions packages/babel-types/src/utils/deprecationWarning.ts
Expand Up @@ -8,9 +8,16 @@ export default function deprecationWarning(
if (warnings.has(oldName)) return;
warnings.add(oldName);

const stack = captureShortStackTrace(1, 2);
const { internal, trace } = captureShortStackTrace(1, 2);
if (internal) {
// If usage comes from an internal package, there is no point in warning because
// 1. The new version of the package will already use the new API
// 2. When the deprecation will become an error (in a future major version), users
// will have to update every package anyway.
return;
}
console.warn(
`${prefix}\`${oldName}\` has been deprecated, please migrate to \`${newName}\`\n${stack}`,
`${prefix}\`${oldName}\` has been deprecated, please migrate to \`${newName}\`\n${trace}`,
);
}

Expand All @@ -26,8 +33,11 @@ function captureShortStackTrace(skip: number, length: number) {
Error.stackTraceLimit = stackTraceLimit;
Error.prepareStackTrace = prepareStackTrace;

return stackTrace
.slice(1 + skip, 1 + skip + length)
.map(frame => ` at ${frame}`)
.join("\n");
if (!stackTrace) return { internal: false, trace: "" };

const shortStackTrace = stackTrace.slice(1 + skip, 1 + skip + length);
return {
internal: /[\\/]@babel[\\/]/.test(shortStackTrace[1].getFileName()),
trace: shortStackTrace.map(frame => ` at ${frame}`).join("\n"),
};
}

0 comments on commit 48089fb

Please sign in to comment.