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

feat: use regex to enhance js engine support #762

Merged
merged 15 commits into from
Sep 9, 2024
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
],
"references.preferredLocation": "peek",
"cSpell.words": [
"Extenerlaize",
"shikijs"
]
}
98 changes: 49 additions & 49 deletions docs/references/engine-js-compat.md

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"type": "module",
"version": "1.16.2",
"private": true,
"packageManager": "pnpm@9.9.0",
"packageManager": "pnpm@9.10.0",
"scripts": {
"lint": "eslint . --cache",
"release": "bumpp && pnpm -r publish",
Expand Down Expand Up @@ -52,6 +52,7 @@
"picocolors": "catalog:",
"pnpm": "catalog:",
"prettier": "catalog:",
"regex": "catalog:",
"rimraf": "catalog:",
"rollup": "catalog:",
"rollup-plugin-copy": "catalog:",
Expand Down
5 changes: 3 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@
},
"dependencies": {
"@shikijs/vscode-textmate": "catalog:",
"@types/hast": "catalog:"
"@types/hast": "catalog:",
"oniguruma-to-js": "catalog:",
"regex": "catalog:"
},
"devDependencies": {
"hast-util-to-html": "catalog:",
"oniguruma-to-js": "catalog:",
"vscode-oniguruma": "catalog:"
}
}
6 changes: 6 additions & 0 deletions packages/core/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ const plugins = [
const external = [
'hast',
'@shikijs/vscode-textmate',

// Externalize them to make it easier to patch and experiments
// Versions are pinned to avoid regressions
// Later we might consider to bundle them.
'oniguruma-to-js',
'regex',
]

export default defineConfig([
Expand Down
19 changes: 16 additions & 3 deletions packages/core/src/engines/javascript.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { onigurumaToRegexp } from 'oniguruma-to-js'
import { rewrite } from 'regex'
import type { JavaScriptRegexEngineOptions, PatternScanner, RegexEngine, RegexEngineString } from '../types'

const MAX = 4294967295
Expand All @@ -7,10 +8,22 @@ const MAX = 4294967295
* The default RegExp constructor for JavaScript regex engine.
*/
export function defaultJavaScriptRegexConstructor(pattern: string): RegExp {
pattern = pattern
// YAML specific handling; TODO: move to tm-grammars
.replaceAll('[^\\s[-?:,\\[\\]{}#&*!|>\'"%@`]]', '[^\\s\\-?:,\\[\\]{}#&*!|>\'"%@`]')

const rewritten = rewrite(pattern, {
flags: 'dgm',
unicodeSetsPlugin: null,
disable: {
n: true,
v: true,
x: true,
},
})

return onigurumaToRegexp(
pattern
// YAML specific handling; TODO: move to tm-grammars
.replaceAll('[^\\s[-?:,\\[\\]{}#&*!|>\'"%@`]]', '[^\\s\\-?:,\\[\\]{}#&*!|>\'"%@`]'),
rewritten.expression,
{
flags: 'dgm',
ignoreContiguousAnchors: true,
Expand Down
36 changes: 25 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ catalog:
minimist: ^1.2.8
monaco-editor-core: ^0.51.0
ofetch: ^1.3.4
oniguruma-to-js: ^0.3.3
oniguruma-to-js: 0.3.3
picocolors: ^1.1.0
pinia: ^2.2.2
pnpm: ^9.9.0
pnpm: ^9.10.0
prettier: ^3.3.3
regex: 4.3.2
rehype-raw: ^7.0.0
rehype-stringify: ^10.0.0
remark-parse: ^11.0.0
Expand Down
Loading