Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/pug-code-gen/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function Compiler(node, options) {
this.mixins = {};
this.dynamicMixins = false;
this.eachCount = 0;
this.eachOfCount = 0;
if (options.doctype) this.setDoctype(options.doctype);
this.runtimeFunctionsUsed = [];
this.inlineRuntimeFunctions = options.inlineRuntimeFunctions || false;
Expand Down Expand Up @@ -767,6 +768,16 @@ Compiler.prototype = {
this.buf.push(' }\n}).call(this);\n');
},

visitEachOf: function(each){
this.buf.push(''
+ '// iterate ' + each.obj + '\n'
+ 'for (const ' + each.val + ' of ' + each.obj + ') {\n')

this.visit(each.block, each);

this.buf.push('}\n');
},

/**
* Visit `attrs`.
*
Expand Down
26 changes: 26 additions & 0 deletions packages/pug-lexer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,31 @@ Lexer.prototype = {
}
},

/**
* EachOf.
*/

eachOf: function() {
var captures;
if (captures = /^(?:each|for) (.*) * of *([^\n]+)/.exec(this.input)) {
this.consume(captures[0].length);
var tok = this.tok('eachOf', captures[1]);
tok.value = captures[1] || null;
this.incrementColumn(captures[0].length - captures[2].length);
this.assertExpression(captures[2])
tok.code = captures[2];
this.incrementColumn(captures[2].length);
this.tokens.push(this.tokEnd(tok));
return true;
}
if (captures = /^- *(?:each|for) +([a-zA-Z_$][\w$]*)(?: *, *([a-zA-Z_$][\w$]*))? +of +([^\n]+)/.exec(this.input)) {
this.error(
'MALFORMED_EACH',
'Pug each and for should not be prefixed with a dash ("-"). They are pug keywords and not part of JavaScript.'
);
}
},

/**
* Code.
*/
Expand Down Expand Up @@ -1485,6 +1510,7 @@ Lexer.prototype = {
|| this.callLexerFunction('mixin')
|| this.callLexerFunction('call')
|| this.callLexerFunction('conditional')
|| this.callLexerFunction('eachOf')
|| this.callLexerFunction('each')
|| this.callLexerFunction('while')
|| this.callLexerFunction('tag')
Expand Down
1 change: 1 addition & 0 deletions packages/pug-lexer/test/check-lexer-functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var lexerFunctions = {
doctype: true,
dot: true,
each: true,
eachOf: true,
eos: true,
endInterpolation: true,
extends: true,
Expand Down
16 changes: 16 additions & 0 deletions packages/pug-parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ Parser.prototype = {
return this.parseDot();
case 'each':
return this.parseEach();
case 'eachOf':
return this.parseEachOf();
case 'code':
return this.parseCode();
case 'blockcode':
Expand Down Expand Up @@ -761,6 +763,20 @@ loop:
return node;
},

parseEachOf: function(){
var tok = this.expect('eachOf');
var node = {
type: 'EachOf',
obj: tok.code,
val: tok.val,
key: tok.key,
block: this.block(),
line: tok.loc.start.line,
column: tok.loc.start.column,
filename: this.filename
};
return node;
},
/**
* 'extends' name
*/
Expand Down
5 changes: 5 additions & 0 deletions packages/pug-walk/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ function walkAST(ast, before, after, options) {
ast.alternate = walkAST(ast.alternate, before, after, options);
}
break;
case 'EachOf':
if (ast.block) {
ast.block = walkAST(ast.block, before, after, options);
}
break;
case 'Conditional':
if (ast.consequent) {
ast.consequent = walkAST(ast.consequent, before, after, options);
Expand Down