-
Notifications
You must be signed in to change notification settings - Fork 127
/
typescript.js
33 lines (29 loc) · 1013 Bytes
/
typescript.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
const path = require('path')
const ts = require('typescript')
const typeConverter = require('./typescript/type-converter')
exports.handlers = {
newDoclet: function({ doclet }) {
if (doclet.tags && doclet.tags.length > 0) {
const categoryTag = doclet.tags.find(tag => tag.title === 'optional')
if (categoryTag) {
doclet.optional = true
}
}
},
beforeParse: function(e) {
if (['.ts', '.tsx'].includes(path.extname(e.filename))) {
// adding const a = 1 ensures that the comments always will be copied,
// even when there is no javascript inside (just interfaces)
let result = ts.transpileModule('const _____a = 1; \n' + e.source, {
compilerOptions: {
target: 'esnext',
esModuleInterop: true,
jsx: path.extname(e.filename) === '.tsx' ? 'react' : null,
}
})
const types = typeConverter(e.source, e.filename)
let src = result.outputText
e.source = src + '\n' + types
}
}
}