Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Braces after case get pushed onto new line #1357

Closed
Muon opened this issue Mar 6, 2018 · 2 comments
Closed

Braces after case get pushed onto new line #1357

Muon opened this issue Mar 6, 2018 · 2 comments

Comments

@Muon
Copy link

Muon commented Mar 6, 2018

Description

If I use braces to start a new block in a case statement to scope some variables, the beautifier moves the braces onto the next line and increases the indentation. However, I would want the braces to remain in line with the case.

Input

The code looked like this before beautification:

switch (op) {
    case "-":
    case "*":
    case "div":
    case "mod": {
        const left = this.left.toIntegerFormula();
        const right = this.right.toIntegerFormula();
        return [op, left, right];
    }
    case "+": {
        if (this.getType() === Type.NUMBER) {
            const left = this.left.toIntegerFormula();
            const right = this.right.toIntegerFormula();
            return [op, left, right];
        }
    }
}

Expected Output

The code should be unchanged after beautification.

Actual Output

The code actually looked like this after beautification:

switch (op) {
    case "-":
    case "*":
    case "div":
    case "mod":
        {
            const left = this.left.toIntegerFormula();
            const right = this.right.toIntegerFormula();
            return [op, left, right];
        }
    case "+":
        {
            if (this.getType() === Type.NUMBER) {
                const left = this.left.toIntegerFormula();
                const right = this.right.toIntegerFormula();
                return [op, left, right];
            }
        }
}

Environment

Checked on both on the current web beautifier and the current npm package.

Settings

{
    "indent_size": 4,
    "indent_char": " ",
    "indent_level": 0,
    "indent_with_tabs": false,
    "preserve_newlines": true,
    "max_preserve_newlines": 10,
    "jslint_happy": false,
    "space_after_anon_function": false,
    "brace_style": "collapse,preserve-inline",
    "keep_array_indentation": false,
    "keep_function_indentation": false,
    "space_before_conditional": true,
    "break_chained_methods": false,
    "eval_code": false,
    "unescape_strings": false,
    "wrap_line_length": 0
}
@StructByLightning
Copy link

The value of brace_style doesn't make any difference as well.

@cheesits456
Copy link

I'm having the same problem and it's annoying because the extra indentation caused by the { being on its own line is conflicting with ESLint

Example

Code before beautifying:

const conzole = console, // to bypass ESLint's "no-console" rule without needing to actually change any configuration
 input = 1;
 
switch (input) {
 case 1:
  conzole.log("one");
  break;
 case 2: {
  conzole.log("two");
  break;
 }
 case 3:
  conzole.log("three");
  break;
}

ESLint output before beautifying:

 0 errors and warnings

Code after beautifying:

const conzole = console, // to bypass ESLint's "no-console" rule without needing to actually change any configuration
 input = 1;

switch (input) {
 case 1:
  conzole.log("one");
  break;
 case 2:
  {
   conzole.log("two");
   break;
  }
 case 3:
  conzole.log("three");
  break;
}

ESLint output after beautifying:

   9:1  error  Expected indentation of 1 space but found 2   indent
  10:1  error  Expected indentation of 2 spaces but found 3  indent
  11:1  error  Expected indentation of 2 spaces but found 3  indent
  12:1  error  Expected indentation of 1 space but found 2   indent

 4 problems (4 errors, 0 warnings)

Config

JS Beautifier config:

{
 "indent_size": 1,
 "indent_char": " ",
 "indent_with_tabs": false,
 "preserve_newlines": 1,
 "max_preserve_newlines": 2,
 "space_in_paren": false,
 "space_in_empty_paren": false,
 "e4x": false,
 "jslint_happy": false,
 "space_after_anon_function": false,
 "brace_style": "collapse,preserve-inline",
 "keep_array_indentation": false,
 "keep_function_indentation": false,
 "eval_code": false,
 "unescape_strings": false,
 "wrap_line_length": 0,
 "unindent_chained_methods": false,
 "break_chained_methods": false,
 "end_with_newline": false,
 "comma_first": false,
 "operator_position": "before-newline"
}

ESLint config:

{
 "env": {
  "es6": true,
  "node": true
 },
 "extends": "eslint:recommended",
 "parserOptions": {
  "ecmaVersion": 2015,
  "sourceType": "module"
 },
 "rules": {
  "indent": ["error", 1, { "SwitchCase": 1 }],
  "linebreak-style": ["error", "unix"],
  "no-cond-assign": ["error", "except-parens"],
  "quotes": ["error", "double"],
  "semi": ["error", "always"]
 }
}

@bitwiseman bitwiseman modified the milestones: v1.9.x, v1.10.x Apr 29, 2019
@bitwiseman bitwiseman modified the milestones: v1.10.x, v1.9.x May 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants