mirror of
https://github.com/beautifier/js-beautify.git
synced 2024-11-23 04:40:06 +00:00
Merge branch 'main' into feat/2113-inline-custom-elements-behavior
This commit is contained in:
commit
d62a502d09
@ -765,7 +765,7 @@ Beautifier.prototype.handle_start_block = function(current_token) {
|
||||
}
|
||||
}
|
||||
if (this._flags.last_token.type !== TOKEN.OPERATOR && this._flags.last_token.type !== TOKEN.START_EXPR) {
|
||||
if (this._flags.last_token.type === TOKEN.START_BLOCK && !this._flags.inline_frame) {
|
||||
if (in_array(this._flags.last_token.type, [TOKEN.START_BLOCK, TOKEN.SEMICOLON]) && !this._flags.inline_frame) {
|
||||
this.print_newline();
|
||||
} else {
|
||||
this._output.space_before_token = true;
|
||||
|
@ -167,6 +167,7 @@ Tokenizer.prototype._get_next_token = function(previous_token, open_token) { //
|
||||
|
||||
token = token || this._read_non_javascript(c);
|
||||
token = token || this._read_string(c);
|
||||
token = token || this._read_pair(c, this._input.peek(1)); // Issue #2062 hack for record type '#{'
|
||||
token = token || this._read_word(previous_token);
|
||||
token = token || this._read_singles(c);
|
||||
token = token || this._read_comment(c);
|
||||
@ -225,6 +226,19 @@ Tokenizer.prototype._read_singles = function(c) {
|
||||
return token;
|
||||
};
|
||||
|
||||
Tokenizer.prototype._read_pair = function(c, d) {
|
||||
var token = null;
|
||||
if (c === '#' && d === '{') {
|
||||
token = this._create_token(TOKEN.START_BLOCK, c + d);
|
||||
}
|
||||
|
||||
if (token) {
|
||||
this._input.next();
|
||||
this._input.next();
|
||||
}
|
||||
return token;
|
||||
};
|
||||
|
||||
Tokenizer.prototype._read_punctuation = function() {
|
||||
var resulting_string = this.__patterns.punct.read();
|
||||
|
||||
|
@ -831,7 +831,7 @@ class Beautifier:
|
||||
|
||||
elif self._flags.last_token.type not in [TOKEN.OPERATOR, TOKEN.START_EXPR]:
|
||||
if (
|
||||
self._flags.last_token.type == TOKEN.START_BLOCK
|
||||
self._flags.last_token.type in [TOKEN.START_BLOCK, TOKEN.SEMICOLON]
|
||||
and not self._flags.inline_frame
|
||||
):
|
||||
self.print_newline()
|
||||
@ -1208,7 +1208,7 @@ class Beautifier:
|
||||
or current_token.previous.text == ")"
|
||||
)
|
||||
):
|
||||
# This conditionial checks backtick strings and makes no changes
|
||||
# This conditional checks backtick strings and makes no changes
|
||||
pass
|
||||
elif self.start_of_statement(current_token):
|
||||
# The conditional starts the statement if appropriate.
|
||||
|
@ -220,6 +220,9 @@ class Tokenizer(BaseTokenizer):
|
||||
|
||||
token = token or self._read_non_javascript(c)
|
||||
token = token or self._read_string(c)
|
||||
token = token or self._read_pair(
|
||||
c, self._input.peek(1)
|
||||
) # Issue #2062 hack for record type '#{'
|
||||
token = token or self._read_word(previous_token)
|
||||
token = token or self._read_singles(c)
|
||||
token = token or self._read_comment(c)
|
||||
@ -257,6 +260,18 @@ class Tokenizer(BaseTokenizer):
|
||||
|
||||
return token
|
||||
|
||||
def _read_pair(self, c, d):
|
||||
token = None
|
||||
|
||||
if c == "#" and d == "{":
|
||||
token = self._create_token(TOKEN.START_BLOCK, c + d)
|
||||
|
||||
if token is not None:
|
||||
self._input.next()
|
||||
self._input.next()
|
||||
|
||||
return token
|
||||
|
||||
def _read_word(self, previous_token):
|
||||
resulting_string = self._patterns.identifier.read()
|
||||
|
||||
|
@ -192,7 +192,7 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
|
||||
'if (foo)' + eo + '{}' + ec + 'else /regex/.test();');
|
||||
test_fragment('if (foo)' + to + '{', 'if (foo)' + eo + '{');
|
||||
test_fragment('foo' + to + '{', 'foo' + eo + '{');
|
||||
test_fragment('return;' + to + '{', 'return;' + eo + '{');
|
||||
test_fragment('return;' + to + '{', 'return;\n{');
|
||||
bt( 'function x()' + to + '{\n foo();\n}zzz', 'function x()' + eo +'{\n foo();\n}\nzzz');
|
||||
bt( 'var a = new function a()' + to + '{};', 'var a = new function a()' + eo + '{};');
|
||||
bt( 'var a = new function a()' + to + ' {},\n b = new function b()' + to + ' {};',
|
||||
|
@ -3367,6 +3367,33 @@ exports.test_data = {
|
||||
' });',
|
||||
'var test = 1;'
|
||||
]
|
||||
}, {
|
||||
comment: "Issue #1852 - semicolon followed by block statement",
|
||||
unchanged: [
|
||||
'(function() {',
|
||||
' some_code_here();',
|
||||
' {',
|
||||
' /* IE11 let bug bypass */',
|
||||
' let index;',
|
||||
' for (index in a) {',
|
||||
' a[index];',
|
||||
' }',
|
||||
' }',
|
||||
'})();'
|
||||
]
|
||||
}, {
|
||||
comment: "Issue #1852 - semicolon followed by block statement 2",
|
||||
input: [
|
||||
'let x = { A: 1 }; { console.log("hello"); }'
|
||||
],
|
||||
output: [
|
||||
'let x = {',
|
||||
' A: 1',
|
||||
'};',
|
||||
'{',
|
||||
' console.log("hello");',
|
||||
'}'
|
||||
]
|
||||
}, {
|
||||
comment: "Issue #772",
|
||||
input: [
|
||||
@ -5350,6 +5377,52 @@ exports.test_data = {
|
||||
]
|
||||
}
|
||||
]
|
||||
}, {
|
||||
name: "Record data type",
|
||||
description: "",
|
||||
tests: [{
|
||||
comment: 'regular record with primitive',
|
||||
input: 'a = #{ b:"c", d:1, e:true };',
|
||||
output: [
|
||||
'a = #{',
|
||||
' b: "c",',
|
||||
' d: 1,',
|
||||
' e: true',
|
||||
'};'
|
||||
]
|
||||
},
|
||||
{
|
||||
comment: 'nested record',
|
||||
input: 'a = #{b:#{ c:1,d:2,}, e:"f"};',
|
||||
output: [
|
||||
'a = #{',
|
||||
' b: #{',
|
||||
' c: 1,',
|
||||
' d: 2,',
|
||||
' },',
|
||||
' e: "f"',
|
||||
'};'
|
||||
]
|
||||
},
|
||||
{
|
||||
comment: '# not directly followed by { is not handled as record',
|
||||
unchanged: [
|
||||
'a = # {',
|
||||
' b: 1,',
|
||||
' d: true',
|
||||
'};'
|
||||
]
|
||||
},
|
||||
{
|
||||
comment: 'example of already valid and beautified record',
|
||||
unchanged: [
|
||||
'a = #{',
|
||||
' b: 1,',
|
||||
' d: true',
|
||||
'};'
|
||||
]
|
||||
}
|
||||
]
|
||||
}, {
|
||||
// =======================================================
|
||||
// New tests groups should be added above this line.
|
||||
|
@ -59,7 +59,7 @@
|
||||
} }
|
||||
|
||||
space_after_anon_function (default false) - should the space before an anonymous function's parens be added, "function()" vs "function ()",
|
||||
NOTE: This option is overriden by jslint_happy (i.e. if jslint_happy is true, space_after_anon_function is true by design)
|
||||
NOTE: This option is overridden by jslint_happy (i.e. if jslint_happy is true, space_after_anon_function is true by design)
|
||||
|
||||
brace_style (default "collapse") - "collapse" | "expand" | "end-expand" | "none" | any of the former + ",preserve-inline"
|
||||
put braces on the same line as control statements (default), or put braces on own line (Allman / ANSI style), or just put end braces on own line, or attempt to keep them where they are.
|
||||
|
Loading…
Reference in New Issue
Block a user