aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2026-03-26 11:16:37 +0800
committerLi Jin <dragon-fly@qq.com>2026-03-26 11:34:38 +0800
commitad0cd3a39d5d77ec32d2f203c7258f727a06ba6e (patch)
tree211f3f6e58c32d379aaf5fe0c4483f374705e4ba /spec/inputs
parentffdbbbd3e286d7440af411b475c5a13d49897898 (diff)
downloadyuescript-ad0cd3a39d5d77ec32d2f203c7258f727a06ba6e.tar.gz
yuescript-ad0cd3a39d5d77ec32d2f203c7258f727a06ba6e.tar.bz2
yuescript-ad0cd3a39d5d77ec32d2f203c7258f727a06ba6e.zip
feat: add m_end position to AST nodes
- Add end line and column (m_end.m_line, m_end.m_col) to AST output - New AST format: [name, begin_line, begin_col, end_line, end_col, ...children] - Update format_spec.yue to normalize end positions for comparison - Add ast_spec.yue tests for AST end position feature Closes #251
Diffstat (limited to 'spec/inputs')
-rw-r--r--spec/inputs/test/ast_spec.yue91
-rw-r--r--spec/inputs/test/format_spec.yue4
2 files changed, 94 insertions, 1 deletions
diff --git a/spec/inputs/test/ast_spec.yue b/spec/inputs/test/ast_spec.yue
new file mode 100644
index 0000000..11efc94
--- /dev/null
+++ b/spec/inputs/test/ast_spec.yue
@@ -0,0 +1,91 @@
1yue = require "yue"
2
3describe "yue.to_ast", ->
4 it "should return AST with end position for simple expression", ->
5 ast = yue.to_ast "x = 1"
6 assert.is_not_nil ast
7 -- ast format: [name, begin_line, begin_col, end_line, end_col, ...children]
8 assert.same ast[1], "File"
9 assert.same ast[2], 1 -- begin line
10 assert.same ast[3], 1 -- begin col
11 assert.is_number ast[4] -- end line
12 assert.is_number ast[5] -- end col
13
14 it "should have correct end position for leaf nodes", ->
15 ast = yue.to_ast "1"
16 assert.is_not_nil ast
17 -- Leaf node with no children should have format: [name, begin_line, begin_col, end_line, end_col, value]
18 assert.same ast[1], "File"
19 assert.same ast[2], 1
20 assert.same ast[3], 1
21 assert.is_number ast[4]
22 assert.is_number ast[5]
23
24 it "should have end position for multi-line code", ->
25 code = [[
26x = 1
27y = 2]]
28 ast = yue.to_ast code
29 assert.is_not_nil ast
30 assert.same ast[1], "File"
31 assert.same ast[2], 1
32 assert.same ast[3], 1
33 -- End position should be on line 2
34 assert.is_true ast[4] >= 2
35
36 it "should have end position for function definition", ->
37 code = [[
38add = (a, b) ->
39 a + b]]
40 ast = yue.to_ast code
41 assert.is_not_nil ast
42 assert.same ast[1], "File"
43 assert.same ast[2], 1
44 assert.same ast[3], 1
45 assert.is_number ast[4]
46 assert.is_number ast[5]
47
48 it "should have end position for table literal", ->
49 ast = yue.to_ast "{a: 1, b: 2}"
50 assert.is_not_nil ast
51 assert.same ast[1], "File"
52 assert.same ast[2], 1
53 assert.same ast[3], 1
54 assert.is_number ast[4]
55 assert.is_number ast[5]
56
57 it "should have end position for class definition", ->
58 code = [[
59class Person
60 new: (@name) =>
61 getName: => @name]]
62 ast = yue.to_ast code
63 assert.is_not_nil ast
64 assert.same ast[1], "File"
65 assert.same ast[2], 1
66 assert.same ast[3], 1
67 assert.is_true ast[4] >= 3
68
69 it "should return nil and error message for invalid syntax", ->
70 ast, err = yue.to_ast "if then else"
71 assert.is_nil ast
72 assert.is_string err
73
74 it "should support flatten level parameter", ->
75 ast = yue.to_ast "x = 1", 0
76 assert.is_not_nil ast
77 assert.same ast[1], "File"
78 assert.is_number ast[4]
79 assert.is_number ast[5]
80
81 it "should have end position in nested structures", ->
82 code = "x = [i for i = 1, 10]"
83 ast = yue.to_ast code
84 assert.is_not_nil ast
85 assert.same ast[1], "File"
86 assert.same ast[2], 1
87 assert.same ast[3], 1
88 assert.is_number ast[4]
89 assert.is_number ast[5]
90 -- End column should reflect the end of the expression
91 assert.is_true ast[5] > 1
diff --git a/spec/inputs/test/format_spec.yue b/spec/inputs/test/format_spec.yue
index 8c6096a..310b610 100644
--- a/spec/inputs/test/format_spec.yue
+++ b/spec/inputs/test/format_spec.yue
@@ -165,7 +165,9 @@ import "yue"
165rewriteLineCol = (item)-> 165rewriteLineCol = (item)->
166 item[2] = 0 166 item[2] = 0
167 item[3] = 0 167 item[3] = 0
168 for i = 4, #item 168 item[4] = 0
169 item[5] = 0
170 for i = 6, #item
169 switch type item[i] when "table" 171 switch type item[i] when "table"
170 if item[i][1] == "comment" 172 if item[i][1] == "comment"
171 table.remove item, i 173 table.remove item, i