Skip to content

Commit 2689316

Browse files
author
GitHub Action
committedJun 20, 2022
Release: 1.14.4
1 parent 3fb3f02 commit 2689316

12 files changed

+1133
-42
lines changed
 

‎js/lib/beautifier.js

+49-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎js/lib/beautifier.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎js/lib/beautifier.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎js/lib/beautifier.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎js/lib/beautify-css.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,7 @@ function Beautifier(source_text, options) {
10911091
"@document": true
10921092
};
10931093
this.NON_SEMICOLON_NEWLINE_PROPERTY = [
1094+
"grid-template-areas",
10941095
"grid-template"
10951096
];
10961097

@@ -1422,7 +1423,8 @@ Beautifier.prototype.beautify = function() {
14221423
}
14231424
}
14241425
} else if (this._ch === '"' || this._ch === '\'') {
1425-
this.preserveSingleSpace(isAfterSpace);
1426+
var preserveQuoteSpace = previous_ch === '"' || previous_ch === '\'';
1427+
this.preserveSingleSpace(preserveQuoteSpace || isAfterSpace);
14261428
this.print_string(this._ch + this.eatString(this._ch));
14271429
this.eatWhitespace(true);
14281430
} else if (this._ch === ';') {
@@ -1466,7 +1468,12 @@ Beautifier.prototype.beautify = function() {
14661468
}
14671469
}
14681470
} else {
1469-
this.preserveSingleSpace(isAfterSpace);
1471+
var space_needed = false;
1472+
if (this._input.lookBack("with")) {
1473+
// look back is not an accurate solution, we need tokens to confirm without whitespaces
1474+
space_needed = true;
1475+
}
1476+
this.preserveSingleSpace(isAfterSpace || space_needed);
14701477
this.print_string(this._ch);
14711478

14721479
// handle scss/sass map
@@ -1524,7 +1531,7 @@ Beautifier.prototype.beautify = function() {
15241531
this._ch = '';
15251532
}
15261533
} else if (this._ch === '!' && !this._input.lookBack("\\")) { // !important
1527-
this.print_string(' ');
1534+
this._output.space_before_token = true;
15281535
this.print_string(this._ch);
15291536
} else {
15301537
var preserveAfterSpace = previous_ch === '"' || previous_ch === '\'';

‎js/lib/beautify-html.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -2355,14 +2355,19 @@ var TagOpenParserToken = function(parent, raw_token) {
23552355
tag_check_match = raw_token.text.match(/^<([^\s>]*)/);
23562356
this.tag_check = tag_check_match ? tag_check_match[1] : '';
23572357
} else {
2358-
tag_check_match = raw_token.text.match(/^{{(?:[\^]|#\*?)?([^\s}]+)/);
2358+
tag_check_match = raw_token.text.match(/^{{~?(?:[\^]|#\*?)?([^\s}]+)/);
23592359
this.tag_check = tag_check_match ? tag_check_match[1] : '';
23602360

2361-
// handle "{{#> myPartial}}
2362-
if (raw_token.text === '{{#>' && this.tag_check === '>' && raw_token.next !== null) {
2363-
this.tag_check = raw_token.next.text.split(' ')[0];
2361+
// handle "{{#> myPartial}}" or "{{~#> myPartial}}"
2362+
if ((raw_token.text.startsWith('{{#>') || raw_token.text.startsWith('{{~#>')) && this.tag_check[0] === '>') {
2363+
if (this.tag_check === '>' && raw_token.next !== null) {
2364+
this.tag_check = raw_token.next.text.split(' ')[0];
2365+
} else {
2366+
this.tag_check = raw_token.text.split('>')[1];
2367+
}
23642368
}
23652369
}
2370+
23662371
this.tag_check = this.tag_check.toLowerCase();
23672372

23682373
if (raw_token.type === TOKEN.COMMENT) {
@@ -2374,9 +2379,17 @@ var TagOpenParserToken = function(parent, raw_token) {
23742379
this.is_end_tag = !this.is_start_tag ||
23752380
(raw_token.closed && raw_token.closed.text === '/>');
23762381

2382+
// if whitespace handler ~ included (i.e. {{~#if true}}), handlebars tags start at pos 3 not pos 2
2383+
var handlebar_starts = 2;
2384+
if (this.tag_start_char === '{' && this.text.length >= 3) {
2385+
if (this.text.charAt(2) === '~') {
2386+
handlebar_starts = 3;
2387+
}
2388+
}
2389+
23772390
// handlebars tags that don't start with # or ^ are single_tags, and so also start and end.
23782391
this.is_end_tag = this.is_end_tag ||
2379-
(this.tag_start_char === '{' && (this.text.length < 3 || (/[^#\^]/.test(this.text.charAt(2)))));
2392+
(this.tag_start_char === '{' && (this.text.length < 3 || (/[^#\^]/.test(this.text.charAt(handlebar_starts)))));
23802393
}
23812394
};
23822395

‎js/lib/beautify.js

+21-10
Original file line numberDiff line numberDiff line change
@@ -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.
25562566
var 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']);
25582568
var 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

Comments
 (0)
Please sign in to comment.