@babel/highlight
Advanced tools
Comparing version 7.22.5 to 8.0.0-alpha.0
111
lib/index.js
@@ -1,12 +0,5 @@ | ||
"use strict"; | ||
import jsTokens from 'js-tokens'; | ||
import { isKeyword, isStrictReservedWord } from '@babel/helper-validator-identifier'; | ||
import Chalk from 'chalk'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = highlight; | ||
exports.getChalk = getChalk; | ||
exports.shouldHighlight = shouldHighlight; | ||
var _jsTokens = require("js-tokens"); | ||
var _helperValidatorIdentifier = require("@babel/helper-validator-identifier"); | ||
var _chalk = require("chalk"); | ||
const sometimesKeywords = new Set(["as", "async", "from", "get", "of", "set"]); | ||
@@ -30,11 +23,7 @@ function getDefs(chalk) { | ||
{ | ||
const JSX_TAG = /^[a-z][\w-]*$/i; | ||
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)) { | ||
const getTokenType = function (token) { | ||
if (token.type === "IdentifierName") { | ||
if (isKeyword(token.value) || isStrictReservedWord(token.value, true) || sometimesKeywords.has(token.value)) { | ||
return "keyword"; | ||
} | ||
if (JSX_TAG.test(token.value) && (text[offset - 1] === "<" || text.slice(offset - 2, offset) == "</")) { | ||
return "jsxIdentifier"; | ||
} | ||
if (token.value[0] !== token.value[0].toLowerCase()) { | ||
@@ -44,18 +33,77 @@ return "capitalized"; | ||
} | ||
if (token.type === "punctuator" && BRACKET.test(token.value)) { | ||
return "bracket"; | ||
if (token.type === "Punctuator" && BRACKET.test(token.value)) { | ||
return "uncolored"; | ||
} | ||
if (token.type === "invalid" && (token.value === "@" || token.value === "#")) { | ||
if (token.type === "Invalid" && token.value === "@") { | ||
return "punctuator"; | ||
} | ||
return token.type; | ||
switch (token.type) { | ||
case "NumericLiteral": | ||
return "number"; | ||
case "StringLiteral": | ||
case "JSXString": | ||
case "NoSubstitutionTemplate": | ||
return "string"; | ||
case "RegularExpressionLiteral": | ||
return "regex"; | ||
case "Punctuator": | ||
case "JSXPunctuator": | ||
return "punctuator"; | ||
case "MultiLineComment": | ||
case "SingleLineComment": | ||
return "comment"; | ||
case "Invalid": | ||
case "JSXInvalid": | ||
return "invalid"; | ||
case "JSXIdentifier": | ||
return "jsxIdentifier"; | ||
default: | ||
return "uncolored"; | ||
} | ||
}; | ||
tokenize = function* (text) { | ||
let match; | ||
while (match = _jsTokens.default.exec(text)) { | ||
const token = _jsTokens.matchToToken(match); | ||
yield { | ||
type: getTokenType(token, match.index, text), | ||
value: token.value | ||
}; | ||
for (const token of jsTokens(text, { | ||
jsx: true | ||
})) { | ||
switch (token.type) { | ||
case "TemplateHead": | ||
yield { | ||
type: "string", | ||
value: token.value.slice(0, -2) | ||
}; | ||
yield { | ||
type: "punctuator", | ||
value: "${" | ||
}; | ||
break; | ||
case "TemplateMiddle": | ||
yield { | ||
type: "punctuator", | ||
value: "}" | ||
}; | ||
yield { | ||
type: "string", | ||
value: token.value.slice(1, -2) | ||
}; | ||
yield { | ||
type: "punctuator", | ||
value: "${" | ||
}; | ||
break; | ||
case "TemplateTail": | ||
yield { | ||
type: "punctuator", | ||
value: "}" | ||
}; | ||
yield { | ||
type: "string", | ||
value: token.value.slice(1) | ||
}; | ||
break; | ||
default: | ||
yield { | ||
type: getTokenType(token), | ||
value: token.value | ||
}; | ||
} | ||
} | ||
@@ -80,9 +128,9 @@ }; | ||
function shouldHighlight(options) { | ||
return !!_chalk.supportsColor || options.forceColor; | ||
return !!Chalk.supportsColor || options.forceColor; | ||
} | ||
function getChalk(options) { | ||
return options.forceColor ? new _chalk.constructor({ | ||
return options.forceColor ? new Chalk.constructor({ | ||
enabled: true, | ||
level: 1 | ||
}) : _chalk; | ||
}) : Chalk; | ||
} | ||
@@ -99,2 +147,3 @@ function highlight(code, options = {}) { | ||
export { highlight as default, getChalk, shouldHighlight }; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@babel/highlight", | ||
"version": "7.22.5", | ||
"version": "8.0.0-alpha.0", | ||
"description": "Syntax highlight JavaScript strings for output in terminals.", | ||
@@ -18,5 +18,5 @@ "author": "The Babel Team (https://babel.dev/team)", | ||
"dependencies": { | ||
"@babel/helper-validator-identifier": "^7.22.5", | ||
"@babel/helper-validator-identifier": "^8.0.0-alpha.0", | ||
"chalk": "^2.0.0", | ||
"js-tokens": "^4.0.0" | ||
"js-tokens": "^8.0.0" | ||
}, | ||
@@ -28,5 +28,9 @@ "devDependencies": { | ||
"engines": { | ||
"node": ">=6.9.0" | ||
"node": "^16.20.0 || ^18.16.0 || >=20.0.0" | ||
}, | ||
"type": "commonjs" | ||
"exports": { | ||
".": "./lib/index.js", | ||
"./package.json": "./package.json" | ||
}, | ||
"type": "module" | ||
} |
@@ -5,3 +5,3 @@ # @babel/highlight | ||
See our website [@babel/highlight](https://babeljs.io/docs/en/babel-highlight) for more information. | ||
See our website [@babel/highlight](https://babeljs.io/docs/babel-highlight) for more information. | ||
@@ -8,0 +8,0 @@ ## Install |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
17747
143
Yes
1
0
+ Added@babel/helper-validator-identifier@8.0.0-alpha.11(transitive)
+ Addedjs-tokens@8.0.3(transitive)
- Removed@babel/helper-validator-identifier@7.24.7(transitive)
- Removedjs-tokens@4.0.0(transitive)
Updatedjs-tokens@^8.0.0