Run prettier
This commit is contained in:
@@ -90,11 +90,11 @@ function getTokenType(match) {
|
||||
*/
|
||||
|
||||
function highlight(defs: Object, text: string) {
|
||||
return text.replace(jsTokens, function (...args) {
|
||||
return text.replace(jsTokens, function(...args) {
|
||||
const type = getTokenType(args);
|
||||
const colorize = defs[type];
|
||||
if (colorize) {
|
||||
return args[0].split(NEWLINE).map((str) => colorize(str)).join("\n");
|
||||
return args[0].split(NEWLINE).map(str => colorize(str)).join("\n");
|
||||
} else {
|
||||
return args[0];
|
||||
}
|
||||
@@ -106,9 +106,15 @@ function highlight(defs: Object, text: string) {
|
||||
*/
|
||||
|
||||
function getMarkerLines(
|
||||
loc: NodeLocation, source: Array<string>, opts: Object
|
||||
loc: NodeLocation,
|
||||
source: Array<string>,
|
||||
opts: Object,
|
||||
): { start: number, end: number, markerLines: Object } {
|
||||
const startLoc: Location = Object.assign({}, { column: 0, line: -1 }, loc.start);
|
||||
const startLoc: Location = Object.assign(
|
||||
{},
|
||||
{ column: 0, line: -1 },
|
||||
loc.start,
|
||||
);
|
||||
const endLoc: Location = Object.assign({}, startLoc, loc.end);
|
||||
const linesAbove = opts.linesAbove || 2;
|
||||
const linesBelow = opts.linesBelow || 3;
|
||||
@@ -165,12 +171,13 @@ function getMarkerLines(
|
||||
return { start, end, markerLines };
|
||||
}
|
||||
|
||||
export function codeFrameColumns (
|
||||
export function codeFrameColumns(
|
||||
rawLines: string,
|
||||
loc: NodeLocation,
|
||||
opts: Object = {},
|
||||
): string {
|
||||
const highlighted = (opts.highlightCode && Chalk.supportsColor) || opts.forceColor;
|
||||
const highlighted =
|
||||
(opts.highlightCode && Chalk.supportsColor) || opts.forceColor;
|
||||
let chalk = Chalk;
|
||||
if (opts.forceColor) {
|
||||
chalk = new Chalk.constructor({ enabled: true });
|
||||
@@ -186,34 +193,39 @@ export function codeFrameColumns (
|
||||
|
||||
const numberMaxWidth = String(end).length;
|
||||
|
||||
const frame = lines.slice(start, end).map((line, index) => {
|
||||
const number = start + 1 + index;
|
||||
const paddedNumber = ` ${number}`.slice(-numberMaxWidth);
|
||||
const gutter = ` ${paddedNumber} | `;
|
||||
const hasMarker = markerLines[number];
|
||||
if (hasMarker) {
|
||||
let markerLine = "";
|
||||
if (Array.isArray(hasMarker)) {
|
||||
const markerSpacing = line.slice(0, Math.max(hasMarker[0] - 1, 0)).replace(/[^\t]/g, " ");
|
||||
const numberOfMarkers = hasMarker[1] || 1;
|
||||
const frame = lines
|
||||
.slice(start, end)
|
||||
.map((line, index) => {
|
||||
const number = start + 1 + index;
|
||||
const paddedNumber = ` ${number}`.slice(-numberMaxWidth);
|
||||
const gutter = ` ${paddedNumber} | `;
|
||||
const hasMarker = markerLines[number];
|
||||
if (hasMarker) {
|
||||
let markerLine = "";
|
||||
if (Array.isArray(hasMarker)) {
|
||||
const markerSpacing = line
|
||||
.slice(0, Math.max(hasMarker[0] - 1, 0))
|
||||
.replace(/[^\t]/g, " ");
|
||||
const numberOfMarkers = hasMarker[1] || 1;
|
||||
|
||||
markerLine = [
|
||||
"\n ",
|
||||
maybeHighlight(defs.gutter, gutter.replace(/\d/g, " ")),
|
||||
markerSpacing,
|
||||
maybeHighlight(defs.marker, "^").repeat(numberOfMarkers),
|
||||
markerLine = [
|
||||
"\n ",
|
||||
maybeHighlight(defs.gutter, gutter.replace(/\d/g, " ")),
|
||||
markerSpacing,
|
||||
maybeHighlight(defs.marker, "^").repeat(numberOfMarkers),
|
||||
].join("");
|
||||
}
|
||||
return [
|
||||
maybeHighlight(defs.marker, ">"),
|
||||
maybeHighlight(defs.gutter, gutter),
|
||||
line,
|
||||
markerLine,
|
||||
].join("");
|
||||
} else {
|
||||
return ` ${maybeHighlight(defs.gutter, gutter)}${line}`;
|
||||
}
|
||||
return [
|
||||
maybeHighlight(defs.marker, ">"),
|
||||
maybeHighlight(defs.gutter, gutter),
|
||||
line,
|
||||
markerLine,
|
||||
].join("");
|
||||
} else {
|
||||
return ` ${maybeHighlight(defs.gutter, gutter)}${line}`;
|
||||
}
|
||||
}).join("\n");
|
||||
})
|
||||
.join("\n");
|
||||
|
||||
if (highlighted) {
|
||||
return chalk.reset(frame);
|
||||
@@ -226,7 +238,7 @@ export function codeFrameColumns (
|
||||
* Create a code frame, adding line numbers, code highlighting, and pointing to a given position.
|
||||
*/
|
||||
|
||||
export default function (
|
||||
export default function(
|
||||
rawLines: string,
|
||||
lineNumber: number,
|
||||
colNumber: ?number,
|
||||
@@ -236,7 +248,7 @@ export default function (
|
||||
deprecationWarningShown = true;
|
||||
|
||||
const deprecationError = new Error(
|
||||
"Passing lineNumber and colNumber is deprecated to babel-code-frame. Please use `codeFrameColumns`."
|
||||
"Passing lineNumber and colNumber is deprecated to babel-code-frame. Please use `codeFrameColumns`.",
|
||||
);
|
||||
deprecationError.name = "DeprecationWarning";
|
||||
|
||||
@@ -249,7 +261,9 @@ export default function (
|
||||
|
||||
colNumber = Math.max(colNumber, 0);
|
||||
|
||||
const location: NodeLocation = { start: { column: colNumber, line: lineNumber } };
|
||||
const location: NodeLocation = {
|
||||
start: { column: colNumber, line: lineNumber },
|
||||
};
|
||||
|
||||
return codeFrameColumns(rawLines, location, opts);
|
||||
}
|
||||
|
||||
@@ -2,35 +2,29 @@ import assert from "assert";
|
||||
import chalk from "chalk";
|
||||
import codeFrame, { codeFrameColumns } from "..";
|
||||
|
||||
describe("babel-code-frame", function () {
|
||||
it("basic usage", function () {
|
||||
const rawLines = [
|
||||
"class Foo {",
|
||||
" constructor()",
|
||||
"};",
|
||||
].join("\n");
|
||||
assert.equal(codeFrame(rawLines, 2, 16), [
|
||||
" 1 | class Foo {",
|
||||
"> 2 | constructor()",
|
||||
" | ^",
|
||||
" 3 | };",
|
||||
].join("\n"));
|
||||
describe("babel-code-frame", function() {
|
||||
it("basic usage", function() {
|
||||
const rawLines = ["class Foo {", " constructor()", "};"].join("\n");
|
||||
assert.equal(
|
||||
codeFrame(rawLines, 2, 16),
|
||||
[
|
||||
" 1 | class Foo {",
|
||||
"> 2 | constructor()",
|
||||
" | ^",
|
||||
" 3 | };",
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("optional column number", function () {
|
||||
const rawLines = [
|
||||
"class Foo {",
|
||||
" constructor()",
|
||||
"};",
|
||||
].join("\n");
|
||||
assert.equal(codeFrame(rawLines, 2, null), [
|
||||
" 1 | class Foo {",
|
||||
"> 2 | constructor()",
|
||||
" 3 | };",
|
||||
].join("\n"));
|
||||
it("optional column number", function() {
|
||||
const rawLines = ["class Foo {", " constructor()", "};"].join("\n");
|
||||
assert.equal(
|
||||
codeFrame(rawLines, 2, null),
|
||||
[" 1 | class Foo {", "> 2 | constructor()", " 3 | };"].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("maximum context lines and padding", function () {
|
||||
it("maximum context lines and padding", function() {
|
||||
const rawLines = [
|
||||
"/**",
|
||||
" * Sums two numbers.",
|
||||
@@ -44,18 +38,21 @@ describe("babel-code-frame", function () {
|
||||
" return a + b",
|
||||
"}",
|
||||
].join("\n");
|
||||
assert.equal(codeFrame(rawLines, 7, 2), [
|
||||
" 5 | * @param b Number",
|
||||
" 6 | * @returns Number",
|
||||
"> 7 | */",
|
||||
" | ^",
|
||||
" 8 | ",
|
||||
" 9 | function sum(a, b) {",
|
||||
" 10 | return a + b",
|
||||
].join("\n"));
|
||||
assert.equal(
|
||||
codeFrame(rawLines, 7, 2),
|
||||
[
|
||||
" 5 | * @param b Number",
|
||||
" 6 | * @returns Number",
|
||||
"> 7 | */",
|
||||
" | ^",
|
||||
" 8 | ",
|
||||
" 9 | function sum(a, b) {",
|
||||
" 10 | return a + b",
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("no unnecessary padding due to one-off errors", function () {
|
||||
it("no unnecessary padding due to one-off errors", function() {
|
||||
const rawLines = [
|
||||
"/**",
|
||||
" * Sums two numbers.",
|
||||
@@ -69,43 +66,49 @@ describe("babel-code-frame", function () {
|
||||
" return a + b",
|
||||
"}",
|
||||
].join("\n");
|
||||
assert.equal(codeFrame(rawLines, 6, 2), [
|
||||
" 4 | * @param a Number",
|
||||
" 5 | * @param b Number",
|
||||
"> 6 | * @returns Number",
|
||||
" | ^",
|
||||
" 7 | */",
|
||||
" 8 | ",
|
||||
" 9 | function sum(a, b) {",
|
||||
].join("\n"));
|
||||
assert.equal(
|
||||
codeFrame(rawLines, 6, 2),
|
||||
[
|
||||
" 4 | * @param a Number",
|
||||
" 5 | * @param b Number",
|
||||
"> 6 | * @returns Number",
|
||||
" | ^",
|
||||
" 7 | */",
|
||||
" 8 | ",
|
||||
" 9 | function sum(a, b) {",
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("tabs", function () {
|
||||
it("tabs", function() {
|
||||
const rawLines = [
|
||||
"\tclass Foo {",
|
||||
"\t \t\t constructor\t(\t)",
|
||||
"\t};",
|
||||
].join("\n");
|
||||
assert.equal(codeFrame(rawLines, 2, 25), [
|
||||
" 1 | \tclass Foo {",
|
||||
"> 2 | \t \t\t constructor\t(\t)",
|
||||
" | \t \t\t \t \t ^",
|
||||
" 3 | \t};",
|
||||
].join("\n"));
|
||||
assert.equal(
|
||||
codeFrame(rawLines, 2, 25),
|
||||
[
|
||||
" 1 | \tclass Foo {",
|
||||
"> 2 | \t \t\t constructor\t(\t)",
|
||||
" | \t \t\t \t \t ^",
|
||||
" 3 | \t};",
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("opts.highlightCode", function () {
|
||||
it("opts.highlightCode", function() {
|
||||
const rawLines = "console.log('babel')";
|
||||
const result = codeFrame(rawLines, 1, 9, { highlightCode: true });
|
||||
const stripped = chalk.stripColor(result);
|
||||
assert.ok(result.length > stripped.length);
|
||||
assert.equal(stripped, [
|
||||
"> 1 | console.log('babel')",
|
||||
" | ^",
|
||||
].join("\n"));
|
||||
assert.equal(
|
||||
stripped,
|
||||
["> 1 | console.log('babel')", " | ^"].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("opts.linesAbove", function () {
|
||||
it("opts.linesAbove", function() {
|
||||
const rawLines = [
|
||||
"/**",
|
||||
" * Sums two numbers.",
|
||||
@@ -119,17 +122,20 @@ describe("babel-code-frame", function () {
|
||||
" return a + b",
|
||||
"}",
|
||||
].join("\n");
|
||||
assert.equal(codeFrame(rawLines, 7, 2, { linesAbove: 1 }), [
|
||||
" 6 | * @returns Number",
|
||||
"> 7 | */",
|
||||
" | ^",
|
||||
" 8 | ",
|
||||
" 9 | function sum(a, b) {",
|
||||
" 10 | return a + b",
|
||||
].join("\n"));
|
||||
assert.equal(
|
||||
codeFrame(rawLines, 7, 2, { linesAbove: 1 }),
|
||||
[
|
||||
" 6 | * @returns Number",
|
||||
"> 7 | */",
|
||||
" | ^",
|
||||
" 8 | ",
|
||||
" 9 | function sum(a, b) {",
|
||||
" 10 | return a + b",
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("opts.linesBelow", function () {
|
||||
it("opts.linesBelow", function() {
|
||||
const rawLines = [
|
||||
"/**",
|
||||
" * Sums two numbers.",
|
||||
@@ -143,16 +149,19 @@ describe("babel-code-frame", function () {
|
||||
" return a + b",
|
||||
"}",
|
||||
].join("\n");
|
||||
assert.equal(codeFrame(rawLines, 7, 2, { linesBelow: 1 }), [
|
||||
" 5 | * @param b Number",
|
||||
" 6 | * @returns Number",
|
||||
"> 7 | */",
|
||||
" | ^",
|
||||
" 8 | ",
|
||||
].join("\n"));
|
||||
assert.equal(
|
||||
codeFrame(rawLines, 7, 2, { linesBelow: 1 }),
|
||||
[
|
||||
" 5 | * @param b Number",
|
||||
" 6 | * @returns Number",
|
||||
"> 7 | */",
|
||||
" | ^",
|
||||
" 8 | ",
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("opts.linesAbove and opts.linesBelow", function () {
|
||||
it("opts.linesAbove and opts.linesBelow", function() {
|
||||
const rawLines = [
|
||||
"/**",
|
||||
" * Sums two numbers.",
|
||||
@@ -166,78 +175,82 @@ describe("babel-code-frame", function () {
|
||||
" return a + b",
|
||||
"}",
|
||||
].join("\n");
|
||||
assert.equal(codeFrame(rawLines, 7, 2, { linesAbove: 1, linesBelow: 1 }), [
|
||||
" 6 | * @returns Number",
|
||||
"> 7 | */",
|
||||
" | ^",
|
||||
" 8 | ",
|
||||
].join("\n"));
|
||||
assert.equal(
|
||||
codeFrame(rawLines, 7, 2, { linesAbove: 1, linesBelow: 1 }),
|
||||
[" 6 | * @returns Number", "> 7 | */", " | ^", " 8 | "].join(
|
||||
"\n",
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it("opts.forceColor", function() {
|
||||
const marker = chalk.red.bold;
|
||||
const gutter = chalk.grey;
|
||||
|
||||
const rawLines = [
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
].join("\n");
|
||||
assert.equal(codeFrame(rawLines, 3, null, { linesAbove: 1, linesBelow: 1, forceColor: true }),
|
||||
chalk.reset([
|
||||
" " + gutter(" 2 | "),
|
||||
marker(">") + gutter(" 3 | "),
|
||||
" " + gutter(" 4 | "),
|
||||
].join("\n"))
|
||||
const rawLines = ["", "", "", ""].join("\n");
|
||||
assert.equal(
|
||||
codeFrame(rawLines, 3, null, {
|
||||
linesAbove: 1,
|
||||
linesBelow: 1,
|
||||
forceColor: true,
|
||||
}),
|
||||
chalk.reset(
|
||||
[
|
||||
" " + gutter(" 2 | "),
|
||||
marker(">") + gutter(" 3 | "),
|
||||
" " + gutter(" 4 | "),
|
||||
].join("\n"),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it("basic usage, new API", function () {
|
||||
const rawLines = [
|
||||
"class Foo {",
|
||||
" constructor()",
|
||||
"};",
|
||||
].join("\n");
|
||||
assert.equal(codeFrameColumns(rawLines, { start: { line: 2, column: 16 } }), [
|
||||
" 1 | class Foo {",
|
||||
"> 2 | constructor()",
|
||||
" | ^",
|
||||
" 3 | };",
|
||||
].join("\n"));
|
||||
it("basic usage, new API", function() {
|
||||
const rawLines = ["class Foo {", " constructor()", "};"].join("\n");
|
||||
assert.equal(
|
||||
codeFrameColumns(rawLines, { start: { line: 2, column: 16 } }),
|
||||
[
|
||||
" 1 | class Foo {",
|
||||
"> 2 | constructor()",
|
||||
" | ^",
|
||||
" 3 | };",
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("mark multiple columns", function() {
|
||||
const rawLines = [
|
||||
"class Foo {",
|
||||
" constructor()",
|
||||
"};",
|
||||
].join("\n");
|
||||
const rawLines = ["class Foo {", " constructor()", "};"].join("\n");
|
||||
assert.equal(
|
||||
codeFrameColumns(rawLines, { start: { line: 2, column: 3 }, end: { line: 2, column: 16 } }), [
|
||||
codeFrameColumns(rawLines, {
|
||||
start: { line: 2, column: 3 },
|
||||
end: { line: 2, column: 16 },
|
||||
}),
|
||||
[
|
||||
" 1 | class Foo {",
|
||||
"> 2 | constructor()",
|
||||
" | ^^^^^^^^^^^^^",
|
||||
" 3 | };",
|
||||
].join("\n"));
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("mark multiple columns across lines", function() {
|
||||
const rawLines = [
|
||||
"class Foo {",
|
||||
" constructor() {",
|
||||
" }",
|
||||
"};",
|
||||
].join("\n");
|
||||
const rawLines = ["class Foo {", " constructor() {", " }", "};"].join(
|
||||
"\n",
|
||||
);
|
||||
assert.equal(
|
||||
codeFrameColumns(rawLines, { start: { line: 2, column: 17 }, end: { line: 3, column: 3 } }), [
|
||||
codeFrameColumns(rawLines, {
|
||||
start: { line: 2, column: 17 },
|
||||
end: { line: 3, column: 3 },
|
||||
}),
|
||||
[
|
||||
" 1 | class Foo {",
|
||||
"> 2 | constructor() {",
|
||||
" | ^",
|
||||
"> 3 | }",
|
||||
" | ^^^",
|
||||
" 4 | };",
|
||||
].join("\n"));
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("mark multiple columns across multiple lines", function() {
|
||||
@@ -249,7 +262,11 @@ describe("babel-code-frame", function () {
|
||||
"};",
|
||||
].join("\n");
|
||||
assert.equal(
|
||||
codeFrameColumns(rawLines, { start: { line: 2, column: 17 }, end: { line: 4, column: 3 } }), [
|
||||
codeFrameColumns(rawLines, {
|
||||
start: { line: 2, column: 17 },
|
||||
end: { line: 4, column: 3 },
|
||||
}),
|
||||
[
|
||||
" 1 | class Foo {",
|
||||
"> 2 | constructor() {",
|
||||
" | ^",
|
||||
@@ -258,7 +275,8 @@ describe("babel-code-frame", function () {
|
||||
"> 4 | }",
|
||||
" | ^^^",
|
||||
" 5 | };",
|
||||
].join("\n"));
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
|
||||
it("mark across multiple lines without columns", function() {
|
||||
@@ -270,12 +288,14 @@ describe("babel-code-frame", function () {
|
||||
"};",
|
||||
].join("\n");
|
||||
assert.equal(
|
||||
codeFrameColumns(rawLines, { start: { line: 2 }, end: { line: 4 } }), [
|
||||
codeFrameColumns(rawLines, { start: { line: 2 }, end: { line: 4 } }),
|
||||
[
|
||||
" 1 | class Foo {",
|
||||
"> 2 | constructor() {",
|
||||
"> 3 | console.log(arguments);",
|
||||
"> 4 | }",
|
||||
" 5 | };",
|
||||
].join("\n"));
|
||||
].join("\n"),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user