convert @babel/highlight to typescript (#12432)

Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
Bogdan Savluk 2021-02-25 01:38:47 +01:00 committed by GitHub
parent 4fa7a72874
commit 0a4dc05806
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 60 additions and 13 deletions

View File

@ -177,3 +177,25 @@ declare module "@babel/generator" {
declare module.exports: any; declare module.exports: any;
} }
declare module "@babel/highlight" {
import typeof { Chalk } from "chalk";
declare type Options = {
forceColor?: boolean,
};
/**
* Whether the code should be highlighted given the passed options.
*/
declare function shouldHighlight(options: Options): boolean;
/**
* The Chalk instance that should be used given the passed options.
*/
declare function getChalk(options: Options): Chalk;
/**
* Highlight `code`.
*/
declare export default function highlight(code: string, options?: Options): string;
declare export { getChalk, shouldHighlight };
}

5
lib/third-party-libs.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
declare module "js-tokens" {
// TODO(Babel 8): Remove this
export { default } from "js-tokens-BABEL_8_BREAKING-true";
export * from "js-tokens-BABEL_8_BREAKING-true";
}

View File

@ -20,6 +20,7 @@
"js-tokens": "condition:BABEL_8_BREAKING ? ^6.0.0 : ^4.0.0" "js-tokens": "condition:BABEL_8_BREAKING ? ^6.0.0 : ^4.0.0"
}, },
"devDependencies": { "devDependencies": {
"@types/chalk": "^2.0.0",
"strip-ansi": "^4.0.0" "strip-ansi": "^4.0.0"
} }
} }

View File

@ -1,10 +1,15 @@
/// <reference path="../../../lib/third-party-libs.d.ts" />
import jsTokens, * as jsTokensNs from "js-tokens"; import jsTokens, * as jsTokensNs from "js-tokens";
import type { Token, JSXToken } from "js-tokens";
import { import {
isStrictReservedWord, isStrictReservedWord,
isKeyword, isKeyword,
} from "@babel/helper-validator-identifier"; } from "@babel/helper-validator-identifier";
import Chalk from "chalk"; import Chalk from "chalk";
type ChalkClass = ReturnType<typeof getChalk>;
/** /**
* Names that are always allowed as identifiers, but also appear as keywords * Names that are always allowed as identifiers, but also appear as keywords
* within certain syntactic productions. * within certain syntactic productions.
@ -16,10 +21,21 @@ import Chalk from "chalk";
*/ */
const sometimesKeywords = new Set(["as", "async", "from", "get", "of", "set"]); const sometimesKeywords = new Set(["as", "async", "from", "get", "of", "set"]);
type InternalTokenType =
| "keyword"
| "capitalized"
| "jsxIdentifier"
| "punctuator"
| "number"
| "string"
| "regex"
| "comment"
| "invalid";
/** /**
* Chalk styles for token types. * Chalk styles for token types.
*/ */
function getDefs(chalk) { function getDefs(chalk: ChalkClass): Record<InternalTokenType, ChalkClass> {
return { return {
keyword: chalk.cyan, keyword: chalk.cyan,
capitalized: chalk.yellow, capitalized: chalk.yellow,
@ -43,13 +59,17 @@ const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
*/ */
const BRACKET = /^[()[\]{}]$/; const BRACKET = /^[()[\]{}]$/;
let tokenize; let tokenize: (
text: string,
) => Generator<{ type: InternalTokenType | "uncolored"; value: string }>;
if (process.env.BABEL_8_BREAKING) { if (process.env.BABEL_8_BREAKING) {
/** /**
* Get the type of token, specifying punctuator type. * Get the type of token, specifying punctuator type.
*/ */
const getTokenType = function (token) { const getTokenType = function (
token: Token | JSXToken,
): InternalTokenType | "uncolored" {
if (token.type === "IdentifierName") { if (token.type === "IdentifierName") {
if ( if (
isKeyword(token.value) || isKeyword(token.value) ||
@ -139,7 +159,7 @@ if (process.env.BABEL_8_BREAKING) {
}; };
} else { } else {
// This is only available in js-tokens@4, and not in js-tokens@6 // This is only available in js-tokens@4, and not in js-tokens@6
const { matchToToken } = jsTokensNs; const { matchToToken } = jsTokensNs as any;
/** /**
* RegExp to test for what seems to be a JSX tag name. * RegExp to test for what seems to be a JSX tag name.
@ -184,7 +204,7 @@ if (process.env.BABEL_8_BREAKING) {
tokenize = function* (text: string) { tokenize = function* (text: string) {
let match; let match;
while ((match = jsTokens.exec(text))) { while ((match = (jsTokens as any).exec(text))) {
const token = matchToToken(match); const token = matchToToken(match);
yield { yield {
@ -198,7 +218,7 @@ if (process.env.BABEL_8_BREAKING) {
/** /**
* Highlight `text` using the token definitions in `defs`. * Highlight `text` using the token definitions in `defs`.
*/ */
function highlightTokens(defs: Object, text: string) { function highlightTokens(defs: Record<string, ChalkClass>, text: string) {
let highlighted = ""; let highlighted = "";
for (const { type, value } of tokenize(text)) { for (const { type, value } of tokenize(text)) {
@ -221,25 +241,23 @@ function highlightTokens(defs: Object, text: string) {
*/ */
type Options = { type Options = {
forceColor?: boolean, forceColor?: boolean;
}; };
/** /**
* Whether the code should be highlighted given the passed options. * Whether the code should be highlighted given the passed options.
*/ */
export function shouldHighlight(options: Options): boolean { export function shouldHighlight(options: Options): boolean {
return Chalk.supportsColor || options.forceColor; return !!Chalk.supportsColor || options.forceColor;
} }
/** /**
* The Chalk instance that should be used given the passed options. * The Chalk instance that should be used given the passed options.
*/ */
export function getChalk(options: Options) { export function getChalk(options: Options) {
let chalk = Chalk; return options.forceColor
if (options.forceColor) { ? new Chalk.constructor({ enabled: true, level: 1 })
chalk = new Chalk.constructor({ enabled: true, level: 1 }); : Chalk;
}
return chalk;
} }
/** /**

View File

@ -914,6 +914,7 @@ __metadata:
resolution: "@babel/highlight@workspace:packages/babel-highlight" resolution: "@babel/highlight@workspace:packages/babel-highlight"
dependencies: dependencies:
"@babel/helper-validator-identifier": "workspace:^7.12.11" "@babel/helper-validator-identifier": "workspace:^7.12.11"
"@types/chalk": ^2.0.0
chalk: ^2.0.0 chalk: ^2.0.0
js-tokens: "condition:BABEL_8_BREAKING ? ^6.0.0 : ^4.0.0" js-tokens: "condition:BABEL_8_BREAKING ? ^6.0.0 : ^4.0.0"
strip-ansi: ^4.0.0 strip-ansi: ^4.0.0