vitest icon indicating copy to clipboard operation
vitest copied to clipboard

/v RegExp flag is not recognized in Vitest

Open Y3drk opened this issue 1 year ago • 4 comments

Describe the bug

When writing a TypeScript (version 5.3.3) library I need a function using RegExp, which has to use the /v flag (trimmed example in test reproduction).

According to MDN docs it should be supported in node 20.0.0+

The function on its own works all right, but when I want to test it with Vitest I receive an error:

 FAIL  tests/index.test.ts [ tests/index.test.ts ]
Error: Unknown regular expression flags.
 ❯ error node_modules/rollup/dist/es/shared/parseAst.js:337:30
 ❯ parseError node_modules/rollup/dist/es/shared/parseAst.js:972:9
 ❯ convertNode node_modules/rollup/dist/es/shared/parseAst.js:2057:12
 ❯ convertProgram node_modules/rollup/dist/es/shared/parseAst.js:965:12
 ❯ parseAstAsync node_modules/rollup/dist/es/shared/parseAst.js:2108:12
 ❯ ssrTransformScript node_modules/vite/dist/node/chunks/dep-stQc5rCc.js:54036:15
 ❯ loadAndTransform node_modules/vite/dist/node/chunks/dep-stQc5rCc.js:53632:11

Reproduction

In a function use regex with /v flag:

export function showcaseVFlag(name: string): string[] {
    const regexp = /(?:\p{RGI_Emoji})|(?:\P{Mark}(?:\p{Mark}*))/gv;
    return [...name.matchAll(regexp)].flat();
}

Then test it with Vitest:

describe("showcaseVFlag", () => {
    it("should separate graphemes correctly", () => {
       expect(showcaseVFlag("Hello 🌍! 👋")).toStrictEqual(["H", "e", "l", "l", "o", " ", "🌍", "!", " ", "👋"]);
    });
 });

System Info

System:
    OS: Windows 11 10.0.22631
    CPU: (12) x64 AMD Ryzen 5 4600H with Radeon Graphics
    Memory: 2.52 GB / 15.37 GB
  Binaries:
    Node: 20.9.0 - C:\Program Files\nodejs\node.EXE
    npm: 9.1.2 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.9.2 - ~\AppData\Roaming\npm\pnpm.CMD
  Browsers:
    Edge: Chromium (121.0.2277.128)
    Internet Explorer: 11.0.22621.1
  npmPackages:
    vite: ^5.1.3 => 5.1.3
    vitest: ^1.2.2 => 1.2.2

Used Package Manager

npm

Validations

Y3drk avatar Feb 16 '24 13:02 Y3drk

Interesting, it looks like this is such a new stuff and ecosystem hasn't caught up with it. Probably this needs to reported to upstream somewhere:

One relevant issue I found is typescript's lib.d.ts

  • https://github.com/microsoft/TypeScript/issues/57055

Just in case, here is a quick repro https://stackblitz.com/edit/vitest-dev-vitest-sgd3av?file=test%2Fbasic.test.ts

import { test } from 'vitest'

test("repro", () => {
  console.log(/fooo/v)
})

Interestingly, syntax highlighting is also not working...

I think workaround for now is to use RegExp constructor instead:

$ node
> console.log(/fooo/v)
/fooo/v
> new RegExp(/fooo/.source, "v")
/fooo/v

hi-ogawa avatar Feb 17 '24 03:02 hi-ogawa

Did Rollup v4 switch to SWC's parser?

  • https://github.com/swc-project/swc/issues/8462
  • https://github.com/swc-project/swc/blob/9ac304be9e3948e2d723afb80a8ff6cc9811d1a9/crates/swc_ecma_parser/src/parser/expr.rs#L343

AriPerkkio avatar Feb 17 '24 08:02 AriPerkkio

The v flag works in jest.

djstrong avatar Feb 17 '24 08:02 djstrong

Nice, I didn't know sapphi-red already reported there :+1: I don't know the detail but Rollup v4 uses swc internally https://github.com/rollup/rollup/pull/5073

hi-ogawa avatar Feb 17 '24 08:02 hi-ogawa