k1r0s/kaop-ts

Function calls are not supported in decorators - Angular 7

Nawsen opened this issue · 3 comments

Hi,

When executing ng build --prod the following error appears.

ERROR in Error during template compile of 'AppComponent'
  Function calls are not supported in decorators but 'beforeMethod' was called in 'log'
    'log' calls 'beforeMethod' at src\app\kaop-test.ts(4,10).

This only happens with AOT enabled.

Example repo: kaop-ts-issue

k1r0s commented

Hi @Nawsen,

After reading angular issues everywhere seems like aot compiler made lots of people happier with that change.

I find a fix by explicitly using join point decorator on the method and importing the advice rather than creating a new decorator expression.

  @beforeMethod(log) // < works
  decoratorTest(testString: string): void {
    console.log('hello');
  }

I don't understand very well what compiler is trying to achieve by forcing developers to do it that way.

workaround: Nawsen/kaop-ts-issue#1

please @mgechev, since you were on AOT stuff, AOP as well, can you give us some advice to properly solve this problem?

@k1r0s Thanks for the response!

How would you go about supplying the advice with params from the component?
What we did before Angular 7:

 @log('important')
 decoratorTest(testString: string): void {
    console.log('hello');
 }
export function log(status: string) {
  return beforeMethod(meta => {
    console.log(status + ' annotation logging...');
  });
}

As I see it, with your workaround this is not possible anymore, correct?

k1r0s commented

it would be possible by just by doing a HOF:

export const log = (...args) => meta => {
  console.log(...args)
}

@beforeMethod(log('important'))
decoratorTest(testString: string): void {
  console.log('hello');
}

I know that this is quite weird but I don't know who to blame