compmd 0.1.1

The CompMD processor: parses, validates and expands CompMD (Component Markdown) documents. In development.
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
//! Loads a document and everything it imports, and binds the names of each (spec §6): F013, F020–F022, F026 for
//! imports, F029, F035, W105, W112, W117, F002 for a signature fence with nothing to attach to, and F037 for a
//! declaration whose type isn't a type name.

use super::names::{Binding, Builder, Level, Names};
use super::origin::CORE;
use super::source::{PackSpec, Source};
use super::{
    Ambient, Body, Component, ComponentId, Document, DocumentId, Found, Language, Origin, Parses,
    Resolved, Store, uses,
};
use crate::diagnostic::{Code, Diagnostic, Fix, Location, Stage};
use crate::lines::LineStarts;
use crate::nearest::nearest;
use crate::parse::{ParseOptions, Parsed, parse};
use crate::reserved;
use crate::settings::{self, Import};
use crate::tree::{
    Declaration, DeclaredType, Definition, DefinitionBody, Node, Position, Signature,
    SignatureFence, TypeName,
};
use semver::Version;
use serde_json::Value;
use std::collections::{BTreeMap, HashMap, HashSet};

/// Why an import loads nothing: the diagnostic it gets, where, and what it says. Its names are dropped.
struct Refusal {
    code: Code,
    at: Location,
    message: String,
}

impl Refusal {
    fn new(code: Code, at: Location, message: impl Into<String>) -> Self {
        Self {
            code,
            at,
            message: message.into(),
        }
    }
}

/// Loads documents, collecting them, their components and the diagnostics.
pub(super) struct Loader<'h, 'p> {
    store: Store<'h>,
    /// The parses of documents loaded before, which a document whose text hasn't changed reuses.
    parses: &'p mut Parses,
    /// The processed document, which diagnostics name with no file.
    main: Origin,
    documents: Vec<Document>,
    components: Vec<Component>,
    diagnostics: Vec<Diagnostic>,
    /// The documents being loaded, outermost first: importing one of them closes a cycle (F020).
    loading: Vec<Origin>,
    loaded: HashMap<Origin, DocumentId>,
    core: Option<DocumentId>,
    ambient: Vec<DocumentId>,
}

impl<'h, 'p> Loader<'h, 'p> {
    pub(super) fn new(store: Store<'h>, main: Origin, parses: &'p mut Parses) -> Self {
        Self {
            store,
            parses,
            main,
            documents: Vec::new(),
            components: Vec::new(),
            diagnostics: Vec::new(),
            loading: Vec::new(),
            loaded: HashMap::new(),
            core: None,
            ambient: Vec::new(),
        }
    }

    /// Loads `@compmd/core`, the ambient packs and the processed document, then reports the names its bodies use.
    pub(super) fn resolve(mut self, text: String, parsed: Parsed, ambient: &Ambient) -> Resolved {
        self.core = self
            .document_at(Origin::core(), Location::new(1, 1), true)
            .ok();
        self.ambient = ambient
            .packs
            .iter()
            .filter_map(|pack| self.ambient_pack(pack))
            .collect();
        let main = self.load(self.main.clone(), text, parsed, false);
        let mut resolved = Resolved {
            documents: self.documents,
            components: self.components,
            main,
            cycles: HashSet::new(),
            diagnostics: Found {
                before: self.diagnostics,
                ..Found::default()
            },
        };
        uses::report(&mut resolved, ambient.report);
        resolved
    }

    /// Loads a document: its imports, its definitions, its names and its exports.
    fn load(&mut self, origin: Origin, text: String, parsed: Parsed, provided: bool) -> DocumentId {
        let id = DocumentId(self.documents.len());
        let file = (origin != self.main).then(|| origin.to_string());
        self.diagnostics.extend(
            parsed
                .diagnostics
                .into_iter()
                .map(|diagnostic| diagnostic.in_file(file.clone())),
        );
        self.documents.push(Document {
            origin: origin.clone(),
            line_starts: LineStarts::new(&text),
            text,
            settings: parsed.settings,
            root: parsed.root,
            names: Names::default(),
            exports: BTreeMap::new(),
            provided,
        });
        self.loaded.insert(origin.clone(), id);
        self.loading.push(origin);
        let mut names = Builder::default();
        for import in self.document(id).settings.imports.clone() {
            self.import(id, &import, &mut names);
        }
        self.implicit(id, &mut names);
        self.define(id, &mut names);
        self.check_frontmatter_types(id);
        let names = names.build();
        let exports = self.exports(id, &names);
        self.untyped(id, &exports);
        let document = &mut self.documents[id.0];
        document.names = names;
        document.exports = exports;
        self.loading.pop();
        id
    }

