Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/staging/main' into staging/release
Browse files Browse the repository at this point in the history
  • Loading branch information
GitHub Action committed Jun 20, 2022
2 parents ab9d7c3 + 3256a5a commit 3fb3f02
Show file tree
Hide file tree
Showing 22 changed files with 1,309 additions and 337 deletions.
Binary file added .DS_Store
Binary file not shown.
474 changes: 474 additions & 0 deletions CHANGELOG.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -144,8 +144,8 @@ $(BUILD_DIR)/virtualenv: | $(BUILD_DIR)
virtualenv --version || pip install virtualenv
virtualenv build/python-dev
virtualenv build/python-rel
$(SCRIPT_DIR)/python-dev pip install pip --upgrade
$(SCRIPT_DIR)/python-rel pip install pip --upgrade
$(SCRIPT_DIR)/python-dev python -m pip install --upgrade pip || exit 0
$(SCRIPT_DIR)/python-rel python -m pip install --upgrade pip || exit 0
$(SCRIPT_DIR)/python-dev3 pip install black
@touch $(BUILD_DIR)/virtualenv

Expand Down
18 changes: 7 additions & 11 deletions README.md
Expand Up @@ -58,17 +58,13 @@ JS Beautifier is hosted on two CDN services: [cdnjs](https://cdnjs.com/libraries

To pull the latest version from one of these services include one set of the script tags below in your document:
```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.3/beautify.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.3/beautify-css.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.3/beautify-html.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.4/beautify.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.4/beautify-css.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.4/beautify-html.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.3/beautify.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.3/beautify-css.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.3/beautify-html.min.js"></script>

<script src="https://cdn.rawgit.com/beautify-web/js-beautify/v1.14.3/js/lib/beautify.js"></script>
<script src="https://cdn.rawgit.com/beautify-web/js-beautify/v1.14.3/js/lib/beautify-css.js"></script>
<script src="https://cdn.rawgit.com/beautify-web/js-beautify/v1.14.3/js/lib/beautify-html.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.4/beautify.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.4/beautify-css.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.4/beautify-html.min.js"></script>
```

Older versions are available by changing the version number.
Expand Down Expand Up @@ -401,4 +397,4 @@ Thanks also to Jason Diamond, Patrick Hof, Nochum Sossonko, Andreas Schneider, D
Vasilevsky, Vital Batmanov, Ron Baldwin, Gabriel Harrison, Chris J. Shull,
Mathias Bynens, Vittorio Gambaletta and others.
(README.md: js-beautify@1.14.3)
(README.md: js-beautify@1.14.4)
409 changes: 200 additions & 209 deletions index.html

Large diffs are not rendered by default.

13 changes: 10 additions & 3 deletions js/src/css/beautifier.js
Expand Up @@ -68,6 +68,7 @@ function Beautifier(source_text, options) {
"@document": true
};
this.NON_SEMICOLON_NEWLINE_PROPERTY = [
"grid-template-areas",
"grid-template"
];

Expand Down Expand Up @@ -399,7 +400,8 @@ Beautifier.prototype.beautify = function() {
}
}
} else if (this._ch === '"' || this._ch === '\'') {
this.preserveSingleSpace(isAfterSpace);
var preserveQuoteSpace = previous_ch === '"' || previous_ch === '\'';
this.preserveSingleSpace(preserveQuoteSpace || isAfterSpace);
this.print_string(this._ch + this.eatString(this._ch));
this.eatWhitespace(true);
} else if (this._ch === ';') {
Expand Down Expand Up @@ -443,7 +445,12 @@ Beautifier.prototype.beautify = function() {
}
}
} else {
this.preserveSingleSpace(isAfterSpace);
var space_needed = false;
if (this._input.lookBack("with")) {
// look back is not an accurate solution, we need tokens to confirm without whitespaces
space_needed = true;
}
this.preserveSingleSpace(isAfterSpace || space_needed);
this.print_string(this._ch);

// handle scss/sass map
Expand Down Expand Up @@ -501,7 +508,7 @@ Beautifier.prototype.beautify = function() {
this._ch = '';
}
} else if (this._ch === '!' && !this._input.lookBack("\\")) { // !important
this.print_string(' ');
this._output.space_before_token = true;
this.print_string(this._ch);
} else {
var preserveAfterSpace = previous_ch === '"' || previous_ch === '\'';
Expand Down
23 changes: 18 additions & 5 deletions js/src/html/beautifier.js
Expand Up @@ -607,14 +607,19 @@ var TagOpenParserToken = function(parent, raw_token) {
tag_check_match = raw_token.text.match(/^<([^\s>]*)/);
this.tag_check = tag_check_match ? tag_check_match[1] : '';
} else {
tag_check_match = raw_token.text.match(/^{{(?:[\^]|#\*?)?([^\s}]+)/);
tag_check_match = raw_token.text.match(/^{{~?(?:[\^]|#\*?)?([^\s}]+)/);
this.tag_check = tag_check_match ? tag_check_match[1] : '';

// handle "{{#> myPartial}}
if (raw_token.text === '{{#>' && this.tag_check === '>' && raw_token.next !== null) {
this.tag_check = raw_token.next.text.split(' ')[0];
// handle "{{#> myPartial}}" or "{{~#> myPartial}}"
if ((raw_token.text.startsWith('{{#>') || raw_token.text.startsWith('{{~#>')) && this.tag_check[0] === '>') {
if (this.tag_check === '>' && raw_token.next !== null) {
this.tag_check = raw_token.next.text.split(' ')[0];
} else {
this.tag_check = raw_token.text.split('>')[1];
}
}
}

this.tag_check = this.tag_check.toLowerCase();

if (raw_token.type === TOKEN.COMMENT) {
Expand All @@ -626,9 +631,17 @@ var TagOpenParserToken = function(parent, raw_token) {
this.is_end_tag = !this.is_start_tag ||
(raw_token.closed && raw_token.closed.text === '/>');

// if whitespace handler ~ included (i.e. {{~#if true}}), handlebars tags start at pos 3 not pos 2
var handlebar_starts = 2;
if (this.tag_start_char === '{' && this.text.length >= 3) {
if (this.text.charAt(2) === '~') {
handlebar_starts = 3;
}
}

// handlebars tags that don't start with # or ^ are single_tags, and so also start and end.
this.is_end_tag = this.is_end_tag ||
(this.tag_start_char === '{' && (this.text.length < 3 || (/[^#\^]/.test(this.text.charAt(2)))));
(this.tag_start_char === '{' && (this.text.length < 3 || (/[^#\^]/.test(this.text.charAt(handlebar_starts)))));
}
};

Expand Down
26 changes: 18 additions & 8 deletions js/src/javascript/beautifier.js
Expand Up @@ -185,6 +185,7 @@ Beautifier.prototype.create_flags = function(flags_base, mode) {
inline_frame: false,
if_block: false,
else_block: false,
class_start_block: false, // class A { INSIDE HERE } or class B extends C { INSIDE HERE }
do_block: false,
do_while: false,
import_block: false,
Expand Down Expand Up @@ -597,6 +598,8 @@ Beautifier.prototype.handle_start_expr = function(current_token) {
(peek_back_two.text === '*' && (peek_back_three.text === '{' || peek_back_three.text === ','))) {
this._output.space_before_token = true;
}
} else if (this._flags.parent && this._flags.parent.class_start_block) {
this._output.space_before_token = true;
}
}
} else {
Expand Down Expand Up @@ -711,6 +714,12 @@ Beautifier.prototype.handle_start_block = function(current_token) {
this.set_mode(MODE.BlockStatement);
}

if (this._flags.last_token) {
if (reserved_array(this._flags.last_token.previous, ['class', 'extends'])) {
this._flags.class_start_block = true;
}
}

var empty_braces = !next_token.comments_before && next_token.text === '}';
var empty_anonymous_function = empty_braces && this._flags.last_word === 'function' &&
this._flags.last_token.type === TOKEN.END_EXPR;
Expand Down Expand Up @@ -1151,13 +1160,6 @@ Beautifier.prototype.handle_operator = function(current_token) {
this.handle_whitespace_and_comments(current_token, preserve_statement_flags);
}

if (reserved_array(this._flags.last_token, special_words)) {
// "return" had a special handling in TK_WORD. Now we need to return the favor
this._output.space_before_token = true;
this.print_token(current_token);
return;
}

// hack for actionscript's import .*;
if (current_token.text === '*' && this._flags.last_token.type === TOKEN.DOT) {
this.print_token(current_token);
Expand Down Expand Up @@ -1285,7 +1287,11 @@ Beautifier.prototype.handle_operator = function(current_token) {
// http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1
// if there is a newline between -- or ++ and anything else we should preserve it.
if (current_token.newlines && (current_token.text === '--' || current_token.text === '++' || current_token.text === '~')) {
this.print_newline(false, true);
var new_line_needed = reserved_array(this._flags.last_token, special_words) && current_token.newlines;
if (new_line_needed && (this._previous_flags.if_block || this._previous_flags.else_block)) {
this.restore_mode();
}
this.print_newline(new_line_needed, true);
}

if (this._flags.last_token.text === ';' && is_expression(this._flags.mode)) {
Expand Down Expand Up @@ -1425,6 +1431,10 @@ Beautifier.prototype.handle_dot = function(current_token) {
this.handle_whitespace_and_comments(current_token, true);
}

if (this._flags.last_token.text.match('^[0-9]+$')) {
this._output.space_before_token = true;
}

if (reserved_array(this._flags.last_token, special_words)) {
this._output.space_before_token = false;
} else {
Expand Down
5 changes: 3 additions & 2 deletions js/src/javascript/tokenizer.js
Expand Up @@ -95,7 +95,7 @@ var punct_pattern = new RegExp(punct);

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

// var template_pattern = /(?:(?:<\?php|<\?=)[\s\S]*?\?>)|(?:<%[\s\S]*?%>)/g;
Expand Down Expand Up @@ -186,7 +186,8 @@ Tokenizer.prototype._read_word = function(previous_token) {
if (!(previous_token.type === TOKEN.DOT ||
(previous_token.type === TOKEN.RESERVED && (previous_token.text === 'set' || previous_token.text === 'get'))) &&
reserved_word_pattern.test(resulting_string)) {
if (resulting_string === 'in' || resulting_string === 'of') { // hack for 'in' and 'of' operators
if ((resulting_string === 'in' || resulting_string === 'of') &&
(previous_token.type === TOKEN.WORD || previous_token.type === TOKEN.STRING)) { // hack for 'in' and 'of' operators
return this._create_token(TOKEN.OPERATOR, resulting_string);
}
return this._create_token(TOKEN.RESERVED, resulting_string);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "js-beautify",
"version": "1.14.3",
"version": "1.14.4",
"description": "beautifier.io for node",
"main": "js/index.js",
"bin": {
Expand Down
2 changes: 1 addition & 1 deletion python/cssbeautifier/__version__.py
@@ -1 +1 @@
__version__ = "1.14.3"
__version__ = "1.14.4"
13 changes: 9 additions & 4 deletions python/cssbeautifier/css/beautifier.py
Expand Up @@ -119,7 +119,7 @@ def __init__(self, source_text, opts=default_options()):
"@document",
}
self.CONDITIONAL_GROUP_RULE = {"@media", "@supports", "@document"}
self.NON_SEMICOLON_NEWLINE_PROPERTY = ["grid-template"]
self.NON_SEMICOLON_NEWLINE_PROPERTY = ["grid-template-areas", "grid-template"]

def eatString(self, endChars):
result = ""
Expand Down Expand Up @@ -422,7 +422,8 @@ def beautify(self):
# pseudo-element
self.print_string(":")
elif self._ch == '"' or self._ch == "'":
self.preserveSingleSpace(isAfterSpace)
preserveQuoteSpace = previous_ch == '"' or previous_ch == "'"
self.preserveSingleSpace(preserveQuoteSpace or isAfterSpace)
self.print_string(self._ch + self.eatString(self._ch))
self.eatWhitespace(True)
elif self._ch == ";":
Expand Down Expand Up @@ -462,7 +463,11 @@ def beautify(self):
parenLevel -= 1
self.outdent()
else:
self.preserveSingleSpace(isAfterSpace)
space_needed = False
if self._input.lookBack("with"):
# look back is not an accurate solution, we need tokens to confirm without whitespaces
space_needed = True
self.preserveSingleSpace(isAfterSpace or space_needed)
self.print_string(self._ch)

# handle scss/sass map
Expand Down Expand Up @@ -533,7 +538,7 @@ def beautify(self):
self._ch = ""
elif self._ch == "!" and not (self._input.lookBack("\\")):
# !important
self.print_string(" ")
self._output.space_before_token = True
self.print_string(self._ch)
else:
preserveAfterSpace = previous_ch == '"' or previous_ch == "'"
Expand Down
2 changes: 1 addition & 1 deletion python/jsbeautifier/__version__.py
@@ -1 +1 @@
__version__ = "1.14.3"
__version__ = "1.14.4"
26 changes: 19 additions & 7 deletions python/jsbeautifier/javascript/beautifier.py
Expand Up @@ -48,6 +48,7 @@ def __init__(self, mode):
self.inline_frame = False
self.if_block = False
self.else_block = False
self.class_start_block = False
self.do_block = False
self.do_while = False
self.import_block = False
Expand Down Expand Up @@ -609,6 +610,8 @@ def handle_start_expr(self, current_token):
)
):
self._output.space_before_token = True
elif self._flags.parent and self._flags.parent.class_start_block:
self._output.space_before_token = True
else:
# Support preserving wrapped arrow function expressions
# a.b('c',
Expand Down Expand Up @@ -759,6 +762,10 @@ def handle_start_block(self, current_token):
else:
self.set_mode(MODE.BlockStatement)

if self._flags.last_token:
if reserved_array(self._flags.last_token.previous, ["class", "extends"]):
self._flags.class_start_block = True

empty_braces = (
(next_token is not None)
and next_token.comments_before is None
Expand Down Expand Up @@ -1307,12 +1314,6 @@ def handle_operator(self, current_token):
preserve_statement_flags = not isGeneratorAsterisk
self.handle_whitespace_and_comments(current_token, preserve_statement_flags)

if reserved_array(self._flags.last_token, _special_word_set):
# return had a special handling in TK_WORD
self._output.space_before_token = True
self.print_token(current_token)
return

# hack for actionscript's import .*;
if current_token.text == "*" and self._flags.last_token.type == TOKEN.DOT:
self.print_token(current_token)
Expand Down Expand Up @@ -1449,7 +1450,15 @@ def handle_operator(self, current_token):
or current_token.text == "++"
or current_token.text == "~"
):
self.print_newline(preserve_statement_flags=True)
new_line_needed = (
reserved_array(self._flags.last_token, _special_word_set)
and current_token.newlines
)
if new_line_needed and (
self._previous_flags.if_block or self._previous_flags.else_block
):
self.restore_mode()
self.print_newline(new_line_needed, True)

if self._flags.last_token.text == ";" and self.is_expression(
self._flags.mode
Expand Down Expand Up @@ -1593,6 +1602,9 @@ def handle_dot(self, current_token):
else:
self.handle_whitespace_and_comments(current_token, True)

if re.search("^([0-9])+$", self._flags.last_token.text):
self._flags.whitespace_before = True

if reserved_array(self._flags.last_token, _special_word_set):
self._output.space_before_token = False
else:
Expand Down
7 changes: 6 additions & 1 deletion python/jsbeautifier/javascript/tokenizer.py
Expand Up @@ -113,6 +113,8 @@ def __init__(self):
"await",
"from",
"as",
"class",
"extends",
]
)

Expand Down Expand Up @@ -267,7 +269,10 @@ def _read_word(self, previous_token):
and (previous_token.text == "set" or previous_token.text == "get")
)
) and reserved_word_pattern.match(resulting_string):
if resulting_string == "in" or resulting_string == "of":
if (resulting_string == "in" or resulting_string == "of") and (
previous_token.type == TOKEN.WORD
or previous_token.type == TOKEN.STRING
):
# in and of are operators, need to hack
return self._create_token(TOKEN.OPERATOR, resulting_string)

Expand Down

0 comments on commit 3fb3f02

Please sign in to comment.