mirror of
https://github.com/beautifier/js-beautify.git
synced 2025-02-25 07:40:49 +00:00
parent
387708b707
commit
e8f01d6805
@ -1312,7 +1312,9 @@ if (!Object.values) {
|
||||
var in_ternary = false;
|
||||
var isGeneratorAsterisk = current_token.text === '*' &&
|
||||
((last_type === 'TK_RESERVED' && in_array(flags.last_text, ['function', 'yield'])) ||
|
||||
(flags.mode === MODE.ObjectLiteral && in_array(last_type, ['TK_START_BLOCK', 'TK_COMMA'])));
|
||||
(flags.mode === MODE.ObjectLiteral && in_array(last_type, ['TK_START_BLOCK', 'TK_COMMA'])) ||
|
||||
(flags.mode === MODE.BlockStatement && in_array(last_type, ['TK_START_BLOCK', 'TK_COMMA', 'TK_END_BLOCK', 'TK_SEMICOLON']))
|
||||
);
|
||||
var isUnary = in_array(current_token.text, ['-', '+']) && (
|
||||
in_array(last_type, ['TK_START_BLOCK', 'TK_START_EXPR', 'TK_EQUALS', 'TK_OPERATOR']) ||
|
||||
in_array(flags.last_text, Tokenizer.line_starters) ||
|
||||
@ -1391,7 +1393,8 @@ if (!Object.values) {
|
||||
if (isGeneratorAsterisk) {
|
||||
allow_wrap_or_preserved_newline();
|
||||
space_before = false;
|
||||
space_after = false;
|
||||
var next_token = get_token(1);
|
||||
space_after = next_token && in_array(next_token.type, ['TK_WORD', 'TK_RESERVED']);
|
||||
} else if (current_token.text === '...') {
|
||||
allow_wrap_or_preserved_newline();
|
||||
space_before = last_type === 'TK_START_BLOCK';
|
||||
|
@ -380,6 +380,28 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
|
||||
' }\n' +
|
||||
'};');
|
||||
|
||||
// also handle generator shorthand in class - #1013
|
||||
bt(
|
||||
'class A {\n' +
|
||||
' fn() {\n' +
|
||||
' return true;\n' +
|
||||
' }\n' +
|
||||
'\n' +
|
||||
' * gen() {\n' +
|
||||
' return true;\n' +
|
||||
' }\n' +
|
||||
'}');
|
||||
bt(
|
||||
'class A {\n' +
|
||||
' * gen() {\n' +
|
||||
' return true;\n' +
|
||||
' }\n' +
|
||||
'\n' +
|
||||
' fn() {\n' +
|
||||
' return true;\n' +
|
||||
' }\n' +
|
||||
'}');
|
||||
|
||||
|
||||
//============================================================
|
||||
// End With Newline - (eof = "\n")
|
||||
|
@ -1232,7 +1232,9 @@ class Beautifier:
|
||||
in_ternary = False
|
||||
isGeneratorAsterisk = current_token.text == '*' and \
|
||||
((self.last_type == 'TK_RESERVED' and self.flags.last_text in ['function', 'yield']) or
|
||||
(self.flags.mode == MODE.ObjectLiteral and self.last_type in ['TK_START_BLOCK', 'TK_COMMA']))
|
||||
(self.flags.mode == MODE.ObjectLiteral and self.last_type in ['TK_START_BLOCK', 'TK_COMMA']) or
|
||||
(self.flags.mode == MODE.BlockStatement and self.last_type in ['TK_START_BLOCK', 'TK_COMMA', 'TK_END_BLOCK', 'TK_SEMICOLON'])
|
||||
)
|
||||
isUnary = current_token.text in ['+', '-'] \
|
||||
and (self.last_type in ['TK_START_BLOCK', 'TK_START_EXPR', 'TK_EQUALS', 'TK_OPERATOR'] \
|
||||
or self.flags.last_text in Tokenizer.line_starters or self.flags.last_text == ',')
|
||||
@ -1303,7 +1305,8 @@ class Beautifier:
|
||||
if isGeneratorAsterisk:
|
||||
self.allow_wrap_or_preserved_newline(current_token)
|
||||
space_before = False
|
||||
space_after = False
|
||||
next_token = self.get_token(1)
|
||||
space_after = next_token and next_token.type in ['TK_WORD','TK_RESERVED']
|
||||
elif current_token.text == '...':
|
||||
self.allow_wrap_or_preserved_newline(current_token)
|
||||
space_before = self.last_type == 'TK_START_BLOCK'
|
||||
|
@ -208,6 +208,28 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
' }\n' +
|
||||
'};')
|
||||
|
||||
# also handle generator shorthand in class - #1013
|
||||
bt(
|
||||
'class A {\n' +
|
||||
' fn() {\n' +
|
||||
' return true;\n' +
|
||||
' }\n' +
|
||||
'\n' +
|
||||
' * gen() {\n' +
|
||||
' return true;\n' +
|
||||
' }\n' +
|
||||
'}')
|
||||
bt(
|
||||
'class A {\n' +
|
||||
' * gen() {\n' +
|
||||
' return true;\n' +
|
||||
' }\n' +
|
||||
'\n' +
|
||||
' fn() {\n' +
|
||||
' return true;\n' +
|
||||
' }\n' +
|
||||
'}')
|
||||
|
||||
|
||||
#============================================================
|
||||
# End With Newline - (eof = "\n")
|
||||
|
@ -119,7 +119,8 @@ exports.test_data = {
|
||||
tests: [
|
||||
{ unchanged: 'return {\n foo() {\n return 42;\n }\n}' },
|
||||
{
|
||||
unchanged: ['var foo = {',
|
||||
unchanged: [
|
||||
'var foo = {',
|
||||
' * bar() {',
|
||||
' yield 42;',
|
||||
' }',
|
||||
@ -137,6 +138,31 @@ exports.test_data = {
|
||||
' }',
|
||||
'};'
|
||||
]
|
||||
}, {
|
||||
comment: 'also handle generator shorthand in class - #1013',
|
||||
unchanged: [
|
||||
'class A {',
|
||||
' fn() {',
|
||||
' return true;',
|
||||
' }',
|
||||
'',
|
||||
' * gen() {',
|
||||
' return true;',
|
||||
' }',
|
||||
'}'
|
||||
]
|
||||
}, {
|
||||
unchanged: [
|
||||
'class A {',
|
||||
' * gen() {',
|
||||
' return true;',
|
||||
' }',
|
||||
'',
|
||||
' fn() {',
|
||||
' return true;',
|
||||
' }',
|
||||
'}'
|
||||
]
|
||||
}
|
||||
]
|
||||
}, {
|
||||
|
Loading…
x
Reference in New Issue
Block a user