Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update js-beautify v1.15.1 to add new angular templating option #181

Merged
merged 6 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@typescript-eslint/parser": "^6.19.1",
"@vscode/web-custom-data": "^0.4.9",
"eslint": "^8.56.0",
"js-beautify": "^1.14.11",
"js-beautify": "^1.15.1",
"mocha": "^10.2.0",
"rimraf": "^5.0.5",
"typescript": "^5.3.3"
Expand Down
8 changes: 4 additions & 4 deletions src/beautify/beautify-css.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// copied from js-beautify/js/lib/beautify-css.js
// version: 1.14.11
// version: 1.15.1
/* AUTO-GENERATED. DO NOT MODIFY. */
/*

Expand Down Expand Up @@ -573,10 +573,10 @@ function Options(options, merge_child_field) {

this.indent_empty_lines = this._get_boolean('indent_empty_lines');

// valid templating languages ['django', 'erb', 'handlebars', 'php', 'smarty']
// For now, 'auto' = all off for javascript, all on for html (and inline javascript).
// valid templating languages ['django', 'erb', 'handlebars', 'php', 'smarty', 'angular']
// For now, 'auto' = all off for javascript, all except angular on for html (and inline javascript/css).
// other values ignored
this.templating = this._get_selection_list('templating', ['auto', 'none', 'django', 'erb', 'handlebars', 'php', 'smarty'], ['auto']);
this.templating = this._get_selection_list('templating', ['auto', 'none', 'angular', 'django', 'erb', 'handlebars', 'php', 'smarty'], ['auto']);
}

Options.prototype._get_array = function(name, default_value) {
Expand Down
4 changes: 2 additions & 2 deletions src/beautify/beautify-html.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ export interface IBeautifyHTMLOptions {
eol?: string;

/**
* List of templating languages (auto,none,django,erb,handlebars,php)
* List of templating languages (auto,none,angular,django,erb,handlebars,php,smarty)
* default ["auto"] = all in html
*/
templating?: ('auto' | 'none' | 'django' | 'erb' | 'handlebars' | 'php')[];
templating?: ('auto' | 'none' | 'angular' | 'django' | 'erb' | 'handlebars' | 'php' | 'smarty')[];

