From c1499c598d7c55c6d16fdfb40c0aa6d585adef79 Mon Sep 17 00:00:00 2001 From: Henry Zhu Date: Wed, 13 Jan 2016 10:06:54 -0500 Subject: [PATCH 1/4] babel-code-frame: add options for linesBefore, linesAfter --- packages/babel-code-frame/src/index.js | 7 ++- packages/babel-code-frame/test/index.js | 81 +++++++++++++++++++++++-- 2 files changed, 80 insertions(+), 8 deletions(-) diff --git a/packages/babel-code-frame/src/index.js b/packages/babel-code-frame/src/index.js index 44dfd1f91541..f1a70263b7fd 100644 --- a/packages/babel-code-frame/src/index.js +++ b/packages/babel-code-frame/src/index.js @@ -83,9 +83,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; diff --git a/packages/babel-code-frame/test/index.js b/packages/babel-code-frame/test/index.js index 910c1aad5a7c..59eef365e920 100644 --- a/packages/babel-code-frame/test/index.js +++ b/packages/babel-code-frame/test/index.js @@ -8,13 +8,13 @@ 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 () { @@ -22,7 +22,7 @@ suite("babel-code-frame", function () { "class Foo {", " constructor()", "};", - ].join('\n'); + ].join("\n"); assert.equal(codeFrame(rawLines, 2, null), [ " 1 | class Foo {", "> 2 | constructor()", @@ -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")); }); }); From 27711d19f4faa5bba9e24f359d52f652df10198a Mon Sep 17 00:00:00 2001 From: Henry Zhu Date: Sat, 24 Sep 2016 15:52:06 -0400 Subject: [PATCH 2/4] add example, use list of keywords --- packages/babel-code-frame/README.md | 2 + packages/babel-code-frame/package.json | 1 - packages/babel-code-frame/src/index.js | 70 +++++++++++++++++++++++++- 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/packages/babel-code-frame/README.md b/packages/babel-code-frame/README.md index ca6f5c16646d..7e61e3ea5b97 100644 --- a/packages/babel-code-frame/README.md +++ b/packages/babel-code-frame/README.md @@ -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 diff --git a/packages/babel-code-frame/package.json b/packages/babel-code-frame/package.json index 9c6de7ae0743..72d37bdb2a30 100644 --- a/packages/babel-code-frame/package.json +++ b/packages/babel-code-frame/package.json @@ -9,7 +9,6 @@ "main": "lib/index.js", "dependencies": { "chalk": "^1.1.0", - "esutils": "^2.0.2", "js-tokens": "^2.0.0" } } diff --git a/packages/babel-code-frame/src/index.js b/packages/babel-code-frame/src/index.js index f1a70263b7fd..3ea4f16bab24 100644 --- a/packages/babel-code-frame/src/index.js +++ b/packages/babel-code-frame/src/index.js @@ -1,5 +1,4 @@ import jsTokens from "js-tokens"; -import esutils from "esutils"; import chalk from "chalk"; /** @@ -24,6 +23,73 @@ 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 +}; + +function isKeyword(str) { + return Object.prototype.hasOwnProperty.call(KEYWORDS, str); +} /** * Get the type of token, specifying punctuator type. @@ -31,7 +97,7 @@ const NEWLINE = /\r\n|[\n\r\u2028\u2029]/; 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"; } From 75a4275693799d589927d75f27a8200098231889 Mon Sep 17 00:00:00 2001 From: Henry Zhu Date: Mon, 26 Sep 2016 11:51:52 -0400 Subject: [PATCH 3/4] a [skip ci] --- packages/babel-code-frame/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/babel-code-frame/package.json b/packages/babel-code-frame/package.json index 72d37bdb2a30..9c6de7ae0743 100644 --- a/packages/babel-code-frame/package.json +++ b/packages/babel-code-frame/package.json @@ -9,6 +9,7 @@ "main": "lib/index.js", "dependencies": { "chalk": "^1.1.0", + "esutils": "^2.0.2", "js-tokens": "^2.0.0" } } From 823f4b935879eb17372a213b038c0dd68022d43f Mon Sep 17 00:00:00 2001 From: Henry Zhu Date: Mon, 26 Sep 2016 11:52:27 -0400 Subject: [PATCH 4/4] Update index.js --- packages/babel-code-frame/src/index.js | 70 +------------------------- 1 file changed, 2 insertions(+), 68 deletions(-) diff --git a/packages/babel-code-frame/src/index.js b/packages/babel-code-frame/src/index.js index 3ea4f16bab24..f1a70263b7fd 100644 --- a/packages/babel-code-frame/src/index.js +++ b/packages/babel-code-frame/src/index.js @@ -1,4 +1,5 @@ import jsTokens from "js-tokens"; +import esutils from "esutils"; import chalk from "chalk"; /** @@ -23,73 +24,6 @@ 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 -}; - -function isKeyword(str) { - return Object.prototype.hasOwnProperty.call(KEYWORDS, str); -} /** * Get the type of token, specifying punctuator type. @@ -97,7 +31,7 @@ function isKeyword(str) { function getTokenType(match) { let token = jsTokens.matchToToken(match); - if (token.type === "name" && isKeyword(token.value)) { + if (token.type === "name" && esutils.keyword.isReservedWordES6(token.value)) { return "keyword"; }