Skip to content

Commit

Permalink
feat: implement pattern to replace React default import method refere…
Browse files Browse the repository at this point in the history
…nces with destructured named imports
  • Loading branch information
abidjappie committed Aug 21, 2024
1 parent 11e1960 commit 900ea57
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions .grit/patterns/js/react_named_imports.md
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;
}
```

0 comments on commit 900ea57

Please sign in to comment.