| 1 |
/// <reference path="../../../lib/third-party-libs.d.ts" /> |
| 2 |
|
| •3 |
// @ts-ignore |
| 4 |
import type { Token as JSToken, JSXToken } from "js-tokens"; |
| 5 |
import jsTokens from "js-tokens"; |
| 6 |
|
| 7 |
import { |
| 8 |
isStrictReservedWord, |
| 9 |
isKeyword, |
| 10 |
} from "@babel/helper-validator-identifier"; |
| 11 |
import Chalk from "chalk"; |
| 12 |
|
| 13 |
type ChalkClass = ReturnType<typeof getChalk>; |
| 14 |
|
| 15 |
/** |
| 16 |
* Names that are always allowed as identifiers, but also appear as keywords |
| 17 |
* within certain syntactic productions. |
| 18 |
* |
| 19 |
* https://tc39.es/ecma262/#sec-keywords-and-reserved-words |
| 20 |
* |
| 21 |
* `target` has been omitted since it is very likely going to be a false |
| 22 |
* positive. |
| 23 |
*/ |
| 24 |
const sometimesKeywords = new Set(["as", "async", "from", "get", "of", "set"]); |
| 25 |
|
| 26 |
type InternalTokenType = |
| 27 |
| "keyword" |
| 28 |
| "capitalized" |
| 29 |
| "jsxIdentifier" |
| 30 |
| "punctuator" |
| 31 |
| "number" |
| 32 |
| "string" |
| 33 |
| "regex" |
| 34 |
| "comment" |
| 35 |
| "invalid"; |
| 36 |
|
| 37 |
type Token = { |
| 38 |
type: InternalTokenType | "uncolored"; |
| 39 |
value: string; |
| 40 |
}; |
| 41 |
/** |
| 42 |
* Chalk styles for token types. |
| 43 |
*/ |
| 44 |
function getDefs(chalk: ChalkClass): Record<InternalTokenType, ChalkClass> { |
| 45 |
return { |
| 46 |
keyword: chalk.cyan, |
| 47 |
capitalized: chalk.yellow, |
| 48 |
jsxIdentifier: chalk.yellow, |
| 49 |
punctuator: chalk.yellow, |
| 50 |
number: chalk.magenta, |
| 51 |
string: chalk.green, |
| 52 |
regex: chalk.magenta, |
| 53 |
comment: chalk.grey, |
| 54 |
invalid: chalk.white.bgRed.bold, |
| 55 |
}; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* RegExp to test for newlines in terminal. |
| 60 |
*/ |
| 61 |
const NEWLINE = /\r\n|[\n\r\u2028\u2029]/; |
| 62 |
|
| 63 |
/** |
| 64 |
* RegExp to test for the three types of brackets. |
| 65 |
*/ |
| 66 |
const BRACKET = /^[()[\]{}]$/; |
| 67 |
|
| 68 |
let tokenize: ( |
| 69 |
text: string, |
| 70 |
) => Generator<{ type: InternalTokenType | "uncolored"; value: string }>; |
| 71 |
|
| 72 |
if (process.env.BABEL_8_BREAKING) { |
| 73 |
/** |
| 74 |
* Get the type of token, specifying punctuator type. |
| 75 |
*/ |
| 76 |
const getTokenType = function ( |
| 77 |
token: JSToken | JSXToken, |
| 78 |
): InternalTokenType | "uncolored" { |
| 79 |
if (token.type === "IdentifierName") { |
| 80 |
if ( |
| 81 |
isKeyword(token.value) || |
| 82 |
isStrictReservedWord(token.value, true) || |
| 83 |
sometimesKeywords.has(token.value) |
| 84 |
) { |
| 85 |
return "keyword"; |
| 86 |
} |
| 87 |
|
| 88 |
if (token.value[0] !== token.value[0].toLowerCase()) { |
| 89 |
return "capitalized"; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
if (token.type === "Punctuator" && BRACKET.test(token.value)) { |
| 94 |
return "uncolored"; |
| 95 |
} |
| 96 |
|
| 97 |
if (token.type === "Invalid" && token.value === "@") { |
| 98 |
return "punctuator"; |
| 99 |
} |
| 100 |
|
| 101 |
switch (token.type) { |
| 102 |
case "NumericLiteral": |
| 103 |
return "number"; |
| 104 |
|
| 105 |
case "StringLiteral": |
| 106 |
case "JSXString": |
| 107 |
case "NoSubstitutionTemplate": |
| 108 |
return "string"; |
| 109 |
|
| 110 |
case "RegularExpressionLiteral": |
| 111 |
return "regex"; |
| 112 |
|
| 113 |
case "Punctuator": |
| 114 |
case "JSXPunctuator": |
| 115 |
return "punctuator"; |
| 116 |
|
| 117 |
case "MultiLineComment": |
| 118 |
case "SingleLineComment": |
| 119 |
return "comment"; |
| 120 |
|
| 121 |
case "Invalid": |
| 122 |
case "JSXInvalid": |
| 123 |
return "invalid"; |
| 124 |
|
| 125 |
case "JSXIdentifier": |
| 126 |
return "jsxIdentifier"; |
| 127 |
|
| 128 |
default: |
| 129 |
return "uncolored"; |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
/** |
| 134 |
* Turn a string of JS into an array of objects. |
| 135 |
*/ |
| 136 |
tokenize = function* (text: string): Generator<Token> { |
| 137 |
for (const token of jsTokens(text, { jsx: true })) { |
| 138 |
switch (token.type) { |
| 139 |
case "TemplateHead": |
| 140 |
yield { type: "string", value: token.value.slice(0, -2) }; |
| 141 |
yield { type: "punctuator", value: "${" }; |
| 142 |
break; |
| 143 |
|
| 144 |
case "TemplateMiddle": |
| 145 |
yield { type: "punctuator", value: "}" }; |
| 146 |
yield { type: "string", value: token.value.slice(1, -2) }; |
| 147 |
yield { type: "punctuator", value: "${" }; |
| 148 |
break; |
| 149 |
|
| 150 |
case "TemplateTail": |
| 151 |
yield { type: "punctuator", value: "}" }; |
| 152 |
yield { type: "string", value: token.value.slice(1) }; |
| 153 |
break; |
| 154 |
|
| 155 |
default: |
| 156 |
yield { |
| 157 |
type: getTokenType(token), |
| 158 |
value: token.value, |
| 159 |
}; |
| 160 |
} |
| 161 |
} |
| 162 |
}; |
| 163 |
} else { |
| 164 |
/** |
| 165 |
* RegExp to test for what seems to be a JSX tag name. |
| 166 |
*/ |
| 167 |
const JSX_TAG = /^[a-z][\w-]*$/i; |
| 168 |
|
| 169 |
// The token here is defined in js-tokens@4. However we don't bother |
| 170 |
// typing it since the whole block will be removed in Babel 8 |
| •171 |
const getTokenType = function (token: any) { |
| 172 |
if (token.type === "name") { |
| 173 |
if ( |
| 174 |
isKeyword(token.value) || |
| 175 |
isStrictReservedWord(token.value, true) || |
| 176 |
sometimesKeywords.has(token.value) |
| 177 |
) { |
| 178 |
return "keyword"; |
| 179 |
} |
| 180 |
|
| 181 |
if ( |
| •182 |
JSX_TAG.test(token.value) |
| 183 |
) { |
| 184 |
return "jsxIdentifier"; |
| 185 |
} |
| 186 |
|
| 187 |
if (token.value[0] !== token.value[0].toLowerCase()) { |
| 188 |
return "capitalized"; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
if (token.type === "punctuator" && BRACKET.test(token.value)) { |
| 193 |
return "bracket"; |
| 194 |
} |
| 195 |
|
| 196 |
if ( |
| 197 |
token.type === "invalid" && |
| 198 |
(token.value === "@" || token.value === "#") |
| 199 |
) { |
| 200 |
return "punctuator"; |
| 201 |
} |
| 202 |
|
| 203 |
return token.type; |
| 204 |
}; |
| 205 |
|
| 206 |
tokenize = function* (text: string) { |
| •207 |
for (const token of jsTokens(text)) { |
| 208 |
yield { |
| •209 |
type: getTokenType(token), |
| 210 |
value: token.value, |
| 211 |
}; |
| 212 |
} |
| 213 |
}; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Highlight `text` using the token definitions in `defs`. |
| 218 |
*/ |
| 219 |
function highlightTokens(defs: Record<string, ChalkClass>, text: string) { |
| 220 |
let highlighted = ""; |
| 221 |
|
| 222 |
for (const { type, value } of tokenize(text)) { |
| 223 |
const colorize = defs[type]; |
| 224 |
if (colorize) { |
| 225 |
highlighted += value |
| 226 |
.split(NEWLINE) |
| 227 |
.map(str => colorize(str)) |
| 228 |
.join("\n"); |
| 229 |
} else { |
| 230 |
highlighted += value; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
return highlighted; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Highlight `text` using the token definitions in `defs`. |
| 239 |
*/ |
| 240 |
|
| 241 |
type Options = { |
| 242 |
forceColor?: boolean; |
| 243 |
}; |
| 244 |
|
| 245 |
/** |
| 246 |
* Whether the code should be highlighted given the passed options. |
| 247 |
*/ |
| 248 |
export function shouldHighlight(options: Options): boolean { |
| •249 |
// @ts-ignore |
| 250 |
return !!Chalk.supportsColor || options.forceColor; |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* The Chalk instance that should be used given the passed options. |
| 255 |
*/ |
| 256 |
export function getChalk(options: Options) { |
| 257 |
return options.forceColor |
| •258 |
// @ts-ignore |
| 259 |
? new Chalk.constructor({ enabled: true, level: 1 }) |
| 260 |
: Chalk; |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Highlight `code`. |
| 265 |
*/ |
| 266 |
export default function highlight(code: string, options: Options = {}): string { |
| 267 |
if (code !== "" && shouldHighlight(options)) { |
| 268 |
const chalk = getChalk(options); |
| 269 |
const defs = getDefs(chalk); |
| 270 |
return highlightTokens(defs, code); |
| 271 |
} else { |
| 272 |
return code; |
| 273 |
} |
| 274 |
} |