Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JSLint: "Wrap a ternary expression in parens, ..."

143 views
Skip to first unread message

emf

unread,
Jul 14, 2022, 4:44:28 PM7/14/22
to
Hi.

JSLint now does not like this code (that it used to find OK), saying

"Wrap a ternary expression in parens, with a line break after
the left paren."

=================
orb = (orb0Btn.checked)
? 0
: (orb5Btn.checked)
? 5
: (orb10Btn.checked)
? 10
: (orb15Btn.checked)
? 15
: 20;
=================

What is now the correct syntax for the ternary expression?

Thanks,

Eustace
--
Natal Transits Calculator
https://emf.neocities.org/nt/nataltransits2.html

Michael Haufe (TNO)

unread,
Jul 15, 2022, 11:40:15 AM7/15/22
to
On Thursday, July 14, 2022 at 3:44:28 PM UTC-5, emf wrote:
> Hi.
>
> JSLint now does not like this code (that it used to find OK), saying
>
> "Wrap a ternary expression in parens, with a line break after
> the left paren."
>
> =================
> orb = (orb0Btn.checked)
> ? 0
> : (orb5Btn.checked)
> ? 5
> : (orb10Btn.checked)
> ? 10
> : (orb15Btn.checked)
> ? 15
> : 20;
> =================
>
> What is now the correct syntax for the ternary expression?

It's just a jslint setting. You can turn that off I'm pretty sure.

I think it wants:

(
p ? truePart : (
p ? truePart : falsePart
)
)


I think this is because nested ternaries can be a little hard to read if you don't format them properly.

I prefer ESLint


emf

unread,
Jul 15, 2022, 2:19:26 PM7/15/22
to
On 2022-07-15 02:27, Stefan Ram wrote:
> emf <emf...@gmail.com> writes:
>> What is now the correct syntax for the ternary expression?
>
> IIRC, JSLint does not check syntax but its authors idea of style.
>
> The syntax for the conditional expression is
>
> ConditionalExpression[In, Yield, Await] :
> ShortCircuitExpression[?In, ?Yield, ?Await]
> ShortCircuitExpression[?In, ?Yield, ?Await] ? AssignmentExpression[+In, ?Yield, ?Await] :
> AssignmentExpression[?In, ?Yield, ?Await]
>
> , so, you can just write, for example, "0?1:1?2:3"
> (which would evaluate to 2).

Brilliant! :)


emf

unread,
Jul 15, 2022, 2:24:37 PM7/15/22
to
OK, JSLint does not find anything wrong with this

orb = ((orb0Btn.checked)
? 0
: (orb5Btn.checked)
? 5
: (orb10Btn.checked)
? 10
: (orb15Btn.checked)
? 15
: 20);

The only now warning is:

1. Bad assignment to 'panel'. 89.5
panel = document.getElementById("panel");

however the program (see sig below in (nataltransits2.js) still
does not work...

Michael Haufe (TNO)

unread,
Jul 16, 2022, 11:37:35 AM7/16/22
to
On Friday, July 15, 2022 at 1:24:37 PM UTC-5, emf wrote:
> [...]
> The only now warning is:
>
> 1. Bad assignment to 'panel'. 89.5
> panel = document.getElementById("panel");
>
> however the program (see sig below in (nataltransits2.js) still
> does not work...
> Eustace

ESLint points out a number of issues remaining:

<https://emf.neocities.org/nt/nataltransits2.js>

11:9 'ephem' is not defined. (no-undef)

15:16 'ephem' is not defined. (no-undef)

19:9 'ephem' is not defined. (no-undef)

20:9 'ephem' is not defined. (no-undef)

29:9 'reader' is not defined. (no-undef)

30:16 'reader' is not defined. (no-undef)

38:5 'reader' is not defined. (no-undef)

39:5 'reader' is not defined. (no-undef)

40:5 'reader' is not defined. (no-undef)

49:16 'getBDay' is not defined. (no-undef)

81:9 'findTransits' is not defined. (no-undef)

83:9 'panel' is not defined. (no-undef)

89:5 'panel' is not defined. (no-undef)

91:22 'e' is defined but never used. (no-unused-vars)


You have to declare a variable before assigning to it.

emf

unread,
Jul 16, 2022, 5:28:15 PM7/16/22
to
On 2022-07-16 08:19, Stefan Ram wrote:
> r...@zedat.fu-berlin.de (Stefan Ram) writes:
>> orb =
>> orb0Btn.checked? 0:
>> orb5Btn.checked? 5:
>> orb10Btn.checked? 10:
>> orb15Btn.checked? 15:
>> 20;
>
> Or,
>
> orb =
> orb0Btn. checked? 0:
> orb5Btn. checked? 5:
> orb10Btn.checked? 10:
> orb15Btn.checked? 15:
> 20;
>
> . Now, imagine that someone does not know all details of the
> ternary operator well, but has a basic idea of it. In which
> way could he be mislead by this?

Thanks! Your idea is very simple and easy to understand!

emf

emf

unread,
Jul 16, 2022, 5:46:28 PM7/16/22
to
There was only 1 warning because in the process of JSLinting I
had added imported globals. This was nataltransits2.js, and
there is also a nataltransits.js,

Well, I have to start checking the program from the beginning to
find out where the bug is. Then I may seek the advise of this
newsgroup again.

The thing is that the webpage was working fine before... I'm
thinking of starting to comment out one "use strict" at a time,
to see if at some point the program will start working again...

Thanks to all,
0 new messages