Skip to content

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

@RobertoNegro

Description

@RobertoNegro

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions