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
Polish babel-code-frame highlight test #15499
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ad39824
resolve chalk from @babel/highlight's workspace
JLHwung eb1d106
improve color supports stub to work with Chalk 4
JLHwung 077af82
remove --color from test jobs
JLHwung 52dd862
create a valid SupportsColor Level
JLHwung a019ff5
also stub chalk.enabled
JLHwung 3f37e77
obtain chalk from getChalk
JLHwung 16ed156
jest e2e test requires --color
JLHwung File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,30 @@ | ||
import chalk from "chalk"; | ||
import stripAnsi from "strip-ansi"; | ||
|
||
import { getChalk } from "@babel/highlight"; | ||
import _codeFrame, { codeFrameColumns } from "../lib/index.js"; | ||
const codeFrame = _codeFrame.default || _codeFrame; | ||
|
||
const chalk = getChalk({}); | ||
|
||
describe("@babel/code-frame", function () { | ||
function stubColorSupport(supported) { | ||
let originalChalkLevel; | ||
let originalChalkSupportsColor; | ||
let originalChalkEnabled; | ||
beforeEach(function () { | ||
originalChalkSupportsColor = chalk.supportsColor; | ||
originalChalkLevel = chalk.level; | ||
originalChalkEnabled = chalk.enabled; | ||
chalk.supportsColor = supported ? { level: 1 } : false; | ||
chalk.level = supported ? 1 : 0; | ||
chalk.enabled = supported; | ||
}); | ||
|
||
afterEach(function () { | ||
chalk.supportsColor = originalChalkSupportsColor; | ||
chalk.level = originalChalkLevel; | ||
chalk.enabled = originalChalkEnabled; | ||
}); | ||
} | ||
test("basic usage", function () { | ||
const rawLines = ["class Foo {", " constructor()", "};"].join("\n"); | ||
expect(codeFrame(rawLines, 2, 16)).toEqual( | ||
|
@@ -94,53 +114,109 @@ describe("@babel/code-frame", function () { | |
); | ||
}); | ||
|
||
test("opts.highlightCode", function () { | ||
const rawLines = "console.log('babel')"; | ||
const result = codeFrame(rawLines, 1, 9, { highlightCode: true }); | ||
const stripped = stripAnsi(result); | ||
expect(result.length).toBeGreaterThan(stripped.length); | ||
expect(stripped).toEqual( | ||
["> 1 | console.log('babel')", " | ^"].join("\n"), | ||
); | ||
}); | ||
describe("when colors are supported", () => { | ||
stubColorSupport(true); | ||
|
||
test("opts.highlightCode with multiple columns and lines", function () { | ||
// prettier-ignore | ||
const rawLines = [ | ||
"function a(b, c) {", | ||
" return b + c;", | ||
"}" | ||
].join("\n"); | ||
test("opts.highlightCode", function () { | ||
const rawLines = "console.log('babel')"; | ||
const result = codeFrame(rawLines, 1, 9, { highlightCode: true }); | ||
const stripped = stripAnsi(result); | ||
expect(result.length).toBeGreaterThan(stripped.length); | ||
expect(stripped).toEqual( | ||
["> 1 | console.log('babel')", " | ^"].join("\n"), | ||
); | ||
}); | ||
|
||
const result = codeFrameColumns( | ||
rawLines, | ||
{ | ||
start: { | ||
line: 1, | ||
column: 1, | ||
test("opts.highlightCode with multiple columns and lines", function () { | ||
// prettier-ignore | ||
const rawLines = [ | ||
"function a(b, c) {", | ||
" return b + c;", | ||
"}" | ||
].join("\n"); | ||
|
||
const result = codeFrameColumns( | ||
rawLines, | ||
{ | ||
start: { | ||
line: 1, | ||
column: 1, | ||
}, | ||
end: { | ||
line: 3, | ||
column: 1, | ||
}, | ||
}, | ||
end: { | ||
line: 3, | ||
column: 1, | ||
{ | ||
highlightCode: true, | ||
message: "Message about things", | ||
}, | ||
}, | ||
{ | ||
highlightCode: true, | ||
message: "Message about things", | ||
}, | ||
); | ||
const stripped = stripAnsi(result); | ||
expect(stripped).toEqual( | ||
// prettier-ignore | ||
[ | ||
"> 1 | function a(b, c) {", | ||
" | ^^^^^^^^^^^^^^^^^^", | ||
"> 2 | return b + c;", | ||
" | ^^^^^^^^^^^^^^^", | ||
"> 3 | }", | ||
" | ^ Message about things", | ||
].join('\n'), | ||
); | ||
); | ||
const stripped = stripAnsi(result); | ||
expect(result.length).toBeGreaterThan(stripped.length); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a new assertion here to ensure that the The other tests are moved to the "when colors are supported" test context. |
||
expect(stripped).toEqual( | ||
// prettier-ignore | ||
[ | ||
"> 1 | function a(b, c) {", | ||
" | ^^^^^^^^^^^^^^^^^^", | ||
"> 2 | return b + c;", | ||
" | ^^^^^^^^^^^^^^^", | ||
"> 3 | }", | ||
" | ^ Message about things", | ||
].join('\n'), | ||
); | ||
}); | ||
test("opts.forceColor", function () { | ||
const marker = chalk.red.bold; | ||
const gutter = chalk.grey; | ||
|
||
const rawLines = ["", "", "", ""].join("\n"); | ||
expect( | ||
codeFrame(rawLines, 3, null, { | ||
linesAbove: 1, | ||
linesBelow: 1, | ||
forceColor: true, | ||
}), | ||
).toEqual( | ||
chalk.reset( | ||
[ | ||
" " + gutter(" 2 |"), | ||
marker(">") + gutter(" 3 |"), | ||
" " + gutter(" 4 |"), | ||
].join("\n"), | ||
), | ||
); | ||
}); | ||
|
||
test("jsx", function () { | ||
const gutter = chalk.grey; | ||
const yellow = chalk.yellow; | ||
|
||
const rawLines = ["<div />"].join("\n"); | ||
|
||
expect( | ||
JSON.stringify( | ||
codeFrame(rawLines, 0, null, { | ||
linesAbove: 1, | ||
linesBelow: 1, | ||
forceColor: true, | ||
}), | ||
), | ||
).toEqual( | ||
JSON.stringify( | ||
chalk.reset( | ||
" " + | ||
gutter(" 1 |") + | ||
" " + | ||
yellow("<") + | ||
yellow("div") + | ||
" " + | ||
yellow("/") + | ||
yellow(">"), | ||
), | ||
), | ||
); | ||
}); | ||
}); | ||
|
||
test("opts.linesAbove", function () { | ||
|
@@ -263,58 +339,6 @@ describe("@babel/code-frame", function () { | |
).toEqual(["> 2 | constructor() {"].join("\n")); | ||
}); | ||
|
||
test("opts.forceColor", function () { | ||
const marker = chalk.red.bold; | ||
const gutter = chalk.grey; | ||
|
||
const rawLines = ["", "", "", ""].join("\n"); | ||
expect( | ||
codeFrame(rawLines, 3, null, { | ||
linesAbove: 1, | ||
linesBelow: 1, | ||
forceColor: true, | ||
}), | ||
).toEqual( | ||
chalk.reset( | ||
[ | ||
" " + gutter(" 2 |"), | ||
marker(">") + gutter(" 3 |"), | ||
" " + gutter(" 4 |"), | ||
].join("\n"), | ||
), | ||
); | ||
}); | ||
|
||
test("jsx", function () { | ||
const gutter = chalk.grey; | ||
const yellow = chalk.yellow; | ||
|
||
const rawLines = ["<div />"].join("\n"); | ||
|
||
expect( | ||
JSON.stringify( | ||
codeFrame(rawLines, 0, null, { | ||
linesAbove: 1, | ||
linesBelow: 1, | ||
forceColor: true, | ||
}), | ||
), | ||
).toEqual( | ||
JSON.stringify( | ||
chalk.reset( | ||
" " + | ||
gutter(" 1 |") + | ||
" " + | ||
yellow("<") + | ||
yellow("div") + | ||
" " + | ||
yellow("/") + | ||
yellow(">"), | ||
), | ||
), | ||
); | ||
}); | ||
|
||
test("basic usage, new API", function () { | ||
const rawLines = ["class Foo {", " constructor()", "};"].join("\n"); | ||
expect( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
getChalk
private API is also used inbabel-code-frame
test here so that we can manipulate the chalk instance required bybabel-highlight
. If we remove thegetChalk
API, we may have to fallback to resolving it from babel-highlight.Previously we manipulated the
chalk
dev dependency. It happens to work when bothchalk
dependencies are hoisted to the top level n_m. It no longer works in #15505 when the top level is chalk v2 (from our own build deps) and libraries depends on chalk v4.