Skip to content

Commit

Permalink
[babel 8] Use chalk@4 (#15814)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Jul 27, 2023
1 parent 544b6e1 commit 1a62af1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/babel-code-frame/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"main": "./lib/index.js",
"dependencies": {
"@babel/highlight": "workspace:^",
"chalk": "^2.4.2"
"chalk": "condition:BABEL_8_BREAKING ? ^4.1.2 : ^2.4.2"
},
"devDependencies": {
"strip-ansi": "^4.0.0"
Expand Down
13 changes: 11 additions & 2 deletions packages/babel-code-frame/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import highlight, { shouldHighlight } from "@babel/highlight";
import chalk, { type Chalk } from "chalk";

import _chalk from "chalk";
const chalk = _chalk as unknown as typeof import("chalk-BABEL_8_BREAKING-true");
type Chalk =
typeof import("chalk-BABEL_8_BREAKING-true").Instance extends new () => infer R
? R
: never;

let chalkWithForcedColor: Chalk = undefined;
function getChalk(forceColor: boolean) {
if (forceColor) {
chalkWithForcedColor ??= new chalk.constructor({ enabled: true, level: 1 });
chalkWithForcedColor ??= process.env.BABEL_8_BREAKING
? new chalk.Instance({ level: 1 })
: // @ts-expect-error .Instance was .constructor in chalk 2
new chalk.constructor({ enabled: true, level: 1 });
return chalkWithForcedColor;
}
return chalk;
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-highlight/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"main": "./lib/index.js",
"dependencies": {
"@babel/helper-validator-identifier": "workspace:^",
"chalk": "^2.4.2",
"chalk": "condition:BABEL_8_BREAKING ? ^4.1.2 : ^2.4.2",
"js-tokens": "condition:BABEL_8_BREAKING ? ^8.0.0 : ^4.0.0"
},
"devDependencies": {
Expand Down
13 changes: 11 additions & 2 deletions packages/babel-highlight/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import {
isStrictReservedWord,
isKeyword,
} from "@babel/helper-validator-identifier";
import chalk, { type Chalk } from "chalk";

import _chalk from "chalk";
const chalk = _chalk as unknown as typeof import("chalk-BABEL_8_BREAKING-true");
type Chalk =
typeof import("chalk-BABEL_8_BREAKING-true").Instance extends new () => infer R
? R
: never;

/**
* Names that are always allowed as identifiers, but also appear as keywords
Expand Down Expand Up @@ -253,7 +259,10 @@ export function shouldHighlight(options: Options): boolean {
let chalkWithForcedColor: Chalk = undefined;
function getChalk(forceColor: boolean) {
if (forceColor) {
chalkWithForcedColor ??= new chalk.constructor({ enabled: true, level: 1 });
chalkWithForcedColor ??= process.env.BABEL_8_BREAKING
? new chalk.Instance({ level: 1 })
: // @ts-expect-error .Instance was .constructor in chalk 2
new chalk.constructor({ enabled: true, level: 1 });
return chalkWithForcedColor;
}
return chalk;
Expand Down
46 changes: 33 additions & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ __metadata:
resolution: "@babel/code-frame@workspace:packages/babel-code-frame"
dependencies:
"@babel/highlight": "workspace:^"
chalk: ^2.4.2
chalk: "condition:BABEL_8_BREAKING ? ^4.1.2 : ^2.4.2"
strip-ansi: ^4.0.0
languageName: unknown
linkType: soft
Expand Down Expand Up @@ -1073,7 +1073,7 @@ __metadata:
resolution: "@babel/highlight@workspace:packages/babel-highlight"
dependencies:
"@babel/helper-validator-identifier": "workspace:^"
chalk: ^2.4.2
chalk: "condition:BABEL_8_BREAKING ? ^4.1.2 : ^2.4.2"
js-tokens: "condition:BABEL_8_BREAKING ? ^8.0.0 : ^4.0.0"
strip-ansi: ^4.0.0
languageName: unknown
Expand Down Expand Up @@ -7083,6 +7083,37 @@ __metadata:
languageName: node
linkType: hard

"chalk-BABEL_8_BREAKING-false@npm:chalk@^2.4.2, chalk@npm:^2.0.0":
version: 2.4.2
resolution: "chalk@npm:2.4.2"
dependencies:
ansi-styles: ^3.2.1
escape-string-regexp: ^1.0.5
supports-color: ^5.3.0
checksum: ec3661d38fe77f681200f878edbd9448821924e0f93a9cefc0e26a33b145f1027a2084bf19967160d11e1f03bfe4eaffcabf5493b89098b2782c3fe0b03d80c2
languageName: node
linkType: hard

"chalk-BABEL_8_BREAKING-true@npm:chalk@^4.1.2":
version: 4.1.2
resolution: "chalk@npm:4.1.2"
dependencies:
ansi-styles: ^4.1.0
supports-color: ^7.1.0
checksum: fe75c9d5c76a7a98d45495b91b2172fa3b7a09e0cc9370e5c8feb1c567b85c4288e2b3fded7cfdd7359ac28d6b3844feb8b82b8686842e93d23c827c417e83fc
languageName: node
linkType: hard

"chalk@condition:BABEL_8_BREAKING ? ^4.1.2 : ^2.4.2":
version: 0.0.0-condition-fb503e
resolution: "chalk@condition:BABEL_8_BREAKING?^4.1.2:^2.4.2#fb503e"
dependencies:
chalk-BABEL_8_BREAKING-false: "npm:chalk@^2.4.2"
chalk-BABEL_8_BREAKING-true: "npm:chalk@^4.1.2"
checksum: 937a9201633153fa0e632bc166c6f87174d8dc08ab7a8008047373af6954f3ee7701b64d378bbff4f8aaa0995d4e2d771ad7b1e32962a8231bc727a928cb9248
languageName: node
linkType: hard

"chalk@npm:^1.1.3":
version: 1.1.3
resolution: "chalk@npm:1.1.3"
Expand All @@ -7096,17 +7127,6 @@ __metadata:
languageName: node
linkType: hard

"chalk@npm:^2.0.0, chalk@npm:^2.4.2":
version: 2.4.2
resolution: "chalk@npm:2.4.2"
dependencies:
ansi-styles: ^3.2.1
escape-string-regexp: ^1.0.5
supports-color: ^5.3.0
checksum: ec3661d38fe77f681200f878edbd9448821924e0f93a9cefc0e26a33b145f1027a2084bf19967160d11e1f03bfe4eaffcabf5493b89098b2782c3fe0b03d80c2
languageName: node
linkType: hard

"chalk@npm:^4.0.0, chalk@npm:^4.1.0":
version: 4.1.0
resolution: "chalk@npm:4.1.0"
Expand Down

0 comments on commit 1a62af1

Please sign in to comment.