@@ -146,6 +146,31 @@ Compiler.prototype = {
146146 t . binaryExpression ( '+' , t . identifier ( 'pug_html' ) , ast )
147147 ) ) ] ;
148148 } ,
149+ ast_with : function ( ast ) {
150+ let exclude = this . options . globals ? this . options . globals . concat ( INTERNAL_VARIABLES ) : INTERNAL_VARIABLES ;
151+ exclude = exclude . concat ( this . runtimeFunctionsUsed . map ( function ( name ) { return 'pug_' + name ; } ) ) ;
152+ exclude . push ( 'undefined' , 'this' , 'locals' )
153+ let vars = findGlobals ( t . program ( ast ) ) . map ( function ( v ) { return v . name } ) . filter ( function ( v ) { return exclude . indexOf ( v ) === - 1 } )
154+ if ( vars . length > 0 ) {
155+ let bag = 'locals'
156+ ast = [ t . expressionStatement (
157+ t . callExpression (
158+ t . memberExpression ( t . functionExpression ( null , vars . map ( function ( v ) { return t . identifier ( v ) } ) , t . blockStatement ( ast ) ) , t . identifier ( 'call' ) ) ,
159+ [ t . thisExpression ( ) ] . concat ( vars . map ( function ( v ) {
160+ return t . conditionalExpression (
161+ t . binaryExpression ( 'in' , t . stringLiteral ( v ) , t . logicalExpression ( '||' , t . identifier ( bag ) , t . objectExpression ( [ ] ) ) ) ,
162+ t . memberExpression ( t . logicalExpression ( '||' , t . identifier ( bag ) , t . objectExpression ( [ ] ) ) , t . identifier ( v ) ) ,
163+ t . conditionalExpression (
164+ t . binaryExpression ( '!==' , t . unaryExpression ( 'typeof' , t . identifier ( v ) ) , t . stringLiteral ( 'undefined' ) ) ,
165+ t . identifier ( v ) ,
166+ t . identifier ( 'undefined' )
167+ )
168+ )
169+ } ) )
170+ ) ) ]
171+ }
172+ return ast ;
173+ } ,
149174 /**
150175 * This method is called once the AST is built in
151176 * order to apply transformations
@@ -260,26 +285,7 @@ Compiler.prototype = {
260285 ] . concat ( ast ) ;
261286 } else {
262287 // transform `ast` into `with(locals || {}) { ast }`
263- let exclude = this . options . globals ? this . options . globals . concat ( INTERNAL_VARIABLES ) : INTERNAL_VARIABLES ;
264- exclude . concat ( this . runtimeFunctionsUsed . map ( function ( name ) { return 'pug_' + name ; } ) ) ;
265- exclude . push ( 'undefined' , 'this' , 'locals' )
266- let vars = findGlobals ( t . program ( ast ) ) . map ( function ( v ) { return v . name } ) . filter ( function ( v ) { return exclude . indexOf ( v ) === - 1 } )
267- let bag = 'locals'
268- ast = [ t . expressionStatement (
269- t . callExpression (
270- t . memberExpression ( t . functionExpression ( null , vars . map ( function ( v ) { return t . identifier ( v ) } ) , t . blockStatement ( ast ) ) , t . identifier ( 'call' ) ) ,
271- [ t . thisExpression ( ) ] . concat ( vars . map ( function ( v ) {
272- return t . conditionalExpression (
273- t . binaryExpression ( 'in' , t . stringLiteral ( v ) , t . logicalExpression ( '||' , t . identifier ( bag ) , t . objectExpression ( [ ] ) ) ) ,
274- t . memberExpression ( t . identifier ( bag ) , t . identifier ( v ) ) ,
275- t . conditionalExpression (
276- t . binaryExpression ( '!==' , t . unaryExpression ( 'typeof' , t . identifier ( v ) ) , t . stringLiteral ( 'undefined' ) ) ,
277- t . identifier ( v ) ,
278- t . identifier ( 'undefined' )
279- )
280- )
281- } ) )
282- ) ) ]
288+ ast = this . ast_with ( ast ) ;
283289 }
284290
285291
0 commit comments