Skip to content

Commit a848b67

Browse files
committed
Fix the rule of naming and identifing a macro to keep the same with a reference's.
1 parent 2e4e3fc commit a848b67

File tree

5 files changed

+74
-7
lines changed

5 files changed

+74
-7
lines changed

lib/engine/velocity.js

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/engine/velocity.l

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
%x rp rw r b bpm bp pm p m
1515

16-
A [a-zA-Z]
16+
A [a-zA-Z0-9-_]
1717
ID [a-zA-Z_][a-zA-Z0-9-_]*
1818
LP (?=[ \t]*\()
1919

@@ -76,8 +76,8 @@ LP (?=[ \t]*\()
7676
"#"("{evaluate}"|"evaluate"){LP} { this.pushState('bp'); return 'EVALUATE'; }
7777
"#"("{define}"|"define"){LP} { this.pushState('bp'); return 'DEFINE'; }
7878
"#"("{macro}"|"macro"){LP} { this.pushState('bpm'); return 'MACRO'; }
79-
"#"(\{{A}+\}|{A}+){LP} { this.pushState('bpm'); return 'MACROCALL'; }
80-
"#@"(\{{A}+\}|{A}+){LP} { this.pushState('bpm'); return 'BMACROCALL'; }
79+
"#"(\{{ID}\}|{ID}){LP} { this.pushState('bpm'); return 'MACROCALL'; }
80+
"#@"(\{{ID}\}|{ID}){LP} { this.pushState('bpm'); return 'BMACROCALL'; }
8181
8282
<bp>[ \t]*"(" { this.popState();
8383
this.pushState('p'); return '('; }

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "velocity",
3-
"version": "0.7.0",
3+
"version": "0.7.1",
44
"description": "A node velocity template engine.",
55
"homepage": "https://github.com/fool2fish/velocity",
66
"keywords": [

test/engine.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,12 @@ describe('engine.test.js', function () {
5858
favorites: ['food', 'travel', 'comic', '...']
5959
}).should.equal(utils.string('macro-result.txt'))
6060
})
61+
62+
it('should throw an error when calling an undefined macro', function() {
63+
var engine = new Engine({
64+
template: '#a()'
65+
})
66+
engine.render.bind().should.throw()
67+
})
6168
})
6269
})

test/parser.test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,4 +569,62 @@ describe('parser.test.js', function () {
569569
alternate.consequent.body.should.length(3)
570570
})
571571
})
572+
573+
describe('Macro', function() {
574+
it('should define a macro', function() {
575+
var ast = parser.parse('#macro(a)#end')
576+
ast.body[0].type.should.equal('Macro')
577+
ast.body[0].name.should.equal('a')
578+
579+
ast = parser.parse('#macro(_a)#end')
580+
ast.body[0].type.should.equal('Macro')
581+
ast.body[0].name.should.equal('_a')
582+
583+
ast = parser.parse('#macro(a-)#end')
584+
ast.body[0].type.should.equal('Macro')
585+
ast.body[0].name.should.equal('a-')
586+
587+
ast = parser.parse('#macro(a1)#end')
588+
ast.body[0].type.should.equal('Macro')
589+
ast.body[0].name.should.equal('a1')
590+
591+
parser.parse.bind('#macro(a*)#end').should.throw()
592+
593+
parser.parse.bind('#macro(1a)#end').should.throw()
594+
})
595+
})
596+
597+
describe('MacroCall', function() {
598+
it('should call a macro', function() {
599+
var ast = parser.parse('#a()')
600+
ast.body[0].type.should.equal('MacroCall')
601+
ast.body[0].name.should.equal('a')
602+
603+
ast = parser.parse('#1a()')
604+
ast.body[0].type.should.equal('Text')
605+
606+
ast = parser.parse('#a*()')
607+
ast.body[0].type.should.equal('Text')
608+
609+
ast = parser.parse('#_a()')
610+
ast.body[0].type.should.equal('MacroCall')
611+
ast.body[0].name.should.equal('_a')
612+
613+
ast = parser.parse('#a-()')
614+
ast.body[0].type.should.equal('MacroCall')
615+
ast.body[0].name.should.equal('a-')
616+
617+
ast = parser.parse('#a1()')
618+
ast.body[0].type.should.equal('MacroCall')
619+
ast.body[0].name.should.equal('a1')
620+
621+
ast = parser.parse('#_if()')
622+
ast.body[0].type.should.equal('MacroCall')
623+
ast.body[0].name.should.equal('_if')
624+
625+
ast = parser.parse('#define1()')
626+
ast.body[0].type.should.equal('MacroCall')
627+
ast.body[0].name.should.equal('define1')
628+
})
629+
})
572630
})

0 commit comments

Comments
 (0)