/**
* Keep text content together between this string
Expand Down
117 changes: 103 additions & 14 deletions src/beautify/beautify-html.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// copied from js-beautify/js/lib/beautify-html.js
// version: 1.14.11
// version: 1.15.1
/* AUTO-GENERATED. DO NOT MODIFY. */
/*

Expand Down Expand Up @@ -642,10 +642,10 @@ function Options(options, merge_child_field) {

this.indent_empty_lines = this._get_boolean('indent_empty_lines');

// valid templating languages ['django', 'erb', 'handlebars', 'php', 'smarty']
// For now, 'auto' = all off for javascript, all on for html (and inline javascript).
// valid templating languages ['django', 'erb', 'handlebars', 'php', 'smarty', 'angular']
// For now, 'auto' = all off for javascript, all except angular on for html (and inline javascript/css).
// other values ignored
this.templating = this._get_selection_list('templating', ['auto', 'none', 'django', 'erb', 'handlebars', 'php', 'smarty'], ['auto']);
this.templating = this._get_selection_list('templating', ['auto', 'none', 'angular', 'django', 'erb', 'handlebars', 'php', 'smarty'], ['auto']);
}

Options.prototype._get_array = function(name, default_value) {
Expand Down Expand Up @@ -1518,7 +1518,8 @@ var template_names = {
erb: false,
handlebars: false,
php: false,
smarty: false
smarty: false,
angular: false
};

// This lets templates appear anywhere we would do a readUntil
Expand Down Expand Up @@ -1861,6 +1862,13 @@ Printer.prototype.indent = function() {
this.indent_level++;
};

Printer.prototype.deindent = function() {
if (this.indent_level > 0) {
this.indent_level--;
this._output.set_indent(this.indent_level, this.alignment_size);
}
};

Printer.prototype.get_full_indent = function(level) {
level = this.indent_level + (level || 0);
if (level < 1) {
Expand Down Expand Up @@ -2055,6 +2063,10 @@ 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) {
parser_token = this._handle_control_flow_open(printer, raw_token);
} 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
printer.add_raw_token(raw_token);
Expand All @@ -2069,6 +2081,38 @@ Beautifier.prototype.beautify = function() {
return sweet_code;
};

Beautifier.prototype._handle_control_flow_open = function(printer, raw_token) {
var parser_token = {
text: raw_token.text,
type: raw_token.type
};
printer.set_space_before_token(raw_token.newlines || raw_token.whitespace_before !== '', true);
if (raw_token.newlines) {
printer.print_preserved_newlines(raw_token);
} else {
printer.set_space_before_token(raw_token.newlines || raw_token.whitespace_before !== '', true);
}
printer.print_token(raw_token);
printer.indent();
return parser_token;
};

Beautifier.prototype._handle_control_flow_close = function(printer, raw_token) {
var parser_token = {
text: raw_token.text,
type: raw_token.type
};

printer.deindent();
if (raw_token.newlines) {
printer.print_preserved_newlines(raw_token);
} else {
printer.set_space_before_token(raw_token.newlines || raw_token.whitespace_before !== '', true);
}
printer.print_token(raw_token);
return parser_token;
};

Beautifier.prototype._handle_tag_close = function(printer, raw_token, last_tag_token) {
var parser_token = {
text: raw_token.text,
Expand Down Expand Up @@ -2768,6 +2812,8 @@ var Pattern = (__webpack_require__(12).Pattern);
var TOKEN = {
TAG_OPEN: 'TK_TAG_OPEN',
TAG_CLOSE: 'TK_TAG_CLOSE',
CONTROL_FLOW_OPEN: 'TK_CONTROL_FLOW_OPEN',
CONTROL_FLOW_CLOSE: 'TK_CONTROL_FLOW_CLOSE',
ATTRIBUTE: 'TK_ATTRIBUTE',
EQUALS: 'TK_EQUALS',
VALUE: 'TK_VALUE',
Expand All @@ -2792,11 +2838,13 @@ var Tokenizer = function(input_string, options) {

this.__patterns = {
word: templatable_reader.until(/[\n\r\t <]/),
word_control_flow_close_excluded: templatable_reader.until(/[\n\r\t <}]/),
single_quote: templatable_reader.until_after(/'/),
double_quote: templatable_reader.until_after(/"/),
attribute: templatable_reader.until(/[\n\r\t =>]|\/>/),
element_name: templatable_reader.until(/[\n\r\t >\/]/),

angular_control_flow_start: pattern_reader.matching(/\@[a-zA-Z]+[^({]*[({]/),
handlebars_comment: pattern_reader.starting_with(/{{!--/).until_after(/--}}/),
handlebars: pattern_reader.starting_with(/{{/).until_after(/}}/),
handlebars_open: pattern_reader.until(/[\n\r\t }]/),
Expand All @@ -2810,6 +2858,7 @@ var Tokenizer = function(input_string, options) {

if (this._options.indent_handlebars) {
this.__patterns.word = this.__patterns.word.exclude('handlebars');
this.__patterns.word_control_flow_close_excluded = this.__patterns.word_control_flow_close_excluded.exclude('handlebars');
}

this._unformatted_content_delimiter = null;
Expand All @@ -2828,14 +2877,16 @@ Tokenizer.prototype._is_comment = function(current_token) { // jshint unused:fal
};

Tokenizer.prototype._is_opening = function(current_token) {
return current_token.type === TOKEN.TAG_OPEN;
return current_token.type === TOKEN.TAG_OPEN || current_token.type === TOKEN.CONTROL_FLOW_OPEN;
};

Tokenizer.prototype._is_closing = function(current_token, open_token) {
return current_token.type === TOKEN.TAG_CLOSE &&
return (current_token.type === TOKEN.TAG_CLOSE &&
(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.text === '}}' && open_token.text[0] === '{' && open_token.text[1] === '{')))
) || (current_token.type === TOKEN.CONTROL_FLOW_CLOSE &&
(current_token.text === '}' && open_token.text.endsWith('{')));
};

Tokenizer.prototype._reset = function() {
Expand All @@ -2854,8 +2905,9 @@ Tokenizer.prototype._get_next_token = function(previous_token, open_token) { //
token = token || this._read_open_handlebars(c, open_token);
token = token || this._read_attribute(c, previous_token, open_token);
token = token || this._read_close(c, open_token);
token = token || this._read_control_flows(c, open_token);
token = token || this._read_raw_content(c, previous_token, open_token);
token = token || this._read_content_word(c);
token = token || this._read_content_word(c, open_token);
token = token || this._read_comment_or_cdata(c);
token = token || this._read_processing(c);
token = token || this._read_open(c, open_token);
Expand Down Expand Up @@ -2920,7 +2972,7 @@ Tokenizer.prototype._read_processing = function(c) { // jshint unused:false
Tokenizer.prototype._read_open = function(c, open_token) {
var resulting_string = null;
var token = null;
if (!open_token) {
if (!open_token || open_token.type === TOKEN.CONTROL_FLOW_OPEN) {
if (c === '<') {

resulting_string = this._input.next();
Expand All @@ -2937,7 +2989,7 @@ Tokenizer.prototype._read_open = function(c, open_token) {
Tokenizer.prototype._read_open_handlebars = function(c, open_token) {
var resulting_string = null;
var token = null;
if (!open_token) {
if (!open_token || open_token.type === TOKEN.CONTROL_FLOW_OPEN) {
if (this._options.indent_handlebars && c === '{' && this._input.peek(1) === '{') {
if (this._input.peek(2) === '!') {
resulting_string = this.__patterns.handlebars_comment.read();
Expand All @@ -2952,11 +3004,48 @@ Tokenizer.prototype._read_open_handlebars = function(c, open_token) {
return 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 AND indenting is set
if (!this._options.templating.includes('angular') || !this._options.indent_handlebars) {
return token;
}

if (c === '@') {
resulting_string = this.__patterns.angular_control_flow_start.read();
if (resulting_string === '') {
return token;
}

var opening_parentheses_count = resulting_string.endsWith('(') ? 1 : 0;
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)) {
var next_char = this._input.next();
if (next_char === null) {
break;
} else if (next_char === '(') {
opening_parentheses_count++;
} else if (next_char === ')') {
closing_parentheses_count++;
}
resulting_string += next_char;
}
token = this._create_token(TOKEN.CONTROL_FLOW_OPEN, resulting_string);
} else if (c === '}' && open_token && open_token.type === TOKEN.CONTROL_FLOW_OPEN) {
resulting_string = this._input.next();
token = this._create_token(TOKEN.CONTROL_FLOW_CLOSE, resulting_string);
}
return token;
};


Tokenizer.prototype._read_close = function(c, open_token) {
var resulting_string = null;
var token = null;
if (open_token) {
if (open_token && open_token.type === TOKEN.TAG_OPEN) {
if (open_token.text[0] === '<' && (c === '>' || (c === '/' && this._input.peek(1) === '>'))) {
resulting_string = this._input.next();
if (c === '/') { // for close tag "/>"
Expand Down Expand Up @@ -3043,7 +3132,7 @@ Tokenizer.prototype._read_raw_content = function(c, previous_token, open_token)
return null;
};

Tokenizer.prototype._read_content_word = function(c) {
Tokenizer.prototype._read_content_word = function(c, open_token) {
var resulting_string = '';
if (this._options.unformatted_content_delimiter) {
if (c === this._options.unformatted_content_delimiter[0]) {
Expand All @@ -3052,7 +3141,7 @@ Tokenizer.prototype._read_content_word = function(c) {
}

if (!resulting_string) {
resulting_string = this.__patterns.word.read();
resulting_string = (open_token && open_token.type === TOKEN.CONTROL_FLOW_OPEN) ? this.__patterns.word_control_flow_close_excluded.read() : this.__patterns.word.read();
}
if (resulting_string) {
return this._create_token(TOKEN.TEXT, resulting_string);
Expand Down
Loading
Loading