cem-tools icon indicating copy to clipboard operation
cem-tools copied to clipboard

cem-plugin-expanded-types: Error when analyzing types with index signatures

Open h4de5 opened this issue 1 year ago • 9 comments

When trying to analyze types like:

  @property({ type: Object })
  foobar: { [key: string]: any } = { foo: 'bar' }

I get the following error:

Error: 

[expand-types-plugin]: Looks like you've hit an error in third party plugin: expand-types-plugin. Please try to create a minimal reproduction and inform the author of the expand-types-plugin plugin.

 SyntaxError: Invalid regular expression: /[key/: Unterminated character class
    at new RegExp (<anonymous>)
    at file:///workspace/node_modules/cem-plugin-expanded-types/dist/index.js:84:7
    at Array.forEach (<anonymous>)
    at getObjectTypes (file:///workspace/node_modules/cem-plugin-expanded-types/dist/index.js:77:9)
    at getExpandedType (file:///workspace/node_modules/cem-plugin-expanded-types/dist/index.js:54:12)
    at getTypeValue (file:///workspace/node_modules/cem-plugin-expanded-types/dist/index.js:193:12)

the problematic regexp seems to be this:

https://github.com/break-stuff/cem-tools/blob/8187639d731c8bcfe0df13b65f1ada8c4db397a4/packages/expanded-types/src/cem-plugin.ts#L133

when run, the cleanPart variable holds the string [key which will not produce a valid regexp.

when creating a dedicated type or interface for that, it works:

  type FooBar = { [key: string]: any };

...

  @property({ type: Object })
  foobar: FooBar = { foo: 'bar' }

h4de5 avatar Feb 27 '24 12:02 h4de5

Do you have a repro I can test? Preliminary tests seem to work fine:

"{ [key: string]: any }".replace(/\/\*[\s\S]*?\*\/|(?<=[^:])\/\/.*|^\/\/.*/g, '')

break-stuff avatar Mar 07 '24 01:03 break-stuff

I'm having this issue here as well. In my case, the type is specified as {[name: string]: any} without spaces, causing the same Regex error.

And as @h4de5 described, creating a dedicated type fixes this indeed.

MartinaeyNL avatar Aug 05 '24 12:08 MartinaeyNL

Same error hit with an { [key: string]: string } type.

certainlyakey avatar Aug 19 '24 14:08 certainlyakey

I'm sorry for the delay on this! The patch for it has gone out in version 1.3.3. Let me know if you run into any issues.

break-stuff avatar Sep 30 '24 02:09 break-stuff

It looks like this is also working in my new package: https://wc-toolkit.com/documentation/type-parser/

break-stuff avatar Feb 17 '25 21:02 break-stuff

@break-stuff does the new package supercede this one? Should i remove it?

certainlyakey avatar Feb 18 '25 14:02 certainlyakey

Yes, this will be deprecated and replaced with the WC Toolkit version.

break-stuff avatar Feb 18 '25 15:02 break-stuff

Yes, this will be deprecated and replaced with the WC Toolkit version.

Excellent! Looking forward to the official deprecation :)

certainlyakey avatar Feb 18 '25 22:02 certainlyakey

It has officially been deprecated. 😁

break-stuff avatar Feb 19 '25 00:02 break-stuff

@certainlyakey did the new package fix the issue for you?

break-stuff avatar Nov 04 '25 11:11 break-stuff

@certainlyakey closing as these have been moved to the WC Toolkit. Please let me know if you are experiencing issues with the new package.

break-stuff avatar Nov 10 '25 12:11 break-stuff