@bascik/language-server 0.1.0-rc.5 → 0.1.0-rc.6

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.
Files changed (115) hide show
  1. package/README.md +72 -30
  2. package/dist/analyzer.d.ts +70 -1
  3. package/dist/analyzer.d.ts.map +1 -1
  4. package/dist/analyzer.js +625 -75
  5. package/dist/analyzer.js.map +1 -1
  6. package/dist/api-rules.d.ts +7 -0
  7. package/dist/api-rules.d.ts.map +1 -1
  8. package/dist/api-rules.js +235 -16
  9. package/dist/api-rules.js.map +1 -1
  10. package/dist/bin.js +4 -1
  11. package/dist/bin.js.map +1 -1
  12. package/dist/check.d.ts +14 -1
  13. package/dist/check.d.ts.map +1 -1
  14. package/dist/check.js +116 -10
  15. package/dist/check.js.map +1 -1
  16. package/dist/cli.d.ts.map +1 -1
  17. package/dist/cli.js +27 -8
  18. package/dist/cli.js.map +1 -1
  19. package/dist/compatibility-rules.json +21 -3
  20. package/dist/component-metadata.d.ts.map +1 -1
  21. package/dist/component-metadata.js +3 -1
  22. package/dist/component-metadata.js.map +1 -1
  23. package/dist/component-usages.d.ts +47 -0
  24. package/dist/component-usages.d.ts.map +1 -0
  25. package/dist/component-usages.js +495 -0
  26. package/dist/component-usages.js.map +1 -0
  27. package/dist/config-reader.d.ts +41 -0
  28. package/dist/config-reader.d.ts.map +1 -0
  29. package/dist/config-reader.js +461 -0
  30. package/dist/config-reader.js.map +1 -0
  31. package/dist/config.d.ts +10 -0
  32. package/dist/config.d.ts.map +1 -1
  33. package/dist/config.js +25 -30
  34. package/dist/config.js.map +1 -1
  35. package/dist/hand-written-escaper.d.ts +35 -0
  36. package/dist/hand-written-escaper.d.ts.map +1 -0
  37. package/dist/hand-written-escaper.js +739 -0
  38. package/dist/hand-written-escaper.js.map +1 -0
  39. package/dist/module-specifiers.d.ts +25 -0
  40. package/dist/module-specifiers.d.ts.map +1 -1
  41. package/dist/module-specifiers.js +234 -165
  42. package/dist/module-specifiers.js.map +1 -1
  43. package/dist/name-groups.d.ts +36 -0
  44. package/dist/name-groups.d.ts.map +1 -0
  45. package/dist/name-groups.js +198 -0
  46. package/dist/name-groups.js.map +1 -0
  47. package/dist/plain-holes.d.ts +116 -0
  48. package/dist/plain-holes.d.ts.map +1 -0
  49. package/dist/plain-holes.js +1970 -0
  50. package/dist/plain-holes.js.map +1 -0
  51. package/dist/project.d.ts +58 -1
  52. package/dist/project.d.ts.map +1 -1
  53. package/dist/project.js +146 -8
  54. package/dist/project.js.map +1 -1
  55. package/dist/references.d.ts +47 -0
  56. package/dist/references.d.ts.map +1 -0
  57. package/dist/references.js +241 -0
  58. package/dist/references.js.map +1 -0
  59. package/dist/script-modules.d.ts +60 -0
  60. package/dist/script-modules.d.ts.map +1 -0
  61. package/dist/script-modules.js +382 -0
  62. package/dist/script-modules.js.map +1 -0
  63. package/dist/server-script-rules.d.ts +94 -1
  64. package/dist/server-script-rules.d.ts.map +1 -1
  65. package/dist/server-script-rules.js +2572 -230
  66. package/dist/server-script-rules.js.map +1 -1
  67. package/dist/server-script-templates.d.ts +250 -0
  68. package/dist/server-script-templates.d.ts.map +1 -0
  69. package/dist/server-script-templates.js +1685 -0
  70. package/dist/server-script-templates.js.map +1 -0
  71. package/dist/server.d.ts +191 -3
  72. package/dist/server.d.ts.map +1 -1
  73. package/dist/server.js +822 -50
  74. package/dist/server.js.map +1 -1
  75. package/dist/start.d.ts +10 -1
  76. package/dist/start.d.ts.map +1 -1
  77. package/dist/start.js +14 -2
  78. package/dist/start.js.map +1 -1
  79. package/dist/string-typed-flow.d.ts +26 -0
  80. package/dist/string-typed-flow.d.ts.map +1 -0
  81. package/dist/string-typed-flow.js +496 -0
  82. package/dist/string-typed-flow.js.map +1 -0
  83. package/dist/typescript-blocks.d.ts +205 -0
  84. package/dist/typescript-blocks.d.ts.map +1 -0
  85. package/dist/typescript-blocks.js +464 -0
  86. package/dist/typescript-blocks.js.map +1 -0
  87. package/dist/typescript-editor-mode.d.ts +28 -0
  88. package/dist/typescript-editor-mode.d.ts.map +1 -0
  89. package/dist/typescript-editor-mode.js +161 -0
  90. package/dist/typescript-editor-mode.js.map +1 -0
  91. package/dist/typescript-engine.d.ts +115 -0
  92. package/dist/typescript-engine.d.ts.map +1 -0
  93. package/dist/typescript-engine.js +286 -0
  94. package/dist/typescript-engine.js.map +1 -0
  95. package/dist/typescript-features.d.ts +136 -0
  96. package/dist/typescript-features.d.ts.map +1 -0
  97. package/dist/typescript-features.js +374 -0
  98. package/dist/typescript-features.js.map +1 -0
  99. package/dist/typescript-loader.d.ts +55 -0
  100. package/dist/typescript-loader.d.ts.map +1 -0
  101. package/dist/typescript-loader.js +202 -0
  102. package/dist/typescript-loader.js.map +1 -0
  103. package/dist/typescript-lsp.d.ts +8 -0
  104. package/dist/typescript-lsp.d.ts.map +1 -0
  105. package/dist/typescript-lsp.js +115 -0
  106. package/dist/typescript-lsp.js.map +1 -0
  107. package/dist/typescript-service.d.ts +350 -0
  108. package/dist/typescript-service.d.ts.map +1 -0
  109. package/dist/typescript-service.js +1465 -0
  110. package/dist/typescript-service.js.map +1 -0
  111. package/dist/typescript-worker.d.ts +11 -0
  112. package/dist/typescript-worker.d.ts.map +1 -0
  113. package/dist/typescript-worker.js +107 -0
  114. package/dist/typescript-worker.js.map +1 -0
  115. package/package.json +20 -7
