From e6f23a3ceab640de2b666291e32b3a28b37f6a5a Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Mon, 23 Sep 2024 11:16:51 +0700 Subject: [PATCH] fix: always import picocolors as namespace (#95) Currently we incorrectly import `picocolors` as if there's a default export: ```ts import pc from 'picocolors'; ``` This is incorrect since `picocolors` exports individual functions. So this fix will update it to the following: ```ts import * as pc from 'picocolors'; ``` --- codemods/chalk/index.js | 17 +++++++++++++++-- test/fixtures/chalk/esm/after.js | 2 +- test/fixtures/chalk/esm/result.js | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/codemods/chalk/index.js b/codemods/chalk/index.js index 1c3e6f3..0303e0b 100644 --- a/codemods/chalk/index.js +++ b/codemods/chalk/index.js @@ -74,8 +74,21 @@ export default function (options) { const nameMatch = imp.getMatch('NAME'); if (nameMatch) { - chalkName = nameMatch.text(); - edits.push(nameMatch.replace('pc')); + const namespaceImport = nameMatch.find({ + rule: { + kind: 'identifier', + inside: { + kind: 'namespace_import', + }, + }, + }); + + if (namespaceImport) { + chalkName = namespaceImport.text(); + } else { + chalkName = nameMatch.text(); + } + edits.push(nameMatch.replace('* as pc')); } edits.push(source.replace(`${quoteType}picocolors${quoteType}`)); diff --git a/test/fixtures/chalk/esm/after.js b/test/fixtures/chalk/esm/after.js index 241d2aa..1b33a47 100644 --- a/test/fixtures/chalk/esm/after.js +++ b/test/fixtures/chalk/esm/after.js @@ -1,4 +1,4 @@ -import pc from 'picocolors'; +import * as pc from 'picocolors'; pc.red(); pc.red('im red'); diff --git a/test/fixtures/chalk/esm/result.js b/test/fixtures/chalk/esm/result.js index 241d2aa..1b33a47 100644 --- a/test/fixtures/chalk/esm/result.js +++ b/test/fixtures/chalk/esm/result.js @@ -1,4 +1,4 @@ -import pc from 'picocolors'; +import * as pc from 'picocolors'; pc.red(); pc.red('im red');