Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip node deprecation warnings when used by an old @babel package #15484

Merged
merged 1 commit into from Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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"),
};
}