Skip to content

Commit ffd14ab

Browse files
authoredApr 22, 2022
Merge pull request #2033 from mhnaeem/feature/space-after-named-function-classes
Fixes #1622 - Adds space_after_named_function option for ES6 classes
2 parents 3f22fa1 + 4cc5e6e commit ffd14ab

File tree

5 files changed

+84
-1
lines changed

5 files changed

+84
-1
lines changed
 

‎js/src/javascript/beautifier.js

+9
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ Beautifier.prototype.create_flags = function(flags_base, mode) {
185185
inline_frame: false,
186186
if_block: false,
187187
else_block: false,
188+
class_start_block: false, // class A { INSIDE HERE } or class B extends C { INSIDE HERE }
188189
do_block: false,
189190
do_while: false,
190191
import_block: false,
@@ -597,6 +598,8 @@ Beautifier.prototype.handle_start_expr = function(current_token) {
597598
(peek_back_two.text === '*' && (peek_back_three.text === '{' || peek_back_three.text === ','))) {
598599
this._output.space_before_token = true;
599600
}
601+
} else if (this._flags.parent && this._flags.parent.class_start_block) {
602+
this._output.space_before_token = true;
600603
}
601604
}
602605
} else {
@@ -711,6 +714,12 @@ Beautifier.prototype.handle_start_block = function(current_token) {
711714
this.set_mode(MODE.BlockStatement);
712715
}
713716

717+
if (this._flags.last_token) {
718+
if (reserved_array(this._flags.last_token.previous, ['class', 'extends'])) {
719+
this._flags.class_start_block = true;
720+
}
721+
}
722+
714723
var empty_braces = !next_token.comments_before && next_token.text === '}';
715724
var empty_anonymous_function = empty_braces && this._flags.last_word === 'function' &&
716725
this._flags.last_token.type === TOKEN.END_EXPR;

‎js/src/javascript/tokenizer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ var punct_pattern = new RegExp(punct);
9595

9696
// words which should always start on new line.
9797
var line_starters = 'continue,try,throw,return,var,let,const,if,switch,case,default,for,while,break,function,import,export'.split(',');
98-
var reserved_words = line_starters.concat(['do', 'in', 'of', 'else', 'get', 'set', 'new', 'catch', 'finally', 'typeof', 'yield', 'async', 'await', 'from', 'as']);
98+
var reserved_words = line_starters.concat(['do', 'in', 'of', 'else', 'get', 'set', 'new', 'catch', 'finally', 'typeof', 'yield', 'async', 'await', 'from', 'as', 'class', 'extends']);
9999
var reserved_word_pattern = new RegExp('^(?:' + reserved_words.join('|') + ')$');
100100

101101
// var template_pattern = /(?:(?:<\?php|<\?=)[\s\S]*?\?>)|(?:<%[\s\S]*?%>)/g;

‎python/jsbeautifier/javascript/beautifier.py

+7
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def __init__(self, mode):
4848
self.inline_frame = False
4949
self.if_block = False
5050
self.else_block = False
51+
self.class_start_block = False
5152
self.do_block = False
5253
self.do_while = False
5354
self.import_block = False
@@ -609,6 +610,8 @@ def handle_start_expr(self, current_token):
609610
)
610611
):
611612
self._output.space_before_token = True
613+
elif self._flags.parent and self._flags.parent.class_start_block:
614+
self._output.space_before_token = True
612615
else:
613616
# Support preserving wrapped arrow function expressions
614617
# a.b('c',
@@ -759,6 +762,10 @@ def handle_start_block(self, current_token):
759762
else:
760763
self.set_mode(MODE.BlockStatement)
761764

765+
if self._flags.last_token:
766+
if reserved_array(self._flags.last_token.previous, ["class", "extends"]):
767+
self._flags.class_start_block = True
768+
762769
empty_braces = (
763770
(next_token is not None)
764771
and next_token.comments_before is None

‎python/jsbeautifier/javascript/tokenizer.py

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ def __init__(self):
113113
"await",
114114
"from",
115115
"as",
116+
"class",
117+
"extends",
116118
]
117119
)
118120

‎test/data/javascript/tests.js

+65
Original file line numberDiff line numberDiff line change
@@ -3191,6 +3191,71 @@ exports.test_data = {
31913191
input: 'switch(x){case -1:break;case !y:{break;}}',
31923192
output: 'switch (x) {\n{{c}}case -1:\n{{c}} break;\n{{c}}case !y: {\n{{c}} break;\n{{c}}\}\n}'
31933193
},
3194+
{
3195+
comment: "Issue #1622 - basic class with function definitions",
3196+
input: [
3197+
'class blah {',
3198+
' constructor() {',
3199+
' this.doStuff()',
3200+
' }',
3201+
' doStuff() {',
3202+
' console.log("stuff")',
3203+
' }',
3204+
'}'
3205+
],
3206+
output: [
3207+
'class blah {',
3208+
' constructor{{nf}}() {',
3209+
' this.doStuff()',
3210+
' }',
3211+
' doStuff{{nf}}() {',
3212+
' console.log("stuff")',
3213+
' }',
3214+
'}'
3215+
]
3216+
},
3217+
{
3218+
comment: "Issue #1622 - class with extends and function definitions",
3219+
input: [
3220+
'class blah extends something {',
3221+
' constructor() {',
3222+
' this.zz = 2 + 2;',
3223+
' }',
3224+
' someOtherFunction() {',
3225+
'this.y = 1;',
3226+
' }',
3227+
'}'
3228+
],
3229+
output: [
3230+
'class blah extends something {',
3231+
' constructor{{nf}}() {',
3232+
' this.zz = 2 + 2;',
3233+
' }',
3234+
' someOtherFunction{{nf}}() {',
3235+
' this.y = 1;',
3236+
' }',
3237+
'}'
3238+
]
3239+
},
3240+
{
3241+
comment: "Issue #1622 - class/extends as a property",
3242+
input: [
3243+
'var a.class = {',
3244+
' ...abc(),',
3245+
'}',
3246+
'b.extends({',
3247+
' bb.s(),',
3248+
'})'
3249+
],
3250+
output: [
3251+
'var a.class = {',
3252+
' ...abc(),',
3253+
'}',
3254+
'b.extends({',
3255+
' bb.s(),',
3256+
'})'
3257+
]
3258+
},
31943259
{
31953260
comment: 'typical greasemonkey start',
31963261
fragment: true,

0 commit comments

Comments
 (0)
Please sign in to comment.