-
Notifications
You must be signed in to change notification settings - Fork 14
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
Automatic do expressions if expression starts with if
, for
, switch
, try
etc.
#9
Comments
I think it should be considered provided that braces are mandatory, i.e. var a = while (foo) { bar; }; and not: var a = while (foo) bar;; |
What happened to I have a question. Does an implicit do expression require |
@claudepache what is the rationale behind making curly-braces mandatory? Wouldn't this break with existing JavaScript syntax for statements, which let the programmer omit curly-braces in case of single-statement conditional-branches and loop-bodies? I recognize that most coding-styleguides and many developers consider curly-braces and semicolons good practice, however, i don't think the standard should be opinionated about a specific coding-style, if this means a break with the rest of the grammar. |
@RaoulSchaffranek that's possible for |
I'll give you my rational. This proposal essentially provides "blocks that return a value". Requiring curly braces for |
I didn't think of it before @ljharb mentioned it, but i didn't mean to make curly-braces optional in all cases, in particular not for <div>
{if (x > 0) {
'x is positive'
} else {
if (x < 0) {
'x is negative'
} else {
'x is zero'
}
}}
</div> Instead of the familiar: <div>
{if (x > 0) {
'x is positive'
} else if (x < 0) {
'x is negative'
} else {
'x is zero'
}}
</div> But then it is only consequent to allow the even shorter variant: <div>
{if (x > 0) 'x is positive'
else if (x < 0) 'x is negative'
else 'x is zero'}
</div> |
My rationale was that, in absence of braces, you would have required semicolons in the middle of an expression, that would cause confusion. For instance, the following is a syntax error: if (true) 1 else 0 // syntax error: missing semicolon after 1 You have to write: if (true) 1; else 0; which is acceptable for if-as-statement, but becomes confusing for if-as-expression: const a = if (true) 1; else 0;; // yes, two semicolons But if you are interested to use expressions rather than statements in the branches of the if-expression (thus not needing semicolons), you have the already-existing <div>
{ x > 0 ? 'x is positive'
: x < 0 ? 'x is negative'
: 'x is zero'
}
</div> |
@RaoulSchaffranek Also: There is ASI, a feature that personally I use liberally, and which allowing you to “forget” semicolons. But... an example is worth thousand words. A programmer writing consciously their semicolons: const a = if (true) 1;
['foo', 'bar'].forEach(f); Another programmer relying on ASI for inserting for them, but carefully adding them manually before statements beginning with dangerous characters: const a = if (true) 1
;['foo', 'bar'].forEach(f) Both are wrong, because both programs are equivalent to: const a = (if (true) 1; else 0;)['foo', 'bar'].forEach(f); |
What would everyone make of formalising this with the following syntax: Here's an example prompted by https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function. The motivation was simple: I hate using // original
async function getProcessedData(url) {
let v;
try {
v = await downloadData(url);
} catch(e) {
v = await downloadFallbackData(url);
}
return processDataInWorker(v);
}
// proposed:
async function getProcessedData(url) {
const v = do-try {
await downloadData(url);
} catch(e) {
await downloadFallbackData(url);
}
return processDataInWorker(v);
}
// variants:
// await do/do-[x] -- allows implicit await from last statement of blocks
// whitespace allowed between do- and [x] -- allows for nicer indentation below
async function getProcessedData(url) {
const v = await do-
try { downloadData(url); }
catch(e) { downloadFallbackData(url); }
return processDataInWorker(v);
} |
I'd prefer if the do weren't necessary, but otherwise I like the idea of using I'd prefer |
It's irked me for quite a while that I see the issue in @claudepache 's comment about the ASI yielding the wrong results in that specific edge case, but I would take the double semicolon solution above the rest of the verbosity being suggested. Hyphenating and introducing more keywords just adds noise to the language, imho. |
In the interests of consolidating discussion, I'm going to close this issue in favor of #39, which is the same proposal but has more discussion. Please see my most recent comment there, in particular. |
I was playing with this plugin a bit, and this is very related to #7, I don't like excessive indentation for a simple if else statement in JSX. I use prettier, and maybe there could be something changed there as well, but look at this. I have a button here:
Now I realise, that, before I end the timer, I need to check if the timer is started. So I use an if else construct:
Suddenly, the button is 6 spaces more indented! And a lot of more extra visual noise. Now since keywords like
if
,switch
,for
,try
can't be used in an expression context, can't we automatically (behind the scenes), make ado
expression construction of this if we encounter such keywords in a expression context. What I mean, could we allow to write the above like this:The text was updated successfully, but these errors were encountered: