diff --git a/js/index.js b/js/index.js index fa4c067d1..7c31cf42c 100644 --- a/js/index.js +++ b/js/index.js @@ -16,18 +16,18 @@ All methods returned accept two arguments, the source string and an options obje function get_beautify(js_beautify, css_beautify, html_beautify) { // the default is js - var beautify = function (src, config) { + var beautify = function(src, config) { return js_beautify.js_beautify(src, config); }; // short aliases - beautify.js = js_beautify.js_beautify; - beautify.css = css_beautify.css_beautify; + beautify.js = js_beautify.js_beautify; + beautify.css = css_beautify.css_beautify; beautify.html = html_beautify.html_beautify; // legacy aliases - beautify.js_beautify = js_beautify.js_beautify; - beautify.css_beautify = css_beautify.css_beautify; + beautify.js_beautify = js_beautify.js_beautify; + beautify.css_beautify = css_beautify.css_beautify; beautify.html_beautify = html_beautify.html_beautify; return beautify; @@ -51,5 +51,4 @@ if (typeof define === "function" && define.amd) { mod.exports = get_beautify(js_beautify, css_beautify, html_beautify); })(module); -} - +} \ No newline at end of file diff --git a/js/lib/beautify-css.js b/js/lib/beautify-css.js index 2dd117c70..3bc19c5f3 100644 --- a/js/lib/beautify-css.js +++ b/js/lib/beautify-css.js @@ -65,7 +65,7 @@ options = options || {}; source_text = source_text || ''; // HACK: newline parsing inconsistent. This brute force normalizes the input. - source_text = source_text.replace(/\r\n|[\r\u2028\u2029]/g, '\n') + source_text = source_text.replace(/\r\n|[\r\u2028\u2029]/g, '\n'); var indentSize = options.indent_size || 4; var indentCharacter = options.indent_char || ' '; @@ -79,12 +79,12 @@ indentSize = parseInt(indentSize, 10); } - if(options.indent_with_tabs){ + if (options.indent_with_tabs) { indentCharacter = '\t'; indentSize = 1; } - eol = eol.replace(/\\r/, '\r').replace(/\\n/, '\n') + eol = eol.replace(/\\r/, '\r').replace(/\\n/, '\n'); // tokenizer @@ -189,7 +189,7 @@ // pseudoclasses can contain () openParen += 1; } else if (ch === ')') { - if (openParen == 0) { + if (openParen === 0) { return false; } openParen -= 1; @@ -295,7 +295,7 @@ print.newLine(true); } } else if (ch === '/' && peek() === '/') { // single line comment - if (!isAfterNewline && last_top_ch !== '{' ) { + if (!isAfterNewline && last_top_ch !== '{') { print.trim(); } print.singleSpace(); @@ -316,7 +316,7 @@ print.singleSpace(); } - variableOrRule = variableOrRule.replace(/\s$/, '') + variableOrRule = variableOrRule.replace(/\s$/, ''); // might be a nesting at-rule if (variableOrRule in css_beautify.NESTED_AT_RULE) { @@ -326,8 +326,8 @@ } } } else if (ch === '#' && peek() === '{') { - print.preserveSingleSpace(); - output.push(eatString('}')); + print.preserveSingleSpace(); + output.push(eatString('}')); } else if (ch === '{') { if (peek(true) === '}') { eatWhitespace(); @@ -423,7 +423,7 @@ print.preserveSingleSpace(); output.push(ch); } else if (ch === '=') { // no whitespace before or after - eatWhitespace() + eatWhitespace(); ch = '='; output.push(ch); } else { @@ -445,7 +445,7 @@ sweetCode += '\n'; } - if (eol != '\n') { + if (eol !== '\n') { sweetCode = sweetCode.replace(/[\n]/g, eol); } @@ -488,4 +488,4 @@ global.css_beautify = css_beautify; } -}()); +}()); \ No newline at end of file diff --git a/js/lib/beautify-html.js b/js/lib/beautify-html.js index 97b4ad6ca..b49f06d4e 100644 --- a/js/lib/beautify-html.js +++ b/js/lib/beautify-html.js @@ -81,7 +81,7 @@ } function rtrim(s) { - return s.replace(/\s+$/g,''); + return s.replace(/\s+$/g, ''); } function style_html(html_source, options, js_beautify, css_beautify) { @@ -107,7 +107,7 @@ // backwards compatibility to 1.3.4 if ((options.wrap_line_length === undefined || parseInt(options.wrap_line_length, 10) === 0) && - (options.max_char !== undefined && parseInt(options.max_char, 10) !== 0)) { + (options.max_char !== undefined && parseInt(options.max_char, 10) !== 0)) { options.wrap_line_length = options.max_char; } @@ -115,7 +115,7 @@ indent_size = (options.indent_size === undefined) ? 4 : parseInt(options.indent_size, 10); indent_character = (options.indent_char === undefined) ? ' ' : options.indent_char; brace_style = (options.brace_style === undefined) ? 'collapse' : options.brace_style; - wrap_line_length = parseInt(options.wrap_line_length, 10) === 0 ? 32786 : parseInt(options.wrap_line_length || 250, 10); + wrap_line_length = parseInt(options.wrap_line_length, 10) === 0 ? 32786 : parseInt(options.wrap_line_length || 250, 10); unformatted = options.unformatted || [ // https://www.w3.org/TR/html5/dom.html#phrasing-content 'a', 'abbr', 'area', 'audio', 'b', 'bdi', 'bdo', 'br', 'button', 'canvas', 'cite', @@ -131,23 +131,23 @@ ]; preserve_newlines = (options.preserve_newlines === undefined) ? true : options.preserve_newlines; max_preserve_newlines = preserve_newlines ? - (isNaN(parseInt(options.max_preserve_newlines, 10)) ? 32786 : parseInt(options.max_preserve_newlines, 10)) - : 0; + (isNaN(parseInt(options.max_preserve_newlines, 10)) ? 32786 : parseInt(options.max_preserve_newlines, 10)) : + 0; indent_handlebars = (options.indent_handlebars === undefined) ? false : options.indent_handlebars; wrap_attributes = (options.wrap_attributes === undefined) ? 'auto' : options.wrap_attributes; wrap_attributes_indent_size = (isNaN(parseInt(options.wrap_attributes_indent_size, 10))) ? indent_size : parseInt(options.wrap_attributes_indent_size, 10); end_with_newline = (options.end_with_newline === undefined) ? false : options.end_with_newline; - extra_liners = (typeof options.extra_liners == 'object') && options.extra_liners ? + extra_liners = (typeof options.extra_liners === 'object') && options.extra_liners ? options.extra_liners.concat() : (typeof options.extra_liners === 'string') ? options.extra_liners.split(',') : 'head,body,/html'.split(','); eol = options.eol ? options.eol : '\n'; - if(options.indent_with_tabs){ + if (options.indent_with_tabs) { indent_character = '\t'; indent_size = 1; } - eol = eol.replace(/\\r/, '\r').replace(/\\n/, '\n') + eol = eol.replace(/\\r/, '\r').replace(/\\n/, '\n'); function Parser() { @@ -395,7 +395,7 @@ var wrapped = this.space_or_wrap(content); var indentAttrs = wrapped && input_char !== '/' && wrap_attributes !== 'force'; space = false; - if (!first_attr && wrap_attributes === 'force' && input_char !== '/') { + if (!first_attr && wrap_attributes === 'force' && input_char !== '/') { this.print_newline(false, content); this.print_indentation(content); indentAttrs = true; @@ -407,10 +407,10 @@ } } for (var i = 0; i < content.length; i++) { - if (content[i] === ' ') { - first_attr = false; - break; - } + if (content[i] === ' ') { + first_attr = false; + break; + } } } @@ -500,15 +500,15 @@ this.tag_type = 'SINGLE'; } else if (tag_check === 'script' && (tag_complete.search('type') === -1 || - (tag_complete.search('type') > -1 && - tag_complete.search(/\b(text|application)\/(x-)?(javascript|ecmascript|jscript|livescript|(ld\+)?json)/) > -1))) { + (tag_complete.search('type') > -1 && + tag_complete.search(/\b(text|application)\/(x-)?(javascript|ecmascript|jscript|livescript|(ld\+)?json)/) > -1))) { if (!peek) { this.record_tag(tag_check); this.tag_type = 'SCRIPT'; } } else if (tag_check === 'style' && (tag_complete.search('type') === -1 || - (tag_complete.search('type') > -1 && tag_complete.search('text/css') > -1))) { + (tag_complete.search('type') > -1 && tag_complete.search('text/css') > -1))) { if (!peek) { this.record_tag(tag_check); this.tag_type = 'STYLE'; @@ -605,21 +605,21 @@ }; function tokenMatcher(delimiter) { - var token = ''; + var token = ''; - var add = function (str) { - var newToken = token + str.toLowerCase(); - token = newToken.length <= delimiter.length ? newToken : newToken.substr(newToken.length - delimiter.length, delimiter.length); - }; + var add = function(str) { + var newToken = token + str.toLowerCase(); + token = newToken.length <= delimiter.length ? newToken : newToken.substr(newToken.length - delimiter.length, delimiter.length); + }; - var doesNotMatch = function () { - return token.indexOf(delimiter) === -1; - }; + var doesNotMatch = function() { + return token.indexOf(delimiter) === -1; + }; - return { - add: add, - doesNotMatch: doesNotMatch - }; + return { + add: add, + doesNotMatch: doesNotMatch + }; } this.get_unformatted = function(delimiter, orig_tag) { //function to return unformatted content in its entirety @@ -745,7 +745,7 @@ this.input = js_source || ''; //gets the input for the Parser // HACK: newline parsing inconsistent. This brute force normalizes the input. - this.input = this.input.replace(/\r\n|[\r\u2028\u2029]/g, '\n') + this.input = this.input.replace(/\r\n|[\r\u2028\u2029]/g, '\n'); this.output = []; this.indent_character = indent_character; @@ -888,9 +888,9 @@ case 'TK_TAG_HANDLEBARS_ELSE': // Don't add a newline if opening {{#if}} tag is on the current line var foundIfOnCurrentLine = false; - for (var lastCheckedOutput=multi_parser.output.length-1; lastCheckedOutput>=0; lastCheckedOutput--) { - if (multi_parser.output[lastCheckedOutput] === '\n') { - break; + for (var lastCheckedOutput = multi_parser.output.length - 1; lastCheckedOutput >= 0; lastCheckedOutput--) { + if (multi_parser.output[lastCheckedOutput] === '\n') { + break; } else { if (multi_parser.output[lastCheckedOutput].match(/{{#if/)) { foundIfOnCurrentLine = true; @@ -979,7 +979,7 @@ sweet_code += '\n'; } - if (eol != '\n') { + if (eol !== '\n') { sweet_code = sweet_code.replace(/[\n]/g, eol); } @@ -989,13 +989,13 @@ if (typeof define === "function" && define.amd) { // Add support for AMD ( https://github.com/amdjs/amdjs-api/wiki/AMD#defineamd-property- ) define(["require", "./beautify", "./beautify-css"], function(requireamd) { - var js_beautify = requireamd("./beautify"); - var css_beautify = requireamd("./beautify-css"); + var js_beautify = requireamd("./beautify"); + var css_beautify = requireamd("./beautify-css"); return { - html_beautify: function(html_source, options) { - return style_html(html_source, options, js_beautify.js_beautify, css_beautify.css_beautify); - } + html_beautify: function(html_source, options) { + return style_html(html_source, options, js_beautify.js_beautify, css_beautify.css_beautify); + } }; }); } else if (typeof exports !== "undefined") { @@ -1019,4 +1019,4 @@ }; } -}()); +}()); \ No newline at end of file diff --git a/js/lib/beautify.js b/js/lib/beautify.js index 0edb2dee5..aed90a0b6 100644 --- a/js/lib/beautify.js +++ b/js/lib/beautify.js @@ -89,13 +89,14 @@ if (!Object.values) { Object.values = function(o) { if (o !== Object(o)) { - throw new TypeError('Object.values called on a non-object'); + throw new TypeError('Object.values called on a non-object'); } - var k=[],p; + var k = [], + p; for (p in o) { - if (Object.prototype.hasOwnProperty.call(o,p)) { - k.push(o[p]); - } + if (Object.prototype.hasOwnProperty.call(o, p)) { + k.push(o[p]); + } } return k; }; @@ -104,70 +105,72 @@ if (!Object.values) { (function() { var acorn = {}; - (function (exports) { - // This section of code is taken from acorn. - // - // Acorn was written by Marijn Haverbeke and released under an MIT - // license. The Unicode regexps (for identifiers and whitespace) were - // taken from [Esprima](http://esprima.org) by Ariya Hidayat. - // - // Git repositories for Acorn are available at - // - // http://marijnhaverbeke.nl/git/acorn - // https://github.com/marijnh/acorn.git - - // ## Character categories - - // Big ugly regular expressions that match characters in the - // whitespace, identifier, and identifier-start categories. These - // are only applied when a character is found to actually have a - // code point above 128. - - var nonASCIIwhitespace = /[\u1680\u180e\u2000-\u200a\u202f\u205f\u3000\ufeff]/; - var nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u0527\u0531-\u0556\u0559\u0561-\u0587\u05d0-\u05ea\u05f0-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u08a0\u08a2-\u08ac\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0977\u0979-\u097f\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c33\u0c35-\u0c39\u0c3d\u0c58\u0c59\u0c60\u0c61\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d60\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e87\u0e88\u0e8a\u0e8d\u0e94-\u0e97\u0e99-\u0e9f\u0ea1-\u0ea3\u0ea5\u0ea7\u0eaa\u0eab\u0ead-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f4\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f0\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1877\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191c\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19c1-\u19c7\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1ce9-\u1cec\u1cee-\u1cf1\u1cf5\u1cf6\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u212f-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u2e2f\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309d-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312d\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fcc\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua697\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua78e\ua790-\ua793\ua7a0-\ua7aa\ua7f8-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa80-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uabc0-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc"; - var nonASCIIidentifierChars = "\u0300-\u036f\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u0620-\u0649\u0672-\u06d3\u06e7-\u06e8\u06fb-\u06fc\u0730-\u074a\u0800-\u0814\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0840-\u0857\u08e4-\u08fe\u0900-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962-\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09d7\u09df-\u09e0\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2-\u0ae3\u0ae6-\u0aef\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b56\u0b57\u0b5f-\u0b60\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c01-\u0c03\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62-\u0c63\u0c66-\u0c6f\u0c82\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2-\u0ce3\u0ce6-\u0cef\u0d02\u0d03\u0d46-\u0d48\u0d57\u0d62-\u0d63\u0d66-\u0d6f\u0d82\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0df2\u0df3\u0e34-\u0e3a\u0e40-\u0e45\u0e50-\u0e59\u0eb4-\u0eb9\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f41-\u0f47\u0f71-\u0f84\u0f86-\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u1000-\u1029\u1040-\u1049\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u170e-\u1710\u1720-\u1730\u1740-\u1750\u1772\u1773\u1780-\u17b2\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u1920-\u192b\u1930-\u193b\u1951-\u196d\u19b0-\u19c0\u19c8-\u19c9\u19d0-\u19d9\u1a00-\u1a15\u1a20-\u1a53\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1b46-\u1b4b\u1b50-\u1b59\u1b6b-\u1b73\u1bb0-\u1bb9\u1be6-\u1bf3\u1c00-\u1c22\u1c40-\u1c49\u1c5b-\u1c7d\u1cd0-\u1cd2\u1d00-\u1dbe\u1e01-\u1f15\u200c\u200d\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2d81-\u2d96\u2de0-\u2dff\u3021-\u3028\u3099\u309a\ua640-\ua66d\ua674-\ua67d\ua69f\ua6f0-\ua6f1\ua7f8-\ua800\ua806\ua80b\ua823-\ua827\ua880-\ua881\ua8b4-\ua8c4\ua8d0-\ua8d9\ua8f3-\ua8f7\ua900-\ua909\ua926-\ua92d\ua930-\ua945\ua980-\ua983\ua9b3-\ua9c0\uaa00-\uaa27\uaa40-\uaa41\uaa4c-\uaa4d\uaa50-\uaa59\uaa7b\uaae0-\uaae9\uaaf2-\uaaf3\uabc0-\uabe1\uabec\uabed\uabf0-\uabf9\ufb20-\ufb28\ufe00-\ufe0f\ufe20-\ufe26\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f"; - var nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]"); - var nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]"); - - // Whether a single character denotes a newline. - - var newline = exports.newline = /[\n\r\u2028\u2029]/; - - // Matches a whole line break (where CRLF is considered a single - // line break). Used to count lines. - - // in javascript, these two differ - // in python they are the same, different methods are called on them - var lineBreak = exports.lineBreak = /\r\n|[\n\r\u2028\u2029]/; - var allLineBreaks = exports.allLineBreaks = new RegExp(lineBreak.source, 'g'); - - - // Test whether a given character code starts an identifier. - - var isIdentifierStart = exports.isIdentifierStart = function(code) { - // permit $ (36) and @ (64). @ is used in ES7 decorators. - if (code < 65) return code === 36 || code === 64; - // 65 through 91 are uppercase letters. - if (code < 91) return true; - // permit _ (95). - if (code < 97) return code === 95; - // 97 through 123 are lowercase letters. - if (code < 123)return true; - return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code)); - }; - - // Test whether a given character is part of an identifier. - - var isIdentifierChar = exports.isIdentifierChar = function(code) { - if (code < 48) return code === 36; - if (code < 58) return true; - if (code < 65) return false; - if (code < 91) return true; - if (code < 97) return code === 95; - if (code < 123)return true; - return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code)); - }; + (function(exports) { + /* jshint curly: false */ + // This section of code is taken from acorn. + // + // Acorn was written by Marijn Haverbeke and released under an MIT + // license. The Unicode regexps (for identifiers and whitespace) were + // taken from [Esprima](http://esprima.org) by Ariya Hidayat. + // + // Git repositories for Acorn are available at + // + // http://marijnhaverbeke.nl/git/acorn + // https://github.com/marijnh/acorn.git + + // ## Character categories + + // Big ugly regular expressions that match characters in the + // whitespace, identifier, and identifier-start categories. These + // are only applied when a character is found to actually have a + // code point above 128. + + var nonASCIIwhitespace = /[\u1680\u180e\u2000-\u200a\u202f\u205f\u3000\ufeff]/; + var nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u0527\u0531-\u0556\u0559\u0561-\u0587\u05d0-\u05ea\u05f0-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u08a0\u08a2-\u08ac\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0977\u0979-\u097f\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c33\u0c35-\u0c39\u0c3d\u0c58\u0c59\u0c60\u0c61\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d60\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e87\u0e88\u0e8a\u0e8d\u0e94-\u0e97\u0e99-\u0e9f\u0ea1-\u0ea3\u0ea5\u0ea7\u0eaa\u0eab\u0ead-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f4\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f0\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1877\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191c\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19c1-\u19c7\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1ce9-\u1cec\u1cee-\u1cf1\u1cf5\u1cf6\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u212f-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u2e2f\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309d-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312d\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fcc\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua697\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua78e\ua790-\ua793\ua7a0-\ua7aa\ua7f8-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa80-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uabc0-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc"; + var nonASCIIidentifierChars = "\u0300-\u036f\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u0620-\u0649\u0672-\u06d3\u06e7-\u06e8\u06fb-\u06fc\u0730-\u074a\u0800-\u0814\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0840-\u0857\u08e4-\u08fe\u0900-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962-\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09d7\u09df-\u09e0\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2-\u0ae3\u0ae6-\u0aef\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b56\u0b57\u0b5f-\u0b60\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c01-\u0c03\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62-\u0c63\u0c66-\u0c6f\u0c82\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2-\u0ce3\u0ce6-\u0cef\u0d02\u0d03\u0d46-\u0d48\u0d57\u0d62-\u0d63\u0d66-\u0d6f\u0d82\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0df2\u0df3\u0e34-\u0e3a\u0e40-\u0e45\u0e50-\u0e59\u0eb4-\u0eb9\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f41-\u0f47\u0f71-\u0f84\u0f86-\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u1000-\u1029\u1040-\u1049\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u170e-\u1710\u1720-\u1730\u1740-\u1750\u1772\u1773\u1780-\u17b2\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u1920-\u192b\u1930-\u193b\u1951-\u196d\u19b0-\u19c0\u19c8-\u19c9\u19d0-\u19d9\u1a00-\u1a15\u1a20-\u1a53\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1b46-\u1b4b\u1b50-\u1b59\u1b6b-\u1b73\u1bb0-\u1bb9\u1be6-\u1bf3\u1c00-\u1c22\u1c40-\u1c49\u1c5b-\u1c7d\u1cd0-\u1cd2\u1d00-\u1dbe\u1e01-\u1f15\u200c\u200d\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2d81-\u2d96\u2de0-\u2dff\u3021-\u3028\u3099\u309a\ua640-\ua66d\ua674-\ua67d\ua69f\ua6f0-\ua6f1\ua7f8-\ua800\ua806\ua80b\ua823-\ua827\ua880-\ua881\ua8b4-\ua8c4\ua8d0-\ua8d9\ua8f3-\ua8f7\ua900-\ua909\ua926-\ua92d\ua930-\ua945\ua980-\ua983\ua9b3-\ua9c0\uaa00-\uaa27\uaa40-\uaa41\uaa4c-\uaa4d\uaa50-\uaa59\uaa7b\uaae0-\uaae9\uaaf2-\uaaf3\uabc0-\uabe1\uabec\uabed\uabf0-\uabf9\ufb20-\ufb28\ufe00-\ufe0f\ufe20-\ufe26\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f"; + var nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]"); + var nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]"); + + // Whether a single character denotes a newline. + + var newline = exports.newline = /[\n\r\u2028\u2029]/; + + // Matches a whole line break (where CRLF is considered a single + // line break). Used to count lines. + + // in javascript, these two differ + // in python they are the same, different methods are called on them + var lineBreak = exports.lineBreak = /\r\n|[\n\r\u2028\u2029]/; + var allLineBreaks = exports.allLineBreaks = new RegExp(lineBreak.source, 'g'); + + + // Test whether a given character code starts an identifier. + + var isIdentifierStart = exports.isIdentifierStart = function(code) { + // permit $ (36) and @ (64). @ is used in ES7 decorators. + if (code < 65) return code === 36 || code === 64; + // 65 through 91 are uppercase letters. + if (code < 91) return true; + // permit _ (95). + if (code < 97) return code === 95; + // 97 through 123 are lowercase letters. + if (code < 123) return true; + return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code)); + }; + + // Test whether a given character is part of an identifier. + + var isIdentifierChar = exports.isIdentifierChar = function(code) { + if (code < 48) return code === 36; + if (code < 58) return true; + if (code < 65) return false; + if (code < 91) return true; + if (code < 97) return code === 95; + if (code < 123) return true; + return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code)); + }; })(acorn); + /* jshint curly: true */ function in_array(what, arr) { for (var i = 0; i < arr.length; i += 1) { @@ -202,9 +205,9 @@ if (!Object.values) { var validPositionValues = Object.values(OPERATOR_POSITION); if (!in_array(opPosition, validPositionValues)) { - throw new Error("Invalid Option Value: The option 'operator_position' must be one of the following values\n" - + validPositionValues - + "\nYou passed in: '" + opPosition + "'"); + throw new Error("Invalid Option Value: The option 'operator_position' must be one of the following values\n" + + validPositionValues + + "\nYou passed in: '" + opPosition + "'"); } return opPosition; @@ -219,19 +222,20 @@ if (!Object.values) { var OPERATOR_POSITION_BEFORE_OR_PRESERVE = [OPERATOR_POSITION.before_newline, OPERATOR_POSITION.preserve_newline]; var MODE = { - BlockStatement: 'BlockStatement', // 'BLOCK' - Statement: 'Statement', // 'STATEMENT' - ObjectLiteral: 'ObjectLiteral', // 'OBJECT', - ArrayLiteral: 'ArrayLiteral', //'[EXPRESSION]', - ForInitializer: 'ForInitializer', //'(FOR-EXPRESSION)', - Conditional: 'Conditional', //'(COND-EXPRESSION)', - Expression: 'Expression' //'(EXPRESSION)' - }; + BlockStatement: 'BlockStatement', // 'BLOCK' + Statement: 'Statement', // 'STATEMENT' + ObjectLiteral: 'ObjectLiteral', // 'OBJECT', + ArrayLiteral: 'ArrayLiteral', //'[EXPRESSION]', + ForInitializer: 'ForInitializer', //'(FOR-EXPRESSION)', + Conditional: 'Conditional', //'(COND-EXPRESSION)', + Expression: 'Expression' //'(EXPRESSION)' + }; function Beautifier(js_source_text, options) { "use strict"; - var output - var tokens = [], token_pos; + var output; + var tokens = [], + token_pos; var Tokenizer; var current_token; var last_type, last_last_text, indent_string; @@ -333,11 +337,11 @@ if (!Object.values) { opt.test_output_raw = (options.test_output_raw === undefined) ? false : options.test_output_raw; // force opt.space_after_anon_function to true if opt.jslint_happy - if(opt.jslint_happy) { + if (opt.jslint_happy) { opt.space_after_anon_function = true; } - if(options.indent_with_tabs){ + if (options.indent_with_tabs) { opt.indent_char = '\t'; opt.indent_size = 1; } @@ -349,7 +353,7 @@ if (!Object.values) { } } - opt.eol = opt.eol.replace(/\\r/, '\r').replace(/\\n/, '\n') + opt.eol = opt.eol.replace(/\\r/, '\r').replace(/\\n/, '\n'); //---------------------------------- indent_string = ''; @@ -359,8 +363,8 @@ if (!Object.values) { } var preindent_index = 0; - if(js_source_text && js_source_text.length) { - while ( (js_source_text.charAt(preindent_index) === ' ' || + if (js_source_text && js_source_text.length) { + while ((js_source_text.charAt(preindent_index) === ' ' || js_source_text.charAt(preindent_index) === '\t')) { baseIndentString += js_source_text.charAt(preindent_index); preindent_index += 1; @@ -397,8 +401,13 @@ if (!Object.values) { tokens = Tokenizer.tokenize(); token_pos = 0; - while (local_token = get_token()) { - for(var i = 0; i < local_token.comments_before.length; i++) { + function get_local_token() { + local_token = get_token(); + return local_token; + } + + while (get_local_token()) { + for (var i = 0; i < local_token.comments_before.length; i++) { // The cleanest handling of inline comments is to treat them as though they aren't there. // Just continue formatting and the behavior should be logical. // Also ignore unknown tokens. Again, this should result in better behavior. @@ -418,7 +427,7 @@ if (!Object.values) { sweet_code += '\n'; } - if (opt.eol != '\n') { + if (opt.eol !== '\n') { sweet_code = sweet_code.replace(/[\n]/g, opt.eol); } @@ -430,7 +439,7 @@ if (!Object.values) { var keep_whitespace = opt.keep_array_indentation && is_array(flags.mode); if (keep_whitespace) { - for (i = 0; i < newlines; i += 1) { + for (var i = 0; i < newlines; i += 1) { print_newline(i > 0); } } else { @@ -441,7 +450,7 @@ if (!Object.values) { if (opt.preserve_newlines) { if (local_token.newlines > 1) { print_newline(); - for (var i = 1; i < newlines; i += 1) { + for (var j = 1; j < newlines; j += 1) { print_newline(true); } } @@ -471,25 +480,26 @@ if (!Object.values) { return out; } - var newline_restricted_tokens = ['break','contiue','return', 'throw']; + var newline_restricted_tokens = ['break', 'contiue', 'return', 'throw']; + function allow_wrap_or_preserved_newline(force_linewrap) { force_linewrap = (force_linewrap === undefined) ? false : force_linewrap; // Never wrap the first token on a line if (output.just_added_newline()) { - return + return; } var shouldPreserveOrForce = (opt.preserve_newlines && current_token.wanted_newline) || force_linewrap; var operatorLogicApplies = in_array(flags.last_text, Tokenizer.positionable_operators) || in_array(current_token.text, Tokenizer.positionable_operators); if (operatorLogicApplies) { - var shouldPrintOperatorNewline = ( - in_array(flags.last_text, Tokenizer.positionable_operators) && - in_array(opt.operator_position, OPERATOR_POSITION_BEFORE_OR_PRESERVE) - ) - || in_array(current_token.text, Tokenizer.positionable_operators); - shouldPreserveOrForce = shouldPreserveOrForce && shouldPrintOperatorNewline; + var shouldPrintOperatorNewline = ( + in_array(flags.last_text, Tokenizer.positionable_operators) && + in_array(opt.operator_position, OPERATOR_POSITION_BEFORE_OR_PRESERVE) + ) || + in_array(current_token.text, Tokenizer.positionable_operators); + shouldPreserveOrForce = shouldPreserveOrForce && shouldPrintOperatorNewline; } if (shouldPreserveOrForce) { @@ -498,7 +508,7 @@ if (!Object.values) { if (last_type === 'TK_RESERVED' && in_array(flags.last_text, newline_restricted_tokens)) { // These tokens should never have a newline inserted // between them and the following expression. - return + return; } var proposed_line_length = output.current_line.get_character_count() + current_token.text.length + (output.space_before_token ? 1 : 0); @@ -535,21 +545,21 @@ if (!Object.values) { function print_token(printable_token) { if (output.raw) { - output.add_raw_token(current_token) + output.add_raw_token(current_token); return; } - if (opt.comma_first && last_type === 'TK_COMMA' - && output.just_added_newline()) { - if(output.previous_line.last() === ',') { + if (opt.comma_first && last_type === 'TK_COMMA' && + output.just_added_newline()) { + if (output.previous_line.last() === ',') { var popped = output.previous_line.pop(); // if the comma was already at the start of the line, // pull back onto that line and reprint the indentation - if(output.previous_line.is_empty()) { - output.previous_line.push(popped); - output.trim(true); - output.current_line.pop(); - output.trim(); + if (output.previous_line.is_empty()) { + output.previous_line.push(popped); + output.trim(true); + output.current_line.pop(); + output.trim(); } // add the comma in front of the next token @@ -570,8 +580,10 @@ if (!Object.values) { function deindent() { if (flags.indentation_level > 0 && - ((!flags.parent) || flags.indentation_level > flags.parent.indentation_level)) + ((!flags.parent) || flags.indentation_level > flags.parent.indentation_level)) { flags.indentation_level -= 1; + + } } function set_mode(mode) { @@ -610,19 +622,19 @@ if (!Object.values) { function start_of_statement() { if ( - (last_type === 'TK_RESERVED' && in_array(flags.last_text, ['var', 'let', 'const']) && current_token.type === 'TK_WORD') || - (last_type === 'TK_RESERVED' && flags.last_text === 'do') || - (last_type === 'TK_RESERVED' && in_array(flags.last_text, ['return', 'throw']) && !current_token.wanted_newline) || - (last_type === 'TK_RESERVED' && flags.last_text === 'else' && !(current_token.type === 'TK_RESERVED' && current_token.text === 'if')) || - (last_type === 'TK_END_EXPR' && (previous_flags.mode === MODE.ForInitializer || previous_flags.mode === MODE.Conditional)) || - (last_type === 'TK_WORD' && flags.mode === MODE.BlockStatement - && !flags.in_case - && !(current_token.text === '--' || current_token.text === '++') - && last_last_text !== 'function' - && current_token.type !== 'TK_WORD' && current_token.type !== 'TK_RESERVED') || - (flags.mode === MODE.ObjectLiteral && ( - (flags.last_text === ':' && flags.ternary_depth === 0) || (last_type === 'TK_RESERVED' && in_array(flags.last_text, ['get', 'set'])))) - ) { + (last_type === 'TK_RESERVED' && in_array(flags.last_text, ['var', 'let', 'const']) && current_token.type === 'TK_WORD') || + (last_type === 'TK_RESERVED' && flags.last_text === 'do') || + (last_type === 'TK_RESERVED' && in_array(flags.last_text, ['return', 'throw']) && !current_token.wanted_newline) || + (last_type === 'TK_RESERVED' && flags.last_text === 'else' && !(current_token.type === 'TK_RESERVED' && current_token.text === 'if')) || + (last_type === 'TK_END_EXPR' && (previous_flags.mode === MODE.ForInitializer || previous_flags.mode === MODE.Conditional)) || + (last_type === 'TK_WORD' && flags.mode === MODE.BlockStatement && + !flags.in_case && + !(current_token.text === '--' || current_token.text === '++') && + last_last_text !== 'function' && + current_token.type !== 'TK_WORD' && current_token.type !== 'TK_RESERVED') || + (flags.mode === MODE.ObjectLiteral && ( + (flags.last_text === ':' && flags.ternary_depth === 0) || (last_type === 'TK_RESERVED' && in_array(flags.last_text, ['get', 'set'])))) + ) { set_mode(MODE.Statement); indent(); @@ -743,7 +755,7 @@ if (!Object.values) { } // Should be a space between await and an IIFE - if(current_token.text === '(' && last_type === 'TK_RESERVED' && flags.last_word === 'await'){ + if (current_token.text === '(' && last_type === 'TK_RESERVED' && flags.last_word === 'await') { output.space_before_token = true; } @@ -788,7 +800,7 @@ if (!Object.values) { } if (opt.space_in_paren) { - if (last_type === 'TK_START_EXPR' && ! opt.space_in_empty_paren) { + if (last_type === 'TK_START_EXPR' && !opt.space_in_empty_paren) { // () [] no inner space in empty parens like these, ever, ref #320 output.trim(); output.space_before_token = false; @@ -816,15 +828,15 @@ if (!Object.values) { function handle_start_block() { // Check if this is should be treated as a ObjectLiteral - var next_token = get_token(1) - var second_token = get_token(2) + var next_token = get_token(1); + var second_token = get_token(2); if (second_token && ( - (in_array(second_token.text, [':', ',']) && in_array(next_token.type, ['TK_STRING', 'TK_WORD', 'TK_RESERVED'])) - || (in_array(next_token.text, ['get', 'set']) && in_array(second_token.type, ['TK_WORD', 'TK_RESERVED'])) + (in_array(second_token.text, [':', ',']) && in_array(next_token.type, ['TK_STRING', 'TK_WORD', 'TK_RESERVED'])) || + (in_array(next_token.text, ['get', 'set']) && in_array(second_token.type, ['TK_WORD', 'TK_RESERVED'])) )) { // We don't support TypeScript,but we didn't break it for a very long time. // We'll try to keep not breaking it. - if (!in_array(last_last_text, ['class','interface'])) { + if (!in_array(last_last_text, ['class', 'interface'])) { set_mode(MODE.ObjectLiteral); } else { set_mode(MODE.BlockStatement); @@ -834,7 +846,7 @@ if (!Object.values) { set_mode(MODE.BlockStatement); } else if (in_array(last_type, ['TK_EQUALS', 'TK_START_EXPR', 'TK_COMMA', 'TK_OPERATOR']) || (last_type === 'TK_RESERVED' && in_array(flags.last_text, ['return', 'throw', 'import'])) - ) { + ) { // Detecting shorthand function syntax is difficult by scanning forward, // so check the surrounding context. // If the block is being returned, imported, passed as arg, @@ -844,7 +856,7 @@ if (!Object.values) { set_mode(MODE.BlockStatement); } - var empty_braces = !next_token.comments_before.length && next_token.text === '}'; + var empty_braces = !next_token.comments_before.length && next_token.text === '}'; var empty_anonymous_function = empty_braces && flags.last_word === 'function' && last_type === 'TK_END_EXPR'; @@ -873,18 +885,18 @@ if (!Object.values) { break; } } while (check_token.type !== 'TK_EOF' && - !(check_token.type === 'TK_END_BLOCK' && check_token.opened === current_token)) + !(check_token.type === 'TK_END_BLOCK' && check_token.opened === current_token)); } if (is_array(previous_flags.mode) && (last_type === 'TK_START_EXPR' || last_type === 'TK_COMMA')) { // if we're preserving inline, // allow newline between comma and next brace. - if (last_type === 'TK_COMMA' || opt.space_in_paren ) { + if (last_type === 'TK_COMMA' || opt.space_in_paren) { output.space_before_token = true; } if (opt.brace_style === 'collapse-preserve-inline' && - (last_type === 'TK_COMMA' || (last_type === 'TK_START_EXPR' && flags.inline_frame ))) { + (last_type === 'TK_COMMA' || (last_type === 'TK_START_EXPR' && flags.inline_frame))) { allow_wrap_or_preserved_newline(); previous_flags.multiline_frame = previous_flags.multiline_frame || flags.multiline_frame; flags.multiline_frame = false; @@ -940,7 +952,7 @@ if (!Object.values) { current_token.type = 'TK_WORD'; } else if (flags.mode === MODE.ObjectLiteral) { var next_token = get_token(1); - if (next_token.text == ':') { + if (next_token.text === ':') { current_token.type = 'TK_WORD'; } } @@ -1001,10 +1013,10 @@ if (!Object.values) { } if (current_token.type === 'TK_RESERVED' && current_token.text === 'function') { - if (in_array(flags.last_text, ['}', ';']) || (output.just_added_newline() && ! in_array(flags.last_text, ['[', '{', ':', '=', ',']))) { + if (in_array(flags.last_text, ['}', ';']) || (output.just_added_newline() && !in_array(flags.last_text, ['[', '{', ':', '=', ',']))) { // make sure there is a nice clean space of at least one blank line // before a new function definition - if ( !output.just_added_blankline() && !current_token.comments_before.length) { + if (!output.just_added_blankline() && !current_token.comments_before.length) { print_newline(); print_newline(true); } @@ -1033,7 +1045,7 @@ if (!Object.values) { } } - if (current_token.type === 'TK_RESERVED' && in_array(current_token.text, ['function', 'get', 'set'])) { + if (current_token.type === 'TK_RESERVED' && in_array(current_token.text, ['function', 'get', 'set'])) { print_token(); flags.last_word = current_token.text; return; @@ -1268,7 +1280,7 @@ if (!Object.values) { in_array(last_type, ['TK_START_BLOCK', 'TK_START_EXPR', 'TK_EQUALS', 'TK_OPERATOR']) || in_array(flags.last_text, Tokenizer.line_starters) || flags.last_text === ',' - ); + ); if (current_token.text === ':') { if (flags.ternary_depth === 0) { @@ -1375,8 +1387,8 @@ if (!Object.values) { } - if (((flags.mode === MODE.BlockStatement && !flags.inline_frame) || flags.mode === MODE.Statement) - && (flags.last_text === '{' || flags.last_text === ';')) { + if (((flags.mode === MODE.BlockStatement && !flags.inline_frame) || flags.mode === MODE.Statement) && + (flags.last_text === '{' || flags.last_text === ';')) { // { foo; --i } // foo(); --bar; print_newline(); @@ -1392,8 +1404,8 @@ if (!Object.values) { function handle_block_comment() { if (output.raw) { - output.add_raw_token(current_token) - if (current_token.directives && current_token.directives['preserve'] === 'end') { + output.add_raw_token(current_token); + if (current_token.directives && current_token.directives.preserve === 'end') { // If we're testing the raw output behavior, do not allow a directive to turn it off. output.raw = opt.test_output_raw; } @@ -1403,7 +1415,7 @@ if (!Object.values) { if (current_token.directives) { print_newline(false, true); print_token(); - if (current_token.directives['preserve'] === 'start') { + if (current_token.directives.preserve === 'start') { output.raw = true; } print_newline(false, true); @@ -1506,31 +1518,31 @@ if (!Object.values) { var _empty = true; this.set_indent = function(level) { - _character_count = parent.baseIndentLength + level * parent.indent_length + _character_count = parent.baseIndentLength + level * parent.indent_length; _indent_count = level; - } + }; this.get_character_count = function() { return _character_count; - } + }; this.is_empty = function() { return _empty; - } + }; this.last = function() { if (!this._empty) { - return _items[_items.length - 1]; + return _items[_items.length - 1]; } else { - return null; + return null; } - } + }; this.push = function(input) { _items.push(input); _character_count += input.length; _empty = false; - } + }; this.pop = function() { var item = null; @@ -1540,14 +1552,14 @@ if (!Object.values) { _empty = _items.length === 0; } return item; - } + }; this.remove_indent = function() { if (_indent_count > 0) { _indent_count -= 1; - _character_count -= parent.indent_length + _character_count -= parent.indent_length; } - } + }; this.trim = function() { while (this.last() === ' ') { @@ -1555,7 +1567,7 @@ if (!Object.values) { _character_count -= 1; } _empty = _items.length === 0; - } + }; this.toString = function() { var result = ''; @@ -1563,20 +1575,20 @@ if (!Object.values) { if (_indent_count >= 0) { result = parent.indent_cache[_indent_count]; } - result += _items.join('') + result += _items.join(''); } return result; - } + }; } function Output(indent_string, baseIndentString) { baseIndentString = baseIndentString || ''; - this.indent_cache = [ baseIndentString ]; + this.indent_cache = [baseIndentString]; this.baseIndentLength = baseIndentString.length; this.indent_length = indent_string.length; this.raw = false; - var lines =[]; + var lines = []; this.baseIndentString = baseIndentString; this.indent_string = indent_string; this.previous_line = null; @@ -1587,7 +1599,7 @@ if (!Object.values) { this.previous_line = this.current_line; this.current_line = new OutputLine(this); lines.push(this.current_line); - } + }; // initialize this.add_outputline(); @@ -1595,7 +1607,7 @@ if (!Object.values) { this.get_line_number = function() { return lines.length; - } + }; // Using object instead of string to allow for later expansion of info about each line this.add_new_line = function(force_newline) { @@ -1611,17 +1623,17 @@ if (!Object.values) { } return false; - } + }; this.get_code = function() { var sweet_code = lines.join('\n').replace(/[\r\n\t ]+$/, ''); return sweet_code; - } + }; this.set_indent = function(level) { // Never indent your first output indent at the start of the file if (lines.length > 1) { - while(level >= this.indent_cache.length) { + while (level >= this.indent_cache.length) { this.indent_cache.push(this.indent_cache[this.indent_cache.length - 1] + this.indent_string); } @@ -1630,7 +1642,7 @@ if (!Object.values) { } this.current_line.set_indent(0); return false; - } + }; this.add_raw_token = function(token) { for (var x = 0; x < token.newlines; x++) { @@ -1639,21 +1651,21 @@ if (!Object.values) { this.current_line.push(token.whitespace_before); this.current_line.push(token.text); this.space_before_token = false; - } + }; this.add_token = function(printable_token) { this.add_space_before_token(); this.current_line.push(printable_token); - } + }; this.add_space_before_token = function() { if (this.space_before_token && !this.just_added_newline()) { this.current_line.push(' '); } this.space_before_token = false; - } + }; - this.remove_redundant_indentation = function (frame) { + this.remove_redundant_indentation = function(frame) { // This implementation is effective but has some issues: // - can cause line wrap to happen too soon due to indent removal // after wrap points are calculated @@ -1674,7 +1686,7 @@ if (!Object.values) { lines[index].remove_indent(); index++; } - } + }; this.trim = function(eat_newlines) { eat_newlines = (eat_newlines === undefined) ? false : eat_newlines; @@ -1684,16 +1696,16 @@ if (!Object.values) { while (eat_newlines && lines.length > 1 && this.current_line.is_empty()) { lines.pop(); - this.current_line = lines[lines.length - 1] + this.current_line = lines[lines.length - 1]; this.current_line.trim(); } this.previous_line = lines.length > 1 ? lines[lines.length - 2] : null; - } + }; this.just_added_newline = function() { return this.current_line.is_empty(); - } + }; this.just_added_blankline = function() { if (this.just_added_newline()) { @@ -1705,7 +1717,7 @@ if (!Object.values) { return line.is_empty(); } return false; - } + }; } @@ -1719,7 +1731,7 @@ if (!Object.values) { this.parent = null; this.opened = null; this.directives = null; - } + }; function tokenizer(input, opts, indent_string) { @@ -1729,10 +1741,10 @@ if (!Object.values) { var digit_oct = /[01234567]/; var digit_hex = /[0123456789abcdefABCDEF]/; - this.positionable_operators = '!= !== % & && * ** + - / : < << <= == === > >= >> >>> ? ^ | ||'.split(' ') + this.positionable_operators = '!= !== % & && * ** + - / : < << <= == === > >= >> >>> ? ^ | ||'.split(' '); var punct = this.positionable_operators.concat( // non-positionable operators - these do not follow operator position settings - '! %= &= *= **= ++ += , -- -= /= :: <<= = => >>= >>>= ^= |= ~'.split(' ')) + '! %= &= *= **= ++ += , -- -= /= :: <<= = => >>= >>>= ^= |= ~'.split(' ')); // words which should always start on new line. this.line_starters = 'continue,try,throw,return,var,let,const,if,switch,case,default,for,while,break,function,import,export'.split(','); @@ -1748,16 +1760,16 @@ if (!Object.values) { var directive_pattern = / (\w+)[:](\w+)/g; var directives_end_ignore_pattern = /([\s\S]*?)((?:\/\*\sbeautify\signore:end\s\*\/)|$)/g; - var template_pattern = /((<\?php|<\?=)[\s\S]*?\?>)|(<%[\s\S]*?%>)/g + var template_pattern = /((<\?php|<\?=)[\s\S]*?\?>)|(<%[\s\S]*?%>)/g; var n_newlines, whitespace_before_token, in_html_comment, tokens, parser_pos; var input_length; this.tokenize = function() { // cache the source's length. - input_length = input.length + input_length = input.length; parser_pos = 0; - in_html_comment = false + in_html_comment = false; tokens = []; var next, last; @@ -1769,7 +1781,7 @@ if (!Object.values) { while (!(last && last.type === 'TK_EOF')) { token_values = tokenize_next(); next = new Token(token_values[1], token_values[0], n_newlines, whitespace_before_token); - while(next.type === 'TK_COMMENT' || next.type === 'TK_BLOCK_COMMENT' || next.type === 'TK_UNKNOWN') { + while (next.type === 'TK_COMMENT' || next.type === 'TK_BLOCK_COMMENT' || next.type === 'TK_UNKNOWN') { if (next.type === 'TK_BLOCK_COMMENT') { next.directives = token_values[2]; } @@ -1787,13 +1799,13 @@ if (!Object.values) { next.parent = last; open_stack.push(open); open = next; - } else if ((next.type === 'TK_END_BLOCK' || next.type === 'TK_END_EXPR') && + } else if ((next.type === 'TK_END_BLOCK' || next.type === 'TK_END_EXPR') && (open && ( (next.text === ']' && open.text === '[') || (next.text === ')' && open.text === '(') || (next.text === '}' && open.text === '{')))) { next.parent = open.parent; - next.opened = open + next.opened = open; open = open_stack.pop(); } @@ -1803,9 +1815,9 @@ if (!Object.values) { } return tokens; - } + }; - function get_directives (text) { + function get_directives(text) { if (!text.match(directives_block_pattern)) { return null; } @@ -1835,7 +1847,7 @@ if (!Object.values) { var last_token; if (tokens.length) { - last_token = tokens[tokens.length-1]; + last_token = tokens[tokens.length - 1]; } else { // For the sake of tokenizing we can pretend that there was on open brace to start last_token = new Token('TK_START_BLOCK', '{'); @@ -1848,7 +1860,7 @@ if (!Object.values) { while (in_array(c, whitespace)) { if (acorn.newline.test(c)) { - if (!(c === '\n' && input.charAt(parser_pos-2) === '\r')) { + if (!(c === '\n' && input.charAt(parser_pos - 2) === '\r')) { n_newlines += 1; whitespace_on_this_line = []; } @@ -1864,7 +1876,7 @@ if (!Object.values) { parser_pos += 1; } - if(whitespace_on_this_line.length) { + if (whitespace_on_this_line.length) { whitespace_before_token = whitespace_on_this_line.join(''); } @@ -1877,9 +1889,9 @@ if (!Object.values) { // switch to hex/oct/bin number, no decimal or e, just hex/oct/bin digits allow_decimal = false; allow_e = false; - if ( /[Bb]/.test(input.charAt(parser_pos)) ) { + if (/[Bb]/.test(input.charAt(parser_pos))) { local_digit = digit_bin; - } else if ( /[Oo]/.test(input.charAt(parser_pos)) ) { + } else if (/[Oo]/.test(input.charAt(parser_pos))) { local_digit = digit_oct; } else { local_digit = digit_hex; @@ -1921,7 +1933,7 @@ if (!Object.values) { return [c, 'TK_WORD']; } - if (acorn.isIdentifierStart(input.charCodeAt(parser_pos-1))) { + if (acorn.isIdentifierStart(input.charCodeAt(parser_pos - 1))) { if (parser_pos < input_length) { while (acorn.isIdentifierChar(input.charCodeAt(parser_pos))) { c += input.charAt(parser_pos); @@ -1933,8 +1945,8 @@ if (!Object.values) { } if (!(last_token.type === 'TK_DOT' || - (last_token.type === 'TK_RESERVED' && in_array(last_token.text, ['set', 'get']))) - && in_array(c, reserved_words)) { + (last_token.type === 'TK_RESERVED' && in_array(last_token.text, ['set', 'get']))) && + in_array(c, reserved_words)) { if (c === 'in') { // hack for 'in' operator return [c, 'TK_OPERATOR']; } @@ -1966,17 +1978,18 @@ if (!Object.values) { if (c === '/') { var comment = ''; + var comment_match; // peek for comment /* ... */ if (input.charAt(parser_pos) === '*') { parser_pos += 1; block_comment_pattern.lastIndex = parser_pos; - var comment_match = block_comment_pattern.exec(input); + comment_match = block_comment_pattern.exec(input); comment = '/*' + comment_match[0]; parser_pos += comment_match[0].length; var directives = get_directives(comment); - if (directives && directives['ignore'] === 'start') { + if (directives && directives.ignore === 'start') { directives_end_ignore_pattern.lastIndex = parser_pos; - comment_match = directives_end_ignore_pattern.exec(input) + comment_match = directives_end_ignore_pattern.exec(input); comment += comment_match[0]; parser_pos += comment_match[0].length; } @@ -1987,7 +2000,7 @@ if (!Object.values) { if (input.charAt(parser_pos) === '/') { parser_pos += 1; comment_pattern.lastIndex = parser_pos; - var comment_match = comment_pattern.exec(input); + comment_match = comment_pattern.exec(input); comment = '//' + comment_match[0]; parser_pos += comment_match[0].length; return [comment, 'TK_COMMENT']; @@ -1995,14 +2008,14 @@ if (!Object.values) { } - var startXmlRegExp = /^<([-a-zA-Z:0-9_.]+|{.+?}|!\[CDATA\[[\s\S]*?\]\])(\s+{.+?}|\s+[-a-zA-Z:0-9_.]+|\s+[-a-zA-Z:0-9_.]+\s*=\s*('[^']*'|"[^"]*"|{.+?}))*\s*(\/?)\s*>/ + var startXmlRegExp = /^<([-a-zA-Z:0-9_.]+|{.+?}|!\[CDATA\[[\s\S]*?\]\])(\s+{.+?}|\s+[-a-zA-Z:0-9_.]+|\s+[-a-zA-Z:0-9_.]+\s*=\s*('[^']*'|"[^"]*"|{.+?}))*\s*(\/?)\s*>/; if (c === '`' || c === "'" || c === '"' || // string ( (c === '/') || // regexp (opts.e4x && c === "<" && input.slice(parser_pos - 1).match(startXmlRegExp)) // xml ) && ( // regex and xml can only appear in specific locations during parsing - (last_token.type === 'TK_RESERVED' && in_array(last_token.text , ['return', 'case', 'throw', 'else', 'do', 'typeof', 'yield'])) || + (last_token.type === 'TK_RESERVED' && in_array(last_token.text, ['return', 'case', 'throw', 'else', 'do', 'typeof', 'yield'])) || (last_token.type === 'TK_END_EXPR' && last_token.text === ')' && last_token.parent && last_token.parent.type === 'TK_RESERVED' && in_array(last_token.parent.text, ['if', 'while', 'for'])) || (in_array(last_token.type, ['TK_COMMENT', 'TK_START_EXPR', 'TK_START_BLOCK', @@ -2022,7 +2035,7 @@ if (!Object.values) { // var in_char_class = false; while (parser_pos < input_length && - ((esc || in_char_class || input.charAt(parser_pos) !== sep) && + ((esc || in_char_class || input.charAt(parser_pos) !== sep) && !acorn.newline.test(input.charAt(parser_pos)))) { resulting_string += input.charAt(parser_pos); if (!esc) { @@ -2049,9 +2062,9 @@ if (!Object.values) { var rootTag = match[2]; var depth = 0; while (match) { - var isEndTag = !! match[1]; + var isEndTag = !!match[1]; var tagName = match[2]; - var isSingletonTag = ( !! match[match.length - 1]) || (tagName.slice(0, 8) === "![CDATA["); + var isSingletonTag = (!!match[match.length - 1]) || (tagName.slice(0, 8) === "![CDATA["); if (tagName === rootTag && !isSingletonTag) { if (isEndTag) { --depth; @@ -2081,7 +2094,7 @@ if (!Object.values) { while (parser_pos < input_length) { current_char = input.charAt(parser_pos); if (!(esc || (current_char !== delimiter && - (allow_unescaped_newlines || !acorn.newline.test(current_char))))) { + (allow_unescaped_newlines || !acorn.newline.test(current_char))))) { break; } @@ -2108,17 +2121,18 @@ if (!Object.values) { if (start_sub && resulting_string.indexOf(start_sub, resulting_string.length - start_sub.length) !== -1) { if (delimiter === '`') { - parse_string('}', allow_unescaped_newlines, '`') - } else { - parse_string('`', allow_unescaped_newlines, '${') + parse_string('}', allow_unescaped_newlines, '`'); + } else { + parse_string('`', allow_unescaped_newlines, '${'); } } } - } + }; + if (sep === '`') { - parse_string('`', true, '${') - } else { - parse_string(sep) + parse_string('`', true, '${'); + } else { + parse_string(sep); } } @@ -2183,7 +2197,7 @@ if (!Object.values) { if (c === '<' && (input.charAt(parser_pos) === '?' || input.charAt(parser_pos) === '%')) { template_pattern.lastIndex = parser_pos - 1; var template_match = template_pattern.exec(input); - if(template_match) { + if (template_match) { c = template_match[0]; parser_pos += c.length - 1; c = c.replace(acorn.allLineBreaks, '\n'); @@ -2317,4 +2331,4 @@ if (!Object.values) { global.js_beautify = js_beautify; } -}()); +}()); \ No newline at end of file diff --git a/js/lib/cli.js b/js/lib/cli.js index ac9c2b2f7..e97e8bd64 100755 --- a/js/lib/cli.js +++ b/js/lib/cli.js @@ -33,8 +33,8 @@ */ var debug = process.env.DEBUG_JSBEAUTIFY || process.env.JSBEAUTIFY_DEBUG ? function() { - console.error.apply(console, arguments); - } : function() {}; + console.error.apply(console, arguments); +} : function() {}; var fs = require('fs'), cc = require('config-chain'), @@ -98,7 +98,7 @@ var fs = require('fs'), "p": ["--preserve_newlines"], "m": ["--max_preserve_newlines"], "P": ["--space_in_paren"], - "E": ["--space_in_empty_paren"], + "Q": ["--space_in_empty_paren"], "j": ["--jslint_happy"], "a": ["--space_after_anon_function"], "b": ["--brace_style"], @@ -137,7 +137,7 @@ var fs = require('fs'), "o": ["--outfile"], "r": ["--replace"], "q": ["--quiet"] - // no shorthand for "config" + // no shorthand for "config" }); function verifyExists(fullPath) { @@ -150,7 +150,7 @@ function findRecursive(dir, fileName) { var result = verifyExists(fullPath); if (!result && (nextDir !== dir)) { - result = findRecursive(nextDir, fileName); + result = findRecursive(nextDir, fileName); } return result; @@ -159,9 +159,8 @@ function findRecursive(dir, fileName) { function getUserHome() { var user_home = ''; try { - user_home = process.env['USERPROFILE'] || process.env['HOME'] || ''; - } catch (ex) { - } + user_home = process.env.USERPROFILE || process.env.HOME || ''; + } catch (ex) {} return user_home; } @@ -276,8 +275,8 @@ function usage(err) { msg.push(' -E, --extra_liners List of tags (defaults to [head,body,/html] that should have an extra newline'); break; case "css": - msg.push(' -L, --selector-separator-newline Add a newline between multiple selectors.') - msg.push(' -N, --newline-between-rules Add a newline between CSS rules.') + msg.push(' -L, --selector-separator-newline Add a newline between multiple selectors.'); + msg.push(' -N, --newline-between-rules Add a newline between CSS rules.'); } if (err) { @@ -503,4 +502,4 @@ function logToStdout(str, config) { if (typeof config.quiet === "undefined" || !config.quiet) { console.log(str); } -} +} \ No newline at end of file diff --git a/js/lib/unpackers/javascriptobfuscator_unpacker.js b/js/lib/unpackers/javascriptobfuscator_unpacker.js index 18d47f6d5..2e68ca68b 100644 --- a/js/lib/unpackers/javascriptobfuscator_unpacker.js +++ b/js/lib/unpackers/javascriptobfuscator_unpacker.js @@ -11,11 +11,11 @@ // var JavascriptObfuscator = { - detect: function (str) { + detect: function(str) { return /^var _0x[a-f0-9]+ ?\= ?\[/.test(str); }, - unpack: function (str) { + unpack: function(str) { if (JavascriptObfuscator.detect(str)) { var matches = /var (_0x[a-f\d]+) ?\= ?\[(.*?)\];/.exec(str); if (matches) { @@ -44,15 +44,15 @@ var JavascriptObfuscator = { var strings = []; var pos = 0; while (pos < str.length) { - if (str.charAt(pos) == '"') { + if (str.charAt(pos) === '"') { // new word var word = ''; pos += 1; while (pos < str.length) { - if (str.charAt(pos) == '"') { + if (str.charAt(pos) === '"') { break; } - if (str.charAt(pos) == '\\') { + if (str.charAt(pos) === '\\') { word += '\\'; pos++; } @@ -67,7 +67,7 @@ var JavascriptObfuscator = { }, - _unescape: function (str) { + _unescape: function(str) { // inefficient if used repeatedly or on small strings, but wonderful on single large chunk of text for (var i = 32; i < 128; i++) { str = str.replace(new RegExp('\\\\x' + i.toString(16), 'ig'), String.fromCharCode(i)); @@ -76,7 +76,7 @@ var JavascriptObfuscator = { return str; }, - run_tests: function (sanity_test) { + run_tests: function(sanity_test) { var t = sanity_test || new SanityTest(); t.test_function(JavascriptObfuscator._smart_split, "JavascriptObfuscator._smart_split"); @@ -100,4 +100,4 @@ var JavascriptObfuscator = { } -}; +}; \ No newline at end of file diff --git a/js/lib/unpackers/myobfuscate_unpacker.js b/js/lib/unpackers/myobfuscate_unpacker.js index 3200db00b..a7f245de1 100644 --- a/js/lib/unpackers/myobfuscate_unpacker.js +++ b/js/lib/unpackers/myobfuscate_unpacker.js @@ -27,7 +27,7 @@ // var MyObfuscate = { - detect: function (str) { + detect: function(str) { if (/^var _?[0O1lI]{3}\=('|\[).*\)\)\);/.test(str)) { return true; } @@ -37,11 +37,11 @@ var MyObfuscate = { return false; }, - unpack: function (str) { + unpack: function(str) { if (MyObfuscate.detect(str)) { var __eval = eval; try { - eval = function (unpacked) { + eval = function(unpacked) { // jshint ignore:line if (MyObfuscate.starts_with(unpacked, 'var _escape')) { // fetch the urlencoded stuff from the script, var matches = /'([^']*)'/.exec(unpacked); @@ -55,11 +55,11 @@ var MyObfuscate = { unpacked = unescaped; } // throw to terminate the script - unpacked = "// Unpacker warning: be careful when using myobfuscate.com for your projects:\n" + + unpacked = "// Unpacker warning: be careful when using myobfuscate.com for your projects:\n" + "// scripts obfuscated by the free online version may call back home.\n" + "\n//\n" + unpacked; throw unpacked; - }; + }; // jshint ignore:line __eval(str); // should throw } catch (e) { // well, it failed. we'll just return the original, instead of crashing on user. @@ -67,24 +67,24 @@ var MyObfuscate = { str = e; } } - eval = __eval; + eval = __eval; // jshint ignore:line } return str; }, - starts_with: function (str, what) { + starts_with: function(str, what) { return str.substr(0, what.length) === what; }, - ends_with: function (str, what) { + ends_with: function(str, what) { return str.substr(str.length - what.length, what.length) === what; }, - run_tests: function (sanity_test) { + run_tests: function(sanity_test) { var t = sanity_test || new SanityTest(); return t; } -}; +}; \ No newline at end of file diff --git a/js/lib/unpackers/p_a_c_k_e_r_unpacker.js b/js/lib/unpackers/p_a_c_k_e_r_unpacker.js index fdf011339..0fbb40530 100644 --- a/js/lib/unpackers/p_a_c_k_e_r_unpacker.js +++ b/js/lib/unpackers/p_a_c_k_e_r_unpacker.js @@ -13,7 +13,7 @@ // var P_A_C_K_E_R = { - detect: function (str) { + detect: function(str) { return (P_A_C_K_E_R.get_chunks(str).length > 0); }, @@ -22,44 +22,47 @@ var P_A_C_K_E_R = { return chunks ? chunks : []; }, - unpack: function (str) { + unpack: function(str) { var chunks = P_A_C_K_E_R.get_chunks(str), chunk; - for(var i = 0; i < chunks.length; i++) { + for (var i = 0; i < chunks.length; i++) { chunk = chunks[i].replace(/\n$/, ''); - str = str.split(chunk).join( P_A_C_K_E_R.unpack_chunk(chunk) ); + str = str.split(chunk).join(P_A_C_K_E_R.unpack_chunk(chunk)); } return str; }, - unpack_chunk: function (str) { + unpack_chunk: function(str) { var unpacked_source = ''; var __eval = eval; if (P_A_C_K_E_R.detect(str)) { try { - eval = function (s) { unpacked_source += s; return unpacked_source; }; + eval = function(s) { // jshint ignore:line + unpacked_source += s; + return unpacked_source; + }; // jshint ignore:line __eval(str); - if (typeof unpacked_source == 'string' && unpacked_source) { + if (typeof unpacked_source === 'string' && unpacked_source) { str = unpacked_source; } } catch (e) { // well, it failed. we'll just return the original, instead of crashing on user. } } - eval = __eval; + eval = __eval; // jshint ignore:line return str; }, - run_tests: function (sanity_test) { - var t = sanity_test || new SanityTest(), + run_tests: function(sanity_test) { + var t = sanity_test || new SanityTest(); - pk1 = "eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}('0 2=1',3,3,'var||a'.split('|'),0,{}))", - unpk1 = 'var a=1', - pk2 = "eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}('0 2=1',3,3,'foo||b'.split('|'),0,{}))", - unpk2 = 'foo b=1', - pk_broken = "eval(function(p,a,c,k,e,r){BORKBORK;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}('0 2=1',3,3,'var||a'.split('|'),0,{}))"; - pk3 = "eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}('0 2=1{}))',3,3,'var||a'.split('|'),0,{}))", - unpk3 = 'var a=1{}))', + var pk1 = "eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}('0 2=1',3,3,'var||a'.split('|'),0,{}))"; + var unpk1 = 'var a=1'; + var pk2 = "eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}('0 2=1',3,3,'foo||b'.split('|'),0,{}))"; + var unpk2 = 'foo b=1'; + var pk_broken = "eval(function(p,a,c,k,e,r){BORKBORK;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}('0 2=1',3,3,'var||a'.split('|'),0,{}))"; + var pk3 = "eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}('0 2=1{}))',3,3,'var||a'.split('|'),0,{}))"; + var unpk3 = 'var a=1{}))'; t.test_function(P_A_C_K_E_R.detect, "P_A_C_K_E_R.detect"); t.expect('', false); @@ -77,4 +80,4 @@ var P_A_C_K_E_R = { } -}; +}; \ No newline at end of file diff --git a/js/lib/unpackers/urlencode_unpacker.js b/js/lib/unpackers/urlencode_unpacker.js index 51a5f7b81..71c72ea43 100644 --- a/js/lib/unpackers/urlencode_unpacker.js +++ b/js/lib/unpackers/urlencode_unpacker.js @@ -18,19 +18,19 @@ if (isNode) { } var Urlencoded = { - detect: function (str) { + detect: function(str) { // the fact that script doesn't contain any space, but has %20 instead // should be sufficient check for now. - if (str.indexOf(' ') == -1) { - if (str.indexOf('%2') != -1) return true; + if (str.indexOf(' ') === -1) { + if (str.indexOf('%2') !== -1) return true; if (str.replace(/[^%]+/g, '').length > 3) return true; } return false; }, - unpack: function (str) { + unpack: function(str) { if (Urlencoded.detect(str)) { - if (str.indexOf('%2B') != -1 || str.indexOf('%2b') != -1) { + if (str.indexOf('%2B') !== -1 || str.indexOf('%2b') !== -1) { // "+" escaped as "%2B" return unescape(str.replace(/\+/g, '%20')); } else { @@ -42,7 +42,7 @@ var Urlencoded = { - run_tests: function (sanity_test) { + run_tests: function(sanity_test) { var t = sanity_test || new SanityTest(); t.test_function(Urlencoded.detect, "Urlencoded.detect"); t.expect('', false); @@ -70,4 +70,4 @@ var Urlencoded = { if (isNode) { module.exports = Urlencoded; -} +} \ No newline at end of file diff --git a/js/test/amd-beautify-tests.js b/js/test/amd-beautify-tests.js index b630ba766..71510e4d2 100755 --- a/js/test/amd-beautify-tests.js +++ b/js/test/amd-beautify-tests.js @@ -9,10 +9,10 @@ var requirejs = require('requirejs'), run_html_tests = require('./generated/beautify-html-tests').run_html_tests; requirejs.config({ - paths: { - 'beautify': "..", - 'beautify-lib': "../lib" - } + paths: { + 'beautify': "..", + 'beautify-lib': "../lib" + } }); function amd_beautifier_index_tests(name, test_runner) { @@ -21,11 +21,11 @@ function amd_beautifier_index_tests(name, test_runner) { var beautify = requirejs('beautify/index'); test_runner( - results, - Urlencoded, - beautify.js, - beautify.html, - beautify.css); + results, + Urlencoded, + beautify.js, + beautify.html, + beautify.css); console.log(results.results_raw()); return results; @@ -39,11 +39,11 @@ function amd_beautifier_tests(name, test_runner) { html_beautify = requirejs('beautify-lib/beautify-html'); test_runner( - results, - Urlencoded, - js_beautify.js_beautify, - html_beautify.html_beautify, - css_beautify.css_beautify); + results, + Urlencoded, + js_beautify.js_beautify, + html_beautify.html_beautify, + css_beautify.css_beautify); console.log(results.results_raw()); return results; @@ -53,11 +53,11 @@ function amd_beautifier_tests(name, test_runner) { if (require.main === module) { process.exit( - amd_beautifier_tests('js-beautifier', run_javascript_tests).get_exitcode() + - amd_beautifier_index_tests('js-beautifier', run_javascript_tests).get_exitcode() + - amd_beautifier_tests('cs-beautifier', run_css_tests).get_exitcode() + - amd_beautifier_index_tests('css-beautifier', run_css_tests).get_exitcode() + - amd_beautifier_tests('html-beautifier', run_html_tests).get_exitcode() + - amd_beautifier_index_tests('html-beautifier', run_html_tests).get_exitcode() - ); -} + amd_beautifier_tests('js-beautifier', run_javascript_tests).get_exitcode() + + amd_beautifier_index_tests('js-beautifier', run_javascript_tests).get_exitcode() + + amd_beautifier_tests('cs-beautifier', run_css_tests).get_exitcode() + + amd_beautifier_index_tests('css-beautifier', run_css_tests).get_exitcode() + + amd_beautifier_tests('html-beautifier', run_html_tests).get_exitcode() + + amd_beautifier_index_tests('html-beautifier', run_html_tests).get_exitcode() + ); +} \ No newline at end of file diff --git a/js/test/generated/beautify-css-tests.js b/js/test/generated/beautify-css-tests.js index f1862c017..d5ad0ce93 100644 --- a/js/test/generated/beautify-css-tests.js +++ b/js/test/generated/beautify-css-tests.js @@ -72,7 +72,7 @@ function run_css_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_bea } function unicode_char(value) { - return String.fromCharCode(value) + return String.fromCharCode(value); } function beautifier_tests() diff --git a/js/test/generated/beautify-html-tests.js b/js/test/generated/beautify-html-tests.js index 0e57faad0..50ddeba4c 100644 --- a/js/test/generated/beautify-html-tests.js +++ b/js/test/generated/beautify-html-tests.js @@ -81,7 +81,7 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be } function unicode_char(value) { - return String.fromCharCode(value) + return String.fromCharCode(value); } function beautifier_tests() diff --git a/js/test/generated/beautify-javascript-tests.js b/js/test/generated/beautify-javascript-tests.js index e9cd7c102..78890c63c 100644 --- a/js/test/generated/beautify-javascript-tests.js +++ b/js/test/generated/beautify-javascript-tests.js @@ -253,7 +253,7 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify, } function unicode_char(value) { - return String.fromCharCode(value) + return String.fromCharCode(value); } function beautifier_tests() @@ -987,11 +987,6 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify, '}'); - reset_options(); - //============================================================ - // New Test Suite - - reset_options(); //============================================================ // Async / await tests diff --git a/js/test/node-beautify-html-perf-tests.js b/js/test/node-beautify-html-perf-tests.js index 45926533f..14cd62052 100644 --- a/js/test/node-beautify-html-perf-tests.js +++ b/js/test/node-beautify-html-perf-tests.js @@ -21,24 +21,23 @@ function node_beautifier_html_tests() { html_beautify(index_html, options); html_beautify(data_attr, options); - var suite = new Benchmark.Suite; - - suite.add("html-beautify (index.html)", function () { - html_beautify(index_html, options); - }) - .add("html-beautify (base64 image)", function () { - html_beautify(data_attr, options); - }) - // add listeners - .on('cycle', function(event) { - console.log(String(event.target)); - }) - .on('error', function(event) { - return 1; - }) - .on('complete', function(event) { - }) - .run() + var suite = new Benchmark.Suite(); + + suite.add("html-beautify (index.html)", function() { + html_beautify(index_html, options); + }) + .add("html-beautify (base64 image)", function() { + html_beautify(data_attr, options); + }) + // add listeners + .on('cycle', function(event) { + console.log(String(event.target)); + }) + .on('error', function(event) { + return 1; + }) + .on('complete', function(event) {}) + .run(); return 0; } @@ -47,4 +46,4 @@ function node_beautifier_html_tests() { if (require.main === module) { process.exit(node_beautifier_html_tests()); -} +} \ No newline at end of file diff --git a/js/test/node-beautify-perf-tests.js b/js/test/node-beautify-perf-tests.js index 4df1af54b..d1a897ee9 100644 --- a/js/test/node-beautify-perf-tests.js +++ b/js/test/node-beautify-perf-tests.js @@ -21,24 +21,23 @@ function node_beautifier_tests() { js_beautify(data, options); js_beautify(data_min, options); - var suite = new Benchmark.Suite; + var suite = new Benchmark.Suite(); suite.add("js-beautify (underscore)", function() { - js_beautify(data, options); - }) - .add("js-beautify (underscore-min)", function() { - js_beautify(data_min, options); - }) - // add listeners - .on('cycle', function(event) { - console.log(String(event.target)); - }) - .on('error', function(event) { - return 1; - }) - .on('complete', function(event) { - }) - .run() + js_beautify(data, options); + }) + .add("js-beautify (underscore-min)", function() { + js_beautify(data_min, options); + }) + // add listeners + .on('cycle', function(event) { + console.log(String(event.target)); + }) + .on('error', function(event) { + return 1; + }) + .on('complete', function(event) {}) + .run(); return 0; } @@ -47,4 +46,4 @@ function node_beautifier_tests() { if (require.main === module) { process.exit(node_beautifier_tests()); -} +} \ No newline at end of file diff --git a/js/test/node-beautify-tests.js b/js/test/node-beautify-tests.js index af594f0a2..af1f0e696 100755 --- a/js/test/node-beautify-tests.js +++ b/js/test/node-beautify-tests.js @@ -26,11 +26,11 @@ function node_beautifier_tests(name, test_runner) { var results = new SanityTest(); test_runner( - results, - Urlencoded, - beautify.js, - beautify.html, - beautify.css); + results, + Urlencoded, + beautify.js, + beautify.html, + beautify.css); console.log(results.results_raw()); return results; @@ -38,9 +38,9 @@ function node_beautifier_tests(name, test_runner) { if (require.main === module) { process.exit( - test_legacy_names() + - node_beautifier_tests('js-beautifier', run_javascript_tests).get_exitcode() + - node_beautifier_tests('cs-beautifier', run_css_tests).get_exitcode() + - node_beautifier_tests('html-beautifier', run_html_tests).get_exitcode() - ); -} + test_legacy_names() + + node_beautifier_tests('js-beautifier', run_javascript_tests).get_exitcode() + + node_beautifier_tests('cs-beautifier', run_css_tests).get_exitcode() + + node_beautifier_tests('html-beautifier', run_html_tests).get_exitcode() + ); +} \ No newline at end of file diff --git a/js/test/sanitytest.js b/js/test/sanitytest.js index 75b45b9b4..9d1cd8d2b 100644 --- a/js/test/sanitytest.js +++ b/js/test/sanitytest.js @@ -11,9 +11,9 @@ // alert(t.results_raw()); // html unescaped -function SanityTest (func, name_of_test) { +function SanityTest(func, name_of_test) { - var test_func = func || function (x) { + var test_func = func || function(x) { return x; }; @@ -55,7 +55,7 @@ function SanityTest (func, name_of_test) { results = 'All ' + n_succeeded + ' tests passed.'; } } else { - for (var i = 0 ; i < failures.length; i++) { + for (var i = 0; i < failures.length; i++) { var f = failures[i]; if (f[0]) { f[0] = f[0] + ' '; @@ -78,47 +78,45 @@ function SanityTest (func, name_of_test) { this.prettyprint = function(something, quote_strings) { var type = typeof something; - switch(type.toLowerCase()) { - case 'string': - if (quote_strings) { - return "'" + something.replace("'", "\\'") + "'"; - } else { + switch (type.toLowerCase()) { + case 'string': + if (quote_strings) { + return "'" + something.replace("'", "\\'") + "'"; + } return something; - } - case 'number': - return '' + something; - case 'boolean': - return something ? 'true' : 'false'; - case 'undefined': - return 'undefined'; - case 'object': - if (something instanceof Array) { - var x = []; - var expected_index = 0; - for (var k in something) { - if (k === expected_index) { - x.push(this.prettyprint(something[k], true)); - expected_index += 1; - } else { - x.push('\n' + k + ': ' + this.prettyprint(something[k], true)); + case 'number': + return '' + something; + case 'boolean': + return something ? 'true' : 'false'; + case 'undefined': + return 'undefined'; + case 'object': + if (something instanceof Array) { + var x = []; + var expected_index = 0; + for (var k in something) { + if (k === expected_index) { + x.push(this.prettyprint(something[k], true)); + expected_index += 1; + } else { + x.push('\n' + k + ': ' + this.prettyprint(something[k], true)); + } } + return '[' + x.join(', ') + ']'; } - return '[' + x.join(', ') + ']'; - } else { return 'object: ' + something; - } - default: - return type + ': ' + something; + default: + return type + ': ' + something; } }; - this.lazy_escape = function (str) { + this.lazy_escape = function(str) { return str.replace(//g, '>').replace(/\n/g, '
'); }; - this.log = function () { + this.log = function() { if (window.console) { if (console.firebug) { console.log.apply(console, Array.prototype.slice.call(arguments)); @@ -132,4 +130,4 @@ function SanityTest (func, name_of_test) { if (typeof module !== 'undefined' && module.exports) { module.exports = SanityTest; -} +} \ No newline at end of file diff --git a/python/jsbeautifier/tests/generated/tests.py b/python/jsbeautifier/tests/generated/tests.py index edd45dc27..db35c1393 100644 --- a/python/jsbeautifier/tests/generated/tests.py +++ b/python/jsbeautifier/tests/generated/tests.py @@ -813,11 +813,6 @@ def unicode_char(value): '}') - self.reset_options(); - #============================================================ - # New Test Suite - - self.reset_options(); #============================================================ # Async / await tests diff --git a/test/data/css/node.mustache b/test/data/css/node.mustache index bcd189adc..f9d727fa7 100644 --- a/test/data/css/node.mustache +++ b/test/data/css/node.mustache @@ -66,7 +66,7 @@ function run_css_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_bea } function unicode_char(value) { - return String.fromCharCode(value) + return String.fromCharCode(value); } function beautifier_tests() diff --git a/test/data/css/tests.js b/test/data/css/tests.js index 6dd127f08..1ce2efd15 100644 --- a/test/data/css/tests.js +++ b/test/data/css/tests.js @@ -182,4 +182,4 @@ exports.test_data = { }, { }] -} \ No newline at end of file +}; \ No newline at end of file diff --git a/test/data/html/node.mustache b/test/data/html/node.mustache index b2eb73d5e..bce7d9f09 100644 --- a/test/data/html/node.mustache +++ b/test/data/html/node.mustache @@ -72,7 +72,7 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be } function unicode_char(value) { - return String.fromCharCode(value) + return String.fromCharCode(value); } function beautifier_tests() diff --git a/test/data/javascript/inputlib.js b/test/data/javascript/inputlib.js index 342395eba..c652270c5 100644 --- a/test/data/javascript/inputlib.js +++ b/test/data/javascript/inputlib.js @@ -1,6 +1,3 @@ -'use strict'; - - //--------// // Inputs // //--------// @@ -85,4 +82,4 @@ var operator_position = { module.exports = { operator_position: operator_position -} \ No newline at end of file +}; \ No newline at end of file diff --git a/test/data/javascript/node.mustache b/test/data/javascript/node.mustache index 0062f2d2f..7d4e36dde 100644 --- a/test/data/javascript/node.mustache +++ b/test/data/javascript/node.mustache @@ -245,7 +245,7 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify, } function unicode_char(value) { - return String.fromCharCode(value) + return String.fromCharCode(value); } function beautifier_tests() diff --git a/test/data/javascript/tests.js b/test/data/javascript/tests.js index 120ac51be..fc09b6f43 100644 --- a/test/data/javascript/tests.js +++ b/test/data/javascript/tests.js @@ -13,50 +13,50 @@ exports.test_data = { groups: [{ name: "Unicode Support", description: "", - tests: [ - { - input: "var ' + unicode_char(3232) + '_' + unicode_char(3232) + ' = \"hi\";" - }, { - input: [ - "var ' + unicode_char(228) + 'x = {", - " ' + unicode_char(228) + 'rgerlich: true", - "};"] - } - ] + tests: [{ + input: "var ' + unicode_char(3232) + '_' + unicode_char(3232) + ' = \"hi\";" + }, { + input: [ + "var ' + unicode_char(228) + 'x = {", + " ' + unicode_char(228) + 'rgerlich: true", + "};" + ] + }] }, { name: "Test template and continuation strings", description: "", tests: [ - { unchanged: '`This is a ${template} string.`' }, - { unchanged: '`This\n is\n a\n ${template}\n string.`' }, - { unchanged: 'a = `This is a continuation\\\nstring.`' }, - { unchanged: 'a = "This is a continuation\\\nstring."' }, - { unchanged: '`SELECT\n nextval(\\\'${this.options.schema ? `${this.options.schema}.` : \\\'\\\'}"${this.tableName}_${this.autoIncrementField}_seq"\\\'::regclass\n ) nextval;`' }, + { unchanged: '`This is a ${template} string.`' }, + { unchanged: '`This\n is\n a\n ${template}\n string.`' }, + { unchanged: 'a = `This is a continuation\\\nstring.`' }, + { unchanged: 'a = "This is a continuation\\\nstring."' }, + { unchanged: '`SELECT\n nextval(\\\'${this.options.schema ? `${this.options.schema}.` : \\\'\\\'}"${this.tableName}_${this.autoIncrementField}_seq"\\\'::regclass\n ) nextval;`' }, ] }, { name: "ES7 Decorators", description: "Permit ES7 decorators, which are invoked with a leading \"@\".", tests: [ - { unchanged: '@foo' }, - { unchanged: '@foo(bar)' }, - { unchanged: [ - '@foo(function(k, v) {', - ' implementation();', - '})' - ]} + { unchanged: '@foo' }, + { unchanged: '@foo(bar)' }, + { + unchanged: [ + '@foo(function(k, v) {', + ' implementation();', + '})' + ] + } ] }, { name: "ES7 exponential", description: "ES7 exponential", tests: [ - { unchanged: 'x ** 2' }, - { unchanged: 'x ** -2' } + { unchanged: 'x ** 2' }, + { unchanged: 'x ** -2' } ] }, { name: "End With Newline", description: "", - matrix: [ - { + matrix: [{ options: [ { name: "end_with_newline", value: "true" } ], @@ -136,8 +136,7 @@ exports.test_data = { oac: ' ' }, ], - tests: [ - { + tests: [{ input: 'var a ={a: 2};\nvar a ={a: 2};', output: 'var a ={a: 2};\nvar a ={a: 2};' }, @@ -154,24 +153,22 @@ exports.test_data = { output: 'if (1){2}else{3}' }, { - input: - 'try{a();}' + + input: 'try{a();}' + 'catch(b){c();}' + 'catch(d){}' + 'finally{e();}', output: - // expected + // expected 'try{a();}' + 'catch (b){c();}' + - 'catch (d){}'+ + 'catch (d){}' + 'finally{e();}' } ], }, { name: "Comma-first option", description: "Put commas at the start of lines instead of the end", - matrix: [ - { + matrix: [{ options: [ { name: "comma_first", value: "false" } ], @@ -191,8 +188,7 @@ exports.test_data = { c3: '\\n , ', // edge cases where engine bails f1: ', ' - } - ], + }], tests: [ { input: '{a:1, b:2}', output: "{\n a: 1{{c1}}b: 2\n}" }, { input: 'var a=1, b=c[d], e=6;', output: 'var a = 1{{c1}}b = c[d]{{c1}}e = 6;' }, @@ -201,21 +197,33 @@ exports.test_data = { { input: 'function foo() {\n return [\n "one"{{c2}}"two"\n ];\n}' }, { input: 'a=[[1,2],[4,5],[7,8]]', output: "a = [\n [1, 2]{{c1}}[4, 5]{{c1}}[7, 8]\n]" }, { input: 'a=[[1,2],[4,5],[7,8],]', output: "a = [\n [1, 2]{{c1}}[4, 5]{{c1}}[7, 8]{{c0}}]" }, - { input: 'a=[[1,2],[4,5],function(){},[7,8]]', - output: "a = [\n [1, 2]{{c1}}[4, 5]{{c1}}function() {}{{c1}}[7, 8]\n]" }, - { input: 'a=[[1,2],[4,5],function(){},function(){},[7,8]]', - output: "a = [\n [1, 2]{{c1}}[4, 5]{{c1}}function() {}{{c1}}function() {}{{c1}}[7, 8]\n]" }, - { input: 'a=[[1,2],[4,5],function(){},[7,8]]', - output: "a = [\n [1, 2]{{c1}}[4, 5]{{c1}}function() {}{{c1}}[7, 8]\n]" }, - { input: 'a=[b,c,function(){},function(){},d]', - output: "a = [b, c, function() {}, function() {}, d]" }, - { input: 'a=[b,c,\nfunction(){},function(){},d]', - output: "a = [b, c{{c1}}function() {}{{c1}}function() {}{{c1}}d\n]" }, + { + input: 'a=[[1,2],[4,5],function(){},[7,8]]', + output: "a = [\n [1, 2]{{c1}}[4, 5]{{c1}}function() {}{{c1}}[7, 8]\n]" + }, + { + input: 'a=[[1,2],[4,5],function(){},function(){},[7,8]]', + output: "a = [\n [1, 2]{{c1}}[4, 5]{{c1}}function() {}{{c1}}function() {}{{c1}}[7, 8]\n]" + }, + { + input: 'a=[[1,2],[4,5],function(){},[7,8]]', + output: "a = [\n [1, 2]{{c1}}[4, 5]{{c1}}function() {}{{c1}}[7, 8]\n]" + }, + { + input: 'a=[b,c,function(){},function(){},d]', + output: "a = [b, c, function() {}, function() {}, d]" + }, + { + input: 'a=[b,c,\nfunction(){},function(){},d]', + output: "a = [b, c{{c1}}function() {}{{c1}}function() {}{{c1}}d\n]" + }, { input: 'a=[a[1],b[4],c[d[7]]]', output: "a = [a[1], b[4], c[d[7]]]" }, { input: '[1,2,[3,4,[5,6],7],8]', output: "[1, 2, [3, 4, [5, 6], 7], 8]" }, - { input: '[[["1","2"],["3","4"]],[["5","6","7"],["8","9","0"]],[["1","2","3"],["4","5","6","7"],["8","9","0"]]]', - output: '[\n [\n ["1", "2"]{{c2}}["3", "4"]\n ]{{c1}}[\n ["5", "6", "7"]{{c2}}["8", "9", "0"]\n ]{{c1}}[\n ["1", "2", "3"]{{c2}}["4", "5", "6", "7"]{{c2}}["8", "9", "0"]\n ]\n]' }, + { + input: '[[["1","2"],["3","4"]],[["5","6","7"],["8","9","0"]],[["1","2","3"],["4","5","6","7"],["8","9","0"]]]', + output: '[\n [\n ["1", "2"]{{c2}}["3", "4"]\n ]{{c1}}[\n ["5", "6", "7"]{{c2}}["8", "9", "0"]\n ]{{c1}}[\n ["1", "2", "3"]{{c2}}["4", "5", "6", "7"]{{c2}}["8", "9", "0"]\n ]\n]' + }, { input: [ 'changeCollection.add({', @@ -234,8 +242,7 @@ exports.test_data = { }, { name: "Space in parens tests", description: "put space inside parens", - matrix: [ - { + matrix: [{ options: [ { name: "space_in_paren", value: "false" }, { name: "space_in_empty_paren", value: "false" }, @@ -264,8 +271,7 @@ exports.test_data = { s: ' ', e: ' ', }], - tests: [ - { + tests: [{ input: 'if(p) foo(a,b);', output: 'if ({{s}}p{{s}}) foo({{s}}a, b{{s}});' }, @@ -318,241 +324,234 @@ exports.test_data = { ], }, { name: "operator_position option - ensure no neswlines if preserve_newlines is false", - matrix: [ - { - options: [ - { name: "operator_position", value: "'before-newline'" }, - { name: "preserve_newlines", value: "false" } - ] - }, { - options: [ - { name: "operator_position", value: "'after-newline'" }, - { name: "preserve_newlines", value: "false" } - ] - }, { - options: [ - { name: "operator_position", value: "'preserve-newline'" }, - { name: "preserve_newlines", value: "false" } - ] - } - ], - tests: [ - { - unchanged: inputlib.operator_position.sanity - }, { - input: inputlib.operator_position.comprehensive, - output: inputlib.operator_position.sanity, - } - ] + matrix: [{ + options: [ + { name: "operator_position", value: "'before-newline'" }, + { name: "preserve_newlines", value: "false" } + ] + }, { + options: [ + { name: "operator_position", value: "'after-newline'" }, + { name: "preserve_newlines", value: "false" } + ] + }, { + options: [ + { name: "operator_position", value: "'preserve-newline'" }, + { name: "preserve_newlines", value: "false" } + ] + }], + tests: [{ + unchanged: inputlib.operator_position.sanity + }, { + input: inputlib.operator_position.comprehensive, + output: inputlib.operator_position.sanity, + }] }, { name: "operator_position option - set to 'before-newline' (default value)", - tests: [ - { - comment: 'comprehensive, various newlines', - input: inputlib.operator_position.comprehensive, - output: [ - 'var res = a + b -', - ' c /', - ' d * e %', - ' f;', - 'var res = g & h |', - ' i ^', - ' j;', - 'var res = (k &&', - ' l ||', - ' m) ?', - ' n :', - ' o;', - 'var res = p >>', - ' q <<', - ' r >>>', - ' s;', - 'var res = t', - '', - ' ===', - ' u !== v !=', - ' w ==', - ' x >=', - ' y <= z > aa <', - ' ab;', - 'ac +', - ' -ad' - ] - }, { - comment: 'colon special case', - input: inputlib.operator_position.colon_special_case, - output: [ - 'var a = {', - ' b: bval,', - ' c: cval,', - ' d: dval', - '};', - 'var e = f ? g :', - ' h;', - 'var i = j ? k :', - ' l;' - ] - }, { - comment: 'catch-all, includes brackets and other various code', - input: inputlib.operator_position.catch_all, - output: [ - 'var d = 1;', - 'if (a === b &&', - ' c) {', - ' d = (c * everything /', - ' something_else) %', - ' b;', - ' e', - ' += d;', - '', - '} else if (!(complex && simple) ||', - ' (emotion && emotion.name === "happy")) {', - ' cryTearsOfJoy(many ||', - ' anOcean ||', - ' aRiver);', - '}' - ] - } - ] + tests: [{ + comment: 'comprehensive, various newlines', + input: inputlib.operator_position.comprehensive, + output: [ + 'var res = a + b -', + ' c /', + ' d * e %', + ' f;', + 'var res = g & h |', + ' i ^', + ' j;', + 'var res = (k &&', + ' l ||', + ' m) ?', + ' n :', + ' o;', + 'var res = p >>', + ' q <<', + ' r >>>', + ' s;', + 'var res = t', + '', + ' ===', + ' u !== v !=', + ' w ==', + ' x >=', + ' y <= z > aa <', + ' ab;', + 'ac +', + ' -ad' + ] + }, { + comment: 'colon special case', + input: inputlib.operator_position.colon_special_case, + output: [ + 'var a = {', + ' b: bval,', + ' c: cval,', + ' d: dval', + '};', + 'var e = f ? g :', + ' h;', + 'var i = j ? k :', + ' l;' + ] + }, { + comment: 'catch-all, includes brackets and other various code', + input: inputlib.operator_position.catch_all, + output: [ + 'var d = 1;', + 'if (a === b &&', + ' c) {', + ' d = (c * everything /', + ' something_else) %', + ' b;', + ' e', + ' += d;', + '', + '} else if (!(complex && simple) ||', + ' (emotion && emotion.name === "happy")) {', + ' cryTearsOfJoy(many ||', + ' anOcean ||', + ' aRiver);', + '}' + ] + }] }, { name: "operator_position option - set to 'after_newline'", options: [{ - name: "operator_position", value: "'after-newline'" + name: "operator_position", + value: "'after-newline'" }], - tests: [ - { - comment: 'comprehensive, various newlines', - input: inputlib.operator_position.comprehensive, - output: [ - 'var res = a + b', - ' - c', - ' / d * e', - ' % f;', - 'var res = g & h', - ' | i', - ' ^ j;', - 'var res = (k', - ' && l', - ' || m)', - ' ? n', - ' : o;', - 'var res = p', - ' >> q', - ' << r', - ' >>> s;', - 'var res = t', - '', - ' === u !== v', - ' != w', - ' == x', - ' >= y <= z > aa', - ' < ab;', - 'ac', - ' + -ad' - ] - }, { - comment: 'colon special case', - input: inputlib.operator_position.colon_special_case, - output: [ - 'var a = {', - ' b: bval,', - ' c: cval,', - ' d: dval', - '};', - 'var e = f ? g', - ' : h;', - 'var i = j ? k', - ' : l;' - ] - }, { - comment: 'catch-all, includes brackets and other various code', - input: inputlib.operator_position.catch_all, - output: [ - 'var d = 1;', - 'if (a === b', - ' && c) {', - ' d = (c * everything', - ' / something_else)', - ' % b;', - ' e', - ' += d;', - '', - '} else if (!(complex && simple)', - ' || (emotion && emotion.name === "happy")) {', - ' cryTearsOfJoy(many', - ' || anOcean', - ' || aRiver);', - '}' - ] - } - ] + tests: [{ + comment: 'comprehensive, various newlines', + input: inputlib.operator_position.comprehensive, + output: [ + 'var res = a + b', + ' - c', + ' / d * e', + ' % f;', + 'var res = g & h', + ' | i', + ' ^ j;', + 'var res = (k', + ' && l', + ' || m)', + ' ? n', + ' : o;', + 'var res = p', + ' >> q', + ' << r', + ' >>> s;', + 'var res = t', + '', + ' === u !== v', + ' != w', + ' == x', + ' >= y <= z > aa', + ' < ab;', + 'ac', + ' + -ad' + ] + }, { + comment: 'colon special case', + input: inputlib.operator_position.colon_special_case, + output: [ + 'var a = {', + ' b: bval,', + ' c: cval,', + ' d: dval', + '};', + 'var e = f ? g', + ' : h;', + 'var i = j ? k', + ' : l;' + ] + }, { + comment: 'catch-all, includes brackets and other various code', + input: inputlib.operator_position.catch_all, + output: [ + 'var d = 1;', + 'if (a === b', + ' && c) {', + ' d = (c * everything', + ' / something_else)', + ' % b;', + ' e', + ' += d;', + '', + '} else if (!(complex && simple)', + ' || (emotion && emotion.name === "happy")) {', + ' cryTearsOfJoy(many', + ' || anOcean', + ' || aRiver);', + '}' + ] + }] }, { name: "operator_position option - set to 'preserve-newline'", options: [{ - name: "operator_position", value: "'preserve-newline'" + name: "operator_position", + value: "'preserve-newline'" }], - tests: [ - { - comment: 'comprehensive, various newlines', - input: inputlib.operator_position.comprehensive, - output: [ - 'var res = a + b', - ' - c /', - ' d * e', - ' %', - ' f;', - 'var res = g & h', - ' | i ^', - ' j;', - 'var res = (k &&', - ' l', - ' || m) ?', - ' n', - ' : o;', - 'var res = p', - ' >> q <<', - ' r', - ' >>> s;', - 'var res = t', - '', - ' === u !== v', - ' !=', - ' w', - ' == x >=', - ' y <= z > aa <', - ' ab;', - 'ac +', - ' -ad' - ] - }, { - comment: 'colon special case', - input: inputlib.operator_position.colon_special_case, - output: [ - 'var a = {', - ' b: bval,', - ' c: cval,', - ' d: dval', - '};', - 'var e = f ? g', - ' : h;', - 'var i = j ? k :', - ' l;' - ] - }, { - comment: 'catch-all, includes brackets and other various code', - unchanged: inputlib.operator_position.catch_all - } - ] + tests: [{ + comment: 'comprehensive, various newlines', + input: inputlib.operator_position.comprehensive, + output: [ + 'var res = a + b', + ' - c /', + ' d * e', + ' %', + ' f;', + 'var res = g & h', + ' | i ^', + ' j;', + 'var res = (k &&', + ' l', + ' || m) ?', + ' n', + ' : o;', + 'var res = p', + ' >> q <<', + ' r', + ' >>> s;', + 'var res = t', + '', + ' === u !== v', + ' !=', + ' w', + ' == x >=', + ' y <= z > aa <', + ' ab;', + 'ac +', + ' -ad' + ] + }, { + comment: 'colon special case', + input: inputlib.operator_position.colon_special_case, + output: [ + 'var a = {', + ' b: bval,', + ' c: cval,', + ' d: dval', + '};', + 'var e = f ? g', + ' : h;', + 'var i = j ? k :', + ' l;' + ] + }, { + comment: 'catch-all, includes brackets and other various code', + unchanged: inputlib.operator_position.catch_all + }] }, { name: "Async / await tests", description: "ES7 async / await tests", tests: [ { input: "async function foo() {}" }, { input: "let w = async function foo() {}" }, - { input: "async function foo() {}\nvar x = await foo();"}, + { input: "async function foo() {}\nvar x = await foo();" }, { comment: "async function as an input to another function", - input: "wrapper(async function foo() {})"}, + input: "wrapper(async function foo() {})" + }, { comment: "await on inline anonymous function. should have a space after await", input_: "async function() {\n var w = await(async function() {\n return await foo();\n })();\n}", @@ -580,10 +579,12 @@ exports.test_data = { { comment: 'Handles inline expressions', input: 'xml=<{a} b="c">\n foox;', - output: 'xml = <{a} b="c">\n foox;' }, + output: 'xml = <{a} b="c">\n foox;' + }, { input: 'xml=<{a} b="c">\n \n foox;', - output: 'xml = <{a} b="c">\n \n foox;' }, + output: 'xml = <{a} b="c">\n \n foox;' + }, { comment: 'xml literals with special characters in elem names - see http://www.w3.org/TR/REC-xml/#NT-NameChar', unchanged: 'xml = <_:.valid.xml- _:.valid.xml-="123"/>;' @@ -596,14 +597,14 @@ exports.test_data = { { comment: 'Handles CDATA', input: 'xml=\n foox/]]>;', - output: 'xml = \n foox/]]>;' }, + output: 'xml = \n foox/]]>;' + }, { input: 'xml=;', output: 'xml = ;' }, { input: 'xml=;', output: 'xml = ;' }, { comment: 'JSX - working jsx from http://prettydiff.com/unit_tests/beautification_javascript_jsx.txt', - unchanged: - [ + unchanged: [ 'var ListItem = React.createClass({', ' render: function() {', ' return (', @@ -618,8 +619,7 @@ exports.test_data = { ] }, { - unchanged: - [ + unchanged: [ 'var List = React.createClass({', ' renderList: function() {', ' return this.props.items.map(function(item) {', @@ -636,8 +636,7 @@ exports.test_data = { ] }, { - unchanged: - [ + unchanged: [ 'var Mist = React.createClass({', ' renderList: function() {', ' return this.props.items.map(function(item) {', @@ -648,8 +647,7 @@ exports.test_data = { ] }, { - unchanged: - [ + unchanged: [ '// JSX', 'var box = ', ' {shouldShowAnswer(user) ?', @@ -670,8 +668,7 @@ exports.test_data = { ] }, { - unchanged: - [ + unchanged: [ 'var Timer = React.createClass({', ' getInitialState: function() {', ' return {', @@ -699,8 +696,7 @@ exports.test_data = { ] }, { - unchanged: - [ + unchanged: [ 'var TodoList = React.createClass({', ' render: function() {', ' var createItem = function(itemText) {', @@ -712,8 +708,7 @@ exports.test_data = { ] }, { - unchanged: - [ + unchanged: [ 'var TodoApp = React.createClass({', ' getInitialState: function() {', ' return {', @@ -752,8 +747,7 @@ exports.test_data = { ] }, { - input: - [ + input: [ 'var converter = new Showdown.converter();', 'var MarkdownEditor = React.createClass({', ' getInitialState: function() {', @@ -784,8 +778,7 @@ exports.test_data = { 'React.render(, mountNode);' ], - output: - [ + output: [ 'var converter = new Showdown.converter();', 'var MarkdownEditor = React.createClass({', ' getInitialState: function() {', @@ -822,8 +815,7 @@ exports.test_data = { }, { comment: 'JSX - Not quite correct jsx formatting that still works', - input: - [ + input: [ 'var content = (', '