presolve-compiler 0.2.0-beta.1

The Presolve compiler toolchain for TypeScript web applications.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! Decorator-free V2 computed-getter recognition.
//!
//! Computed getters are derived candidates, not framework intrinsics. The
//! parser supplies source-faithful getter expressions; this lowering admits
//! only the closed direct-State, call-free subset described by the V2 contract.

use std::collections::{BTreeMap, BTreeSet};

use presolve_parser::{
    ParsedComputedExpression, ParsedComputedExpressionKind, ParsedFile, SourceSpan,
};

use crate::{
    normalize_authored_semantics_v1, AuthoredSemanticCandidateKindV1,
    AuthoredSemanticNormalizationErrorV1, AuthoredSourceRangeV1,
    CanonicalAuthoredDeclarationKindV1, CanonicalAuthoredSemanticModelV1,
    ResolvedAuthoredSemanticCandidateV1,
};

/// One analysis-proven non-intrinsic computed getter site.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ComputedGetterSiteV1 {
    pub subject: String,
    pub declaration_source: AuthoredSourceRangeV1,
    pub state_dependencies: Vec<String>,
    pub computed_dependencies: Vec<String>,
}

#[derive(Debug, Clone)]
struct ComputedGetterCandidateV1 {
    source: AuthoredSourceRangeV1,
    state_reads: BTreeSet<String>,
    computed_reads: BTreeSet<String>,
}

/// The canonical derived-getter product for one source file.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ComputedGetterLoweringV1 {
    pub sites: Vec<ComputedGetterSiteV1>,
    pub model: CanonicalAuthoredSemanticModelV1,
}

/// A source-to-canonical-model mismatch while selecting derived getters.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ComputedGetterLoweringErrorV1 {
    SourcePathMismatch,
    UnknownComponentDeclaration { start: usize, end: usize },
    InvalidAuthoredSemantics(AuthoredSemanticNormalizationErrorV1),
}

impl std::fmt::Display for ComputedGetterLoweringErrorV1 {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::SourcePathMismatch => write!(
                formatter,
                "canonical component/State model and derived computed getter source must match"
            ),
            Self::UnknownComponentDeclaration { start, end } => write!(
                formatter,
                "canonical component declaration has no source class at {start}..{end}"
            ),
            Self::InvalidAuthoredSemantics(error) => error.fmt(formatter),
        }
    }
}

impl std::error::Error for ComputedGetterLoweringErrorV1 {}

/// Select only getters that have complete direct-State and pure-expression
/// proof. Unsupported getters deliberately remain ordinary JavaScript.
pub fn computed_getter_sites_v1(
    parsed: &ParsedFile,
    model: &CanonicalAuthoredSemanticModelV1,
) -> Result<Vec<ComputedGetterSiteV1>, ComputedGetterLoweringErrorV1> {
    if model.source_path != parsed.path {
        return Err(ComputedGetterLoweringErrorV1::SourcePathMismatch);
    }
    let components = model
        .declarations
        .iter()
        .filter(|declaration| declaration.kind == CanonicalAuthoredDeclarationKindV1::Component)
        .map(|declaration| (declaration.subject.clone(), range_key(declaration.source)))
        .collect::<BTreeSet<_>>();
    for (subject, declaration_key) in &components {
        if !parsed
            .classes
            .iter()
            .any(|class| class.name == *subject && range_key(range(class.span)) == *declaration_key)
        {
            return Err(ComputedGetterLoweringErrorV1::UnknownComponentDeclaration {
                start: declaration_key.0,
                end: declaration_key.1,
            });
        }
    }

    let state_names_by_component = model
        .declarations
        .iter()
        .filter(|declaration| declaration.kind == CanonicalAuthoredDeclarationKindV1::State)
        .filter_map(|declaration| {
            let (component, name) = declaration.subject.rsplit_once('.')?;
            Some((component.to_owned(), name.to_owned()))
        })
        .fold(
            BTreeMap::<String, BTreeSet<String>>::new(),
            |mut names, (component, name)| {
                names.entry(component).or_default().insert(name);
                names
            },
        );

    let mut sites = Vec::new();
    for class in parsed
        .classes
        .iter()
        .filter(|class| components.contains(&(class.name.clone(), range_key(range(class.span)))))
    {
        let state_names = state_names_by_component
            .get(&class.name)
            .cloned()
            .unwrap_or_default();
        let getter_names = class
            .methods
            .iter()
            .filter(|method| method.is_getter && !method.is_static && !method.is_async)
            .map(|method| method.name.clone())
            .collect::<BTreeSet<_>>();
        let mut candidates = BTreeMap::new();
        for method in class
            .methods
            .iter()
            .filter(|method| method.is_getter && !method.is_static && !method.is_async)
        {
            let Some(expression) = method.computed_expression.as_ref() else {
                continue;
            };
            let mut state_reads = BTreeSet::new();
            let mut computed_reads = BTreeSet::new();
            if !collect_getter_reads(
                expression,
                &state_names,
                &getter_names,
                &mut state_reads,
                &mut computed_reads,
            ) {
                continue;
            }
            candidates.insert(
                method.name.clone(),
                ComputedGetterCandidateV1 {
                    source: range(method.span),
                    state_reads,
                    computed_reads,
                },
            );
        }
        for (name, candidate) in &candidates {
            let Some(state_dependencies) =
                transitive_state_dependencies(name, &candidates, &mut BTreeSet::new())
            else {
                continue;
            };
            if state_dependencies.is_empty() {
                continue;
            }
            sites.push(ComputedGetterSiteV1 {
                subject: format!("{}.{}", class.name, name),
                declaration_source: candidate.source,
                state_dependencies: state_dependencies
                    .into_iter()
                    .map(|name| format!("{}.{}", class.name, name))
                    .collect(),
                computed_dependencies: candidate
                    .computed_reads
                    .iter()
                    .map(|name| format!("{}.{}", class.name, name))
                    .collect(),
            });
        }
    }
    sites.sort_by_key(|site| {
        (
            site.declaration_source.start,
            site.declaration_source.end,
            site.subject.clone(),
        )
    });
    Ok(sites)
}

