From ada9f66ea1eb5d90324748641a4d4b9e5893c2db Mon Sep 17 00:00:00 2001 From: cw100dev Date: Thu, 8 Sep 2022 17:14:37 -0700 Subject: [PATCH 1/7] changes to both js and python jsbeautifiers for issue #2062 to accoutn for new record types --- .DS_Store | Bin 8196 -> 0 bytes .github/PULL_REQUEST_TEMPLATE.md | 12 ++++++------ js/src/javascript/beautifier.js | 5 ++++- python/jsbeautifier/javascript/beautifier.py | 6 +++++- test/data/javascript/tests.js | 1 + 5 files changed, 16 insertions(+), 8 deletions(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 9a98638419cc3072aab942ae3e81d7cf393056b6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8196 zcmeI1(QeZ)7=@3!Zj@00LtN|zDK9`J-XN4_tP*SzE7zlGyACJ~f>yA*-0vND1)he7 z;R?=gTUG3&7u+BweoyvEod4MIm$a@u0H(j5J^>B^beKi&-(k^^=J<5X0=T`<~qQ760fP((y6twO}Bb5-fH|3!#F#yClXFgy_Qa$9md&Ve9y+OP>k)J zzQl&Zq)xR|0!pAxK-TX2$hl(wf?0L_K3o)&d{N}QE+46<%)MVW@d((ZXN|X5U>Ze! zk0QHxhGY6q@EjRl;FQ|KhuCKxMZO30onk=GIrWI8oS(4fHIGJ(ZCpof-1D9hUSWtn zog=E;Nz)ztjNJPi-x5=D`O5f9ta#UpaI~!-k*6j5A7PAT_pQRNYxhWUwmT-n6??hB z71>WBpOCEjZjs)QBg!~}Gh|`3}iZS;~&#i*z?U^P}p;89<|VoTG-|j*43W(=({4nOZK-6`w-^l zZiV@6z5y9)2c(iUJ$;aN;Mil!V?Grb(<*FTSO>9f#*$Pmj*Nr2UB(ssTQpG(B6sZP zF%SC{azWIAi`ww(k-O}@$rGP*l(lzJ-q)Z7BHfx~s!foP*8kb|{r?*@BE3~5palMufN2lT27S8htlr?3Sw3NY!z@YSN~hMsBKRS| e^UXgDd7Lm*JgL{xDQlQ?Bfw(NLJ9m;0>1%>f*@A_ diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 46cbfa90f..95b6143af 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,5 +1,5 @@ # Description -- [ ] Source branch in your fork has meaningful name (not `main`) +- [ Iss2062_records ] Source branch in your fork has meaningful name (not `main`) Fixes Issue: @@ -11,9 +11,9 @@ These items can be completed after PR is created. (Check any items that are not applicable (NA) for this PR) -- [ ] JavaScript implementation -- [ ] Python implementation (NA if HTML beautifier) -- [ ] Added Tests to data file(s) -- [ ] Added command-line option(s) (NA if -- [ ] README.md documents new feature/option(s) +- [X] JavaScript implementation +- [X] Python implementation (NA if HTML beautifier) +- [X] Added Tests to data file(s) +- [NA] Added command-line option(s) (NA if +- [NA] README.md documents new feature/option(s) diff --git a/js/src/javascript/beautifier.js b/js/src/javascript/beautifier.js index 51cd10d8e..730080a32 100644 --- a/js/src/javascript/beautifier.js +++ b/js/src/javascript/beautifier.js @@ -720,6 +720,9 @@ Beautifier.prototype.handle_start_block = function(current_token) { } } + // Issue #2062 check to see if defining a new Record type - #{} + // Conditional on line 774 uses this variable + var is_record = this._flags.last_word === '#'; 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; @@ -768,7 +771,7 @@ Beautifier.prototype.handle_start_block = function(current_token) { if (this._flags.last_token.type === TOKEN.START_BLOCK && !this._flags.inline_frame) { this.print_newline(); } else { - this._output.space_before_token = true; + this._output.space_before_token = !is_record; } } } diff --git a/python/jsbeautifier/javascript/beautifier.py b/python/jsbeautifier/javascript/beautifier.py index efeb584b4..381d18f8b 100644 --- a/python/jsbeautifier/javascript/beautifier.py +++ b/python/jsbeautifier/javascript/beautifier.py @@ -766,6 +766,10 @@ def handle_start_block(self, current_token): if reserved_array(self._flags.last_token.previous, ["class", "extends"]): self._flags.class_start_block = True + # Issue #2062 check to see if defining a new Record type - #{} + # Conditional on line 843 uses this variable + is_record = self._flags.last_token.text == "#" + empty_braces = ( (next_token is not None) and next_token.comments_before is None @@ -836,7 +840,7 @@ def handle_start_block(self, current_token): ): self.print_newline() else: - self._output.space_before_token = True + self._output.space_before_token = not is_record self.print_token(current_token) self.indent() diff --git a/test/data/javascript/tests.js b/test/data/javascript/tests.js index 367c3e231..cfe9606cf 100644 --- a/test/data/javascript/tests.js +++ b/test/data/javascript/tests.js @@ -5368,6 +5368,7 @@ exports.test_data = { { fragment: true, unchanged: ' < div' }, { input: 'a = 1', output: 'a = 1' }, { input: 'a=1', output: 'a = 1' }, + { input: '#{ hello: "world" }', output: '#{\n hello: "world"\n}' }, { unchanged: '(3) / 2' }, { unchanged: '["a", "b"].join("")' }, { unchanged: 'a();\n\nb();' }, From 1430b97c842939ecff185bd1673653a31cbb0084 Mon Sep 17 00:00:00 2001 From: Antonius Anggito Arissaputro Date: Fri, 18 Nov 2022 18:24:27 +0700 Subject: [PATCH 2/7] Revert PULL_REQUEST_TEMPLATE.md changes --- .github/PULL_REQUEST_TEMPLATE.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 95b6143af..46cbfa90f 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,5 +1,5 @@ # Description -- [ Iss2062_records ] Source branch in your fork has meaningful name (not `main`) +- [ ] Source branch in your fork has meaningful name (not `main`) Fixes Issue: @@ -11,9 +11,9 @@ These items can be completed after PR is created. (Check any items that are not applicable (NA) for this PR) -- [X] JavaScript implementation -- [X] Python implementation (NA if HTML beautifier) -- [X] Added Tests to data file(s) -- [NA] Added command-line option(s) (NA if -- [NA] README.md documents new feature/option(s) +- [ ] JavaScript implementation +- [ ] Python implementation (NA if HTML beautifier) +- [ ] Added Tests to data file(s) +- [ ] Added command-line option(s) (NA if +- [ ] README.md documents new feature/option(s) From 7c050207859d2eea7ffc30ebb3bf62aa6b34586c Mon Sep 17 00:00:00 2001 From: Antonius Anggito Arissaputro Date: Fri, 25 Nov 2022 02:54:44 +0700 Subject: [PATCH 3/7] Change js and py jsbeautifier for record as START_BLOCK token --- js/src/javascript/beautifier.js | 5 +---- js/src/javascript/tokenizer.js | 14 ++++++++++++++ python/jsbeautifier/javascript/beautifier.py | 6 +----- python/jsbeautifier/javascript/tokenizer.py | 13 +++++++++++++ 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/js/src/javascript/beautifier.js b/js/src/javascript/beautifier.js index 333ec617f..dfec38351 100644 --- a/js/src/javascript/beautifier.js +++ b/js/src/javascript/beautifier.js @@ -720,9 +720,6 @@ Beautifier.prototype.handle_start_block = function(current_token) { } } - // Issue #2062 check to see if defining a new Record type - #{} - // Conditional on line 774 uses this variable - var is_record = this._flags.last_word === '#'; 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; @@ -771,7 +768,7 @@ Beautifier.prototype.handle_start_block = function(current_token) { 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 = !is_record; + this._output.space_before_token = true; } } } diff --git a/js/src/javascript/tokenizer.js b/js/src/javascript/tokenizer.js index 18d3cfc6a..80e74a88f 100644 --- a/js/src/javascript/tokenizer.js +++ b/js/src/javascript/tokenizer.js @@ -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(); diff --git a/python/jsbeautifier/javascript/beautifier.py b/python/jsbeautifier/javascript/beautifier.py index 9ab1343cf..69f2dbdaf 100644 --- a/python/jsbeautifier/javascript/beautifier.py +++ b/python/jsbeautifier/javascript/beautifier.py @@ -766,10 +766,6 @@ def handle_start_block(self, current_token): if reserved_array(self._flags.last_token.previous, ["class", "extends"]): self._flags.class_start_block = True - # Issue #2062 check to see if defining a new Record type - #{} - # Conditional on line 843 uses this variable - is_record = self._flags.last_token.text == "#" - empty_braces = ( (next_token is not None) and next_token.comments_before is None @@ -840,7 +836,7 @@ def handle_start_block(self, current_token): ): self.print_newline() else: - self._output.space_before_token = not is_record + self._output.space_before_token = True self.print_token(current_token) self.indent() diff --git a/python/jsbeautifier/javascript/tokenizer.py b/python/jsbeautifier/javascript/tokenizer.py index e53e910f8..fb5ae45ec 100644 --- a/python/jsbeautifier/javascript/tokenizer.py +++ b/python/jsbeautifier/javascript/tokenizer.py @@ -220,6 +220,7 @@ def _get_next_token(self, previous_token, open_token): 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 +258,18 @@ def _read_singles(self, c): 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() From 9f8042edc7503c2708c57862fbcca7f09d08cdc2 Mon Sep 17 00:00:00 2001 From: Antonius Anggito Arissaputro Date: Fri, 25 Nov 2022 04:18:45 +0700 Subject: [PATCH 4/7] Update jsbeautifier record data type tests --- test/data/javascript/tests.js | 47 ++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/test/data/javascript/tests.js b/test/data/javascript/tests.js index 411691696..8599a3231 100644 --- a/test/data/javascript/tests.js +++ b/test/data/javascript/tests.js @@ -5377,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. @@ -5395,7 +5441,6 @@ exports.test_data = { { fragment: true, unchanged: ' < div' }, { input: 'a = 1', output: 'a = 1' }, { input: 'a=1', output: 'a = 1' }, - { input: '#{ hello: "world" }', output: '#{\n hello: "world"\n}' }, { unchanged: '(3) / 2' }, { unchanged: '["a", "b"].join("")' }, { unchanged: 'a();\n\nb();' }, From 93927c78b757722aea9770a284064eaabd65c80a Mon Sep 17 00:00:00 2001 From: Antonius Anggito Arissaputro Date: Fri, 25 Nov 2022 14:11:37 +0700 Subject: [PATCH 5/7] Fix typo on record test --- test/data/javascript/tests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/data/javascript/tests.js b/test/data/javascript/tests.js index 8599a3231..25967eea8 100644 --- a/test/data/javascript/tests.js +++ b/test/data/javascript/tests.js @@ -5421,7 +5421,7 @@ exports.test_data = { ' d: true', '};' ] - }, + } ] }, { // ======================================================= From a7003210571d8fe0597a4f4267d87e08517607a0 Mon Sep 17 00:00:00 2001 From: Antonius Anggito Arissaputro Date: Fri, 25 Nov 2022 14:50:28 +0700 Subject: [PATCH 6/7] Minor style check --- js/src/javascript/tokenizer.js | 2 +- python/jsbeautifier/javascript/tokenizer.py | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/js/src/javascript/tokenizer.js b/js/src/javascript/tokenizer.js index 80e74a88f..ee35c571f 100644 --- a/js/src/javascript/tokenizer.js +++ b/js/src/javascript/tokenizer.js @@ -229,7 +229,7 @@ Tokenizer.prototype._read_singles = function(c) { Tokenizer.prototype._read_pair = function(c, d) { var token = null; if (c === '#' && d === '{') { - token = this._create_token(TOKEN.START_BLOCK, c+d); + token = this._create_token(TOKEN.START_BLOCK, c + d); } if (token) { diff --git a/python/jsbeautifier/javascript/tokenizer.py b/python/jsbeautifier/javascript/tokenizer.py index fb5ae45ec..a121a31ac 100644 --- a/python/jsbeautifier/javascript/tokenizer.py +++ b/python/jsbeautifier/javascript/tokenizer.py @@ -220,7 +220,9 @@ def _get_next_token(self, previous_token, open_token): 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_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) @@ -262,7 +264,7 @@ def _read_pair(self, c, d): token = None if c == "#" and d == "{": - token = self._create_token(TOKEN.START_BLOCK, c+d) + token = self._create_token(TOKEN.START_BLOCK, c + d) if token is not None: self._input.next() From 40020a4b73850b8c3e7dc4f31f03cf1fd79a3a05 Mon Sep 17 00:00:00 2001 From: Antonius Anggito Arissaputro Date: Sun, 27 Nov 2022 23:40:09 +0700 Subject: [PATCH 7/7] Bring back .DS_Store from main --- .DS_Store | Bin 0 -> 8196 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..9a98638419cc3072aab942ae3e81d7cf393056b6 GIT binary patch literal 8196 zcmeI1(QeZ)7=@3!Zj@00LtN|zDK9`J-XN4_tP*SzE7zlGyACJ~f>yA*-0vND1)he7 z;R?=gTUG3&7u+BweoyvEod4MIm$a@u0H(j5J^>B^beKi&-(k^^=J<5X0=T`<~qQ760fP((y6twO}Bb5-fH|3!#F#yClXFgy_Qa$9md&Ve9y+OP>k)J zzQl&Zq)xR|0!pAxK-TX2$hl(wf?0L_K3o)&d{N}QE+46<%)MVW@d((ZXN|X5U>Ze! zk0QHxhGY6q@EjRl;FQ|KhuCKxMZO30onk=GIrWI8oS(4fHIGJ(ZCpof-1D9hUSWtn zog=E;Nz)ztjNJPi-x5=D`O5f9ta#UpaI~!-k*6j5A7PAT_pQRNYxhWUwmT-n6??hB z71>WBpOCEjZjs)QBg!~}Gh|`3}iZS;~&#i*z?U^P}p;89<|VoTG-|j*43W(=({4nOZK-6`w-^l zZiV@6z5y9)2c(iUJ$;aN;Mil!V?Grb(<*FTSO>9f#*$Pmj*Nr2UB(ssTQpG(B6sZP zF%SC{azWIAi`ww(k-O}@$rGP*l(lzJ-q)Z7BHfx~s!foP*8kb|{r?*@BE3~5palMufN2lT27S8htlr?3Sw3NY!z@YSN~hMsBKRS| e^UXgDd7Lm*JgL{xDQlQ?Bfw(NLJ9m;0>1%>f*@A_ literal 0 HcmV?d00001