presolve-compiler 0.1.0-alpha.4

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
use std::collections::BTreeMap;

use crate::{
    ComponentNode, ExecutionBoundary, FormDeclarationStatus, FormId, SemanticId, SemanticOwner,
    SourceProvenance,
};

/// First-class immutable compiler-owned Form declaration.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FormEntity {
    pub id: FormId,
    pub owner: SemanticOwner,
    pub authored_field: SemanticId,
    pub name: String,
    pub provenance: SourceProvenance,
    pub boundary: ExecutionBoundary,
}

/// Lower only valid normalized I2 candidates into canonical Form entities.
/// Invalid and duplicate candidates remain on their owning component as
/// immutable diagnostic inputs and never become executable products.
///
/// # Panics
///
/// Panics if a candidate marked valid is missing canonical owner, Form, or
/// authored-field identity. That would violate the I2 staging invariant.
#[must_use]
pub fn collect_form_entities(components: &[ComponentNode]) -> BTreeMap<FormId, FormEntity> {
    components
        .iter()
        .flat_map(|component| component.form_declaration_candidates.iter())
        .filter(|candidate| candidate.status == FormDeclarationStatus::Valid)
        .map(|candidate| {
            let id = candidate
                .form_id
                .clone()
                .expect("valid Form candidate has canonical identity");
            let owner = candidate
                .owner_component
                .clone()
                .expect("valid Form candidate has canonical owner");
            (
                id.clone(),
                FormEntity {
                    id,
                    owner: SemanticOwner::entity(owner),
                    authored_field: candidate
                        .authored_field
                        .clone()
                        .expect("valid Form candidate has authored field identity"),
                    name: candidate
                        .authored_name
                        .clone()
                        .expect("valid Form candidate has authored name"),
                    provenance: candidate.provenance.clone(),
                    boundary: ExecutionBoundary::Client,
                },
            )
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use crate::{
        build_application_semantic_model, build_application_semantic_model_for_unit,
        build_semantic_graph, validate_application_semantic_model, AuthoredDeclarationKind,
        CompilationUnit, ExecutionBoundary, FormDeclarationStatus, FormDeclarationViolation,
        FormId, SemanticEntityKind, SemanticOwner, SemanticType, SEMANTIC_GRAPH_SCHEMA_VERSION,
    };

    #[test]
    fn lowers_valid_form_fields_with_identity_ownership_type_and_provenance() {
        let source = r#"
@component("user-profile")
class UserProfile {
  @form()
  profileForm!: Form;

  @form()
  declare settingsForm: Form;

  render() { return <main />; }
}
"#;
        let parsed = presolve_parser::parse_file("src/UserProfile.tsx", source);
        let asm = build_application_semantic_model(&parsed);
        let component = &asm.components[0];
        let profile_id = FormId::for_owner(&component.id, "profileForm");
        let profile = asm.form(&profile_id).expect("profile Form");

        assert_eq!(asm.forms().len(), 2);
        assert_eq!(
            profile.id.as_str(),
            "module:src/UserProfile.tsx/component:user-profile/form:profileForm"
        );
        assert_eq!(profile.owner, SemanticOwner::entity(component.id.clone()));
        assert_eq!(
            profile.authored_field.as_str(),
            "module:src/UserProfile.tsx/component:user-profile/form-field:profileForm"
        );
        assert_eq!(profile.boundary, ExecutionBoundary::Client);
        assert_eq!(
            profile.provenance.span.start,
            source.find("@form()\n  profileForm").unwrap()
        );
        assert_eq!(
            asm.owner(profile.id.as_semantic_id()),
            Some(&SemanticOwner::entity(component.id.clone()))
        );
        assert_eq!(
            asm.semantic_type_of(profile.id.as_semantic_id()),
            Some(&SemanticType::Form)
        );
        assert!(asm
            .entity(profile.id.as_semantic_id())
            .is_some_and(|entity| entity.kind() == SemanticEntityKind::Form));
        assert!(asm
            .form_declaration_candidates()
            .iter()
            .all(|candidate| candidate.status == FormDeclarationStatus::Valid));
        assert!(validate_application_semantic_model(&asm).is_empty());
        let graph = build_semantic_graph(&asm);
        assert_eq!(graph.schema_version, SEMANTIC_GRAPH_SCHEMA_VERSION);
        assert!(graph
            .nodes
            .iter()
            .any(|node| node.id == *profile.id.as_semantic_id()));
    }

    #[test]
    fn keeps_form_names_component_scoped_and_multi_file_order_deterministic() {
        let login = r#"
@component("login-panel")
class LoginPanel {
  @form() credentials!: Form;
  render() { return <main />; }
}
"#;
        let signup = r#"
@component("signup-panel")
class SignupPanel {
  @form() credentials!: Form;
  render() { return <main />; }
}
"#;
        let first =
            CompilationUnit::parse_sources([("src/Signup.tsx", signup), ("src/Login.tsx", login)]);
        let second =
            CompilationUnit::parse_sources([("src/Login.tsx", login), ("src/Signup.tsx", signup)]);
        let first = build_application_semantic_model_for_unit(&first);
        let second = build_application_semantic_model_for_unit(&second);

        assert_eq!(first.forms, second.forms);
        assert_eq!(
            first.form_declaration_candidates(),
            second.form_declaration_candidates()
        );
        assert_eq!(first.forms.len(), 2);
        assert_ne!(first.forms.keys().next(), first.forms.keys().nth(1));
        assert!(first.forms.values().all(|form| form.name == "credentials"));
    }

    #[test]
    #[allow(clippy::too_many_lines)]
    fn retains_every_invalid_form_candidate_without_fabricating_identities() {
        let source = r#"
@form()
class ClassTarget {
  @form() orphan!: Form;
}

@form()
@component("profile")
class Profile {
  @form() good!: Form;
  @form() static staticForm!: Form;
  @form() initialized: Form = createForm();
  @form() notDeclarationOnly: Form;
  @form() missingType!;
  @form() wrong!: string;
  @form() nullable!: Form | null;
  @form() generic!: Form<any>;
  @form
  bare!: Form;
  @form("named") named!: Form;
  @form({}, true) many!: Form;
  @form() @slot() conflicting!: Form;
  @form() duplicate!: Form;
  @form() duplicate!: Form;
  @form() ["computed"]!: Form;
  @form() submit() {}
  parameter(@form() value: Form) {}
  @form() get current(): Form { return this.good; }
  @form() set current(value: Form) {}
  render() { return <main />; }
}
"#;
        let parsed = presolve_parser::parse_file("src/InvalidForms.tsx", source);
        let asm = build_application_semantic_model(&parsed);
        let candidates = asm.form_declaration_candidates();

        assert_eq!(asm.forms().len(), 1);
        assert_eq!(asm.forms()[0].name, "good");
        assert_eq!(candidates.len(), 22);
        assert_eq!(
            candidates
                .iter()
                .filter(|candidate| candidate.status == FormDeclarationStatus::Valid)
                .count(),
            1
        );
        assert!(candidates.iter().all(|candidate| {
            candidate.status == FormDeclarationStatus::Valid || !candidate.violations().is_empty()
        }));

        for name in [
            "staticForm",
            "initialized",
            "notDeclarationOnly",
            "missingType",
            "wrong",
            "nullable",
            "generic",
            "bare",
            "named",
            "many",
            "conflicting",
            "duplicate",
        ] {
            assert!(
                candidates.iter().any(|candidate| {
                    candidate.authored_name.as_deref() == Some(name) && candidate.form_id.is_some()
                }),
                "{name}"
            );
        }
        assert!(candidates
            .iter()
            .filter(|candidate| { candidate.authored_name.as_deref() == Some("duplicate") })
            .all(|candidate| candidate
                .violations()
                .contains(&FormDeclarationViolation::DuplicateName)));
        assert_eq!(
            candidates
                .iter()
                .filter(|candidate| candidate.authored_name.as_deref() == Some("duplicate"))
                .count(),
            2
        );
        assert!(candidates.iter().any(|candidate| {
            candidate.authored_name.as_deref() == Some("initialized")
                && candidate
                    .violations()
                    .contains(&FormDeclarationViolation::InitializedField)
        }));
        assert!(candidates.iter().any(|candidate| {
            candidate.authored_name.as_deref() == Some("bare")
                && candidate
                    .violations()
                    .contains(&FormDeclarationViolation::InvalidDecoratorInvocation)
        }));
        assert!(candidates.iter().any(|candidate| {
            candidate.authored_name.as_deref() == Some("conflicting")
                && candidate
                    .violations()
                    .contains(&FormDeclarationViolation::ConflictingSemanticDecorator)
        }));
        assert_eq!(
            candidates
                .iter()
                .find(|candidate| candidate.authored_name.as_deref() == Some("many"))
                .expect("many-argument candidate")
                .decorator_argument_provenance
                .len(),
            2
        );
        assert!(candidates
            .iter()
            .filter(|candidate| {
                matches!(
                    candidate.declaration_kind,
                    AuthoredDeclarationKind::Class
                        | AuthoredDeclarationKind::Method
                        | AuthoredDeclarationKind::Getter
                        | AuthoredDeclarationKind::Setter
                        | AuthoredDeclarationKind::Parameter
                ) || candidate
                    .violations()
                    .contains(&FormDeclarationViolation::InvalidOwner)
                    || candidate
                        .violations()
                        .contains(&FormDeclarationViolation::UnsupportedFieldName)
            })
            .all(|candidate| candidate.form_id.is_none()));
    }

    #[test]
    fn resolves_form_only_through_the_builtin_type_authority() {
        let cases = [
            r#"
import { Form } from "./user-form";
@component("profile")
class Profile {
  @form() profile!: Form;
  render() { return <main />; }
}
"#,
            r#"
interface Form {}
@component("profile")
class Profile {
  @form() profile!: Form;
  render() { return <main />; }
}
"#,
            r#"
class Form {}
@component("profile")
class Profile {
  @form() profile!: Form;
  render() { return <main />; }
}
"#,
            r#"
class CustomForm extends Form {}
@component("profile")
class Profile {
  @form() profile!: CustomForm;
  render() { return <main />; }
}
"#,
            r#"
type FormAlias = Form;
@component("profile")
class Profile {
  @form() profile!: FormAlias;
  render() { return <main />; }
}
"#,
        ];

        for (index, source) in cases.into_iter().enumerate() {
            let parsed = presolve_parser::parse_file(format!("src/InvalidType{index}.tsx"), source);
            let asm = build_application_semantic_model(&parsed);
            let candidates = asm.form_declaration_candidates();
            assert!(asm.forms().is_empty(), "case {index}");
            assert_eq!(candidates.len(), 1, "case {index}");
            assert!(
                candidates[0].violations().iter().any(|violation| matches!(
                    violation,
                    FormDeclarationViolation::InvalidType { .. }
                )),
                "case {index}"
            );
            assert!(candidates[0].form_id.is_some(), "case {index}");
        }
    }

    #[test]
    fn resolves_the_form_marker_from_the_public_presolve_package() {
        let source = r#"
import { Form } from "presolve";
@component("profile")
class Profile {
  @form() profile!: Form;
  render() { return <main />; }
}
"#;
        let parsed = presolve_parser::parse_file("src/Profile.tsx", source);
        let asm = build_application_semantic_model(&parsed);
        assert_eq!(asm.forms().len(), 1);
        assert_eq!(asm.forms()[0].name, "profile");
        assert_eq!(
            asm.form_declaration_candidates()[0].status,
            FormDeclarationStatus::Valid
        );
    }

    #[test]
    fn rejects_repeated_form_decorators_without_selecting_a_winner() {
        let parsed = presolve_parser::parse_file(
            "src/DuplicateDecorator.tsx",
            r#"
@component("profile")
class Profile {
  @form()
  @form()
  profile!: Form;
  render() { return <main />; }
}
"#,
        );
        let asm = build_application_semantic_model(&parsed);
        let candidates = asm.form_declaration_candidates();

        assert!(asm.forms().is_empty());
        assert_eq!(candidates.len(), 2);
        assert!(candidates.iter().all(|candidate| {
            candidate.form_id.is_some()
                && candidate
                    .violations()
                    .contains(&FormDeclarationViolation::DuplicateFormDecorator)
                && !candidate
                    .violations()
                    .contains(&FormDeclarationViolation::DuplicateName)
        }));
    }

    #[test]
    fn does_not_copy_inherited_form_declarations_into_derived_components() {
        let parsed = presolve_parser::parse_file(
            "src/Inheritance.tsx",
            r#"
@component("base-panel")
class BasePanel {
  @form() baseForm!: Form;
  render() { return <main />; }
}

@component("derived-panel")
class DerivedPanel extends BasePanel {
  render() { return <main />; }
}
"#,
        );
        let asm = build_application_semantic_model(&parsed);
        let derived = asm
            .components
            .iter()
            .find(|component| component.class_name == "DerivedPanel")
            .expect("derived component");

        assert_eq!(asm.forms().len(), 1);
        assert!(derived.form_declaration_candidates.is_empty());
        assert!(asm.forms().iter().all(|form| {
            form.owner
                .entity_id()
                .is_some_and(|owner| owner != &derived.id)
        }));
    }
}