@shikijs/engine-javascript 3.23.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,37 @@
1
- import '@shikijs/types';
2
- import 'oniguruma-to-es';
3
- export { J as JavaScriptRegexEngineOptions, c as createJavaScriptRegexEngine, d as defaultJavaScriptRegexConstructor } from './shared/engine-javascript.CDEDnU-m.mjs';
4
- import '@shikijs/vscode-textmate';
1
+ import { t as JavaScriptRegexScannerOptions } from "./scanner-CFS1MV4a.mjs";
2
+ import { ToRegExpOptions } from "oniguruma-to-es";
3
+ import { RegexEngine } from "@shikijs/types";
4
+
5
+ //#region src/engine-compile.d.ts
6
+ interface JavaScriptRegexEngineOptions extends JavaScriptRegexScannerOptions {
7
+ /**
8
+ * The target ECMAScript version.
9
+ *
10
+ * Oniguruma-To-ES uses RegExp features from later versions of ECMAScript to add support for a
11
+ * few more grammars. If using target `ES2024` or later, the RegExp `v` flag is used which
12
+ * requires Node.js 20+ or Chrome 112+.
13
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicodeSets
14
+ *
15
+ * For maximum compatibility, you can set it to `ES2018` which uses the RegExp `u` flag.
16
+ *
17
+ * Set to `auto` to automatically detect the latest version supported by the environment.
18
+ *
19
+ * @default 'auto'
20
+ */
21
+ target?: 'auto' | 'ES2025' | 'ES2024' | 'ES2018';
22
+ }
23
+ /**
24
+ * The default regex constructor for the JavaScript RegExp engine.
25
+ */
26
+ declare function defaultJavaScriptRegexConstructor(pattern: string, options?: ToRegExpOptions): RegExp;
27
+ /**
28
+ * Use the modern JavaScript RegExp engine to implement the OnigScanner.
29
+ *
30
+ * As Oniguruma supports some features that can't be emulated using native JavaScript regexes, some
31
+ * patterns are not supported. Errors will be thrown when parsing TextMate grammars with
32
+ * unsupported patterns, and when the grammar includes patterns that use invalid Oniguruma syntax.
33
+ * Set `forgiving` to `true` to ignore these errors and skip any unsupported or invalid patterns.
34
+ */
35
+ declare function createJavaScriptRegexEngine(options?: JavaScriptRegexEngineOptions): RegexEngine;
36
+ //#endregion
37
+ export { JavaScriptRegexEngineOptions, createJavaScriptRegexEngine, defaultJavaScriptRegexConstructor };
@@ -1,52 +1,48 @@
1
- import { toRegExp } from 'oniguruma-to-es';
2
- import { J as JavaScriptScanner } from './shared/engine-javascript.hzpS1_41.mjs';
1
+ import { t as JavaScriptScanner } from "./scanner-BFcBmQR1.mjs";
2
+ import { toRegExp } from "oniguruma-to-es";
3
3
 
4
+ //#region src/engine-compile.ts
5
+ /**
6
+ * The default regex constructor for the JavaScript RegExp engine.
7
+ */
4
8
  function defaultJavaScriptRegexConstructor(pattern, options) {
5
- return toRegExp(
6
- pattern,
7
- {
8
- global: true,
9
- hasIndices: true,
10
- // This has no benefit for the standard JS engine, but it avoids a perf penalty for
11
- // precompiled grammars when constructing extremely long patterns that aren't always used
12
- lazyCompileLength: 3e3,
13
- rules: {
14
- // Needed since TextMate grammars merge backrefs across patterns
15
- allowOrphanBackrefs: true,
16
- // Improves search performance for generated regexes
17
- asciiWordBoundaries: true,
18
- // Follow `vscode-oniguruma` which enables this Oniguruma option by default
19
- captureGroup: true,
20
- // Oniguruma uses depth limit `20`; lowered here to keep regexes shorter and maybe
21
- // sometimes faster, but can be increased if issues reported due to low limit
22
- recursionLimit: 5,
23
- // Oniguruma option for `^`->`\A`, `$`->`\Z`; improves search performance without any
24
- // change in meaning since TM grammars search line by line
25
- singleline: true
26
- },
27
- ...options
28
- }
29
- );
9
+ return toRegExp(pattern, {
10
+ global: true,
11
+ hasIndices: true,
12
+ lazyCompileLength: 3e3,
13
+ rules: {
14
+ allowOrphanBackrefs: true,
15
+ asciiWordBoundaries: true,
16
+ captureGroup: true,
17
+ recursionLimit: 5,
18
+ singleline: true
19
+ },
20
+ ...options
21
+ });
30
22
  }
