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

Wrong indentation when the last line in a case is a right brace #1683

Closed
fmkang opened this issue Jun 28, 2019 · 4 comments
Closed

Wrong indentation when the last line in a case is a right brace #1683

fmkang opened this issue Jun 28, 2019 · 4 comments

Comments

@fmkang
Copy link

fmkang commented Jun 28, 2019

Description

The indentation is wrong if the last line of code is a right brace in a case code block.

However, if you put another statement (something other than a comment) after the right brace, it becomes correct.

Input

The code looked like this before beautification:

switch (a) {
    case 'foo':
    case 'bar':
        if (foo == bar) {
            console.log(a);
        } else {
            console.log(a);
        }
    case 'baz':
        console.log(a);
}

Expected Output

The code should have kept the same after beautification:

switch (a) {
    case 'foo':
    case 'bar':
        if (foo == bar) {
            console.log(a);
        } else {
            console.log(a);
        }
    case 'baz':
        console.log(a);
}

Actual Output

The code actually looked like this after beautification:

switch (a) {
    case 'foo':
    case 'bar':
        if (foo == bar) {
            console.log(a);
        } else {
            console.log(a);
        }
        case 'baz':
            console.log(a);
}

The last case is mistakenly indented.

If you add one line after the brace, it will keep the same:

switch (a) {
    case 'foo':
    case 'bar':
        if (foo == bar) {
            console.log(a);
        } else {
            console.log(a);
        }
        console.log(a);
    case 'baz':
        console.log(a);
}

Environment

OS: macOS 10.14.5

Settings

As default:

{
  "indent_size": "4",
  "indent_char": " ",
  "max_preserve_newlines": "5",
  "preserve_newlines": true,
  "keep_array_indentation": false,
  "break_chained_methods": false,
  "indent_scripts": "normal",
  "brace_style": "collapse",
  "space_before_conditional": true,
  "unescape_strings": false,
  "jslint_happy": false,
  "end_with_newline": false,
  "wrap_line_length": "0",
  "indent_inner_html": false,
  "comma_first": false,
  "e4x": false,
  "indent_empty_lines": false
}
@bitwiseman bitwiseman modified the milestones: v1.10.1, v1.10.x Jul 1, 2019
@bitwiseman bitwiseman modified the milestones: v1.10.x, v1.10.xx Jan 14, 2020
@bitwiseman bitwiseman modified the milestones: v1.10.x, v1.11.x Apr 5, 2020
@bitwiseman bitwiseman modified the milestones: v1.11.x, v1.12.x Aug 13, 2020
@bitwiseman bitwiseman modified the milestones: v1.12.x, 1.13.x Aug 20, 2020
@marianayap
Copy link

@bitwiseman Hello! Can I give this issue a try?

@bitwiseman
Copy link
Member

@marianayap Please do!

@marianayap
Copy link

@bitwiseman Hello! I won't be able to continue with the issue. It's open for anyone that wants to contribute. Thank you!

@bitwiseman bitwiseman modified the milestones: v1.13.1, v1.13.x Jan 8, 2021
@bitwiseman bitwiseman modified the milestones: v1.13.6, v1.13.x Jan 26, 2021
@bitwiseman bitwiseman modified the milestones: v1.13.7, v1.13.x Apr 13, 2021
@pr0grammm
Copy link
Contributor

pr0grammm commented Jun 16, 2021

Hello @bitwiseman, I have been given a task to contribute to an open source repo and I am interested in this project! Could I please be assigned this issue?

pr0grammm pushed a commit to pr0grammm/js-beautify that referenced this issue Jun 18, 2021
problem: Wrong indentation observed when the last line in a case is a right
brace.

solution: Deindent for case/default token is skipped whenever the previous
token is a right brace.
However this logic doesn't always work as highlighted in the issue.
Added code to skip deindent only if the closing right brace corresponds to
opening left brace of case action block.
pr0grammm added a commit to pr0grammm/js-beautify that referenced this issue Jun 21, 2021
pr0grammm added a commit to pr0grammm/js-beautify that referenced this issue Jun 21, 2021
pr0grammm added a commit to pr0grammm/js-beautify that referenced this issue Jun 21, 2021
bitwiseman added a commit that referenced this issue Jun 23, 2021
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

4 participants