-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmacro.ts
More file actions
24 lines (22 loc) · 943 Bytes
/
Copy pathmacro.ts
File metadata and controls
24 lines (22 loc) · 943 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { createMacro, MacroHandler } from 'babel-plugin-macros';
import { transform } from './transform';
const metadataMacro: MacroHandler = ({ references }) => {
references.default.forEach(reference => {
const decorator = reference.findParent(parent => parent.isDecorator());
if (!decorator) {
throw new Error("Metadata macro should be used as class decorator");
}
const classDeclaration = decorator.findParent(parent => parent.isClassDeclaration());
if (!classDeclaration) {
throw new Error("Metadata macro should be used as class decorator");
}
if (classDeclaration.isClassDeclaration()) {
if (!classDeclaration.node.decorators) {
classDeclaration.node.decorators = [];
}
transform(classDeclaration);
classDeclaration.node.decorators = classDeclaration.node.decorators.filter(it => it !== decorator.node);
}
})
};
export default createMacro(metadataMacro);