@shapeshift-labs/frontier-lang-compiler 0.2.142 → 0.2.143

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.
@@ -15,6 +15,7 @@ import {
15
15
  createStagedDeclarationReplayRecord
16
16
  } from './js-ts-safe-merge-staged-declaration-replay.js';
17
17
  import { createStagedTopLevelSemanticFallback } from './js-ts-safe-merge-staged-top-level-fallback.js';
18
+ import { createVariableDeclaratorSemanticFallbackResult } from './js-ts-safe-merge-variable-declarator-fallback.js';
18
19
  import { idFragment, uniqueStrings } from './native-import-utils.js';
19
20
 
20
21
  function semanticEditFallbackResult(input, topLevelResult) {
@@ -29,7 +30,11 @@ function semanticEditFallbackResult(input, topLevelResult) {
29
30
  if (nextArtifacts.status === 'verified') selectedFallback = candidate;
30
31
  artifacts = nextArtifacts.status === 'verified' ? nextArtifacts : artifacts;
31
32
  }
32
- if (artifacts.status !== 'verified') return semanticEditFallbackBlockedResult(input, topLevelResult, artifacts);
33
+ if (artifacts.status !== 'verified') {
34
+ const variableDeclaratorResult = createVariableDeclaratorSemanticFallbackResult(input, topLevelResult, stagedFallback);
35
+ if (variableDeclaratorResult) return variableDeclaratorResult;
36
+ return semanticEditFallbackBlockedResult(input, topLevelResult, artifacts);
37
+ }
33
38
  const resultBase = selectedFallback?.stagedTopLevelResult ?? topLevelResult;
34
39
  const mergedSourceText = artifacts.projection.sourceText;
35
40
  const gates = semanticEditGates(artifacts);
@@ -0,0 +1,188 @@
1
+ import { JsTsSafeMergeStatuses } from './js-ts-safe-merge-constants.js';
2
+ import { createJsTsSafeMergeSemanticArtifacts } from './js-ts-safe-merge-semantic-artifacts.js';
3
+ import { uniqueStrings } from './js-ts-safe-merge-context.js';
4
+ import { semanticFallbackChangedExistingDeclarations } from './js-ts-safe-merge-semantic-edit-fallback-utils.js';
5
+ import {
6
+ sameVariableDeclaratorText,
7
+ sameVariableStatementShell,
8
+ variableStatementsByKey
9
+ } from './js-ts-safe-merge-variable-declarator-parser.js';
10
+
11
+ function createVariableDeclaratorSemanticFallbackResult(input, topLevelResult, stagedFallback) {
12
+ const currentSourceText = fallbackCurrentSourceText(input, stagedFallback);
13
+ const merge = mergeVariableDeclaratorSources({
14
+ baseSourceText: input.baseSourceText,
15
+ workerSourceText: input.workerSourceText,
16
+ headSourceText: input.headSourceText,
17
+ currentSourceText
18
+ });
19
+ if (!merge.ok || merge.sourceText === currentSourceText) return undefined;
20
+ const resultBase = stagedFallback?.stagedTopLevelResult ?? topLevelResult;
21
+ const language = input.language ?? topLevelResult.language ?? 'typescript';
22
+ const sourcePath = input.sourcePath ?? topLevelResult.sourcePath ?? 'inline.ts';
23
+ const phase = stagedFallback
24
+ ? 'staged-top-level-variable-declarator-semantic-fallback'
25
+ : 'variable-declarator-semantic-fallback';
26
+ const artifacts = createJsTsSafeMergeSemanticArtifacts({
27
+ ...input,
28
+ id: `${String(input.id ?? topLevelResult.id ?? 'js_ts_safe_merge')}_variable_declarator`,
29
+ language,
30
+ sourcePath,
31
+ headSourceText: currentSourceText,
32
+ headHash: undefined,
33
+ currentSourceHash: undefined
34
+ }, {
35
+ ...resultBase,
36
+ id: `${String(input.id ?? resultBase.id ?? 'js_ts_safe_merge')}_variable_declarator`,
37
+ language,
38
+ sourcePath,
39
+ mergedSourceText: merge.sourceText,
40
+ outputSourceText: merge.sourceText
41
+ });
42
+ if (artifacts.status !== 'verified') return undefined;
43
+ const gates = semanticArtifactGates(artifacts);
44
+ return {
45
+ ...resultBase,
46
+ id: String(input.id ?? resultBase.id ?? topLevelResult.id),
47
+ status: JsTsSafeMergeStatuses.merged,
48
+ mergedSourceText: merge.sourceText,
49
+ outputSourceText: merge.sourceText,
50
+ conflicts: [],
51
+ gates,
52
+ admission: {
53
+ status: 'auto-merge-candidate',
54
+ action: 'apply',
55
+ reviewRequired: false,
56
+ autoApplyCandidate: true,
57
+ autoMergeClaim: false,
58
+ semanticEquivalenceClaim: false,
59
+ reasonCodes: []
60
+ },
61
+ summary: {
62
+ ...resultBase.summary,
63
+ changedExistingDeclarations: semanticFallbackChangedExistingDeclarations(topLevelResult, resultBase, stagedFallback),
64
+ conflicts: 0,
65
+ gatesPassed: gates.filter((gate) => gate.status === 'passed').length,
66
+ semanticEditOperations: artifacts.script.summary.operations,
67
+ semanticEditAppliedOperations: artifacts.replay.summary.applied,
68
+ semanticEditReplayStatus: artifacts.replay.status,
69
+ variableDeclaratorStatements: merge.summary.statements,
70
+ variableDeclaratorEdits: merge.summary.edits,
71
+ composedPhases: 2
72
+ },
73
+ metadata: {
74
+ ...resultBase.metadata,
75
+ composed: {
76
+ phase,
77
+ phases: stagedFallback
78
+ ? ['top-level-neutralization', 'top-level-ledger', 'variable-declarator']
79
+ : ['top-level-ledger', 'variable-declarator'],
80
+ originalReasonCodes: topLevelResult.admission?.reasonCodes ?? [],
81
+ stagedTopLevelSummary: stagedFallback?.stagedTopLevelResult?.summary,
82
+ neutralization: stagedFallback?.neutralization?.summary,
83
+ variableDeclaratorFallback: merge.summary
84
+ }
85
+ },
86
+ semanticArtifacts: artifacts
87
+ };
88
+ }
89
+
90
+ function fallbackCurrentSourceText(input, stagedFallback) {
91
+ return stagedFallback?.directReplayCurrentSourceText
92
+ ?? stagedFallback?.replayCurrentSourceText
93
+ ?? input.headSourceText;
94
+ }
95
+
96
+ function mergeVariableDeclaratorSources(input) {
97
+ if (![input.baseSourceText, input.workerSourceText, input.headSourceText, input.currentSourceText].every((text) => typeof text === 'string')) {
98
+ return blocked('missing-source-text');
99
+ }
100
+ const base = variableStatementsByKey(input.baseSourceText);
101
+ const worker = variableStatementsByKey(input.workerSourceText);
102
+ const head = variableStatementsByKey(input.headSourceText);
103
+ const current = variableStatementsByKey(input.currentSourceText);
104
+ if ([base, worker, head, current].some((source) => source.reasonCodes.length)) {
105
+ return blocked('variable-declarator-parse-blocked');
106
+ }
107
+ const edits = [];
108
+ let changedStatements = 0;
109
+ for (const statement of base.statements) {
110
+ const key = statement.names.join('\0');
111
+ const workerStatement = worker.byKey.get(key);
112
+ const headStatement = head.byKey.get(key);
113
+ const currentStatement = current.byKey.get(key);
114
+ if (!workerStatement || !headStatement || !currentStatement) continue;
115
+ const merged = mergeVariableStatement(statement, workerStatement, headStatement, currentStatement);
116
+ if (merged.status === 'blocked') return blocked(...merged.reasonCodes);
117
+ if (!merged.replacement || sameVariableDeclaratorText(merged.replacement, currentStatement.text)) continue;
118
+ edits.push({ start: currentStatement.start, end: currentStatement.end, replacement: merged.replacement });
119
+ changedStatements += 1;
120
+ }
121
+ if (!edits.length) return blocked('no-variable-declarator-merge-candidate');
122
+ const sourceText = edits.sort((left, right) => right.start - left.start)
123
+ .reduce((text, edit) => text.slice(0, edit.start) + edit.replacement + text.slice(edit.end), input.currentSourceText);
124
+ return {
125
+ ok: true,
126
+ sourceText,
127
+ summary: {
128
+ statements: changedStatements,
129
+ edits: edits.length
130
+ }
131
+ };
132
+ }
133
+
134
+ function mergeVariableStatement(base, worker, head, current) {
135
+ if (!sameVariableStatementShell(base, worker) || !sameVariableStatementShell(base, head) || !sameVariableStatementShell(base, current)) {
136
+ return blockedStatement('variable-declarator-shell-changed');
137
+ }
138
+ const declarators = [];
139
+ let changed = false;
140
+ for (let index = 0; index < base.declarators.length; index += 1) {
141
+ const baseDeclarator = base.declarators[index];
142
+ const workerDeclarator = worker.declarators[index];
143
+ const headDeclarator = head.declarators[index];
144
+ const currentDeclarator = current.declarators[index];
145
+ if (![workerDeclarator, headDeclarator, currentDeclarator].every((entry) => entry?.name === baseDeclarator.name)) {
146
+ return blockedStatement('variable-declarator-name-order-changed');
147
+ }
148
+ const workerChanged = !sameVariableDeclaratorText(baseDeclarator.text, workerDeclarator.text);
149
+ const headChanged = !sameVariableDeclaratorText(baseDeclarator.text, headDeclarator.text);
150
+ if (workerChanged && headChanged && !sameVariableDeclaratorText(workerDeclarator.text, headDeclarator.text)) {
151
+ return blockedStatement('variable-declarator-conflict');
152
+ }
153
+ if (!sameVariableDeclaratorText(currentDeclarator.text, headDeclarator.text)
154
+ && !sameVariableDeclaratorText(currentDeclarator.text, workerDeclarator.text)) {
155
+ return blockedStatement('variable-declarator-current-diverged');
156
+ }
157
+ const replacement = workerChanged ? workerDeclarator.text : currentDeclarator.text;
158
+ if (!sameVariableDeclaratorText(replacement, currentDeclarator.text)) changed = true;
159
+ declarators.push(replacement);
160
+ }
161
+ return {
162
+ status: 'merged',
163
+ replacement: changed ? `${current.prefix}${declarators.join(', ')}${current.suffix}` : undefined
164
+ };
165
+ }
166
+
167
+ function semanticArtifactGates(artifacts) {
168
+ return [
169
+ gate('semantic-edit-script', artifacts.script?.admission?.status === 'auto-merge-candidate', artifacts.script?.admission?.reasonCodes),
170
+ gate('semantic-edit-projection', artifacts.projection?.status === 'projected', artifacts.projection?.admission?.reasonCodes),
171
+ gate('semantic-edit-replay', artifacts.replay?.status === 'accepted-clean', artifacts.replay?.admission?.reasonCodes),
172
+ gate('semantic-edit-already-applied', artifacts.alreadyAppliedReplay?.status === 'already-applied', artifacts.alreadyAppliedReplay?.admission?.reasonCodes)
173
+ ];
174
+ }
175
+
176
+ function gate(id, passed, reasonCodes = []) {
177
+ return { id, status: passed ? 'passed' : 'blocked', reasonCodes: passed ? [] : uniqueStrings(reasonCodes) };
178
+ }
179
+
180
+ function blocked(...reasonCodes) {
181
+ return { ok: false, reasonCodes: uniqueStrings(reasonCodes) };
182
+ }
183
+
184
+ function blockedStatement(...reasonCodes) {
185
+ return { status: 'blocked', reasonCodes: uniqueStrings(reasonCodes) };
186
+ }
187
+
188
+ export { createVariableDeclaratorSemanticFallbackResult };
@@ -0,0 +1,179 @@
1
+ import { normalizeLineEndings, uniqueStrings } from './js-ts-safe-merge-context.js';
2
+
3
+ function variableStatementsByKey(sourceText) {
4
+ const reasonCodes = [];
5
+ const statements = topLevelStatements(sourceText)
6
+ .map((statement) => parseVariableStatement(statement))
7
+ .filter(Boolean);
8
+ const byKey = new Map();
9
+ for (const statement of statements) {
10
+ const key = statement.names.join('\0');
11
+ if (byKey.has(key)) reasonCodes.push('duplicate-variable-declarator-statement-key');
12
+ byKey.set(key, statement);
13
+ }
14
+ return { statements, byKey, reasonCodes: uniqueStrings(reasonCodes) };
15
+ }
16
+
17
+ function parseVariableStatement(statement) {
18
+ const text = statement.text;
19
+ if (/[`]/.test(text)) return undefined;
20
+ const leading = text.match(/^\s*/)?.[0].length ?? 0;
21
+ const meaningful = text.slice(leading);
22
+ const match = /^(?:export\s+)?(?:declare\s+)?(?:const|let|var)\s+/u.exec(meaningful);
23
+ if (!match) return undefined;
24
+ const prefixEnd = leading + match[0].length;
25
+ const suffixStart = statementSuffixStart(text);
26
+ if (suffixStart <= prefixEnd) return undefined;
27
+ const declaratorText = text.slice(prefixEnd, suffixStart);
28
+ if (/[\r\n]/.test(declaratorText)) return undefined;
29
+ const declarators = splitDeclarators(declaratorText, prefixEnd);
30
+ if (declarators.length < 2 || declarators.some((entry) => !entry.name)) return undefined;
31
+ return {
32
+ start: statement.start,
33
+ end: statement.end,
34
+ text,
35
+ prefix: text.slice(0, prefixEnd),
36
+ suffix: text.slice(suffixStart),
37
+ names: declarators.map((entry) => entry.name),
38
+ declarators
39
+ };
40
+ }
41
+
42
+ function statementSuffixStart(text) {
43
+ let index = text.length;
44
+ while (index > 0 && /\s/u.test(text[index - 1])) index -= 1;
45
+ if (text[index - 1] === ';') index -= 1;
46
+ while (index > 0 && /\s/u.test(text[index - 1])) index -= 1;
47
+ return index;
48
+ }
49
+
50
+ function splitDeclarators(text, offset) {
51
+ const parts = [];
52
+ let start = 0;
53
+ let state = 'code';
54
+ let depth = 0;
55
+ for (let index = 0; index < text.length; index += 1) {
56
+ const char = text[index];
57
+ const next = text[index + 1];
58
+ if (state === 'line-comment') {
59
+ if (char === '\n') state = 'code';
60
+ continue;
61
+ }
62
+ if (state === 'block-comment') {
63
+ if (char === '*' && next === '/') {
64
+ index += 1;
65
+ state = 'code';
66
+ }
67
+ continue;
68
+ }
69
+ if (state !== 'code') {
70
+ if (char === '\\') index += 1;
71
+ else if (char === state) state = 'code';
72
+ continue;
73
+ }
74
+ if (char === '/' && next === '/') {
75
+ state = 'line-comment';
76
+ index += 1;
77
+ continue;
78
+ }
79
+ if (char === '/' && next === '*') {
80
+ state = 'block-comment';
81
+ index += 1;
82
+ continue;
83
+ }
84
+ if (char === '"' || char === "'") {
85
+ state = char;
86
+ continue;
87
+ }
88
+ if (char === '(' || char === '[' || char === '{') depth += 1;
89
+ else if (char === ')' || char === ']' || char === '}') depth -= 1;
90
+ else if (char === ',' && depth === 0) {
91
+ parts.push(declaratorPart(text, start, index, offset));
92
+ start = index + 1;
93
+ }
94
+ if (depth < 0) return [];
95
+ }
96
+ if (state !== 'code' || depth !== 0) return [];
97
+ parts.push(declaratorPart(text, start, text.length, offset));
98
+ return parts;
99
+ }
100
+
101
+ function declaratorPart(text, start, end, offset) {
102
+ let localStart = start;
103
+ let localEnd = end;
104
+ while (localStart < localEnd && /\s/u.test(text[localStart])) localStart += 1;
105
+ while (localEnd > localStart && /\s/u.test(text[localEnd - 1])) localEnd -= 1;
106
+ const value = text.slice(localStart, localEnd);
107
+ const name = /^[A-Za-z_$][\w$]*/u.exec(value)?.[0];
108
+ return {
109
+ name: name && value[name.length] !== '?' ? name : undefined,
110
+ text: value,
111
+ start: offset + localStart,
112
+ end: offset + localEnd
113
+ };
114
+ }
115
+
116
+ function topLevelStatements(sourceText) {
117
+ const statements = [];
118
+ let start = 0;
119
+ let state = 'code';
120
+ let depth = 0;
121
+ for (let index = 0; index < sourceText.length; index += 1) {
122
+ const char = sourceText[index];
123
+ const next = sourceText[index + 1];
124
+ if (state === 'line-comment') {
125
+ if (char === '\n') state = 'code';
126
+ continue;
127
+ }
128
+ if (state === 'block-comment') {
129
+ if (char === '*' && next === '/') {
130
+ index += 1;
131
+ state = 'code';
132
+ }
133
+ continue;
134
+ }
135
+ if (state !== 'code') {
136
+ if (char === '\\') index += 1;
137
+ else if (char === state) state = 'code';
138
+ continue;
139
+ }
140
+ if (char === '/' && next === '/') {
141
+ state = 'line-comment';
142
+ index += 1;
143
+ continue;
144
+ }
145
+ if (char === '/' && next === '*') {
146
+ state = 'block-comment';
147
+ index += 1;
148
+ continue;
149
+ }
150
+ if (char === '"' || char === "'" || char === '`') {
151
+ state = char;
152
+ continue;
153
+ }
154
+ if (char === '(' || char === '[' || char === '{') depth += 1;
155
+ else if (char === ')' || char === ']' || char === '}') depth -= 1;
156
+ else if (char === ';' && depth === 0) {
157
+ statements.push({ start, end: index + 1, text: sourceText.slice(start, index + 1) });
158
+ start = index + 1;
159
+ }
160
+ }
161
+ if (sourceText.slice(start).trim()) statements.push({ start, end: sourceText.length, text: sourceText.slice(start) });
162
+ return statements;
163
+ }
164
+
165
+ function sameVariableStatementShell(left, right) {
166
+ return sameVariableDeclaratorText(left.prefix, right.prefix)
167
+ && sameVariableDeclaratorText(left.suffix, right.suffix)
168
+ && left.names.join('\0') === right.names.join('\0');
169
+ }
170
+
171
+ function sameVariableDeclaratorText(left, right) {
172
+ return normalizeLineEndings(String(left ?? '').trim(), '\n') === normalizeLineEndings(String(right ?? '').trim(), '\n');
173
+ }
174
+
175
+ export {
176
+ sameVariableDeclaratorText,
177
+ sameVariableStatementShell,
178
+ variableStatementsByKey
179
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshift-labs/frontier-lang-compiler",
3
- "version": "0.2.142",
3
+ "version": "0.2.143",
4
4
  "description": "Compiler facade for Frontier Lang source documents and language projection adapters.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",