    fn document(&self, id: DocumentId) -> &Document {
        &self.documents[id.0]
    }

    fn report(&mut self, id: DocumentId, code: Code, at: Location, message: impl Into<String>) {
        self.report_fixed(id, code, at, message, None);
    }

    fn report_fixed(
        &mut self,
        id: DocumentId,
        code: Code,
        at: Location,
        message: impl Into<String>,
        fix: Option<Fix>,
    ) {
        let origin = &self.document(id).origin;
        let file = (*origin != self.main).then(|| origin.to_string());
        self.diagnostics.push(
            Diagnostic::new(code, at, message)
                .in_stage(Stage::Resolve)
                .in_file(file)
                .with_fix(fix),
        );
    }

    /// Loads what `import` names and binds its names; when it loads nothing, its names are dropped.
    fn import(&mut self, id: DocumentId, import: &Import, names: &mut Builder) {
        let from = &import.from;
        let loaded = match Source::parse(&from.value) {
            Err(why) => Err(Refusal::new(Code::F026, from.at, why.to_string())),
            Ok(Source::Document(path)) => self.relative(id, path, from.at),
            Ok(Source::File {
                path,
                language,
                stem,
            }) => {
                self.file(id, import, path, language, stem, names);
                return;
            }
            Ok(Source::Pack(spec)) => self.pack(id, &spec, import),
        };
        match loaded {
            Ok(target) => self.bind(id, import, target, names),
            Err(refusal) => {
                self.report(id, refusal.code, refusal.at, refusal.message);
                drop_names(&import.names, names);
            }
        }
    }

    /// The document at `path`, relative to the document `id`.
    fn relative(
        &mut self,
        id: DocumentId,
        path: &str,
        at: Location,
    ) -> Result<DocumentId, Refusal> {
        let document = self.document(id);
        let origin = document
            .origin
            .join(path)
            .ok_or_else(|| Refusal::new(Code::F026, at, format!("`{path}` leaves its root")))?;
        self.document_at(origin, at, document.provided)
    }

    /// The document at `origin`, loaded unless it is already.
    fn document_at(
        &mut self,
        origin: Origin,
        at: Location,
        provided: bool,
    ) -> Result<DocumentId, Refusal> {
        if self.loading.contains(&origin) {
            return Err(Refusal::new(
                Code::F020,
                at,
                format!("importing `{origin}` closes an import cycle; the import is dropped"),
            ));
        }
        if let Some(id) = self.loaded.get(&origin) {
            return Ok(*id);
        }
        let text = self
            .store
            .read(&origin)
            .ok_or_else(|| Refusal::new(Code::F026, at, format!("`{origin}` can't be loaded")))?;
        let parsed = match self.parses.0.get(&origin) {
            Some((before, parsed)) if *before == text => parsed.clone(),
            _ => {
                let parsed = parse(&text, &ParseOptions::default());
                self.parses
                    .0
                    .insert(origin.clone(), (text.clone(), parsed.clone()));
                parsed
            }
        };
        Ok(self.load(origin, text, parsed, provided))
    }

