Skip to content
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

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

Merged
merged 4 commits into from Sep 26, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/babel-code-frame/README.md
Expand Up @@ -38,3 +38,5 @@ If the column number is not known, you may pass `null` instead.
name | type | default | description
-----------------------|----------|-----------------|------------------------------------------------------
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
1 change: 0 additions & 1 deletion packages/babel-code-frame/package.json
Expand Up @@ -9,7 +9,6 @@
"main": "lib/index.js",
"dependencies": {
"chalk": "^1.1.0",
"esutils": "^2.0.2",
"js-tokens": "^2.0.0"
}
}
77 changes: 73 additions & 4 deletions packages/babel-code-frame/src/index.js
@@ -1,5 +1,4 @@
import jsTokens from "js-tokens";
import esutils from "esutils";
import chalk from "chalk";

/**
Expand All @@ -24,14 +23,81 @@ let defs = {
*/

const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
const KEYWORDS = {
"abstract": true,
"await": true,
"boolean": true,
"break": true,
"byte": true,
"case": true,
"catch": true,
"char": true,
"class": true,
"const": true,
"continue": true,
"debugger": true,
"default": true,
"delete": true,
"do": true,
"double": true,
"else": true,
"enum": true,
"export": true,
"extends": true,
"false": true,
"final": true,
"finally": true,
"float": true,
"for": true,
"function": true,
"goto": true,
"if": true,
"implements": true,
"import": true,
"in": true,
"instanceof": true,
"int": true,
"interface": true,
"let": true,
"long": true,
"native": true,
"new": true,
"null": true,
"package": true,
"private": true,
"protected": true,
"public": true,
"return": true,
"short": true,
"static": true,
"super": true,
"switch": true,
"synchronized": true,
"this": true,
"throw": true,
"transient": true,
"true": true,
"try": true,
"typeof": true,
"var": true,
"void": true,
"volatile": true,
"while": true,
"with": true,
"yield": true
};
Copy link
Member

@danez danez Sep 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we also use esutils in in other places to check for keywords. If we now want to manage keywords ourself should we maybe but them in a central place, so all packages can use them?

Copy link
Member Author

@hzoo hzoo Sep 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was going to remove the deps for this package so other tools can use easily, what do you think? Or maybe it's not that big of a deal and we should leave it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong opinion about using or not using esutils, but I think it would be nice to have a single source of truth for keywords throughout all the babel packages.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'l just leave esutils in for now and we can decide that later


function isKeyword(str) {
return Object.prototype.hasOwnProperty.call(KEYWORDS, str);
}

/**
* Get the type of token, specifying punctuator type.
*/

function getTokenType(match) {
let token = jsTokens.matchToToken(match);
if (token.type === "name" && esutils.keyword.isReservedWordES6(token.value)) {
if (token.type === "name" && isKeyword(token.value)) {
return "keyword";
}

Expand Down Expand Up @@ -83,9 +149,12 @@ export default function (
let highlighted = opts.highlightCode && chalk.supportsColor;
if (highlighted) rawLines = highlight(rawLines);

let linesAbove = opts.linesAbove || 2;
let linesBelow = opts.linesBelow || 3;

let lines = rawLines.split(NEWLINE);
let start = Math.max(lineNumber - 3, 0);
let end = Math.min(lines.length, lineNumber + 3);
let start = Math.max(lineNumber - (linesAbove + 1), 0);
let end = Math.min(lines.length, lineNumber + linesBelow);

if (!lineNumber && !colNumber) {
start = 0;
Expand Down
81 changes: 75 additions & 6 deletions packages/babel-code-frame/test/index.js
Expand Up @@ -8,21 +8,21 @@ suite("babel-code-frame", function () {
"class Foo {",
" constructor()",
"};",
].join('\n');
].join("\n");
assert.equal(codeFrame(rawLines, 2, 16), [
" 1 | class Foo {",
"> 2 | constructor()",
" | ^",
" 3 | };",
].join('\n'));
].join("\n"));
});

test("optional column number", function () {
const rawLines = [
"class Foo {",
" constructor()",
"};",
].join('\n');
].join("\n");
assert.equal(codeFrame(rawLines, 2, null), [
" 1 | class Foo {",
"> 2 | constructor()",
Expand Down Expand Up @@ -104,17 +104,86 @@ suite("babel-code-frame", function () {
"> 2 | \t \t\t constructor\t(\t)",
" | \t \t\t \t \t ^",
" 3 | \t};",
].join('\n'));
].join("\n"));
});

test("opts.highlightCode", function () {
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);
assert.ok(result.length > stripped.length);
assert.equal(stripped, [
"> 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"));
});
});