baseguard 1.0.2 → 1.0.4

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 (169) hide show
  1. package/.baseguardrc.example.json +63 -63
  2. package/.eslintrc.json +24 -24
  3. package/.prettierrc +7 -7
  4. package/CHANGELOG.md +195 -195
  5. package/DEPLOYMENT.md +624 -624
  6. package/DEPLOYMENT_CHECKLIST.md +239 -239
  7. package/DEPLOYMENT_SUMMARY_v1.0.2.md +202 -202
  8. package/QUICK_START.md +134 -134
  9. package/README.md +488 -488
  10. package/RELEASE_NOTES_v1.0.2.md +434 -434
  11. package/bin/base.js +628 -613
  12. package/dist/ai/fix-manager.d.ts.map +1 -1
  13. package/dist/ai/fix-manager.js +1 -1
  14. package/dist/ai/fix-manager.js.map +1 -1
  15. package/dist/ai/gemini-analyzer.d.ts.map +1 -1
  16. package/dist/ai/gemini-analyzer.js +29 -35
  17. package/dist/ai/gemini-analyzer.js.map +1 -1
  18. package/dist/ai/gemini-code-fixer.d.ts.map +1 -1
  19. package/dist/ai/gemini-code-fixer.js +58 -58
  20. package/dist/ai/gemini-code-fixer.js.map +1 -1
  21. package/dist/ai/jules-implementer.d.ts +3 -0
  22. package/dist/ai/jules-implementer.d.ts.map +1 -1
  23. package/dist/ai/jules-implementer.js +63 -32
  24. package/dist/ai/jules-implementer.js.map +1 -1
  25. package/dist/ai/unified-code-fixer.js.map +1 -1
  26. package/dist/commands/check.d.ts.map +1 -1
  27. package/dist/commands/check.js +1 -1
  28. package/dist/commands/check.js.map +1 -1
  29. package/dist/commands/config.js +2 -1
  30. package/dist/commands/config.js.map +1 -1
  31. package/dist/commands/fix.d.ts.map +1 -1
  32. package/dist/commands/fix.js +44 -15
  33. package/dist/commands/fix.js.map +1 -1
  34. package/dist/core/api-key-manager.js +2 -2
  35. package/dist/core/api-key-manager.js.map +1 -1
  36. package/dist/core/baseguard.d.ts +1 -0
  37. package/dist/core/baseguard.d.ts.map +1 -1
  38. package/dist/core/baseguard.js +13 -10
  39. package/dist/core/baseguard.js.map +1 -1
  40. package/dist/core/baseline-checker.d.ts.map +1 -1
  41. package/dist/core/baseline-checker.js +2 -1
  42. package/dist/core/baseline-checker.js.map +1 -1
  43. package/dist/core/configuration-recovery.d.ts.map +1 -1
  44. package/dist/core/configuration-recovery.js +1 -1
  45. package/dist/core/configuration-recovery.js.map +1 -1
  46. package/dist/core/debug-logger.d.ts.map +1 -1
  47. package/dist/core/debug-logger.js +1 -1
  48. package/dist/core/debug-logger.js.map +1 -1
  49. package/dist/core/error-handler.d.ts.map +1 -1
  50. package/dist/core/error-handler.js +2 -1
  51. package/dist/core/error-handler.js.map +1 -1
  52. package/dist/core/gitignore-manager.js +5 -5
  53. package/dist/core/graceful-degradation-manager.d.ts.map +1 -1
  54. package/dist/core/graceful-degradation-manager.js +16 -16
  55. package/dist/core/graceful-degradation-manager.js.map +1 -1
  56. package/dist/core/lazy-loader.d.ts.map +1 -1
  57. package/dist/core/lazy-loader.js +9 -2
  58. package/dist/core/lazy-loader.js.map +1 -1
  59. package/dist/core/memory-manager.d.ts +0 -3
  60. package/dist/core/memory-manager.d.ts.map +1 -1
  61. package/dist/core/memory-manager.js.map +1 -1
  62. package/dist/core/parser-worker.d.ts +2 -0
  63. package/dist/core/parser-worker.d.ts.map +1 -0
  64. package/dist/core/parser-worker.js +19 -0
  65. package/dist/core/parser-worker.js.map +1 -0
  66. package/dist/core/startup-optimizer.d.ts +2 -0
  67. package/dist/core/startup-optimizer.d.ts.map +1 -1
  68. package/dist/core/startup-optimizer.js +19 -12
  69. package/dist/core/startup-optimizer.js.map +1 -1
  70. package/dist/core/system-error-handler.d.ts.map +1 -1
  71. package/dist/core/system-error-handler.js +18 -11
  72. package/dist/core/system-error-handler.js.map +1 -1
  73. package/dist/git/automation-engine.d.ts.map +1 -1
  74. package/dist/git/automation-engine.js +5 -4
  75. package/dist/git/automation-engine.js.map +1 -1
  76. package/dist/git/github-manager.d.ts.map +1 -1
  77. package/dist/git/github-manager.js.map +1 -1
  78. package/dist/git/hook-manager.js +5 -5
  79. package/dist/git/hook-manager.js.map +1 -1
  80. package/dist/parsers/parser-manager.d.ts.map +1 -1
  81. package/dist/parsers/parser-manager.js +1 -1
  82. package/dist/parsers/parser-manager.js.map +1 -1
  83. package/dist/parsers/svelte-parser.js +1 -1
  84. package/dist/parsers/svelte-parser.js.map +1 -1
  85. package/dist/parsers/vanilla-parser.d.ts.map +1 -1
  86. package/dist/parsers/vanilla-parser.js.map +1 -1
  87. package/dist/parsers/vue-parser.d.ts.map +1 -1
  88. package/dist/parsers/vue-parser.js.map +1 -1
  89. package/dist/ui/components.d.ts +1 -1
  90. package/dist/ui/components.d.ts.map +1 -1
  91. package/dist/ui/components.js +11 -11
  92. package/dist/ui/components.js.map +1 -1
  93. package/dist/ui/terminal-header.js +14 -14
  94. package/package.json +105 -105
  95. package/src/ai/__tests__/gemini-analyzer.test.ts +180 -180
  96. package/src/ai/agentkit-orchestrator.ts +533 -533
  97. package/src/ai/fix-manager.ts +362 -362
  98. package/src/ai/gemini-analyzer.ts +665 -671
  99. package/src/ai/gemini-code-fixer.ts +539 -540
  100. package/src/ai/index.ts +3 -3
  101. package/src/ai/jules-implementer.ts +504 -460
  102. package/src/ai/unified-code-fixer.ts +347 -347
  103. package/src/commands/automation.ts +343 -343
  104. package/src/commands/check.ts +298 -299
  105. package/src/commands/config.ts +584 -583
  106. package/src/commands/fix.ts +264 -238
  107. package/src/commands/index.ts +6 -6
  108. package/src/commands/init.ts +155 -155
  109. package/src/commands/status.ts +306 -306
  110. package/src/core/api-key-manager.ts +298 -298
  111. package/src/core/baseguard.ts +757 -756
  112. package/src/core/baseline-checker.ts +564 -563
  113. package/src/core/cache-manager.ts +271 -271
  114. package/src/core/configuration-recovery.ts +672 -673
  115. package/src/core/configuration.ts +595 -595
  116. package/src/core/debug-logger.ts +590 -590
  117. package/src/core/directory-filter.ts +420 -420
  118. package/src/core/error-handler.ts +518 -517
  119. package/src/core/file-processor.ts +337 -337
  120. package/src/core/gitignore-manager.ts +168 -168
  121. package/src/core/graceful-degradation-manager.ts +596 -596
  122. package/src/core/index.ts +16 -16
  123. package/src/core/lazy-loader.ts +317 -307
  124. package/src/core/memory-manager.ts +290 -295
  125. package/src/core/parser-worker.ts +33 -0
  126. package/src/core/startup-optimizer.ts +246 -243
  127. package/src/core/system-error-handler.ts +755 -750
  128. package/src/git/automation-engine.ts +361 -361
  129. package/src/git/github-manager.ts +190 -192
  130. package/src/git/hook-manager.ts +210 -210
  131. package/src/git/index.ts +3 -3
  132. package/src/index.ts +7 -7
  133. package/src/parsers/feature-validator.ts +558 -558
  134. package/src/parsers/index.ts +7 -7
  135. package/src/parsers/parser-manager.ts +418 -419
  136. package/src/parsers/parser.ts +25 -25
  137. package/src/parsers/react-parser-optimized.ts +160 -160
  138. package/src/parsers/react-parser.ts +358 -358
  139. package/src/parsers/svelte-parser.ts +510 -510
  140. package/src/parsers/vanilla-parser.ts +685 -686
  141. package/src/parsers/vue-parser.ts +476 -478
  142. package/src/types/index.ts +95 -95
  143. package/src/ui/components.ts +567 -567
  144. package/src/ui/help.ts +192 -192
  145. package/src/ui/index.ts +3 -3
  146. package/src/ui/prompts.ts +680 -680
  147. package/src/ui/terminal-header.ts +58 -58
  148. package/test-build.js +40 -40
  149. package/test-config-commands.js +55 -55
  150. package/test-header-simple.js +32 -32
  151. package/test-terminal-header.js +11 -11
  152. package/test-ui.js +28 -28
  153. package/tests/e2e/baseguard.e2e.test.ts +515 -515
  154. package/tests/e2e/cross-platform.e2e.test.ts +419 -419
  155. package/tests/e2e/git-integration.e2e.test.ts +486 -486
  156. package/tests/fixtures/react-project/package.json +13 -13
  157. package/tests/fixtures/react-project/src/App.css +75 -75
  158. package/tests/fixtures/react-project/src/App.tsx +76 -76
  159. package/tests/fixtures/svelte-project/package.json +10 -10
  160. package/tests/fixtures/svelte-project/src/App.svelte +368 -368
  161. package/tests/fixtures/vanilla-project/index.html +75 -75
  162. package/tests/fixtures/vanilla-project/script.js +330 -330
  163. package/tests/fixtures/vanilla-project/styles.css +358 -358
  164. package/tests/fixtures/vue-project/package.json +11 -11
  165. package/tests/fixtures/vue-project/src/App.vue +215 -215
  166. package/tsconfig.json +34 -34
  167. package/vitest.config.ts +11 -11
  168. package/dist/terminal-header.d.ts +0 -12
  169. package/dist/terminal-header.js +0 -45
