Skip to content

Commit de9b038

Browse files
committed
refactor: rename dynamic expressiosn to template strings
1 parent f4846fc commit de9b038

File tree

8 files changed

+141
-141
lines changed

8 files changed

+141
-141
lines changed

crates/djc-template-parser/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ How it works is:
339339

340340
```py
341341
def resolve_template_string(ctx: Context, expr: str) -> Any:
342-
return DynamicFilterExpression(
342+
return TemplateStringExpression(
343343
expr_str=expr,
344344
filters=filters,
345345
tags=tags,
@@ -425,7 +425,7 @@ INPUT:
425425

426426
Generated function:
427427
```py
428-
def compiled_func(context, *, expression, translation, variable, filter):
428+
def compiled_func(context, *, template_string, translation, variable, filter):
429429
def _handle_spread(value, raw_token_str, args, kwargs, kwarg_seen):
430430
if hasattr(value, "keys"):
431431
kwargs.extend(value.items())

crates/djc-template-parser/src/ast.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//!
99
//! The AST represents Django template tags in a structured format that captures:
1010
//! - Tag names and attributes
11-
//! - Values with their types (strings, numbers, variables, expressions, etc.)
11+
//! - Values with their types (strings, numbers, variables, template_strings, etc.)
1212
//! - Filter chains and filter arguments
1313
//! - Position information (line/column, start/end indices)
1414
//! - Syntax type (Django `{% %}` vs HTML `< />` tags)
@@ -20,7 +20,7 @@
2020
//! - **`TagValue`**: Represents a value with type information and optional filters - `'some_val'|upper`
2121
//! - **`TagToken`**: Represents a token with position information
2222
//! - **`TagValueFilter`**: Represents a filter applied to a value
23-
//! - **`ValueKind`**: Enum of supported value types (list, dict, int, float, variable, expression, translation, string)
23+
//! - **`ValueKind`**: Enum of supported value types (list, dict, int, float, variable, template_string, translation, string)
2424
//! - **`TagSyntax`**: Enum of supported tag syntaxes (Django vs HTML)
2525
//!
2626
//! All AST types are exposed to Python via PyO3 bindings.
@@ -144,7 +144,7 @@ pub enum ValueKind {
144144
Int,
145145
Float,
146146
Variable,
147-
Expression,
147+
TemplateString, // A string that contains a Django template tags, e.g. `"{{ my_var }}"`
148148
Translation,
149149
String,
150150
}
@@ -159,7 +159,7 @@ impl ValueKind {
159159
"int" => Ok(ValueKind::Int),
160160
"float" => Ok(ValueKind::Float),
161161
"variable" => Ok(ValueKind::Variable),
162-
"expression" => Ok(ValueKind::Expression),
162+
"template_string" => Ok(ValueKind::TemplateString),
163163
"translation" => Ok(ValueKind::Translation),
164164
"string" => Ok(ValueKind::String),
165165
_ => Err(pyo3::exceptions::PyValueError::new_err(format!(
@@ -176,7 +176,7 @@ impl ValueKind {
176176
ValueKind::Int => "int".to_string(),
177177
ValueKind::Float => "float".to_string(),
178178
ValueKind::Variable => "variable".to_string(),
179-
ValueKind::Expression => "expression".to_string(),
179+
ValueKind::TemplateString => "template_string".to_string(),
180180
ValueKind::Translation => "translation".to_string(),
181181
ValueKind::String => "string".to_string(),
182182
}

crates/djc-template-parser/src/tag_compiler.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//! - **Argument ordering**: Maintains Python-like behavior with positional args before keyword args
1111
//! - **Spread operators**: Handles `...list` and `**dict` spread syntax
1212
//! - **Filter compilation**: Converts Django filter chains to Python expressions
13-
//! - **Value type handling**: Properly handles strings, numbers, variables, expressions, etc.
13+
//! - **Value type handling**: Properly handles strings, numbers, variables, template_strings, etc.
1414
//! - **Error detection**: Compile-time detection of invalid argument ordering
1515
//! - **Indentation**: Properly indents generated code for readability
1616
//!
@@ -100,7 +100,7 @@ pub fn compile_ast_to_string(attributes: &[TagAttr]) -> Result<String, CompileEr
100100
}
101101

102102
let mut final_code = String::new();
103-
let signature = "def compiled_func(context, *, expression, translation, variable, filter):";
103+
let signature = "def compiled_func(context, *, template_string, translation, variable, filter):";
104104
final_code.push_str(signature);
105105
final_code.push_str("\n");
106106

@@ -163,7 +163,7 @@ fn compile_value(value: &TagValue) -> Result<String, CompileError> {
163163
Ok(value.token.token.clone())
164164
}
165165
ValueKind::Variable => Ok(format!("variable(context, '{}')", value.token.token)),
166-
ValueKind::Expression => Ok(format!("expression(context, {})", value.token.token)),
166+
ValueKind::TemplateString => Ok(format!("template_string(context, {})", value.token.token)),
167167
ValueKind::Translation => {
168168
let inner_string_start = value.token.token.find('(').map(|i| i + 1).unwrap_or(0);
169169
let inner_string_end = value
@@ -281,7 +281,7 @@ mod tests {
281281
TagValue {
282282
token: create_tag_token(&quoted_token),
283283
children: vec![],
284-
kind: ValueKind::Expression,
284+
kind: ValueKind::TemplateString,
285285
spread: None,
286286
filters: vec![],
287287
start_index: 0,
@@ -344,7 +344,7 @@ mod tests {
344344
fn test_no_attributes() {
345345
let ast = vec![];
346346
let result = compile_ast_to_string(&ast).unwrap();
347-
let expected = r#"def compiled_func(context, *, expression, translation, variable, filter):
347+
let expected = r#"def compiled_func(context, *, template_string, translation, variable, filter):
348348
args = []
349349
kwargs = []
350350
return args, kwargs"#;
@@ -355,7 +355,7 @@ mod tests {
355355
fn test_single_arg() {
356356
let ast = vec![create_arg_attr(create_var_tag_value("my_var"))];
357357
let result = compile_ast_to_string(&ast).unwrap();
358-
let expected = r#"def compiled_func(context, *, expression, translation, variable, filter):
358+
let expected = r#"def compiled_func(context, *, template_string, translation, variable, filter):
359359
args = []
360360
kwargs = []
361361
args.append(variable(context, 'my_var'))
@@ -371,7 +371,7 @@ mod tests {
371371
create_arg_attr(create_int_tag_value(123)),
372372
];
373373
let result = compile_ast_to_string(&ast).unwrap();
374-
let expected = r#"def compiled_func(context, *, expression, translation, variable, filter):
374+
let expected = r#"def compiled_func(context, *, template_string, translation, variable, filter):
375375
args = []
376376
kwargs = []
377377
args.append(variable(context, 'my_var'))
@@ -385,7 +385,7 @@ mod tests {
385385
fn test_single_kwarg() {
386386
let ast = vec![create_kwarg_attr("key", create_var_tag_value("my_var"))];
387387
let result = compile_ast_to_string(&ast).unwrap();
388-
let expected = r#"def compiled_func(context, *, expression, translation, variable, filter):
388+
let expected = r#"def compiled_func(context, *, template_string, translation, variable, filter):
389389
args = []
390390
kwargs = []
391391
kwargs.append(('key', variable(context, 'my_var')))
@@ -400,7 +400,7 @@ mod tests {
400400
create_kwarg_attr("key2", create_string_tag_value("hello")),
401401
];
402402
let result = compile_ast_to_string(&ast).unwrap();
403-
let expected = r#"def compiled_func(context, *, expression, translation, variable, filter):
403+
let expected = r#"def compiled_func(context, *, template_string, translation, variable, filter):
404404
args = []
405405
kwargs = []
406406
kwargs.append(('key1', variable(context, 'my_var')))
@@ -416,7 +416,7 @@ mod tests {
416416
create_kwarg_attr("key", create_string_tag_value("value")),
417417
];
418418
let result = compile_ast_to_string(&ast).unwrap();
419-
let expected = r#"def compiled_func(context, *, expression, translation, variable, filter):
419+
let expected = r#"def compiled_func(context, *, template_string, translation, variable, filter):
420420
args = []
421421
kwargs = []
422422
args.append(42)
@@ -434,7 +434,7 @@ mod tests {
434434
create_kwarg_attr("key", create_string_tag_value("value")),
435435
];
436436
let result = compile_ast_to_string(&ast).unwrap();
437-
let expected = r#"def compiled_func(context, *, expression, translation, variable, filter):
437+
let expected = r#"def compiled_func(context, *, template_string, translation, variable, filter):
438438
def _handle_spread(value, raw_token_str, args, kwargs, kwarg_seen):
439439
if hasattr(value, "keys"):
440440
kwargs.extend(value.items())
@@ -471,7 +471,7 @@ mod tests {
471471
create_kwarg_attr("key2", create_string_tag_value("value2")),
472472
];
473473
let result = compile_ast_to_string(&ast).unwrap();
474-
let expected = r#"def compiled_func(context, *, expression, translation, variable, filter):
474+
let expected = r#"def compiled_func(context, *, template_string, translation, variable, filter):
475475
def _handle_spread(value, raw_token_str, args, kwargs, kwarg_seen):
476476
if hasattr(value, "keys"):
477477
kwargs.extend(value.items())
@@ -499,13 +499,13 @@ mod tests {
499499
}
500500

501501
#[test]
502-
fn test_expression_arg() {
502+
fn test_template_string_arg() {
503503
let ast = vec![create_arg_attr(create_expr_tag_value("\"{{ my_var }}\""))];
504504
let result = compile_ast_to_string(&ast).unwrap();
505-
let expected = r#"def compiled_func(context, *, expression, translation, variable, filter):
505+
let expected = r#"def compiled_func(context, *, template_string, translation, variable, filter):
506506
args = []
507507
kwargs = []
508-
args.append(expression(context, "{{ my_var }}"))
508+
args.append(template_string(context, "{{ my_var }}"))
509509
return args, kwargs"#;
510510
assert_eq!(result, expected.to_string());
511511
}
@@ -514,7 +514,7 @@ mod tests {
514514
fn test_translation_arg() {
515515
let ast = vec![create_arg_attr(create_trans_tag_value("\"hello world\""))];
516516
let result = compile_ast_to_string(&ast).unwrap();
517-
let expected = r#"def compiled_func(context, *, expression, translation, variable, filter):
517+
let expected = r#"def compiled_func(context, *, template_string, translation, variable, filter):
518518
args = []
519519
kwargs = []
520520
args.append(translation(context, "hello world"))
@@ -534,7 +534,7 @@ mod tests {
534534
});
535535
let ast = vec![create_arg_attr(value)];
536536
let result = compile_ast_to_string(&ast).unwrap();
537-
let expected = r#"def compiled_func(context, *, expression, translation, variable, filter):
537+
let expected = r#"def compiled_func(context, *, template_string, translation, variable, filter):
538538
args = []
539539
kwargs = []
540540
args.append(filter(context, 'upper', variable(context, 'my_var'), None))
@@ -554,7 +554,7 @@ mod tests {
554554
});
555555
let ast = vec![create_arg_attr(value)];
556556
let result = compile_ast_to_string(&ast).unwrap();
557-
let expected = r#"def compiled_func(context, *, expression, translation, variable, filter):
557+
let expected = r#"def compiled_func(context, *, template_string, translation, variable, filter):
558558
args = []
559559
kwargs = []
560560
args.append(filter(context, 'default', variable(context, 'my_var'), "none"))
@@ -581,7 +581,7 @@ mod tests {
581581
});
582582
let ast = vec![create_arg_attr(value)];
583583
let result = compile_ast_to_string(&ast).unwrap();
584-
let expected = r#"def compiled_func(context, *, expression, translation, variable, filter):
584+
let expected = r#"def compiled_func(context, *, template_string, translation, variable, filter):
585585
args = []
586586
kwargs = []
587587
args.append(filter(context, 'default', filter(context, 'upper', variable(context, 'my_var'), None), "none"))
@@ -603,7 +603,7 @@ mod tests {
603603
};
604604
let ast = vec![create_arg_attr(list_value)];
605605
let result = compile_ast_to_string(&ast).unwrap();
606-
let expected = r#"def compiled_func(context, *, expression, translation, variable, filter):
606+
let expected = r#"def compiled_func(context, *, template_string, translation, variable, filter):
607607
args = []
608608
kwargs = []
609609
args.append([1, variable(context, 'my_var')])
@@ -628,7 +628,7 @@ mod tests {
628628
};
629629
let ast = vec![create_kwarg_attr("data", dict_value)];
630630
let result = compile_ast_to_string(&ast).unwrap();
631-
let expected = r#"def compiled_func(context, *, expression, translation, variable, filter):
631+
let expected = r#"def compiled_func(context, *, template_string, translation, variable, filter):
632632
args = []
633633
kwargs = []
634634
kwargs.append(('data', {"key": variable(context, 'my_var')}))
@@ -651,7 +651,7 @@ mod tests {
651651
];
652652
let result = compile_ast_to_string(&ast).unwrap();
653653

654-
let expected = r#"def compiled_func(context, *, expression, translation, variable, filter):
654+
let expected = r#"def compiled_func(context, *, template_string, translation, variable, filter):
655655
def _handle_spread(value, raw_token_str, args, kwargs, kwarg_seen):
656656
if hasattr(value, "keys"):
657657
kwargs.extend(value.items())
@@ -689,7 +689,7 @@ mod tests {
689689
flag_attr,
690690
];
691691
let result = compile_ast_to_string(&ast).unwrap();
692-
let expected = r#"def compiled_func(context, *, expression, translation, variable, filter):
692+
let expected = r#"def compiled_func(context, *, template_string, translation, variable, filter):
693693
args = []
694694
kwargs = []
695695
kwargs.append(('key', "value"))

0 commit comments

Comments
 (0)