Skip to content

fix: quote slice IDs in generated slice index files when necessary - #259

Open
angeloashmore wants to merge 1 commit into
mainfrom
aa/quote-slice-ids
Open

fix: quote slice IDs in generated slice index files when necessary#259
angeloashmore wants to merge 1 commit into
mainfrom
aa/quote-slice-ids

Conversation

@angeloashmore

@angeloashmore angeloashmore commented Aug 14, 2026

Copy link
Copy Markdown
Member

Resolves:

Description

The generated slice index file writes slice IDs as bare object keys. An ID that is not a valid JavaScript identifier (e.g. my-slice) makes the whole file invalid JavaScript. The API accepts such IDs, so a prismic pull can deliver them and break a project's build.

Checklist

  • A comprehensive Linear ticket, providing sufficient context and details to facilitate the review of the PR, is linked to the PR.
  • If my changes require tests, I added them.
  • If my changes affect backward compatibility, it has been discussed.
  • If my changes require an update to the CONTRIBUTING.md guide, I updated it.

Preview

A generated index file with a normal ID (hero) and an ID that is not a valid identifier (legacy-hero):

// Code generated by Prismic. DO NOT EDIT.

import Hero from "./Hero";
import LegacyHero from "./LegacyHero";

export const components = {
	hero: Hero,
	"legacy-hero": LegacyHero
};

Before this PR, the legacy-hero entry was written as legacy-hero: LegacyHero, which is a syntax error.

How to QA 1

  1. Run prismic slice create MySlice --id my-slice.
  2. Open the slice index file. Make sure that the entry is "my-slice": MySlice and that the file is valid JavaScript.
  3. Create a slice with a normal snake_case ID. Make sure that its key has no quotes.

🤖 Generated with Claude Code


Note

Low Risk
Scoped to CLI code generation and tests; valid identifier IDs behave as before with no runtime auth or data-path changes.

Overview
Generated slice library index files now emit valid JavaScript object keys for slice IDs that are not legal identifiers (e.g. hyphenated IDs like legacy-hero).

A shared formatObjectKey helper leaves normal identifiers unquoted and uses JSON.stringify for everything else. Next.js, Nuxt, and SvelteKit adapters use it when building the components map in createSliceIndexFile.

Test support was updated so slice-index assertions accept quoted keys, and a slice create test checks that non-identifier IDs appear quoted in slices/index.js.

Reviewed by Cursor Bugbot for commit 18330d7. Bugbot is set up for automated code reviews on this repo. Configure here.

Footnotes

  1. Please use these labels when submitting a review:
    ❓ #ask: Ask a question.
    💡 #idea: Suggest an idea.
    ⚠️ #issue: Strongly suggest a change.
    🎉 #nice: Share a compliment.

The generated components map wrote slice IDs as bare object keys. An
ID that is not a valid JavaScript identifier (for example, an ID with
a dash) made the file invalid. The API accepts such IDs, so a pull
can deliver them. Now the generator quotes an ID when it is not a
valid identifier. Valid IDs stay bare, so formatters do not change
the output.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
angeloashmore added a commit that referenced this pull request Aug 14, 2026
The eval used a snake_case slice ID to work around unquoted keys in
the generated index file. PR #259 quotes IDs that are not valid
identifiers, so the standard test slice works. Run the eval only
after this branch includes that fix.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@angeloashmore
angeloashmore marked this pull request as ready for review August 14, 2026 02:25

@lihbr lihbr left a comment

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.

Very minor suggestion, looks good otherwise :)

Comment thread test/slice-create.test.ts
Comment on lines +37 to +44
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}` });
});

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants