From 1ffd900e0d0ba9631c11824ec66750a0e11d07b9 Mon Sep 17 00:00:00 2001 From: Gergely Gyorgy Both Date: Wed, 15 Nov 2023 13:45:22 +0100 Subject: [PATCH] Add tests for fixing issue #2219 --- index.html | 3 +- js/src/html/beautifier.js | 10 +- js/src/html/tokenizer.js | 14 +- test/data/html/tests.js | 298 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 312 insertions(+), 13 deletions(-) diff --git a/index.html b/index.html index 2d7704101..ecfeb872c 100644 --- a/index.html +++ b/index.html @@ -39,7 +39,8 @@ - + + diff --git a/js/src/html/beautifier.js b/js/src/html/beautifier.js index f70451cca..32c882a57 100644 --- a/js/src/html/beautifier.js +++ b/js/src/html/beautifier.js @@ -112,7 +112,7 @@ Printer.prototype.indent = function() { }; Printer.prototype.deindent = function() { - if (this.indent_level > 0 ) { + if (this.indent_level > 0) { this.indent_level--; this._output.set_indent(this.indent_level, this.alignment_size); } @@ -312,9 +312,9 @@ Beautifier.prototype.beautify = function() { parser_token = this._handle_tag_close(printer, raw_token, last_tag_token); } else if (raw_token.type === TOKEN.TEXT) { parser_token = this._handle_text(printer, raw_token, last_tag_token); - } else if(raw_token.type === TOKEN.CONTROL_FLOW_OPEN) { + } else if (raw_token.type === TOKEN.CONTROL_FLOW_OPEN) { parser_token = this._handle_control_flow_open(printer, raw_token); - } else if(raw_token.type === TOKEN.CONTROL_FLOW_CLOSE) { + } else if (raw_token.type === TOKEN.CONTROL_FLOW_CLOSE) { parser_token = this._handle_control_flow_close(printer, raw_token); } else { // This should never happen, but if it does. Print the raw token @@ -336,7 +336,7 @@ Beautifier.prototype._handle_control_flow_open = function(printer, raw_token) { type: raw_token.type }; printer.set_space_before_token(raw_token.newlines || raw_token.whitespace_before !== '', true); - if(raw_token.newlines) { + if (raw_token.newlines) { printer.print_preserved_newlines(raw_token); } else { printer.set_space_before_token(raw_token.newlines || raw_token.whitespace_before !== '', true); @@ -353,7 +353,7 @@ Beautifier.prototype._handle_control_flow_close = function(printer, raw_token) { }; printer.deindent(); - if(raw_token.newlines) { + if (raw_token.newlines) { printer.print_preserved_newlines(raw_token); } else { printer.set_space_before_token(raw_token.newlines || raw_token.whitespace_before !== '', true); diff --git a/js/src/html/tokenizer.js b/js/src/html/tokenizer.js index 2f3e223bd..7d5e01d3f 100644 --- a/js/src/html/tokenizer.js +++ b/js/src/html/tokenizer.js @@ -107,7 +107,7 @@ Tokenizer.prototype._is_closing = function(current_token, open_token) { (open_token && ( ((current_token.text === '>' || current_token.text === '/>') && open_token.text[0] === '<') || (current_token.text === '}}' && open_token.text[0] === '{' && open_token.text[1] === '{'))) - ) || (current_token.type === TOKEN.CONTROL_FLOW_CLOSE && + ) || (current_token.type === TOKEN.CONTROL_FLOW_CLOSE && (current_token.text === '}' && open_token.text.endsWith('{'))); }; @@ -226,11 +226,11 @@ Tokenizer.prototype._read_open_handlebars = function(c, open_token) { return token; }; -Tokenizer.prototype._read_control_flows = function (c, open_token) { +Tokenizer.prototype._read_control_flows = function(c, open_token) { var resulting_string = ''; var token = null; // Only check for control flows if angular templating is set - if(!this._options.templating.includes('angular')) { + if (!this._options.templating.includes('angular')) { return token; } @@ -239,13 +239,13 @@ Tokenizer.prototype._read_control_flows = function (c, open_token) { var closing_parentheses_count = 0; // The opening brace of the control flow is where the number of opening and closing parentheses equal // e.g. @if({value: true} !== null) { - while(!(resulting_string.endsWith('{') && opening_parentheses_count === closing_parentheses_count)) { + while (!(resulting_string.endsWith('{') && opening_parentheses_count === closing_parentheses_count)) { var next_char = this._input.next(); - if(next_char === null) { + if (next_char === null) { break; - } else if(next_char === '(') { + } else if (next_char === '(') { opening_parentheses_count++; - } else if(next_char === ')') { + } else if (next_char === ')') { closing_parentheses_count++; } resulting_string += next_char; diff --git a/test/data/html/tests.js b/test/data/html/tests.js index 6d37e432e..e79b2a4bc 100644 --- a/test/data/html/tests.js +++ b/test/data/html/tests.js @@ -3839,6 +3839,304 @@ exports.test_data = { '' ] }] + }, { + name: "Indenting angular control flow with indent size 2", + description: "https://github.com/beautify-web/js-beautify/issues/2219", + template: "^^^ $$$", + options: [ + { name: "templating", value: "'angular'" }, + { name: "indent_size", value: "2" } + ], + tests: [{ + input: [ + '@if (a > b) {', + '{{a}} is greater than {{b}}', + '}', + '', + '@if (a > b) {', + '{{a}} is greater than {{b}}', + '} @else if (b > a) {', + '{{a}} is less than {{b}}', + '} @else {', + '{{a}} is equal to {{b}}', + '}', + '', + '@for (item of items; track item.name) {', + '
  • {{ item.name }}
  • ', + '} @empty {', + '
  • There are no items.
  • ', + '}', + '', + '@switch (condition) {', + '@case (caseA) { ', + 'Case A.', + '}', + '@case (caseB) {', + 'Case B.', + '}', + '@default {', + 'Default case.', + '}', + '}' + ], + output: [ + '@if (a > b) {', + ' {{a}} is greater than {{b}}', + '}', + '', + '@if (a > b) {', + ' {{a}} is greater than {{b}}', + '} @else if (b > a) {', + ' {{a}} is less than {{b}}', + '} @else {', + ' {{a}} is equal to {{b}}', + '}', + '', + '@for (item of items; track item.name) {', + '
  • {{ item.name }}
  • ', + '} @empty {', + '
  • There are no items.
  • ', + '}', + '', + '@switch (condition) {', + ' @case (caseA) {', + ' Case A.', + ' }', + ' @case (caseB) {', + ' Case B.', + ' }', + ' @default {', + ' Default case.', + ' }', + '}' + ] + }] + }, { + name: "Indenting angular control flow with default indent size", + description: "https://github.com/beautify-web/js-beautify/issues/2219", + template: "^^^ $$$", + options: [ + { name: "templating", value: "'angular, handlebars'" } + ], + tests: [{ + input: [ + '@if (a > b) {', + '{{a}} is greater than {{b}}', + '}', + '', + '@if (a > b) {', + '{{a}} is greater than {{b}}', + '} @else if (b > a) {', + '{{a}} is less than {{b}}', + '} @else {', + '{{a}} is equal to {{b}}', + '}', + '', + '@for (item of items; track item.name) {', + '
  • {{ item.name }}
  • ', + '} @empty {', + '
  • There are no items.
  • ', + '}', + '', + '@switch (condition) {', + '@case (caseA) { ', + 'Case A.', + '}', + '@case (caseB) {', + 'Case B.', + '}', + '@default {', + 'Default case.', + '}', + '}' + ], + output: [ + '@if (a > b) {', + ' {{a}} is greater than {{b}}', + '}', + '', + '@if (a > b) {', + ' {{a}} is greater than {{b}}', + '} @else if (b > a) {', + ' {{a}} is less than {{b}}', + '} @else {', + ' {{a}} is equal to {{b}}', + '}', + '', + '@for (item of items; track item.name) {', + '
  • {{ item.name }}
  • ', + '} @empty {', + '
  • There are no items.
  • ', + '}', + '', + '@switch (condition) {', + ' @case (caseA) {', + ' Case A.', + ' }', + ' @case (caseB) {', + ' Case B.', + ' }', + ' @default {', + ' Default case.', + ' }', + '}' + ] + }, { + input: [ + '@if (a > b) {', + ' {{a}} is greater than {{b}}', + ' }', + '', + ' @if (a > b) {', + ' {{a}} is greater than {{b}}', + ' } @else if (b > a) {', + ' {{a}} is less than {{b}}', + ' } @else {', + ' {{a}} is equal to {{b}}', + '}', + '', + ' @for (item of items; track item.name) {', + '
  • {{ item.name }}
  • ', + ' } @empty {', + '
  • There are no items.
  • ', + ' }', + '', + ' @switch (condition) {', + '@case (caseA) { ', + 'Case A.', + ' }', + ' @case (caseB) {', + 'Case B.', + ' }', + ' @default {', + 'Default case.', + '}', + ' }' + ], + output: [ + '@if (a > b) {', + ' {{a}} is greater than {{b}}', + '}', + '', + '@if (a > b) {', + ' {{a}} is greater than {{b}}', + '} @else if (b > a) {', + ' {{a}} is less than {{b}}', + '} @else {', + ' {{a}} is equal to {{b}}', + '}', + '', + '@for (item of items; track item.name) {', + '
  • {{ item.name }}
  • ', + '} @empty {', + '
  • There are no items.
  • ', + '}', + '', + '@switch (condition) {', + ' @case (caseA) {', + ' Case A.', + ' }', + ' @case (caseB) {', + ' Case B.', + ' }', + ' @default {', + ' Default case.', + ' }', + '}' + ] + }, { + input: [ + '@if( {value: true}; as val) {', + '
    {{val.value}}
    ', + '}' + ], + output: [ + '@if( {value: true}; as val) {', + '
    {{val.value}}
    ', + '}' + ] + }, { + input: [ + '@if( {value: true}; as val) {', + '
    ', + '@defer {', + '{{val.value}}', + '}', + '
    ', + '}' + ], + output: [ + '@if( {value: true}; as val) {', + '
    ', + ' @defer {', + ' {{val.value}}', + ' }', + '
    ', + '}' + ] + }, { + unchanged: [ + '
    @if(true) { {{"{}" + " }"}} }
    ' + ] + }, { + input: [ + '
    ', + '@for (item of items; track item.id; let idx = $index, e = $even) {', + 'Item #{{ idx }}: {{ item.name }}', + '

    ', + 'Item #{{ idx }}: {{ item.name }}', + '

    ', + '}', + '
    ' + ], + output: [ + '
    ', + ' @for (item of items; track item.id; let idx = $index, e = $even) {', + ' Item #{{ idx }}: {{ item.name }}', + '

    ', + ' Item #{{ idx }}: {{ item.name }}', + '

    ', + ' }', + '
    ' + ] + }, { + input: [ + '
    ', + '@for (item of items; track item.id; let idx = $index, e = $even) {', + '{{{value: true} | json}}', + '

    ', + 'Item #{{ idx }}: {{ item.name }}', + '

    ', + '{{ {value: true} }}', + '
    ', + '@if(true) {', + '{{ {value: true} }}', + ' }', + '', + 'Placeholder', + '
    ', + '}', + '
    ' + ], + output: [ + '
    ', + ' @for (item of items; track item.id; let idx = $index, e = $even) {', + ' {{{value: true} | json}}', + '

    ', + ' Item #{{ idx }}: {{ item.name }}', + '

    ', + ' {{ {value: true} }}', + '
    ', + ' @if(true) {', + ' {{ {value: true} }}', + ' }', + '', + ' Placeholder', + '
    ', + ' }', + '
    ' + ] + }] }, { name: "New Test Suite" }]