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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ | |
"main": "lib/index.js", | ||
"dependencies": { | ||
"chalk": "^1.1.0", | ||
"esutils": "^2.0.2", | ||
"js-tokens": "^2.0.0" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import jsTokens from "js-tokens"; | ||
import esutils from "esutils"; | ||
import chalk from "chalk"; | ||
|
||
/** | ||
|
@@ -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 | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"; | ||
} | ||
|
||
|
@@ -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; | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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