The best way to control Claude Code, Codex and more from any computer or phone

main
patch 87 lines 3 KB
Raw
1 diff --git a/dev/lib/parse.js b/dev/lib/parse.js
2 index ffb0f88..c63f0a3 100644
3 --- a/dev/lib/parse.js
4 +++ b/dev/lib/parse.js
5 @@ -1,6 +1,7 @@
6 /**
7 * @import {
8 * Create,
9 + * Extension,
10 * FullNormalizedExtension,
11 * InitialConstruct,
12 * ParseContext,
13 @@ -16,6 +17,11 @@ import {string, text} from './initialize/text.js'
14 import * as defaultConstructs from './constructs.js'
15 import {createTokenizer} from './create-tokenizer.js'
16
17 +/** @type {WeakMap<ReadonlyArray<Extension>, FullNormalizedExtension>} */
18 +const combined = new WeakMap()
19 +/** @type {Array<Extension>} */
20 +const noExtensions = []
21 +
22 /**
23 * @param {ParseOptions | null | undefined} [options]
24 * Configuration (optional).
25 @@ -24,9 +30,16 @@ import {createTokenizer} from './create-tokenizer.js'
26 */
27 export function parse(options) {
28 const settings = options || {}
29 - const constructs = /** @type {FullNormalizedExtension} */ (
30 - combineExtensions([defaultConstructs, ...(settings.extensions || [])])
31 - )
32 + const extensions = settings.extensions || noExtensions
33 + // Parsers are made per document; their constructs only depend on the extensions, which must
34 + // not change once parsed with (unified fills them while attaching plugins, before parsing).
35 + let constructs = combined.get(extensions)
36 + if (!constructs) {
37 + constructs = /** @type {FullNormalizedExtension} */ (
38 + combineExtensions([defaultConstructs, ...extensions])
39 + )
40 + combined.set(extensions, constructs)
41 + }
42
43 /** @type {ParseContext} */
44 const parser = {
45 diff --git a/lib/parse.js b/lib/parse.js
46 index 4682319..11062f2 100644
47 --- a/lib/parse.js
48 +++ b/lib/parse.js
49 @@ -1,6 +1,7 @@
50 /**
51 * @import {
52 * Create,
53 + * Extension,
54 * FullNormalizedExtension,
55 * InitialConstruct,
56 * ParseContext,
57 @@ -16,6 +17,11 @@ import { string, text } from './initialize/text.js';
58 import * as defaultConstructs from './constructs.js';
59 import { createTokenizer } from './create-tokenizer.js';
60
61 +/** @type {WeakMap<ReadonlyArray<Extension>, FullNormalizedExtension>} */
62 +const combined = new WeakMap();
63 +/** @type {Array<Extension>} */
64 +const noExtensions = [];
65 +
66 /**
67 * @param {ParseOptions | null | undefined} [options]
68 * Configuration (optional).
69 @@ -24,8 +30,16 @@ import { createTokenizer } from './create-tokenizer.js';
70 */
71 export function parse(options) {
72 const settings = options || {};
73 - const constructs = /** @type {FullNormalizedExtension} */
74 - combineExtensions([defaultConstructs, ...(settings.extensions || [])]);
75 + const extensions = settings.extensions || noExtensions;
76 + // Parsers are made per document; their constructs only depend on the extensions, which must
77 + // not change once parsed with (unified fills them while attaching plugins, before parsing).
78 + let constructs = combined.get(extensions);
79 + if (!constructs) {
80 + constructs = /** @type {FullNormalizedExtension} */ (
81 + combineExtensions([defaultConstructs, ...extensions])
82 + );
83 + combined.set(extensions, constructs);
84 + }
85
86 /** @type {ParseContext} */
87 const parser = {