Skip to content

Commit 7aebdee

Browse files
committed
better for warnings
1 parent d2d0255 commit 7aebdee

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

test/pages/for.page

+11-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,14 @@ Item at index 0: [@list.0]
1818

1919
for [@map] {
2020
Pair key = [@key]; value = [@value]
21-
}
21+
}
22+
23+
for [@nonexist] {
24+
test
25+
}
26+
27+
for [@nonexist as] {
28+
}
29+
30+
for {
31+
}

wikifier/block-for.go

+22-7
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import (
77
type forBlock struct {
88
iterableName string
99
itemName string
10+
invalid bool
1011
_scope *variableScope
1112
*parserBlock
1213
}
1314

1415
func newForBlock(name string, b *parserBlock) block {
1516
scope := newVariableScopeWithParent(b.parentBlock().variables())
16-
return &forBlock{"", "", scope, b}
17+
return &forBlock{"", "", false, scope, b}
1718
}
1819

1920
func (b *forBlock) variables() *variableScope {
@@ -33,13 +34,27 @@ func (b *forBlock) parse(p *Page) {
3334

3435
// warn if missing iterable name
3536
if b.iterableName == "" {
36-
b.warn(b.openPos, "for{} block is missing iterable name; should be for [@var]")
37+
b.warn(b.openPos, "Missing iterable name, should be: for [@var] {...}")
38+
b.invalid = true
3739
return
3840
}
3941

4042
// warn if missing item name
4143
if b.itemName == "" {
42-
b.warn(b.openPos, "for{} block is missing item name after 'as'")
44+
b.warn(b.openPos, "Missing item name after 'as', should be: for [@var as @item] {...}")
45+
b.invalid = true
46+
return
47+
}
48+
49+
found, err := b.variables().Get(b.iterableName)
50+
if err != nil {
51+
b.warn(b.openPos, err.Error())
52+
b.invalid = true
53+
return
54+
}
55+
if found == nil {
56+
b.warn(b.openPos, "@"+b.iterableName+" is not defined")
57+
b.invalid = true
4358
return
4459
}
4560

@@ -51,14 +66,14 @@ func (b *forBlock) html(p *Page, el element) {
5166
el.setMeta("noTags", true)
5267
el.setMeta("noIndent", true)
5368

54-
// do nothing, this was warned above
55-
if b.itemName == "" || b.iterableName == "" {
69+
// do nothing if invalid
70+
if b.invalid {
5671
return
5772
}
5873

5974
iterable, err := b.variables().Get(b.iterableName)
6075
if err != nil {
61-
b.warn(b.openPos, "for{} block: "+err.Error())
76+
b.warn(b.openPos, err.Error())
6277
return
6378
}
6479

@@ -77,6 +92,6 @@ func (b *forBlock) html(p *Page, el element) {
7792
handleGenericContent(b, p, el)
7893
}
7994
default:
80-
b.warn(b.openPos, "for{} block: @"+b.iterableName+" is not iterable")
95+
b.warn(b.openPos, "@"+b.iterableName+" is not iterable")
8196
}
8297
}

wikifier/block-style.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (sb *styleBlock) parse(page *Page) {
4545
if str, ok := entry.value.(string); ok {
4646
rules[entry.keyTitle] = str
4747
} else {
48-
sb.warn(entry.pos, "non-string value to style{}")
48+
sb.warn(entry.pos, "Only string values are allowed")
4949
}
5050
}
5151

wikifier/block.go

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ func (b *parserBlock) shouldSkipRune(rune) bool {
137137
}
138138

139139
func (b *parserBlock) warn(pos Position, warning string) {
140+
warning = fmt.Sprintf("%s{}: %s", b.blockType(), warning)
140141
b._page.warn(pos, warning)
141142
}
142143

0 commit comments

Comments
 (0)