plop icon indicating copy to clipboard operation
plop copied to clipboard

Append action erring on string with regex

Open jwrudzin opened this issue 5 years ago • 22 comments

I'm seeing the same issue that was marked closed in #178. Opening a new issue here to track the problem.

I'm running plop v2.5.3 and seeing the same issue (using the same code) as outlined in #178.

jwrudzin avatar Nov 22 '19 20:11 jwrudzin

Similar regex problem while using append. plop cant find the below string and appends again. The problem is with the "(" and ")". They must be escaped while regexing before append by plop.

<Route key="AboutPage" exact path={PATHS.ABOUT_PAGE} render={(props) => <AboutPage {...props} />} />

The

cemcakirlar avatar Nov 24 '19 19:11 cemcakirlar

the problem from @jwrudzin seems to be the "[" and "\" which must be escaped by plop to match

{handle: "[data-{{ dashCase name}}]", require: {{properCase name}} }\,

the correct regex to match the above is

{handle: "\[data-{{ dashCase name}}]", require: {{properCase name}} }\\,

per https://regexr.com/

cemcakirlar avatar Nov 24 '19 20:11 cemcakirlar

in file append.js I made the below modification, and it seems to be working now for append. Someone may commit this maybe, I am not a familiar with source control systems.

    const stringToAppendSanitized = (separator + stringToAppend).replace(/[.*+?^${}()|[\]\\]/g, "\\$&") //added by cakirlarc
    // const lastPartWithoutDuplicates = lastPart.replace(new RegExp(separator + stringToAppend, 'g'), ''); //original plop
    const lastPartWithoutDuplicates = lastPart.replace(new RegExp(stringToAppendSanitized, 'g'), ''); //added by cakirlarc

@jwrudzin @amwmedia

cemcakirlar avatar Nov 24 '19 20:11 cemcakirlar

Thanks, I'll take a look at this soon

amwmedia avatar Nov 25 '19 15:11 amwmedia

Has there been any update on this? I'm unfortunately still running into this issue even using the escaped regex string cakirlarc posted.

jwrudzin avatar Feb 13 '20 19:02 jwrudzin

Looking further, there seems to have been a PR already opened for this exact issue:

https://github.com/plopjs/node-plop/pull/139

We're waiting on some tests being written but once this is done, we can merge and cut a new release

crutchcorn avatar Feb 13 '20 20:02 crutchcorn

Looks like those tests were written -- any chance we can get that merged?

bryantAXS avatar Feb 19 '20 22:02 bryantAXS

@bryantAXS I don't seem to see tests in the pull request in question. Am I missing something obvious?

crutchcorn avatar Feb 19 '20 22:02 crutchcorn

Can someone give me an input that still throws this error? I am working on writing tests so that we can get this fix in (branch fix-append-unique) and get get node-plop to throw errors for either inputs:

<Route key="AboutPage" exact path={PATHS.ABOUT_PAGE} render={(props) => <AboutPage {...props} />} />

Nor

{handle: "[data-{{ dashCase name}}]", require: {{properCase name}} }\,

Tests are a good thing to get working, but useless if I can't make them fail in the un-regexed codebase

crutchcorn avatar Feb 24 '20 01:02 crutchcorn

@crutchcorn sorry, I misread the pull request!

I just pulled down the latest 2.5.4 and it looks like I'm still running into the issue.

In this case the input used in the name variable is TestingForGithub

actions.push({
    type: 'append',
    path: 'src/scripts/app.js',
    pattern: /(Components Loader -- DO NOT REMOVE COMMENT!)/gi, 
    template: '{handle: "[data-{{ dashCase name}}]", require: {{properCase name}} }\,',
});

The errors generated are:

✔  ++ /src/components/molecules/TestingForGithub/TestingForGithub.js
✔  _+ /src/scripts/app.js
✖  _+ Invalid regular expression: /
{handle: "[data-testing-for-github]", require: TestingForGithub },/: Range out of order in character class
✖  ++ src/components/{{lowerCase type}}/{{properCase name}}/{{properCase name}}.scss Aborted due to previous action failure
✖  ++ src/components/{{lowerCase type}}/{{properCase name}}/{{properCase name}}.twig Aborted due to previous action failure
✖  _+ src/styles/includes/{{lowerCase type}}.scss Aborted due to previous action failure

Does that help?

bryantAXS avatar Feb 25 '20 16:02 bryantAXS

I'm still unable to get a failing test in node-plop for whatever reason when trying to write tests. I'll be taking a closer look at the integration within plop to see more specifically where the problem lies and if the PR that's been opened will really fix it. If it does, we'll merge. Just trying to do due diligence

crutchcorn avatar Feb 27 '20 16:02 crutchcorn

@crutchcorn sounds good - thanks!

bryantAXS avatar Mar 03 '20 22:03 bryantAXS

this can be worked around if you use the SafeString from handlebars, as such

template: new Handlebars.SafeString('special string').toString()

at least in my case it solved the issue, but this is very cumbersome

datner avatar Mar 10 '20 15:03 datner

@crutchcorn Just checking to see if there's any update here? Still hoping to get this fix merged in. Thanks!

jwrudzin avatar Apr 27 '20 19:04 jwrudzin

I think we can merge in a PR that I have open for this https://github.com/plopjs/node-plop/pull/164. That said, I'd like someone to test that this actually solves the problem. If someone can pull in node-plop and plop and do some testing on a scenario they know is broken, then I can merge and we can cut a release

crutchcorn avatar Apr 30 '20 16:04 crutchcorn

The initial issue to be solved by https://github.com/plopjs/node-plop/pull/164 can be reproduced here https://codesandbox.io/s/ecstatic-dubinsky-igwz6?file=/plopfile.js Just run npx plop with same name three times and open the index.ts file.

novli avatar Jun 08 '20 15:06 novli

any updates on this one?

valgussev avatar Feb 10 '21 14:02 valgussev

Seeing the same error on ^2.7.4 with a [ character in the template string.

Tried new Handlebars.SafeString as above, but same result.

\\ to escape the characters removes the error, but leaves a single \ in the appended content. Any help would be much appreciated 💪

tomdev10 avatar May 11 '21 09:05 tomdev10

I'm getting the same error in 2.7.6 with

        - "deployment-templates/spring-boot/**/*"

If you want to reproduce it's as simple as adding append action:

        {
          type: 'append',
          path: '../.github/monorepo.yml',
          templateFile: 'spring-boot/.github/monorepo.yml',
        },

the template:

  {{serviceName}}:
    depends_on:
      files:
        - "deployment-templates/spring-boot/**/*"

    required_checks:
      - build
      - test

gustaff-weldon avatar Oct 19 '21 15:10 gustaff-weldon

any updates on this one? this occur plop 3.1.1 v

siosio34 avatar Aug 24 '22 05:08 siosio34

@siosio34 It does not seem so, the PR was moved to https://github.com/plopjs/plop/pull/330 from what I was able to find

gustaff-weldon avatar Aug 25 '22 14:08 gustaff-weldon

It would be great to get https://github.com/plopjs/plop/pull/330 merged in for the next release. I spent way too long trying to troubleshoot why a template with "()" in it wasn't appending unique.

mdoll-arcadis avatar Sep 01 '22 20:09 mdoll-arcadis