Skip to content

Commit

Permalink
feat: use regex to enhance js engine support (#762)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Sep 9, 2024
1 parent 4769588 commit ed36296
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 68 deletions.
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

0 comments on commit ed36296

Please sign in to comment.