@@ -0,0 +1,198 @@
1
+ /**
2
+ * `shared-name-group`: name groups a component holds whole, and how many times a page renders the component.
3
+ *
4
+ * `name` is a page-level contract (WHATWG HTML): radios with one name and one form owner (or none, in one tree)
5
+ * are one radio group, `<details>` with one name are one exclusive accordion, and `usemap="#x"` uses the first
6
+ * `<map name="x">` in the tree. Bascik ships `name` as written unless a project opts in to
7
+ * `scoping.attributes.name`, so a component that holds a whole group, used twice on a page, makes one group of
8
+ * both copies.
9
+ *
10
+ * A group is held whole when the component has two or more radios (outside a `<form>` of its own, with no
11
+ * `form` attribute) or two or more `<details>` with one literal name, or any `<map name>`. One radio or one
12
+ * `<details>` per component is the opposite case, a group built from many instances, and is left alone. A name
13
+ * bound to a prop (`data-bascik-attr-name`) differs per copy and is left alone too.
14
+ */
15
+ import * as path from "node:path";
16
+ import { findComponentUsages } from "./component-usages.js";
17
+ import { scanHtmlTags } from "./references.js";
18
+ import { isInsideComponentRoots, isUsageSourcePath, } from "./project.js";
19
+ const isAttributeSpace = (code) => code === 9 || code === 10 || code === 12 || code === 13 || code === 32;
20
+ /** The attributes of the open tag whose attribute part runs from `from` to its `>` at `end - 1`, as a browser reads them. */
21
+ function readAttributes(html, from, end) {
22
+ const attributes = [];
23
+ const stop = end - 1;
24
+ let at = from;
25
+ while (at < stop) {
26
+ const code = html.charCodeAt(at);
27
+ if (isAttributeSpace(code) || code === 47 /* / */) {
28
+ at++;
29
+ continue;
30
+ }
31
+ const start = at;
32
+ // An attribute name starts with any character, `=` included, and runs to space, `/`, `>` or `=`.
33
+ at++;
34
+ while (at < stop) {
35
+ const c = html.charCodeAt(at);
36
+ if (isAttributeSpace(c) || c === 47 || c === 61 /* = */)
37
+ break;
38
+ at++;
39
+ }
40
+ const name = html.slice(start, at).toLowerCase();
41
+ let cursor = at;
42
+ while (cursor < stop && isAttributeSpace(html.charCodeAt(cursor)))
43
+ cursor++;
44
+ let value;
45
+ if (cursor < stop && html.charCodeAt(cursor) === 61) {
46
+ cursor++;
47
+ while (cursor < stop && isAttributeSpace(html.charCodeAt(cursor)))
48
+ cursor++;
49
+ const quote = html[cursor];
50
+ if (quote === '"' || quote === "'") {
51
+ const close = html.indexOf(quote, cursor + 1);
52
+ const valueEnd = close === -1 || close > stop ? stop : close;
53
+ value = html.slice(cursor + 1, valueEnd);
54
+ cursor = Math.min(valueEnd + 1, stop);
55
+ }
56
+ else {
57
+ const valueStart = cursor;
58
+ while (cursor < stop && !isAttributeSpace(html.charCodeAt(cursor)))
59
+ cursor++;
60
+ value = html.slice(valueStart, cursor);
61
+ }
62
+ at = cursor;
63
+ }
64
+ attributes.push({ name, value, start, end: at });
65
+ }
66
+ return attributes;
67
+ }
68
+ /** The first attribute named `name`: a browser keeps the first of duplicates. */
69
+ const attributeNamed = (attributes, name) => attributes.find((attribute) => attribute.name === name);
70
+ /**
71
+ * The name groups `html` (a component's source) holds whole, in source order of their first element. Tags in
72
+ * comments and in `<script>`, `<style>` and `<textarea>` bodies do not count.
73
+ */
74
+ export function findNameGroups(html) {
75
+ if (!/\bname\s*=/i.test(html))
76
+ return [];
77
+ const groups = new Map();
78
+ let formDepth = 0;
79
+ for (const tag of scanHtmlTags(html)) {
80
+ const tagName = tag.name.toLowerCase();
81
+ if (tagName === "form") {
82
+ formDepth = tag.closing ? Math.max(0, formDepth - 1) : formDepth + 1;
83
+ continue;
84
+ }
85
+ if (tag.closing ||
86
+ (tagName !== "input" && tagName !== "details" && tagName !== "map"))
87
+ continue;
88
+ const attributes = readAttributes(html, tag.nameStart + tag.name.length, tag.end);
89
+ // A bound name differs per copy (the binding wins over a literal name).
90
+ if (attributeNamed(attributes, "data-bascik-attr-name"))
91
+ continue;
92
+ const nameAttribute = attributeNamed(attributes, "name");
93
+ const name = nameAttribute?.value;
94
+ if (!nameAttribute || !name)
95
+ continue;
96
+ let kind;
97
+ if (tagName === "input") {
98
+ const type = attributeNamed(attributes, "type")?.value;
99
+ if (type?.trim().toLowerCase() !== "radio")
100
+ continue;
101
+ // Inside the component's own form, each copy's radios have their own form owner. A `form` attribute
102
+ // ties the radio to a form elsewhere, which this file cannot judge.
103
+ if (formDepth > 0 || attributeNamed(attributes, "form"))
104
+ continue;
105
+ kind = "radio";
106
+ }
107
+ else {
108
+ kind = tagName === "details" ? "details" : "map";
109
+ }
110
+ const key = `${kind}\u0000${name}`;
111
+ const group = groups.get(key);
112
+ if (group)
113
+ group.count++;
114
+ else
115
+ groups.set(key, {
116
+ kind,
117
+ name,
118
+ count: 1,
119
+ attributeStart: nameAttribute.start,
120
+ attributeEnd: nameAttribute.end,
121
+ });
122
+ }
123
+ return [...groups.values()].filter((group) => group.kind === "map" || group.count >= 2);
124
+ }
125
+ /**
126
+ * The page that renders `componentName` most often, counting instances through nested components (once in a
127
+ * wrapper a page uses twice is twice), with open buffers' unsaved text in place of the files on disk. Pages
128
+ * are the HTML the project tracks outside the component roots. Undefined when no page renders it.
129
+ */
130
+ export function countInstancesOnPages(componentName, snapshot, openDocumentTexts) {
131
+ const target = componentName.toLowerCase();
132
+ const isComponentFile = (file) => isInsideComponentRoots(file.replace(/\\/g, "/"), snapshot.componentRoots);
133
+ const pages = new Map();
134
+ for (const [file, html] of snapshot.htmlUsageByFile)
135
+ if (!isComponentFile(file))
136
+ pages.set(file, html);
137
+ const openText = new Map();
138
+ for (const { fsPath, text } of openDocumentTexts) {
139
+ openText.set(fsPath, text);
140
+ if (fsPath.toLowerCase().endsWith(".html") &&
141
+ !isComponentFile(fsPath) &&
142
+ (pages.has(fsPath) || isUsageSourcePath(fsPath, snapshot)))
143
+ pages.set(fsPath, text);
144
+ }
145
+ const names = new Set(snapshot.componentMap.keys());
146
+ const sourceOf = (name) => {
147
+ const file = snapshot.componentMap.get(name);
148
+ if (!file)
149
+ return undefined;
150
+ return openText.get(file) ?? snapshot.componentSourceByFile?.get(file);
151
+ };
152
+ // Instances of the target one instance of `name` renders. A component reached again while it is being
153
+ // counted (components that use each other) adds nothing: the compiler stops such a loop at its limits.
154
+ const within = new Map();
155
+ const visiting = new Set();
156
+ const instancesWithin = (name) => {
157
+ const known = within.get(name);
158
+ if (known !== undefined)
159
+ return known;
160
+ if (visiting.has(name))
161
+ return 0;
162
+ const source = sourceOf(name);
163
+ if (source === undefined)
164
+ return 0;
165
+ visiting.add(name);
166
+ let count = 0;
167
+ for (const usage of findComponentUsages(source, names))
168
+ count += usage.name === target ? 1 : instancesWithin(usage.name);
169
+ visiting.delete(name);
170
+ within.set(name, count);
171
+ return count;
172
+ };
173
+ let best;
174
+ for (const [page, html] of [...pages].sort(([a], [b]) => a.localeCompare(b))) {
175
+ let count = 0;
176
+ for (const usage of findComponentUsages(html, names))
177
+ count += usage.name === target ? 1 : instancesWithin(usage.name);
178
+ if (count > 0 && (!best || count > best.count))
179
+ best = { page, count };
180
+ }
181
+ return best;
182
+ }
183
+ const PROP_FIX = 'give each copy its own name through a prop: data-bascik-attr-name="group" in place of name here, and data-bascik-prop-group="..." with a different value on each';
184
+ /** The warning for `group` in `component`, which `instances.page` renders `instances.count` times. */
185
+ export function sharedNameGroupMessage(group, component, instances, projectRoot) {
186
+ const page = path.relative(projectRoot, instances.page).replace(/\\/g, "/");
187
+ const used = `<${component}> is used ${instances.count} times on ${page}`;
188
+ const name = `name="${group.name}"`;
189
+ switch (group.kind) {
190
+ case "radio":
191
+ return `${name} is on ${group.count} radios with no <form> of their own, and ${used}, so the copies act as one radio group on the page: choosing in one copy clears the others. Wrap the radios in the component's own <form>, or ${PROP_FIX} <${component}>.`;
192
+ case "details":
193
+ return `${name} is on ${group.count} <details>, and ${used}, so the copies act as one exclusive accordion on the page: opening an item in one copy closes the open item in the others. To give each copy its own group, ${PROP_FIX} <${component}>. For one accordion across the copies on purpose, ignore this with bascik-ignore shared-name-group.`;
194
+ case "map":
195
+ return `<map ${name}> is declared once per copy, and ${used}, so the page has ${instances.count} maps named "${group.name}" and every usemap="#${group.name}" uses the first. Give each copy its own map name through props (data-bascik-attr-name on the <map>, data-bascik-attr-usemap on the <img>), or move the map out of the component.`;
196
+ }
197
+ }
198
+ //# sourceMappingURL=name-groups.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"name-groups.js","sourceRoot":"","sources":["../src/name-groups.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EACL,sBAAsB,EACtB,iBAAiB,GAElB,MAAM,cAAc,CAAC;AAwBtB,MAAM,gBAAgB,GAAG,CAAC,IAAY,E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
+ * in html`...` only escapes text and never markup someone meant (the
4
+ * allowlist behind the server-script-unescaped-markup quick fix).
5
+ *
6
+ * A hole is plain when every value it reads comes from these sources,
7
+ * directly or through local `const` bindings of them, string concatenation
8
+ * or templates of them, String(), Number() and the like, and string methods
9
+ * (toFixed, toUpperCase, trim, slice, padStart and similar) on them:
10
+ *
11
+ * - string, number, bigint and boolean literals with no `&` or `<` in
12
+ * their value (an escape such as `\x3c` or `\u0026` counts by the
13
+ * character it writes), and `const` objects or arrays of only those that
14
+ * the file reads by a literal key or index and never changes, exports or
15
+ * passes on;
16
+ * - request data: the `request` parameter, `context.params` and
17
+ * `context.remoteIp`, in request-time code (and `const` values taken
18
+ * from them, and a URL or URLSearchParams made from them), while the
19
+ * script neither changes it (headers.set, a params write) nor hands a
20
+ * request object to other code;
21
+ * - `process.env` values;
22
+ * - `Date` and `Date.now()`-derived values, and `Math` results;
23
+ * - parameters typed number, bigint or boolean (or a union of those) of a
24
+ * local function that is not exported and is only called by name, that
25
+ * the file never reassigns or casts with `as`, and that every call and
26
+ * the default value fill with a provable number;
27
+ * - local functions that are not exported, are only called by name, and
28
+ * whose every return is plain itself;
29
+ * - html values (an html`...` template, raw(), safeUrl(), json()), which
30
+ * html inserts as they are, while nothing turns them into text (String(),
31
+ * a string method, + or a template).
32
+ *
33
+ * Every argument of a call in the chain (a string method, a Date or
34
+ * request method, optional calls too) must be plain itself.
35
+ *
36
+ * Anything else is unknown: object fields and array slots other code can
37
+ * change, Map and Set reads, class fields, closures, catch and loop
38
+ * bindings, globalThis, destructuring targets, imported values, `let` and
39
+ * `var`, parameters of any other type, and parameters of exported
40
+ * functions, callbacks and methods (a JavaScript caller can pass markup
41
+ * whatever the type says).
42
+ *
43
+ * The built-ins are trusted only while the file leaves them alone: a file
44
+ * that declares its own String, Math, Date, process and the like, writes to
45
+ * one of them or to process.env, hands one to other code (Object.assign,
46
+ * Object.defineProperty), writes through globalThis, or reaches a prototype
47
+ * gets no fix for those reads (for a prototype, for any hole). Code that
48
+ * rewrites built-ins or globals is out of scope: a file that calls eval,
49
+ * Function, Object.defineProperty, Reflect.defineProperty, Reflect.set or
50
+ * Object.setPrototypeOf gets no fix for any hole.
51
+ *
52
+ * Names are merged across scopes: a name declared anywhere in a way that
53
+ * is not plain is unknown wherever it is read. Each name, `const` value
54
+ * and helper is judged once, so a file's checks stay linear in its tokens.
55
+ */
56
+ import type { Token } from "./module-specifiers.js";
57
+ /** Why a hole is not provably plain: the value it reads and why. */
58
+ export interface Unproven {
59
+ what: string;
60
+ why: string;
61
+ }
62
+ /** A local function as localFunctions finds it. */
63
+ export interface PlainFunction {
64
+ name: string;
65
+ /** The name's offset. */
66
+ start: number;
67
+ end: number;
68
+ /** The body's first offset: its `{`, or an arrow's expression. */
69
+ body: number;
70
+ /** An arrow whose body is an expression, not a block. */
71
+ expression: boolean;
72
+ }
73
+ export interface PlainOptions {
74
+ /** Request-time code: `request` and `context` parameters hold request data. */
75
+ requestTime: boolean;
76
+ functions: readonly PlainFunction[];
77
+ /** Template holes, as [first code offset, end offset) pairs. */
78
+ holes: readonly (readonly [number, number])[];
79
+ /** Whether markup is built as a string between two offsets. */
80
+ markupWithin: (from: number, to: number) => boolean;
81
+ /**
82
+ * Whether an untagged template literal whose text holds `&` or `<` (see
83
+ * holdsMarkupCharacter) starts between two offsets. A template with holes
84
+ * leaves no token for its text, so this is how its text is seen.
85
+ */
86
+ markupCharacterTextWithin?: (from: number, to: number) => boolean;
87
+ /**
88
+ * Whether an untagged (or plain-string tagged) template literal starts
89
+ * between two offsets: an html value in its hole turns into text.
90
+ */
91
+ plainTemplateWithin?: (from: number, to: number) => boolean;
92
+ /** html tag names and html, raw, safeUrl and json as the script imports them. */
93
+ isHtmlValue: (name: string) => boolean;
94
+ htmlTags: readonly string[];
95
+ /** The end offset of the template literal whose backtick is at an offset. */
96
+ templateEndAt: (start: number) => number | undefined;
97
+ }
98
+ /**
99
+ * Whether a string or template literal's value, its escapes decoded,
100
+ * holds `&` or `<`: html would escape it, so an entity (`&mdash;`) or a
101
+ * tag written as `\x3cb>` would show as text after wrapping.
102
+ */
103
+ export declare function holdsMarkupCharacter(raw: string): boolean;
104
+ /**
105
+ * Whether the text between tokens of a number holds only digits, a radix or exponent, white space and
106
+ * comments (and, with `signs`, + and -). A hand-written scan: a regular expression with comments in a
107
+ * repeated group backtracks exponentially on a run of slashes.
108
+ */
109
+ export declare function isNumberText(text: string, signs?: boolean): boolean;
110
+ /**
111
+ * Builds the checker for one script: `(from, to)` gives undefined when the
112
+ * code between the offsets (a hole's expression) is provably plain data,
113
+ * or what it reads that is not.
114
+ */
115
+ export declare function plainHoleChecker(body: string, tokens: readonly Token[], opts: PlainOptions): (from: number, to: number) => Unproven | undefined;
116
+ //# sourceMappingURL=plain-holes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plain-holes.d.ts","sourceRoot":"","sources":["../src/plain-holes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsDG;AACH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAC;AAEpD,oEAAoE;AACpE,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;CACb;AAED,mDAAmD;AACnD,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,yBAAyB;IACzB,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,kEAAkE;IAClE,IAAI,EAAE,MAAM,CAAC;IACb,yDAAyD;IACzD,UAAU,EAAE,OAAO,CAAC;CACrB;AAED,MAAM,WAAW,YAAY;IAC3B,+EAA+E;IAC/E,WAAW,EAAE,OAAO,CAAC;IACrB,SAAS,EAAE,SAAS,aAAa,EAAE,CAAC;IACpC,gEAAgE;IAChE,KAAK,EAAE,SAAS,CAAC,SAAS,CAAC,MAAM,EAAE,M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