@shapeshift-labs/frontier-lang-compiler 0.2.99 → 0.2.101

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 (57) hide show
  1. package/dist/declarations/bidirectional-target-change-evidence.d.ts +299 -0
  2. package/dist/declarations/bidirectional-target-change.d.ts +19 -120
  3. package/dist/declarations/native-project-admission.d.ts +43 -22
  4. package/dist/declarations/semantic-edit-bundle.d.ts +13 -1
  5. package/dist/declarations/semantic-edit-replay-diagnostics.d.ts +24 -0
  6. package/dist/declarations/semantic-edit-script.d.ts +53 -51
  7. package/dist/declarations/semantic-lineage.d.ts +62 -51
  8. package/dist/declarations/semantic-merge-candidates.d.ts +39 -0
  9. package/dist/declarations/semantic-patch-bundle.d.ts +13 -0
  10. package/dist/declarations/semantic-sidecar-admission.d.ts +14 -0
  11. package/dist/declarations/semantic-sidecar.d.ts +12 -14
  12. package/dist/internal/index-impl/bidirectionalTargetRoundtripEvidence.js +200 -0
  13. package/dist/internal/index-impl/createBidirectionalTargetChangeRecord.js +62 -17
  14. package/dist/internal/index-impl/createNativeSourcePreservation.js +16 -1
  15. package/dist/internal/index-impl/createProjectImportAdmissionRecord.js +151 -1
  16. package/dist/internal/index-impl/createSemanticImportSidecar.js +5 -0
  17. package/dist/internal/index-impl/createSemanticImportSidecarAdmission.js +29 -11
  18. package/dist/internal/index-impl/declarationRecord.js +2 -2
  19. package/dist/internal/index-impl/inferSemanticLineageEvents.js +8 -0
  20. package/dist/internal/index-impl/nativeChangeProjectionEndpoint.js +56 -16
  21. package/dist/internal/index-impl/projectImportAdmissionMergeScore.js +26 -74
  22. package/dist/internal/index-impl/projectImportAdmissionProjectionCoverage.js +74 -0
  23. package/dist/internal/index-impl/projectSemanticEditScriptToSource.js +92 -74
  24. package/dist/internal/index-impl/replaySemanticEditProjection.js +114 -40
  25. package/dist/internal/index-impl/semanticEditBundleAdmission.js +95 -12
  26. package/dist/internal/index-impl/semanticEditBundleIndex.js +16 -10
  27. package/dist/internal/index-impl/semanticEditInsertionAnchors.js +8 -5
  28. package/dist/internal/index-impl/semanticEditReplayDiagnostics.js +167 -0
  29. package/dist/internal/index-impl/semanticEditSourceRanges.js +283 -0
  30. package/dist/internal/index-impl/semanticHistoryLineageResolution.js +56 -3
  31. package/dist/internal/index-impl/semanticIndexFromNativeDeclarations.js +2 -2
  32. package/dist/internal/index-impl/semanticLineageHashEvidence.js +97 -0
  33. package/dist/internal/index-impl/semanticLineageInferenceMatching.js +158 -13
  34. package/dist/internal/index-impl/semanticLineageResolutionRecords.js +46 -2
  35. package/dist/internal/index-impl/semanticMergeCandidateRecords.js +22 -2
  36. package/dist/internal/index-impl/semanticMergeCandidateScoreFacets.js +221 -0
  37. package/dist/internal/index-impl/semanticPatchBundleAdmission.js +122 -20
  38. package/dist/internal/index-impl/semanticPatchBundleLineageLinks.js +199 -0
  39. package/dist/internal/index-impl/semanticPatchBundleOverlaps.js +29 -3
  40. package/dist/internal/index-impl/semanticPatchBundleRecords.js +28 -104
  41. package/dist/internal/index-impl/semanticPatchBundleSourceRecords.js +127 -0
  42. package/dist/internal/index-impl/sourcePreservationFromProjectionContext.js +9 -2
  43. package/dist/internal/index-impl/sourceTextForSpan.js +4 -9
  44. package/dist/lightweight-dependency-relations.js +113 -7
  45. package/dist/native-import-language-profiles.js +10 -2
  46. package/dist/native-import-utils.js +15 -1
  47. package/dist/native-region-scanner-js-helpers.js +68 -18
  48. package/dist/native-region-scanner-js-imports.js +7 -0
  49. package/dist/native-region-scanner-js.js +16 -8
  50. package/dist/native-region-scanner.js +2 -1
  51. package/dist/semantic-import-regions.js +8 -6
  52. package/dist/semantic-import-sidecar-admission-types.d.ts +14 -0
  53. package/dist/semantic-import-sidecar-entry.js +151 -7
  54. package/dist/semantic-import-sidecar-types.d.ts +18 -13
  55. package/dist/semantic-import-source-preservation-utils.js +55 -0
  56. package/dist/semantic-import-source-preservation.js +98 -3
  57. package/package.json +1 -1
