Automatic Comma Insertion

As the name suggestions, what could become of the idea to adopt "automatic comma insertion" within ECMAScript in objects and functions, for example, all of the following will automatically insert commas between the newlines while parsing where it would otherwise without them throw a syntax error.

var obj = {
    a
    b
    c
}

var arr = [
    a
    b
    c
]

var fun = foo(
    a
    b
    c
)

function foo (
   a
   b
   c
) {
    return
}

Given how horrific automatic semicolon insertion is - and the only thing that those of us that use semicolons, and those of us that don't, can agree on is that the only thing worse than "you have to do the thing you don't like" is "the current situation where you can do either one", this seems like it would be a strict detriment to the language.

5 Likes

I do like it when languages make commas optional like that. However, such a language needs to be built from the ground up with support for this sort of thing in mind. Otherwise, like @ljharb said, we'll just end up with another ASI-like catastraphy.

For example, how do you parse something like this?

const array = [
  2 * (3 + 3)
  (4 + 4) * 5
];

Right now this is valid JavaScript, and would get interpretted as trying to call a number as a function, which yes, it is a runtime error, but it's still valid and we can't break that. (i.e. to JavaScript, that looks like "2 * (3 + 3)(4 + 4) * 5"). We can't break this current behavior, and would still need JavaScript to interpret this in the same way.

Generally, for a language to get around this sort of issue, they have to be new-line sensitive, treating new lines as a statement terminator, or in this case, as a comma. JavaScript isn't sensitive to the presence of new lines (sometimes it's aware of their absence though), and we can't retroactively make it sensitive in these sorts of scenarios without breaking backward compatibility or introducing a new kind of ASI, making this sort of feat practically impossible.

3 Likes

I agree. And parentheses are not the only problematic things. What about these examples?

const array1 = [
    1
    +"1"
];
// Is this [1, 1] or ["11"] ?

const array2 = [
    !
    true
];
// Would this cause an error?

function func(){
    return true
    false
}
// What gets returned here?
// Do we treat the line break as a semicolon or a comma?

I can see automatic commas cause many problems and confusion if they were included.

1 Like

I have a different proposal: optional parentheses in statement conditions. Basically, Rust-like/Go-like statement syntax:

while c {...}

if true {...}

for let i = 0; i < a.length; i++
{
  ...
}

for const x of iter {
  ...
}

Making more syntax optional for purely aesthetic reasons objectively introduces style debates and the necessity for linter rules, and subjectively makes the language less explicit.

I think that even were they optional, it would be a far better style to require them (much as virtually all styleguides require the use of curly braces, invocation parens, etc) - so it seems better not to add the optionality.

1 Like

I agree with this. Changing syntax for aesthetic reasons is a job for CoffeeScript anyways