@@ -1,510 +1,510 @@
1
- import { Parser } from './parser.js';
2
- import type { DetectedFeature } from '../types/index.js';
3
- import { LazyLoader } from '../core/lazy-loader.js';
4
- import { parse as parseBabel } from '@babel/parser';
5
- import traverse from '@babel/traverse';
6
- import * as t from '@babel/types';
7
- import postcss from 'postcss';
8
-
9
- /**
10
- * Parser for Svelte files (.svelte) - extracts ALL web platform features
11
- * while ignoring Svelte-specific syntax
12
- */
13
- export class SvelteParser extends Parser {
14
- private readonly SVELTE_SPECIFIC_APIS = new Set([
15
- // Svelte stores
16
- 'writable', 'readable', 'derived', 'get', 'subscribe', 'set', 'update',
17
-
18
- // Svelte lifecycle
19
- 'onMount', 'onDestroy', 'beforeUpdate', 'afterUpdate', 'tick',
20
- 'setContext', 'getContext', 'hasContext', 'getAllContexts',
21
-
22
- // Svelte actions and transitions
23
- 'createEventDispatcher', 'dispatch',
24
-
25
- // SvelteKit specific
26
- 'page', 'navigating', 'updated', 'goto', 'prefetch', 'prefetchRoutes',
27
- 'invalidate', 'invalidateAll', 'preloadData', 'preloadCode',
28
-
29
- // Svelte compiler directives (handled separately)
30
- 'bind', 'on', 'use', 'transition', 'in', 'out', 'animate'
31
- ]);
32
-
33
- private readonly SVELTE_DIRECTIVES = new Set([
34
- 'bind:', 'on:', 'use:', 'transition:', 'in:', 'out:', 'animate:', 'class:', 'style:'
35
- ]);
36
-
37
- private readonly WEB_PLATFORM_APIS = new Set([
38
- // Canvas APIs
39
- 'getContext', 'CanvasRenderingContext2D', 'WebGLRenderingContext', 'WebGL2RenderingContext',
40
- 'OffscreenCanvas', 'ImageBitmap', 'createImageBitmap', 'Path2D',
41
-
42
- // WebRTC APIs
43
- 'RTCPeerConnection', 'RTCDataChannel', 'RTCSessionDescription', 'RTCIceCandidate',
44
- 'getUserMedia', 'getDisplayMedia', 'MediaStream', 'MediaStreamTrack',
45
-
46
- // WebAssembly
47
- 'WebAssembly', 'instantiate', 'compile', 'validate',
48
-
49
- // Service Workers & PWA
50
- 'ServiceWorker', 'serviceWorker', 'register', 'Cache', 'caches',
51
- 'PushManager', 'Notification', 'showNotification',
52
-
53
- // DOM APIs
54
- 'querySelector', 'querySelectorAll', 'getElementById', 'getElementsByClassName',
55
- 'addEventListener', 'removeEventListener', 'dispatchEvent', 'CustomEvent',
56
- 'MutationObserver', 'ResizeObserver', 'IntersectionObserver', 'PerformanceObserver',
57
- 'AbortController', 'AbortSignal', 'FormData', 'URLSearchParams', 'URL',
58
- 'fetch', 'Request', 'Response', 'Headers', 'Blob', 'File', 'FileReader',
59
-
60
- // Web APIs
61
- 'navigator', 'geolocation', 'permissions', 'clipboard', 'share',
62
- 'requestAnimationFrame', 'cancelAnimationFrame', 'requestIdleCallback',
63
- 'setTimeout', 'setInterval', 'clearTimeout', 'clearInterval',
64
- 'localStorage', 'sessionStorage', 'indexedDB', 'crypto', 'performance',
65
-
66
- // Audio/Video APIs
67
- 'AudioContext', 'MediaRecorder', 'MediaSource', 'SourceBuffer',
68
- 'HTMLMediaElement', 'HTMLAudioElement', 'HTMLVideoElement',
69
-
70
- // Modern JavaScript APIs
71
- 'structuredClone', 'queueMicrotask', 'reportError',
72
- 'WeakRef', 'FinalizationRegistry', 'AggregateError',
73
-
74
- // Intl APIs
75
- 'Intl', 'DateTimeFormat', 'NumberFormat', 'Collator', 'PluralRules',
76
- 'RelativeTimeFormat', 'ListFormat', 'Locale'
77
- ]);
78
-
79
- canParse(filePath: string): boolean {
80
- return filePath.endsWith('.svelte');
81
- }
82
-
83
- getSupportedExtensions(): string[] {
84
- return ['.svelte'];
85
- }
86
-
87
- getName(): string {
88
- return 'SvelteParser';
89
- }
90
-
91
- async parseFeatures(content: string, filePath: string): Promise<DetectedFeature[]> {
92
- const features: DetectedFeature[] = [];
93
-
94
- try {
95
- // Lazy load Svelte compiler
96
- const svelteCompiler = await LazyLoader.getSvelteCompiler();
97
- const ast = svelteCompiler.parse(content, { filename: filePath });
98
-
99
- // Parse script sections for JavaScript features
100
- if (ast.instance) {
101
- const scriptFeatures = await this.parseScriptSection(
102
- ast.instance,
103
- content,
104
- filePath,
105
- 'instance'
106
- );
107
- features.push(...scriptFeatures);
108
- }
109
-
110
- if (ast.module) {
111
- const moduleFeatures = await this.parseScriptSection(
112
- ast.module,
113
- content,
114
- filePath,
115
- 'module'
116
- );
117
- features.push(...moduleFeatures);
118
- }
119
-
120
- // Parse CSS from style sections
121
- if (ast.css) {
122
- const styleFeatures = await this.parseStyleSection(
123
- ast.css,
124
- content,
125
- filePath
126
- );
127
- features.push(...styleFeatures);
128
- }
129
-
130
- // Parse HTML template for standard elements
131
- if (ast.html) {
132
- const templateFeatures = this.parseTemplateSection(
133
- ast.html,
134
- content,
135
- filePath
136
- );
137
- features.push(...templateFeatures);
138
- }
139
-
140
- } catch (error) {
141
- console.warn(`Warning: Could not parse Svelte file ${filePath}: ${error instanceof Error ? error.message : 'Unknown error'}`);
142
- }
143
-
144
- return features;
145
- }
146
-
147
- private async parseScriptSection(
148
- scriptNode: any,
149
- fullContent: string,
150
- filePath: string,
151
- sectionType: 'instance' | 'module'
152
- ): Promise<DetectedFeature[]> {
153
- const features: DetectedFeature[] = [];
154
-
155
- try {
156
- // Extract the script content from the full file
157
- const scriptContent = this.extractScriptContent(scriptNode, fullContent);
158
-
159
- // Determine if TypeScript
160
- const isTypeScript = scriptNode.attributes?.some((attr: any) =>
161
- attr.name === 'lang' && (attr.value[0]?.data === 'ts' || attr.value[0]?.data === 'typescript')
162
- );
163
-
164
- const ast = parseBabel(scriptContent, {
165
- sourceType: 'module',
166
- plugins: [
167
- 'typescript' as any,
168
- 'decorators-legacy' as any,
169
- 'classProperties' as any,
170
- 'objectRestSpread' as any,
171
- 'asyncGenerators' as any,
172
- 'functionBind' as any,
173
- 'exportDefaultFrom',
174
- 'exportNamespaceFrom',
175
- 'dynamicImport',
176
- 'nullishCoalescingOperator',
177
- 'optionalChaining',
178
- 'topLevelAwait'
179
- ].filter(plugin => isTypeScript || plugin !== 'typescript')
180
- });
181
-
182
- traverse(ast, {
183
- // Extract JavaScript Web APIs
184
- MemberExpression: (path: any) => {
185
- const feature = this.extractWebAPIFeature(path.node, scriptContent, scriptNode.start);
186
- if (feature) {
187
- features.push({ ...feature, file: filePath });
188
- }
189
- },
190
-
191
- // Extract function calls to Web APIs
192
- CallExpression: (path: any) => {
193
- const feature = this.extractWebAPICall(path.node, scriptContent, scriptNode.start);
194
- if (feature) {
195
- features.push({ ...feature, file: filePath });
196
- }
197
- },
198
-
199
- // Extract modern JavaScript syntax features
200
- OptionalMemberExpression: (path: any) => {
201
- features.push({
202
- feature: 'optional-chaining',
203
- type: 'js',
204
- context: this.getContext(scriptContent, path.node.loc?.start.line || 0),
205
- line: (path.node.loc?.start.line || 0) + this.getLineOffset(scriptNode.start, fullContent),
206
- column: path.node.loc?.start.column || 0,
207
- file: filePath
208
- });
209
- },
210
-
211
- // Nullish coalescing
212
- LogicalExpression: (path: any) => {
213
- if (path.node.operator === '??') {
214
- features.push({
215
- feature: 'nullish-coalescing',
216
- type: 'js',
217
- context: this.getContext(scriptContent, path.node.loc?.start.line || 0),
218
- line: (path.node.loc?.start.line || 0) + this.getLineOffset(scriptNode.start, fullContent),
219
- column: path.node.loc?.start.column || 0,
220
- file: filePath
221
- });
222
- }
223
- },
224
-
225
- // Private class fields
226
- ClassPrivateProperty: (path: any) => {
227
- features.push({
228
- feature: 'private-fields',
229
- type: 'js',
230
- context: this.getContext(scriptContent, path.node.loc?.start.line || 0),
231
- line: (path.node.loc?.start.line || 0) + this.getLineOffset(scriptNode.start, fullContent),
232
- column: path.node.loc?.start.column || 0,
233
- file: filePath
234
- });
235
- },
236
-
237
- // Top-level await
238
- AwaitExpression: (path: any) => {
239
- if (this.isTopLevelAwait(path)) {
240
- features.push({
241
- feature: 'top-level-await',
242
- type: 'js',
243
- context: this.getContext(scriptContent, path.node.loc?.start.line || 0),
244
- line: (path.node.loc?.start.line || 0) + this.getLineOffset(scriptNode.start, fullContent),
245
- column: path.node.loc?.start.column || 0,
246
- file: filePath
247
- });
248
- }
249
- }
250
- });
251
-
252
- } catch (error) {
253
- console.warn(`Warning: Could not parse ${sectionType} script in ${filePath}: ${error instanceof Error ? error.message : 'Unknown error'}`);
254
- }
255
-
256
- return features;
257
- }
258
-
259
- private async parseStyleSection(
260
- styleNode: any,
261
- fullContent: string,
262
- filePath: string
263
- ): Promise<DetectedFeature[]> {
264
- const features: DetectedFeature[] = [];
265
-
266
- try {
267
- const styleContent = this.extractStyleContent(styleNode, fullContent);
268
- const lineOffset = this.getLineOffset(styleNode.start, fullContent);
269
-
270
- const root = postcss.parse(styleContent);
271
-
272
- root.walkDecls((decl: any) => {
273
- features.push({
274
- feature: decl.prop,
275
- type: 'css',
276
- context: `${decl.prop}: ${decl.value}`,
277
- line: (decl.source?.start?.line || 0) + lineOffset,
278
- column: decl.source?.start?.column || 0,
279
- file: filePath
280
- });
281
- });
282
-
283
- root.walkRules((rule: any) => {
284
- // Extract CSS selectors that might be modern features
285
- if (rule.selector.includes(':has(') ||
286
- rule.selector.includes(':is(') ||
287
- rule.selector.includes(':where(') ||
288
- rule.selector.includes(':focus-visible')) {
289
- features.push({
290
- feature: this.extractSelectorFeature(rule.selector),
291
- type: 'css',
292
- context: rule.selector,
293
- line: (rule.source?.start?.line || 0) + lineOffset,
294
- column: rule.source?.start?.column || 0,
295
- file: filePath
296
- });
297
- }
298
- });
299
-
300
- root.walkAtRules((atRule: any) => {
301
- // Extract at-rules like @supports, @container, etc.
302
- features.push({
303
- feature: `@${atRule.name}`,
304
- type: 'css',
305
- context: `@${atRule.name} ${atRule.params}`,
306
- line: (atRule.source?.start?.line || 0) + lineOffset,
307
- column: atRule.source?.start?.column || 0,
308
- file: filePath
309
- });
310
- });
311
-
312
- } catch (error) {
313
- console.warn(`Warning: Could not parse style section in ${filePath}: ${error instanceof Error ? error.message : 'Unknown error'}`);
314
- }
315
-
316
- return features;
317
- }
318
-
319
- private parseTemplateSection(
320
- htmlNode: any,
321
- fullContent: string,
322
- filePath: string
323
- ): DetectedFeature[] {
324
- const features: DetectedFeature[] = [];
325
-
326
- try {
327
- // Walk through the HTML AST to find standard HTML elements
328
- this.walkHtmlNode(htmlNode, (node: any) => {
329
- if (node.type === 'Element') {
330
- const tagName = node.name;
331
-
332
- // Check for modern HTML elements
333
- if (this.isModernHTMLElement(tagName)) {
334
- const lineOffset = this.getLineOffset(node.start, fullContent);
335
- features.push({
336
- feature: tagName,
337
- type: 'html',
338
- context: this.getNodeContext(node, fullContent),
339
- line: lineOffset,
340
- column: 0,
341
- file: filePath
342
- });
343
- }
344
-
345
- // Check for modern HTML attributes (ignore Svelte directives)
346
- if (node.attributes) {
347
- node.attributes.forEach((attr: any) => {
348
- if (attr.type === 'Attribute' && !this.isSvelteDirective(attr.name)) {
349
- if (this.isModernHTMLAttribute(attr.name, attr.value)) {
350
- const lineOffset = this.getLineOffset(node.start, fullContent);
351
- features.push({
352
- feature: attr.name,
353
- type: 'html',
354
- context: this.getNodeContext(node, fullContent),
355
- line: lineOffset,
356
- column: 0,
357
- file: filePath
358
- });
359
- }
360
- }
361
- });
362
- }
363
- }
364
- });
365
-
366
- } catch (error) {
367
- console.warn(`Warning: Could not parse template section in ${filePath}: ${error instanceof Error ? error.message : 'Unknown error'}`);
368
- }
369
-
370
- return features;
371
- }
372
-
373
- private extractWebAPIFeature(node: t.MemberExpression, content: string, offset: number): DetectedFeature | null {
374
- const apiName = this.getMemberExpressionName(node);
375
-
376
- if (!apiName || this.SVELTE_SPECIFIC_APIS.has(apiName)) {
377
- return null;
378
- }
379
-
380
- if (this.WEB_PLATFORM_APIS.has(apiName)) {
381
- return {
382
- feature: apiName,
383
- type: 'js',
384
- context: this.getContext(content, node.loc?.start.line || 0),
385
- line: (node.loc?.start.line || 0) + this.getLineOffset(offset, content),
386
- column: node.loc?.start.column || 0
387
- };
388
- }
389
-
390
- return null;
391
- }
392
-
393
- private extractWebAPICall(node: t.CallExpression, content: string, offset: number): DetectedFeature | null {
394
- let apiName = '';
395
-
396
- if (t.isIdentifier(node.callee)) {
397
- apiName = node.callee.name;
398
- } else if (t.isMemberExpression(node.callee)) {
399
- apiName = this.getMemberExpressionName(node.callee);
400
- }
401
-
402
- if (!apiName || this.SVELTE_SPECIFIC_APIS.has(apiName)) {
403
- return null;
404
- }
405
-
406
- if (this.WEB_PLATFORM_APIS.has(apiName)) {
407
- return {
408
- feature: apiName,
409
- type: 'js',
410
- context: this.getContext(content, node.loc?.start.line || 0),
411
- line: (node.loc?.start.line || 0) + this.getLineOffset(offset, content),
412
- column: node.loc?.start.column || 0
413
- };
414
- }
415
-
416
- return null;
417
- }
418
-
419
- private extractScriptContent(scriptNode: any, fullContent: string): string {
420
- const start = scriptNode.content.start;
421
- const end = scriptNode.content.end;
422
- return fullContent.slice(start, end);
423
- }
424
-
425
- private extractStyleContent(styleNode: any, fullContent: string): string {
426
- const start = styleNode.content.start;
427
- const end = styleNode.content.end;
428
- return fullContent.slice(start, end);
429
- }
430
-
431
- private getLineOffset(position: number, content: string): number {
432
- return content.slice(0, position).split('\n').length - 1;
433
- }
434
-
435
- private walkHtmlNode(node: any, callback: (node: any) => void): void {
436
- callback(node);
437
- if (node.children) {
438
- node.children.forEach((child: any) => this.walkHtmlNode(child, callback));
439
- }
440
- }
441
-
442
- private getMemberExpressionName(node: t.MemberExpression): string {
443
- const parts: string[] = [];
444
-
445
- const traverse = (n: t.Expression): void => {
446
- if (t.isIdentifier(n)) {
447
- parts.unshift(n.name);
448
- } else if (t.isMemberExpression(n)) {
449
- if (t.isIdentifier(n.property)) {
450
- parts.unshift(n.property.name);
451
- }
452
- traverse(n.object);
453
- }
454
- };
455
-
456
- traverse(node);
457
- return parts.join('.');
458
- }
459
-
460
- private isTopLevelAwait(path: any): boolean {
461
- let parent = path.parent;
462
- while (parent) {
463
- if (t.isFunction(parent) || t.isArrowFunctionExpression(parent)) {
464
- return false;
465
- }
466
- parent = path.parentPath?.parent;
467
- }
468
- return true;
469
- }
470
-
471
- private extractSelectorFeature(selector: string): string {
472
- if (selector.includes(':has(')) return ':has()';
473
- if (selector.includes(':is(')) return ':is()';
474
- if (selector.includes(':where(')) return ':where()';
475
- if (selector.includes(':focus-visible')) return ':focus-visible';
476
- return selector;
477
- }
478
-
479
- private isModernHTMLElement(tagName: string): boolean {
480
- const modernElements = new Set([
481
- 'dialog', 'details', 'summary', 'main', 'article', 'section', 'nav', 'aside',
482
- 'header', 'footer', 'figure', 'figcaption', 'time', 'mark', 'progress', 'meter',
483
- 'canvas', 'video', 'audio', 'source', 'track', 'embed', 'object'
484
- ]);
485
- return modernElements.has(tagName);
486
- }
487
-
488
- private isSvelteDirective(attrName: string): boolean {
489
- return Array.from(this.SVELTE_DIRECTIVES).some((directive: string) => attrName.startsWith(directive));
490
- }
491
-
492
- private isModernHTMLAttribute(attrName: string, attrValue: any): boolean {
493
- const modernAttrs = new Set([
494
- 'loading', 'decoding', 'fetchpriority', 'enterkeyhint', 'inputmode'
495
- ]);
496
- return modernAttrs.has(attrName);
497
- }
498
-
499
- private getNodeContext(node: any, fullContent: string): string {
500
- const start = node.start;
501
- const end = Math.min(node.end, start + 100); // Limit context length
502
- return fullContent.slice(start, end).trim();
503
- }
504
-
505
- private getContext(content: string, line: number): string {
506
- const lines = content.split('\n');
507
- const targetLine = lines[line - 1] || '';
508
- return targetLine.trim();
509
- }
510
- }
1
+ import { Parser } from './parser.js';
2
+ import type { DetectedFeature } from '../types/index.js';
3
+ import { LazyLoader } from '../core/lazy-loader.js';
4
+ import { parse as parseBabel } from '@babel/parser';
5
+ import traverse from '@babel/traverse';
6
+ import * as t from '@babel/types';
7
+ import postcss from 'postcss';
8
+
9
+ /**
10
+ * Parser for Svelte files (.svelte) - extracts ALL web platform features
11
+ * while ignoring Svelte-specific syntax
12
+ */
13
+ export class SvelteParser extends Parser {
14
+ private readonly SVELTE_SPECIFIC_APIS = new Set([
15
+ // Svelte stores
16
+ 'writable', 'readable', 'derived', 'get', 'subscribe', 'set', 'update',
17
+
18
+ // Svelte lifecycle
19
+ 'onMount', 'onDestroy', 'beforeUpdate', 'afterUpdate', 'tick',
20
+ 'setContext', 'getContext', 'hasContext', 'getAllContexts',
21
+
22
+ // Svelte actions and transitions
23
+ 'createEventDispatcher', 'dispatch',
24
+
25
+ // SvelteKit specific
26
+ 'page', 'navigating', 'updated', 'goto', 'prefetch', 'prefetchRoutes',
27
+ 'invalidate', 'invalidateAll', 'preloadData', 'preloadCode',
28
+
29
+ // Svelte compiler directives (handled separately)
30
+ 'bind', 'on', 'use', 'transition', 'in', 'out', 'animate'
31
+ ]);
32
+
33
+ private readonly SVELTE_DIRECTIVES = new Set([
34
+ 'bind:', 'on:', 'use:', 'transition:', 'in:', 'out:', 'animate:', 'class:', 'style:'
35
+ ]);
36
+
37
+ private readonly WEB_PLATFORM_APIS = new Set([
38
+ // Canvas APIs
39
+ 'getContext', 'CanvasRenderingContext2D', 'WebGLRenderingContext', 'WebGL2RenderingContext',
40
+ 'OffscreenCanvas', 'ImageBitmap', 'createImageBitmap', 'Path2D',
41
+
42
+ // WebRTC APIs
43
+ 'RTCPeerConnection', 'RTCDataChannel', 'RTCSessionDescription', 'RTCIceCandidate',
44
+ 'getUserMedia', 'getDisplayMedia', 'MediaStream', 'MediaStreamTrack',
45
+
46
+ // WebAssembly
47
+ 'WebAssembly', 'instantiate', 'compile', 'validate',
48
+
49
+ // Service Workers & PWA
50
+ 'ServiceWorker', 'serviceWorker', 'register', 'Cache', 'caches',
51
+ 'PushManager', 'Notification', 'showNotification',
52
+
53
+ // DOM APIs
54
+ 'querySelector', 'querySelectorAll', 'getElementById', 'getElementsByClassName',
55
+ 'addEventListener', 'removeEventListener', 'dispatchEvent', 'CustomEvent',
56
+ 'MutationObserver', 'ResizeObserver', 'IntersectionObserver', 'PerformanceObserver',
57
+ 'AbortController', 'AbortSignal', 'FormData', 'URLSearchParams', 'URL',
58
+ 'fetch', 'Request', 'Response', 'Headers', 'Blob', 'File', 'FileReader',
59
+
60
+ // Web APIs
61
+ 'navigator', 'geolocation', 'permissions', 'clipboard', 'share',
62
+ 'requestAnimationFrame', 'cancelAnimationFrame', 'requestIdleCallback',
63
+ 'setTimeout', 'setInterval', 'clearTimeout', 'clearInterval',
64
+ 'localStorage', 'sessionStorage', 'indexedDB', 'crypto', 'performance',
65
+
66
+ // Audio/Video APIs
67
+ 'AudioContext', 'MediaRecorder', 'MediaSource', 'SourceBuffer',
68
+ 'HTMLMediaElement', 'HTMLAudioElement', 'HTMLVideoElement',
69
+
70
+ // Modern JavaScript APIs
71
+ 'structuredClone', 'queueMicrotask', 'reportError',
72
+ 'WeakRef', 'FinalizationRegistry', 'AggregateError',
73
+
74
+ // Intl APIs
75
+ 'Intl', 'DateTimeFormat', 'NumberFormat', 'Collator', 'PluralRules',
76
+ 'RelativeTimeFormat', 'ListFormat', 'Locale'
77
+ ]);
78
+
79
+ canParse(filePath: string): boolean {
80
+ return filePath.endsWith('.svelte');
81
+ }
82
+
83
+ getSupportedExtensions(): string[] {
84
+ return ['.svelte'];
85
+ }
86
+
87
+ getName(): string {
88
+ return 'SvelteParser';
89
+ }
90
+
91
+ async parseFeatures(content: string, filePath: string): Promise<DetectedFeature[]> {
92
+ const features: DetectedFeature[] = [];
93
+
94
+ try {
95
+ // Lazy load Svelte compiler
96
+ const svelteCompiler = await LazyLoader.getSvelteCompiler();
97
+ const ast = svelteCompiler.parse(content, { filename: filePath });
98
+
99
+ // Parse script sections for JavaScript features
100
+ if (ast.instance) {
101
+ const scriptFeatures = await this.parseScriptSection(
102
+ ast.instance,
103
+ content,
104
+ filePath,
105
+ 'instance'
106
+ );
107
+ features.push(...scriptFeatures);
108
+ }
109
+
110
+ if (ast.module) {
111
+ const moduleFeatures = await this.parseScriptSection(
112
+ ast.module,
113
+ content,
114
+ filePath,
115
+ 'module'
116
+ );
117
+ features.push(...moduleFeatures);
118
+ }
119
+
120
+ // Parse CSS from style sections
121
+ if (ast.css) {
122
+ const styleFeatures = await this.parseStyleSection(
123
+ ast.css,
124
+ content,
125
+ filePath
126
+ );
127
+ features.push(...styleFeatures);
128
+ }
129
+
130
+ // Parse HTML template for standard elements
131
+ if (ast.html) {
132
+ const templateFeatures = this.parseTemplateSection(
133
+ ast.html,
134
+ content,
135
+ filePath
136
+ );
137
+ features.push(...templateFeatures);
138
+ }
139
+
140
+ } catch (error) {
141
+ console.warn(`Warning: Could not parse Svelte file ${filePath}: ${error instanceof Error ? error.message : 'Unknown error'}`);
142
+ }
143
+
144
+ return features;
145
+ }
146
+
147
+ private async parseScriptSection(
148
+ scriptNode: any,
149
+ fullContent: string,
150
+ filePath: string,
151
+ sectionType: 'instance' | 'module'
152
+ ): Promise<DetectedFeature[]> {
153
+ const features: DetectedFeature[] = [];
154
+
155
+ try {
156
+ // Extract the script content from the full file
157
+ const scriptContent = this.extractScriptContent(scriptNode, fullContent);
158
+
159
+ // Determine if TypeScript
160
+ const isTypeScript = scriptNode.attributes?.some((attr: any) =>
161
+ attr.name === 'lang' && (attr.value[0]?.data === 'ts' || attr.value[0]?.data === 'typescript')
162
+ );
163
+
164
+ const ast = parseBabel(scriptContent, {
165
+ sourceType: 'module',
166
+ plugins: [
167
+ 'typescript' as any,
168
+ 'decorators-legacy' as any,
169
+ 'classProperties' as any,
170
+ 'objectRestSpread' as any,
171
+ 'asyncGenerators' as any,
172
+ 'functionBind' as any,
173
+ 'exportDefaultFrom',
174
+ 'exportNamespaceFrom',
175
+ 'dynamicImport',
176
+ 'nullishCoalescingOperator',
177
+ 'optionalChaining',
178
+ 'topLevelAwait'
179
+ ].filter(plugin => isTypeScript || plugin !== 'typescript')
180
+ });
181
+
182
+ traverse(ast, {
183
+ // Extract JavaScript Web APIs
184
+ MemberExpression: (path: any) => {
185
+ const feature = this.extractWebAPIFeature(path.node, scriptContent, scriptNode.start);
186
+ if (feature) {
187
+ features.push({ ...feature, file: filePath });
188
+ }
189
+ },
190
+
191
+ // Extract function calls to Web APIs
192
+ CallExpression: (path: any) => {
193
+ const feature = this.extractWebAPICall(path.node, scriptContent, scriptNode.start);
194
+ if (feature) {
195
+ features.push({ ...feature, file: filePath });
196
+ }
197
+ },
198
+
199
+ // Extract modern JavaScript syntax features
200
+ OptionalMemberExpression: (path: any) => {
201
+ features.push({
202
+ feature: 'optional-chaining',
203
+ type: 'js',
204
+ context: this.getContext(scriptContent, path.node.loc?.start.line || 0),
205
+ line: (path.node.loc?.start.line || 0) + this.getLineOffset(scriptNode.start, fullContent),
206
+ column: path.node.loc?.start.column || 0,
207
+ file: filePath
208
+ });
209
+ },
210
+
211
+ // Nullish coalescing
212
+ LogicalExpression: (path: any) => {
213
+ if (path.node.operator === '??') {
214
+ features.push({
215
+ feature: 'nullish-coalescing',
216
+ type: 'js',
217
+ context: this.getContext(scriptContent, path.node.loc?.start.line || 0),
218
+ line: (path.node.loc?.start.line || 0) + this.getLineOffset(scriptNode.start, fullContent),
219
+ column: path.node.loc?.start.column || 0,
220
+ file: filePath
221
+ });
222
+ }
223
+ },
224
+
225
+ // Private class fields
226
+ ClassPrivateProperty: (path: any) => {
227
+ features.push({
228
+ feature: 'private-fields',
229
+ type: 'js',
230
+ context: this.getContext(scriptContent, path.node.loc?.start.line || 0),
231
+ line: (path.node.loc?.start.line || 0) + this.getLineOffset(scriptNode.start, fullContent),
232
+ column: path.node.loc?.start.column || 0,
233
+ file: filePath
234
+ });
235
+ },
236
+
237
+ // Top-level await
238
+ AwaitExpression: (path: any) => {
239
+ if (this.isTopLevelAwait(path)) {
240
+ features.push({
241
+ feature: 'top-level-await',
242
+ type: 'js',
243
+ context: this.getContext(scriptContent, path.node.loc?.start.line || 0),
244
+ line: (path.node.loc?.start.line || 0) + this.getLineOffset(scriptNode.start, fullContent),
245
+ column: path.node.loc?.start.column || 0,
246
+ file: filePath
247
+ });
248
+ }
249
+ }
250
+ });
251
+
252
+ } catch (error) {
253
+ console.warn(`Warning: Could not parse ${sectionType} script in ${filePath}: ${error instanceof Error ? error.message : 'Unknown error'}`);
254
+ }
255
+
256
+ return features;
257
+ }
258
+
259
+ private async parseStyleSection(
260
+ styleNode: any,
261
+ fullContent: string,
262
+ filePath: string
263
+ ): Promise<DetectedFeature[]> {
264
+ const features: DetectedFeature[] = [];
265
+
266
+ try {
267
+ const styleContent = this.extractStyleContent(styleNode, fullContent);
268
+ const lineOffset = this.getLineOffset(styleNode.start, fullContent);
269
+
270
+ const root = postcss.parse(styleContent);
271
+
272
+ root.walkDecls((decl: any) => {
273
+ features.push({
274
+ feature: decl.prop,
275
+ type: 'css',
276
+ context: `${decl.prop}: ${decl.value}`,
277
+ line: (decl.source?.start?.line || 0) + lineOffset,
278
+ column: decl.source?.start?.column || 0,
279
+ file: filePath
280
+ });
281
+ });
282
+
283
+ root.walkRules((rule: any) => {
284
+ // Extract CSS selectors that might be modern features
285
+ if (rule.selector.includes(':has(') ||
286
+ rule.selector.includes(':is(') ||
287
+ rule.selector.includes(':where(') ||
288
+ rule.selector.includes(':focus-visible')) {
289
+ features.push({
290
+ feature: this.extractSelectorFeature(rule.selector),
291
+ type: 'css',
292
+ context: rule.selector,
293
+ line: (rule.source?.start?.line || 0) + lineOffset,
294
+ column: rule.source?.start?.column || 0,
295
+ file: filePath
296
+ });
297
+ }
298
+ });
299
+
300
+ root.walkAtRules((atRule: any) => {
301
+ // Extract at-rules like @supports, @container, etc.
302
+ features.push({
303
+ feature: `@${atRule.name}`,
304
+ type: 'css',
305
+ context: `@${atRule.name} ${atRule.params}`,
306
+ line: (atRule.source?.start?.line || 0) + lineOffset,
307
+ column: atRule.source?.start?.column || 0,
308
+ file: filePath
309
+ });
310
+ });
311
+
312
+ } catch (error) {
313
+ console.warn(`Warning: Could not parse style section in ${filePath}: ${error instanceof Error ? error.message : 'Unknown error'}`);
314
+ }
315
+
316
+ return features;
317
+ }
318
+
319
+ private parseTemplateSection(
320
+ htmlNode: any,
321
+ fullContent: string,
322
+ filePath: string
323
+ ): DetectedFeature[] {
324
+ const features: DetectedFeature[] = [];
325
+
326
+ try {
327
+ // Walk through the HTML AST to find standard HTML elements
328
+ this.walkHtmlNode(htmlNode, (node: any) => {
329
+ if (node.type === 'Element') {
330
+ const tagName = node.name;
331
+
332
+ // Check for modern HTML elements
333
+ if (this.isModernHTMLElement(tagName)) {
334
+ const lineOffset = this.getLineOffset(node.start, fullContent);
335
+ features.push({
336
+ feature: tagName,
337
+ type: 'html',
338
+ context: this.getNodeContext(node, fullContent),
339
+ line: lineOffset,
340
+ column: 0,
341
+ file: filePath
342
+ });
343
+ }
344
+
345
+ // Check for modern HTML attributes (ignore Svelte directives)
346
+ if (node.attributes) {
347
+ node.attributes.forEach((attr: any) => {
348
+ if (attr.type === 'Attribute' && !this.isSvelteDirective(attr.name)) {
349
+ if (this.isModernHTMLAttribute(attr.name, attr.value)) {
350
+ const lineOffset = this.getLineOffset(node.start, fullContent);
351
+ features.push({
352
+ feature: attr.name,
353
+ type: 'html',
354
+ context: this.getNodeContext(node, fullContent),
355
+ line: lineOffset,
356
+ column: 0,
357
+ file: filePath
358
+ });
359
+ }
360
+ }
361
+ });
362
+ }
363
+ }
364
+ });
365
+
366
+ } catch (error) {
367
+ console.warn(`Warning: Could not parse template section in ${filePath}: ${error instanceof Error ? error.message : 'Unknown error'}`);
368
+ }
369
+
370
+ return features;
371
+ }
372
+
373
+ private extractWebAPIFeature(node: t.MemberExpression, content: string, offset: number): DetectedFeature | null {
374
+ const apiName = this.getMemberExpressionName(node);
375
+
376
+ if (!apiName || this.SVELTE_SPECIFIC_APIS.has(apiName)) {
377
+ return null;
378
+ }
379
+
380
+ if (this.WEB_PLATFORM_APIS.has(apiName)) {
381
+ return {
382
+ feature: apiName,
383
+ type: 'js',
384
+ context: this.getContext(content, node.loc?.start.line || 0),
385
+ line: (node.loc?.start.line || 0) + this.getLineOffset(offset, content),
386
+ column: node.loc?.start.column || 0
387
+ };
388
+ }
389
+
390
+ return null;
391
+ }
392
+
393
+ private extractWebAPICall(node: t.CallExpression, content: string, offset: number): DetectedFeature | null {
394
+ let apiName = '';
395
+
396
+ if (t.isIdentifier(node.callee)) {
397
+ apiName = node.callee.name;
398
+ } else if (t.isMemberExpression(node.callee)) {
399
+ apiName = this.getMemberExpressionName(node.callee);
400
+ }
401
+
402
+ if (!apiName || this.SVELTE_SPECIFIC_APIS.has(apiName)) {
403
+ return null;
404
+ }
405
+
406
+ if (this.WEB_PLATFORM_APIS.has(apiName)) {
407
+ return {
408
+ feature: apiName,
409
+ type: 'js',
410
+ context: this.getContext(content, node.loc?.start.line || 0),
411
+ line: (node.loc?.start.line || 0) + this.getLineOffset(offset, content),
412
+ column: node.loc?.start.column || 0
413
+ };
414
+ }
415
+
416
+ return null;
417
+ }
418
+
419
+ private extractScriptContent(scriptNode: any, fullContent: string): string {
420
+ const start = scriptNode.content.start;
421
+ const end = scriptNode.content.end;
422
+ return fullContent.slice(start, end);
423
+ }
424
+
425
+ private extractStyleContent(styleNode: any, fullContent: string): string {
426
+ const start = styleNode.content.start;
427
+ const end = styleNode.content.end;
428
+ return fullContent.slice(start, end);
429
+ }
430
+
431
+ private getLineOffset(position: number, content: string): number {
432
+ return content.slice(0, position).split('\n').length - 1;
433
+ }
434
+
435
+ private walkHtmlNode(node: any, callback: (node: any) => void): void {
436
+ callback(node);
437
+ if (node.children) {
438
+ node.children.forEach((child: any) => this.walkHtmlNode(child, callback));
439
+ }
440
+ }
441
+
442
+ private getMemberExpressionName(node: t.MemberExpression): string {
443
+ const parts: string[] = [];
444
+
445
+ const traverse = (n: t.Expression): void => {
446
+ if (t.isIdentifier(n)) {
447
+ parts.unshift(n.name);
448
+ } else if (t.isMemberExpression(n)) {
449
+ if (t.isIdentifier(n.property)) {
450
+ parts.unshift(n.property.name);
451
+ }
452
+ traverse(n.object);
453
+ }
454
+ };
455
+
456
+ traverse(node);
457
+ return parts.join('.');
458
+ }
459
+
460
+ private isTopLevelAwait(path: any): boolean {
461
+ let parent = path.parent;
462
+ while (parent) {
463
+ if (t.isFunction(parent) || t.isArrowFunctionExpression(parent)) {
464
+ return false;
465
+ }
466
+ parent = path.parentPath?.parent;
467
+ }
468
+ return true;
469
+ }
470
+
471
+ private extractSelectorFeature(selector: string): string {
472
+ if (selector.includes(':has(')) return ':has()';
473
+ if (selector.includes(':is(')) return ':is()';
474
+ if (selector.includes(':where(')) return ':where()';
475
+ if (selector.includes(':focus-visible')) return ':focus-visible';
476
+ return selector;
477
+ }
478
+
479
+ private isModernHTMLElement(tagName: string): boolean {
480
+ const modernElements = new Set([
481
+ 'dialog', 'details', 'summary', 'main', 'article', 'section', 'nav', 'aside',
482
+ 'header', 'footer', 'figure', 'figcaption', 'time', 'mark', 'progress', 'meter',
483
+ 'canvas', 'video', 'audio', 'source', 'track', 'embed', 'object'
484
+ ]);
485
+ return modernElements.has(tagName);
486
+ }
487
+
488
+ private isSvelteDirective(attrName: string): boolean {
489
+ return Array.from(this.SVELTE_DIRECTIVES).some((directive: string) => attrName.startsWith(directive));
490
+ }
491
+
492
+ private isModernHTMLAttribute(attrName: string, _attrValue: any): boolean {
493
+ const modernAttrs = new Set([
494
+ 'loading', 'decoding', 'fetchpriority', 'enterkeyhint', 'inputmode'
495
+ ]);
496
+ return modernAttrs.has(attrName);
497
+ }
498
+
499
+ private getNodeContext(node: any, fullContent: string): string {
500
+ const start = node.start;
501
+ const end = Math.min(node.end, start + 100); // Limit context length
502
+ return fullContent.slice(start, end).trim();
503
+ }
504
+
505
+ private getContext(content: string, line: number): string {
506
+ const lines = content.split('\n');
507
+ const targetLine = lines[line - 1] || '';
508
+ return targetLine.trim();
509
+ }
510
+ }