mirror of
https://github.com/beautifier/js-beautify.git
synced 2025-02-20 13:12:40 +00:00
Merge branch 'master' into add-tests
This commit is contained in:
commit
74009348f2
@ -87,6 +87,8 @@ punct = punct.replace(/[-[\]{}()*+?.,\\^$|#]/g, "\\$&");
|
||||
punct = punct.replace(/ /g, '|');
|
||||
|
||||
var punct_pattern = new RegExp(punct, 'g');
|
||||
var shebang_pattern = /#![^\n\r\u2028\u2029]*(?:\r\n|[\n\r\u2028\u2029])?/g;
|
||||
var include_pattern = /#include[^\n\r\u2028\u2029]*(?:\r\n|[\n\r\u2028\u2029])?/g;
|
||||
|
||||
// words which should always start on new line.
|
||||
var line_starters = 'continue,try,throw,return,var,let,const,if,switch,case,default,for,while,break,function,import,export'.split(',');
|
||||
@ -213,18 +215,23 @@ Tokenizer.prototype._read_non_javascript = function(c) {
|
||||
var resulting_string = '';
|
||||
|
||||
if (c === '#') {
|
||||
c = this._input.next();
|
||||
if (this._is_first_token()) {
|
||||
resulting_string = this._input.read(shebang_pattern);
|
||||
|
||||
if (this._is_first_token() && this._input.peek() === '!') {
|
||||
// shebang
|
||||
resulting_string = c;
|
||||
while (this._input.hasNext() && c !== '\n') {
|
||||
c = this._input.next();
|
||||
resulting_string += c;
|
||||
if (resulting_string) {
|
||||
return this._create_token(TOKEN.UNKNOWN, resulting_string.trim() + '\n');
|
||||
}
|
||||
}
|
||||
|
||||
// handles extendscript #includes
|
||||
resulting_string = this._input.read(include_pattern);
|
||||
|
||||
if (resulting_string) {
|
||||
return this._create_token(TOKEN.UNKNOWN, resulting_string.trim() + '\n');
|
||||
}
|
||||
|
||||
c = this._input.next();
|
||||
|
||||
// Spidermonkey-specific sharp variables for circular references. Considered obsolete.
|
||||
var sharp = '#';
|
||||
if (this._input.hasNext() && this._input.testChar(digit)) {
|
||||
|
@ -5540,6 +5540,21 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
|
||||
'/* comment */');
|
||||
test_fragment('#');
|
||||
test_fragment('#!');
|
||||
test_fragment('#include');
|
||||
test_fragment('#include "settings.jsxinc"');
|
||||
test_fragment(
|
||||
'#include "settings.jsxinc"\n' +
|
||||
'\n' +
|
||||
'\n' +
|
||||
'/* comment */');
|
||||
test_fragment(
|
||||
'#include "settings.jsxinc"\n' +
|
||||
'\n' +
|
||||
'\n' +
|
||||
'#include "settings.jsxinc"\n' +
|
||||
'\n' +
|
||||
'\n' +
|
||||
'/* comment */');
|
||||
bt('function namespace::something()');
|
||||
test_fragment(
|
||||
'<!--\n' +
|
||||
|
@ -78,6 +78,8 @@ punct = re.compile(r'([-[\]{}()*+?.,\\^$|#])').sub(r'\\\1', punct)
|
||||
punct = punct.replace(' ', '|')
|
||||
|
||||
punct_pattern = re.compile(punct)
|
||||
shebang_pattern = re.compile(r'#![^\n]*(?:\r\n|[\n\r\u2028\u2029])?')
|
||||
include_pattern = re.compile(r'#include[^\n\r\u2028\u2029]*(?:\r\n|[\n\r\u2028\u2029])?')
|
||||
|
||||
# Words which always should start on a new line
|
||||
line_starters = frozenset(
|
||||
@ -344,15 +346,21 @@ class Tokenizer(BaseTokenizer):
|
||||
resulting_string = ''
|
||||
|
||||
if c == '#':
|
||||
c = self._input.next()
|
||||
|
||||
# she-bang
|
||||
if self._is_first_token() and self._input.peek() == '!':
|
||||
resulting_string = c
|
||||
while self._input.hasNext() and c != '\n':
|
||||
c = self._input.next()
|
||||
resulting_string += c
|
||||
if self._is_first_token():
|
||||
resulting_string = self._input.read(shebang_pattern)
|
||||
if resulting_string:
|
||||
return self._create_token(TOKEN.UNKNOWN, resulting_string.strip() + '\n')
|
||||
|
||||
# handles extendscript #includes
|
||||
resulting_string = self._input.read(include_pattern)
|
||||
|
||||
if resulting_string:
|
||||
return self._create_token(TOKEN.UNKNOWN, resulting_string.strip() + '\n')
|
||||
|
||||
c = self._input.next()
|
||||
|
||||
# Spidermonkey-specific sharp variables for circular references
|
||||
# https://developer.mozilla.org/En/Sharp_variables_in_JavaScript
|
||||
# http://mxr.mozilla.org/mozilla-central/source/js/src/jsscan.cpp
|
||||
|
@ -5286,6 +5286,21 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
'/* comment */')
|
||||
test_fragment('#')
|
||||
test_fragment('#!')
|
||||
test_fragment('#include')
|
||||
test_fragment('#include "settings.jsxinc"')
|
||||
test_fragment(
|
||||
'#include "settings.jsxinc"\n' +
|
||||
'\n' +
|
||||
'\n' +
|
||||
'/* comment */')
|
||||
test_fragment(
|
||||
'#include "settings.jsxinc"\n' +
|
||||
'\n' +
|
||||
'\n' +
|
||||
'#include "settings.jsxinc"\n' +
|
||||
'\n' +
|
||||
'\n' +
|
||||
'/* comment */')
|
||||
bt('function namespace::something()')
|
||||
test_fragment(
|
||||
'<!--\n' +
|
||||
|
@ -3343,6 +3343,10 @@ exports.test_data = {
|
||||
{ fragment: true, unchanged: "#!she/bangs, she bangs\n\n\n/* comment */" },
|
||||
{ fragment: true, unchanged: "#" },
|
||||
{ fragment: true, unchanged: "#!" },
|
||||
{ fragment: true, unchanged: "#include" },
|
||||
{ fragment: true, unchanged: '#include "settings.jsxinc"' },
|
||||
{ fragment: true, unchanged: '#include "settings.jsxinc"\n\n\n/* comment */' },
|
||||
{ fragment: true, unchanged: '#include "settings.jsxinc"\n\n\n#include "settings.jsxinc"\n\n\n/* comment */' },
|
||||
|
||||
{ unchanged: "function namespace::something()" },
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user