microsoft / typespec

https://typespec.io/
MIT License
4.41k stars 207 forks source link

Cannot name a decorator `flags` `lib` `linter` or `onEmit` (special exports) #3719

Closed timotheeguerin closed 2 months ago

timotheeguerin commented 3 months ago

Problem

Cannot name a decorator flags lib linter or onEmit (special exports) Due to how to use certain reserved name in JS we cannot name a decorator with those names

Add a new $decorators export

export const $decorators = {
  Azure.Core: {
    flags: $flags,
    "foo-bar": fooBarDecorator
  }
}

we can additionally provide a typed helper

function defineDecorators(decorators: Record<string, Record<string, DecoratorFunction>>) {
   return decorators;
}
export const $decorators = defineDecorators({
  Azure.Core: {
    flags: $flags,
    "foo-bar": fooBarDecorator
  }
})

Additional items

timotheeguerin commented 3 months ago

Option 1: USe $$ for known configs

This would reserve $ for decorators. For back compat we would still check if the $ variant of $lib, $flags, etc. is a config object and use that instead.

Issues with this approach:

  1. Still cannot exports reserved js names or non js identifiers that could technically be valid decorator names(e.g. foo-bar)
  2. Subset of above How do I name a decorator @$flags

Option 2: Dedicated $decorators export

this allows to exports all decorators

export const $decorators = {
  flags: $flags,
  "foo-bar": fooBarDecorator
}

this could even solve the confusing way namespace are defined in JS

export const $decorators = {
  Azure.Core: {
    flags: $flags,
    "foo-bar": fooBarDecorator
  }
}
markcowl commented 2 months ago
markcowl commented 2 months ago

est: 5