    /// Imports a component file (`./chart.jsx`), which exports one component, named after its stem.
    fn file(
        &mut self,
        id: DocumentId,
        import: &Import,
        path: &str,
        language: Language,
        stem: &str,
        names: &mut Builder,
    ) {
        let at = import.from.at;
        let Some(origin) = self.document(id).origin.join(path) else {
            self.report(id, Code::F026, at, format!("`{path}` leaves its root"));
            drop_names(&import.names, names);
            return;
        };
        if self.store.read(&origin).is_none() {
            self.report(id, Code::F026, at, format!("`{origin}` can't be loaded"));
            drop_names(&import.names, names);
            return;
        }
        let entries = match &import.names {
            settings::Names::All(at) => vec![(stem.to_string(), *at)],
            settings::Names::Listed(entries) => entries
                .iter()
                .filter_map(|entry| {
                    if entry.name == stem {
                        return Some((entry.renamed.clone(), entry.at));
                    }
                    self.report(
                        id,
                        Code::F021,
                        entry.at,
                        format!(
                            "`{path}` exports one component, `{stem}`, not `{}`",
                            entry.name
                        ),
                    );
                    None
                })
                .collect(),
        };
        for (local, at) in entries {
            if self.admit(id, names, &local, at) {
                let component = self.add(Component {
                    name: local.clone(),
                    document: id,
                    signature: None,
                    body: Body::File {
                        origin: origin.clone(),
                        language,
                    },
                    fence: Vec::new(),
                    position: None,
                });
                names.declare(&local, Binding::Component(component, Level::Listed));
            }
        }
    }

    /// The pack `spec` names: its newest matching release, checked against the import's `integrity`.
    fn pack(
        &mut self,
        id: DocumentId,
        spec: &PackSpec,
        import: &Import,
    ) -> Result<DocumentId, Refusal> {
        let from = &import.from;
        let release = self
            .store
            .releases(&spec.name)
            .into_iter()
            .filter(|release| spec.range.matches(&release.version))
            .max_by(|a, b| a.version.cmp(&b.version))
            .ok_or_else(|| {
                Refusal::new(
                    Code::F026,
                    from.at,
                    format!("no release of `{}` matches `{}`", spec.name, from.value),
                )
            })?;
        if let Some(integrity) = &import.integrity {
            if !spec.exact {
                self.report(
                    id,
                    Code::F009,
                    integrity.at,
                    "`integrity` needs an exact version, such as `@1.4.2`, since a range takes whichever release is newest; ignored",
                );
            } else if release.integrity.as_deref() != Some(integrity.value.as_str()) {
                return Err(Refusal::new(
                    Code::F035,
                    integrity.at,
                    format!(
                        "`{}@{}` doesn't match this `integrity`; the import is dropped",
                        spec.name, release.version
                    ),
                ));
            }
        }
        self.package(&spec.name, release.version, from.at, false)
    }

    /// The newest release of the ambient pack `name`.
    fn ambient_pack(&mut self, name: &str) -> Option<DocumentId> {
        let release = self
            .store
            .releases(name)
            .into_iter()
            .max_by(|a, b| a.version.cmp(&b.version))?;
        self.package(name, release.version, Location::new(1, 1), true)
            .ok()
    }

    /// The document of a pack's release, named by its `package.json` (spec §6.4), checked against it (F022).
    fn package(
        &mut self,
        name: &str,
        version: Version,
        at: Location,
        provided: bool,
    ) -> Result<DocumentId, Refusal> {
        let unloadable = || {
            Refusal::new(
                Code::F026,
                at,
                format!("`{name}@{version}` can't be loaded"),
            )
        };
        let manifest: Value = self
            .store
            .read(&Origin::Pack {
                name: name.to_string(),
                version: version.clone(),
                path: "package.json".to_string(),
            })
            .and_then(|text| serde_json::from_str(&text).ok())
            .ok_or_else(unloadable)?;
        let path = manifest
            .get("compmd")
            .and_then(Value::as_str)
            .map_or("index.comp.md", |path| path.trim_start_matches("./"));
        let origin = Origin::Pack {
            name: name.to_string(),
            path: path.to_string(),
            version,
        };
        let fresh = !self.loaded.contains_key(&origin);
        let id = self.document_at(origin, at, provided)?;
        if fresh {
            self.check_manifest(id, &manifest);
        }
        Ok(id)
    }

    /// F022: a pack's `export.pack` and `export.version` must be its manifest's.
    fn check_manifest(&mut self, id: DocumentId, manifest: &Value) {
        let Some(export) = self.document(id).settings.export.clone() else {
            return;
        };
        let differs = |written: &Option<String>, key: &str| {
            written
                .as_deref()
                .is_some_and(|written| manifest.get(key).and_then(Value::as_str) != Some(written))
        };
        if differs(&export.pack, "name") || differs(&export.version, "version") {
            self.report(
                id,
                Code::F022,
                export.at,
                "`export.pack` and `export.version` must match the pack's `package.json`, which is used",
            );
        }
    }

