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

try/catch one liners #143

Closed
LinusU opened this issue May 21, 2015 · 6 comments

Comments

@LinusU
Copy link
Member

commented May 21, 2015

Isn't the following allowed? I'm getting a rather cryptic error message and since we do allow e.g. if one liners I figured it would be okay.

var result
try { result = parse(raw) }
catch (err) { return cb(err) }

cb(null, result)
Closing curly brace does not appear on the same line as the subsequent block.
@julien-f

This comment has been minimized.

Copy link

commented May 21, 2015

I think it's because, catch should follow the closing braces of the try:

try { result = parse(raw) } catch (err) { return cb(err) }

// or

try {
  result = parse(raw)
} catch (err) {
  return cb(err)
}
@LinusU

This comment has been minimized.

Copy link
Member Author

commented May 21, 2015

Wow, fast reply!

Having them on one line actually works, but it makes for very long lines so I'm not sure if I want to use that.

Hmm, well we do allow the following

if (false) return 123
else return 456

But not with curly brackets:

if (true) { return 123 }
else { return 456 }

But with try and catch the brackets are mandatory :(

@julien-f

This comment has been minimized.

Copy link

commented May 21, 2015

You can use the following, it's ugly but it should work:

try { result = parse(raw)
} catch (err) { return cb(err) }
@LinusU

This comment has been minimized.

Copy link
Member Author

commented May 21, 2015

Ugh, that is ugly :) I think I'll stick with the 4-line version for now.

But I do propose that we have the rules be something that disallows anything after the {, unless the } is on the same line. And maybe even disallow having try and catch on the same line?

That way the thing I originally wanted would work and that ugly hack wouldn't...

I don't think there is any use case where you want to allow text after the curly bracket, unless when it's a one liner that closes on that line.

@julien-f

This comment has been minimized.

Copy link

commented May 29, 2015

standard is based on ESLint, therefore you will to find this rule in the set of existing ones or propose a new one there.

@feross

This comment has been minimized.

Copy link
Member

commented May 29, 2015

Like @julien-f says, you should use:

try { result = parse(raw) } catch (err) { return cb(err) }

// or

try {
  result = parse(raw)
} catch (err) {
  return cb(err)
}

This is a result of our requiring "one true brace style", i.e. opening brace of a block must be on the same line. You can read more here: http://eslint.org/docs/rules/brace-style I agree that this try-catch case seems like it should be an exception. If you can get eslint to change it or add an option for this, I would merge it.

But this isn't a bug in standard, so I'm going to close this issue for now.

@feross feross closed this May 29, 2015

@lock lock bot locked as resolved and limited conversation to collaborators May 11, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
3 participants
You can’t perform that action at this time.