Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix imports for commonly-named components #398

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 additions & 2 deletions packages/studio-plugin/src/sourcefiles/PageFile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ArrowFunction, FunctionDeclaration, Project } from "ts-morph";
import { Result } from "true-myth";
import { PageState } from "../types";
import { FileMetadata, PageState } from "../types";
import TemplateConfigWriter from "../writers/TemplateConfigWriter";
import ReactComponentFileWriter from "../writers/ReactComponentFileWriter";
import upath from "upath";
Expand Down Expand Up @@ -110,8 +110,12 @@ export default class PageFile {
* source file will be mutated to update the template configuration.
*
* @param updatedPageState - the updated state for the page file
* @param UUIDToFileMetadata - mapping of metadataUUID to FileMetadata
*/
updatePageFile(updatedPageState: PageState): void {
updatePageFile(
updatedPageState: PageState,
UUIDToFileMetadata: Record<string, FileMetadata>
): void {
const onFileUpdate = (
pageComponent: FunctionDeclaration | ArrowFunction
) => {
Expand All @@ -134,6 +138,7 @@ export default class PageFile {
componentTree: updatedPageState.componentTree,
cssImports: updatedPageState.cssImports,
onFileUpdate,
UUIDToFileMetadata,
});
}
}
3 changes: 2 additions & 1 deletion packages/studio-plugin/src/writers/FileSystemWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export class FileSystemWriter {
*/
writeToPageFile(pageName: string, pageState: PageState): void {
const pageFile = this.orchestrator.getOrCreatePageFile(pageName);
pageFile.updatePageFile(pageState);
const UUIDToFileMetadata = this.orchestrator.getUUIDToFileMetadata();
pageFile.updatePageFile(pageState, UUIDToFileMetadata);
}

writeToSiteSettings(siteSettingsValues: SiteSettingsValues): void {
Expand Down
43 changes: 40 additions & 3 deletions packages/studio-plugin/src/writers/ReactComponentFileWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ import StudioSourceFileParser from "../parsers/StudioSourceFileParser";
import {
ComponentState,
ComponentStateKind,
ErrorComponentState,
FileMetadata,
PropVal,
PropValueKind,
PropValues,
PropValueType,
StandardComponentState,
} from "../types";
import StudioSourceFileWriter from "./StudioSourceFileWriter";
import ComponentTreeHelpers from "../utils/ComponentTreeHelpers";
import camelCase from "camelcase";
import { CustomTags } from "../parsers/helpers/TypeNodeParsingHelper";
import getImportSpecifier from "../utils/getImportSpecifier";

/**
* ReactComponentFileWriter is a class for housing data
Expand Down Expand Up @@ -161,6 +165,39 @@ export default class ReactComponentFileWriter {
}
}

/**
* Gets the import data for each component in the component tree that is
* defined in the user's repo (e.g. in `src/components`). Note, for now, we
* only allow default imports for components.
*/
private getComponentImports(
componentTree: ComponentState[],
UUIDToFileMetadata: Record<string, FileMetadata>
): { name: string; moduleSpecifier: string }[] {
return componentTree
.filter(
(c): c is StandardComponentState | ErrorComponentState =>
c.kind === ComponentStateKind.Standard ||
c.kind === ComponentStateKind.Error
)
.filter((c) => {
if (UUIDToFileMetadata[c.metadataUUID]) {
return true;
}
console.error(
`Error adding import in ${this.componentName}: unable to find metadata for ${c.componentName}.`
);
return false;
})
.map((c) => {
const moduleSpecifier = getImportSpecifier(
this.studioSourceFileParser.getFilepath(),
UUIDToFileMetadata[c.metadataUUID].filepath
);
return { name: c.componentName, moduleSpecifier };
});
}

/**
* Update a React component file, which include:
* - file imports
Expand All @@ -170,16 +207,16 @@ export default class ReactComponentFileWriter {
*/
updateFile({
componentTree,
UUIDToFileMetadata,
cssImports,
onFileUpdate,
defaultImports,
}: {
componentTree: ComponentState[];
UUIDToFileMetadata: Record<string, FileMetadata>;
cssImports?: string[];
onFileUpdate?: (
functionComponent: FunctionDeclaration | ArrowFunction
) => void;
defaultImports?: { name: string; moduleSpecifier: string }[];
}): void {
let defaultExport: VariableDeclaration | FunctionDeclaration;
try {
Expand Down Expand Up @@ -207,7 +244,7 @@ export default class ReactComponentFileWriter {
this.studioSourceFileWriter.updateFileImports(
{},
cssImports,
defaultImports
this.getComponentImports(componentTree, UUIDToFileMetadata)
);
this.studioSourceFileWriter.writeToFile();
}
Expand Down
5 changes: 5 additions & 0 deletions packages/studio-plugin/tests/FileSystemManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import {
PageState,
} from "../src/types";
import { createTestProject } from "./__utils__/createTestSourceFile";
import * as uuidUtils from "uuid";

jest.mock("uuid");
jest.spyOn(uuidUtils, "v4").mockReturnValue("mock-metadata-uuid");

const bannerComponentState: ComponentState = {
kind: ComponentStateKind.Standard,
Expand All @@ -34,6 +38,7 @@ const projectRoot = upath.resolve(
const tsMorphProject: Project = createTestProject();
const paths = getUserPaths(projectRoot);
paths.pages = upath.join(projectRoot, "pages");
paths.components = upath.join(projectRoot, "components");

const orchestrator = new ParsingOrchestrator(tsMorphProject, {
paths,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Banner() {
return <></>;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Banner from "../components/Banner";

export default function NewPage() {
return <Banner />;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Container from "../../ComponentFile/Container";
import Text from "../../ComponentFile/Text";

export default function IndexPage() {
return (
<>
<Container />
<Text />
</>
);
}
6 changes: 3 additions & 3 deletions packages/studio-plugin/tests/__fixtures__/componentStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const streamConfigMultipleFieldsComponentTree: ComponentState[] = [
componentName: "ComplexBanner",
parentUUID: "mock-uuid-0",
uuid: "mock-uuid-1",
metadataUUID: "banner-metadata",
metadataUUID: "mock-metadata-uuid",
props: {
title: {
kind: PropValueKind.Expression,
Expand Down Expand Up @@ -151,7 +151,7 @@ export const streamConfigMultipleFieldsComponentTree: ComponentState[] = [
componentName: "ComplexBanner",
parentUUID: "mock-uuid-0",
uuid: "mock-uuid-3",
metadataUUID: "banner-metadata",
metadataUUID: "mock-metadata-uuid",
props: {
title: {
kind: PropValueKind.Expression,
Expand All @@ -165,7 +165,7 @@ export const streamConfigMultipleFieldsComponentTree: ComponentState[] = [
componentName: "ComplexBanner",
parentUUID: "mock-uuid-0",
uuid: "mock-uuid-4",
metadataUUID: "banner-metadata",
metadataUUID: "mock-metadata-uuid",
props: {
title: {
kind: PropValueKind.Literal,
Expand Down
Loading
Loading