23
+ /**
24
+ * Use the modern JavaScript RegExp engine to implement the OnigScanner.
25
+ *
26
+ * As Oniguruma supports some features that can't be emulated using native JavaScript regexes, some
27
+ * patterns are not supported. Errors will be thrown when parsing TextMate grammars with
28
+ * unsupported patterns, and when the grammar includes patterns that use invalid Oniguruma syntax.
29
+ * Set `forgiving` to `true` to ignore these errors and skip any unsupported or invalid patterns.
30
+ */
31
31
  function createJavaScriptRegexEngine(options = {}) {
32
- const _options = Object.assign(
33
- {
34
- target: "auto",
35
- cache: /* @__PURE__ */ new Map()
36
- },
37
- options
38
- );
39
- _options.regexConstructor ||= (pattern) => defaultJavaScriptRegexConstructor(pattern, { target: _options.target });
40
- return {
41
- createScanner(patterns) {
42
- return new JavaScriptScanner(patterns, _options);
43
- },
44
- createString(s) {
45
- return {
46
- content: s
47
- };
48
- }
49
- };
32
+ const _options = Object.assign({
33
+ target: "auto",
34
+ cache: /* @__PURE__ */ new Map()
35
+ }, options);
36
+ _options.regexConstructor ||= (pattern) => defaultJavaScriptRegexConstructor(pattern, { target: _options.target });
37
+ return {
38
+ createScanner(patterns) {
39
+ return new JavaScriptScanner(patterns, _options);
40
+ },
41
+ createString(s) {
42
+ return { content: s };
43
+ }
44
+ };
50
45
  }
51
46
 
52
- export { createJavaScriptRegexEngine, defaultJavaScriptRegexConstructor };
47
+ //#endregion
48
+ export { createJavaScriptRegexEngine, defaultJavaScriptRegexConstructor };
@@ -1,5 +1,6 @@
1
- import { RegexEngine } from '@shikijs/types';
1
+ import { RegexEngine } from "@shikijs/types";
2
2
 
3
+ //#region src/engine-raw.d.ts
3
4
  /**
4
5
  * Raw JavaScript regex engine that only supports precompiled grammars.
5
6
  *
@@ -8,5 +9,5 @@ import { RegexEngine } from '@shikijs/types';
8
9
  * Zero dependencies.
9
10
  */
10
11
  declare function createJavaScriptRawEngine(): RegexEngine;
11
-
12
- export { createJavaScriptRawEngine };
12
+ //#endregion
13
+ export { createJavaScriptRawEngine };
@@ -1,22 +1,29 @@
1
- import { J as JavaScriptScanner } from './shared/engine-javascript.hzpS1_41.mjs';
1
+ import { t as JavaScriptScanner } from "./scanner-BFcBmQR1.mjs";
2
2
 
3
+ //#region src/engine-raw.ts
4
+ /**
5
+ * Raw JavaScript regex engine that only supports precompiled grammars.
6
+ *
7
+ * This further simplifies the engine by excluding the regex compilation step.
8
+ *
9
+ * Zero dependencies.
10
+ */
3
11
  function createJavaScriptRawEngine() {
4
- const options = {
5
- cache: /* @__PURE__ */ new Map(),
6
- regexConstructor: () => {
7
- throw new Error("JavaScriptRawEngine: only support precompiled grammar");
8
- }
9
- };
10
- return {
11
- createScanner(patterns) {
12
- return new JavaScriptScanner(patterns, options);
13
- },
14
- createString(s) {
15
- return {
16
- content: s
17
- };
18
- }
19
- };
12
+ const options = {
13
+ cache: /* @__PURE__ */ new Map(),
14
+ regexConstructor: () => {
15
+ throw new Error("JavaScriptRawEngine: only support precompiled grammar");
16
+ }
17
+ };
18
+ return {
19
+ createScanner(patterns) {
20
+ return new JavaScriptScanner(patterns, options);
21
+ },
22
+ createString(s) {
23
+ return { content: s };
24
+ }
25
+ };
20
26
  }
