Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

[enhancement] Update babel-code-frame to @babel/code-frame #4632

Merged
merged 1 commit into from
Apr 3, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"coverage": "rimraf coverage .nyc_output && nyc npm test"
},
"dependencies": {
"babel-code-frame": "^6.22.0",
"@babel/code-frame": "^7.0.0",
"builtin-modules": "^1.1.1",
"chalk": "^2.3.0",
"commander": "^2.12.1",
Expand All @@ -47,7 +47,7 @@
"typescript": ">=2.1.0 || >=2.1.0-dev || >=2.2.0-dev || >=2.3.0-dev || >=2.4.0-dev || >=2.5.0-dev || >=2.6.0-dev || >=2.7.0-dev || >=2.8.0-dev || >=2.9.0-dev || >=3.0.0-dev || >= 3.1.0-dev || >= 3.2.0-dev"
},
"devDependencies": {
"@types/babel-code-frame": "^6.20.0",
"@types/babel__code-frame": "^7.0.1",
"@types/chai": "^3.5.0",
"@types/diff": "^3.2.0",
"@types/glob": "^5.0.30",
Expand Down
9 changes: 4 additions & 5 deletions src/formatters/codeFrameFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import codeFrame = require("babel-code-frame");
import { codeFrameColumns } from "@babel/code-frame";
import chalk from "chalk";

import { AbstractFormatter } from "../language/formatter/abstractFormatter";
Expand Down Expand Up @@ -76,11 +76,10 @@ export class Formatter extends AbstractFormatter {
ruleName = chalk.gray(`(${ruleName})`);

// Frame
const lineAndCharacter = failure.getStartPosition().getLineAndCharacter();
const frame = codeFrame(
const { character: column, line } = failure.getStartPosition().getLineAndCharacter();
const frame = codeFrameColumns(
failure.getRawLines(),
lineAndCharacter.line + 1, // babel-code-frame is 1 index
lineAndCharacter.character,
{ start: { line: line + 1, column } }, // babel-code-frame is 1 index
{
forceColor: chalk.enabled,
highlightCode: true,
Expand Down
53 changes: 27 additions & 26 deletions test/formatters/codeFrameFormatterTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe("CodeFrame Formatter", () => {
let sourceFile: ts.SourceFile;
let formatter: IFormatter;
let colorsEnabled: boolean;

before(() => {
colorsEnabled = chalk.enabled;
const Formatter = TestUtils.getFormatter("codeFrame");
Expand All @@ -35,11 +36,11 @@ describe("CodeFrame Formatter", () => {
});

after(() => {
(chalk as any).enabled = colorsEnabled;
chalk.enabled = colorsEnabled;
});

it("formats failures with colors", () => {
(chalk as any).enabled = true;
chalk.enabled = true;
const maxPosition = sourceFile.getFullWidth();

const failures = [
Expand Down Expand Up @@ -84,36 +85,36 @@ describe("CodeFrame Formatter", () => {

const expectedResultColored = `formatters/codeFrameFormatter.test.ts
\u001b[31mfirst failure\u001b[39m \u001b[90m(first-name)\u001b[39m
\u001b[0m\u001b[31m\u001b[1m>\u001b[22m\u001b[39m\u001b[90m 1 | \u001b[39mmodule \u001b[33mCodeFrameModule\u001b[39m {
\u001b[90m 2 | \u001b[39m \u001b[36mexport\u001b[39m \u001b[36mclass\u001b[39m \u001b[33mCodeFrameClass\u001b[39m {
\u001b[90m 3 | \u001b[39m private name\u001b[33m:\u001b[39m string\u001b[33m;\u001b[39m
\u001b[90m 4 | \u001b[39m\u001b[0m
\u001b[0m\u001b[31m\u001b[1m>\u001b[22m\u001b[39m\u001b[90m 1 | \u001b[39mmodule \u001b[33mCodeFrameModule\u001b[39m {\u001b[0m
\u001b[0m \u001b[90m 2 | \u001b[39m \u001b[36mexport\u001b[39m \u001b[36mclass\u001b[39m \u001b[33mCodeFrameClass\u001b[39m {\u001b[0m
\u001b[0m \u001b[90m 3 | \u001b[39m private name\u001b[33m:\u001b[39m string\u001b[33m;\u001b[39m\u001b[0m
\u001b[0m \u001b[90m 4 | \u001b[39m\u001b[0m

\u001b[31mfull failure\u001b[39m \u001b[90m(full-name)\u001b[39m
\u001b[0m\u001b[31m\u001b[1m>\u001b[22m\u001b[39m\u001b[90m 1 | \u001b[39mmodule \u001b[33mCodeFrameModule\u001b[39m {
\u001b[90m 2 | \u001b[39m \u001b[36mexport\u001b[39m \u001b[36mclass\u001b[39m \u001b[33mCodeFrameClass\u001b[39m {
\u001b[90m 3 | \u001b[39m private name\u001b[33m:\u001b[39m string\u001b[33m;\u001b[39m
\u001b[90m 4 | \u001b[39m\u001b[0m
\u001b[0m\u001b[31m\u001b[1m>\u001b[22m\u001b[39m\u001b[90m 1 | \u001b[39mmodule \u001b[33mCodeFrameModule\u001b[39m {\u001b[0m
\u001b[0m \u001b[90m 2 | \u001b[39m \u001b[36mexport\u001b[39m \u001b[36mclass\u001b[39m \u001b[33mCodeFrameClass\u001b[39m {\u001b[0m
\u001b[0m \u001b[90m 3 | \u001b[39m private name\u001b[33m:\u001b[39m string\u001b[33m;\u001b[39m\u001b[0m
\u001b[0m \u001b[90m 4 | \u001b[39m\u001b[0m

\u001b[33mwarning failure\u001b[39m \u001b[90m(warning-name)\u001b[39m
\u001b[0m\u001b[31m\u001b[1m>\u001b[22m\u001b[39m\u001b[90m 1 | \u001b[39mmodule \u001b[33mCodeFrameModule\u001b[39m {
\u001b[90m 2 | \u001b[39m \u001b[36mexport\u001b[39m \u001b[36mclass\u001b[39m \u001b[33mCodeFrameClass\u001b[39m {
\u001b[90m 3 | \u001b[39m private name\u001b[33m:\u001b[39m string\u001b[33m;\u001b[39m
\u001b[90m 4 | \u001b[39m\u001b[0m

\u001b[31m&<>'\" should be escaped\u001b[39m \u001b[90m(escape)\u001b[39m
\u001b[0m\u001b[31m\u001b[1m>\u001b[22m\u001b[39m\u001b[90m 1 | \u001b[39mmodule \u001b[33mCodeFrameModule\u001b[39m {
\u001b[90m | \u001b[39m \u001b[31m\u001b[1m^\u001b[22m\u001b[39m
\u001b[90m 2 | \u001b[39m \u001b[36mexport\u001b[39m \u001b[36mclass\u001b[39m \u001b[33mCodeFrameClass\u001b[39m {
\u001b[90m 3 | \u001b[39m private name\u001b[33m:\u001b[39m string\u001b[33m;\u001b[39m
\u001b[90m 4 | \u001b[39m\u001b[0m
\u001b[0m\u001b[31m\u001b[1m>\u001b[22m\u001b[39m\u001b[90m 1 | \u001b[39mmodule \u001b[33mCodeFrameModule\u001b[39m {\u001b[0m
\u001b[0m \u001b[90m 2 | \u001b[39m \u001b[36mexport\u001b[39m \u001b[36mclass\u001b[39m \u001b[33mCodeFrameClass\u001b[39m {\u001b[0m
\u001b[0m \u001b[90m 3 | \u001b[39m private name\u001b[33m:\u001b[39m string\u001b[33m;\u001b[39m\u001b[0m
\u001b[0m \u001b[90m 4 | \u001b[39m\u001b[0m

\u001b[31m&<>\'" should be escaped\u001b[39m \u001b[90m(escape)\u001b[39m
\u001b[0m\u001b[31m\u001b[1m>\u001b[22m\u001b[39m\u001b[90m 1 | \u001b[39mmodule \u001b[33mCodeFrameModule\u001b[39m {\u001b[0m
\u001b[0m \u001b[90m | \u001b[39m \u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[0m
\u001b[0m \u001b[90m 2 | \u001b[39m \u001b[36mexport\u001b[39m \u001b[36mclass\u001b[39m \u001b[33mCodeFrameClass\u001b[39m {\u001b[0m
\u001b[0m \u001b[90m 3 | \u001b[39m private name\u001b[33m:\u001b[39m string\u001b[33m;\u001b[39m\u001b[0m
\u001b[0m \u001b[90m 4 | \u001b[39m\u001b[0m

\u001b[31mlast failure\u001b[39m \u001b[90m(last-name)\u001b[39m
\u001b[0m \u001b[90m 7 | \u001b[39m }
\u001b[90m 8 | \u001b[39m }
\u001b[31m\u001b[1m>\u001b[22m\u001b[39m\u001b[90m 9 | \u001b[39m}
\u001b[90m | \u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m
\u001b[90m 10 | \u001b[39m\u001b[0m
\u001b[0m \u001b[90m 7 | \u001b[39m }\u001b[0m
\u001b[0m \u001b[90m 8 | \u001b[39m }\u001b[0m
\u001b[0m\u001b[31m\u001b[1m>\u001b[22m\u001b[39m\u001b[90m 9 | \u001b[39m}\u001b[0m
\u001b[0m \u001b[90m | \u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[0m
\u001b[0m \u001b[90m 10 | \u001b[39m\u001b[0m

`;

Expand Down