Skip to content

Commit 340b577

Browse files
author
GitHub Action
committed
Release: 1.14.10
1 parent 13bb155 commit 340b577

File tree

9 files changed

+138
-34
lines changed

9 files changed

+138
-34
lines changed

js/lib/beautifier.js

Lines changed: 21 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/lib/beautifier.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/lib/beautifier.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/lib/beautifier.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/lib/beautify-css.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,13 +1281,11 @@ Beautifier.prototype.beautify = function() {
12811281

12821282
if (variable.match(/[ :]$/)) {
12831283
// we have a variable or pseudo-class, add it and insert one space before continuing
1284-
variable = this.eatString(": ").replace(/\s$/, '');
1284+
variable = this.eatString(": ").replace(/\s+$/, '');
12851285
this.print_string(variable);
12861286
this._output.space_before_token = true;
12871287
}
12881288

1289-
variable = variable.replace(/\s$/, '');
1290-
12911289
// might be sass variable
12921290
if (parenLevel === 0 && variable.indexOf(':') !== -1) {
12931291
insidePropertyValue = true;
@@ -1307,13 +1305,11 @@ Beautifier.prototype.beautify = function() {
13071305

13081306
if (variableOrRule.match(/[ :]$/)) {
13091307
// we have a variable or pseudo-class, add it and insert one space before continuing
1310-
variableOrRule = this.eatString(": ").replace(/\s$/, '');
1308+
variableOrRule = this.eatString(": ").replace(/\s+$/, '');
13111309
this.print_string(variableOrRule);
13121310
this._output.space_before_token = true;
13131311
}
13141312

1315-
variableOrRule = variableOrRule.replace(/\s$/, '');
1316-
13171313
// might be less variable
13181314
if (parenLevel === 0 && variableOrRule.indexOf(':') !== -1) {
13191315
insidePropertyValue = true;

js/lib/beautify.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,9 @@ Beautifier.prototype.handle_word = function(current_token) {
10361036
}
10371037

10381038
if (this._flags.last_token.type === TOKEN.COMMA || this._flags.last_token.type === TOKEN.START_EXPR || this._flags.last_token.type === TOKEN.EQUALS || this._flags.last_token.type === TOKEN.OPERATOR) {
1039-
if (!this.start_of_object_property()) {
1039+
if (!this.start_of_object_property() && !(
1040+
// start of object property is different for numeric values with +/- prefix operators
1041+
in_array(this._flags.last_token.text, ['+', '-']) && this._last_last_text === ':' && this._flags.parent.mode === MODE.ObjectLiteral)) {
10401042
this.allow_wrap_or_preserved_newline(current_token);
10411043
}
10421044
}
@@ -1317,6 +1319,12 @@ Beautifier.prototype.handle_operator = function(current_token) {
13171319
return;
13181320
}
13191321

1322+
if (in_array(current_token.text, ['-', '+']) && this.start_of_object_property()) {
1323+
// numeric value with +/- symbol in front as a property
1324+
this.print_token(current_token);
1325+
return;
1326+
}
1327+
13201328
// Allow line wrapping between operators when operator_position is
13211329
// set to before or preserve
13221330
if (this._flags.last_token.type === TOKEN.OPERATOR && in_array(this._options.operator_position, OPERATOR_POSITION_BEFORE_OR_PRESERVE)) {
@@ -2143,12 +2151,13 @@ var nonASCIIidentifierChars = "\\u0300-\\u036f\\u0483-\\u0487\\u0591-\\u05bd\\u0
21432151
//var nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
21442152
//var nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
21452153

2146-
var identifierStart = "(?:\\\\u[0-9a-fA-F]{4}|[" + baseASCIIidentifierStartChars + nonASCIIidentifierStartChars + "])";
2147-
var identifierChars = "(?:\\\\u[0-9a-fA-F]{4}|[" + baseASCIIidentifierChars + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "])*";
2154+
var unicodeEscapeOrCodePoint = "\\\\u[0-9a-fA-F]{4}|\\\\u\\{[0-9a-fA-F]+\\}";
2155+
var identifierStart = "(?:" + unicodeEscapeOrCodePoint + "|[" + baseASCIIidentifierStartChars + nonASCIIidentifierStartChars + "])";
2156+
var identifierChars = "(?:" + unicodeEscapeOrCodePoint + "|[" + baseASCIIidentifierChars + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "])*";
21482157

21492158
exports.identifier = new RegExp(identifierStart + identifierChars, 'g');
21502159
exports.identifierStart = new RegExp(identifierStart);
2151-
exports.identifierMatch = new RegExp("(?:\\\\u[0-9a-fA-F]{4}|[" + baseASCIIidentifierChars + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "])+");
2160+
exports.identifierMatch = new RegExp("(?:" + unicodeEscapeOrCodePoint + "|[" + baseASCIIidentifierChars + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "])+");
21522161

21532162
var nonASCIIwhitespace = /[\u1680\u180e\u2000-\u200a\u202f\u205f\u3000\ufeff]/; // jshint ignore:line
21542163

@@ -2953,6 +2962,9 @@ function unescape_string(s) {
29532962
matched = input_scan.match(/x([0-9A-Fa-f]{2})/g);
29542963
} else if (input_scan.peek() === 'u') {
29552964
matched = input_scan.match(/u([0-9A-Fa-f]{4})/g);
2965+
if (!matched) {
2966+
matched = input_scan.match(/u\{([0-9A-Fa-f]+)\}/g);
2967+
}
29562968
} else {
29572969
out += '\\';
29582970
if (input_scan.hasNext()) {
@@ -2976,7 +2988,9 @@ function unescape_string(s) {
29762988
} else if (escaped >= 0x00 && escaped < 0x20) {
29772989
// leave 0x00...0x1f escaped
29782990
out += '\\' + matched[0];
2979-
continue;
2991+
} else if (escaped > 0x10FFFF) {
2992+
// If the escape sequence is out of bounds, keep the original sequence and continue conversion
2993+
out += '\\' + matched[0];
29802994
} else if (escaped === 0x22 || escaped === 0x27 || escaped === 0x5c) {
29812995
// single-quote, apostrophe, backslash - escape these
29822996
out += '\\' + String.fromCharCode(escaped);

js/lib/cli.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
3333
*/
3434
/*jshint strict:false */
35+
/*jshint esversion: 6 */
36+
37+
const { globSync } = require('glob');
3538

3639
var debug = process.env.DEBUG_JSBEAUTIFY || process.env.JSBEAUTIFY_DEBUG ? function() {
3740
console.error.apply(console, arguments);
@@ -41,7 +44,7 @@ var fs = require('fs'),
4144
cc = require('config-chain'),
4245
beautify = require('../index'),
4346
nopt = require('nopt'),
44-
glob = require('glob');
47+
glob = require("glob");
4548

4649
nopt.invalidHandler = function(key, val) {
4750
throw new Error(key + " was invalid with value \"" + val + "\"");
@@ -634,8 +637,7 @@ function checkFiles(parsed) {
634637
// Input was a glob
635638
if (isGlob) {
636639
hadGlob = true;
637-
foundFiles = glob(f, {
638-
sync: true,
640+
foundFiles = globSync(f, {
639641
absolute: true,
640642
ignore: ['**/node_modules/**', '**/.git/**']
641643
});

js/test/generated/beautify-javascript-tests.js

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,18 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
362362
'var' + unicode_char(160) + unicode_char(3232) + '_' + unicode_char(3232) + ' = "hi";',
363363
// -- output --
364364
'var ' + unicode_char(3232) + '_' + unicode_char(3232) + ' = "hi";');
365+
366+
// Issue #2159: Invalid prettification of object with unicode escape character as object key - test scenario: object with unicode as key
367+
bt(
368+
'{\\u{1d4b6}:"ascr"}',
369+
// -- output --
370+
'{\n' +
371+
' \\u{1d4b6}: "ascr"\n' +
372+
'}');
373+
bt(
374+
'var \\u{E4}\\u{ca0}\\u{0cA0}\\u{000000Ca0} = {\n' +
375+
' \\u{ca0}rgerlich: true\n' +
376+
'};');
365377

366378

367379
//============================================================
@@ -2755,6 +2767,18 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
27552767
' proper: "first_token_should_never_wrap" +\n' +
27562768
' "but_this_can"\n' +
27572769
'}');
2770+
2771+
// Issue #1932 - Javascript object property with -/+ symbol wraps issue
2772+
bt(
2773+
'{\n' +
2774+
' "1234567891234567891234567891234": -433,\n' +
2775+
' "abcdefghijklmnopqrstuvwxyz12345": +11\n' +
2776+
'}',
2777+
// -- output --
2778+
'{\n' +
2779+
' "1234567891234567891234567891234": -433,\n' +
2780+
' "abcdefghijklmnopqrstuvwxyz12345": +11\n' +
2781+
'}');
27582782
test_fragment(
27592783
'' + wrap_input_2 + '',
27602784
// -- output --
@@ -10271,21 +10295,40 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
1027110295
bt('"—"');
1027210296
bt('"\\x41\\x42\\x43\\x01"', '"\\x41\\x42\\x43\\x01"');
1027310297
bt('"\\u2022"', '"\\u2022"');
10298+
bt('"\\u{2022}"', '"\\u{2022}"');
1027410299
bt('a = /\s+/');
1027510300
// bt('a = /\\x41/','a = /A/');
1027610301
bt('"\\u2022";a = /\s+/;"\\x41\\x42\\x43\\x01".match(/\\x41/);','"\\u2022";\na = /\s+/;\n"\\x41\\x42\\x43\\x01".match(/\\x41/);');
10277-
test_fragment('"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff and \\xzz","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"', '"\\x22\\x27", \'\\x22\\x27\', "\\x5c", \'\\x5c\', "\\xff and \\xzz", "unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"');
10302+
10303+
test_fragment('"\\x41\\x42\\x01\\x43"');
10304+
test_fragment('"\\x41\\x42\\u0001\\x43"');
10305+
test_fragment('"\\x41\\x42\\u{0001}\\x43"');
10306+
test_fragment('"\\x20\\x40\\x4a"');
10307+
test_fragment('"\\xff\\x40\\x4a"');
10308+
test_fragment('"\\u0072\\u016B\\u0137\\u012B\\u0074\\u0069\\u0073"');
10309+
test_fragment('"\\u{0072}\\u{016B}\\u{110000}\\u{137}\\u012B\\x74\\u{0000069}\\u{073}"');
10310+
test_fragment('"Google Chrome est\\u00E1 actualizado."');
10311+
test_fragment(
10312+
'"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff and \\xzz","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"',
10313+
'"\\x22\\x27", \'\\x22\\x27\', "\\x5c", \'\\x5c\', "\\xff and \\xzz", "unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"');
1027810314

1027910315
opts.unescape_strings = true;
10316+
10317+
test_fragment('"\\x41\\x42\\x01\\x43"', '"AB\\x01C"');
10318+
test_fragment('"\\x41\\x42\\u0001\\x43"', '"AB\\u0001C"');
10319+
test_fragment('"\\x41\\x42\\u{0001}\\x43"', '"AB\\u{0001}C"');
1028010320
test_fragment('"\\x20\\x40\\x4a"', '" @J"');
1028110321
test_fragment('"\\xff\\x40\\x4a"');
1028210322
test_fragment('"\\u0072\\u016B\\u0137\\u012B\\u0074\\u0069\\u0073"', '"\u0072\u016B\u0137\u012B\u0074\u0069\u0073"');
10323+
test_fragment('"\\u{0072}\\u{016B}\\u{110000}\\u{137}\\u012B\\x74\\u{0000069}\\u{073}"', '"\u0072\u016B\\u{110000}\u0137\u012B\u0074\u0069\u0073"');
1028310324
test_fragment('"Google Chrome est\\u00E1 actualizado."', '"Google Chrome está actualizado."');
10284-
test_fragment('"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff and \\xzz","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff"',
10285-
'"\\"\\\'", \'\\"\\\'\', "\\\\", \'\\\\\', "\\xff and \\xzz", "unicode \\u0000 \\" \\\' \\\\ ' + unicode_char(0xffff) + '"');
10325+
test_fragment(
10326+
'"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff and \\xzz","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff"',
10327+
'"\\"\\\'", \'\\"\\\'\', "\\\\", \'\\\\\', "\\xff and \\xzz", "unicode \\u0000 \\" \\\' \\\\ ' + unicode_char(0xffff) + '"');
1028610328

1028710329
// For error case, return the string unchanged
10288-
test_fragment('"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff and \\xzz","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"',
10330+
test_fragment(
10331+
'"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff and \\xzz","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"',
1028910332
'"\\"\\\'", \'\\"\\\'\', "\\\\", \'\\\\\', "\\xff and \\xzz", "unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"');
1029010333

1029110334
reset_options();

python/jsbeautifier/tests/generated/tests.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,32 @@ def unicode_char(value):
8383
bt('"—"')
8484
bt('"\\x41\\x42\\x43\\x01"', '"\\x41\\x42\\x43\\x01"')
8585
bt('"\\u2022"', '"\\u2022"')
86+
bt('"\\u{2022}"', '"\\u{2022}"')
8687
bt('a = /\s+/')
8788
#bt('a = /\\x41/','a = /A/')
8889
bt('"\\u2022";a = /\s+/;"\\x41\\x42\\x43\\x01".match(/\\x41/);','"\\u2022";\na = /\s+/;\n"\\x41\\x42\\x43\\x01".match(/\\x41/);')
89-
test_fragment('"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff and \\xzz","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"', '"\\x22\\x27", \'\\x22\\x27\', "\\x5c", \'\\x5c\', "\\xff and \\xzz", "unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"')
90+
91+
test_fragment('"\\x41\\x42\\x01\\x43"')
92+
test_fragment('"\\x41\\x42\\u0001\\x43"')
93+
test_fragment('"\\x41\\x42\\u{0001}\\x43"')
94+
test_fragment('"\\x20\\x40\\x4a"')
95+
test_fragment('"\\xff\\x40\\x4a"')
96+
test_fragment('"\\u0072\\u016B\\u0137\\u012B\\u0074\\u0069\\u0073"')
97+
test_fragment('"\\u{0072}\\u{016B}\\u{110000}\\u{137}\\u012B\\x74\\u{0000069}\\u{073}"')
98+
test_fragment('"Google Chrome est\\u00E1 actualizado."')
99+
test_fragment(
100+
'"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff and \\xzz","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"',
101+
'"\\x22\\x27", \'\\x22\\x27\', "\\x5c", \'\\x5c\', "\\xff and \\xzz", "unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"')
90102

91103
self.options.unescape_strings = True
92104

93-
bt('"\\x41\\x42\\x43\\x01"', '"ABC\\x01"')
105+
bt('"\\x41\\x42\\x01\\x43"', '"AB\\x01C"')
106+
bt('"\\x41\\x42\\u0001\\x43"', '"AB\\u0001C"')
107+
bt('"\\x41\\x42\\u{0001}\\x43"', '"AB\\u{0001}C"')
94108
test_fragment('"\\x20\\x40\\x4a"', '" @J"')
95109
test_fragment('"\\xff\\x40\\x4a"')
96110
test_fragment('"\\u0072\\u016B\\u0137\\u012B\\u0074\\u0069\\u0073"', six.u('"\u0072\u016B\u0137\u012B\u0074\u0069\u0073"'))
111+
test_fragment('"\\u{0072}\\u{016B}\\u{110000}\\u{137}\\u012B\\x74\\u{0000069}\\u{073}"', six.u('"\u0072\u016B\\u{110000}\u0137\u012B\u0074\u0069\u0073"'))
97112

98113
bt('a = /\s+/')
99114
test_fragment('"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff"',
@@ -170,6 +185,18 @@ def unicode_char(value):
170185
'var' + unicode_char(160) + unicode_char(3232) + '_' + unicode_char(3232) + ' = "hi";',
171186
# -- output --
172187
'var ' + unicode_char(3232) + '_' + unicode_char(3232) + ' = "hi";')
188+
189+
# Issue #2159: Invalid prettification of object with unicode escape character as object key - test scenario: object with unicode as key
190+
bt(
191+
'{\\u{1d4b6}:"ascr"}',
192+
# -- output --
193+
'{\n' +
194+
' \\u{1d4b6}: "ascr"\n' +
195+
'}')
196+
bt(
197+
'var \\u{E4}\\u{ca0}\\u{0cA0}\\u{000000Ca0} = {\n' +
198+
' \\u{ca0}rgerlich: true\n' +
199+
'};')
173200

174201

175202
#============================================================
@@ -2525,6 +2552,18 @@ def unicode_char(value):
25252552
' proper: "first_token_should_never_wrap" +\n' +
25262553
' "but_this_can"\n' +
25272554
'}')
2555+
2556+
# Issue #1932 - Javascript object property with -/+ symbol wraps issue
2557+
bt(
2558+
'{\n' +
2559+
' "1234567891234567891234567891234": -433,\n' +
2560+
' "abcdefghijklmnopqrstuvwxyz12345": +11\n' +
2561+
'}',
2562+
# -- output --
2563+
'{\n' +
2564+
' "1234567891234567891234567891234": -433,\n' +
2565+
' "abcdefghijklmnopqrstuvwxyz12345": +11\n' +
2566+
'}')
25282567
test_fragment(
25292568
'' + wrap_input_2 + '',
25302569
# -- output --

0 commit comments

Comments
 (0)