    /// Binds the names `import` takes from the loaded document `target`.
    fn bind(&mut self, id: DocumentId, import: &Import, target: DocumentId, names: &mut Builder) {
        let exports = self.document(target).exports.clone();
        match &import.names {
            settings::Names::All(_) => {
                for (name, binding) in exports {
                    if let Binding::Component(component, _) = binding {
                        names.share(&name, component, &import.from.value, Level::Wildcard);
                    }
                }
            }
            settings::Names::Listed(entries) => {
                for entry in entries {
                    let binding = match exports.get(&entry.name) {
                        Some(Binding::Component(component, _)) => {
                            Binding::Component(*component, Level::Listed)
                        }
                        Some(other) => other.clone(),
                        None => {
                            self.report(
                                id,
                                Code::F021,
                                entry.at,
                                format!("`{}` doesn't export `{}`", import.from.value, entry.name),
                            );
                            Binding::Dropped
                        }
                    };
                    if self.admit(id, names, &entry.renamed, entry.at) {
                        names.declare(&entry.renamed, binding);
                    }
                }
            }
        }
    }

    /// `@compmd/core` and the ambient packs, which every document imports.
    fn implicit(&self, id: DocumentId, names: &mut Builder) {
        let core = self.core.filter(|core| *core != id);
        for (name, binding) in core.iter().flat_map(|core| &self.document(*core).exports) {
            if let Binding::Component(component, _) = binding {
                names.share(name, *component, CORE, Level::Core);
            }
        }
        for pack in self.ambient.iter().filter(|pack| **pack != id) {
            for (name, binding) in &self.document(*pack).exports {
                if let Binding::Component(component, _) = binding {
                    names.ambient(name, *component);
                }
            }
        }
    }

    /// Whether `name` may be declared at the first two levels: not a keyword or `md`, which `@compmd/core` alone
    /// defines, and not declared yet (F013). Shadowing a `@compmd/core` component is allowed, with W112.
    fn admit(&mut self, id: DocumentId, names: &Builder, name: &str, at: Location) -> bool {
        let core = self.document(id).origin.is_core();
        if let Some(why) = reserved::as_component(name).filter(|_| !core) {
            self.report(id, Code::F013, at, why);
            return false;
        }
        if names.is_declared(name) {
            self.report(
                id,
                Code::F013,
                at,
                format!(
                    "`{name}` is already declared in this document; the first declaration wins"
                ),
            );
            return false;
        }
        let core = self.core.filter(|core| *core != id);
        if core.is_some_and(|core| self.document(core).exports.contains_key(name)) {
            self.report(
                id,
                Code::W112,
                at,
                format!("`{name}` shadows `@compmd/core`'s `{name}` in this document"),
            );
        }
        true
    }

    fn add(&mut self, component: Component) -> ComponentId {
        self.components.push(component);
        ComponentId(self.components.len() - 1)
    }

    /// The document's definitions and signature fences.
    fn define(&mut self, id: DocumentId, names: &mut Builder) {
        let root = &self.document(id).root;
        let definitions: Vec<Definition> = root
            .children
            .iter()
            .filter_map(|node| match node {
                Node::ComponentDefinition(definition) => Some(definition.clone()),
                _ => None,
            })
            .collect();
        let fences: Vec<SignatureFence> = root
            .children
            .iter()
            .filter_map(|node| match node {
                Node::ComponentSignature(fence) => Some(fence.clone()),
                _ => None,
            })
            .collect();
        for definition in definitions {
            self.definition(id, definition, names);
        }
        for fence in fences {
            self.signature_fence(id, fence, names);
        }
    }

