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

Rewrite pug-code-gen as an babel AST generator #3019

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2ff672e
Rewrite pug-code-gen as an babel AST generator
jeromew Jun 1, 2018
486a3d8
remove emptyStatement in return
jeromew Jun 4, 2018
5cf03d8
Remove buffer optimization (should be move to a babel plugin)
jeromew Jun 5, 2018
d66116d
Refactor to make visitors return an AST array
jeromew Jun 6, 2018
ab49478
replace concat with push.apply
jeromew Jun 7, 2018
4dba99b
Add babel plugin for buffer assignment compaction
jeromew Jun 7, 2018
d2089a1
babel plugin: use mode strict
jeromew Jun 7, 2018
81a98c9
lexer can now pass AST to code-gen (avoid double parsing)
jeromew Jun 7, 2018
00fb754
Improve ast lexer --> code-gen path
jeromew Jun 8, 2018
6dc8edd
remove mention of acorn
jeromew Jun 8, 2018
b2e996d
use preprocessed AST in mixin args
jeromew Jun 8, 2018
76e51b7
perf: modify babel-plugin-transform-with usage
jeromew Jun 18, 2018
f5267a0
remove babel transformFromAst (perf). Custom code for with and compac…
jeromew Oct 4, 2018
cca9a97
Upgrade with to 6.0.0
jeromew Oct 4, 2018
43e756b
Remove babel plugins. Remove useless code optimization.
jeromew Feb 1, 2019
11965fc
Extract custom `with` code in a method
jeromew Feb 2, 2019
cb8b447
rebase babel branch on master
jeromew Jun 13, 2020
8c99d16
format code
jeromew Jun 13, 2020
f9f235e
Upgrade to babel 7
jeromew Jun 13, 2020
55ddab1
Improve performance
jeromew Jun 15, 2020
c782102
Fix format
jeromew Jun 15, 2020
0202c28
Improve performance
jeromew Jun 16, 2020
3f26f90
Replace stringify with @babel/generator's jsescOption isScriptContext
jeromew Jun 16, 2020
3d729e1
Improve postprocess speed by avoiding compacted branches
jeromew Jun 16, 2020
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
Prev Previous commit
Next Next commit
Improve performance
  • Loading branch information
jeromew committed Jun 15, 2020
commit 55ddab1bf805482db8249df0e8480bcdc9f278dc
95 changes: 50 additions & 45 deletions packages/pug-code-gen/index.js
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ var selfClosing = require('void-elements');
var constantinople = require('constantinople');
var stringify = require('js-stringify');

var findGlobals = require('with/lib/globals.js');
var findGlobals = require('with/lib/globals.js').default;

var t = require('@babel/types');
var gen = require('@babel/generator');
@@ -34,6 +34,8 @@ var INTERNAL_VARIABLES = [
];

var push = Array.prototype.push;
var unshift = Array.prototype.unshift;
var concat = Array.prototype.concat;

