Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/adapters/nextjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
getSimulatorUrl,
setSimulatorUrl,
} from "../lib/prismic/clients/core";
import { dedent } from "../lib/string";
import { dedent, formatObjectKey } from "../lib/string";
import { appendTrailingSlash } from "../lib/url";
import { buildRoutePath, getRepositoryName } from "../project";
import { checkIsTypeScriptProject, findProjectRoot } from "../project";
Expand Down Expand Up @@ -104,7 +104,7 @@ export class NextJsAdapter extends Adapter {
});
const componentLines = slices.map((slice) => {
const componentName = pascalCase(slice.model.name);
return `${slice.model.id}: ${componentName}`;
return `${formatObjectKey(slice.model.id)}: ${componentName}`;
});
const contents = dedent`
// Code generated by Prismic. DO NOT EDIT.
Expand Down
4 changes: 2 additions & 2 deletions src/adapters/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
getSimulatorUrl,
setSimulatorUrl,
} from "../lib/prismic/clients/core";
import { dedent } from "../lib/string";
import { dedent, formatObjectKey } from "../lib/string";
import { appendTrailingSlash } from "../lib/url";
import { buildRoutePath, getRepositoryName, readConfig, updateConfig } from "../project";
import { checkIsTypeScriptProject, findProjectRoot } from "../project";
Expand Down Expand Up @@ -90,7 +90,7 @@ export class NuxtAdapter extends Adapter {
const slices = allSlices.filter((slice) => slice.library.href === library.href);
const componentLines = slices.map((slice) => {
const relativeDirectory = relative(fileURLToPath(library), fileURLToPath(slice.directory));
return `${slice.model.id}: defineAsyncComponent(() => import("./${relativeDirectory}/index.vue"))`;
return `${formatObjectKey(slice.model.id)}: defineAsyncComponent(() => import("./${relativeDirectory}/index.vue"))`;
});
const contents = dedent`
// Code generated by Prismic. DO NOT EDIT.
Expand Down
4 changes: 2 additions & 2 deletions src/adapters/sveltekit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
getSimulatorUrl,
setSimulatorUrl,
} from "../lib/prismic/clients/core";
import { dedent } from "../lib/string";
import { dedent, formatObjectKey } from "../lib/string";
import { appendTrailingSlash } from "../lib/url";
import { buildRoutePath, getRepositoryName } from "../project";
import { checkIsTypeScriptProject, findProjectRoot } from "../project";
Expand Down Expand Up @@ -107,7 +107,7 @@ export class SvelteKitAdapter extends Adapter {
});
const componentLines = slices.map((slice) => {
const componentName = pascalCase(slice.model.name);
return `${slice.model.id}: ${componentName}`;
return `${formatObjectKey(slice.model.id)}: ${componentName}`;
});
const contents = dedent`
// Code generated by Prismic. DO NOT EDIT.
Expand Down
4 changes: 4 additions & 0 deletions src/lib/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export function dedent(strings: TemplateStringsArray | string, ...values: unknow
return baseDedentWithOptions(Object.assign(resolved, { raw: resolved }), ...values);
}

export function formatObjectKey(key: string): string {
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key) ? key : JSON.stringify(key);
}

export function formatTable(
rows: string[][],
config?: { headers?: string[]; separator?: string },
Expand Down
2 changes: 1 addition & 1 deletion test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ expect.extend({
const sliceIndexPath = new URL("../index.js", sliceDirectory);
try {
const sliceIndex = await readFile(sliceIndexPath, "utf8");
if (!new RegExp(`\\b${slice.id}: `).test(sliceIndex)) {
if (!new RegExp(`(?:"|\\b)${slice.id}"?: `).test(sliceIndex)) {
problems.push(
`slice "${slice.id}" not found in slice library index (${sliceIndexPath.href})`,
);
Expand Down
9 changes: 9 additions & 0 deletions test/slice-create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ it("creates a slice with a custom id", async ({ expect, prismic, project }) => {
expect(created).toBeDefined();
});

it("quotes the slice ID in the slice index file", async ({ expect, prismic, project }) => {
const { id, name } = buildSlice();

const { stderr, exitCode } = await prismic("slice", ["create", name, "--id", id]);
expect(exitCode, stderr).toBe(0);

await expect(project).toHaveFile("slices/index.js", { contains: `"${id}": ${name}` });
});
Comment on lines +37 to +44

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ #suggestion: I find it a bit hard to link here that the slice ID coming from buildSlice has to be quoted. Essentially, this test breaks if we change buildSlice's ID to sliceS${id} (hypen removed)


it("creates a slice in the first configured library", async ({
expect,
prismic,
Expand Down