-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (38 loc) · 1.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const handledByTransformTypescript = path =>
path.node.declare || path.node.id.type === 'StringLiteral';
const visitor = {
TSModuleDeclaration: {
enter(modulePath) {
if (!handledByTransformTypescript(modulePath)) {
// Make sure there are only allowed node types inside
modulePath.get('body').traverse({
enter(path) {
if (!path.isModuleDeclaration()) {
throw path.buildCodeFrameError(
'Namespaces must only contain type and interface declarations'
);
}
},
blacklist: [
'TSModuleDeclaration', // nested ones will get their own visitor passes
'TSTypeAliasDeclaration',
'TSInterfaceDeclaration',
],
});
}
},
exit(modulePath) {
if (!handledByTransformTypescript(modulePath)) {
modulePath.remove();
}
},
},
};
module.exports = ({ types: t }) => ({
visitor: {
// Gotta run before plugin-transform-typescript
Program(path) {
path.traverse(visitor);
},
},
});