Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

fix: hook import #267

Merged
merged 5 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 8 additions & 2 deletions .grit/patterns/react_hooks.grit
Original file line number Diff line number Diff line change
Expand Up @@ -314,15 +314,21 @@ pattern adjust_imports() {
$hooks = join(list = $hooks, separator = ", "),
or {
// ugly dealing with imports
contains import_specifier(name = `Component`) => `$hooks`,
contains import_specifier(name = `Component`) as $specifier where {
$specifier <: within import_statement($source) where {
$source <: `"react"`,
},
$specifier => $hooks,
},
contains `import React, { $x } from "react"` => `import React, { $x, $hooks } from "react"`,
contains `import React from "react"` as $i where {
if ($i <: not contains namespace_import()) {
$i => `import React, { $hooks } from 'react';`
} else {
$i => `$i\nimport { $hooks } from 'react';`
}
}
},
true where $hooks <: ensure_import_from(`'react'`),
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions .grit/patterns/react_to_hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,8 @@ class MyComponent extends Component<PropsWithChildren> {
```

```ts
import { useRef } from 'react';

const MyComponent = () => {
/**
* Comment on a private class property
Expand Down Expand Up @@ -1154,3 +1156,36 @@ export const Nice = (inputProps) => {
return null;
};
```

## Adds import from React when Component imported from elsewhere

```js
import { Component } from 'base';

export default class Loader extends Component {
async componentDidMount() {
await loadSomething();
}

render() {
return null;
}
}
```

```ts
import { Component } from 'base';
import { useEffect } from 'react';

const Loader = () => {
useEffect(() => {
(async () => {
await loadSomething();
})();
}, []);

return null;
};

export default Loader;
```
Loading