Skip to content

Commit

Permalink
fix(factory): work to export instance
Browse files Browse the repository at this point in the history
```
export class Foo {}
export default new Foo()
```
  • Loading branch information
h13i32maru committed Apr 23, 2017
1 parent d656ff5 commit 43b9207
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
24 changes: 13 additions & 11 deletions src/Factory/DocFactory.js
Expand Up @@ -124,21 +124,23 @@ export default class DocFactory {
break;
}

const classNode = ASTUtil.findClassDeclarationNode(targetClassName, this._ast);
const {classNode, exported} = ASTUtil.findClassDeclarationNode(targetClassName, this._ast);
if (classNode) {
const pseudoExportNode1 = this._copy(exportNode);
pseudoExportNode1.declaration = this._copy(classNode);
pseudoExportNode1.leadingComments = null;
pseudoExportNode1.declaration.__PseudoExport__ = pseudoClassExport;
pseudoExportNodes.push(pseudoExportNode1);
if (!exported) {
const pseudoExportNode1 = this._copy(exportNode);
pseudoExportNode1.declaration = this._copy(classNode);
pseudoExportNode1.leadingComments = null;
pseudoExportNode1.declaration.__PseudoExport__ = pseudoClassExport;
pseudoExportNodes.push(pseudoExportNode1);
ASTUtil.sanitize(classNode);
}

if (targetVariableName) {
const pseudoExportNode2 = this._copy(exportNode);
pseudoExportNode2.declaration = ASTUtil.createVariableDeclarationAndNewExpressionNode(targetVariableName, targetClassName, exportNode.loc);
pseudoExportNodes.push(pseudoExportNode2);
}

ASTUtil.sanitize(classNode);
ASTUtil.sanitize(exportNode);
}

Expand Down Expand Up @@ -196,8 +198,8 @@ export default class DocFactory {
for (const declaration of exportNode.declaration.declarations) {
if (!declaration.init || declaration.init.type !== 'NewExpression') continue;

const classNode = ASTUtil.findClassDeclarationNode(declaration.init.callee.name, this._ast);
if (classNode) {
const {classNode, exported} = ASTUtil.findClassDeclarationNode(declaration.init.callee.name, this._ast);
if (classNode && !exported) {
const pseudoExportNode = this._copy(exportNode);
pseudoExportNode.declaration = this._copy(classNode);
pseudoExportNode.leadingComments = null;
Expand Down Expand Up @@ -231,8 +233,8 @@ export default class DocFactory {
pseudoClassExport = false;
}

const classNode = ASTUtil.findClassDeclarationNode(targetClassName, this._ast);
if (classNode) {
const {classNode, exported} = ASTUtil.findClassDeclarationNode(targetClassName, this._ast);
if (classNode && !exported) {
const pseudoExportNode = this._copy(exportNode);
pseudoExportNode.declaration = this._copy(classNode);
pseudoExportNode.leadingComments = null;
Expand Down
12 changes: 8 additions & 4 deletions src/Util/ASTUtil.js
Expand Up @@ -85,16 +85,20 @@ export default class ASTUtil {
* find ClassDeclaration node.
* @param {string} name - class name.
* @param {AST} ast - find in this ast.
* @returns {ASTNode|null} found ast node.
* @returns {{classNode: ASTNode|null, exported: boolean|null}} found ast node.
*/
static findClassDeclarationNode(name, ast) {
if (!name) return null;
if (!name) return {classNode: null, exported: null};

for (const node of ast.program.body) {
if (node.type === 'ClassDeclaration' && node.id.name === name) return node;
if (node.type === 'ClassDeclaration' && node.id.name === name) return {classNode: node, exported: false};

if (node.type === 'ExportDefaultDeclaration' || node.type === 'ExportNamedDeclaration') {
if (node.declaration && node.declaration.type === 'ClassDeclaration' && node.declaration.id.name === name) return {classNode: node, exported: true};
}
}

return null;
return {classNode: null, exported: null};
}

/**
Expand Down
19 changes: 19 additions & 0 deletions test/fixture/package/src/Export/NewExpressionAndExported.js
@@ -0,0 +1,19 @@
/**
* this is TestExportNewExpressionAndExported.
*/
export class TestExportNewExpressionAndExported {}

/**
* this is instance of TestExportNewExpressionAndExported.
*/
export default new TestExportNewExpressionAndExported();

/**
* this is TestExportNewExpressionAndExported2.
*/
export class TestExportNewExpressionAndExported2 {}

/**
* this is instance of TestExportNewExpressionAndExported2.
*/
export const testExportNewExpressionAndExported2 = new TestExportNewExpressionAndExported2();

0 comments on commit 43b9207

Please sign in to comment.