proposal-decorator-metadata icon indicating copy to clipboard operation
proposal-decorator-metadata copied to clipboard

Does method replace/reuse break the basic use case?

Open senocular opened this issue 2 years ago • 0 comments

Given this example from the readme:

const METADATA = new WeakMap();

function meta(value) {
  return (_, context) => {
    METADATA.set(context.metadataKey, value);
  };
}

@meta('a')
class C {
  @meta('b')
  m() {}
}

METADATA.get(C[Symbol.metadata]); // 'a'
METADATA.get(C.m[Symbol.metadata]); // 'b'

Would this fall apart if another decorator was involved that also replaced the method with a function that it could also use for other replacements? For example something like...

const env = { inProd: true }
const NO_OP = () => {}

function noopInProd() {
  if (env.inProd) {
    return NO_OP
  }
}

Which when used with the original example (with an additional method)

@meta('a')
class C {
  @meta('b')
  @noopInProd
  m() {}

  @meta('c')
  @noopInProd
  m2() {}
}

METADATA.get(C.m[Symbol.metadata]); // 'c'?
METADATA.get(C.m2[Symbol.metadata]); // 'c'?

Since these methods would now be the same function, wouldn't they give the same metadata? (Also, I assume the gets are supposed to be referring to the methods through C.prototype rather than C directly?)

senocular avatar May 16 '22 23:05 senocular