Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding 'each .. of' syntax #3179

Merged
merged 5 commits into from Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions packages/pug-code-gen/index.js
Expand Up @@ -767,6 +767,52 @@ Compiler.prototype = {
this.buf.push(' }\n}).call(this);\n');
},

visitEachOf: function(each){
var indexVarName = each.key || 'pug_index' + this.eachCount;
this.eachCount++;

this.buf.push(''
+ '// iterate ' + each.obj + '\n'
+ ';(function(){\n'
+ ' var $$obj = ' + each.obj + ';\n'
maxrumsey marked this conversation as resolved.
Show resolved Hide resolved
+ ' if (\'number\' == typeof $$obj.length) {');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to use the for (const ... of ...) { js syntax and not refer to length at all as in general iteration objects are not referenced by indexing. We'll need to decide how/if we want to handle the key. One option would be to assume value, key means de-structure as [value, key]. I think it might be better to offer:

each x of foo

and

each [x, y] of foo

rather than supporting:

each x, y of foo

That way, if we wanted to, we'd have a clear path towards supporting more destructuring in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed it so you can have

each [key, value] of map

as well as

each keyValPair of map


if (each.alternate) {
this.buf.push(' if ($$obj.length) {');
}

this.buf.push(''
+ ' for (var ' + indexVarName + ' = 0, $$l = $$obj.length; ' + indexVarName + ' < $$l; ' + indexVarName + '++) {\n'
+ ' var ' + each.val + ' = $$obj[' + indexVarName + '];');

this.visit(each.block, each);

this.buf.push(' }');

if (each.alternate) {
this.buf.push(' } else {');
this.visit(each.alternate, each);
this.buf.push(' }');
}

this.buf.push(''
+ ' } else {\n'
+ ' var $$l = 0;\n'
+ ' for (var ' + indexVarName + ' of $$obj) {\n'
+ ' $$l++;\n'
+ ' var ' + each.val + ' = ' + indexVarName + ';');

this.visit(each.block, each);

this.buf.push(' }');
if (each.alternate) {
this.buf.push(' if ($$l === 0) {');
this.visit(each.alternate, each);
this.buf.push(' }');
}
this.buf.push(' }\n}).call(this);\n');
maxrumsey marked this conversation as resolved.
Show resolved Hide resolved
},

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

/**
* EachOf.
*/

eachOf: function() {
var captures;
if (captures = /^(?:each|for) +([a-zA-Z_$][\w$]*)(?: *, *([a-zA-Z_$][\w$]*))? * of *([^\n]+)/.exec(this.input)) {
this.consume(captures[0].length);
var tok = this.tok('eachOf', captures[1]);
tok.key = captures[2] || null;
this.incrementColumn(captures[0].length - captures[3].length);
this.assertExpression(captures[3])
tok.code = captures[3];
this.incrementColumn(captures[3].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 no longer be prefixed with a dash ("-"). They are pug keywords and not part of JavaScript.'
maxrumsey marked this conversation as resolved.
Show resolved Hide resolved
);
}
},

/**
* 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
Expand Up @@ -23,6 +23,7 @@ var lexerFunctions = {
doctype: true,
dot: true,
each: true,
eachOf: true,
eos: true,
endInterpolation: true,
extends: true,
Expand Down
20 changes: 20 additions & 0 deletions packages/pug-parser/index.js
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,24 @@ loop:
return node;
},

parseEachOf: function(){
var tok = this.expect('eachOf');
var node = {
type: 'EachOf',
obj: tok.code,
val: tok.val,
key: tok.key,
maxrumsey marked this conversation as resolved.
Show resolved Hide resolved
block: this.block(),
line: tok.loc.start.line,
column: tok.loc.start.column,
filename: this.filename
};
if (this.peek().type == 'else') {
maxrumsey marked this conversation as resolved.
Show resolved Hide resolved
this.advance();
node.alternate = this.block();
}
return node;
},
/**
* 'extends' name
*/
Expand Down
8 changes: 8 additions & 0 deletions packages/pug-walk/index.js
Expand Up @@ -56,6 +56,14 @@ 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);
}
if (ast.alternate) {
maxrumsey marked this conversation as resolved.
Show resolved Hide resolved
ast.alternate = walkAST(ast.alternate, before, after, options);
}
break;
case 'Conditional':
if (ast.consequent) {
ast.consequent = walkAST(ast.consequent, before, after, options);
Expand Down