From 0b8ca4061cd8586e0b822bb905335a9ab3647ffb Mon Sep 17 00:00:00 2001 From: Josh Date: Sat, 4 Jan 2025 23:21:11 -0600 Subject: [PATCH] rename --- crates/djls-template-ast/src/ast.rs | 6 +- crates/djls-template-ast/src/parser.rs | 12 +- ...rser__tests__comments__parse_comments.snap | 3 +- ..._tests__django__parse_complex_if_elif.snap | 62 +++--- ...tests__django__parse_django_for_block.snap | 50 ++--- ..._tests__django__parse_django_if_block.snap | 26 +-- ..._tests__django__parse_django_variable.snap | 15 +- ...r__tests__django__parse_filter_chains.snap | 23 +- ...r__tests__django__parse_mixed_content.snap | 206 ++++++++---------- ...r__tests__django__parse_nested_for_if.snap | 63 +++--- ...__tests__errors__parse_error_recovery.snap | 83 ++++--- ...ts__errors__parse_unclosed_django_for.snap | 30 ++- ...sts__errors__parse_unclosed_django_if.snap | 17 +- ...er__tests__full_templates__parse_full.snap | 95 ++++---- 14 files changed, 321 insertions(+), 370 deletions(-) diff --git a/crates/djls-template-ast/src/ast.rs b/crates/djls-template-ast/src/ast.rs index a119f05..6187022 100644 --- a/crates/djls-template-ast/src/ast.rs +++ b/crates/djls-template-ast/src/ast.rs @@ -36,7 +36,7 @@ impl Ast { pub enum Node { Text(String), Comment(String), - Tag(TagNode), + Block(BlockNode), Variable { bits: Vec, filters: Vec, @@ -44,8 +44,8 @@ pub enum Node { } #[derive(Clone, Debug, Serialize)] -pub enum TagNode { - Block { +pub enum BlockNode { + Standard { name: String, bits: Vec, children: Vec, diff --git a/crates/djls-template-ast/src/parser.rs b/crates/djls-template-ast/src/parser.rs index a9b8450..6232f17 100644 --- a/crates/djls-template-ast/src/parser.rs +++ b/crates/djls-template-ast/src/parser.rs @@ -1,4 +1,4 @@ -use crate::ast::{Ast, AstError, DjangoFilter, DjangoNode, Node, TagNode}; +use crate::ast::{Ast, AstError, BlockNode, DjangoFilter, Node}; use crate::tagspecs::TagSpec; use crate::tokens::{Token, TokenStream, TokenType}; use thiserror::Error; @@ -151,13 +151,13 @@ impl Parser { if spec.closing.as_deref() == Some(&tag) { // If we have a current branch, add it to children if let Some((name, bits, branch_children)) = current_branch { - children.push(Node::Tag(TagNode::Branch { + children.push(Node::Block(BlockNode::Branch { name, bits, children: branch_children, })); } - children.push(Node::Tag(TagNode::Closing { + children.push(Node::Block(BlockNode::Closing { name: tag, bits: vec![], })); @@ -169,7 +169,7 @@ impl Parser { if let Some(branch) = branches.iter().find(|i| i.name == tag) { // If we have a current branch, add it to children if let Some((name, bits, branch_children)) = current_branch { - children.push(Node::Tag(TagNode::Branch { + children.push(Node::Block(BlockNode::Branch { name, bits, children: branch_children, @@ -194,7 +194,7 @@ impl Parser { } } // If we get here, it's an unexpected tag - let node = Node::Tag(TagNode::Block { + let node = Node::Block(BlockNode::Standard { name: tag_name.clone(), bits: bits.clone(), children: children.clone(), @@ -208,7 +208,7 @@ impl Parser { } } - let node = Node::Tag(TagNode::Block { + let node = Node::Block(BlockNode::Standard { name: tag_name.clone(), bits, children, diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__comments__parse_comments.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__comments__parse_comments.snap index f4d1297..d9e7e4c 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__comments__parse_comments.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__comments__parse_comments.snap @@ -4,6 +4,5 @@ expression: ast --- nodes: - Text: "" - - Django: - Comment: Django comment + - Comment: Django comment errors: [] 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 6f60c46..112361d 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 @@ -3,37 +3,33 @@ source: crates/djls-template-ast/src/parser.rs expression: ast --- nodes: - - Django: - Tag: - Block: - name: if - bits: - - if - - x - - ">" - - "0" - children: - - Text: Positive - - Django: - Tag: - Branch: - name: elif - bits: - - x - - "<" - - "0" - children: - - Text: Negative - - Django: - Tag: - Branch: - name: else - bits: [] - children: - - Text: Zero - - Django: - Tag: - Closing: - name: endif - bits: [] + - Block: + Standard: + name: if + bits: + - if + - x + - ">" + - "0" + children: + - Text: Positive + - Block: + Branch: + name: elif + bits: + - x + - "<" + - "0" + children: + - Text: Negative + - Block: + Branch: + name: else + bits: [] + children: + - Text: Zero + - Block: + Closing: + name: endif + bits: [] errors: [] 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 37ebb12..7571c2c 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 @@ -3,31 +3,27 @@ source: crates/djls-template-ast/src/parser.rs expression: ast --- nodes: - - Django: - Tag: - Block: - name: for - bits: - - for - - item - - in - - items - children: - - Django: - Variable: - bits: - - item - filters: [] - - Django: - Tag: - Branch: - name: empty - bits: [] - children: - - Text: No items - - Django: - Tag: - Closing: - name: endfor - bits: [] + - Block: + Standard: + name: for + bits: + - for + - item + - in + - items + children: + - Variable: + bits: + - item + filters: [] + - Block: + Branch: + name: empty + bits: [] + children: + - Text: No items + - Block: + Closing: + name: endfor + bits: [] errors: [] 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 c78efe8..39973ad 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 @@ -3,18 +3,16 @@ source: crates/djls-template-ast/src/parser.rs expression: ast --- nodes: - - Django: - Tag: - Block: - name: if - bits: - - if - - user.is_authenticated - children: - - Text: Welcome - - Django: - Tag: - Closing: - name: endif - bits: [] + - Block: + Standard: + name: if + bits: + - if + - user.is_authenticated + children: + - Text: Welcome + - Block: + Closing: + name: endif + bits: [] errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_variable.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_variable.snap index 51372e2..e51a045 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_variable.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_variable.snap @@ -3,12 +3,11 @@ source: crates/djls-template-ast/src/parser.rs expression: ast --- nodes: - - Django: - Variable: - bits: - - user - - name - filters: - - name: title - arguments: [] + - Variable: + bits: + - user + - name + filters: + - name: title + arguments: [] errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_filter_chains.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_filter_chains.snap index c477b90..abae67b 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_filter_chains.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_filter_chains.snap @@ -3,16 +3,15 @@ source: crates/djls-template-ast/src/parser.rs expression: ast --- nodes: - - Django: - Variable: - bits: - - value - filters: - - name: default - arguments: - - "'nothing'" - - name: title - arguments: [] - - name: upper - arguments: [] + - Variable: + bits: + - value + filters: + - name: default + arguments: + - "'nothing'" + - name: title + arguments: [] + - name: upper + arguments: [] errors: [] 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 aef217a..f0827f9 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 @@ -4,115 +4,101 @@ expression: ast --- nodes: - Text: "Welcome, " - - Django: - Tag: - Block: - name: if - bits: - - if - - user.is_authenticated - children: - - Text: "\n " - - Django: - Variable: - bits: - - user - - name - filters: - - name: title - arguments: [] - - name: default - arguments: - - "'Guest'" - - Text: "\n " - - Django: - Tag: - Block: - name: for - bits: - - for - - group - - in - - user.groups - children: - - Text: "\n " - - Django: - Tag: - Block: - name: if - bits: - - if - - forloop.first - children: - - Text: ( - - Django: - Tag: - Closing: - name: endif - bits: [] - - Text: "\n " - - Django: - Variable: - bits: - - group - - name - filters: [] - - Text: "\n " - - Django: - Tag: - Block: - name: if - bits: - - if - - not - - forloop.last - children: - - Text: ", " - - Django: - Tag: - Closing: - name: endif - bits: [] - - Text: "\n " - - Django: - Tag: - Block: - name: if - bits: - - if - - forloop.last - children: - - Text: ) - - Django: - Tag: - Closing: - name: endif - bits: [] - - Text: "\n " - - Django: - Tag: - Branch: - name: empty - bits: [] - children: - - Text: "\n (no groups)\n " - - Django: - Tag: - Closing: - name: endfor - bits: [] - - Text: "\n" - - Django: - Tag: - Branch: - name: else - bits: [] - children: - - Text: "\n Guest\n" - - Django: - Tag: - Closing: - name: endif - bits: [] + - Block: + Standard: + name: if + bits: + - if + - user.is_authenticated + children: + - Text: "\n " + - Variable: + bits: + - user + - name + filters: + - name: title + arguments: [] + - name: default + arguments: + - "'Guest'" + - Text: "\n " + - Block: + Standard: + name: for + bits: + - for + - group + - in + - user.groups + children: + - Text: "\n " + - Block: + Standard: + name: if + bits: + - if + - forloop.first + children: + - Text: ( + - Block: + Closing: + name: endif + bits: [] + - Text: "\n " + - Variable: + bits: + - group + - name + filters: [] + - Text: "\n " + - Block: + Standard: + name: if + bits: + - if + - not + - forloop.last + children: + - Text: ", " + - Block: + Closing: + name: endif + bits: [] + - Text: "\n " + - Block: + Standard: + name: if + bits: + - if + - forloop.last + children: + - Text: ) + - Block: + Closing: + name: endif + bits: [] + - Text: "\n " + - Block: + Branch: + name: empty + bits: [] + children: + - Text: "\n (no groups)\n " + - Block: + Closing: + name: endfor + bits: [] + - Text: "\n" + - Block: + Branch: + name: else + bits: [] + children: + - Text: "\n Guest\n" + - Block: + Closing: + name: endif + bits: [] - Text: "!" errors: [] 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 0620bc6..d93a949 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 @@ -3,38 +3,33 @@ source: crates/djls-template-ast/src/parser.rs expression: ast --- nodes: - - Django: - Tag: - Block: - name: for - bits: - - for - - item - - in - - items - children: - - Django: - Tag: - Block: - name: if - bits: - - if - - item.active - children: - - Django: - Variable: - bits: - - item - - name - filters: [] - - Django: - Tag: - Closing: - name: endif - bits: [] - - Django: - Tag: - Closing: - name: endfor - bits: [] + - Block: + Standard: + name: for + bits: + - for + - item + - in + - items + children: + - Block: + Standard: + name: if + bits: + - if + - item.active + children: + - Variable: + bits: + - item + - name + filters: [] + - Block: + Closing: + name: endif + bits: [] + - Block: + Closing: + name: endfor + bits: [] errors: [] 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 e388dd1..e1427d0 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 @@ -4,50 +4,43 @@ expression: ast --- nodes: - Text: "
\n

Header

\n " - - Django: - Tag: - Block: - name: if - bits: - - if - - user.is_authenticated - children: - - Text: "\n " - - Django: - Comment: This if is unclosed which does matter - - Text: "\n

Welcome " - - Django: - Variable: - bits: - - user - - name - filters: [] - - Text: "

\n
\n " - - Django: - Comment: "This div is unclosed which doesn't matter" - - Text: "\n " - - Django: - Tag: - Block: - name: for - bits: - - for - - item - - in - - items - children: - - Text: "\n " - - Django: - Variable: - bits: - - item - filters: [] - - Text: "\n " - - Django: - Tag: - Closing: - name: endfor - bits: [] - - Text: "\n
Page Footer
\n
" + - Block: + Standard: + name: if + bits: + - if + - user.is_authenticated + children: + - Text: "\n " + - Comment: This if is unclosed which does matter + - Text: "\n

Welcome " + - Variable: + bits: + - user + - name + filters: [] + - Text: "

\n
\n " + - Comment: "This div is unclosed which doesn't matter" + - Text: "\n " + - Block: + Standard: + name: for + bits: + - for + - item + - in + - items + children: + - Text: "\n " + - Variable: + bits: + - item + filters: [] + - Text: "\n " + - Block: + Closing: + name: endfor + bits: [] + - Text: "\n
Page Footer
\n
" errors: - UnclosedTag: if 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 ae2d0fc..35a4608 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 @@ -3,21 +3,19 @@ source: crates/djls-template-ast/src/parser.rs expression: ast --- nodes: - - Django: - Tag: - Block: - name: for - bits: - - for - - item - - in - - items - children: - - Django: - Variable: - bits: - - item - - name - filters: [] + - Block: + Standard: + name: for + bits: + - for + - item + - in + - items + children: + - Variable: + bits: + - item + - name + filters: [] errors: - UnclosedTag: for 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 08a6685..d4ed42d 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 @@ -3,14 +3,13 @@ source: crates/djls-template-ast/src/parser.rs expression: ast --- nodes: - - Django: - Tag: - Block: - name: if - bits: - - if - - user.is_authenticated - children: - - Text: Welcome + - Block: + Standard: + name: if + bits: + - if + - user.is_authenticated + children: + - Text: Welcome errors: - UnclosedTag: if 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 cf43fde..f1b9571 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 @@ -4,56 +4,49 @@ expression: ast --- nodes: - Text: "\n\n \n \n \n \n \n \n
\n " - - Django: - Tag: - Block: - name: if - bits: - - if - - user.is_authenticated - children: - - Text: "\n " - - Django: - Comment: Welcome message - - Text: "\n

Welcome, " - - Django: - Variable: - bits: - - user - - name - filters: - - name: title - arguments: [] - - name: default - arguments: - - "'Guest'" - - Text: "!

\n " - - Django: - Tag: - Block: - name: if - bits: - - if - - user.is_staff - children: - - Text: "\n Admin\n " - - Django: - Tag: - Branch: - name: else - bits: [] - children: - - Text: "\n User\n " - - Django: - Tag: - Closing: - name: endif - bits: [] - - Text: "\n " - - Django: - Tag: - Closing: - name: endif - bits: [] + - Block: + Standard: + name: if + bits: + - if + - user.is_authenticated + children: + - Text: "\n " + - Comment: Welcome message + - Text: "\n

Welcome, " + - Variable: + bits: + - user + - name + filters: + - name: title + arguments: [] + - name: default + arguments: + - "'Guest'" + - Text: "!

\n " + - Block: + Standard: + name: if + bits: + - if + - user.is_staff + children: + - Text: "\n Admin\n " + - Block: + Branch: + name: else + bits: [] + children: + - Text: "\n User\n " + - Block: + Closing: + name: endif + bits: [] + - Text: "\n " + - Block: + Closing: + name: endif + bits: [] - Text: "\n
\n \n" errors: []