    fn definition(&mut self, id: DocumentId, definition: Definition, names: &mut Builder) {
        let at = location(definition.position);
        if !self.admit(id, names, &definition.name, at) {
            return;
        }
        let compmd = matches!(definition.body, DefinitionBody::Compmd { .. });
        if let Some(signature) = &definition.signature {
            self.check_params(id, signature, at, compmd);
        }
        let body = match definition.body {
            DefinitionBody::Compmd { children } => Body::Compmd(children),
            DefinitionBody::Host { source, runtime } => Body::Host {
                language: definition.language,
                runtime,
                source,
            },
        };
        let component = self.add(Component {
            name: definition.name.clone(),
            document: id,
            signature: definition.signature,
            body,
            fence: definition.params,
            position: definition.position,
        });
        names.declare(
            &definition.name,
            Binding::Component(component, Level::Local),
        );
    }

    /// A signature fence gives its signature to the document's host-language component of that name; in a pack the
    /// processor provides, a fence alone is a component the processor implements (spec §9).
    fn signature_fence(&mut self, id: DocumentId, fence: SignatureFence, names: &mut Builder) {
        let at = location(fence.position);
        if let Some(signature) = &fence.signature {
            self.check_params(id, signature, at, false);
        }
        let host = match names.declared(&fence.name) {
            Some(Binding::Component(component, _)) => Some(*component).filter(|component| {
                let component = &self.components[component.0];
                component.document == id && component.body.is_host()
            }),
            _ => None,
        };
        if let Some(component) = host {
            self.components[component.0]
                .signature
                .get_or_insert(fence.signature.unwrap_or_default());
        } else if self.document(id).provided {
            if self.admit(id, names, &fence.name, at) {
                let component = self.add(Component {
                    name: fence.name.clone(),
                    document: id,
                    signature: fence.signature,
                    body: Body::Processor,
                    fence: Vec::new(),
                    position: fence.position,
                });
                names.declare(&fence.name, Binding::Component(component, Level::Local));
            }
        } else {
            self.report(
                id,
                Code::F002,
                at,
                format!(
                    "the signature fence for `{}` has nothing to attach to: no definition of `{0}` and no component file imported as `{0}`",
                    fence.name
                ),
            );
        }
    }

    /// F013 for a signature's params: two whose names differ only in `-` and `_`, which expressions can't tell apart,
    /// and, for a `compmd` definition, a name expressions can't use (a `name` param binds a name and is never read).
    fn check_params(&mut self, id: DocumentId, signature: &Signature, at: Location, compmd: bool) {
        let mut seen = HashSet::new();
        for param in &signature.params {
            if !seen.insert(param.name.replace('-', "_")) {
                self.report(
                    id,
                    Code::F013,
                    at,
                    format!("two params differ only in `-` and `_` from `{}`, which expressions read alike", param.name),
                );
            }
            let binds_a_name = param.declared == DeclaredType::Name(TypeName::Name);
            if let Some(why) =
                reserved::as_variable(&param.name).filter(|_| compmd && !binds_a_name)
            {
                self.report(id, Code::F013, at, why);
            }
            self.check_type(id, param);
        }
    }

    /// F037 for the declarations of the document's frontmatter: its inputs, and its data imports' schemas.
    fn check_frontmatter_types(&mut self, id: DocumentId) {
        let settings = &self.document(id).settings;
        let fields = settings
            .data
            .iter()
            .flat_map(|data| data.schema.iter().flatten())
            .map(|field| &field.declaration);
        let declarations: Vec<Declaration> = settings
            .inputs
            .iter()
            .map(|input| &input.declaration)
            .chain(fields)
            .cloned()
            .collect();
        for declaration in &declarations {
            self.check_type(id, declaration);
        }
    }

    /// F037 for a declaration whose type isn't a type name (spec §7.3), with the nearest one and the fix that writes
    /// it. The declaration is kept: its type takes any value.
    fn check_type(&mut self, id: DocumentId, declaration: &Declaration) {
        let (Some(at), Some(written)) = (declaration.unknown_at, declaration.declared.unknown())
        else {
            return;
        };
        // `enum` is a type only inside `input<…>`.
        let near = nearest(
            written,
            TypeName::NAMES
                .iter()
                .copied()
                .filter(|name| *name != "enum"),
        );
        let hint = near.map_or_else(String::new, |near| format!("; did you mean `{near}`?"));
        let fix = near.map(|near| Fix::replacing(at, written, near));
        self.report_fixed(
            id,
            Code::F037,
            at,
            format!(
                "`{written}` isn't a type, so `{}` takes any value{hint}",
                declaration.name
            ),
            fix,
        );
    }

