@babel/highlight
Advanced tools
Comparing version 7.10.4 to 7.12.13
@@ -10,3 +10,3 @@ "use strict"; | ||
var _jsTokens = _interopRequireWildcard(require("js-tokens")); | ||
var jsTokensNs = _interopRequireWildcard(require("js-tokens")); | ||
@@ -23,2 +23,4 @@ var _helperValidatorIdentifier = require("@babel/helper-validator-identifier"); | ||
const sometimesKeywords = new Set(["as", "async", "from", "get", "of", "set"]); | ||
function getDefs(chalk) { | ||
@@ -28,3 +30,3 @@ return { | ||
capitalized: chalk.yellow, | ||
jsx_tag: chalk.yellow, | ||
jsxIdentifier: chalk.yellow, | ||
punctuator: chalk.yellow, | ||
@@ -40,45 +42,66 @@ number: chalk.magenta, | ||
const NEWLINE = /\r\n|[\n\r\u2028\u2029]/; | ||
const JSX_TAG = /^[a-z][\w-]*$/i; | ||
const BRACKET = /^[()[\]{}]$/; | ||
let tokenize; | ||
{ | ||
const { | ||
matchToToken | ||
} = jsTokensNs; | ||
const JSX_TAG = /^[a-z][\w-]*$/i; | ||
function getTokenType(match) { | ||
const [offset, text] = match.slice(-2); | ||
const token = (0, _jsTokens.matchToToken)(match); | ||
const getTokenType = function (token, offset, text) { | ||
if (token.type === "name") { | ||
if ((0, _helperValidatorIdentifier.isKeyword)(token.value) || (0, _helperValidatorIdentifier.isStrictReservedWord)(token.value, true) || sometimesKeywords.has(token.value)) { | ||
return "keyword"; | ||
} | ||
if (token.type === "name") { | ||
if ((0, _helperValidatorIdentifier.isKeyword)(token.value) || (0, _helperValidatorIdentifier.isReservedWord)(token.value)) { | ||
return "keyword"; | ||
if (JSX_TAG.test(token.value) && (text[offset - 1] === "<" || text.substr(offset - 2, 2) == "</")) { | ||
return "jsxIdentifier"; | ||
} | ||
if (token.value[0] !== token.value[0].toLowerCase()) { | ||
return "capitalized"; | ||
} | ||
} | ||
if (JSX_TAG.test(token.value) && (text[offset - 1] === "<" || text.substr(offset - 2, 2) == "</")) { | ||
return "jsx_tag"; | ||
if (token.type === "punctuator" && BRACKET.test(token.value)) { | ||
return "bracket"; | ||
} | ||
if (token.value[0] !== token.value[0].toLowerCase()) { | ||
return "capitalized"; | ||
if (token.type === "invalid" && (token.value === "@" || token.value === "#")) { | ||
return "punctuator"; | ||
} | ||
} | ||
if (token.type === "punctuator" && BRACKET.test(token.value)) { | ||
return "bracket"; | ||
} | ||
return token.type; | ||
}; | ||
if (token.type === "invalid" && (token.value === "@" || token.value === "#")) { | ||
return "punctuator"; | ||
} | ||
tokenize = function* (text) { | ||
let match; | ||
return token.type; | ||
while (match = jsTokensNs.default.exec(text)) { | ||
const token = matchToToken(match); | ||
yield { | ||
type: getTokenType(token, match.index, text), | ||
value: token.value | ||
}; | ||
} | ||
}; | ||
} | ||
function highlightTokens(defs, text) { | ||
return text.replace(_jsTokens.default, function (...args) { | ||
const type = getTokenType(args); | ||
let highlighted = ""; | ||
for (const { | ||
type, | ||
value | ||
} of tokenize(text)) { | ||
const colorize = defs[type]; | ||
if (colorize) { | ||
return args[0].split(NEWLINE).map(str => colorize(str)).join("\n"); | ||
highlighted += value.split(NEWLINE).map(str => colorize(str)).join("\n"); | ||
} else { | ||
return args[0]; | ||
highlighted += value; | ||
} | ||
}); | ||
} | ||
return highlighted; | ||
} | ||
@@ -85,0 +108,0 @@ |
{ | ||
"name": "@babel/highlight", | ||
"version": "7.10.4", | ||
"version": "7.12.13", | ||
"description": "Syntax highlight JavaScript strings for output in terminals.", | ||
"author": "suchipi <me@suchipi.com>", | ||
"homepage": "https://babeljs.io/", | ||
"homepage": "https://babel.dev/docs/en/next/babel-highlight", | ||
"license": "MIT", | ||
@@ -18,3 +18,3 @@ "publishConfig": { | ||
"dependencies": { | ||
"@babel/helper-validator-identifier": "^7.10.4", | ||
"@babel/helper-validator-identifier": "^7.12.11", | ||
"chalk": "^2.0.0", | ||
@@ -25,4 +25,3 @@ "js-tokens": "^4.0.0" | ||
"strip-ansi": "^4.0.0" | ||
}, | ||
"gitHead": "7fd40d86a0d03ff0e9c3ea16b29689945433d4df" | ||
} | ||
} | ||
} |
@@ -5,3 +5,3 @@ # @babel/highlight | ||
See our website [@babel/highlight](https://babeljs.io/docs/en/next/babel-highlight.html) for more information. | ||
See our website [@babel/highlight](https://babeljs.io/docs/en/babel-highlight) for more information. | ||
@@ -8,0 +8,0 @@ ## Install |
5967
104