tc39 / proposal-dynamic-import

import() proposal for JavaScript
https://tc39.github.io/proposal-dynamic-import/
MIT License
1.86k stars 47 forks source link

Trailing comma #67

Open danez opened 5 years ago

danez commented 5 years ago

Currently it is not allowed to have a trailing comma inside the import call, but maybe it would be nice to be able to do that.

The reasons for trailing commas are usually about adding/removing elements from a multiline list of elements. This obviously does not apply here as import always has one argument, but it still would be nice to have this possibility for the sake of consistency with function calls. Especially for code formatters I can see that it would be a little bit easier to treat import calls the same as function calls.

Was there already a decision on this and what's your opinion on this? Should I try to create a PR?

raulsebastianmihaila commented 5 years ago

I think that that would only accentuate the possible confusion with functions. I think we shouldn't make the import syntax look more like function calls than it already does, since it's not a function call, especially by allowing a trailing comma where it's impossible to add something after the comma. Code formatters are automatic, they shouldn't need too much help; developers need more help because they're humans and potential confusion should be reduced. A good editor with syntax highlighting should highlight the import syntax different from function syntax.

robpalme commented 5 years ago

Agreed. import() is not a function. It's more like typeof which equally does not support a trailing comma.

ljharb commented 5 years ago

However, the intention was to eventually allow a second “argument” - at which point, a trailing comma in the first argument would have to be allowed. The transition would be awkward - it’d be better to allow it now than add it later.

import is not a function, but import() is very much like a function call.

raulsebastianmihaila commented 5 years ago

I would say that import() is more unlike a function call. It's not clear what transition would be awkward (for who, from what to what?) and why. However, assuming the intent is to help with simplifying parsing the code, then I imagine that it should also be allowed for the import pseudofunction to receive any number of arguments (and of course semantically all of them should be evaluated) as it is with any normal function call. Otherwise, I imagine that allowing a trailing comma alone would not simplify much.

ljharb commented 5 years ago

@raulsebastianmihaila what i mean is, let’s say next year we allow a second argument in import(), for providing metadata or something (not relevant here). Suddenly, it would become valid to have a trailing comma after the first argument, because a second is optional - and people would likely want to enforce the comma-dangle eslint rule, which would mean they’d have to go in and add (or autofix) the trailing comma everywhere.

In other words, the entire point of having a trailing comma (from a style POV) is to keep diffs smaller when you need to later add an item to the list - ie, to avoid having to add the comma in order to add the item. (I agree there’s no parsing advantage or disadvantage either way)

hax commented 5 years ago

I think most programmers think import() is very like an async function even they know it's not a real function. At least it is much more like function than typeof. You can write typeof x without parens, and x could be an undeclared variable which never allowed in any other place, so obviously typeof is not a function. But it's hard to say why import() is not a function, though you can only "direct call" it and can't use it indirectly.

It's a bit like eval. eval is a function due to historical reason (so eval() can have trailing comma even it only have one parameter), we have two different semantic for eval of direct call and indirect call. Direct call of eval() is very same as import(). For example, both have implicit context related --- scope for eval, baseurl for import.

Technically, we could also make import a function, and make indirect call of import have the semantic like System.loader.import() in old whatwg loader spec.

Of coz it's not a good idea to repeat the mistake of introducing confusing direct/indirect call semantics. But I think it's ok to make the direct call syntax more consistent --- allow trailing comma in import().

pauldraper commented 4 years ago

If import() is really like typeof, then why not make it look that way?

As is, it appears that its authors wanted it to syntactically appear as a function.