Skip to content

Commit

Permalink
fix: check for null variable when excludeNotExported is true (#694)
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng authored and aciccarello committed Feb 2, 2018
1 parent 3581d04 commit fa6d9fe
Show file tree
Hide file tree
Showing 5 changed files with 391 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/lib/converter/nodes/variable.ts
Expand Up @@ -62,21 +62,23 @@ export class VariableConverter extends ConverterNodeComponent<ts.VariableDeclara
const kind = scope.kind & ReflectionKind.ClassOrInterface ? ReflectionKind.Property : ReflectionKind.Variable;
const variable = createDeclaration(context, node, kind, name);

switch (kind) {
case ReflectionKind.Variable:
if (node.parent.flags & ts.NodeFlags.Const) {
variable.setFlag(ReflectionFlag.Const, true);
} else if (node.parent.flags & ts.NodeFlags.Let) {
variable.setFlag(ReflectionFlag.Let, true);
}
break;
case ReflectionKind.Property:
if (variable // child inheriting will return null on createDeclaration
&& node.modifiers
&& node.modifiers.some( m => m.kind === ts.SyntaxKind.AbstractKeyword )) {
// The variable can be null if `excludeNotExported` is `true`
if (variable) {
switch (kind) {
case ReflectionKind.Variable:
if (node.parent.flags & ts.NodeFlags.Const) {
variable.setFlag(ReflectionFlag.Const, true);
} else if (node.parent.flags & ts.NodeFlags.Let) {
variable.setFlag(ReflectionFlag.Let, true);
}
break;
case ReflectionKind.Property:
if (node.modifiers
&& node.modifiers.some( m => m.kind === ts.SyntaxKind.AbstractKeyword )) {
variable.setFlag(ReflectionFlag.Abstract, true);
}
break;
}
break;
}
}

context.withScope(variable, () => {
Expand Down
34 changes: 34 additions & 0 deletions src/test/converter.ts
Expand Up @@ -97,3 +97,37 @@ describe('Converter', function() {
});
});
});

describe('Converter with excludeNotExported=true', function() {
const base = Path.join(__dirname, 'converter');
const path = Path.join(base, 'export-with-local');
let app: Application;

it('constructs', function() {
app = new Application({
mode: 'Modules',
logger: 'none',
target: 'ES5',
module: 'CommonJS',
experimentalDecorators: true,
excludeNotExported: true,
jsx: 'react'
});
});

let result: ProjectReflection;

it('converts fixtures', function() {
resetReflectionID();
result = app.convert(app.expandInputFiles([path]));
Assert(result instanceof ProjectReflection, 'No reflection returned');
});

it('matches specs', function() {
const specs = JSON.parse(FS.readFileSync(Path.join(path, 'specs-without-exported.json')).toString());
let data = JSON.stringify(result.toObject(), null, ' ');
data = data.split(normalizePath(base)).join('%BASE%');

compareReflections(JSON.parse(data), specs);
});
});
13 changes: 13 additions & 0 deletions src/test/converter/export-with-local/export-with-local.ts
@@ -0,0 +1,13 @@
export const x = 5;

export function add(x: number, y: number) {
return x + y;
}

// Add a local var/function to make sure typedoc won't choke
// if excludeNotExported is true
let localVar = 'local';

function times(x: number, y: number) {
return x * y;
}
127 changes: 127 additions & 0 deletions src/test/converter/export-with-local/specs-without-exported.json
@@ -0,0 +1,127 @@
{
"id": 0,
"name": "typedoc",
"kind": 0,
"flags": {},
"children": [
{
"id": 1,
"name": "\"export-with-local\"",
"kind": 1,
"kindString": "External module",
"flags": {
"isExported": true
},
"originalName": "%BASE%/export-with-local/export-with-local.ts",
"children": [
{
"id": 2,
"name": "x",
"kind": 32,
"kindString": "Variable",
"flags": {
"isExported": true,
"isConst": true
},
"sources": [
{
"fileName": "export-with-local.ts",
"line": 1,
"character": 14
}
],
"type": {
"type": "unknown",
"name": "5"
},
"defaultValue": "5"
},
{
"id": 3,
"name": "add",
"kind": 64,
"kindString": "Function",
"flags": {
"isExported": true
},
"signatures": [
{
"id": 4,
"name": "add",
"kind": 4096,
"kindString": "Call signature",
"flags": {},
"parameters": [
{
"id": 5,
"name": "x",
"kind": 32768,
"kindString": "Parameter",
"flags": {},
"type": {
"type": "intrinsic",
"name": "number"
}
},
{
"id": 6,
"name": "y",
"kind": 32768,
"kindString": "Parameter",
"flags": {},
"type": {
"type": "intrinsic",
"name": "number"
}
}
],
"type": {
"type": "intrinsic",
"name": "number"
}
}
],
"sources": [
{
"fileName": "export-with-local.ts",
"line": 3,
"character": 19
}
]
}
],
"groups": [
{
"title": "Variables",
"kind": 32,
"children": [
2
]
},
{
"title": "Functions",
"kind": 64,
"children": [
3
]
}
],
"sources": [
{
"fileName": "export-with-local.ts",
"line": 1,
"character": 0
}
]
}
],
"groups": [
{
"title": "External modules",
"kind": 1,
"children": [
1
]
}
]
}

0 comments on commit fa6d9fe

Please sign in to comment.