@@ -330,6 +330,7 @@ Beautifier.prototype.create_flags = function(flags_base, mode) {
330330 inline_frame : false ,
331331 if_block : false ,
332332 else_block : false ,
333+ class_start_block : false , // class A { INSIDE HERE } or class B extends C { INSIDE HERE }
333334 do_block : false ,
334335 do_while : false ,
335336 import_block : false ,
@@ -742,6 +743,8 @@ Beautifier.prototype.handle_start_expr = function(current_token) {
742743 ( peek_back_two . text === '*' && ( peek_back_three . text === '{' || peek_back_three . text === ',' ) ) ) {
743744 this . _output . space_before_token = true ;
744745 }
746+ } else if ( this . _flags . parent && this . _flags . parent . class_start_block ) {
747+ this . _output . space_before_token = true ;
745748 }
746749 }
747750 } else {
@@ -856,6 +859,12 @@ Beautifier.prototype.handle_start_block = function(current_token) {
856859 this . set_mode ( MODE . BlockStatement ) ;
857860 }
858861
862+ if ( this . _flags . last_token ) {
863+ if ( reserved_array ( this . _flags . last_token . previous , [ 'class' , 'extends' ] ) ) {
864+ this . _flags . class_start_block = true ;
865+ }
866+ }
867+
859868 var empty_braces = ! next_token . comments_before && next_token . text === '}' ;
860869 var empty_anonymous_function = empty_braces && this . _flags . last_word === 'function' &&
861870 this . _flags . last_token . type === TOKEN . END_EXPR ;
@@ -1296,13 +1305,6 @@ Beautifier.prototype.handle_operator = function(current_token) {
12961305 this . handle_whitespace_and_comments ( current_token , preserve_statement_flags ) ;
12971306 }
12981307
1299- if ( reserved_array ( this . _flags . last_token , special_words ) ) {
1300- // "return" had a special handling in TK_WORD. Now we need to return the favor
1301- this . _output . space_before_token = true ;
1302- this . print_token ( current_token ) ;
1303- return ;
1304- }
1305-
13061308 // hack for actionscript's import .*;
13071309 if ( current_token . text === '*' && this . _flags . last_token . type === TOKEN . DOT ) {
13081310 this . print_token ( current_token ) ;
@@ -1430,7 +1432,11 @@ Beautifier.prototype.handle_operator = function(current_token) {
14301432 // http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1
14311433 // if there is a newline between -- or ++ and anything else we should preserve it.
14321434 if ( current_token . newlines && ( current_token . text === '--' || current_token . text === '++' || current_token . text === '~' ) ) {
1433- this . print_newline ( false , true ) ;
1435+ var new_line_needed = reserved_array ( this . _flags . last_token , special_words ) && current_token . newlines ;
1436+ if ( new_line_needed && ( this . _previous_flags . if_block || this . _previous_flags . else_block ) ) {
1437+ this . restore_mode ( ) ;
1438+ }
1439+ this . print_newline ( new_line_needed , true ) ;
14341440 }
14351441
14361442 if ( this . _flags . last_token . text === ';' && is_expression ( this . _flags . mode ) ) {
@@ -1570,6 +1576,10 @@ Beautifier.prototype.handle_dot = function(current_token) {
15701576 this . handle_whitespace_and_comments ( current_token , true ) ;
15711577 }
15721578
1579+ if ( this . _flags . last_token . text . match ( '^[0-9]+$' ) ) {
1580+ this . _output . space_before_token = true ;
1581+ }
1582+
15731583 if ( reserved_array ( this . _flags . last_token , special_words ) ) {
15741584 this . _output . space_before_token = false ;
15751585 } else {
@@ -2554,7 +2564,7 @@ var punct_pattern = new RegExp(punct);
25542564
25552565// words which should always start on new line.
25562566var line_starters = 'continue,try,throw,return,var,let,const,if,switch,case,default,for,while,break,function,import,export' . split ( ',' ) ;
2557- var reserved_words = line_starters . concat ( [ 'do' , 'in' , 'of' , 'else' , 'get' , 'set' , 'new' , 'catch' , 'finally' , 'typeof' , 'yield' , 'async' , 'await' , 'from' , 'as' ] ) ;
2567+ var reserved_words = line_starters . concat ( [ 'do' , 'in' , 'of' , 'else' , 'get' , 'set' , 'new' , 'catch' , 'finally' , 'typeof' , 'yield' , 'async' , 'await' , 'from' , 'as' , 'class' , 'extends' ] ) ;
25582568var reserved_word_pattern = new RegExp ( '^(?:' + reserved_words . join ( '|' ) + ')$' ) ;
25592569
25602570// var template_pattern = /(?:(?:<\?php|<\?=)[\s\S]*?\?>)|(?:<%[\s\S]*?%>)/g;
@@ -2645,7 +2655,8 @@ Tokenizer.prototype._read_word = function(previous_token) {
26452655 if ( ! ( previous_token . type === TOKEN . DOT ||
26462656 ( previous_token . type === TOKEN . RESERVED && ( previous_token . text === 'set' || previous_token . text === 'get' ) ) ) &&
26472657 reserved_word_pattern . test ( resulting_string ) ) {
2648- if ( resulting_string === 'in' || resulting_string === 'of' ) { // hack for 'in' and 'of' operators
2658+ if ( ( resulting_string === 'in' || resulting_string === 'of' ) &&
2659+ ( previous_token . type === TOKEN . WORD || previous_token . type === TOKEN . STRING ) ) { // hack for 'in' and 'of' operators
26492660 return this . _create_token ( TOKEN . OPERATOR , resulting_string ) ;
26502661 }
26512662 return this . _create_token ( TOKEN . RESERVED , resulting_string ) ;
0 commit comments