diff --git a/lib/babel-packages.js.flow b/lib/babel-packages.js.flow
index 4a91be1750..fc29e08ae4 100644
--- a/lib/babel-packages.js.flow
+++ b/lib/babel-packages.js.flow
@@ -177,3 +177,25 @@ declare module "@babel/generator" {
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 };
+}
diff --git a/lib/third-party-libs.d.ts b/lib/third-party-libs.d.ts
new file mode 100644
index 0000000000..d76cd8d16b
--- /dev/null
+++ b/lib/third-party-libs.d.ts
@@ -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";
+}
diff --git a/packages/babel-highlight/package.json b/packages/babel-highlight/package.json
index bdcdeb89dc..3dc0288bb3 100644
--- a/packages/babel-highlight/package.json
+++ b/packages/babel-highlight/package.json
@@ -20,6 +20,7 @@
"js-tokens": "condition:BABEL_8_BREAKING ? ^6.0.0 : ^4.0.0"
},
"devDependencies": {
+ "@types/chalk": "^2.0.0",
"strip-ansi": "^4.0.0"
}
}
diff --git a/packages/babel-highlight/src/index.js b/packages/babel-highlight/src/index.ts
similarity index 85%
rename from packages/babel-highlight/src/index.js
rename to packages/babel-highlight/src/index.ts
index f2639a521c..6ef68de5fe 100644
--- a/packages/babel-highlight/src/index.js
+++ b/packages/babel-highlight/src/index.ts
@@ -1,10 +1,15 @@
+///
+
import jsTokens, * as jsTokensNs from "js-tokens";
+import type { Token, JSXToken } from "js-tokens";
import {
isStrictReservedWord,
isKeyword,
} from "@babel/helper-validator-identifier";
import Chalk from "chalk";
+type ChalkClass = ReturnType;
+
/**
* Names that are always allowed as identifiers, but also appear as keywords
* within certain syntactic productions.
@@ -16,10 +21,21 @@ import Chalk from "chalk";
*/
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.
*/
-function getDefs(chalk) {
+function getDefs(chalk: ChalkClass): Record {
return {
keyword: chalk.cyan,
capitalized: chalk.yellow,
@@ -43,13 +59,17 @@ const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
*/
const BRACKET = /^[()[\]{}]$/;
-let tokenize;
+let tokenize: (
+ text: string,
+) => Generator<{ type: InternalTokenType | "uncolored"; value: string }>;
if (process.env.BABEL_8_BREAKING) {
/**
* 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 (
isKeyword(token.value) ||
@@ -139,7 +159,7 @@ if (process.env.BABEL_8_BREAKING) {
};
} else {
// 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.
@@ -184,7 +204,7 @@ if (process.env.BABEL_8_BREAKING) {
tokenize = function* (text: string) {
let match;
- while ((match = jsTokens.exec(text))) {
+ while ((match = (jsTokens as any).exec(text))) {
const token = matchToToken(match);
yield {
@@ -198,7 +218,7 @@ if (process.env.BABEL_8_BREAKING) {
/**
* Highlight `text` using the token definitions in `defs`.
*/
-function highlightTokens(defs: Object, text: string) {
+function highlightTokens(defs: Record, text: string) {
let highlighted = "";
for (const { type, value } of tokenize(text)) {
@@ -221,25 +241,23 @@ function highlightTokens(defs: Object, text: string) {
*/
type Options = {
- forceColor?: boolean,
+ forceColor?: boolean;
};
/**
* Whether the code should be highlighted given the passed options.
*/
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.
*/
export function getChalk(options: Options) {
- let chalk = Chalk;
- if (options.forceColor) {
- chalk = new Chalk.constructor({ enabled: true, level: 1 });
- }
- return chalk;
+ return options.forceColor
+ ? new Chalk.constructor({ enabled: true, level: 1 })
+ : Chalk;
}
/**
diff --git a/yarn.lock b/yarn.lock
index 4147a21416..b69ded9bd2 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -914,6 +914,7 @@ __metadata:
resolution: "@babel/highlight@workspace:packages/babel-highlight"
dependencies:
"@babel/helper-validator-identifier": "workspace:^7.12.11"
+ "@types/chalk": ^2.0.0
chalk: ^2.0.0
js-tokens: "condition:BABEL_8_BREAKING ? ^6.0.0 : ^4.0.0"
strip-ansi: ^4.0.0