diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a3b4d28..f77546e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,15 @@ # Changelog +## v1.7.5 + +### Description + + +### Closed Issues +* Strict mode: js_source_text is not defined [CSS] ([#1286](https://github.com/beautify-web/js-beautify/issues/1286)) +* Made brace_style option more inclusive ([#1277](https://github.com/beautify-web/js-beautify/pull/1277)) +* White space before"!important" tag missing in CSS beautify ([#1273](https://github.com/beautify-web/js-beautify/issues/1273)) + + ## v1.7.4 ### Description @@ -32,6 +43,7 @@ Lessons learned: ### Closed Issues +* undindent-chained-methods option. Resolves #482 ([#1240](https://github.com/beautify-web/js-beautify/pull/1240)) * Add test and tools folder to npmignore ([#1239](https://github.com/beautify-web/js-beautify/issues/1239)) * incorrect new-line insertion after "yield" ([#1206](https://github.com/beautify-web/js-beautify/issues/1206)) * Do not modify built-in objects ([#1205](https://github.com/beautify-web/js-beautify/issues/1205)) diff --git a/README.md b/README.md index 8f6fe06e..b35def4c 100644 --- a/README.md +++ b/README.md @@ -25,17 +25,17 @@ JS Beautifier is hosted on two CDN services: [cdnjs](https://cdnjs.com/libraries To pull from one of these services include one set of the script tags below in your document: ```html - - - + + + - - - + + + - - - + + + ``` Disclaimer: These are free services, so there are [no uptime or support guarantees](https://github.com/rgrove/rawgit/wiki/Frequently-Asked-Questions#i-need-guaranteed-100-uptime-should-i-use-cdnrawgitcom). @@ -65,8 +65,11 @@ res = jsbeautifier.beautify_file('some_file.js') ``` python opts = jsbeautifier.default_options() opts.indent_size = 2 +opts.space_in_empty_paren = True res = jsbeautifier.beautify('some javascript', opts) ``` +The configuration option names are the same as the CLI names but with underscores instead of dashes. The example above would be set on the command-line as `--indent-size 2 --space-in-empty-paren`. + ## JavaScript @@ -83,18 +86,23 @@ You can also use `js-beautify` as a `node` library (install locally, the `npm` d $ npm install js-beautify ``` +Import and call the approriate beautifier method for javascript (js), css, or html. All three method signatures are `beautify(code, options)`. `code` is a the string of code to be beautified. options is an object with the settings you would like used to beautify the code. + +The configuration option names are the same as the CLI names but with underscores instead of dashes. For example, `--indent-size 2 --space-in-empty-paren` would be `{ indent_size: 2, space_in_empty_paren: true }`. + ```js -var beautify = require('js-beautify').js_beautify, +var beautify = require('js-beautify').js, fs = require('fs'); fs.readFile('foo.js', 'utf8', function (err, data) { if (err) { throw err; } - console.log(beautify(data, { indent_size: 2 })); + console.log(beautify(data, { indent_size: 2, space_in_empty_paren: true })); }); ``` + ## Options These are the command-line flags for both Python and JS scripts: @@ -322,4 +330,4 @@ Thanks also to Jason Diamond, Patrick Hof, Nochum Sossonko, Andreas Schneider, D Vasilevsky, Vital Batmanov, Ron Baldwin, Gabriel Harrison, Chris J. Shull, Mathias Bynens, Vittorio Gambaletta and others. -(README.md: js-beautify@1.7.4) +(README.md: js-beautify@1.7.5) diff --git a/js/lib/beautify-html.js b/js/lib/beautify-html.js index c7fcf6d0..3db2c78c 100644 --- a/js/lib/beautify-html.js +++ b/js/lib/beautify-html.js @@ -318,8 +318,8 @@ function Beautifier(html_source, options, js_beautify, css_beautify) { // Doctype and xml elements '!doctype', '?xml', - // ?php tag - '?php', + // ?php and ?= tags + '?php', '?=', // other tags that were in this list, keeping just in case 'basefont', 'isindex' ], @@ -647,10 +647,8 @@ function Beautifier(html_source, options, js_beautify, css_beautify) { // must check for space first otherwise the tag could have the first attribute included, and // then not un-indent correctly - if (tag_complete.indexOf(' ') !== -1) { //if there's whitespace, thats where the tag name ends - tag_index = tag_complete.indexOf(' '); - } else if (tag_complete.indexOf('\n') !== -1) { //if there's a line break, thats where the tag name ends - tag_index = tag_complete.indexOf('\n'); + if (tag_complete.search(/\s/) !== -1) { //if there's whitespace, thats where the tag name ends + tag_index = tag_complete.search(/\s/); } else if (tag_complete.charAt(0) === '{') { tag_index = tag_complete.indexOf('}'); } else { //otherwise go with the tag ending @@ -915,6 +913,11 @@ function Beautifier(html_source, options, js_beautify, css_beautify) { //unformatted? var next_tag = this.get_tag(true /* peek. */ ); + next_tag = next_tag || ''; + if (typeof next_tag !== 'string') { + next_tag = next_tag[0]; + } + // test next_tag to see if it is just html tag (no external content) var tag = (next_tag || "").match(/^\s*<\s*\/?([a-z]*)\s*[^>]*>\s*$/); diff --git a/js/src/html/beautifier.js b/js/src/html/beautifier.js index 61f5f5a6..cf045f41 100644 --- a/js/src/html/beautifier.js +++ b/js/src/html/beautifier.js @@ -170,8 +170,8 @@ function Beautifier(html_source, options, js_beautify, css_beautify) { // Doctype and xml elements '!doctype', '?xml', - // ?php tag - '?php', + // ?php and ?= tags + '?php', '?=', // other tags that were in this list, keeping just in case 'basefont', 'isindex' ], @@ -499,10 +499,8 @@ function Beautifier(html_source, options, js_beautify, css_beautify) { // must check for space first otherwise the tag could have the first attribute included, and // then not un-indent correctly - if (tag_complete.indexOf(' ') !== -1) { //if there's whitespace, thats where the tag name ends - tag_index = tag_complete.indexOf(' '); - } else if (tag_complete.indexOf('\n') !== -1) { //if there's a line break, thats where the tag name ends - tag_index = tag_complete.indexOf('\n'); + if (tag_complete.search(/\s/) !== -1) { //if there's whitespace, thats where the tag name ends + tag_index = tag_complete.search(/\s/); } else if (tag_complete.charAt(0) === '{') { tag_index = tag_complete.indexOf('}'); } else { //otherwise go with the tag ending @@ -767,6 +765,11 @@ function Beautifier(html_source, options, js_beautify, css_beautify) { //unformatted? var next_tag = this.get_tag(true /* peek. */ ); + next_tag = next_tag || ''; + if (typeof next_tag !== 'string') { + next_tag = next_tag[0]; + } + // test next_tag to see if it is just html tag (no external content) var tag = (next_tag || "").match(/^\s*<\s*\/?([a-z]*)\s*[^>]*>\s*$/); diff --git a/js/test/generated/beautify-html-tests.js b/js/test/generated/beautify-html-tests.js index 4d3ea075..a900968d 100644 --- a/js/test/generated/beautify-html-tests.js +++ b/js/test/generated/beautify-html-tests.js @@ -2646,6 +2646,17 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be ''); + //============================================================ + // Regression Tests + reset_options(); + + // #1202 + test_fragment('5A - IN-SPRINT TESTING'); + test_fragment('9'); + test_fragment('WA GlassKote'); + test_fragment('"A Beer Has No Name"'); + + //============================================================ // Php formatting reset_options(); @@ -2673,6 +2684,15 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be '\n' + '\n' + ''); + test_fragment( + '\n' + + '\n' + + ''); + test_fragment( + '\n' + + 'Test'); //============================================================ diff --git a/package-lock.json b/package-lock.json index 2f4932a9..b6004bd8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "js-beautify", - "version": "1.7.4", + "version": "1.7.5", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index fbbe1bd8..6017a81d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "js-beautify", - "version": "1.7.4", + "version": "1.7.5", "description": "jsbeautifier.org for node", "main": "js/index.js", "bin": { diff --git a/python/jsbeautifier/__init__.py b/python/jsbeautifier/__init__.py index 444895b8..69e1febf 100644 --- a/python/jsbeautifier/__init__.py +++ b/python/jsbeautifier/__init__.py @@ -9,7 +9,7 @@ import errno import copy from jsbeautifier.__version__ import __version__ from jsbeautifier.javascript.options import BeautifierOptions -from jsbeautifier.javascript.beautifier import Beautifier +from jsbeautifier.javascript.beautifier import Beautifier, sanitizeOperatorPosition # # The MIT License (MIT) @@ -117,7 +117,7 @@ def beautify_file(file_name, opts = default_options() ): 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) + print("Must pipe input or define at least one file.\n", file=sys.stderr) usage(sys.stderr) raise Exception() else: @@ -136,7 +136,6 @@ Javascript beautifier (http://jsbeautifier.org/) Usage: jsbeautifier.py [options] can be "-", which means stdin. - defaults to stdout Input options: @@ -153,7 +152,7 @@ Output options: -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 () + -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 @@ -161,9 +160,11 @@ Output options: -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 - -w, --wrap-line-length Attempt to wrap line when it exceeds this length. + -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. 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: @@ -175,7 +176,7 @@ Rarely needed options: -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: @@ -210,7 +211,7 @@ 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', + ['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', diff --git a/python/jsbeautifier/__version__.py b/python/jsbeautifier/__version__.py index 043606c7..89962bff 100644 --- a/python/jsbeautifier/__version__.py +++ b/python/jsbeautifier/__version__.py @@ -1 +1 @@ -__version__ = '1.7.4' +__version__ = '1.7.5' diff --git a/python/jsbeautifier/tests/shell-smoke-test.sh b/python/jsbeautifier/tests/shell-smoke-test.sh index 32798c99..3e28efcc 100755 --- a/python/jsbeautifier/tests/shell-smoke-test.sh +++ b/python/jsbeautifier/tests/shell-smoke-test.sh @@ -121,15 +121,13 @@ test_cli_js_beautify() $CLI_SCRIPT -o $TEST_TEMP/js-beautify-rn.js -e '\r\n' $TEST_TEMP/js-beautify-n.js # ensure eol processed correctly - # Issue #987 - strange error when processing --eol - # uncomment to reproduce - # $CLI_SCRIPT -o $TEST_TEMP/js-beautify-n-dash.js --indent-size 2 --eol '\n' $TEST_TEMP/js-beautify-n.js - # $CLI_SCRIPT -o $TEST_TEMP/js-beautify-rn-dash.js --indent-size 2 --eol '\r\n' $TEST_TEMP/js-beautify-n.js - # diff -q $TEST_TEMP/js-beautify-n-dash.js $TEST_TEMP/js-beautify-rn-dash.js && { - # diff $TEST_TEMP/js-beautify-n-dash.js $TEST_TEMP/js-beautify-rn-dash.js | cat -t -e - # echo "js-beautify output for $TEST_TEMP/js-beautify-n-dash.js and $TEST_TEMP/js-beautify-rn-dash.js was expected to be different." - # cleanup 1 - # } + $CLI_SCRIPT -o $TEST_TEMP/js-beautify-n-dash.js --indent-size 2 --eol '\n' $TEST_TEMP/js-beautify-n.js + $CLI_SCRIPT -o $TEST_TEMP/js-beautify-rn-dash.js --indent-size 2 --eol '\r\n' $TEST_TEMP/js-beautify-n.js + diff -q $TEST_TEMP/js-beautify-n-dash.js $TEST_TEMP/js-beautify-rn-dash.js && { + diff $TEST_TEMP/js-beautify-n-dash.js $TEST_TEMP/js-beautify-rn-dash.js | cat -t -e + echo "js-beautify output for $TEST_TEMP/js-beautify-n-dash.js and $TEST_TEMP/js-beautify-rn-dash.js was expected to be different." + cleanup 1 + } diff -q $TEST_TEMP/js-beautify-n.js $TEST_TEMP/js-beautify-rn.js && { diff $TEST_TEMP/js-beautify-n.js $TEST_TEMP/js-beautify-rn.js | cat -t -e diff --git a/test/data/html/tests.js b/test/data/html/tests.js index 021d8ce8..9e3908a5 100644 --- a/test/data/html/tests.js +++ b/test/data/html/tests.js @@ -793,9 +793,22 @@ exports.test_data = { '' ] }, ] + }, { + name: "Regression Tests", + description: "Regression Tests", + options: [], + tests: [{ + comment: '#1202', + fragment: true, + unchanged: '5A - IN-SPRINT TESTING' + }, + { fragment: true, unchanged: '9' }, + { fragment: true, unchanged: 'WA GlassKote' }, + { fragment: true, unchanged: '"A Beer Has No Name"' }, + ] }, { name: "Php formatting", - description: "Php () treated as comments.", + description: "Php ( and ) treated as comments.", options: [], tests: [{ fragment: true, @@ -825,6 +838,21 @@ exports.test_data = { '', '' ] + }, { + fragment: true, + unchanged: [ + '', + '', + '' + ] + }, { + fragment: true, + unchanged: [ + '', + 'Test' + ] }] }, { name: "Support simple language specific option inheritance/overriding", diff --git a/tools/build.sh b/tools/build.sh index b691daf6..efe249aa 100755 --- a/tools/build.sh +++ b/tools/build.sh @@ -36,8 +36,8 @@ build_all() build_py() { - echo Building python module... - pip install -e ./python || exit 1 + echo Building python module... + /usr/bin/env python -m pip install -e ./python || exit 1 } build_js() diff --git a/tools/generate-changelog.sh b/tools/generate-changelog.sh index 7fe7661e..b4497bcc 100755 --- a/tools/generate-changelog.sh +++ b/tools/generate-changelog.sh @@ -15,20 +15,22 @@ main() exit 1 fi - jq --version || { + local JQ + JQ=$(command -v jq) || { echo "Required tool 'jq' missing. Failed." exit 1 } - gsort --version || { - echo "Required tool 'gsort' missing. Failed." + local GSORT + GSORT=$(command -v gsort || command -v sort) || { + echo "Required tool 'GNU sort' missing. Failed." exit 1 } IFS=$'\n' echo "# Changelog" > CHANGELOG.md - for m in $(curl -s "https://api.github.com/repos/$1/milestones?state=closed" | jq -c '.[] | [.title, .number, .description]' | gsort -r -V); do + for m in $(curl -s "https://api.github.com/repos/$1/milestones?state=closed" | "$JQ" -c '.[] | [.title, .number, .description]' | "$GSORT" -r -V); do mid=$(echo $m | sed 's/\[".*",\(.*\),".*"\]/\1/') title=$(echo $m | sed 's/\["\(.*\)",.*,".*"\]/\1/') @@ -39,7 +41,7 @@ main() echo $m | sed 's/\[".*",.*,"\(.*\)"\]/\1/' | sed -e 's/\\"/"/g' | sed -e 's/\\r\\n/\\n/g' | sed -e 's/\\n/\'$'\n/g' >> CHANGELOG.md echo "" >> CHANGELOG.md echo '### Closed Issues' >> CHANGELOG.md - for i in $(curl -s "https://api.github.com/repos/$1/issues?milestone=$mid&state=closed" | jq -c '.[] | [.html_url, .number, .title]'); do + for i in $(curl -s "https://api.github.com/repos/$1/issues?milestone=$mid&state=closed" | "$JQ" -c '.[] | [.html_url, .number, .title]'); do echo $i | sed 's/\["\(.*\)",\(.*\),\"\(.*\)\"\]/* \3 ([#\2](\1))/' | sed 's/\\"/"/g' >> CHANGELOG.md done echo "" >> CHANGELOG.md