Skip to content
This repository was archived by the owner on Mar 31, 2025. It is now read-only.

Commit 823d3f9

Browse files
davidreherpetebacondarwin
authored andcommitted
fix(typescript/readTypeScriptModules): getReturnType should cope with computed initializers
Closes #187
1 parent 18193fe commit 823d3f9

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export class Parent {
2+
someProp = {
3+
foo: 'bar',
4+
};
5+
}
6+
7+
export class Child extends Parent {
8+
someProp = Object.assign(this.someProp, {
9+
bar: 'baz'
10+
});
11+
}

typescript/processors/readTypeScriptModules.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,16 @@ module.exports = function readTypeScriptModules(tsParser, modules, getFileInfo,
383383
// The symbol does not have a "type" but it is being initialized
384384
// so we can deduce the type of from the initializer (mostly).
385385
if (declaration.initializer.expression) {
386-
return declaration.initializer.expression.text.trim();
387-
} else {
388-
return getType(sourceFile, declaration.initializer).trim();
386+
var initializerExpressionText = declaration.initializer.expression.text;
387+
var intrinsicNameFromTypeChecker = typeChecker.getTypeOfSymbolAtLocation(symbol, sourceFile).intrinsicName;
388+
// we might not have an expression text.
389+
if (initializerExpressionText) {
390+
return initializerExpressionText.replace(/\s+/g, ' ').trim();
391+
} else if (intrinsicNameFromTypeChecker) {
392+
return intrinsicNameFromTypeChecker.trim();
393+
}
389394
}
395+
return getType(sourceFile, declaration.initializer).trim();
390396
}
391397
}
392398

typescript/processors/readTypeScriptModules.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,23 @@ describe('readTypeScriptModules', function() {
180180
expect(moduleDoc.name).toEqual('privateModule');
181181
});
182182
});
183+
184+
describe('getReturnType', function () {
185+
it('should not throw if "declaration.initializer.expression.text" is undefined', function () {
186+
processor.sourceFiles = ['getReturnType.ts'];
187+
var docs = [];
188+
expect(function () { processor.$process(docs); }).not.toThrow();
189+
});
190+
191+
it('should try get the type from the typeChecker if possible', function () {
192+
processor.sourceFiles = ['getReturnType.ts'];
193+
var docs = [];
194+
processor.$process(docs);
195+
196+
var overriddenSomePropDoc = _.last(docs);
197+
expect(overriddenSomePropDoc.returnType).toEqual('any');
198+
});
199+
});
183200
});
184201

185202
function getNames(collection) {

0 commit comments

Comments
 (0)