Skip to content

Commit

Permalink
Make js-beautifier kinder to templateing
Browse files Browse the repository at this point in the history
Related to #490, #417
  • Loading branch information
bitwiseman committed Jun 16, 2015
1 parent b7b5f89 commit 13b8031
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 12 deletions.
12 changes: 12 additions & 0 deletions js/lib/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,8 @@
var directives_pattern = /\/\*\sbeautify\s(\w+[:]\w+)+\s\*\//g;
var directives_end_ignore_pattern = /([\s\S]*?)((?:\/\*\sbeautify\signore:end\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;

Expand Down Expand Up @@ -1929,6 +1931,16 @@
}
}

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) {
c = template_match[0];
parser_pos += c.length - 1;
return [c, 'TK_STRING'];
}
}

if (c === '<' && input.substring(parser_pos - 1, parser_pos + 3) === '<!--') {
parser_pos += 3;
c = '<!--';
Expand Down
17 changes: 14 additions & 3 deletions js/test/beautify-javascript-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,20 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
'};');



// Template Formatting
bt('<?=$view["name"]; ?>');
bt('a = <?= external() ?>;');
bt(
'<?php\n' +
'for($i = 1; $i <= 100; $i++;) {\n' +
' #count to 100!\n' +
' echo($i . "</br>");\n' +
'}\n' +
'?>');
bt('a = <%= external() %>;');


// jslint and space after anon function - (f = " ", c = "")
opts.jslint_happy = true;
opts.space_after_anon_function = true;
Expand Down Expand Up @@ -1621,9 +1635,6 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
beautify_brace_tests('end-expand');
beautify_brace_tests('none');

bt('a = <?= external() ?> ;'); // not the most perfect thing in the world, but you're the weirdo beaufifying php mix-ins with javascript beautifier
bt('a = <%= external() %> ;');

bt('// func-comment\n\nfunction foo() {}\n\n// end-func-comment');

test_fragment('roo = {\n /*\n ****\n FOO\n ****\n */\n BAR: 0\n};');
Expand Down
9 changes: 9 additions & 0 deletions python/jsbeautifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,7 @@ def __init__ (self, input, opts, indent_string):
self.directives_pattern = re.compile('\/\*\sbeautify\s(\w+[:]\w+)+\s\*\/')
self.directives_end_ignore_pattern = re.compile('([\s\S]*?)((?:\/\*\sbeautify\signore:end\s\*\/)|$)')

self.template_pattern = re.compile('((<\?php|<\?=)[\s\S]*?\?>)|(<%[\s\S]*?%>)')

def tokenize(self):
self.in_html_comment = False
Expand Down Expand Up @@ -1732,6 +1733,14 @@ def __tokenize_next(self):
self.parser_pos += 2
return sharp, 'TK_WORD'

if c == '<' and self.input[self.parser_pos] in ['?', '%']:
template_match = self.template_pattern.match(self.input, self.parser_pos - 1);
if template_match:
c = template_match.group(0)
self.parser_pos += len(c) - 1
return c, 'TK_STRING'


if c == '<' and self.input[self.parser_pos - 1 : self.parser_pos + 3] == '<!--':
self.parser_pos += 3
c = '<!--'
Expand Down
15 changes: 12 additions & 3 deletions python/jsbeautifier/tests/testjsbeautifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,18 @@ def unicode_char(value):
'/* beautify ignore:end */\n' +
'};')

# Template Formatting
bt('<?=$view["name"]; ?>')
bt('a = <?= external() ?>;')
bt(
'<?php\n' +
'for($i = 1; $i <= 100; $i++;) {\n' +
' #count to 100!\n' +
' echo($i . "</br>");\n' +
'}\n' +
'?>')
bt('a = <%= external() %>;')

# jslint and space after anon function - (f = " ", c = "")
self.options.jslint_happy = true
self.options.space_after_anon_function = true
Expand Down Expand Up @@ -1769,9 +1781,6 @@ def unicode_char(value):

self.options.brace_style = 'collapse';

bt('a = <?= external() ?> ;') # not the most perfect thing in the world, but you're the weirdo beaufifying php mix-ins with javascript beautifier
bt('a = <%= external() %> ;')

test_fragment('roo = {\n /*\n ****\n FOO\n ****\n */\n BAR: 0\n};')
test_fragment("if (zz) {\n // ....\n}\n(function")

Expand Down
20 changes: 20 additions & 0 deletions test/data/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,26 @@ exports.test_data = {
}
]
},
{
name: "Template Formatting",
description: "Php (<?php ... ?>) and underscore.js templating treated as strings.",
options: [],
tests: [
{ unchanged: '<?=$view["name"]; ?>' },
{ unchanged: 'a = <?= external() ?>;' },
{ unchanged:
[
'<?php',
'for($i = 1; $i <= 100; $i++;) {',
' #count to 100!',
' echo($i . "</br>");',
'}',
'?>'
]
},
{ unchanged: 'a = <%= external() %>;' }
]
},
{
name: "jslint and space after anon function",
description: "jslint_happy and space_after_anon_function tests",
Expand Down
3 changes: 0 additions & 3 deletions test/template/node-javascript.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,6 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
beautify_brace_tests('end-expand');
beautify_brace_tests('none');

bt('a = <?= external() ?> ;'); // not the most perfect thing in the world, but you're the weirdo beaufifying php mix-ins with javascript beautifier
bt('a = <%= external() %> ;');

bt('// func-comment\n\nfunction foo() {}\n\n// end-func-comment');

test_fragment('roo = {\n /*\n ****\n FOO\n ****\n */\n BAR: 0\n};');
Expand Down
3 changes: 0 additions & 3 deletions test/template/python-javascript.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,6 @@ class TestJSBeautifier(unittest.TestCase):

self.options.brace_style = 'collapse';

bt('a = <?= external() ?> ;') # not the most perfect thing in the world, but you're the weirdo beaufifying php mix-ins with javascript beautifier
bt('a = <%= external() %> ;')

test_fragment('roo = {\n /*\n ****\n FOO\n ****\n */\n BAR: 0\n};')
test_fragment("if (zz) {\n // ....\n}\n(function")

Expand Down

0 comments on commit 13b8031

Please sign in to comment.