From ad293c6b260703610a3210a8e1d3ba5346a9a215 Mon Sep 17 00:00:00 2001 From: Josh Date: Mon, 6 Jan 2025 23:18:04 -0600 Subject: [PATCH] rename block type to container --- crates/djls-template-ast/SPEC.md | 54 +++++++++---------- crates/djls-template-ast/src/ast.rs | 20 +++---- crates/djls-template-ast/src/parser.rs | 4 +- ..._tests__django__parse_complex_if_elif.snap | 2 +- ...tests__django__parse_django_for_block.snap | 2 +- ..._tests__django__parse_django_if_block.snap | 2 +- ...r__tests__django__parse_mixed_content.snap | 10 ++-- ...r__tests__django__parse_nested_for_if.snap | 4 +- ...__tests__errors__parse_error_recovery.snap | 4 +- ...ts__errors__parse_unclosed_django_for.snap | 2 +- ...sts__errors__parse_unclosed_django_if.snap | 2 +- ...er__tests__full_templates__parse_full.snap | 4 +- crates/djls-template-ast/src/tagspecs.rs | 14 ++--- crates/djls-template-ast/tagspecs/django.toml | 18 +++---- 14 files changed, 71 insertions(+), 71 deletions(-) diff --git a/crates/djls-template-ast/SPEC.md b/crates/djls-template-ast/SPEC.md index 061ee4e..68c20ce 100644 --- a/crates/djls-template-ast/SPEC.md +++ b/crates/djls-template-ast/SPEC.md @@ -110,11 +110,6 @@ Represents Django template tags that may have nested content, assignments, and c ```rust pub enum Block { - Block { - tag: Tag, - nodes: Vec, - closing: Option>, - }, Branch { tag: Tag, nodes: Vec, @@ -122,6 +117,11 @@ pub enum Block { Closing { tag: Tag, }, + Container { + tag: Tag, + nodes: Vec, + closing: Option>, + }, Inclusion { tag: Tag, template_name: String, @@ -159,24 +159,6 @@ pub struct Assignment { #### Variants -##### `Block::Block` - -Represents standard block tags that may contain child nodes and require a closing tag. - -```rust -Block::Block { - tag: Tag, // The opening Tag of the block - nodes: Vec, // Nodes contained within the block - closing: Option>, // Contains Block::Closing if present -} -``` - -Examples: - -- `{% if %}...{% endif %}` -- `{% for %}...{% endfor %}` -- `{% with %}...{% endwith %}` - ##### `Block::Branch` Represents branch tags that are part of control flow structures and contain child nodes. @@ -210,6 +192,24 @@ Examples: - `{% endfor %}` - `{% endwith %}` +##### `Block::Container` + +Represents standard block tags that may contain child nodes and require a closing tag. + +```rust +Block::Block { + tag: Tag, // The opening Tag of the block + nodes: Vec, // Nodes contained within the block + closing: Option>, // Contains Block::Closing if present +} +``` + +Examples: + +- `{% if %}...{% endif %}` +- `{% for %}...{% endfor %}` +- `{% with %}...{% endwith %}` + ##### `Block::Inclusion` Represents tags that include or extend templates. @@ -250,7 +250,7 @@ Tag Specifications (TagSpecs) define how tags are parsed and understood. They al ```toml [package.module.path.tag_name] # Path where tag is registered, e.g., django.template.defaulttags -type = "block" | "inclusion" | "single" +type = "container" | "inclusion" | "single" closing = "closing_tag_name" # For block tags that require a closing tag branches = ["branch_tag_name", ...] # For block tags that support branches @@ -267,7 +267,7 @@ The `name` field in args should match the internal name used in Django's node im ### Tag Types -- `block`: Tags that wrap content and require a closing tag +- `container`: Tags that wrap content and require a closing tag ```django {% if condition %}content{% endif %} @@ -298,7 +298,7 @@ The `name` field in args should match the internal name used in Django's node im ```toml [django.template.defaulttags.if] -type = "block" +type = "container" closing = "endif" branches = ["elif", "else"] args = [{ name = "condition", required = true }] @@ -316,7 +316,7 @@ args = [{ name = "template_name", required = true }] ```toml [django.template.defaulttags.autoescape] -type = "block" +type = "container" closing = "endautoescape" args = [{ name = "setting", required = true, allowed_values = ["on", "off"] }] ``` diff --git a/crates/djls-template-ast/src/ast.rs b/crates/djls-template-ast/src/ast.rs index 2feda7f..bbb6cbc 100644 --- a/crates/djls-template-ast/src/ast.rs +++ b/crates/djls-template-ast/src/ast.rs @@ -145,11 +145,6 @@ impl Node { #[derive(Debug, Clone, Serialize)] pub enum Block { - Block { - tag: Tag, - nodes: Vec, - closing: Option>, - }, Branch { tag: Tag, nodes: Vec, @@ -157,6 +152,11 @@ pub enum Block { Closing { tag: Tag, }, + Container { + tag: Tag, + nodes: Vec, + closing: Option>, + }, Inclusion { tag: Tag, template_name: String, @@ -169,7 +169,7 @@ pub enum Block { impl Block { pub fn tag(&self) -> &Tag { match self { - Self::Block { tag, .. } + Self::Container { tag, .. } | Self::Branch { tag, .. } | Self::Single { tag } | Self::Inclusion { tag, .. } @@ -179,7 +179,7 @@ impl Block { pub fn nodes(&self) -> Option<&Vec> { match self { - Block::Block { nodes, .. } => Some(nodes), + Block::Container { nodes, .. } => Some(nodes), Block::Branch { nodes, .. } => Some(nodes), _ => None, } @@ -187,7 +187,7 @@ impl Block { pub fn closing(&self) -> Option<&Block> { match self { - Block::Block { closing, .. } => closing.as_deref(), + Block::Container { closing, .. } => closing.as_deref(), _ => None, } } @@ -306,7 +306,7 @@ mod tests { #[test] fn test_block_spans() { - let nodes = vec![Node::Block(Block::Block { + let nodes = vec![Node::Block(Block::Container { tag: Tag { name: "if".to_string(), bits: vec!["user.is_authenticated".to_string()], @@ -341,7 +341,7 @@ mod tests { assert!(errors.is_empty()); let nodes = ast.nodes(); - if let Node::Block(Block::Block { + if let Node::Block(Block::Container { tag, nodes, closing, diff --git a/crates/djls-template-ast/src/parser.rs b/crates/djls-template-ast/src/parser.rs index 2c5c02b..0ff0f35 100644 --- a/crates/djls-template-ast/src/parser.rs +++ b/crates/djls-template-ast/src/parser.rs @@ -120,7 +120,7 @@ impl Parser { match spec { Some(spec) => match spec.tag_type { - TagType::Block => self.parse_block_tag(tag, spec), + TagType::Container => self.parse_block_tag(tag, spec), TagType::Single => Ok(Node::Block(Block::Single { tag })), TagType::Inclusion => { let template_name = tag.bits.get(1).cloned().unwrap_or_default(); @@ -161,7 +161,7 @@ impl Parser { .push(ParserError::Ast(AstError::UnclosedTag(tag.name.clone()))); } - Ok(Node::Block(Block::Block { + Ok(Node::Block(Block::Container { tag, nodes, closing, diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_complex_if_elif.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_complex_if_elif.snap index c28b497..2572694 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_complex_if_elif.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_complex_if_elif.snap @@ -4,7 +4,7 @@ expression: ast --- nodes: - Block: - Block: + Container: tag: name: if bits: diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_for_block.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_for_block.snap index 288db4f..4c8c857 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_for_block.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_for_block.snap @@ -4,7 +4,7 @@ expression: ast --- nodes: - Block: - Block: + Container: tag: name: for bits: diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_if_block.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_if_block.snap index abb2362..d1f6bca 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_if_block.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_if_block.snap @@ -4,7 +4,7 @@ expression: ast --- nodes: - Block: - Block: + Container: tag: name: if bits: diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_mixed_content.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_mixed_content.snap index 4e5884c..1340a46 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_mixed_content.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_mixed_content.snap @@ -9,7 +9,7 @@ nodes: start: 0 length: 8 - Block: - Block: + Container: tag: name: if bits: @@ -43,7 +43,7 @@ nodes: start: 47 length: 31 - Block: - Block: + Container: tag: name: for bits: @@ -60,7 +60,7 @@ nodes: assignment: ~ nodes: - Block: - Block: + Container: tag: name: if bits: @@ -101,7 +101,7 @@ nodes: start: 171 length: 10 - Block: - Block: + Container: tag: name: if bits: @@ -135,7 +135,7 @@ nodes: length: 5 assignment: ~ - Block: - Block: + Container: tag: name: if bits: diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_nested_for_if.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_nested_for_if.snap index 9ae4785..93a0c47 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_nested_for_if.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_nested_for_if.snap @@ -4,7 +4,7 @@ expression: ast --- nodes: - Block: - Block: + Container: tag: name: for bits: @@ -21,7 +21,7 @@ nodes: assignment: ~ nodes: - Block: - Block: + Container: tag: name: if bits: diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_error_recovery.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_error_recovery.snap index fbbfb1a..6a21ffa 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_error_recovery.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_error_recovery.snap @@ -14,7 +14,7 @@ nodes: start: 28 length: 15 - Block: - Block: + Container: tag: name: if bits: @@ -62,7 +62,7 @@ nodes: start: 198 length: 41 - Block: - Block: + Container: tag: name: for bits: diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_django_for.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_django_for.snap index b88f824..d910ede 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_django_for.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_django_for.snap @@ -4,7 +4,7 @@ expression: ast --- nodes: - Block: - Block: + Container: tag: name: for bits: diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_django_if.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_django_if.snap index 0f2ba36..775b76c 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_django_if.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_django_if.snap @@ -4,7 +4,7 @@ expression: ast --- nodes: - Block: - Block: + Container: tag: name: if bits: diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__full_templates__parse_full.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__full_templates__parse_full.snap index 8d705c4..b8da3ff 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__full_templates__parse_full.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__full_templates__parse_full.snap @@ -94,7 +94,7 @@ nodes: start: 394 length: 56 - Block: - Block: + Container: tag: name: if bits: @@ -143,7 +143,7 @@ nodes: start: 598 length: 6 - Block: - Block: + Container: tag: name: if bits: diff --git a/crates/djls-template-ast/src/tagspecs.rs b/crates/djls-template-ast/src/tagspecs.rs index 80b54a9..9c9d918 100644 --- a/crates/djls-template-ast/src/tagspecs.rs +++ b/crates/djls-template-ast/src/tagspecs.rs @@ -137,9 +137,9 @@ impl TagSpec { #[derive(Clone, Debug, Deserialize, PartialEq)] #[serde(rename_all = "lowercase")] pub enum TagType { - Block, - Single, + Container, Inclusion, + Single, } #[derive(Clone, Debug, Deserialize)] @@ -238,7 +238,7 @@ mod tests { let pyproject_content = r#" [tool.djls.template.tags.mytag] -type = "block" +type = "container" closing = "endmytag" branches = ["mybranch"] args = [{ name = "myarg", required = true }] @@ -248,10 +248,10 @@ args = [{ name = "myarg", required = true }] let specs = TagSpecs::load_all(root)?; let if_tag = specs.get("if").expect("if tag should be present"); - assert_eq!(if_tag.tag_type, TagType::Block); + assert_eq!(if_tag.tag_type, TagType::Container); let my_tag = specs.get("mytag").expect("mytag should be present"); - assert_eq!(my_tag.tag_type, TagType::Block); + assert_eq!(my_tag.tag_type, TagType::Container); assert_eq!(my_tag.closing, Some("endmytag".to_string())); let branches = my_tag @@ -276,14 +276,14 @@ args = [{ name = "myarg", required = true }] let djls_content = r#" [mytag1] -type = "block" +type = "container" closing = "endmytag1" "#; fs::write(root.join("djls.toml"), djls_content)?; let pyproject_content = r#" [tool.djls.template.tags] -mytag2.type = "block" +mytag2.type = "container" mytag2.closing = "endmytag2" "#; fs::write(root.join("pyproject.toml"), pyproject_content)?; diff --git a/crates/djls-template-ast/tagspecs/django.toml b/crates/djls-template-ast/tagspecs/django.toml index 35282d9..77ee171 100644 --- a/crates/djls-template-ast/tagspecs/django.toml +++ b/crates/djls-template-ast/tagspecs/django.toml @@ -1,14 +1,14 @@ [django.template.defaulttags.autoescape] args = [{ name = "setting", required = true, allowed_values = ["on", "off"] }] closing = "endautoescape" -type = "block" +type = "container" [django.template.defaulttags.block] closing = "endblock" -type = "block" +type = "container" [django.template.defaulttags.comment] -type = "block" +type = "container" closing = "endcomment" @@ -34,10 +34,10 @@ args = [ ] branches = ["empty"] closing = "endfor" -type = "block" +type = "container" [django.template.defaulttags.filter] -type = "block" +type = "container" closing = "endfilter" args = [{ name = "filter_expr", required = true }] @@ -49,7 +49,7 @@ args = [{ name = "variables", required = true }] args = [{ name = "condition", required = true }] branches = ["elif", "else"] closing = "endif" -type = "block" +type = "container" [django.template.defaulttags.include] type = "inclusion" @@ -68,7 +68,7 @@ type = "single" args = [{ name = "format_string", required = true }] [django.template.defaulttags.spaceless] -type = "block" +type = "container" closing = "endspaceless" [django.template.defaulttags.templatetag] @@ -96,10 +96,10 @@ args = [ ] [django.template.defaulttags.verbatim] -type = "block" +type = "container" closing = "endverbatim" [django.template.defaulttags.with] -type = "block" +type = "container" closing = "endwith" args = [{ name = "extra_context", required = true }]