Debian Codegraph @

packages/babel-highlight/src/index.ts

raw symbols ◀ ▶ SCIP
2 Debian patches touch this file
1 /// <reference path="../../../lib/third-party-libs.d.ts" />
2
•3 // @ts-ignore
4 import type { Token as JSToken, JSXToken } from "js-tokens"scip-typescript npm babel 7.20.15 lib/`third-party-libs.d.ts`/`"js-tokens"`/;
5 import jsTokens from "js-tokens"scip-typescript npm babel 7.20.15 lib/`third-party-libs.d.ts`/`"js-tokens"`/;
6
7 import {
8 isStrictReservedWordscip-typescript npm @babel/helper-validator-identifier 7.19.1 src/`keyword.ts`/isStrictReservedWord().,
9 isKeywordscip-typescript npm @babel/helper-validator-identifier 7.19.1 src/`keyword.ts`/isKeyword().,
10 } from "@babel/helper-validator-identifier"scip-typescript npm @babel/helper-validator-identifier 7.19.1 src/`index.ts`/;
11 import Chalk from "chalk";
12
13 type ChalkClassscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/ChalkClass# = ReturnTypescip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/ReturnType#<typeof getChalkscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/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 sometimesKeywordsscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/sometimesKeywords. = new Setscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/Set#(["as", "async", "from", "get", "of", "set"]);
25
26 type InternalTokenTypescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/InternalTokenType# =
27 | "keyword"
28 | "capitalized"
29 | "jsxIdentifier"
30 | "punctuator"
31 | "number"
32 | "string"
33 | "regex"
34 | "comment"
35 | "invalid";
36
37 type Tokenscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Token# = {
38 typescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Token#typeLiteral4:type.: InternalTokenTypescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/InternalTokenType# | "uncolored";
39 valuescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Token#typeLiteral4:value.: string;
40 };
41 /**
42 * Chalk styles for token types.
43 */
44 function getDefsscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/getDefs().(chalkscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/getDefs().(chalk): ChalkClassscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/ChalkClass#): Recordscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Record#<InternalTokenTypescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/InternalTokenType#, ChalkClassscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/ChalkClass#> {
45 return {
46 keywordscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/keyword0:: chalkscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/getDefs().(chalk).cyan,
47 capitalizedscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/capitalized0:: chalkscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/getDefs().(chalk).yellow,
48 jsxIdentifierscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/jsxIdentifier0:: chalkscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/getDefs().(chalk).yellow,
49 punctuatorscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/punctuator0:: chalkscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/getDefs().(chalk).yellow,
50 numberscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/number0:: chalkscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/getDefs().(chalk).magenta,
51 stringscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/string0:: chalkscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/getDefs().(chalk).green,
52 regexscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/regex0:: chalkscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/getDefs().(chalk).magenta,
53 commentscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/comment0:: chalkscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/getDefs().(chalk).grey,
54 invalidscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/invalid0:: chalkscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/getDefs().(chalk).white.bgRed.bold,
55 };
56 }
57
58 /**
59 * RegExp to test for newlines in terminal.
60 */
61 const NEWLINEscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/NEWLINE. = /\r\n|[\n\r\u2028\u2029]/;
62
63 /**
64 * RegExp to test for the three types of brackets.
65 */
66 const BRACKETscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/BRACKET. = /^[()[\]{}]$/;
67
68 let tokenizescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/tokenize.: (
69 text: string,
70 ) => Generatorscip-typescript npm typescript 5.9.3 lib/`lib.es2015.generator.d.ts`/Generator#<{ type: InternalTokenTypescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/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 ): InternalTokenTypescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/InternalTokenType# | "uncolored" {
79 if (token.type === "IdentifierName") {
80 if (
81 isKeywordscip-typescript npm @babel/helper-validator-identifier 7.19.1 src/`keyword.ts`/isKeyword().(token.value) ||
82 isStrictReservedWordscip-typescript npm @babel/helper-validator-identifier 7.19.1 src/`keyword.ts`/isStrictReservedWord().(token.value, true) ||
83 sometimesKeywordsscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/sometimesKeywords..hasscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/Set#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" && BRACKETscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/BRACKET..testscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/RegExp#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 tokenizescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/tokenize. = function* (text: string): Generatorscip-typescript npm typescript 5.9.3 lib/`lib.es2015.generator.d.ts`/Generator#<Tokenscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Token#> {
137 for (const token of jsTokens(text, { jsxscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/jsx0:: true })) {
138 switch (token.type) {
139 case "TemplateHead":
140 yield { typescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Token#typeLiteral4:type.: "string", valuescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Token#typeLiteral4:value.: token.value.slice(0, -2) };
141 yield { typescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Token#typeLiteral4:type.: "punctuator", valuescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Token#typeLiteral4:value.: "${" };
142 break;
143
144 case "TemplateMiddle":
145 yield { typescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Token#typeLiteral4:type.: "punctuator", valuescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Token#typeLiteral4:value.: "}" };
146 yield { typescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Token#typeLiteral4:type.: "string", valuescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Token#typeLiteral4:value.: token.value.slice(1, -2) };
147 yield { typescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Token#typeLiteral4:type.: "punctuator", valuescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Token#typeLiteral4:value.: "${" };
148 break;
149
150 case "TemplateTail":
151 yield { typescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Token#typeLiteral4:type.: "punctuator", valuescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Token#typeLiteral4:value.: "}" };
152 yield { typescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Token#typeLiteral4:type.: "string", valuescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Token#typeLiteral4:value.: token.value.slice(1) };
153 break;
154
155 default:
156 yield {
157 typescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Token#typeLiteral4:type.: getTokenType(token),
158 valuescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Token#typeLiteral4: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 isKeywordscip-typescript npm @babel/helper-validator-identifier 7.19.1 src/`keyword.ts`/isKeyword().(token.value) ||
175 isStrictReservedWordscip-typescript npm @babel/helper-validator-identifier 7.19.1 src/`keyword.ts`/isStrictReservedWord().(token.value, true) ||
176 sometimesKeywordsscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/sometimesKeywords..hasscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/Set#has().(token.value)
177 ) {
178 return "keyword";
179 }
180
181 if (
•182 JSX_TAG.testscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/RegExp#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" && BRACKETscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/BRACKET..testscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/RegExp#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 tokenizescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/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 highlightTokensscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/highlightTokens().(defsscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/highlightTokens().(defs): Recordscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Record#<string, ChalkClassscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/ChalkClass#>, textscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/highlightTokens().(text): string) {
220 let highlighted = "";
221
222 for (const { type, value } of tokenizescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/tokenize.(textscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/highlightTokens().(text))) {
223 const colorize = defsscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/highlightTokens().(defs)[type];
224 if (colorize) {
225 highlighted += value
226 .splitscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/String#split().(NEWLINEscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/NEWLINE.)
227 .mapscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#map().(str => colorize(str))
228 .joinscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#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 Optionsscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Options# = {
242 forceColorscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Options#typeLiteral57:forceColor.?: boolean;
243 };
244
245 /**
246 * Whether the code should be highlighted given the passed options.
247 */
248 export function shouldHighlightscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/shouldHighlight().(optionsscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/shouldHighlight().(options): Optionsscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Options#): boolean {
•249 // @ts-ignore
250 return !!Chalk.supportsColor || optionsscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/shouldHighlight().(options).forceColorscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Options#typeLiteral57:forceColor.;
251 }
252
253 /**
254 * The Chalk instance that should be used given the passed options.
255 */
256 export function getChalkscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/getChalk().(optionsscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/getChalk().(options): Optionsscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Options#) {
257 return optionsscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/getChalk().(options).forceColorscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Options#typeLiteral57:forceColor.
•258 // @ts-ignore
259 ? new Chalk.constructor({ enabledscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/enabled0:: true, levelscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/level0:: 1 })
260 : Chalk;
261 }
262
263 /**
264 * Highlight `code`.
265 */
266 export default function highlightscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/highlight().(codescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/highlight().(code): string, optionsscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/highlight().(options): Optionsscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/Options# = {}): string {
267 if (codescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/highlight().(code) !== "" && shouldHighlightscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/shouldHighlight().(optionsscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/highlight().(options))) {
268 const chalk = getChalkscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/getChalk().(optionsscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/highlight().(options));
269 const defs = getDefsscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/getDefs().(chalk);
270 return highlightTokensscip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/highlightTokens().(defs, codescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/highlight().(code));
271 } else {
272 return codescip-typescript npm @babel/highlight 7.18.6 src/`index.ts`/highlight().(code);
273 }
274 }