    /// The document's exports (spec §6.2): F013 for a keyword, F029 and W117 for host-language components.
    fn exports(&mut self, id: DocumentId, names: &Names) -> BTreeMap<String, Binding> {
        let Some(export) = self.document(id).settings.export.clone() else {
            return BTreeMap::new();
        };
        let mut exports = BTreeMap::new();
        match &export.names {
            settings::Names::All(at) => {
                let defined: Vec<ComponentId> = (0..self.components.len())
                    .map(ComponentId)
                    .filter(|component| {
                        let component = &self.components[component.0];
                        component.document == id && !matches!(component.body, Body::File { .. })
                    })
                    .collect();
                for component in defined {
                    let name = self.components[component.0].name.clone();
                    exports.insert(name, Binding::Component(component, Level::Local));
                    self.check_export(id, component, *at);
                }
            }
            settings::Names::Listed(entries) => {
                for entry in entries {
                    if let Some(why) = reserved::as_component(&entry.renamed) {
                        self.report(id, Code::F013, entry.at, why);
                        continue;
                    }
                    let Some(binding) = names.get(&entry.name).cloned() else {
                        continue;
                    };
                    if let Binding::Component(component, _) = binding {
                        self.check_export(id, component, entry.at);
                    }
                    exports.insert(entry.renamed.clone(), binding);
                }
            }
        }
        exports
    }

    /// An exported host-language component needs a signature fence (F029) and, unless plain JavaScript, a runtime
    /// (W117, at its definition, by the document that defines it).
    fn check_export(&mut self, id: DocumentId, component: ComponentId, at: Location) {
        let exported = &self.components[component.0];
        if !exported.body.is_host() {
            return;
        }
        if exported.signature.is_none() {
            let name = exported.name.clone();
            self.report(
                id,
                Code::F029,
                at,
                format!("`{name}` is exported, so it needs a signature fence: packs are used where it may not run"),
            );
        }
        let exported = &self.components[component.0];
        let unpinned = match &exported.body {
            Body::Host {
                language,
                runtime: None,
                ..
            } => Language::from_name(language).is_some_and(Language::has_runtimes),
            _ => false,
        };
        if unpinned && exported.document == id {
            let (name, at) = (exported.name.clone(), location(exported.position));
            self.report(
                id,
                Code::W117,
                at,
                format!("`{name}` is exported without `runtime=`, so it changes when the binding's default moves"),
            );
        }
    }

    /// W105: a local host-language component without a signature fence, whose params can't be typed.
    fn untyped(&mut self, id: DocumentId, exports: &BTreeMap<String, Binding>) {
        let exported: HashSet<ComponentId> = exports
            .values()
            .filter_map(|binding| match binding {
                Binding::Component(component, _) => Some(*component),
                _ => None,
            })
            .collect();
        let untyped: Vec<(String, Location)> = self
            .components
            .iter()
            .enumerate()
            .filter(|(index, component)| {
                component.document == id
                    && matches!(component.body, Body::Host { .. })
                    && component.signature.is_none()
                    && !exported.contains(&ComponentId(*index))
            })
            .map(|(_, component)| (component.name.clone(), location(component.position)))
            .collect();
        for (name, at) in untyped {
            self.report(
                id,
                Code::W105,
                at,
                format!("`{name}` has no signature fence, so its params go through unchecked"),
            );
        }
    }
}

/// The names of an import that loads nothing: listed ones stand for nothing, without F002; a wildcard import may
/// have held any name.
fn drop_names(listed: &settings::Names, names: &mut Builder) {
    match listed {
        settings::Names::All(_) => names.incomplete(),
        settings::Names::Listed(entries) => {
            for entry in entries {
                if !names.is_declared(&entry.renamed) {
                    names.declare(&entry.renamed, Binding::Dropped);
                }
            }
        }
    }
}

/// Where a node starts.
fn location(position: Option<Position>) -> Location {
    position.map_or(Location::new(1, 1), |position| position.start.into())
}