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

Conversation

abidjappie
Copy link
Contributor

@abidjappie abidjappie commented Aug 21, 2024

Description

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.

Background

This is mainly a pattern is used on my current code base, but I hope it can be useful to others.

Using the destructured named imports is the recommended way to use React imports.

When this pattern is used in combination with a linter/ formatter (e.g. Biome) you can easily optimize legacy React code bases.

Caveat

This pattern always adds a new import when there is non-destructured import (if your coding style requires it) e.g.

// this:
import React from 'react';
import { ReactNode } from 'react';
// instead of this:
import React, { ReactNode } from 'react';

Additionally, if you are using typed imports you may need to rely on your linter.

@abidjappie
Copy link
Contributor Author

I get the following error when trying to run on my local.

grit patterns test --filter "react_named" --verbose

Found 1 testable patterns.
Error: Failed to compile pattern react_named_imports: Too many params for text: expected maximum 1

grit doctor:

Client environment
  OS: macos
  Architecture: aarch64
Configuration
  Expected location: /Users/<redacted>/Library/pnpm/global/5/node_modules/@getgrit/launcher/.install/manifests.json
  Config: local config at /Users/<redacted>/code/gritql-stdlib/.grit
  Existing Grit modules dir: /Users/<redacted>/code/gritql-stdlib/.grit/.gritmodules
  Grit API URL: https://api-gateway-prod-6et7uue.uc.gateway.dev (override by setting GRIT_API_URL)
  Grit GraphQL API URL: https://grit-prod-central.hasura.app/v1 (override by setting GRAPHQL_API_URL)
  Grit App URL: https://app.grit.io (override by setting GRIT_APP_URL)
Authentication
  You are not authenticated.
Installed binaries
  timekeeper: not installed. Run grit install to install missing binaries
  gouda: not installed. Run grit install to install missing binaries
  workflow_runner: not installed. Run grit install to install missing binaries
  cli: 0.17.0-alpha.1698733778 (release 0.17.0-alpha.1698733778)
  marzano: 0.2.2-alpha.1716146031 (release 0.2.2-alpha.1716146031)

Copy link
Contributor

@morgante morgante left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thank for contributing.

I get the following error when trying to run on my local.

It looks like you have a very old version of Grit installed. Try reinstalling/updating.

@morgante morgante merged commit c9c12a8 into getgrit:main Aug 21, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants