Skip to content

Commit

Permalink
Merge pull request SoftwareBrothers#182 from SoftwareBrothers/handle-…
Browse files Browse the repository at this point in the history
…ts-types-definitions

feat: handle TS types definitions
  • Loading branch information
ariansobczak-rst committed Jan 20, 2022
2 parents 9929ce0 + 8f19ed7 commit 637a208
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion fixtures/interface.ts → fixtures/typescript/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export enum PropertyPlace {
/**
* JSON representation of a Property.
*/
type PropertyJSON = {
interface PropertyJSON {
/**
* If given property should be treated as a title
*/
Expand Down
20 changes: 20 additions & 0 deletions fixtures/typescript/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Example of union type definition
*/
type SomeCombinedType = string | number | 'Specific string' | SomeOtherType;


/**
* Example of simple type definition
*/
type SomeOtherType = boolean;

/**
* Example of generic type definition with T as parameter
*/
type SomeGenericType<T> = T | SomeOtherGenericType<T> | { property: T, other: string }

/**
* Example of generic type definition with T as parameter
*/
type SomeOtherGenericType<T> = { children: T[] }
2 changes: 1 addition & 1 deletion tmpl/type.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
var data = obj;
var self = this;
data.forEach(function(name, i) { ?>
<span class="param-type"><?js= self.linkto(name, self.htmlsafe(name)) ?></span>
<code class="param-type"><?js= self.linkto(name, self.htmlsafe(name)) ?></code>
<?js if (i < data.length-1) { ?>|<?js } ?>
<?js }); ?>
5 changes: 5 additions & 0 deletions typescript/type-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ module.exports = function typeConverter(src, filename = 'test.ts') {
comment = appendComment(comment, `@typedef {object} ${name}`)
return convertMembers(comment, statement.type, src)
}
if (ts.isUnionTypeNode(statement.type) || ts.isSimpleInlineableExpression(statement.type)) {
let typeName = getTypeName(statement.type, src)
comment = appendComment(comment, `@typedef {${typeName}} ${name}`)
return convertMembers(comment, statement.type, src)
}
}
if (ts.isInterfaceDeclaration(statement)) {
comment = appendComment(comment, `@interface ${name}`)
Expand Down
9 changes: 1 addition & 8 deletions typescript/type-converter.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
const fs = require('fs')
const path = require('path')
const { expect } = require('chai')
const typeConverter = require('./type-converter')

const src = fs.readFileSync(path.join(__dirname, '../fixtures/typescript/entity.ts'), 'utf-8')

const interface1 = require('../fixtures/typescript/interface1')
const interface2 = require('../fixtures/typescript/interface2')
const interface3 = require('../fixtures/typescript/interface3')
Expand Down Expand Up @@ -98,8 +94,5 @@ describe('.typeConverter', function () {
})
})


// it.only('parses test', function() {
// console.log(typeConverter(src, '../fixtures/interface.ts'))
// })
// TODO: Provide tests for typescript
})

0 comments on commit 637a208

Please sign in to comment.