-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement pattern to replace React default import method refere…
…nces with destructured named imports
- Loading branch information
1 parent
11e1960
commit 900ea57
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
--- | ||
title: Replace React default imports with destructured named imports | ||
tags: [import, react, global] | ||
--- | ||
|
||
This pattern replaces React default import method references (e.g. `React.ReactNode`) with destructured named imports (`import { ReactNode } from 'react'`). | ||
Running this will also make sure that `React` is imported. | ||
|
||
```grit | ||
engine marzano(0.1) | ||
language js(jsx) | ||
`React.$reactImport` where { | ||
$reactImport <: ensure_import_from(`"react"`), | ||
} => `$reactImport` | ||
``` | ||
|
||
## Replace method on React default import | ||
|
||
Given the following interface with `React` global: | ||
|
||
```typescript | ||
import React from 'react'; | ||
|
||
interface MyComponentProps { | ||
children: React.ReactNode; | ||
anotherProp?: boolean; | ||
} | ||
``` | ||
|
||
The result should import the relevant module: | ||
|
||
```typescript | ||
import React from 'react'; | ||
import { ReactNode } from 'react'; | ||
|
||
interface MyComponentProps { | ||
children: ReactNode; | ||
anotherProp?: boolean; | ||
} | ||
``` | ||
|
||
Patterns such as `unused_imports` can be used to clean up the duplicate import. | ||
|
||
## Replace React global | ||
|
||
Given the following interface with `React` global: | ||
|
||
```typescript | ||
interface MyComponentProps { | ||
children: React.ReactNode; | ||
anotherProp?: boolean; | ||
} | ||
``` | ||
|
||
The result should import the relevant module: | ||
|
||
```typescript | ||
import { ReactNode } from 'react'; | ||
|
||
interface MyComponentProps { | ||
children: ReactNode; | ||
anotherProp?: boolean; | ||
} | ||
``` |