Skip to content

Commit

Permalink
fix: add support to emit decorator metadata (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi authored Feb 20, 2023
1 parent 9b698b7 commit e947bec
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
node_modules
dist
*.log*
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"acorn": "^8.8.2",
"babel-plugin-dynamic-import-node": "^2.3.3",
"babel-plugin-parameter-decorator": "^1.0.16",
"babel-plugin-transform-typescript-metadata": "^0.3.2",
"changelogen": "^0.4.1",
"config": "^3.3.9",
"create-require": "^1.1.1",
Expand All @@ -63,11 +64,13 @@
"pirates": "^4.0.5",
"pkg-types": "^1.0.2",
"prettier": "^2.8.4",
"reflect-metadata": "^0.1.13",
"semver": "^7.3.8",
"terser-webpack-plugin": "^5.3.6",
"ts-loader": "^9.4.2",
"tslib": "^2.5.0",
"typescript": "^4.9.5",
"vite": "^4.1.2",
"vitest": "^0.28.5",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.1"
Expand Down
24 changes: 20 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ export default function transform(opts: TransformOptions): TRANSFORM_RESULT {
require("@babel/plugin-transform-typescript"),
{ allowDeclareFields: true },
]);
// `unshift` because this plugin must come before `@babel/plugin-syntax-class-properties`
_opts.plugins.unshift([
require("@babel/plugin-proposal-decorators"),
{ legacy: true },
]);
// `unshift` because these plugin must come before `@babel/plugin-syntax-class-properties`
_opts.plugins.unshift(
[require("babel-plugin-transform-typescript-metadata")],
[require("@babel/plugin-proposal-decorators"), { legacy: true }]
);
_opts.plugins.push(require("babel-plugin-parameter-decorator"));
_opts.plugins.push(require("@babel/plugin-syntax-import-assertions"));
}
Expand Down
9 changes: 7 additions & 2 deletions test/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,14 @@ Logical nullish assignment: 50 20"
`;
exports[`fixtures > typescript > stdout 1`] = `
"{
"Decorator metadata keys: design:type
Decorator called with 3 arguments.
Decorator called with 3 arguments.
Decorator called with 3 arguments.
Decorator called with 1 arguments.
{
file: '<cwd>/test.ts',
dir: '<cwd>',
resolve: '<cwd>/test.ts'
} undefined"
} undefined [class DecoratedClass]"
`;
20 changes: 17 additions & 3 deletions test/fixtures/typescript/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import "reflect-metadata";
function decorator(...args: any) {
console.log("Decorator called with arguments:", args);
console.log("Decorator called with " + args.length + " arguments.");
}

function anotherDecorator() {
return function (object: any, propertyName: any) {
console.log(
"Decorator metadata keys: " +
Reflect.getMetadataKeys(object, propertyName)
);
};
}

@decorator
export default class DecoratedClass {
@decorator
decoratedProperty = null;
@anotherDecorator()
decoratedProperty: string;

@decorator
get decoratedAccessor() {
Expand All @@ -16,4 +26,8 @@ export default class DecoratedClass {
decoratedFunction(@decorator decoratedParameter: any) {
return decoratedParameter;
}

constructor() {
this.decoratedProperty = "foo";
}
}
3 changes: 2 additions & 1 deletion test/fixtures/typescript/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import test, { FeedService } from "./test";
import Clazz from "./decorators";
export type { Test } from "./types";

console.log(test(), FeedService);
console.log(test(), FeedService, Clazz);

0 comments on commit e947bec

Please sign in to comment.