@@ -0,0 +1,127 @@
1
+ import { idFragment, uniqueStrings } from '../../native-import-utils.js';
2
+
3
+ export function normalizeSources(entries, context) {
4
+ return entries.filter(Boolean).map((entry, index) => compactRecord({
5
+ id: entry.id ?? entry.sourceId,
6
+ side: entry.side,
7
+ importId: entry.importId ?? entry.importResultId,
8
+ language: entry.language ?? context.language,
9
+ sourcePath: entry.sourcePath ?? context.sourcePath,
10
+ sourceHash: entry.sourceHash ?? entry.hash,
11
+ baseHash: entry.baseHash ?? entry.beforeHash ?? context.baseHash ?? context.beforeHash,
12
+ targetHash: entry.targetHash ?? entry.afterHash ?? context.targetHash ?? context.afterHash,
13
+ nativeSourceId: entry.nativeSourceId,
14
+ nativeAstId: entry.nativeAstId,
15
+ semanticIndexId: entry.semanticIndexId,
16
+ universalAstId: entry.universalAstId,
17
+ sourceMapIds: uniqueStrings(entry.sourceMapIds),
18
+ ordinal: index
19
+ })).filter((entry) => entry.importId || entry.sourcePath || entry.sourceHash || entry.baseHash || entry.targetHash);
20
+ }
21
+
22
+ export function sourceRef(importResult, side, sourceHash) {
23
+ if (!importResult) return undefined;
24
+ return compactRecord({
25
+ id: `${side}_${importResult.id ?? idFragment(importResult.sourcePath ?? 'source')}`,
26
+ side,
27
+ importId: importResult.id,
28
+ language: importResult.language,
29
+ sourcePath: importResult.sourcePath,
30
+ sourceHash: sourceHash ?? importResult.nativeSource?.sourceHash ?? importResult.nativeAst?.sourceHash ?? importResult.sourceHash,
31
+ nativeSourceId: importResult.nativeSource?.id,
32
+ nativeAstId: importResult.nativeAst?.id,
33
+ semanticIndexId: importResult.semanticIndex?.id,
34
+ universalAstId: importResult.universalAst?.id,
35
+ sourceMapIds: uniqueStrings((importResult.sourceMaps ?? []).map((map) => map.id))
36
+ });
37
+ }
38
+
39
+ export function normalizeRegions(regions, context) {
40
+ return regions.filter(Boolean).map((region, index) => {
41
+ const projection = region.metadata?.changedRegionProjection ?? region.projection;
42
+ const projected = projection?.region ?? {};
43
+ const key = firstString(region.key, region.ownershipKey, projected.key, region.conflictKey, region.id);
44
+ const conflictKey = firstString(region.conflictKey, projection?.conflictKey, key);
45
+ const links = array(projection?.sourceMapLinks ?? region.sourceMapLinks);
46
+ return compactRecord({
47
+ id: region.id ?? projected.id ?? `changed_region_${index + 1}`,
48
+ key,
49
+ conflictKey,
50
+ changeKind: region.changeKind ?? projection?.changeKind,
51
+ regionKind: region.regionKind ?? region.ownershipRegionKind ?? projected.kind,
52
+ granularity: region.granularity ?? projected.granularity,
53
+ precision: region.precision ?? projected.precision,
54
+ language: region.language ?? projection?.language ?? context.language,
55
+ sourcePath: region.sourcePath ?? projection?.sourcePath ?? context.sourcePath,
56
+ sourceHash: region.sourceHash ?? projection?.after?.sourceHash ?? projection?.before?.sourceHash,
57
+ symbolId: region.symbolId ?? projected.symbolId,
58
+ symbolName: region.symbolName ?? region.name ?? projected.symbolName,
59
+ symbolKind: region.symbolKind ?? projected.symbolKind,
60
+ sourceSpan: region.sourceSpan ?? projected.sourceSpan,
61
+ sourceMapLinkIds: uniqueStrings([...strings(region.sourceMapLinkIds), ...links.map((link) => link.id)]),
62
+ sourceMapIds: uniqueStrings([...strings(region.sourceMapIds), ...links.map((link) => link.sourceMapId)]),
63
+ sourceMapMappingIds: uniqueStrings([...strings(region.sourceMapMappingIds), ...links.map((link) => link.sourceMapMappingId)]),
64
+ lineageResolutionIds: uniqueStrings([...strings(region.lineageResolutionIds), ...strings(projection?.lineageResolutionIds), ...strings(region.metadata?.bidirectionalTargetChange?.lineageResolutionIds), ...strings(region.metadata?.semanticHistoryLineageResolution?.lineageResolutionIds), region.metadata?.semanticHistoryLineageResolution?.id]),
65
+ lineageEventIds: uniqueStrings([...strings(region.lineageEventIds), ...strings(projection?.lineageEventIds), ...strings(region.metadata?.semanticHistoryLineageResolution?.lineageEventIds)]),
66
+ lineageSourcePaths: uniqueStrings([...strings(region.lineageSourcePaths), ...strings(projection?.lineageSourcePaths), ...strings(region.metadata?.semanticHistoryLineageResolution?.sourcePaths)]),
67
+ lineageEvidenceIds: uniqueStrings([...strings(region.lineageEvidenceIds), ...strings(projection?.lineageEvidenceIds), ...strings(region.metadata?.semanticHistoryLineageResolution?.evidenceIds)]),
68
+ lineageProofIds: uniqueStrings([...strings(region.lineageProofIds), ...strings(projection?.lineageProofIds), ...strings(region.metadata?.semanticHistoryLineageResolution?.proofIds)]),
69
+ lineageReasonCodes: uniqueStrings([...strings(region.lineageReasonCodes), ...strings(projection?.lineageReasonCodes), ...strings(region.metadata?.semanticHistoryLineageResolution?.reasonCodes)]),
70
+ admission: compactRecord({
71
+ readiness: projection?.admission?.readiness ?? region.admission?.readiness,
72
+ action: projection?.admission?.action ?? region.admission?.action,
73
+ reasonCodes: uniqueStrings([...strings(region.admission?.reasonCodes), ...strings(projection?.admission?.reasons)]),
74
+ conflictKeys: uniqueStrings([...strings(region.admission?.conflictKeys), ...strings(projection?.admission?.conflictKeys)])
75
+ }),
76
+ metadata: region.metadata
77
+ });
78
+ });
79
+ }
80
+
81
+ export function normalizeSourceMapLinks(links) {
82
+ const seen = new Set();
83
+ const result = [];
84
+ for (const link of links.filter(Boolean)) {
85
+ const id = link.id ?? `source_map_link_${result.length + 1}`;
86
+ if (seen.has(id)) continue;
87
+ seen.add(id);
88
+ result.push(compactRecord({
89
+ id,
90
+ side: link.side,
91
+ sourceMapId: link.sourceMapId,
92
+ sourceMapMappingId: link.sourceMapMappingId,
93
+ sourcePath: link.sourcePath,
94
+ sourceHash: link.sourceHash,
95
+ targetPath: link.targetPath,
96
+ targetHash: link.targetHash,
97
+ semanticSymbolId: link.semanticSymbolId,
98
+ semanticOccurrenceId: link.semanticOccurrenceId,
99
+ semanticNodeId: link.semanticNodeId,
100
+ nativeSourceId: link.nativeSourceId,
101
+ nativeAstNodeId: link.nativeAstNodeId,
102
+ precision: link.precision,
103
+ sourceSpan: link.sourceSpan,
104
+ generatedSpan: link.generatedSpan,
105
+ regionKey: link.ownershipRegionKey,
106
+ regionKind: link.ownershipRegionKind
107
+ }));
108
+ }
109
+ return result;
110
+ }
111
+
112
+ function array(value) {
113
+ if (value === undefined || value === null) return [];
114
+ return Array.isArray(value) ? value : [value];
115
+ }
116
+
117
+ function strings(value) {
118
+ return array(value).map((entry) => String(entry ?? '')).filter(Boolean);
119
+ }
120
+
121
+ function firstString(...values) {
122
+ return values.map((value) => value === undefined || value === null ? '' : String(value)).find(Boolean);
123
+ }
124
+
125
+ function compactRecord(value) {
126
+ return Object.fromEntries(Object.entries(value ?? {}).filter(([, entry]) => entry !== undefined && (!Array.isArray(entry) || entry.length > 0)));
127
+ }
@@ -1,5 +1,12 @@
1
1
  export function sourcePreservationFromProjectionContext(context) {
2
- return context.nativeSource?.metadata?.sourcePreservation
2
+ return context.sourcePreservation
3
+ ?? context.metadata?.sourcePreservation
4
+ ?? context.importResult?.metadata?.sourcePreservation
5
+ ?? context.importResult?.nativeSource?.metadata?.sourcePreservation
6
+ ?? context.importResult?.nativeAst?.metadata?.sourcePreservation
7
+ ?? context.nativeSource?.metadata?.sourcePreservation
3
8
  ?? context.nativeAst?.metadata?.sourcePreservation
4
- ?? context.nativeSource?.ast?.metadata?.sourcePreservation;
9
+ ?? context.nativeSource?.ast?.metadata?.sourcePreservation
10
+ ?? context.universalAst?.metadata?.sourcePreservation
11
+ ?? context.importResult?.universalAst?.metadata?.sourcePreservation;
5
12
  }
