fix(frontend): honour the Setext no-declaration rule; drop dead helper

RFC-LANG-0002 allows a mode declaration only as an ATX heading prefix; a
Setext heading merely inherits the nearest ancestor mode. heading_declaration
was applied to every heading, so a Setext 'title' beginning with @exec/
@knowledge silently switched modes and could create a function.

- skip declaration parsing for Setext headings (they keep their literal name
  and inherited mode); add a regression test
- remove the dead normalized_heading_name helper and its imports, which only
  existed for the removed link-form call resolution

Verified: clippy -D warnings clean; cargo test --workspace --locked green
(505 passed, 0 failed, 2 ignored).
This commit is contained in:
cflmy committed 2026-11-21 15:52:46 +08:00
1 parent a3f9770761
commit 0bbe0f6524
2 files changed
+24 -8

No files matched your search

+1 -6
View File
@@ -9,7 +9,7 @@ use marqdo_expression::{
parse_payload_statement, AssignTarget, Expr, PayloadStatement as Payload, StatementKind,
};
use marqdo_ir::{PlaceTarget, Stmt};
use marqdo_markdown::{normalize_name, MarkdownDocument, MdKind, MdNode};
use marqdo_markdown::{MarkdownDocument, MdKind, MdNode};
use marqdo_span::Span;
use marqdo_value::{codes, Diagnostic};
@@ -409,8 +409,3 @@ impl Mapper {
(out, Stop::Eof)
}
}
/// Normalise a heading name (re-exported for callers that resolve link targets).
pub fn normalized_heading_name(node: &MdNode) -> Option<String> {
normalize_name(&marqdo_markdown::plain_text(node))
}
+23 -2
View File
@@ -204,11 +204,17 @@ pub fn build_units(doc: &MarkdownDocument, module: &ModuleId) -> UnitIndex {
let mut heading_infos: Vec<HeadingInfo> = Vec::new();
for (node_index, child) in doc.root.children.iter().enumerate() {
let MdKind::Heading { level, .. } = &child.kind else {
let MdKind::Heading { level, setext } = &child.kind else {
continue;
};
let level = *level;
let (declared, declared_name, declaration_error) = heading_declaration(child);
// RFC-LANG-0002: only ATX headings may carry a mode declaration; a
// Setext heading simply inherits the nearest ancestor mode.
let (declared, declared_name, declaration_error) = if *setext {
(None, None, None)
} else {
heading_declaration(child)
};
if let Some(error) = declaration_error {
index.diagnostics.push(error);
if declared_name.is_none() {
@@ -488,6 +494,21 @@ mod tests {
assert!(index.diagnostics.is_empty());
}
#[test]
fn setext_headings_inherit_and_never_declare_a_mode() {
// `@exec f` is the level-2 heading text under a Setext underline; it is
// not a declaration, so the H2 stays Knowledge and creates no function.
let index = index_of("# @knowledge 项目\n\n@exec f\n---\n\n`return 1`\n");
assert!(index.functions.is_empty(), "{:?}", index.functions);
let heading = index
.headings
.iter()
.find(|heading| heading.level == 2)
.expect("level-2 heading");
assert_eq!(heading.mode, SemanticMode::Knowledge);
assert_eq!(heading.name, "@exec f");
}
#[test]
fn empty_heading_is_an_error() {
let index = index_of("## @exec\n\n正文\n");