From 94074252897be1983ef82bb2e2e4795d0eec5ab8 Mon Sep 17 00:00:00 2001 From: Seren Kwok Date: Tue, 5 Dec 2023 21:11:46 -0500 Subject: [PATCH 1/4] in theory --- .grit/patterns/react_hooks.grit | 11 +++++- .grit/patterns/react_to_hooks.md | 66 ++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/.grit/patterns/react_hooks.grit b/.grit/patterns/react_hooks.grit index c9ce3b96..156998c8 100644 --- a/.grit/patterns/react_hooks.grit +++ b/.grit/patterns/react_hooks.grit @@ -314,7 +314,12 @@ 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()) { @@ -322,7 +327,9 @@ pattern adjust_imports() { } else { $i => `$i\nimport { $hooks } from 'react';` } - } + }, + // $program => . `import { $hooks } from 'react';\n$program`, + true where $hooks <: ensure_import_from(`'react'`), } } } diff --git a/.grit/patterns/react_to_hooks.md b/.grit/patterns/react_to_hooks.md index 03e928df..0ab4b7c7 100644 --- a/.grit/patterns/react_to_hooks.md +++ b/.grit/patterns/react_to_hooks.md @@ -1154,3 +1154,69 @@ export const Nice = (inputProps) => { return null; }; ``` + +## Adds import from React when no React import + +```js +import { BaseComponent } from 'base'; + +export default class Loader extends BaseComponent { + async componentDidMount() { + await loadSomething(); + } + + render() { + return null; + } +} +``` + +```ts +import { useEffect } from 'react'; + +const Loader = () => { + useEffect(() => { + (async () => { + await loadSomething(); + })(); + }, []); + + return null; +}; + +export default Loader; +``` + +## 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 { useEffect } from 'react'; + +import { Component } from 'base'; + +const Loader = () => { + useEffect(() => { + (async () => { + await loadSomething(); + })(); + }, []); + + return null; +}; + +export default Loader; +``` From d5814b93119c9f3bebf0d9d5de2608963128b6fd Mon Sep 17 00:00:00 2001 From: Seren Kwok Date: Wed, 6 Dec 2023 13:31:41 -0500 Subject: [PATCH 2/4] one no match --- .grit/patterns/react_to_hooks.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.grit/patterns/react_to_hooks.md b/.grit/patterns/react_to_hooks.md index 0ab4b7c7..23c3abd2 100644 --- a/.grit/patterns/react_to_hooks.md +++ b/.grit/patterns/react_to_hooks.md @@ -766,6 +766,8 @@ class MyComponent extends Component { ``` ```ts +import { useRef } from 'react'; + const MyComponent = () => { /** * Comment on a private class property @@ -1204,9 +1206,8 @@ export default class Loader extends Component { ``` ```ts -import { useEffect } from 'react'; - import { Component } from 'base'; +import { useEffect } from 'react'; const Loader = () => { useEffect(() => { From e8a00d5ff7fc55fb9a0783f8d67bf56e8ad78677 Mon Sep 17 00:00:00 2001 From: Seren Kwok Date: Wed, 6 Dec 2023 13:38:15 -0500 Subject: [PATCH 3/4] we --- .grit/patterns/react_to_hooks.md | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/.grit/patterns/react_to_hooks.md b/.grit/patterns/react_to_hooks.md index 23c3abd2..74df8a55 100644 --- a/.grit/patterns/react_to_hooks.md +++ b/.grit/patterns/react_to_hooks.md @@ -1157,38 +1157,6 @@ export const Nice = (inputProps) => { }; ``` -## Adds import from React when no React import - -```js -import { BaseComponent } from 'base'; - -export default class Loader extends BaseComponent { - async componentDidMount() { - await loadSomething(); - } - - render() { - return null; - } -} -``` - -```ts -import { useEffect } from 'react'; - -const Loader = () => { - useEffect(() => { - (async () => { - await loadSomething(); - })(); - }, []); - - return null; -}; - -export default Loader; -``` - ## Adds import from React when Component imported from elsewhere ```js From aa2e58a84766864e746a86cac3332b200fc5b4eb Mon Sep 17 00:00:00 2001 From: Seren Kwok <86184664+seren5240@users.noreply.github.com> Date: Wed, 6 Dec 2023 18:46:31 +0000 Subject: [PATCH 4/4] Update react_hooks.grit --- .grit/patterns/react_hooks.grit | 1 - 1 file changed, 1 deletion(-) diff --git a/.grit/patterns/react_hooks.grit b/.grit/patterns/react_hooks.grit index 156998c8..2a6482cd 100644 --- a/.grit/patterns/react_hooks.grit +++ b/.grit/patterns/react_hooks.grit @@ -328,7 +328,6 @@ pattern adjust_imports() { $i => `$i\nimport { $hooks } from 'react';` } }, - // $program => . `import { $hooks } from 'react';\n$program`, true where $hooks <: ensure_import_from(`'react'`), } }