@@ -1,12 +1,7 @@
1
+ import { spanOffsets } from './semanticEditSourceRanges.js';
2
+
1
3
  export function sourceTextForSpan(sourceText, span) {
2
4
  if (typeof sourceText !== 'string' || !span) return undefined;
3
- if (typeof span.start === 'number' && typeof span.end === 'number' && span.end >= span.start) {
4
- return sourceText.slice(span.start, span.end);
5
- }
6
- if (typeof span.startLine === 'number') {
7
- const lines = sourceText.split(/\r?\n/);
8
- const endLine = typeof span.endLine === 'number' && span.endLine >= span.startLine ? span.endLine : span.startLine;
9
- return lines.slice(span.startLine - 1, endLine).join('\n');
10
- }
11
- return undefined;
5
+ const range = spanOffsets(sourceText, span);
6
+ return range ? sourceText.slice(range.start, range.end) : undefined;
12
7
  }
@@ -68,12 +68,14 @@ function addIdentifierTarget(map, identifier, target) {
68
68
 
69
69
  function scanDeclarationDependencies(input, documentId, scan, identifiers, lines, records) {
70
70
  const state = { inBlockComment: false };
71
+ const factState = { braceDepth: 0, pendingSwitch: false, switchDepth: 0 };
71
72
  for (let lineNumber = scan.startLine; lineNumber <= scan.endLine; lineNumber += 1) {
72
73
  const scanLine = maskDependencyLine(input, lines[lineNumber - 1]?.line ?? '', state);
73
- addLightweightSemanticFacts(input, documentId, scan.declaration, scanLine, lineNumber, records);
74
+ addLightweightSemanticFacts(input, documentId, scan.declaration, scanLine, lineNumber, records, factState);
74
75
  for (const match of scanLine.matchAll(/[A-Za-z_$][\w$]*/g)) {
75
76
  const name = match[0];
76
77
  if (!isDependencyIdentifier(name) || !identifiers.has(name)) continue;
78
+ if (isIgnoredDependencyOccurrence(input, scanLine, match.index, name)) continue;
77
79
  const targets = identifiers.get(name).filter((target) => target.symbolId !== scan.declaration.symbolId);
78
80
  for (const target of targets) {
79
81
  addDependencyRecord(input, documentId, scan.declaration, target, {
@@ -87,10 +89,11 @@ function scanDeclarationDependencies(input, documentId, scan, identifiers, lines
87
89
  }
88
90
  }
89
91
 
90
- function addLightweightSemanticFacts(input, documentId, declaration, line, lineNumber, records) {
92
+ function addLightweightSemanticFacts(input, documentId, declaration, line, lineNumber, records, factState) {
93
+ if (!shouldScanRuntimeFacts(input, declaration)) return;
91
94
  const text = String(line ?? '').trim();
92
95
  if (!text) return;
93
- for (const item of lightweightControlFlowKinds(text)) {
96
+ for (const item of lightweightControlFlowKinds(text, factState)) {
94
97
  addFactRecord(input, documentId, declaration, 'controlFlow', item, lineNumber, records);
95
98
  }
96
99
  for (const item of lightweightEffectKinds(text)) {
@@ -99,11 +102,45 @@ function addLightweightSemanticFacts(input, documentId, declaration, line, lineN
99
102
  for (const item of lightweightMutationKinds(text)) {
100
103
  addFactRecord(input, documentId, declaration, 'mutation', item, lineNumber, records);
101
104
  }
105
+ updateLightweightFactState(text, factState);
102
106
  }
103
107
 
104
- function lightweightControlFlowKinds(line) {
108
+ function shouldScanRuntimeFacts(input, declaration) {
109
+ if (!isJavaScriptLike(input)) return true;
110
+ if (declaration?.fields?.typeKind) return false;
111
+ if (/^Type(?:Alias|Method|Property|FunctionProperty)/.test(String(declaration?.kind ?? ''))) return false;
112
+ return !['interface', 'type'].includes(String(declaration?.symbolKind ?? '').toLowerCase());
113
+ }
114
+
115
+ function isJavaScriptLike(input) {
116
+ return ['javascript', 'typescript'].includes(String(input?.language ?? '').toLowerCase());
117
+ }
118
+
119
+ function isIgnoredDependencyOccurrence(input, line, startIndex, name) {
120
+ if (!isJavaScriptLike(input)) return false;
121
+ const endIndex = startIndex + String(name).length;
122
+ const previous = previousNonSpace(line, startIndex - 1);
123
+ const next = nextNonSpace(line, endIndex);
124
+ return previous === '.' || next === ':';
125
+ }
126
+
127
+ function previousNonSpace(line, index) {
128
+ for (let cursor = index; cursor >= 0; cursor -= 1) {
129
+ if (!/\s/.test(line[cursor])) return line[cursor];
130
+ }
131
+ return '';
132
+ }
133
+
134
+ function nextNonSpace(line, index) {
135
+ for (let cursor = index; cursor < line.length; cursor += 1) {
136
+ if (!/\s/.test(line[cursor])) return line[cursor];
137
+ }
138
+ return '';
139
+ }
140
+
141
+ function lightweightControlFlowKinds(line, state = {}) {
105
142
  const kinds = [];
106
- if (/\b(if|else|switch|case|default)\b/.test(line)) kinds.push('branch');
143
+ if (hasBranchSyntax(line, state)) kinds.push('branch');
107
144
  if (/\b(for|while|do)\b/.test(line)) kinds.push('loop');
108
145
  if (/\b(return|yield)\b/.test(line)) kinds.push('exit');
109
146
  if (/\b(throw|catch|finally|try)\b/.test(line)) kinds.push('exception');
@@ -114,7 +151,7 @@ function lightweightControlFlowKinds(line) {
114
151
  function lightweightEffectKinds(line) {
115
152
  const kinds = [];
116
153
  if (/\bawait\b|import\s*\(/.test(line)) kinds.push('async');
117
- if (/\b(fetch|XMLHttpRequest|WebSocket|EventSource)\s*\(/.test(line)) kinds.push('network');
154
+ if (hasGlobalNetworkCall(line)) kinds.push('network');
118
155
  if (/\b(localStorage|sessionStorage|indexedDB|caches|cookie)\b/.test(line)) kinds.push('storage');
119
156
  if (/\b(setTimeout|setInterval|requestAnimationFrame|queueMicrotask)\s*\(/.test(line)) kinds.push('scheduler');
120
157
  if (/\b(console|process|Deno|Bun)\s*\./.test(line)) kinds.push('host');
@@ -125,12 +162,81 @@ function lightweightEffectKinds(line) {
125
162
  function lightweightMutationKinds(line) {
126
163
  const kinds = [];
127
164
  if (/\bdelete\s+[A-Za-z_$][\w$.[\]]*/.test(line)) kinds.push('delete');
128
- if (/(?:^|[^=!<>])=(?!=|>)/.test(line)) kinds.push('assignment');
165
+ if (hasRuntimeAssignment(line)) kinds.push('assignment');
129
166
  if (/\+\+|--|(?:\+=|-=|\*=|\/=|%=|\|\|=|&&=|\?\?=)/.test(line)) kinds.push('update');
130
167
  if (/\.(?:push|pop|shift|unshift|splice|sort|reverse|set|add|delete|clear)\s*\(/.test(line)) kinds.push('mutating-call');
131
168
  return kinds;
132
169
  }
133
170
 
171
+ function hasBranchSyntax(line, state) {
172
+ return /\bif\s*\(/.test(line)
173
+ || /(?:^|[}\s;])else\b(?:\s+if\s*\(|\s*[{;]|$)/.test(line)
174
+ || /\bswitch\s*\(/.test(line)
175
+ || ((state?.switchDepth ?? 0) > 0 && /^\s*(?:case\b[^:]*|default)\s*:/.test(line));
176
+ }
177
+
178
+ function updateLightweightFactState(line, state = {}) {
179
+ const hadSwitch = /\bswitch\s*\(/.test(line);
180
+ const beforeDepth = state.braceDepth ?? 0;
181
+ const delta = blockBraceDelta(line);
182
+ if (hadSwitch) state.pendingSwitch = true;
183
+ state.braceDepth = Math.max(0, beforeDepth + delta);
184
+ if (state.pendingSwitch && state.braceDepth > beforeDepth) {
185
+ state.switchDepth = state.braceDepth;
186
+ state.pendingSwitch = false;
187
+ }
188
+ if ((state.switchDepth ?? 0) > 0 && state.braceDepth < state.switchDepth) {
189
+ state.switchDepth = 0;
190
+ }
191
+ }
192
+
193
+ function blockBraceDelta(line) {
194
+ let delta = 0;
195
+ for (const char of String(line ?? '')) {
196
+ if (char === '{') delta += 1;
197
+ else if (char === '}') delta -= 1;
198
+ }
199
+ return delta;
200
+ }
201
+
202
+ function hasGlobalNetworkCall(line) {
203
+ return hasBareCall(line, ['fetch', 'XMLHttpRequest', 'WebSocket', 'EventSource'])
204
+ || hasGlobalPropertyCall(line, ['fetch', 'XMLHttpRequest', 'WebSocket', 'EventSource']);
205
+ }
206
+
207
+ function hasBareCall(line, names) {
208
+ return names.some((name) => new RegExp(`(?:^|[^\\w$.])${name}\\s*\\(`).test(line));
209
+ }
210
+
211
+ function hasGlobalPropertyCall(line, names) {
212
+ return names.some((name) => new RegExp(`\\b(?:window|globalThis|self)\\s*\\.\\s*${name}\\s*\\(`).test(line));
213
+ }
214
+
215
+ function hasRuntimeAssignment(line) {
216
+ const text = String(line ?? '');
217
+ for (let index = 0; index < text.length; index += 1) {
218
+ if (text[index] !== '=' || !isPlainAssignmentOperator(text, index)) continue;
219
+ if (!isLocalDeclarationInitializer(text, index)) return true;
220
+ }
221
+ return false;
222
+ }
223
+
224
+ function isPlainAssignmentOperator(text, index) {
225
+ const previous = text[index - 1] ?? '';
226
+ const next = text[index + 1] ?? '';
227
+ if (next === '=' || next === '>') return false;
228
+ return !['=', '!', '<', '>', '+', '-', '*', '/', '%', '&', '|', '?', '^'].includes(previous);
229
+ }
230
+
231
+ function isLocalDeclarationInitializer(text, index) {
232
+ const prefix = text.slice(0, index);
233
+ const statementStart = Math.max(prefix.lastIndexOf(';'), prefix.lastIndexOf('{'), prefix.lastIndexOf('}'));
234
+ const statement = prefix.slice(statementStart + 1).trim();
235
+ return /^(?:export\s+)?(?:declare\s+)?(?:const|let|var|using)\b/.test(statement)
236
+ || /^for\s*\([^;)]*(?:const|let|var)\b/.test(statement)
237
+ || /^(?:export\s+)?type\s+[A-Za-z_$][\w$]*(?:\s*<[^>]+>)?\s*$/.test(statement);
238
+ }
239
+
134
240
  function addFactRecord(input, documentId, declaration, predicate, factKind, lineNumber, records) {
135
241
  const key = `${declaration.symbolId}|${predicate}|${factKind}|${lineNumber}`;
136
242
  if (records.seen.has(key)) return;
@@ -17,13 +17,21 @@ export const NativeImportLanguageProfiles = Object.freeze([
17
17
  aliases: ['js', 'mjs', 'cjs', 'jsx'],
18
18
  extensions: ['.js', '.mjs', '.cjs', '.jsx'],
19
19
  parserAdapters: ['estree', 'babel', 'tree-sitter'],
20
- lossKinds: ['declarationOnlyCoverage', 'opaqueNative', 'sourceMapApproximation', 'sourcePreservation', 'dynamicRuntime']
20
+ lossKinds: ['declarationOnlyCoverage', 'opaqueNative', 'sourceMapApproximation', 'sourcePreservation', 'dynamicRuntime'],
21
+ notes: [
22
+ 'lightweight scanner records declarations only; exact parser adapters must be injected by the host',
23
+ '.jsx sources are classified as javascript for declaration scanning; JSX element trees remain opaque without host parser evidence'
24
+ ]
21
25
  }),
22
26
  nativeImportLanguageProfile('typescript', {
23
27
  aliases: ['ts', 'tsx'],
24
28
  extensions: ['.ts', '.tsx'],
25
29
  parserAdapters: ['typescript-compiler-api', 'babel', 'tree-sitter'],
26
- lossKinds: ['declarationOnlyCoverage', 'opaqueNative', 'sourceMapApproximation', 'sourcePreservation', 'unsupportedSyntax']
30
+ lossKinds: ['declarationOnlyCoverage', 'opaqueNative', 'sourceMapApproximation', 'sourcePreservation', 'unsupportedSyntax'],
31
+ notes: [
32
+ 'lightweight scanner records declarations only; exact parser adapters must be injected by the host',
33
+ '.tsx sources are classified as typescript for declaration scanning; JSX element trees remain opaque without host parser evidence'
34
+ ]
27
35
  }),
28
36
  nativeImportLanguageProfile('python', {
29
37
  aliases: ['py'],
@@ -1,5 +1,5 @@
1
1
  export function uniqueStrings(values) {
2
- return [...new Set((values ?? []).map((value) => String(value)).filter(Boolean))];
2
+ return [...new Set((values ?? []).filter((value) => value !== undefined && value !== null).map((value) => String(value)).filter(Boolean))];
3
3
  }
4
4
 
5
5
  export function uniqueRecordsById(records) {
@@ -141,3 +141,17 @@ export function idFragment(value) {
141
141
  .replace(/^_+|_+$/g, '')
142
142
  .slice(0, 80) || 'native';
143
143
  }
144
+
145
+ export function caseSensitiveIdFragment(value) {
146
+ const text = String(value ?? 'native');
147
+ return `${idFragment(text)}_${caseSensitiveHash(text)}`;
148
+ }
149
+
150
+ function caseSensitiveHash(value) {
151
+ let hash = 0x811c9dc5;
152
+ for (let index = 0; index < value.length; index += 1) {
153
+ hash ^= value.charCodeAt(index);
154
+ hash = Math.imul(hash, 0x01000193);
155
+ }
156
+ return (hash >>> 0).toString(36);
157
+ }
@@ -4,7 +4,6 @@ import {
4
4
  splitParameters
5
5
  } from './native-region-scanner-core.js';
6
6
  import { jsImportDeclarations } from './native-region-scanner-js-imports.js';
7
-
8
7
  function jsCommentOnlyLine(trimmed) {
9
8
  return trimmed.startsWith('//') || trimmed.startsWith('/*') || trimmed.startsWith('*');
10
9
  }
@@ -121,10 +120,71 @@ function jsRegionKindForDeclarationName(name, source = '') {
121
120
  function jsExportedContainerDeclaration(input, lineNumber, trimmed) {
122
121
  let match = trimmed.match(/^export\s+default\s+(.+)$/);
123
122
  if (match) return jsContainerExport(input, lineNumber, 'ExportDefaultContainer', 'default', match[1], { exportDefault: true });
124
- match = trimmed.match(/^(?:module\.)?exports(?:\.([A-Za-z_$][\w$]*))?\s*=\s*(.+)$/);
123
+ match = trimmed.match(/^(module\.exports|exports)(?:\.([A-Za-z_$][\w$]*))?\s*=\s*(.+)$/);
124
+ if (!match) return undefined;
125
+ const name = match[2] ? `${match[1]}.${match[2]}` : 'module.exports';
126
+ return jsContainerExport(input, lineNumber, 'CommonJsContainerExport', name, match[3], { export: 'commonjs' });
127
+ }
128
+
129
+ function jsExportedFunctionWrapperDeclaration(input, lineNumber, trimmed) {
130
+ const match = trimmed.match(/^export\s+default\s+((?:React\.)?(?:forwardRef|memo|lazy|observer)|Object\.freeze)\s*(?:<[^>]+>)?\s*\(\s*(.+)$/);
131
+ if (!match) return undefined;
132
+ const wrapper = match[1];
133
+ const argument = match[2].trim();
134
+ let functionMatch = argument.match(/^(?:async\s+)?function\*?\s*([A-Za-z_$][\w$]*)?\s*(?:<[^({;]+>)?\s*\(([^)]*)\)/);
135
+ if (functionMatch) {
136
+ return nativeDeclaration(input, lineNumber, 'ExportDefaultFunctionWrapperDeclaration', 'function', functionMatch[1] ?? 'default', {
137
+ exportDefault: true,
138
+ wrapper,
139
+ parameters: splitParameters(functionMatch[2])
140
+ }, true);
141
+ }
142
+ functionMatch = argument.match(/^(?:async\s*)?(?:\(([^)]*)\)|([A-Za-z_$][\w$]*))\s*(?::\s*[^=]+)?=>/);
143
+ if (functionMatch) {
144
+ return nativeDeclaration(input, lineNumber, 'ExportDefaultFunctionWrapperDeclaration', 'function', 'default', {
145
+ exportDefault: true,
146
+ wrapper,
147
+ parameters: splitParameters(functionMatch[1] ?? functionMatch[2])
148
+ }, true);
149
+ }
150
+ const classMatch = argument.match(/^(?:abstract\s+)?class\b(?:\s+(?!(?:extends|implements)\b)([A-Za-z_$][\w$]*))?/);
151
+ if (classMatch) {
152
+ return nativeDeclaration(input, lineNumber, 'ExportDefaultClassWrapperDeclaration', 'class', classMatch[1] ?? 'default', {
153
+ exportDefault: true,
154
+ wrapper
155
+ }, true);
156
+ }
157
+ const aliasMatch = argument.match(/^([A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*)\s*(?:[,)]|$)/);
158
+ if (!aliasMatch) return undefined;
159
+ return nativeDeclaration(input, lineNumber, 'ExportDefaultWrappedAlias', 'variable', 'default', {
160
+ exportDefault: true,
161
+ wrapper,
162
+ alias: aliasMatch[1],
163
+ initializerKind: 'function-wrapper'
164
+ }, false, {
165
+ metadata: { exportDefault: true, wrapper, alias: aliasMatch[1], initializerKind: 'function-wrapper' }
166
+ });
167
+ }
168
+
169
+ function jsExportAliasDeclaration(input, lineNumber, trimmed) {
170
+ let match = trimmed.match(/^export\s+default\s+([A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*)\s*;?$/);
171
+ if (match) return jsAliasExport(input, lineNumber, 'ExportDefaultAlias', 'default', match[1], { exportDefault: true }, trimmed);
172
+ match = trimmed.match(/^module\.exports\s*=\s*([A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*)\s*;?$/);
173
+ if (match) return jsAliasExport(input, lineNumber, 'CommonJsAliasExport', 'module.exports', match[1], { export: 'commonjs' }, trimmed);
174
+ match = trimmed.match(/^(?:module\.)?exports\.([A-Za-z_$][\w$]*)\s*=\s*([A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*)\s*;?$/);
125
175
  if (!match) return undefined;
126
- const name = match[1] ? `exports.${match[1]}` : 'module.exports';
127
- return jsContainerExport(input, lineNumber, 'CommonJsContainerExport', name, match[2], { export: 'commonjs' });
176
+ return jsAliasExport(input, lineNumber, 'CommonJsAliasExport', match[1], match[2], { export: 'commonjs' }, trimmed);
177
+ }
178
+
179
+ function jsAliasExport(input, lineNumber, languageKind, name, alias, fields, source) {
180
+ const regionKind = jsRegionKindForDeclarationName(name, source);
181
+ return nativeDeclaration(input, lineNumber, languageKind, 'variable', name, {
182
+ ...fields,
183
+ alias
184
+ }, false, {
185
+ regionKind,
186
+ metadata: { ...fields, alias }
187
+ });
128
188
  }
129
189
 
130
190
  function jsContainerExport(input, lineNumber, languageKind, name, initializer, fields) {
@@ -254,17 +314,7 @@ function findUnescapedBacktick(text, startIndex) {
254
314
  return -1;
255
315
  }
256
316
 
257
- export {
258
- jsCommentOnlyLine,
259
- jsContainerDelta,
260
- jsDeclarationScanLine,
261
- jsExportedContainerDeclaration,
262
- jsInitializerKind,
263
- jsImportDeclarations,
264
- jsObjectPropertyDeclaration,
265
- jsObjectRegionContext,
266
- jsRegionKindForDeclarationName,
267
- jsRouteRecordDeclaration,
268
- jsVariableHasBody,
269
- jsVariableSymbolKind
270
- };
317
+ export { jsCommentOnlyLine, jsContainerDelta, jsDeclarationScanLine };
318
+ export { jsExportAliasDeclaration, jsExportedContainerDeclaration, jsExportedFunctionWrapperDeclaration };
319
+ export { jsInitializerKind, jsImportDeclarations, jsObjectPropertyDeclaration, jsObjectRegionContext };
320
+ export { jsRegionKindForDeclarationName, jsRouteRecordDeclaration, jsVariableHasBody, jsVariableSymbolKind };
@@ -44,6 +44,13 @@ export function jsImportDeclarations(input, lineNumber, trimmed) {
44
44
  importedName: 'default',
45
45
  importKind: trimmed.includes('import') ? 'dynamic-import-binding' : 'commonjs-require'
46
46
  }]);
47
+ const sideEffectRequireMatch = trimmed.match(/^require\s*\(\s*(['"])([^'"]+)\1\s*\)\s*;?$/);
48
+ if (sideEffectRequireMatch) {
49
+ return jsImportModuleDeclarations(input, lineNumber, sideEffectRequireMatch[2], 'CommonJsSideEffectRequireDeclaration', [], {
50
+ sideEffectOnly: true,
51
+ importKind: 'commonjs-require'
52
+ });
53
+ }
47
54
  return [];
48
55
  }
49
56
 
@@ -10,7 +10,9 @@ import {
10
10
  jsCommentOnlyLine,
11
11
  jsContainerDelta,
12
12
  jsDeclarationScanLine,
13
+ jsExportAliasDeclaration,
13
14
  jsExportedContainerDeclaration,
15
+ jsExportedFunctionWrapperDeclaration,
14
16
  jsInitializerKind,
15
17
  jsImportDeclarations,
16
18
  jsObjectPropertyDeclaration,
@@ -75,11 +77,13 @@ function scanJavaScriptLike(input) {
75
77
  pushDeclaration(nativeDeclaration(input, number, 'FunctionDeclaration', 'function', match[1], { parameters: splitParameters(match[2]) }, declarationLine.includes('{')));
76
78
  } else if ((match = trimmed.match(/^export\s+default\s+(?:async\s+)?function\*?\s*([A-Za-z_$][\w$]*)?\s*(?:<[^({;]+>)?\s*\(([^)]*)\)\s*(?::\s*[^={]+)?/))) {
77
79
  pushDeclaration(nativeDeclaration(input, number, 'ExportDefaultFunctionDeclaration', 'function', match[1] ?? 'default', { parameters: splitParameters(match[2]), exportDefault: true }, trimmed.includes('{')));
78
- } else if ((match = declarationLine.match(/^(?:default\s+)?(?:abstract\s+)?class\s+([A-Za-z_$][\w$]*)/))) {
79
- pushDeclaration(nativeDeclaration(input, number, declarationLine.startsWith('default ') ? 'ExportDefaultClassDeclaration' : 'ClassDeclaration', 'class', match[1], { exportDefault: declarationLine.startsWith('default ') || undefined }, declarationLine.includes('{')));
80
- pushDeclarations(jsInlineClassMemberDeclarations(input, number, declarationLine, match[1]));
80
+ } else if ((match = declarationLine.match(/^(default\s+)?(?:abstract\s+)?class\b(?:\s+(?!(?:extends|implements)\b)([A-Za-z_$][\w$]*))?/)) && (match[1] || match[2])) {
81
+ const className = match[2] ?? 'default';
82
+ const exportDefault = Boolean(match[1]);
83
+ pushDeclaration(nativeDeclaration(input, number, exportDefault ? 'ExportDefaultClassDeclaration' : 'ClassDeclaration', 'class', className, { exportDefault: exportDefault || undefined }, declarationLine.includes('{')));
84
+ pushDeclarations(jsInlineClassMemberDeclarations(input, number, declarationLine, className));
81
85
  if (jsStructureDelta(declarationLine).value > 0) {
82
- currentClass = match[1];
86
+ currentClass = className;
83
87
  classDepth = 0;
84
88
  }
85
89
  } else if ((match = declarationLine.match(/^interface\s+([A-Za-z_$][\w$]*)/))) {
@@ -109,27 +113,31 @@ function scanJavaScriptLike(input) {
109
113
  pushDeclarations(jsInlineNestedObjectDeclarations(input, number, declarationLine, declarations[declarations.length - 1]));
110
114
  const objectContext = jsObjectRegionContext(match[1], declarationLine, number, regionKind);
111
115
  if (objectContext) objectStack.push(objectContext);
116
+ } else if ((match = jsExportedFunctionWrapperDeclaration(input, number, trimmed))) {
117
+ pushDeclaration(match);
112
118
  } else if ((match = jsExportedContainerDeclaration(input, number, trimmed))) {
113
119
  pushDeclaration(match.declaration);
114
120
  pushDeclarations(jsInlineNestedObjectDeclarations(input, number, trimmed, match.declaration));
115
121
  if (match.context) objectStack.push(match.context);
122
+ } else if ((match = jsExportAliasDeclaration(input, number, trimmed))) {
123
+ pushDeclaration(match);
116
124
  } else if ((match = trimmed.match(/^(?:module\.)?exports\.([A-Za-z_$][\w$]*)\s*=\s*(?:async\s+)?function\*?\s*\(([^)]*)\)/))) {
117
125
  pushDeclaration(nativeDeclaration(input, number, 'CommonJsFunctionExport', 'function', match[1], { parameters: splitParameters(match[2]) }, true));
118
126
  } else if ((match = trimmed.match(/^(?:module\.)?exports\.([A-Za-z_$][\w$]*)\s*=/))) {
119
127
  const regionKind = jsRegionKindForDeclarationName(match[1], trimmed);
120
128
  pushDeclaration(nativeDeclaration(input, number, 'CommonJsExport', 'variable', match[1], { export: 'commonjs' }, false, { regionKind }));
121
- } else if (currentClass && (match = declarationLine.match(/^(?:(?:public|private|protected|static|async|override|readonly|abstract|accessor|get|set)\s+)*(?:async\s+)?(?:get\s+|set\s+)?([A-Za-z_$][\w$]*)\??\s*(?:<[^({;]+>)?\s*\(([^)]*)\)\s*(?::\s*[^={]+)?(?:\{|=>|$)/)) && !jsControlKeyword(match[1])) {
129
+ } else if (currentClass && (match = declarationLine.match(/^(?:(?:public|private|protected|static|async|override|readonly|abstract|accessor|get|set)\s+)*(?:async\s+)?(?:get\s+|set\s+)?(#?[A-Za-z_$][\w$]*)\??\s*(?:<[^({;]+>)?\s*\(([^)]*)\)\s*(?::\s*[^={]+)?(?:\{|=>|$)/)) && !jsControlKeyword(match[1])) {
122
130
  pushDeclaration(nativeDeclaration(input, number, 'MethodDefinition', 'method', `${currentClass}.${match[1]}`, {
123
131
  methodName: match[1],
124
132
  owner: currentClass,
125
133
  parameters: splitParameters(match[2])
126
134
  }, declarationLine.includes('{') || declarationLine.includes('=>')));
127
- } else if (currentClass && (match = declarationLine.match(/^(?:(?:public|private|protected|static|readonly|declare|accessor)\s+)*([A-Za-z_$][\w$]*)[?!]?\s*(?::\s*([^=;{]+))?(?:[=;]|$)/))) {
135
+ } else if (currentClass && (match = declarationLine.match(/^(?:(?:public|private|protected|static|readonly|declare|accessor)\s+)*(#?[A-Za-z_$][\w$]*)[?!]?\s*(?::\s*([^=;{]+))?(?:[=;]|$)/))) {
128
136
  pushDeclaration(nativeDeclaration(input, number, 'PropertyDefinition', 'property', `${currentClass}.${match[1]}`, {
129
137
  propertyName: match[1],
130
138
  owner: currentClass,
131
139
  valueType: match[2]?.trim()
132
- }, false));
140
+ }, false, { regionKind: 'property' }));
133
141
  }
134
142
  if (currentClass) {
135
143
  classDepth += braceDelta(trimmed);
@@ -263,7 +271,7 @@ function jsInlineClassMemberDeclarations(input, lineNumber, declarationLine, cla
263
271
  if (open < 0 || close <= open) return [];
264
272
  const body = declarationLine.slice(open + 1, close);
265
273
  const declarations = [];
266
- for (const match of body.matchAll(/(?:(?:public|private|protected|static|async|override|readonly|abstract|accessor|get|set)\s+)*(?:async\s+)?(?:get\s+|set\s+)?([A-Za-z_$][\w$]*)\??\s*(?:<[^({;]+>)?\s*\(([^)]*)\)\s*(?::\s*[^={;]+)?\s*(?:\{|=>)/g)) {
274
+ for (const match of body.matchAll(/(?:(?:public|private|protected|static|async|override|readonly|abstract|accessor|get|set)\s+)*(?:async\s+)?(?:get\s+|set\s+)?(#?[A-Za-z_$][\w$]*)\??\s*(?:<[^({;]+>)?\s*\(([^)]*)\)\s*(?::\s*[^={;]+)?\s*(?:\{|=>)/g)) {
267
275
  if (jsControlKeyword(match[1])) continue;
268
276
  declarations.push(nativeDeclaration(input, lineNumber, 'MethodDefinition', 'method', `${className}.${match[1]}`, {
269
277
  methodName: match[1],
@@ -26,6 +26,7 @@ import {
26
26
  scanHaskell,
27
27
  scanR
28
28
  } from './native-region-scanner-functional.js';
29
+ import { normalizeNativeLanguageId } from './native-import-utils.js';
29
30
  export { lightweightCoverageLosses } from './native-region-scanner-core.js';
30
31
  export {
31
32
  detectNewlineStyle,
@@ -34,7 +35,7 @@ export {
34
35
  } from './native-source-preservation-scanner.js';
35
36
 
36
37
  function scanNativeDeclarations(input) {
37
- const language = String(input.language).toLowerCase();
38
+ const language = normalizeNativeLanguageId(input.language) || String(input.language).toLowerCase();
38
39
  if (language === 'javascript' || language === 'typescript') return scanJavaScriptLike(input);
39
40
  if (language === 'python') return scanPython(input);
40
41
  if (language === 'rust') return scanRust(input);
@@ -1,4 +1,4 @@
1
- import { idFragment, uniqueStrings } from './native-import-utils.js';
1
+ import { caseSensitiveIdFragment, idFragment, uniqueStrings } from './native-import-utils.js';
2
2
 
3
3
  const NativeImportRegionTaxonomyKinds = Object.freeze([
4
4
  'symbol',
@@ -20,20 +20,20 @@ function semanticOwnershipRegionForSymbol(imported, symbol, mapping, nativeNode,
20
20
  const language = symbol.language ?? imported?.language ?? imported?.nativeAst?.language ?? imported?.nativeSource?.language;
21
21
  const sourceSpan = mapping?.sourceSpan ?? symbol.definitionSpan ?? nativeNode?.span;
22
22
  const regionKind = semanticRegionKindForSymbol(symbol, mapping, nativeNode);
23
- const key = [
23
+ const key = symbol?.metadata?.ownershipRegionKey ?? [
24
24
  options.regionPrefix ?? 'source',
25
25
  sourcePath ?? `${language}:memory`,
26
26
  regionKind,
27
27
  symbol.name ?? symbol.id
28
28
  ].map((part) => String(part).replace(/\s+/g, ' ').trim()).join('#');
29
29
  return {
30
- id: `region_${idFragment(key)}`,
30
+ id: symbol?.metadata?.ownershipRegionId ?? `region_${caseSensitiveIdFragment(key)}`,
31
31
  key,
32
32
  regionKind,
33
33
  granularity: 'symbol',
34
34
  language,
35
35
  sourcePath,
36
- sourceHash: imported?.nativeSource?.sourceHash ?? imported?.nativeAst?.sourceHash,
36
+ sourceHash: imported?.nativeSource?.sourceHash ?? imported?.nativeAst?.sourceHash ?? imported?.sourceHash,
37
37
  symbolId: symbol.id,
38
38
  symbolName: symbol.name,
39
39
  symbolKind: symbol.kind,
@@ -54,7 +54,7 @@ function semanticOwnershipRegionForDeclaration(input, declaration, documentId) {
54
54
  const regionKind = semanticRegionKindForDeclaration(declaration);
55
55
  const key = ['source', sourcePath, regionKind, name].map((part) => String(part).replace(/\s+/g, ' ').trim()).join('#');
56
56
  return {
57
- id: `region_${idFragment(key)}`,
57
+ id: `region_${caseSensitiveIdFragment(key)}`,
58
58
  key,
59
59
  regionKind,
60
60
  granularity: 'symbol',
@@ -76,17 +76,19 @@ function semanticOwnershipRegionForDeclaration(input, declaration, documentId) {
76
76
  }
77
77
 
78
78
  function semanticPatchHintForRegion(region, readiness, options = {}) {
79
+ const supportedOperations = semanticRegionSupportedOperations(region);
79
80
  return {
80
81
  id: `hint_${idFragment(region.id)}`,
81
82
  kind: 'source-region-patch',
82
83
  ownershipRegionId: region.id,
83
84
  ownershipKey: region.key,
85
+ operation: supportedOperations[0] ?? 'replace-region',
84
86
  sourcePath: region.sourcePath,
85
87
  sourceHash: region.sourceHash,
86
88
  sourceSpan: region.sourceSpan,
87
89
  readiness,
88
90
  precision: region.precision,
89
- supportedOperations: semanticRegionSupportedOperations(region),
91
+ supportedOperations,
90
92
  projection: {
91
93
  sourceLanguage: region.language,
92
94
  targetPath: options.targetPath ?? region.sourcePath,