Skip to content

Commit

Permalink
feat(napi/transform): support enabling emitDecoratorMetadata (#9190)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Feb 19, 2025
1 parent 6c24357 commit 8a5051e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
10 changes: 10 additions & 0 deletions napi/transform/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ export interface DecoratorOptions {
* @default false
*/
legacy?: boolean
/**
* Enables emitting decorator metadata.
*
* This option the same as [emitDecoratorMetadata](https://www.typescriptlang.org/tsconfig/#emitDecoratorMetadata)
* in TypeScript, and it only works when `legacy` is true.
*
* @see https://www.typescriptlang.org/tsconfig/#emitDecoratorMetadata
* @default false
*/
emitDecoratorMetadata?: boolean
}

export interface ErrorLabel {
Expand Down
11 changes: 10 additions & 1 deletion napi/transform/src/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,22 @@ pub struct DecoratorOptions {
/// @see https://www.typescriptlang.org/tsconfig/#experimentalDecorators
/// @default false
pub legacy: Option<bool>,

/// Enables emitting decorator metadata.
///
/// This option the same as [emitDecoratorMetadata](https://www.typescriptlang.org/tsconfig/#emitDecoratorMetadata)
/// in TypeScript, and it only works when `legacy` is true.
///
/// @see https://www.typescriptlang.org/tsconfig/#emitDecoratorMetadata
/// @default false
pub emit_decorator_metadata: Option<bool>,
}

impl From<DecoratorOptions> for oxc::transformer::DecoratorOptions {
fn from(options: DecoratorOptions) -> Self {
oxc::transformer::DecoratorOptions {
legacy: options.legacy.unwrap_or_default(),
emit_decorator_metadata: false,
emit_decorator_metadata: options.emit_decorator_metadata.unwrap_or_default(),
}
}
}
Expand Down
37 changes: 37 additions & 0 deletions napi/transform/test/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,41 @@ describe('legacy decorator', () => {
"
`);
});

describe('emitDecoratorMetadata', () => {
it('matches output', () => {
const code = `
export default @dce class C {
@dce
prop = 0;
method(@dce param) {}
}
`;
const ret = transform('test.tsx', code, {
decorator: {
legacy: true,
emitDecoratorMetadata: true,
},
});
expect(ret.code).toMatchInlineSnapshot(`
"import _decorateMetadata from "@oxc-project/runtime/helpers/decorateMetadata";
import _decorate from "@oxc-project/runtime/helpers/decorate";
import _decorateParam from "@oxc-project/runtime/helpers/decorateParam";
let C = class C {
prop = 0;
method(param) {}
};
_decorate([dce, _decorateMetadata("design:type", Object)], C.prototype, "prop", void 0);
_decorate([
_decorateParam(0, dce),
_decorateParam(0, _decorateMetadata("design:type", Function)),
_decorateParam(0, _decorateMetadata("design:paramtypes", [Object])),
_decorateParam(0, _decorateMetadata("design:returntype", void 0))
], C.prototype, "method", null);
C = _decorate([dce], C);
export default C;
"
`);
});
});
});

0 comments on commit 8a5051e

Please sign in to comment.