var tpl_interp = babelTemplate(
'null == (pug_interp = VALUE) ? "" : pug_interp'
@@ -42,6 +44,8 @@ var tpl_interp_escape = babelTemplate(
'ESCAPE(null == (pug_interp = VALUE) ? "" : pug_interp)'
);

var tpl_json_buffer = JSON.stringify(babelTemplate.ast(`pug_html = pug_html + placeholder`))

module.exports = generateCode;
module.exports.CodeGenerator = Compiler;
function generateCode(ast, options) {
@@ -144,15 +148,9 @@ Compiler.prototype = {
return node;
},
ast_buffer: function(ast) {
return [
t.expressionStatement(
t.assignmentExpression(
'=',
t.identifier('pug_html'),
t.binaryExpression('+', t.identifier('pug_html'), ast)
)
),
];
const o = JSON.parse(tpl_json_buffer)
o.expression.right.right = ast
return [o]
},
ast_with: function(ast) {
let exclude = this.options.globals
@@ -459,9 +457,10 @@ Compiler.prototype = {
*/

buffer: function(str) {
var lit = this.ast_stringify(t.stringLiteral(str));
var ast = this.ast_buffer(lit);
return ast;
const lit = t.stringLiteral(str)
lit.extra = {rawValue: lit.value, raw: stringify(lit.value)};
//var lit = this.ast_stringify(t.stringLiteral(str));
return this.ast_buffer(lit);
},

/**
@@ -522,6 +521,8 @@ Compiler.prototype = {
* @api public
*/

visitCacheLine: JSON.stringify(babelTemplate.ast(`pug_debug_line = 1;`)),
visitCacheFilename: JSON.stringify(babelTemplate.ast(`pug_debug_filename = "";`)),
visit: function(node, parent) {
var ast = [];
var debug = this.debug;
@@ -545,25 +546,17 @@ Compiler.prototype = {

if (debug && node.debug !== false && node.type !== 'Block') {
if (node.line) {
ast.push(
t.expressionStatement(
t.assignmentExpression(
'=',
t.identifier('pug_debug_line'),
t.numericLiteral(node.line)
)
)
);

const astLine = JSON.parse(this.visitCacheLine)
astLine.expression.right.value = node.line
astLine.expression.right.extra = null
ast.push(astLine)

if (node.filename) {
ast.push(
t.expressionStatement(
t.assignmentExpression(
'=',
t.identifier('pug_debug_filename'),
t.stringLiteral(node.filename)
)
)
);
const astFile = JSON.parse(this.visitCacheFilename)
astFile.expression.right.value = node.filename
astFile.expression.right.extra = null
ast.push(astFile)
}
}
}
@@ -599,8 +592,9 @@ Compiler.prototype = {
throw new TypeError(msg);
}

push.apply(ast, this.visitNode(node, parent));
return ast;
const res = this.visitNode(node, parent)
unshift.apply(res, ast);
return res;
},

/**
@@ -611,6 +605,7 @@ Compiler.prototype = {
*/

visitNode: function(node, parent) {
//console.log('visit', node.type)
return this['visit' + node.type](node, parent);
},

@@ -685,8 +680,11 @@ Compiler.prototype = {
) {
push.apply(ast, this.prettyIndent(1, true));
}
//console.log('start block')
const blocks = Array(2*block.nodes.length)
for (var i = 0; i < block.nodes.length; ++i) {
// Pretty print text
blocks[2*i] = []
if (
pp &&
i > 0 &&
@@ -695,10 +693,12 @@ Compiler.prototype = {
block.nodes[i - 1].type === 'Text' &&
/\n$/.test(block.nodes[i - 1].val)
) {
push.apply(ast, this.prettyIndent(1, false));
blocks[2*i] = this.prettyIndent(1, false)
}
push.apply(ast, this.visit(block.nodes[i], block));
const b = this.visit(block.nodes[i], block)
blocks[2*i+1] = b;
}
ast = ast.concat.apply(ast, blocks);
return ast;
},

@@ -1074,6 +1074,7 @@ Compiler.prototype = {
// pretty print
if (pp && !tag.isInline) push.apply(ast, this.prettyIndent(0, true));
if (tag.selfClosing || (!this.xml && selfClosing[tag.name])) {
/*
push.apply(ast, this.buffer('<'));
push.apply(ast, bufferName());
push.apply(
@@ -1088,6 +1089,16 @@ Compiler.prototype = {
} else {
push.apply(ast, this.buffer('/>'));
}
*/
ast = concat.apply(ast, [
this.buffer('<'),
bufferName(),
this.visitAttributes(
tag.attrs,
this.attributeBlocks(tag.attributeBlocks)
),
this.buffer((this.terse && !tag.selfClosing) ? '>' : '/>')
])
// if it is non-empty throw an error
if (
tag.code ||
@@ -1513,16 +1524,14 @@ Compiler.prototype = {
*/

visitAttributes: function(attrs, attributeBlocks) {
var ast = [];
let ast = [];
if (attributeBlocks.length) {
if (attrs.length) {
var val = this.attrs(attrs);
attributeBlocks.unshift(val);
}
if (attributeBlocks.length > 1) {
push.apply(
ast,
this.bufferExpression(
ast = this.bufferExpression(
this.runtime('attrs') +
'(' +
this.runtime('merge') +
@@ -1532,22 +1541,18 @@ Compiler.prototype = {
stringify(this.terse) +
')'
)
);
} else {
push.apply(
ast,
this.bufferExpression(
ast = this.bufferExpression(
this.runtime('attrs') +
'(' +
attributeBlocks[0] +
', ' +
stringify(this.terse) +
')'
)
);
}
} else if (attrs.length) {
push.apply(ast, this.bufferExpression(this.attrs(attrs, true)));
ast = this.bufferExpression(this.attrs(attrs, true));
}
return ast;
},
2 changes: 1 addition & 1 deletion packages/pug-code-gen/package.json
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@
"pug-error": "^1.3.3",
"pug-runtime": "^2.0.5",
"void-elements": "^3.1.0",
"with": "^6.0.0"
"with": "^7.0.2"
},
"files": [
"index.js"