diff --git a/packages/babel-code-frame/src/index.js b/packages/babel-code-frame/src/index.js index 44dfd1f91541..a74f18631ac1 100644 --- a/packages/babel-code-frame/src/index.js +++ b/packages/babel-code-frame/src/index.js @@ -7,16 +7,18 @@ import chalk from "chalk"; */ let defs = { - string: chalk.red, - punctuator: chalk.bold, - curly: chalk.green, - parens: chalk.blue.bold, - square: chalk.yellow, - keyword: chalk.cyan, - number: chalk.magenta, - regex: chalk.magenta, - comment: chalk.grey, - invalid: chalk.inverse + keyword: chalk.yellow.bold, + capitalized: chalk.yellow, + jsx_tag: chalk.yellow.dim, + punctuator: chalk.yellow.dim, + // bracket: intentionally omitted. + number: chalk.yellow, + string: chalk.yellow, + regex: chalk.yellow, + comment: chalk.grey, + invalid: chalk.white.bgRed.bold, + gutter: chalk.grey, + marker: chalk.red.bold, }; /** @@ -25,28 +27,45 @@ let defs = { const NEWLINE = /\r\n|[\n\r\u2028\u2029]/; +/** + * RegExp to test for what seems to be a JSX tag name. + */ + +const JSX_TAG = /^[a-z][\w-]*$/i; + +/** + * RegExp to test for the three types of brackets. + */ + +const BRACKET = /^[()\[\]{}]$/; + /** * Get the type of token, specifying punctuator type. */ function getTokenType(match) { + let [offset, text] = match.slice(-2); let token = jsTokens.matchToToken(match); - if (token.type === "name" && esutils.keyword.isReservedWordES6(token.value)) { - return "keyword"; - } - if (token.type === "punctuator") { - switch (token.value) { - case "{": - case "}": - return "curly"; - case "(": - case ")": - return "parens"; - case "[": - case "]": - return "square"; + if (token.type === "name") { + if (esutils.keyword.isReservedWordES6(token.value)) { + return "keyword"; + } + + if ( + JSX_TAG.test(token.value) && + (text[offset - 1] === "<" || text.substr(offset - 2, 2) == " { + return highlighted ? chalkFn(string) : string; + }; if (highlighted) rawLines = highlight(rawLines); let lines = rawLines.split(NEWLINE); @@ -102,11 +124,21 @@ export default function ( let markerLine = ""; if (colNumber) { let markerSpacing = line.slice(0, colNumber - 1).replace(/[^\t]/g, " "); - markerLine = `\n ${gutter.replace(/\d/g, " ")}${markerSpacing}^`; + markerLine = [ + "\n ", + maybeHighlight(defs.gutter, gutter.replace(/\d/g, " ")), + markerSpacing, + maybeHighlight(defs.marker, "^") + ].join(""); } - return `>${gutter}${line}${markerLine}`; + return [ + maybeHighlight(defs.marker, ">"), + maybeHighlight(defs.gutter, gutter), + line, + markerLine + ].join(""); } else { - return ` ${gutter}${line}`; + return ` ${maybeHighlight(defs.gutter, gutter)}${line}`; } }).join("\n");