21
27
 
22
- export { createJavaScriptRawEngine };
28
+ //#endregion
29
+ export { createJavaScriptRawEngine };
package/dist/index.d.mts CHANGED
@@ -1,5 +1,4 @@
1
- export { J as JavaScriptRegexEngineOptions, a as JavaScriptRegexScannerOptions, b as JavaScriptScanner, c as createJavaScriptRegexEngine, d as defaultJavaScriptRegexConstructor } from './shared/engine-javascript.CDEDnU-m.mjs';
2
- export { createJavaScriptRawEngine } from './engine-raw.mjs';
3
- import '@shikijs/types';
4
- import 'oniguruma-to-es';
5
- import '@shikijs/vscode-textmate';
1
+ import { n as JavaScriptScanner, t as JavaScriptRegexScannerOptions } from "./scanner-CFS1MV4a.mjs";
2
+ import { JavaScriptRegexEngineOptions, createJavaScriptRegexEngine, defaultJavaScriptRegexConstructor } from "./engine-compile.mjs";
3
+ import { createJavaScriptRawEngine } from "./engine-raw.mjs";
4
+ export { JavaScriptRegexEngineOptions, JavaScriptRegexScannerOptions, JavaScriptScanner, createJavaScriptRawEngine, createJavaScriptRegexEngine, defaultJavaScriptRegexConstructor };
package/dist/index.mjs CHANGED
@@ -1,4 +1,5 @@
1
- export { createJavaScriptRegexEngine, defaultJavaScriptRegexConstructor } from './engine-compile.mjs';
2
- export { createJavaScriptRawEngine } from './engine-raw.mjs';
3
- export { J as JavaScriptScanner } from './shared/engine-javascript.hzpS1_41.mjs';
4
- import 'oniguruma-to-es';
1
+ import { t as JavaScriptScanner } from "./scanner-BFcBmQR1.mjs";
2
+ import { createJavaScriptRegexEngine, defaultJavaScriptRegexConstructor } from "./engine-compile.mjs";
3
+ import { createJavaScriptRawEngine } from "./engine-raw.mjs";
4
+
5
+ export { JavaScriptScanner, createJavaScriptRawEngine, createJavaScriptRegexEngine, defaultJavaScriptRegexConstructor };
@@ -0,0 +1,76 @@
1
+ //#region src/scanner.ts
2
+ const MAX = 4294967295;
3
+ var JavaScriptScanner = class {
4
+ regexps;
5
+ constructor(patterns, options = {}) {
6
+ this.patterns = patterns;
7
+ this.options = options;
8
+ const { forgiving = false, cache, regexConstructor } = options;
9
+ if (!regexConstructor) throw new Error("Option `regexConstructor` is not provided");
10
+ this.regexps = patterns.map((p) => {
11
+ if (typeof p !== "string") return p;
12
+ const cached = cache?.get(p);
13
+ if (cached) {
14
+ if (cached instanceof RegExp) return cached;
15
+ if (forgiving) return null;
16
+ throw cached;
17
+ }
18
+ try {
19
+ const regex = regexConstructor(p);
20
+ cache?.set(p, regex);
21
+ return regex;
22
+ } catch (e) {
23
+ cache?.set(p, e);
24
+ if (forgiving) return null;
25
+ throw e;
26
+ }
27
+ });
28
+ }
29
+ findNextMatchSync(string, startPosition, _options) {
30
+ const str = typeof string === "string" ? string : string.content;
31
+ const pending = [];
32
+ function toResult(index, match, offset = 0) {
33
+ return {
34
+ index,
35
+ captureIndices: match.indices.map((indice) => {
36
+ if (indice == null) return {
37
+ start: MAX,
38
+ end: MAX,
39
+ length: 0
40
+ };
41
+ return {
42
+ start: indice[0] + offset,
43
+ end: indice[1] + offset,
44
+ length: indice[1] - indice[0]
45
+ };
46
+ })
47
+ };
48
+ }
49
+ for (let i = 0; i < this.regexps.length; i++) {
50
+ const regexp = this.regexps[i];
51
+ if (!regexp) continue;
52
+ try {
53
+ regexp.lastIndex = startPosition;
54
+ const match = regexp.exec(str);
55
+ if (!match) continue;
56
+ if (match.index === startPosition) return toResult(i, match, 0);
57
+ pending.push([
58
+ i,
59
+ match,
60
+ 0
61
+ ]);
62
+ } catch (e) {
63
+ if (this.options.forgiving) continue;
64
+ throw e;
65
+ }
66
+ }
67
+ if (pending.length) {
68
+ const minIndex = Math.min(...pending.map((m) => m[1].index));
69
+ for (const [i, match, offset] of pending) if (match.index === minIndex) return toResult(i, match, offset);
70
+ }
71
+ return null;
72
+ }
73
+ };
74
+
75
+ //#endregion
76
+ export { JavaScriptScanner as t };
@@ -0,0 +1,31 @@
1
+ import { PatternScanner, RegexEngineString } from "@shikijs/types";
2
+ import { IOnigMatch } from "@shikijs/vscode-textmate";
3
+
4
+ //#region src/scanner.d.ts
5
+ interface JavaScriptRegexScannerOptions {
6
+ /**
7
+ * Whether to allow invalid regex patterns.
8
+ *
9
+ * @default false
10
+ */
11
+ forgiving?: boolean;
12
+ /**
13
+ * Cache for regex patterns.
14
+ */
15
+ cache?: Map<string, RegExp | Error> | null;
16
+ /**
17
+ * Custom pattern to RegExp constructor.
18
+ *
19
+ * By default `oniguruma-to-es` is used.
20
+ */
21
+ regexConstructor?: (pattern: string) => RegExp;
22
+ }
23
+ declare class JavaScriptScanner implements PatternScanner {
24
+ patterns: (string | RegExp)[];
25
+ options: JavaScriptRegexScannerOptions;
26
+ regexps: (RegExp | null)[];
27
+ constructor(patterns: (string | RegExp)[], options?: JavaScriptRegexScannerOptions);
28
+ findNextMatchSync(string: string | RegexEngineString, startPosition: number, _options: number): IOnigMatch | null;
29
+ }
30
+ //#endregion
31
+ export { JavaScriptScanner as n, JavaScriptRegexScannerOptions as t };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shikijs/engine-javascript",
3
3
  "type": "module",
