Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal for decorator support #43

Closed
renaudpawlak opened this issue Jan 29, 2016 · 21 comments
Closed

Proposal for decorator support #43

renaudpawlak opened this issue Jan 29, 2016 · 21 comments
Milestone

Comments

@renaudpawlak
Copy link
Contributor

We need to support decorators: microsoft/TypeScript#2249

To support decorators, I propose a meta annotation @decorator.

For instance (this is still approximate code):

@Decorator
public @interface readonly {
}

@Decorator
public @interface enumerable {
     boolean value();
}

public class C {
  @readonly
  @enumerable(false)
  void method() { }
}

public static Globals {
    public static <T> void readonly(Function target, String key, TypedPropertyDescriptor<T> descriptor) {
        descriptor.writable = false;
    }

    public static <T> TypedPropertyDescriptor<T> function enumerable(value) {
        return function ((Function target, String key, TypedPropertyDescriptor<T> descriptor) =>  {
           descriptor.enumerable = value;
        });
    }
}

Would transpile to (approximate code also):

class C {
  @readonly
  @enumerable(false)
  method() { }
}

function readonly(target, key, descriptor) {
    descriptor.writable = false;
}

function enumerable(value) {
  return function (target, key, descriptor) {
     descriptor.enumerable = value;
  }
}

This this just a proposal that needs to be refined. In particular, the definition of the global decoration methods could be improved and the overall pattern needs to be validated.

@renaudpawlak renaudpawlak added this to the 1.1.0 milestone Jan 29, 2016
@lgrignon
Copy link
Collaborator

Important point, for sure.
You did not mention that we may need to declare "ambient" decorators (from candies, or home made js libs). However, it seems trivial since we could only declare the @decorator public @interface ... without declaring the function. Still, there could be a problem if we decide to raise an error if the function related to the decorator does not exist. There, we could annotate @decorator with @ambient.

What do you think about (1) check that the decorator function exists (2) annotate with Ambient in candies and local declarations?

Thanks.

@kiamesdavies
Copy link

Please, any progress with decorators? I really want to get started with Angular 2 and beloved JSweet

@renaudpawlak
Copy link
Contributor Author

Well... there has been more demand on React, so I did it first. But I am really impatient to make Angular 2 to work! Now that React works, Angular 2 will probably be one of the next things to be done. I'll keep you updated.

@kiamesdavies
Copy link

Wow, thanks for the prompt response. Will delay our project and focus on
the server side for now, anxiously waiting.

Good job.
On Apr 21, 2016 5:45 PM, "Renaud Pawlak" [email protected] wrote:

Well... there has been more demand on React, so I did it first. But I am
really impatient to make Angular 2 to work! Now that React works, Angular 2
will probably be one of the next things to be done. I'll keep you updated.


You are receiving this because you commented.
Reply to this email directly or view it on GitHub
#43 (comment)

@renaudpawlak renaudpawlak modified the milestones: 1.2.0, 1.1.0 May 22, 2016
@kito99
Copy link

kito99 commented Sep 4, 2016

@renaudpawlak, I'm doing a JavaOne presentation on JSweet and I was hoping to use it with Angular2. What's the status of this? Are there any other barriers to Angular2 support?

@renaudpawlak
Copy link
Contributor Author

Hi. Excellent initiative! Well, AFAIK, this is the only barrier (but I cannot be sure until annotations are supported). Actually, all boils down to the core barrier: having more time ;)
I could give it a try (but I cannot promise it will work perfectly). When would be your final date to have it working?

@kito99
Copy link

kito99 commented Sep 5, 2016

Thanks for the quick response, @renaudpawlak.

Well, my presentation is in exactly two weeks, so ASAP :-). Here is a link: https://oracle.rainfocus.com/scripts/catalog/oow16.jsp?event=javaone&search=jsweet&search.event=javaone.

I realize that's a very short time frame, so it may be better to shoot for December. I'm giving the same presentation again (although probably to a much smaller audience): https://live360events.com/Events/Orlando-2016/Sessions/Tuesday/ADT06-Full-Stack-Java-with-JSweet-Angular-2-PrimeNG-and-JAXRS.aspx.

If Angular2 support isn't possible with JSweet, I'll probably change the JavaOne session to just use TypeScript on the front-end.

@renaudpawlak
Copy link
Contributor Author

I will give it a first try today (I have just finished something and I have a free slot)... we will see how it goes... maybe it will go very smoothly. I'll keep in touch.

@renaudpawlak
Copy link
Contributor Author

Ok! I have good news. I managed to create an Angular 2 quickstart with JSweet. That's cool :)
Since translating the Angular definitions automatically seemed to pose many problems, and because of the short deadline to have something working, I have decided to create the Angular 2 definitions in the project itself. Eventually these definitions (def.angular package) will be bundled in a candy, but right now, you will need to extend them for your own needs.

