javascript-decorators icon indicating copy to clipboard operation
javascript-decorators copied to clipboard

The use of comma seems inconsistent between use cases

Open otakustay opened this issue 9 years ago • 4 comments

These cases are tested with babel

For a class decorator, it MUST NOT ends with a comma:

@foo
class Foo {}

// This is NOT OK
@foo;
class Foo {}

For a class method / property decorator, it CAN ends with a comma:

// Both are OK
class Foo {
  @foo
  foo() {}

  @foo;
  bar() {}

  @foo
  tom = 0;

  @foo;
  jack = 1;
}

For an object method decorator, it MUST NOT ends with a comma:

var bar = {
  @foo
  foo() {}

  // This is NOT OK
  @foo;
  bar() {}
}

Why don't we allow comma in all cases so the syntax could be consistent, I think ending with a comma is more safe

otakustay avatar Jul 11 '15 04:07 otakustay

No worries but ; is a semicolon, , is a comma

jamiebuilds avatar Jul 11 '15 17:07 jamiebuilds

This is likely a deliberate decision. Semicolon terminators imply a statement list and the execution of decorators is different in that it's more analogous to function calls.

sebmck avatar Jul 11 '15 18:07 sebmck

It's just weird to have different syntax between class method and plain object method, for classes we could add a semicolon after decorator to eliminate syntax conflicts like class Foo { @foo['foo' + 'bar']() { ... } }, however this could also happen in object methods which doesn't allow semicolons after decorator

otakustay avatar Jul 13 '15 04:07 otakustay

Close, invalid? Weird stuff here, definitely.

For clarification, it might be that the babel parser just ignores the extra semicolon and will apply the decorator just the same. So this might be an issue with babel rather?

silkentrance avatar Feb 28 '16 23:02 silkentrance