Skip to content

Commit

Permalink
made some refinements in the code
Browse files Browse the repository at this point in the history
  • Loading branch information
“talatkuyuk” committed Feb 5, 2024
1 parent c662810 commit d7904c9
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CONTINUE, EXIT, visit } from "estree-util-visit";
import { type Node } from "estree";
import { CONTINUE, EXIT, SKIP, visit } from "estree-util-visit";
import type { Node, VariableDeclaration } from "estree";

export type TestFunction = (componentName: string) => boolean | undefined | null;

Expand All @@ -21,7 +21,7 @@ function passTest(test: string | string[] | TestFunction | undefined, componentN
*
*/

function emptyComponent(): Node {
function statementOfEmptyComponent(): VariableDeclaration {
return {
type: "VariableDeclaration",
declarations: [
Expand Down Expand Up @@ -78,17 +78,19 @@ function emptyComponent(): Node {
export default function RecmaEscapeMissingComponents(test?: string | string[] | TestFunction) {
return (tree: Node) => {
// inserts the Empty Component definition statement above the function _createMdxContent(props){}
visit(tree, (node, key, index) => {
if (node.type !== "FunctionDeclaration") return CONTINUE;
visit(tree, (node, key, index, ancestors) => {
if (!index) return;

if (node.id?.type === "Identifier" && node.id?.name === "_createMdxContent") {
if ("body" in tree) {
const body = tree["body"] as Node[];
if (ancestors.length !== 1) return SKIP;

body.splice(index!, 0, emptyComponent());
if (node.type !== "FunctionDeclaration") return SKIP;

return EXIT;
}
if (node.id?.name !== "_createMdxContent") return SKIP;

if (tree.type === "Program") {
tree["body"].splice(index, 0, statementOfEmptyComponent());

return EXIT;
}

return CONTINUE;
Expand Down

0 comments on commit d7904c9

Please sign in to comment.