Parse React File with Node.js Function using jscodeshift

Job ID: 38091520

Budget: $10 – $30 USD

I'm seeking a knowledgeable Node.js to create a specific function. This function should examine a pre-existing React file to identify any import statements that may be missing and automatically add the missing imports.

Key responsibilities:

- Analyze the React file to identify missing import statements
- Add the identified missing imports automatically
- Append multiple missing imports as grouped together in a single statement
- Code should be in Typescript

This task requires a solid understanding of Node.js. Having previous experience with jscodeshift is a must. The solution also need to use a package named @hypermod/utils. (https://www.codeshiftcommunity.com/docs/utils)

This is the function that I need

function fixMissingImports(code: string, importsMapping: Record<string, string>): string

The code is a string of a content of a file tsx or jsx file.
the function should first analyze and see what imports of a React components are missing, then add the missing imports according to the importsMapping.

Example 1:
if a code is this:

export default function App() {
return <div>
<Box>some content</Box>
</div>;
}

the function should detect that an import of Box component is missing,
and it should look at the importsMapping["Box"] to get an import to add to the file

example 2:
if the code is this:

import {Box} from "@mui/system";

export default function App() {
return <Stack>
<Box>some content</Box>
</Stack>;
}

the function need to detect that Stack is missing
and in case the importsMapping["Stack"] will return "@mui/system"

The final output of the function need to be like this:

import {Box, Stack} from "@mui/system";

export default function App() {
return <Stack>
<Box>some content</Box>
</Stack>;
}
Related categories: JavaScript Node.js