/// Normalize the selected analysis-derived getters through authored semantics.
pub fn lower_computed_getters_v1(
    parsed: &ParsedFile,
    model: &CanonicalAuthoredSemanticModelV1,
) -> Result<ComputedGetterLoweringV1, ComputedGetterLoweringErrorV1> {
    let sites = computed_getter_sites_v1(parsed, model)?;
    let candidates = sites
        .iter()
        .map(|site| ResolvedAuthoredSemanticCandidateV1 {
            subject: site.subject.clone(),
            source: site.declaration_source,
            kind: AuthoredSemanticCandidateKindV1::DerivedComputedGetter {
                state_dependencies: site.state_dependencies.clone(),
                computed_dependencies: site.computed_dependencies.clone(),
            },
        });
    let model = normalize_authored_semantics_v1(parsed, candidates)
        .map_err(ComputedGetterLoweringErrorV1::InvalidAuthoredSemantics)?;
    Ok(ComputedGetterLoweringV1 { sites, model })
}

fn collect_getter_reads(
    expression: &ParsedComputedExpression,
    state_names: &BTreeSet<String>,
    getter_names: &BTreeSet<String>,
    state_reads: &mut BTreeSet<String>,
    computed_reads: &mut BTreeSet<String>,
) -> bool {
    match &expression.kind {
        ParsedComputedExpressionKind::Literal(_) => true,
        ParsedComputedExpressionKind::ThisMember(name) => {
            if state_names.contains(name) {
                state_reads.insert(name.clone());
                true
            } else if getter_names.contains(name) {
                computed_reads.insert(name.clone());
                true
            } else {
                false
            }
        }
        ParsedComputedExpressionKind::MemberAccess { object, .. } => collect_getter_reads(
            object,
            state_names,
            getter_names,
            state_reads,
            computed_reads,
        ),
        ParsedComputedExpressionKind::IndexAccess { object, index } => {
            collect_getter_reads(
                object,
                state_names,
                getter_names,
                state_reads,
                computed_reads,
            ) && collect_getter_reads(
                index,
                state_names,
                getter_names,
                state_reads,
                computed_reads,
            )
        }
        ParsedComputedExpressionKind::Conditional {
            condition,
            when_true,
            when_false,
        } => {
            collect_getter_reads(
                condition,
                state_names,
                getter_names,
                state_reads,
                computed_reads,
            ) && collect_getter_reads(
                when_true,
                state_names,
                getter_names,
                state_reads,
                computed_reads,
            ) && collect_getter_reads(
                when_false,
                state_names,
                getter_names,
                state_reads,
                computed_reads,
            )
        }
        ParsedComputedExpressionKind::Template { expressions, .. } => {
            expressions.iter().all(|expression| {
                collect_getter_reads(
                    expression,
                    state_names,
                    getter_names,
                    state_reads,
                    computed_reads,
                )
            })
        }
        ParsedComputedExpressionKind::Call { .. } => false,
        ParsedComputedExpressionKind::Arithmetic { left, right, .. }
        | ParsedComputedExpressionKind::Comparison { left, right, .. }
        | ParsedComputedExpressionKind::Logical { left, right, .. }
        | ParsedComputedExpressionKind::NullishCoalescing { left, right } => {
            collect_getter_reads(left, state_names, getter_names, state_reads, computed_reads)
                && collect_getter_reads(
                    right,
                    state_names,
                    getter_names,
                    state_reads,
                    computed_reads,
                )
        }
        ParsedComputedExpressionKind::Unary { operand, .. } => collect_getter_reads(
            operand,
            state_names,
            getter_names,
            state_reads,
            computed_reads,
        ),
    }
}

