assemblyscript icon indicating copy to clipboard operation
assemblyscript copied to clipboard

Decorators are not valid here

Open ColinEberhardt opened this issue 4 years ago • 19 comments

The use of function decorators results in a TS compiler error, Decorators are not valid here.

@inline
export function floorDiv(a: i32, b: i32): i32 {
  return (a >= 0 ? a : a - b + 1) / b;
}

It also prevents the use of tools such as Prettier to format the code.

ColinEberhardt avatar Mar 17 '21 06:03 ColinEberhardt

It's problem of TypeScript and prettier, they both don't support top-level decorators

MaxGraey avatar Mar 17 '21 11:03 MaxGraey

It's problem of TypeScript and prettier, they both don't support top-level decorators

Agreed, but it is also a problem for AssemblyScript developers ;-)

In one case, it results in compilation errors that are not easy to understand, and in the other, it renders the tool unusable. It would be good to consider potential options that remove this friction.

ColinEberhardt avatar Mar 17 '21 11:03 ColinEberhardt

The only thing what we can do is implementing our own language server: https://github.com/Shopify/asls

For now you could add // @as-ignore: decorator one line below inline decorator which hide TS error. But not affect to prettifier unfortunately

MaxGraey avatar Mar 17 '21 11:03 MaxGraey

OK, that sounds like a big job! Considering how close AS is to TS, it is a shame that these small differences cannot be accommodated with the TS tooling directly.

ColinEberhardt avatar Mar 17 '21 13:03 ColinEberhardt

What's wrong with ESLint? Does it not perform as well as prettier? I've found it supports decorators fine and is what the main project uses.

willemneal avatar Mar 17 '21 13:03 willemneal

This has recently been mentioned in Discord, and there was the idea to introduce a comment-like alternative, say /*! @inline */ that is only interpreted as being part of the code by asc. Could even be extended to a general concept for actual code, not just decorators. It's more a hack, sadly, but perhaps that'd be useful? If not, I guess the alternatives are to not use decorators on functions in code that is meant to the portable, like the compiler itself does, or try to advocate for our use case in the tooling we rely on, or wait for actual function decorators to become a thing, if ever.

dcodeIO avatar Mar 17 '21 15:03 dcodeIO

I like this idea because it open opportunity implement more DSL-like annotations like doc-testing for example

MaxGraey avatar Mar 17 '21 15:03 MaxGraey

I like the idea of

// @ts-ignore: decorator
@lazy const x: u32 = 0;

Becoming

// @lazy
const x: u32 = 0;

willemneal avatar Mar 18 '21 17:03 willemneal

I very much like that idea as it would allow me to use some familiar and powerful tooling for my AS projects

ColinEberhardt avatar Mar 18 '21 18:03 ColinEberhardt

What's wrong with ESLint?

ESlint is a different type of tool, it’s focus is on linting, i.e. enforcing specific coding styles to create more safe and consistent code. Prettier is a formatter.

there is some overlap between the two, however, people often use both together.

ColinEberhardt avatar Mar 18 '21 19:03 ColinEberhardt

Yes, I would really love it if we could turn decorators into the comment-format... It would be great!

JairusSW avatar Apr 01 '21 19:04 JairusSW

This has recently been mentioned in Discord, and there was the idea to introduce a comment-like alternative, say /*! @inline */ that is only interpreted as being part of the code by asc. Could even be extended to a general concept for actual code, not just decorators. It's more a hack, sadly, but perhaps that'd be useful?

Adding a further note - when running AS as TS (as per the portability instructions), these function decorators also cause problems:

TSError: ⨯ Unable to compile TypeScript:
assembly/tz/rule.ts:3:1 - error TS1206: Decorators are not valid here.

Another reason why comment-style alternatives would be useful.

ColinEberhardt avatar May 27 '21 13:05 ColinEberhardt

bump keeping this issue open 😊

ColinEberhardt avatar Jun 27 '21 06:06 ColinEberhardt

any updates?

emretepedev avatar Sep 28 '23 13:09 emretepedev

any updates?

Actually not. @dcodeIO @MaxGraey @CountBleck WDYT? I think supporting comment-style decorator is helpful to reuse infrastructures in TS for example linter and formatter.

HerrCai0907 avatar Sep 28 '23 13:09 HerrCai0907

What I've wondered so far was how we'd annotate these. Imagine

// @inline

which could just be a commented out decorator in development, and we'd not want to recognize it as a decorator in this case. We'd need some format to indicate that this is intentional I guess.

dcodeIO avatar Sep 28 '23 19:09 dcodeIO

Maybe something as simple as a space after the @ or a triple slash could indicate "commented" decorators.

CountBleck avatar Sep 28 '23 21:09 CountBleck

I agree triple slash. Just like reference in TS

HerrCai0907 avatar Sep 28 '23 21:09 HerrCai0907

Maybe we shouldn't change anything, just wait for this new proposal: https://github.com/tc39/proposal-function-and-object-literal-element-decorators

MaxGraey avatar Feb 19 '24 22:02 MaxGraey