Skip to content

Commit ab26436

Browse files
refactor(setup-init): relax checks on React imports (#1596)
refactor(setup-init): relax checks on React imports due to IDE formatters removing it if not necessary - filter out `import React from "react"` from the AST when parsing current and new content
1 parent 6a2a3f7 commit ab26436

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

.changeset/cute-breads-attack.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"flowbite-react": patch
3+
---
4+
5+
refactor(setup-init): relax checks on React imports due to IDE formatters removing it if not necessary
6+
7+
- filter out `import React from "react"` from the AST when parsing current and new content

packages/ui/src/cli/commands/setup-init.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from "fs/promises";
2+
import type { namedTypes } from "ast-types";
23
import { parse } from "recast";
34
import { initFilePath, initJsxFilePath } from "../consts";
45
import { compareNodes } from "../utils/compare-nodes";
@@ -49,8 +50,8 @@ ThemeInit.displayName = "ThemeInit";
4950
}
5051

5152
if (currentContent) {
52-
const currentAst = parse(currentContent);
53-
const newAst = parse(content);
53+
const currentAst = removeReactImport(parse(currentContent));
54+
const newAst = removeReactImport(parse(content));
5455

5556
if (!compareNodes(currentAst.program, newAst.program)) {
5657
console.log(`Updating ${targetPath} file...`);
@@ -69,3 +70,22 @@ ThemeInit.displayName = "ThemeInit";
6970
console.error(`Failed to update ${targetPath}:`, error);
7071
}
7172
}
73+
74+
/**
75+
* Removes `import React from "react"` from the AST
76+
*/
77+
function removeReactImport(ast: namedTypes.File): namedTypes.File {
78+
if (ast?.program?.body) {
79+
ast.program.body = ast.program.body.filter(
80+
(node) =>
81+
!(
82+
node.type === "ImportDeclaration" &&
83+
"value" in node.source &&
84+
typeof node.source.value === "string" &&
85+
node.source.value === "react" &&
86+
node.specifiers?.[0]?.local?.name === "React"
87+
),
88+
);
89+
}
90+
return ast;
91+
}

0 commit comments

Comments
 (0)