HookyQR / VSCodeBeautify

Enable js-beautify (https://github.com/beautify-web/js-beautify) in VS Code
MIT License
607 stars 178 forks source link

[JS] Wrong formatting (invalid syntax) #327

Open larmcr opened 5 years ago

larmcr commented 5 years ago

Settings

VS Code (settings.json):

{
  "workbench.iconTheme": "material-icon-theme",
  "workbench.colorTheme": "Nord",
  "editor.fontFamily": "Courier New",
  "editor.fontSize": 15,
  "editor.minimap.enabled": false,
  "editor.tabSize": 2,
  "editor.renderWhitespace": "all",
  "dateTime.customFormat": "[ww]ww.d - dddd, MMMM Do, YYYY",
  "git.enableSmartCommit": true,
  "diffEditor.ignoreTrimWhitespace": false,
  "explorer.sortOrder": "type",
  "markdown-pdf.format": "Letter",
  "markdown-pdf.headerTemplate": "<div style=\"font-size: 1px; margin: 0 auto;\"></div>",
  "markdown-pdf.breaks": true,
  "terminal.integrated.rendererType": "dom",
  "window.zoomLevel": 1,
}

beautifyrc.json:

{
  "title": "JSON schema for beautifyrc",
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "definitions": {
    "CHJProperties": {
      "type": "object",
      "properties": {
        "indent_char": {
          "description": "Indentation character. [JS,CSS,HTML]",
          "type": "string",
          "default": " ",
          "maxLength": 1
        },
        "indent_empty_lines": {
          "description": "Keep indentation on empty lines. [JS,CSS,HTML]",
          "type": "boolean",
          "default": false
        },

        "indent_size": {
          "description": "Indent size. [JS,CSS,HTML]",
          "type": "integer",
          "default": 4
        },
        "eol": {
          "description": "Character(s) to use as line terminators. [JS,CSS,HTML]",
          "type": "string",
          "default": "\n"
        },
        "indent_with_tabs": {
          "description": "Indent with tabs, overrides 'indent_size' and 'indent_char' [JS,CSS,HTML]",
          "type": "boolean",
          "default": false
        },
        "end_with_newline": {
          "description": "Ensure newline at end of file. [JS,CSS,HTML]",
          "type": "boolean",
          "default": false
        },
        "preserve_newlines": {
          "description": "Preserve line-breaks. [JS,CSS,HTML]",
          "type": "boolean",
          "default": true
        }
      }
    },
    "HJProperties": {
      "type": "object",
      "properties": {

        "max_preserve_newlines": {
          "description": "Number of line-breaks to be preserved in one chunk. [JS,HTML]",
          "type": "integer",
          "default": 10
        },
        "templating": {
          "description": "List of template langagues to use. [JS,HTML]\nNote: auto = none in JavaScript, all in html",
          "type": "array",
          "items": {
            "type": "string",
            "enum": ["auto", "none", "django", "erb", "handlebars", "php"]
          },
          "default": ["auto"]
        },
        "wrap_line_length": {
          "description": "Wrap lines at next opportunity after N characters. [JS,HTML]",
          "type": "integer",
          "default": 0
        }
      }
    },
    "CProperties": {
      "type": "object",
      "properties": {
        "selector_separator_newline": {
          "description": "Add a newline between multiple selectors. [CSS]",
          "type": "boolean",
          "default": true
        },
        "newline_between_rules": {
          "description": "Add a newline between CSS rules. [CSS]",
          "type": "boolean",
          "default": false
        },
        "space_around_selector_separator": {
          "description": "(Deprecated: use space_around_combinator) [CSS]",
          "type": "boolean",
          "default": false
        },
        "space_around_combinator": {
          "description": "Ensure space around selector separators (>+~). [CSS]",
          "type": "boolean",
          "default": false
        }
      }
    },
    "HProperties": {
      "type": "object",
      "properties": {
        "void_elements": {
          "description": "HTLM void elements - aka self-closing tags. [HTML]",
          "type": "array",
          "items": {
            "type": "string"
          },
          "default": ["area", "base", "br", "col", "embed", "hr", "img", "input", "keygen", "link", "menuitem", "meta",
            "param", "source", "track", "wbr", "!doctype", "?xml", "?php", "?=", "basefont", "isindex"]
        },
        "inline": {
          "description": "",
          "type": "array",
          "items": {
            "type": "string"
          },
          "default": [
            "a", "abbr", "area", "audio", "b", "bdi", "bdo", "br", "button", "canvas", "cite",
            "code", "data", "datalist", "del", "dfn", "em", "embed", "i", "iframe", "img",
            "input", "ins", "kbd", "keygen", "label", "map", "mark", "math", "meter", "noscript",
            "object", "output", "progress", "q", "ruby", "s", "samp", "select", "small",
            "span", "strong", "sub", "sup", "svg", "template", "textarea", "time", "u", "var",
            "video", "wbr", "text",
            "acronym", "address", "big", "dt", "ins", "strike", "tt"
          ]
        },
        "wrap_attributes": {
          "description": "Wrap attributes to new lines. [HTML]",
          "type": "string",
          "default": "auto",
          "enum": ["auto", "force", "force-aligned", "force-expand-multiline", "align-multiple", "preserve",
            "preserve-aligned"]
        },
        "wrap_attributes_indent_size": {
          "description": "Indent wrapped attributes to after N characters. Defaults to 'indent_size'. [HTML]",
          "type": "number"
        },
        "indent_inner_html": {
          "description": "Indent <head> and <body> sections. [HTML]",
          "type": "boolean",
          "default": false
        },
        "indent_scripts": {
          "description": "[keep|separate|normal] [HTML]",
          "type": "string",
          "default": "normal",
          "enum": ["keep", "separate", "normal"]
        },
        "unformatted": {
          "description": "List of tags that should not be reformatted. [HTML]",
          "type": "array",
          "items": {
            "type": "string"
          },
          "default": []
        },
        "content_unformatted": {
          "description": "List of tags whose content should not be reformatted. [HTML]",
          "type": "array",
          "items": {
            "type": "string"
          },
          "default": ["pre", "textarea"]
        },
        "extra_liners": {
          "description": "List of tags that should have an extra newline before them. [HTML]",
          "type": "array",
          "items": {
            "type": "string"
          },
          "default": ["head", "body", "/html"]
        },
        "indent_body_inner_html": {
          "description": "Indent elements within html <body> element. [HTML]",
          "type": "boolean",
          "default": true
        },
        "indent_head_inner_html": {
          "description": "Indent elements within html <head> element. [HTML]",
          "type": "boolean",
          "default": true
        },
        "indent_handlebars": {
          "description": "format and indent {{#foo}} and {{/foo}}. [HTML]",
          "type": "boolean",
          "default": false
        }
      }
    },
    "JProperties": {
      "type": "object",
      "properties": {
        "brace_style": {
          "description": "[collapse|expand|end-expand|none][,preserve-inline]  [JS]",
          "type": "string",
          "default": "collapse",
          "enum": ["collapse", "expand", "end-expand", "none", "collapse,preserve-inline", "expand,preserve-inline",
            "end-expand,preserve-inline", "none,preserve-inline"]
        },
        "indent_level": {
          "description": "Initial indentation level. [JS]",
          "type": "integer",
          "default": 0
        },
        "space_in_paren": {
          "description": "Add padding spaces within parentheses, ie. f( a, b ). [JS]",
          "type": "boolean",
          "default": false
        },
        "space_in_empty_paren": {
          "description": "Leave space in empty parentheses, ie. f( ). [JS]",
          "type": "boolean",
          "default": false
        },
        "jslint_happy": {
          "description": "Enable jslint-stricter mode. (Forces 'space_after_anon_function') [JS]",
          "type": "boolean",
          "default": false
        },
        "space_after_anon_function": {
          "description": "Add a space before an anonymous function's parens, ie. function (). [JS]",
          "type": "boolean",
          "default": false
        },
        "space_after_named_function": {
          "description": "Add a space before a named function's parens, ie. function example (). [JS]",
          "type": "boolean",
          "default": false
        },
        "break_chained_methods": {
          "description": "Break chained method calls across subsequent lines. [JS]",
          "type": "boolean",
          "default": false
        },
        "keep_array_indentation": {
          "description": "Preserve array indentation. [JS]",
          "type": "boolean",
          "default": false
        },
        "keep_function_indentation": {
          "description": "Preserve function indentation. [JS]",
          "type": "boolean",
          "default": false
        },
        "space_before_conditional": {
          "description": "Ensure a space before conditional statement. [JS]",
          "type": "boolean",
          "default": true
        },
        "unescape_strings": {
          "description": "Decode printable characters encoded in xNN notation. [JS]",
          "type": "boolean",
          "default": false
        },
        "comma_first": {
          "description": "Put commas at the beginning of new line instead of end. [JS]",
          "type": "boolean",
          "default": false
        },
        "operator_position": {
          "description": "Move operators to before or after a new line, or keep as is. [JS]",
          "type": "string",
          "enum": ["before-newline", "after-newline", "preserve-newline"],
          "default": "before-newline"
        },
        "e4x": {
          "description": "Pass E4X xml literals through untouched. [JS]",
          "type": "boolean",
          "default": false
        },
        "unindent_chained_methods": {
          "description": "Unindent chained methods. [JS]",
          "type": "boolean",
          "default": false
        }
      }
    }
  },
  "allOf": [
    { "$ref": "#/definitions/CHJProperties" },
    { "$ref": "#/definitions/HJProperties" },
    { "$ref": "#/definitions/CProperties" },
    { "$ref": "#/definitions/HProperties" },
    { "$ref": "#/definitions/JProperties" },
    {
      "properties": {
        "css": {
          "type": "object",
          "allOf": [
            { "$ref": "#/definitions/CHJProperties" },
            { "$ref": "#/definitions/CProperties" }]
        },
        "js": {
          "type": "object",
          "allOf": [
            { "$ref": "#/definitions/CHJProperties" },
            { "$ref": "#/definitions/HJProperties" },
            { "$ref": "#/definitions/JProperties" }
          ]
        },
        "html": {
          "type": "object",
          "allOf": [
            { "$ref": "#/definitions/CHJProperties" },
            { "$ref": "#/definitions/HJProperties" },
            { "$ref": "#/definitions/HProperties" }]
        }
      }
        }]
}

Action performed

Format javascript file with Beautify file command:

var a = true
!function foo(b) { a = b; }(false)

Expected results

"Code is syntactically valid." (according to Esprima):

var a = true
!function foo(b) {
  a = b;
}(false)

Actual results

"Error: Line 1: Unexpected token !" (according to Esprima):

var a = true! function foo(b) {
  a = b;
}(false)
HookyQR commented 5 years ago

Please raise this issue with https://github.com/beautify-web/js-beautify