mirror of
https://github.com/beautifier/js-beautify.git
synced 2025-02-15 02:38:04 +00:00
switch/case formatting vastly improved (that means, finally fixed)
This commit is contained in:
parent
25fa7dde1b
commit
2aec84e84d
@ -3,7 +3,6 @@
|
||||
|
||||
require_once('beautify.php');
|
||||
|
||||
$tests_done = 0;
|
||||
$tests_passed = 0;
|
||||
$tests_failed = 0;
|
||||
|
||||
@ -90,7 +89,7 @@ bt('try{a();}catch(b){c();}finally{d();}', "try {\n a();\n} catch(b) {\n c
|
||||
bt('(xx)()'); // magic function call
|
||||
bt('a[1]()'); // another magic function call
|
||||
bt('if(a){b();}else if(', "if (a) {\n b();\n} else if (");
|
||||
|
||||
bt('switch(x) {case 0: case 1: a(); break; default: break}', "switch (x) {\ncase 0:\ncase 1:\n a();\n break;\ndefault:\n break\n}");
|
||||
// known problems:
|
||||
# bt('if(a)if(b)break', "if (a)\n if (b)\n break"); // won't fix, at least now
|
||||
|
||||
|
53
beautify.php
53
beautify.php
@ -58,9 +58,10 @@ function js_beautify($js_source_text, $tab_size = 4)
|
||||
$input = $js_source_text;
|
||||
$input_length = strlen($input);
|
||||
|
||||
$last_word = ''; // last TK_WORD passed
|
||||
$last_type = TK_EOF; // last token type
|
||||
$output = '';
|
||||
$last_word = ''; // last TK_WORD passed
|
||||
$last_type = TK_EOF; // last token type
|
||||
$last_text = ''; // last token text
|
||||
$output = '';
|
||||
|
||||
// words which should always start on new line.
|
||||
// simple hack for cases when lines aren't ending with semicolon.
|
||||
@ -74,9 +75,8 @@ function js_beautify($js_source_text, $tab_size = 4)
|
||||
|
||||
|
||||
$indent = 0;
|
||||
|
||||
$pos = 0; // parser position
|
||||
|
||||
$in_case = false; // flag for parser that case/default has been processed, and next colon needs special attention
|
||||
|
||||
while (true) {
|
||||
list($token_text, $token_type) = get_next_token($pos);
|
||||
@ -134,7 +134,21 @@ function js_beautify($js_source_text, $tab_size = 4)
|
||||
break;
|
||||
|
||||
case TK_WORD:
|
||||
|
||||
|
||||
if ($token_text == 'case' or $token_text == 'default') {
|
||||
if ($last_text == ':') {
|
||||
// switch cases following one another
|
||||
remove_indent();
|
||||
} else {
|
||||
$indent--;
|
||||
nl();
|
||||
$indent++;
|
||||
}
|
||||
token();
|
||||
$in_case = true;
|
||||
break;
|
||||
}
|
||||
|
||||
$prefix = PRINT_NONE;
|
||||
if ($last_type == TK_END_BLOCK) {
|
||||
if (!in_array(strtolower($token_text), array('else', 'catch', 'finally'))) {
|
||||
@ -161,7 +175,7 @@ function js_beautify($js_source_text, $tab_size = 4)
|
||||
|
||||
if (in_array($token_text, $line_starters) or $prefix == PRINT_NL) {
|
||||
if ($last_type != TK_END_EXPR) {
|
||||
if ($last_type != TK_START_EXPR or $token_text != 'var') { // no need to force newline on 'var': for (var x = 0...)
|
||||
if (($last_type != TK_START_EXPR or $token_text != 'var') and $last_text != ':') { // no need to force newline on 'var': for (var x = 0...)
|
||||
if ($token_text == 'if' and $last_type == TK_WORD and $last_word == 'else') {
|
||||
// no newline for } else if {
|
||||
space();
|
||||
@ -195,6 +209,18 @@ function js_beautify($js_source_text, $tab_size = 4)
|
||||
case TK_PUNCT:
|
||||
$start_delim = true;
|
||||
$end_delim = true;
|
||||
|
||||
if ($token_text == ':' and $in_case) {
|
||||
token(); // colon really asks for separate treatment
|
||||
nl();
|
||||
$expecting_case = false;
|
||||
break;
|
||||
}
|
||||
|
||||
$in_case = false;
|
||||
|
||||
|
||||
|
||||
if ($token_text == ',') {
|
||||
if ($in == IN_EXPR) {
|
||||
token();
|
||||
@ -226,7 +252,6 @@ function js_beautify($js_source_text, $tab_size = 4)
|
||||
// zz: xx
|
||||
// can't differentiate ternary op, so for now it's a ? b: c; without space before colon
|
||||
$start_delim = false;
|
||||
} elseif ($last_type == TK_WORD) {
|
||||
}
|
||||
if ($start_delim) {
|
||||
space();
|
||||
@ -260,7 +285,8 @@ function js_beautify($js_source_text, $tab_size = 4)
|
||||
break;
|
||||
}
|
||||
|
||||
$last_type = $token_type;
|
||||
$last_type = $token_type;
|
||||
$last_text = $token_text;
|
||||
}
|
||||
|
||||
return $output;
|
||||
@ -314,6 +340,15 @@ function unindent()
|
||||
}
|
||||
|
||||
|
||||
function remove_indent()
|
||||
{
|
||||
global $tab_string, $output;
|
||||
$tab_string_len = strlen($tab_string);
|
||||
if (substr($output, -$tab_string_len) == $tab_string) {
|
||||
$output = substr($output, 0, -$tab_string_len);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function in($where)
|
||||
{
|
||||
|
41
index.php
41
index.php
@ -70,36 +70,12 @@ window.onload = function() {
|
||||
</script>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<style type="text/css">
|
||||
form {
|
||||
margin: 0 10px 0 10px;
|
||||
}
|
||||
textarea {
|
||||
width: 100%;
|
||||
height: 320px;
|
||||
border: 1px solid #ccc;
|
||||
padding: 3px;
|
||||
font-family: liberation mono, consolas, courier new, courier, monospace;
|
||||
font-size: 12px;
|
||||
}
|
||||
h1 {
|
||||
font-family: trebuchet ms, arial, sans-serif;
|
||||
font-weight: normal;
|
||||
font-size: 28px;
|
||||
color: #666;
|
||||
margin-bottom: 15px;
|
||||
border-bottom: 1px solid #666;
|
||||
}
|
||||
button { width: 100%;}
|
||||
code, .code {
|
||||
font-family: liberation mono, consolas, lucida console, courier new, courier, monospace;
|
||||
font-size: 12px;
|
||||
}
|
||||
pre {
|
||||
font-size: 12px;
|
||||
font-family: liberation mono, consolas, courier new, courier, monospace;
|
||||
margin-left: 20px;
|
||||
color: #777;
|
||||
}
|
||||
form { margin: 0 10px 0 10px }
|
||||
textarea { width: 100%; height: 320px; border: 1px solid #ccc; padding: 3px; font-family: liberation mono, consolas, courier new, courier, monospace; font-size: 12px; }
|
||||
h1 { font-family: trebuchet ms, arial, sans-serif; font-weight: normal; font-size: 28px; color: #666; margin-bottom: 15px; border-bottom: 1px solid #666; }
|
||||
button { width: 100%; cursor: pointer;}
|
||||
code, .code { font-family: liberation mono, consolas, lucida console, courier new, courier, monospace; font-size: 12px; }
|
||||
pre { font-size: 12px; font-family: liberation mono, consolas, courier new, courier, monospace; margin-left: 20px; color: #777; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
@ -120,15 +96,16 @@ var latest_changes=new Object(
|
||||
'... who cares ...',
|
||||
'2007-02-08':
|
||||
'Initial release'});
|
||||
var a=b?(c%d):e[f];
|
||||
HTML
|
||||
);
|
||||
|
||||
?></textarea><br />
|
||||
<button type="submit">Beautify</button>
|
||||
<p>This script is useful to explore ugly javascripts, e.g <a href="http://createwebapp.com/javascripts/autocomplete.js">compacted in one line</a>. All the other beautifiers mostly suck.</p>
|
||||
<p>This script was intended to explore ugly javascripts, e.g <a href="http://createwebapp.com/javascripts/autocomplete.js">compacted in one line</a>, but you may want to pretty-format your own javascripts too, and they'll get nice and shiny.</p>
|
||||
<p>PHP source can be <a href="beautify.phps">seen online here</a> or fetched from subversion repository at <a href="svn://edev.uk.to/beautify/">svn://edev.uk.to/beautify</a>. Feel free to use and abuse.</p>
|
||||
<p>In case of glitches you may wish to tell me about them—<code>elfz<span style="color:#999">[at]</span>laacz<span style="color:#999">[dot]</span>lv</code></p>
|
||||
<p>Jia Liu has <a href="http://ayueer.spaces.live.com/Blog/cns!9E99E1260983291B!1136.entry">translated this to Ruby,</a> if you're into that kind of thing (the page is in chinese, though, and the version of beautifier is kind of obsolete already).</p>
|
||||
<p>Jia Liu has <a href="http://ayueer.spaces.live.com/Blog/cns!9E99E1260983291B!1136.entry">translated this to Ruby,</a> if you're into that kind of thing (the page is in chinese, though, and the version of beautifier is obsolete already).</p>
|
||||
</form>
|
||||
<?php
|
||||
if (file_exists('.svnlog')) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user