4
- "version": "3.23.0",
4
+ "version": "4.0.0",
5
5
  "description": "Engine for Shiki using JavaScript's native RegExp",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -27,13 +27,16 @@
27
27
  "files": [
28
28
  "dist"
29
29
  ],
30
+ "engines": {
31
+ "node": ">=20"
32
+ },
30
33
  "dependencies": {
31
34
  "@shikijs/vscode-textmate": "^10.0.2",
32
35
  "oniguruma-to-es": "^4.3.4",
33
- "@shikijs/types": "3.23.0"
36
+ "@shikijs/types": "4.0.0"
34
37
  },
35
38
  "scripts": {
36
- "build": "unbuild",
37
- "dev": "unbuild --stub"
39
+ "build": "tsdown",
40
+ "dev": "tsdown --watch"
38
41
  }
39
42
  }
@@ -1,63 +0,0 @@
1
- import { PatternScanner, RegexEngineString, RegexEngine } from '@shikijs/types';
2
- import { ToRegExpOptions } from 'oniguruma-to-es';
3
- import { IOnigMatch } from '@shikijs/vscode-textmate';
4
-
5
- interface JavaScriptRegexScannerOptions {
6
- /**
7
- * Whether to allow invalid regex patterns.
8
- *
9
- * @default false
10
- */
11
- forgiving?: boolean;
12
- /**
13
- * Cache for regex patterns.
14
- */
15
- cache?: Map<string, RegExp | Error> | null;
16
- /**
17
- * Custom pattern to RegExp constructor.
18
- *
19
- * By default `oniguruma-to-es` is used.
20
- */
21
- regexConstructor?: (pattern: string) => RegExp;
22
- }
23
- declare class JavaScriptScanner implements PatternScanner {
24
- patterns: (string | RegExp)[];
25
- options: JavaScriptRegexScannerOptions;
26
- regexps: (RegExp | null)[];
27
- constructor(patterns: (string | RegExp)[], options?: JavaScriptRegexScannerOptions);
28
- findNextMatchSync(string: string | RegexEngineString, startPosition: number, _options: number): IOnigMatch | null;
29
- }
30
-
31
- interface JavaScriptRegexEngineOptions extends JavaScriptRegexScannerOptions {
32
- /**
33
- * The target ECMAScript version.
34
- *
35
- * Oniguruma-To-ES uses RegExp features from later versions of ECMAScript to add support for a
36
- * few more grammars. If using target `ES2024` or later, the RegExp `v` flag is used which
37
- * requires Node.js 20+ or Chrome 112+.
38
- * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicodeSets
39
- *
40
- * For maximum compatibility, you can set it to `ES2018` which uses the RegExp `u` flag.
41
- *
42
- * Set to `auto` to automatically detect the latest version supported by the environment.
43
- *
44
- * @default 'auto'
45
- */
46
- target?: 'auto' | 'ES2025' | 'ES2024' | 'ES2018';
47
- }
48
- /**
49
- * The default regex constructor for the JavaScript RegExp engine.
50
- */
51
- declare function defaultJavaScriptRegexConstructor(pattern: string, options?: ToRegExpOptions): RegExp;
52
- /**
53
- * Use the modern JavaScript RegExp engine to implement the OnigScanner.
54
- *
55
- * As Oniguruma supports some features that can't be emulated using native JavaScript regexes, some
56
- * patterns are not supported. Errors will be thrown when parsing TextMate grammars with
57
- * unsupported patterns, and when the grammar includes patterns that use invalid Oniguruma syntax.
58
- * Set `forgiving` to `true` to ignore these errors and skip any unsupported or invalid patterns.
59
- */
60
- declare function createJavaScriptRegexEngine(options?: JavaScriptRegexEngineOptions): RegexEngine;
61
-
62
- export { JavaScriptScanner as b, createJavaScriptRegexEngine as c, defaultJavaScriptRegexConstructor as d };
63
- export type { JavaScriptRegexEngineOptions as J, JavaScriptRegexScannerOptions as a };
@@ -1,93 +0,0 @@
1
- const MAX = 4294967295;
2
- class JavaScriptScanner {
3
- constructor(patterns, options = {}) {
4
- this.patterns = patterns;
5
- this.options = options;
6
- const {
7
- forgiving = false,
8
- cache,
9
- regexConstructor
10
- } = options;
11
- if (!regexConstructor) {
12
- throw new Error("Option `regexConstructor` is not provided");
13
- }
14
- this.regexps = patterns.map((p) => {
15
- if (typeof p !== "string") {
16
- return p;
17
- }
18
- const cached = cache?.get(p);
19
- if (cached) {
20
- if (cached instanceof RegExp) {
21
- return cached;
22
- }
23
- if (forgiving)
24
- return null;
25
- throw cached;
26
- }
27
- try {
28
- const regex = regexConstructor(p);
29
- cache?.set(p, regex);
30
- return regex;
31
- } catch (e) {
32
- cache?.set(p, e);
33
- if (forgiving)
34
- return null;
35
- throw e;
36
- }
37
- });
38
- }
39
- regexps;
40
- findNextMatchSync(string, startPosition, _options) {
41
- const str = typeof string === "string" ? string : string.content;
42
- const pending = [];
43
- function toResult(index, match, offset = 0) {
44
- return {
45
- index,
46
- captureIndices: match.indices.map((indice) => {
47
- if (indice == null) {
48
- return {
49
- start: MAX,
50
- end: MAX,
51
- length: 0
52
- };
53
- }
54
- return {
55
- start: indice[0] + offset,
56
- end: indice[1] + offset,
57
- length: indice[1] - indice[0]
58
- };
59
- })
60
- };
61
- }
62
- for (let i = 0; i < this.regexps.length; i++) {
63
- const regexp = this.regexps[i];
64
- if (!regexp)
65
- continue;
66
- try {
67
- regexp.lastIndex = startPosition;
68
- const match = regexp.exec(str);
69
- if (!match)
70
- continue;
71
- if (match.index === startPosition) {
72
- return toResult(i, match, 0);
73
- }
74
- pending.push([i, match, 0]);
75
- } catch (e) {
76
- if (this.options.forgiving)
77
- continue;
78
- throw e;
79
- }
80
- }
81
- if (pending.length) {
82
- const minIndex = Math.min(...pending.map((m) => m[1].index));
83
- for (const [i, match, offset] of pending) {
84
- if (match.index === minIndex) {
85
- return toResult(i, match, offset);
86
- }
87
- }
88
- }
89
- return null;
90
- }
91
- }
92
-
93
- export { JavaScriptScanner as J };