prismicio / prismic-ts-codegen

A Prismic model-to-TypeScript-type generator.
Apache License 2.0
18 stars 6 forks source link

custom types with an `id` starting with a number cause an error #29

Closed EddyVinck closed 2 years ago

EddyVinck commented 2 years ago

Hi! 👋 I noticed a bug when the id in my custom type starts with a number.

Versions

Reproduction

Steps to reproduce

What is expected?

An error stating that my ID can't start with a number would be great! Maybe an update to slice-machine-ui to prevent this would be nice as well.

What is actually happening?

I'm getting a really vague error:

npx prismic-ts-codegen
NotImplementedError: Unexpected! Inserting syntax kind of InterfaceDeclaration, but ExpressionStatement was inserted.
    at getRangeWithoutCommentsFromArray (/Users/eddy/dev/my-project/node_modules/ts-morph/dist/ts-morph.js:919:23)
    at insertIntoBracesOrSourceFileWithGetChildren (/Users/eddy/dev/my-project/node_modules/ts-morph/dist/ts-morph.js:2525:12)
    at SourceFile._insertChildren (/Users/eddy/dev/my-project/node_modules/ts-morph/dist/ts-morph.js:11451:20)
    at SourceFile.insertInterfaces (/Users/eddy/dev/my-project/node_modules/ts-morph/dist/ts-morph.js:11263:25)
    at SourceFile.addInterfaces (/Users/eddy/dev/my-project/node_modules/ts-morph/dist/ts-morph.js:11257:25)
    at SourceFile.addInterface (/Users/eddy/dev/my-project/node_modules/ts-morph/dist/ts-morph.js:11254:25)
    at addTypeAliasForCustomType (file:///Users/eddy/dev/my-project/node_modules/prismic-ts-codegen/bin/cli.mjs:711:32)
    at generateTypes (file:///Users/eddy/dev/my-project/node_modules/prismic-ts-codegen/bin/cli.mjs:950:7)
    at main (file:///Users/eddy/dev/my-project/node_modules/prismic-ts-codegen/bin/cli.mjs:1204:21)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
angeloashmore commented 2 years ago

Hey @EddyVinck, thanks for the bug report!

This should be fixed as of v0.1.3. Custom Types or Shared Slices that start with a number (e.g. 404-page) will have types prefixed with an underscore (e.g. _404Page).

If you want to learn more about the underlying issue and why the underscore is necessary, see PR #30.

You can update to the latest version with the following command:

npm install --save-dev prismic-ts-codegen

Then re-generate your types:

npx prismic-ts-codegen

If this doesn't fix the issue, please let me know and I'll take another look. Thanks! 🙂