So, I have a first support of decorators, only for types. It follows this proposal. It will be extended to variables, functions, and parameter decorators, but the most important for Angular 2 is done.

One really cool thing about this project is that JSweet just converts to TypeScript and tsc takes care of the TS to JS conversion (there is a new option jsOnly, which allows JSweet to let an external tsc conf to take care of the JavaScript generation). This is a first but I think it could become a more common use case as TS declarations get more and more easily available.

All the details on how to use in the project: https://github.com/cincheo/jsweet-angular2-quickstart

Please tell me if this is a good start for you or if something is missing or unclear.

I hope you will enjoy it as much as I enjoyed making Angular 2 work with JSweet ;)

@kito99
Copy link

kito99 commented Sep 6, 2016

Wow! Thanks so much, @renaudpawlak! So far it looks great, but I'll take a deeper look at it tomorrow!

After we get through the Angular stuff, we can talk about PolymerTS ;-).

@lgrignon
Copy link
Collaborator

lgrignon commented Sep 7, 2016

Haha, actually we already got this one running ;) take a look at:
https://github.com/lgrignon/jsweet-cordova-polymer-example

@renaudpawlak
Copy link
Contributor Author

@kito99 do you need anything else for your presentation? Don't hesitate to tell me if anything is unclear or if you hit any issue.

@kito99
Copy link

kito99 commented Sep 9, 2016

Thanks for pointing that out, @lgrignon. I'll have to take a closer look when I get a chance. I'm using PolymerTS on currently, so I'd love to see the same support for annotations/decorators ;-).

@kito99
Copy link

kito99 commented Sep 9, 2016

@renaudpawlak, I just got it working locally. I had to add the repositories to pom.xml:

  <repositories>
    <repository>
      <id>jsweet-central</id>
      <name>libs-release</name>
      <url>http://repository.jsweet.org/artifactory/libs-release-local</url>
    </repository>
    <repository>
      <snapshots />
      <id>jsweet-snapshots</id>
      <name>libs-snapshot</name>
      <url>http://repository.jsweet.org/artifactory/libs-snapshot-local</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>jsweet-plugins-release</id>
      <name>plugins-release</name>
      <url>http://repository.jsweet.org/artifactory/plugins-release-local</url>
    </pluginRepository>
    <pluginRepository>
      <snapshots />
      <id>jsweet-plugins-snapshots</id>
      <name>plugins-snapshot</name>
      <url>http://repository.jsweet.org/artifactory/plugins-snapshot-local</url>
    </pluginRepository>
  </pluginRepositories>

I also had to run mvn install before running mvn generate-sources.

So far, so good!

@renaudpawlak
Copy link
Contributor Author

renaudpawlak commented Sep 9, 2016

Grrr. I always forget the repos. Thank you for reporting. I have just pushed them.
I don't see why you'd have to run mvn install first... I'll have to start fresh when possible to understand, I guess.

@kito99
Copy link

kito99 commented Sep 9, 2016

@renaudpawlak, how hard would it be to create a candy for PrimeNG (https://github.com/primefaces/primeng)?

@kito99
Copy link

kito99 commented Sep 9, 2016

I'll start leaving issues/questions in the angular2-starter project. I've already found a few things.

@renaudpawlak
Copy link
Contributor Author

I have just created a quickstart project to show how to create candies from an existing JS lib: https://github.com/cincheo/jsweet-candy-js-quickstart

As you can see, it is pretty straightforward, and I don't see any limitations to the approach (can be used to write a primeng candy)... but I still need to document better how to write definitions with JSweet.

@kito99 please do ask your questions. Maybe you want to share an example project and maybe we could build a first angular 2 candy for the occasion. Don't hesitate to use my private email when it gets too much into the details.

@renaudpawlak renaudpawlak modified the milestones: 1.3.0, 1.2.0 Dec 3, 2016
@renaudpawlak renaudpawlak modified the milestones: 2.0.0, 1.3.0, 2.1.0 May 3, 2017
@mindhivefi
Copy link

Is there something changed with decorator support or am I doing something wrong? The transpiler makes the references to annotations right but it does not compile the actual annotation ts-classes at all. With the latest transpiler I qet error like:
...ERROR output:48 - cannot find module './TestAnnotation' at ...`

@renaudpawlak
Copy link
Contributor Author

Current snapshot (2.0.0-SNAPSHOT) supports class, method, and field decorators. See the related test to see what is supported exactly. If it does not work as in the test, it might be a configuration issue (or an edge-case bug).

@mindhivefi
Copy link

Okey. Your example helped me forward. Thanks!

It seems that the decorator can not be defined on separate file. If I add all decorations to same java file where the Globals are defined it works. I don't know yet very well how JSweet works from inside, but I can guess there is a natural explanation why it works like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants