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

In append action when unique is enabled there is no escape for regexp special characters #462

Open
RobertoNegro opened this issue Nov 28, 2024 · 0 comments

Comments

@RobertoNegro
Copy link

Hello,

As in #178 , #188 , #353 , the append action has a major problem when using regexp special characters in the template and when unique is set to true (or undefined since true is the default value).

In node-plop code there is:

// append.js:23
const lastPartWithoutDuplicates = lastPart.replace(
  new RegExp(separator + stringToAppend, "g"),
  "",
);

Since the stringToAppend variable is the rendered template without further manipulation, if the template contains some regexp special characters, the regexp might be invalid.

For example, if you define as action:

{
    type: 'append',
    path: 'file.json',
    pattern: /"elements": {(?<insertion>)/g,
    template: '"list": [ "element-list" ];',
},

And your file.json is:

{
  "elements": {
   }
}

when executed it gives the following error: Range out of order in character class. (since t-l is an invalid character range). This can be fixed by using for example: template: '"list": \\[ "element-list" ];',, but then the json generated is invalid (it prints literally "list": \[ "element-list" ]), so adding escape characters is not an option.

There was a PR fixing this: #330 but it was closed without merging.

If this happens to you, a temporary solution might be to explicitly set unique: false in your action configuration. But this is not ideal, because it means that you'll get duplicates if you run multiple times your generator.

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

No branches or pull requests

1 participant