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

Small ternary issue && ... || ... #2867

Open
drcmda opened this issue Sep 20, 2017 · 3 comments
Open

Small ternary issue && ... || ... #2867

drcmda opened this issue Sep 20, 2017 · 3 comments
Labels
area:binary expressions lang:javascript Issues affecting JS type:bug Issues identifying ugly output, or a defect in the program

Comments

@drcmda
Copy link

drcmda commented Sep 20, 2017

Not that it made much sense in that case when ? ... : ... would do the same, i was trying to prevent a longer prettier output (? : would have added more lines)

Prettier 1.7.0
Playground link

--jsx-bracket-same-line
--print-width 120
--no-semi
--single-quote
--tab-width 4
--trailing-comma all

Input:

let test = {
    plugins: [
        new WebpackNotifierPlugin(),
        ...((env.prod && [
            new webpack.DefinePlugin({ 'process.env': { NODE_ENV: JSON.stringify('production') } }),
            new webpack.LoaderOptionsPlugin({ minimize: true, debug: false }),
            new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false }, mangle: true }),
        ]) || []),
    ],
}

Output:

let test = {
    plugins: [
        new WebpackNotifierPlugin(),
        ...((env.prod && [
            new webpack.DefinePlugin({ 'process.env': { NODE_ENV: JSON.stringify('production') } }),
            new webpack.LoaderOptionsPlugin({ minimize: true, debug: false }),
            new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false }, mangle: true }),
        ]) ||
            []),
    ],
}

Expected behavior:

I wouldn't expect the trailing [] to hang in the place prettier puts it right now.

@duailibe
Copy link
Member

duailibe commented Sep 20, 2017

Indeed. Why do we only inline array and objects when they're not empty? i.e.

const foo =
  bar &&
  baz &&
  [];

const foo = bar &&
    baz && [0];

playground

@suchipi suchipi added the type:bug Issues identifying ugly output, or a defect in the program label Sep 20, 2017
@duailibe
Copy link
Member

Okay now I get why it's like this.. goes back to #1342 but I'm not sure the fix was correct (i.e. the example above)

@azz azz added the lang:javascript Issues affecting JS label Oct 1, 2017
@AHH0623
Copy link

AHH0623 commented Apr 16, 2024

good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:binary expressions lang:javascript Issues affecting JS type:bug Issues identifying ugly output, or a defect in the program
Projects
None yet
Development

No branches or pull requests

6 participants