Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement pattern to replace React default import method references with destuctured named imports #235

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
```
Loading