Skip to content

Commit e6f23a3

Browse files
authored
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'; ```
1 parent b305054 commit e6f23a3

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

codemods/chalk/index.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,21 @@ export default function (options) {
7474
const nameMatch = imp.getMatch('NAME');
7575

7676
if (nameMatch) {
77-
chalkName = nameMatch.text();
78-
edits.push(nameMatch.replace('pc'));
77+
const namespaceImport = nameMatch.find({
78+
rule: {
79+
kind: 'identifier',
80+
inside: {
81+
kind: 'namespace_import',
82+
},
83+
},
84+
});
85+
86+
if (namespaceImport) {
87+
chalkName = namespaceImport.text();
88+
} else {
89+
chalkName = nameMatch.text();
90+
}
91+
edits.push(nameMatch.replace('* as pc'));
7992
}
8093

8194
edits.push(source.replace(`${quoteType}picocolors${quoteType}`));

test/fixtures/chalk/esm/after.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import pc from 'picocolors';
1+
import * as pc from 'picocolors';
22

33
pc.red();
44
pc.red('im red');

test/fixtures/chalk/esm/result.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import pc from 'picocolors';
1+
import * as pc from 'picocolors';
22

33
pc.red();
44
pc.red('im red');

0 commit comments

Comments
 (0)