Skip to content

Commit

Permalink
fix: always import picocolors as namespace
Browse files Browse the repository at this point in the history
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';
```
  • Loading branch information
43081j committed Sep 19, 2024
1 parent b305054 commit a3d7b14
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
17 changes: 15 additions & 2 deletions codemods/chalk/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`));
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/chalk/esm/after.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pc from 'picocolors';
import * as pc from 'picocolors';

pc.red();
pc.red('im red');
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/chalk/esm/result.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pc from 'picocolors';
import * as pc from 'picocolors';

pc.red();
pc.red('im red');
Expand Down

0 comments on commit a3d7b14

Please sign in to comment.