Skip to content

Commit

Permalink
so much smarter
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante committed Jul 5, 2024
1 parent 136ee39 commit 7fabb7e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
40 changes: 38 additions & 2 deletions .grit/patterns/js/_convert_default_imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ language js
pattern replace_default_import($source, $new_name) {
or {
`import * as $alias from $source` => `import { $new_name as $alias } from $source`,
`import * as $alias from $source` as $import where {
if ($alias <: $new_name) {
$import => `import { $new_name } from $source`
} else {
$import => `import { $new_name as $alias } from $source`
}
},
`import { $imports } from $source` where {
$imports <: contains `default` => $new_name
},
Expand All @@ -23,7 +29,13 @@ pattern replace_default_import($source, $new_name) {
$imports += `, $new_name as $alias`
}
},
`import $alias from $source` => `import { $new_name as $alias } from $source`,
`import $alias from $source` as $import where {
if ($alias <: contains $new_name) {
$import => `import { $new_name } from $source`
} else {
$import => `import { $new_name as $alias } from $source`
}
}
}
}
Expand Down Expand Up @@ -128,3 +140,27 @@ import myAlias, { otherImport } from 'star';
// @filename: here.js
import { otherImport, myImport as myAlias } from 'star';
```

## Handle standalone import without alias

```ts
// @filename: here.js
import myImport from 'star';
```

```ts
// @filename: here.js
import { myImport } from 'star';
```

## Handle standalone import with alias

```ts
// @filename: here.js
import myAlias from 'star';
```

```ts
// @filename: here.js
import { myImport as myAlias } from 'star';
```
5 changes: 2 additions & 3 deletions .grit/patterns/js/migrate_default_imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ tags: [migration, js, imports, default, multifile]

This pattern combines [convert_default_exports](https://github.com/getgrit/stdlib/blob/9dfce85d25977e08bbd476f693e6cbc07ef08316/.grit/patterns/js/_convert_default_exports.md) and [\_convert_default_imports](https://github.com/getgrit/stdlib/blob/9dfce85d25977e08bbd476f693e6cbc07ef08316/.grit/patterns/js/_convert_default_imports.md#L4) to replace default exports to named exports and replace default imports with named imports.


```grit
language js
Expand Down Expand Up @@ -55,7 +54,7 @@ export function name() {
}

// @filename: foo/module_user.ts
import { name as name } from 'mymodule';
import { name } from 'mymodule';
import other from 'othermodule';
```

Expand All @@ -82,7 +81,7 @@ export function name() {
}

// @filename: folder2/bar.js
import { name as name } from '../folder1/foo';
import { name } from '../folder1/foo';

// @filename: folder3/baz
// This is a different folder - ignore it
Expand Down

0 comments on commit 7fabb7e

Please sign in to comment.