babel-code-frame: add options for linesBefore, linesAfter (#4561)

* babel-code-frame: add options for linesBefore, linesAfter

* add example, use list of keywords

* a [skip ci]

* Update index.js
This commit is contained in:
Henry Zhu 2016-09-26 16:12:35 -04:00 committed by GitHub
parent 03d772c2ec
commit afbe3997a8
3 changed files with 82 additions and 8 deletions

View File

@ -38,3 +38,5 @@ If the column number is not known, you may pass `null` instead.
name | type | default | description name | type | default | description
-----------------------|----------|-----------------|------------------------------------------------------ -----------------------|----------|-----------------|------------------------------------------------------
highlightCode | boolean | `false` | Syntax highlight the code as JavaScript for terminals highlightCode | boolean | `false` | Syntax highlight the code as JavaScript for terminals
linesAbove | number | 2 | The number of lines to show above the error
linesBelow | number | 3 | The number of lines to show below the error

View File

@ -83,9 +83,12 @@ export default function (
let highlighted = opts.highlightCode && chalk.supportsColor; let highlighted = opts.highlightCode && chalk.supportsColor;
if (highlighted) rawLines = highlight(rawLines); if (highlighted) rawLines = highlight(rawLines);
let linesAbove = opts.linesAbove || 2;
let linesBelow = opts.linesBelow || 3;
let lines = rawLines.split(NEWLINE); let lines = rawLines.split(NEWLINE);
let start = Math.max(lineNumber - 3, 0); let start = Math.max(lineNumber - (linesAbove + 1), 0);
let end = Math.min(lines.length, lineNumber + 3); let end = Math.min(lines.length, lineNumber + linesBelow);
if (!lineNumber && !colNumber) { if (!lineNumber && !colNumber) {
start = 0; start = 0;

View File

@ -8,13 +8,13 @@ suite("babel-code-frame", function () {
"class Foo {", "class Foo {",
" constructor()", " constructor()",
"};", "};",
].join('\n'); ].join("\n");
assert.equal(codeFrame(rawLines, 2, 16), [ assert.equal(codeFrame(rawLines, 2, 16), [
" 1 | class Foo {", " 1 | class Foo {",
"> 2 | constructor()", "> 2 | constructor()",
" | ^", " | ^",
" 3 | };", " 3 | };",
].join('\n')); ].join("\n"));
}); });
test("optional column number", function () { test("optional column number", function () {
@ -22,7 +22,7 @@ suite("babel-code-frame", function () {
"class Foo {", "class Foo {",
" constructor()", " constructor()",
"};", "};",
].join('\n'); ].join("\n");
assert.equal(codeFrame(rawLines, 2, null), [ assert.equal(codeFrame(rawLines, 2, null), [
" 1 | class Foo {", " 1 | class Foo {",
"> 2 | constructor()", "> 2 | constructor()",
@ -104,17 +104,86 @@ suite("babel-code-frame", function () {
"> 2 | \t \t\t constructor\t(\t)", "> 2 | \t \t\t constructor\t(\t)",
" | \t \t\t \t \t ^", " | \t \t\t \t \t ^",
" 3 | \t};", " 3 | \t};",
].join('\n')); ].join("\n"));
}); });
test("opts.highlightCode", function () { test("opts.highlightCode", function () {
const rawLines = "console.log('babel')"; const rawLines = "console.log('babel')";
const result = codeFrame(rawLines, 1, 9, {highlightCode: true}) const result = codeFrame(rawLines, 1, 9, {highlightCode: true});
const stripped = chalk.stripColor(result); const stripped = chalk.stripColor(result);
assert.ok(result.length > stripped.length); assert.ok(result.length > stripped.length);
assert.equal(stripped, [ assert.equal(stripped, [
"> 1 | console.log('babel')", "> 1 | console.log('babel')",
" | ^", " | ^",
].join("\n")) ].join("\n"));
});
test("opts.linesAbove", function () {
var rawLines = [
"/**",
" * Sums two numbers.",
" *",
" * @param a Number",
" * @param b Number",
" * @returns Number",
" */",
"",
"function sum(a, b) {",
" 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"));
});
test("opts.linesBelow", function () {
var rawLines = [
"/**",
" * Sums two numbers.",
" *",
" * @param a Number",
" * @param b Number",
" * @returns Number",
" */",
"",
"function sum(a, b) {",
" return a + b",
"}"
].join("\n");
assert.equal(codeFrame(rawLines, 7, 2, { linesBelow: 1 }), [
" 5 | * @param b Number",
" 6 | * @returns Number",
"> 7 | */",
" | ^",
" 8 | "
].join("\n"));
});
test("opts.linesAbove and opts.linesBelow", function () {
var rawLines = [
"/**",
" * Sums two numbers.",
" *",
" * @param a Number",
" * @param b Number",
" * @returns Number",
" */",
"",
"function sum(a, b) {",
" return a + b",
"}"
].join("\n");
assert.equal(codeFrame(rawLines, 7, 2, { linesAbove: 1, linesBelow: 1 }), [
" 6 | * @returns Number",
"> 7 | */",
" | ^",
" 8 | "
].join("\n"));
}); });
}); });