fn transitive_state_dependencies(
    name: &str,
    candidates: &BTreeMap<String, ComputedGetterCandidateV1>,
    visiting: &mut BTreeSet<String>,
) -> Option<BTreeSet<String>> {
    let candidate = candidates.get(name)?;
    if !visiting.insert(name.to_owned()) {
        return None;
    }
    let mut states = candidate.state_reads.clone();
    for computed in &candidate.computed_reads {
        states.extend(transitive_state_dependencies(
            computed, candidates, visiting,
        )?);
    }
    visiting.remove(name);
    Some(states)
}

fn range(span: SourceSpan) -> AuthoredSourceRangeV1 {
    AuthoredSourceRangeV1 {
        start: span.start,
        end: span.end,
        line: span.line,
        column: span.column,
    }
}

fn range_key(range: AuthoredSourceRangeV1) -> (usize, usize) {
    (range.start, range.end)
}

#[cfg(test)]
mod tests {
    use crate::{
        lower_component_inheritance_v1, lower_state_initializers_v1,
        CanonicalAuthoredDeclarationKindV1, DerivedAuthoredEvidenceV2,
        ResolvedComponentInheritanceV1, ResolvedIntrinsicIdentityV1, ResolvedStateInitializerV1,
    };

    use super::{computed_getter_sites_v1, lower_computed_getters_v1};

    fn identity(name: &str) -> ResolvedIntrinsicIdentityV1 {
        ResolvedIntrinsicIdentityV1 {
            name: name.to_owned(),
            flags: 32,
            declaration_modules: vec!["node_modules/presolve/src/index.d.ts".to_owned()],
        }
    }

    #[test]
    fn admits_only_direct_state_call_free_getters_without_decorator_recognition() {
        let parsed = presolve_parser::parse_file(
            "src/Counter.tsx",
            r#"
class Counter extends Base {
  count = state(0);
  get doubled() { return this.count * 2; }
  get quadrupled() { return this.doubled * 2; }
  get cycleA() { return this.cycleB; }
  get cycleB() { return this.cycleA; }
  get plain() { return "ordinary"; }
  get unknown() { return this.missing; }
  get called() { return Math.abs(this.count); }
  static get staticValue() { return 1; }
}
"#,
        );
        let component_site = crate::component_inheritance_sites_v1(&parsed)
            .into_iter()
            .next()
            .expect("component site");
        let components = lower_component_inheritance_v1(
            &parsed,
            [ResolvedComponentInheritanceV1 {
                heritage_source: component_site.heritage_source,
                component_identity: identity("Component"),
            }],
        )
        .expect("canonical component")
        .model;
        let state_site = crate::state_initializer_sites_v1(&parsed, &components)
            .expect("State sites")
            .into_iter()
            .next()
            .expect("State site");
        let states = lower_state_initializers_v1(
            &parsed,
            &components,
            [ResolvedStateInitializerV1 {
                callee_source: state_site.callee_source,
                state_identity: identity("state"),
            }],
        )
        .expect("canonical State")
        .model;
        let input = crate::compose_authored_semantics_v1([components, states])
            .expect("canonical source model");

        let sites = computed_getter_sites_v1(&parsed, &input).expect("derived sites");
        assert_eq!(sites.len(), 2);
        assert_eq!(sites[0].subject, "Counter.doubled");
        assert_eq!(sites[0].state_dependencies, ["Counter.count"]);
        assert!(sites[0].computed_dependencies.is_empty());
        assert_eq!(sites[1].subject, "Counter.quadrupled");
        assert_eq!(sites[1].state_dependencies, ["Counter.count"]);
        assert_eq!(sites[1].computed_dependencies, ["Counter.doubled"]);

        let lowered = lower_computed_getters_v1(&parsed, &input).expect("derived lowering");
        assert_eq!(lowered.model.schema_version, 3);
        let declaration = &lowered.model.declarations[0];
        assert_eq!(
            declaration.kind,
            CanonicalAuthoredDeclarationKindV1::Computed
        );
        assert!(declaration.intrinsic_identity.is_none());
        assert_eq!(
            declaration.derived_evidence,
            Some(DerivedAuthoredEvidenceV2::ComputedGetter {
                state_dependencies: vec!["Counter.count".to_owned()],
                computed_dependencies: Vec::new(),
            })
        );
        assert_eq!(lowered.model.declarations[1].subject, "Counter.quadrupled");
        assert_eq!(
            lowered.model.declarations[1].derived_evidence,
            Some(DerivedAuthoredEvidenceV2::ComputedGetter {
                state_dependencies: vec!["Counter.count".to_owned()],
                computed_dependencies: vec!["Counter.doubled".to_owned()],
            })
        );
    }
}