Skip to content

Commit

Permalink
Merge pull request #406 from akalongman/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
akalongman authored Oct 30, 2019
2 parents 0a17fd1 + a45c7db commit 7115958
Show file tree
Hide file tree
Showing 33 changed files with 1,485 additions and 1,991 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
language: python

dist: trusty

python:
- 3.3

Expand Down
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,7 @@ Language specific options:
"wrap_line_length": 0, // Wrap lines at next opportunity after N characters
"break_chained_methods": false, // Break chained method calls across subsequent lines
"end_with_newline": false, // Add new line at end of file
"comma_first": false, // Add comma first
"space_after_anon_function": false, // Add a space before an anonymous function's parens, ie. function ()
"space_after_named_function": false, // Add a space before a named function's parens, i.e. function example ()
"unindent_chained_methods": false, // Don't indent chained method calls
"operator_position": "before-newline" // Set operator position (before-newline|after-newline|preserve-newline) [before-newline]
"comma_first": false // Add comma first
}
```

Expand Down
8 changes: 0 additions & 8 deletions codeformatter/jsformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,6 @@ def format(self, text):
else:
options.space_after_anon_function = False

if (
'space_after_named_function' in self.opts and
self.opts['space_after_named_function']
):
options.space_after_named_function = True
else:
options.space_after_named_function = False

if (
'unindent_chained_methods' in self.opts and
self.opts['unindent_chained_methods']
Expand Down
2 changes: 1 addition & 1 deletion codeformatter/lib/cssbeautifier/css/beautifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
import copy
from .options import BeautifierOptions
from jsbeautifier.core.options import _mergeOpts as mergeOpts
from jsbeautifier.core.options import mergeOpts
from jsbeautifier.core.output import Output
from jsbeautifier.__version__ import __version__

Expand Down
100 changes: 39 additions & 61 deletions codeformatter/lib/jsbeautifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#
# The MIT License (MIT)

# Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
# Copyright (c) 2007-2017 Einar Lielmanis, Liam Newman, and contributors.

# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
Expand All @@ -37,9 +37,9 @@
# SOFTWARE.
#
# Originally written by Einar Lielmanis et al.,
# Conversion to python by Einar Lielmanis, einar@beautifier.io,
# Conversion to python by Einar Lielmanis, einar@jsbeautifier.org,
# Parsing improvement for brace-less and semicolon-less statements
# by Liam Newman <bitwiseman@beautifier.io>
# by Liam Newman <bitwiseman@gmail.com>
# Python is not my native language, feel free to push things around.
#
# Use either from command line (script displays its usage when run
Expand All @@ -62,18 +62,14 @@
# Here are the available options: (read source)


class MissingInputStreamError(Exception):
pass

def default_options():
return BeautifierOptions()


def beautify(string, opts=default_options()):
def beautify(string, opts = default_options() ):
b = Beautifier()
return b.beautify(string, opts)


def set_file_editorconfig_opts(filename, js_options):
from editorconfig import get_properties, EditorConfigError
try:
Expand All @@ -91,8 +87,7 @@ def set_file_editorconfig_opts(filename, js_options):
if _ecoptions.get("max_line_length") == "off":
js_options.wrap_line_length = 0
else:
js_options.wrap_line_length = int(
_ecoptions["max_line_length"])
js_options.wrap_line_length = int(_ecoptions["max_line_length"])

if _ecoptions.get("insert_final_newline") == 'true':
js_options.end_with_newline = True
Expand All @@ -107,18 +102,24 @@ def set_file_editorconfig_opts(filename, js_options):
elif _ecoptions["end_of_line"] == "crlf":
js_options.eol = '\r\n'

except EditorConfigError:
except EditorConfigError as ex:
# do not error on bad editor config
print("Error loading EditorConfig. Ignoring.", file=sys.stderr)

def beautify_file(file_name, opts=default_options()):
input_string = ''
if file_name == '-': # stdin
if sys.stdin.isatty():
raise MissingInputStreamError()

stream = sys.stdin
input_string = ''.join(stream.readlines())
def beautify_file(file_name, opts = default_options() ):
input_string = ''
if file_name == '-': # stdin
try:
if sys.stdin.isatty():
raise Exception()

stream = sys.stdin
input_string = ''.join(stream.readlines())
except Exception as ex:
print("Must pipe input or define at least one file.", file=sys.stderr)
usage(sys.stderr)
raise Exception()
else:
stream = io.open(file_name, 'rt', newline='')
input_string = ''.join(stream.readlines())
Expand All @@ -130,11 +131,12 @@ def usage(stream=sys.stdout):

print("jsbeautifier.py@" + __version__ + """
Javascript beautifier (https://beautifier.io/)
Javascript beautifier (http://jsbeautifier.org/)
Usage: jsbeautifier.py [options] <infile>
<infile> can be "-", which means stdin.
<outfile> defaults to stdout
Input options:
Expand All @@ -151,20 +153,17 @@ def usage(stream=sys.stdout):
-P, --space-in-paren Add padding spaces within paren, ie. f( a, b )
-E, --space-in-empty-paren Add a single space inside empty paren, ie. f( )
-j, --jslint-happy More jslint-compatible output
-a, --space-after-anon-function Add a space before an anonymous function's parens, ie. function ()
--space-after-named-function Add a space before a named function's parens, i.e. function example ()
-a, --space_after_anon_function Add a space before an anonymous function's parens, ie. function ()
-b, --brace-style=collapse Brace style (collapse, expand, end-expand, none)(,preserve-inline)
-k, --keep-array-indentation Keep array indentation.
-r, --replace Write output in-place, replacing input
-o, --outfile=FILE Specify a file to output to (default stdout)
-f, --keep-function-indentation Do not re-indent function bodies defined in var lines.
-x, --unescape-strings Decode printable chars encoded in \\xNN notation.
-X, --e4x Pass E4X xml literals through untouched
-C, --comma-first Put commas at the beginning of new line instead of end.
-O, --operator-position=STRING Set operator position (before-newline, after-newline, preserve-newline)
-w, --wrap-line-length Attempt to wrap line when it exceeds this length.
-w, --wrap-line-length Attempt to wrap line when it exceeds this length.
NOTE: Line continues until next wrap point is found.
-n, --end-with-newline End output with newline
-n, --end_with_newline End output with newline
--editorconfig Enable setting configuration from EditorConfig
Rarely needed options:
Expand All @@ -176,7 +175,7 @@ def usage(stream=sys.stdout):
-l, --indent-level=NUMBER Initial indentation level. (default 0).
-h, --help, --usage Prints this help statement.
-v, --version Show the version
-v, --version Show the version
""", file=stream)
if stream == sys.stderr:
Expand All @@ -189,22 +188,19 @@ def mkdir_p(path):
try:
if path:
os.makedirs(path)
except OSError as exc: # Python >2.5
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise Exception()




def isFileDifferent(filepath, expected):
try:
return (
''.join(
io.open(
filepath,
'rt',
newline='').readlines()) != expected)
except BaseException:
return (''.join(io.open(filepath, 'rt', newline='').readlines()) != expected)
except:
return True


Expand All @@ -214,11 +210,11 @@ def main():

try:
opts, args = getopt.getopt(argv, "s:c:e:o:rdEPjabkil:xhtfvXnCO:w:",
['indent-size=', 'indent-char=', 'eol=', 'outfile=', 'replace', 'disable-preserve-newlines',
'space-in-paren', 'space-in-empty-paren', 'jslint-happy', 'space-after-anon-function',
'brace-style=', 'keep-array-indentation', 'indent-level=', 'unescape-strings',
'help', 'usage', 'stdin', 'eval-code', 'indent-with-tabs', 'keep-function-indentation', 'version',
'e4x', 'end-with-newline', 'comma-first', 'operator-position=', 'wrap-line-length', 'editorconfig', 'space-after-named-function'])
['indent-size=','indent-char=','eol=''outfile=', 'replace', 'disable-preserve-newlines',
'space-in-paren', 'space-in-empty-paren', 'jslint-happy', 'space-after-anon-function',
'brace-style=', 'keep-array-indentation', 'indent-level=', 'unescape-strings',
'help', 'usage', 'stdin', 'eval-code', 'indent-with-tabs', 'keep-function-indentation', 'version',
'e4x', 'end-with-newline','comma-first','operator-position=','wrap-line-length','editorconfig'])
except getopt.GetoptError as ex:
print(ex, file=sys.stderr)
return usage(sys.stderr)
Expand All @@ -234,7 +230,7 @@ def main():
for opt, arg in opts:
if opt in ('--keep-array-indentation', '-k'):
js_options.keep_array_indentation = True
if opt in ('--keep-function-indentation', '-f'):
if opt in ('--keep-function-indentation','-f'):
js_options.keep_function_indentation = True
elif opt in ('--outfile', '-o'):
outfile = arg
Expand All @@ -258,8 +254,6 @@ def main():
js_options.jslint_happy = True
elif opt in ('--space_after_anon_function', '-a'):
js_options.space_after_anon_function = True
elif opt in ('--space_after_named_function'):
js_options.space_after_named_function = True
elif opt in ('--eval-code'):
js_options.eval_code = True
elif opt in ('--brace-style', '-b'):
Expand All @@ -273,7 +267,7 @@ def main():
elif opt in ('--comma-first', '-C'):
js_options.comma_first = True
elif opt in ('--operator-position', '-O'):
js_options.operator_position = arg
js_options.operator_position = sanitizeOperatorPosition(arg)
elif opt in ('--wrap-line-length ', '-w'):
js_options.wrap_line_length = int(arg)
elif opt in ('--stdin', '-i'):
Expand All @@ -285,6 +279,7 @@ def main():
elif opt in ('--help', '--usage', '-h'):
return usage()


if not file:
file = '-'

Expand Down Expand Up @@ -329,31 +324,14 @@ def main():
f.write(pretty)
except TypeError:
# This is not pretty, but given how we did the version import
# it is the only way to do this without having setup.py
# fail on a missing six dependency.
# it is the only way to do this without having setup.py fail on a missing six dependency.
six = __import__("six")
f.write(six.u(pretty))

except MissingInputStreamError:
print(
"Must pipe input or define at least one file.\n",
file=sys.stderr)
usage(sys.stderr)
return 1

except UnicodeError as ex:
print("Error while decoding input or encoding output:",
file=sys.stderr)
print(ex, file=sys.stderr)
return 1

except Exception as ex:
print(ex, file=sys.stderr)
return 1

# Success
return 0


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion codeformatter/lib/jsbeautifier/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.8.7'
__version__ = '1.7.4'
Loading

0 comments on commit 7115958

Please sign in to comment.