Skip to content

Commit

Permalink
Beautify and jshint initial run on project files
Browse files Browse the repository at this point in the history
Fixes #336
  • Loading branch information
bitwiseman committed Feb 4, 2016
1 parent 9680b5f commit aeaa642
Show file tree
Hide file tree
Showing 25 changed files with 969 additions and 887 deletions.
13 changes: 6 additions & 7 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,5 +51,4 @@ if (typeof define === "function" && define.amd) {
mod.exports = get_beautify(js_beautify, css_beautify, html_beautify);

})(module);
}

}
22 changes: 11 additions & 11 deletions js/lib/beautify-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || ' ';
Expand All @@ -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
Expand Down Expand Up @@ -189,7 +189,7 @@
// pseudoclasses can contain ()
openParen += 1;
} else if (ch === ')') {
if (openParen == 0) {
if (openParen === 0) {
return false;
}
openParen -= 1;
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -423,7 +423,7 @@
print.preserveSingleSpace();
output.push(ch);
} else if (ch === '=') { // no whitespace before or after
eatWhitespace()
eatWhitespace();
ch = '=';
output.push(ch);
} else {
Expand All @@ -445,7 +445,7 @@
sweetCode += '\n';
}

if (eol != '\n') {
if (eol !== '\n') {
sweetCode = sweetCode.replace(/[\n]/g, eol);
}

Expand Down Expand Up @@ -488,4 +488,4 @@
global.css_beautify = css_beautify;
}

}());
}());
78 changes: 39 additions & 39 deletions js/lib/beautify-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -107,15 +107,15 @@

// 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;
}

indent_inner_html = (options.indent_inner_html === undefined) ? false : options.indent_inner_html;
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',
Expand All @@ -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() {

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
}

Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -979,7 +979,7 @@
sweet_code += '\n';
}

if (eol != '\n') {
if (eol !== '\n') {
sweet_code = sweet_code.replace(/[\n]/g, eol);
}

Expand All @@ -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") {
Expand All @@ -1019,4 +1019,4 @@
};
}

}());
}());
Loading

0 comments on commit aeaa642

Please sign in to comment.