Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code-frame/hightlight typings #14643

Merged
merged 3 commits into from Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions packages/babel-code-frame/src/index.ts
@@ -1,5 +1,7 @@
import highlight, { shouldHighlight, getChalk } from "@babel/highlight";

type Chalk = ReturnType<typeof getChalk>;

let deprecationWarningShown = false;

type Location = {
Expand Down Expand Up @@ -37,7 +39,7 @@ export interface Options {
/**
* Chalk styles for code frame token types.
*/
function getDefs(chalk) {
function getDefs(chalk: Chalk) {
return {
gutter: chalk.grey,
marker: chalk.red.bold,
Expand All @@ -55,14 +57,16 @@ const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
* Extract what lines should be marked and highlighted.
*/

type MarkerLines = Record<number, true | [number, number]>;

function getMarkerLines(
loc: NodeLocation,
source: Array<string>,
opts: Options,
): {
start: number;
end: number;
markerLines: any;
markerLines: MarkerLines;
} {
const startLoc: Location = {
column: 0,
Expand Down Expand Up @@ -91,7 +95,7 @@ function getMarkerLines(
}

const lineDiff = endLine - startLine;
const markerLines = {};
const markerLines: MarkerLines = {};

if (lineDiff) {
for (let i = 0; i <= lineDiff; i++) {
Expand Down Expand Up @@ -135,7 +139,7 @@ export function codeFrameColumns(
(opts.highlightCode || opts.forceColor) && shouldHighlight(opts);
const chalk = getChalk(opts);
const defs = getDefs(chalk);
const maybeHighlight = (chalkFn, string) => {
const maybeHighlight = (chalkFn: Chalk, string: string) => {
return highlighted ? chalkFn(string) : string;
};
const lines = rawLines.split(NEWLINE);
Expand Down
14 changes: 10 additions & 4 deletions packages/babel-highlight/src/index.ts
@@ -1,6 +1,6 @@
/// <reference path="../../../lib/third-party-libs.d.ts" />

import type { Token, JSXToken } from "js-tokens";
import type { Token as JSToken, JSXToken } from "js-tokens";
import jsTokens from "js-tokens";

import {
Expand Down Expand Up @@ -33,6 +33,10 @@ type InternalTokenType =
| "comment"
| "invalid";

type Token = {
type: InternalTokenType | "uncolored";
value: string;
};
/**
* Chalk styles for token types.
*/
Expand Down Expand Up @@ -69,7 +73,7 @@ if (process.env.BABEL_8_BREAKING) {
* Get the type of token, specifying punctuator type.
*/
const getTokenType = function (
token: Token | JSXToken,
token: JSToken | JSXToken,
): InternalTokenType | "uncolored" {
if (token.type === "IdentifierName") {
if (
Expand Down Expand Up @@ -128,7 +132,7 @@ if (process.env.BABEL_8_BREAKING) {
/**
* Turn a string of JS into an array of objects.
*/
tokenize = function* (text: string) {
tokenize = function* (text: string): Generator<Token> {
for (const token of jsTokens(text, { jsx: true })) {
switch (token.type) {
case "TemplateHead":
Expand Down Expand Up @@ -161,7 +165,9 @@ if (process.env.BABEL_8_BREAKING) {
*/
const JSX_TAG = /^[a-z][\w-]*$/i;

const getTokenType = function (token, offset, text) {
// The token here is defined in js-tokens@4. However we don't bother
// typing it since the whole block will be removed in Babel 8
const getTokenType = function (token: any, offset: number, text: string) {
if (token.type === "name") {
if (
isKeyword(token.value) ||
Expand Down
1 change: 1 addition & 0 deletions scripts/generators/tsconfig.js
Expand Up @@ -117,6 +117,7 @@ fs.writeFileSync(
["./lib/babel-plugin-dynamic-import-node.d.ts"],
],
["globals", ["./node_modules/globals-BABEL_8_BREAKING-true"]],
["js-tokens", ["./node_modules/js-tokens-BABEL_8_BREAKING-true"]],
["regexpu-core", ["./lib/regexpu-core.d.ts"]],
]),
},
Expand Down
3 changes: 3 additions & 0 deletions tsconfig.json
Expand Up @@ -702,6 +702,9 @@
"globals": [
"./node_modules/globals-BABEL_8_BREAKING-true"
],
"js-tokens": [
"./node_modules/js-tokens-BABEL_8_BREAKING-true"
],
"regexpu-core": [
"./lib/regexpu-core.d.ts"
]
Expand Down