Remove deprecated default export from @babel/code-frame (#17772)

* Remove deprecated default export from `@babel/code-frame`

* Clean

* Update tests
This commit is contained in:
fisker Cheung
2026-02-04 10:16:10 -05:00
committed by GitHub
parent 973504b8b6
commit fcc6264ebe
4 changed files with 84 additions and 104 deletions
-32
View File
@@ -4,8 +4,6 @@ import {
type Options,
} from "./common.ts";
let deprecationWarningShown = false;
export type { Options };
export function codeFrameColumns(
@@ -16,36 +14,6 @@ export function codeFrameColumns(
return _codeFrameColumns(rawLines, loc, opts);
}
/**
* Create a code frame, adding line numbers, code highlighting, and pointing to a given position.
*/
export default function (
rawLines: string,
lineNumber: number,
colNumber?: number | null,
opts: Options = {},
): string {
if (!deprecationWarningShown) {
deprecationWarningShown = true;
const message =
"Passing lineNumber and colNumber is deprecated to @babel/code-frame. Please use `codeFrameColumns`.";
const deprecationError = new Error(message);
deprecationError.name = "DeprecationWarning";
console.warn(new Error(message));
}
colNumber = Math.max(colNumber, 0);
const location: NodeLocation = {
start: { column: colNumber, line: lineNumber },
};
return codeFrameColumns(rawLines, location, opts);
}
export function highlight(code: string) {
return code;
}
-38
View File
@@ -8,8 +8,6 @@ import { highlight } from "./highlight.ts";
export { highlight };
let deprecationWarningShown = false;
export type { Options };
export function codeFrameColumns(
@@ -32,39 +30,3 @@ export function codeFrameColumns(
: undefined,
);
}
/**
* Create a code frame, adding line numbers, code highlighting, and pointing to a given position.
*/
export default function (
rawLines: string,
lineNumber: number,
colNumber?: number | null,
opts: Options = {},
): string {
if (!deprecationWarningShown) {
deprecationWarningShown = true;
const message =
"Passing lineNumber and colNumber is deprecated to @babel/code-frame. Please use `codeFrameColumns`.";
if (process.emitWarning) {
// A string is directly supplied to emitWarning, because when supplying an
// Error object node throws in the tests because of different contexts
process.emitWarning(message, "DeprecationWarning");
} else {
const deprecationError = new Error(message);
deprecationError.name = "DeprecationWarning";
console.warn(new Error(message));
}
}
colNumber = Math.max(colNumber, 0);
const location: NodeLocation = {
start: { column: colNumber, line: lineNumber },
};
return codeFrameColumns(rawLines, location, opts);
}
@@ -1,7 +1,6 @@
import { stripVTControlCharacters, styleText } from "node:util";
import _codeFrame, { codeFrameColumns } from "../lib/index.js";
const codeFrame = _codeFrame.default || _codeFrame;
import { codeFrameColumns } from "../lib/index.js";
const gutter = input => styleText("gray", input, { validateStream: false });
const yellow = input => styleText("yellow", input, { validateStream: false });
@@ -28,7 +27,11 @@ describe("highlight", function () {
test("opts.highlightCode", function () {
const rawLines = "console.log('babel')";
const result = codeFrame(rawLines, 1, 9, { highlightCode: true });
const result = codeFrameColumns(
rawLines,
{ start: { line: 1, column: 9 } },
{ highlightCode: true },
);
const stripped = stripVTControlCharacters(result);
expect(result.length).toBeGreaterThan(stripped.length);
expect(stripped).toEqual(
@@ -83,11 +86,15 @@ describe("highlight", function () {
test("opts.forceColor", function () {
const rawLines = ["", "", "", ""].join("\n");
expect(
codeFrame(rawLines, 3, null, {
linesAbove: 1,
linesBelow: 1,
forceColor: true,
}),
codeFrameColumns(
rawLines,
{ start: { line: 3, column: null } },
{
linesAbove: 1,
linesBelow: 1,
forceColor: true,
},
),
).toEqual(
reset(
[
@@ -104,11 +111,15 @@ describe("highlight", function () {
expect(
JSON.stringify(
codeFrame(rawLines, 0, null, {
linesAbove: 1,
linesBelow: 1,
forceColor: true,
}),
codeFrameColumns(
rawLines,
{ start: { line: 0, column: null } },
{
linesAbove: 1,
linesBelow: 1,
forceColor: true,
},
),
),
).toEqual(
JSON.stringify(
@@ -131,11 +142,15 @@ describe("highlight", function () {
expect(
JSON.stringify(
codeFrame(rawLines, 0, null, {
linesAbove: 1,
linesBelow: 1,
forceColor: true,
}),
codeFrameColumns(
rawLines,
{ start: { line: 0, column: null } },
{
linesAbove: 1,
linesBelow: 1,
forceColor: true,
},
),
),
).toEqual(
JSON.stringify(
@@ -161,7 +176,11 @@ describe("highlight", function () {
test("opts.highlightCode", function () {
const rawLines = "console.log('babel')";
const result = codeFrame(rawLines, 1, 9, { highlightCode: true });
const result = codeFrameColumns(
rawLines,
{ start: { line: 1, column: 9 } },
{ highlightCode: true },
);
const stripped = stripVTControlCharacters(result);
expect(result).toBe(stripped);
expect(stripped).toEqual(
@@ -172,11 +191,15 @@ describe("highlight", function () {
test("opts.forceColor", function () {
const rawLines = ["", "", "", ""].join("\n");
expect(
codeFrame(rawLines, 3, null, {
linesAbove: 1,
linesBelow: 1,
forceColor: true,
}),
codeFrameColumns(
rawLines,
{ start: { line: 3, column: null } },
{
linesAbove: 1,
linesBelow: 1,
forceColor: true,
},
),
).toEqual(
reset(
[
+37 -10
View File
@@ -1,11 +1,12 @@
import { stripVTControlCharacters } from "node:util";
import _codeFrame, { codeFrameColumns, highlight } from "../lib/index.js";
const codeFrame = _codeFrame.default || _codeFrame;
import { codeFrameColumns, highlight } from "../lib/index.js";
describe("@babel/code-frame", function () {
test("basic usage", function () {
const rawLines = ["class Foo {", " constructor()", "};"].join("\n");
expect(codeFrame(rawLines, 2, 16)).toEqual(
expect(
codeFrameColumns(rawLines, { start: { line: 2, column: 16 } }),
).toEqual(
[
" 1 | class Foo {",
"> 2 | constructor()",
@@ -17,7 +18,9 @@ describe("@babel/code-frame", function () {
test("optional column number", function () {
const rawLines = ["class Foo {", " constructor()", "};"].join("\n");
expect(codeFrame(rawLines, 2, null)).toEqual(
expect(
codeFrameColumns(rawLines, { start: { line: 2, column: null } }),
).toEqual(
[" 1 | class Foo {", "> 2 | constructor()", " 3 | };"].join("\n"),
);
});
@@ -36,7 +39,9 @@ describe("@babel/code-frame", function () {
" return a + b",
"}",
].join("\n");
expect(codeFrame(rawLines, 7, 2)).toEqual(
expect(
codeFrameColumns(rawLines, { start: { line: 7, column: 2 } }),
).toEqual(
[
" 5 | * @param b Number",
" 6 | * @returns Number",
@@ -63,7 +68,9 @@ describe("@babel/code-frame", function () {
" return a + b",
"}",
].join("\n");
expect(codeFrame(rawLines, 6, 2)).toEqual(
expect(
codeFrameColumns(rawLines, { start: { line: 6, column: 2 } }),
).toEqual(
[
" 4 | * @param a Number",
" 5 | * @param b Number",
@@ -82,7 +89,9 @@ describe("@babel/code-frame", function () {
"\t \t\t constructor\t(\t)",
"\t};",
].join("\n");
expect(codeFrame(rawLines, 2, 25)).toEqual(
expect(
codeFrameColumns(rawLines, { start: { line: 2, column: 25 } }),
).toEqual(
[
" 1 | \tclass Foo {",
"> 2 | \t \t\t constructor\t(\t)",
@@ -106,7 +115,13 @@ describe("@babel/code-frame", function () {
" return a + b",
"}",
].join("\n");
expect(codeFrame(rawLines, 7, 2, { linesAbove: 1 })).toEqual(
expect(
codeFrameColumns(
rawLines,
{ start: { line: 7, column: 2 } },
{ linesAbove: 1 },
),
).toEqual(
[
" 6 | * @returns Number",
"> 7 | */",
@@ -132,7 +147,13 @@ describe("@babel/code-frame", function () {
" return a + b",
"}",
].join("\n");
expect(codeFrame(rawLines, 7, 2, { linesBelow: 1 })).toEqual(
expect(
codeFrameColumns(
rawLines,
{ start: { line: 7, column: 2 } },
{ linesBelow: 1 },
),
).toEqual(
[
" 5 | * @param b Number",
" 6 | * @returns Number",
@@ -157,7 +178,13 @@ describe("@babel/code-frame", function () {
" return a + b",
"}",
].join("\n");
expect(codeFrame(rawLines, 7, 2, { linesAbove: 1, linesBelow: 1 })).toEqual(
expect(
codeFrameColumns(
rawLines,
{ start: { line: 7, column: 2 } },
{ linesAbove: 1, linesBelow: 1 },
),
).toEqual(
[" 6 | * @returns Number", "> 7 | */", " | ^", " 8 |"].join("\n"),
);
});