tmptplayer
tmptplayer

Reputation: 509

Prettier removes escape characters in Angular

I am using Prettier in my Angular project, and I'm trying to include a regex pattern for a form validator. When I run Prettier, the string is fundamentally altered to make the pattern validation dysfunctional, as shown here:

Before:

export const EmailVal: ScaffoldValidator = PatternVal(
  "/^[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z_+])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9}$/"
);

After:

export const EmailVal: ScaffoldValidator = PatternVal(
  "/^[0-9a-zA-Z]([-.w]*[0-9a-zA-Z_+])*@([0-9a-zA-Z][-w]*[0-9a-zA-Z].)+[a-zA-Z]{2,9}$/"
);

I've gone through the Prettier Docs, which speaks to changing escapes within the context of single vs double quotes, yet asserts that other escapes will be untouched. I've also gone through my tsLint.json, and nothing stands out to me as the source. It does denote "Codelyzer" as the ruleset. Is there a setting somewhere I can disable to prevent this behavior?

Upvotes: 1

Views: 1979

Answers (1)

TmTron
TmTron

Reputation: 19411

You can directly use a regex (note: no quotes - just the slashes):

/^[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z_+])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9}$/;

or correct the string escapes (note the double backslashes):

"/^[0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z_+])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9}$/";
                  ^^                                 ^^              ^^  

Consider using EsLint - it can warn you about these cases (see no-useless-escape rule): enter image description here

Upvotes: 2

Related Questions