Skip to content

Commit

Permalink
fix(export): crash indirect instance export.
Browse files Browse the repository at this point in the history
  • Loading branch information
h13i32maru committed Feb 6, 2016
1 parent 97a5423 commit c9937c2
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 35 deletions.
20 changes: 13 additions & 7 deletions src/Factory/DocFactory.js
Expand Up @@ -95,7 +95,13 @@ export default class DocFactory {

switch(exportNode.declaration.type) {
case 'NewExpression':
targetClassName = exportNode.declaration.callee.name;
if (exportNode.declaration.callee.type === 'Identifier') {
targetClassName = exportNode.declaration.callee.name;
} else if (exportNode.declaration.callee.type === 'MemberExpression') {
targetClassName = exportNode.declaration.callee.property.name;
} else {
targetClassName = '';
}
targetVariableName = targetClassName.replace(/^./, c => c.toLowerCase());
pseudoClassExport = true;
break;
Expand All @@ -108,7 +114,6 @@ export default class DocFactory {
ASTUtil.sanitize(varNode);
} else {
targetClassName = exportNode.declaration.name;
targetVariableName = targetClassName.replace(/^./, c => c.toLowerCase());
pseudoClassExport = false;
}
break;
Expand All @@ -123,12 +128,13 @@ export default class DocFactory {
pseudoExportNode1.declaration = this._copy(classNode);
pseudoExportNode1.leadingComments = null;
pseudoExportNode1.declaration.__esdoc__pseudo_export = pseudoClassExport;

let pseudoExportNode2 = this._copy(exportNode);
pseudoExportNode2.declaration = ASTUtil.createVariableDeclarationAndNewExpressionNode(targetVariableName, targetClassName, exportNode.loc);

pseudoExportNodes.push(pseudoExportNode1);
pseudoExportNodes.push(pseudoExportNode2);

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

ASTUtil.sanitize(classNode);
ASTUtil.sanitize(exportNode);
Expand Down
12 changes: 12 additions & 0 deletions test/fixture/src/Export.js
Expand Up @@ -96,3 +96,15 @@ class MyExport10 {
* this is MyExport10 instance.
*/
export let myExport10 = new MyExport10();

/**
* this is MyExport11
*/
class MyExport11 {}
/** @ignore */
let obj = {};
obj.MyExport11 = MyExport11;
/**
* this is indirect instance export
*/
export default new obj.MyExport11();
10 changes: 5 additions & 5 deletions test/src/BuilderTest/CoverageDocTest.js
Expand Up @@ -8,9 +8,9 @@ describe('Coverage:', ()=> {
it('has coverage.json', ()=>{
let json = fs.readFileSync('./test/fixture/esdoc/coverage.json', {encoding: 'utf8'}).toString();
let coverage = JSON.parse(json);
assert.equal(coverage.coverage, '89.72%');
assert.equal(coverage.expectCount, 146);
assert.equal(coverage.actualCount, 131);
assert.equal(coverage.coverage, '89.79%');
assert.equal(coverage.expectCount, 147);
assert.equal(coverage.actualCount, 132);
assert.deepEqual(coverage.files, {
"src/ForTestDoc/AbstractDoc.js": {
"expectCount": 3,
Expand Down Expand Up @@ -58,8 +58,8 @@ describe('Coverage:', ()=> {
"undocumentLines": []
},
"src/Export.js": {
"expectCount": 23,
"actualCount": 23,
"expectCount": 24,
"actualCount": 24,
"undocumentLines": []
},
"src/MyExpressionExtend.js": {
Expand Down
2 changes: 1 addition & 1 deletion test/src/BuilderTest/ExportDocTest.js
Expand Up @@ -10,7 +10,7 @@ describe('Export:', ()=>{

it('has instance notice.', ()=>{
assert.includes(doc, '[data-ice="importPath"]', "import MyExport1 from 'esdoc-test-fixture/src/Export.js'");
assert.includes(doc, '.self-detail', 'You can directly use instance of this class. myExport1');
//assert.includes(doc, '.self-detail', 'You can directly use instance of this class. myExport1');
});
});

Expand Down
4 changes: 2 additions & 2 deletions test/src/BuilderTest/IdentifiersDocTest.js
Expand Up @@ -8,7 +8,7 @@ describe('Identifiers:', ()=> {
it('has class summary.', ()=>{
find(doc, '[data-ice="classSummary"]', (doc)=>{
assert.includes(doc, '[data-ice="target"]:nth-of-type(4)', 'public MyClass1 this class was deprecated. use MyClass1Ex instead of this class. this class is experimental. this class is dangerous. this is MyClass1 desc.');
assert.includes(doc, '[data-ice="target"]:nth-of-type(24)', 'public SuperMyClass1 this is SuperMyClass1.');
assert.includes(doc, '[data-ice="target"]:nth-of-type(25)', 'public SuperMyClass1 this is SuperMyClass1.');
});
});

Expand All @@ -30,7 +30,7 @@ describe('Identifiers:', ()=> {
/** @test {IdentifiersDocBuilder#_buildIdentifierDoc} */
it('has variable summary.', ()=>{
find(doc, '[data-ice="variableSummary"]', (doc)=>{
assert.includes(doc, '[data-ice="target"]:nth-of-type(1)', 'public myExport1: MyExport1');
assert.includes(doc, '[data-ice="target"]:nth-of-type(1)', 'public myExport10: MyExport10');
assert.includes(doc, '[data-ice="target"]:nth-of-type(7)', 'public myVariable1: Object this is myVariable1 desc.');
assert.includes(doc, '[data-ice="target"]:nth-of-type(9)', 'public myVariable3: number this is myVariable3 desc.');
});
Expand Down
26 changes: 13 additions & 13 deletions test/src/BuilderTest/NavDocTest.js
Expand Up @@ -8,29 +8,29 @@ describe('Nav:', ()=> {
it('has each nav.', ()=>{
find(doc, '[data-ice="nav"]', (doc)=>{
// class
assert.includes(doc, '[data-ice="doc"]:nth-of-type(11)', 'MyClass1');
assert.includes(doc, '[data-ice="doc"]:nth-of-type(11) a', 'class/src/MyClass.js~MyClass1.html', 'href');
assert.includes(doc, '[data-ice="doc"]:nth-of-type(12)', 'MyClass1');
assert.includes(doc, '[data-ice="doc"]:nth-of-type(12) a', 'class/src/MyClass.js~MyClass1.html', 'href');

// interface
assert.includes(doc, '[data-ice="doc"]:nth-of-type(28)', 'MyInterface1');
assert.includes(doc, '[data-ice="doc"]:nth-of-type(28) a', 'class/src/MyInterface.js~MyInterface1.html', 'href');
assert.includes(doc, '[data-ice="doc"]:nth-of-type(29)', 'MyInterface1');
assert.includes(doc, '[data-ice="doc"]:nth-of-type(29) a', 'class/src/MyInterface.js~MyInterface1.html', 'href');

// function
assert.includes(doc, '[data-ice="doc"]:nth-of-type(31)', 'myFunction1');
assert.includes(doc, '[data-ice="doc"]:nth-of-type(31) a', 'function/index.html#static-function-myFunction1', 'href');
assert.includes(doc, '[data-ice="doc"]:nth-of-type(32)', 'myFunction1');
assert.includes(doc, '[data-ice="doc"]:nth-of-type(32) a', 'function/index.html#static-function-myFunction1', 'href');

// variable
assert.includes(doc, '[data-ice="doc"]:nth-of-type(43)', 'myExport1');
assert.includes(doc, '[data-ice="doc"]:nth-of-type(51)', 'myVariable1');
assert.includes(doc, '[data-ice="doc"]:nth-of-type(51) a', 'variable/index.html#static-variable-myVariable1', 'href');
assert.includes(doc, '[data-ice="doc"]:nth-of-type(44)', 'myExport10');
assert.includes(doc, '[data-ice="doc"]:nth-of-type(52)', 'myVariable1');
assert.includes(doc, '[data-ice="doc"]:nth-of-type(52) a', 'variable/index.html#static-variable-myVariable1', 'href');

// typedef
assert.includes(doc, '[data-ice="doc"]:nth-of-type(59)', 'MyTypedef1');
assert.includes(doc, '[data-ice="doc"]:nth-of-type(59) a', 'typedef/index.html#static-typedef-MyTypedef1', 'href');
assert.includes(doc, '[data-ice="doc"]:nth-of-type(60)', 'MyTypedef1');
assert.includes(doc, '[data-ice="doc"]:nth-of-type(60) a', 'typedef/index.html#static-typedef-MyTypedef1', 'href');

// external
assert.includes(doc, '[data-ice="doc"]:nth-of-type(61)', 'MyError2');
assert.includes(doc, '[data-ice="doc"]:nth-of-type(61) a', 'example.com', 'href');
assert.includes(doc, '[data-ice="doc"]:nth-of-type(62)', 'MyError2');
assert.includes(doc, '[data-ice="doc"]:nth-of-type(62) a', 'example.com', 'href');
});
});
});
8 changes: 1 addition & 7 deletions test/src/BuilderTest/VariableDocTest.js
Expand Up @@ -7,8 +7,7 @@ describe('MyVariable:', ()=> {
/** @test {SingleDocBuilder#_buildSingleDoc} */
it('has summary.', ()=>{
find(doc, '[data-ice="summary"]', (doc)=>{
assert.includes(doc, '[data-ice="target"]:nth-of-type(1)', 'public myExport1: MyExport1');
assert.includes(doc, '[data-ice="target"]:nth-of-type(2)', 'public myExport10: MyExport10');
assert.includes(doc, '[data-ice="target"]:nth-of-type(1)', 'public myExport10: MyExport10');
assert.includes(doc, '[data-ice="target"]:nth-of-type(3)', 'public myExport2: MyExport2');
assert.includes(doc, '[data-ice="target"]:nth-of-type(4)', 'public myExport3: MyExport3');
assert.includes(doc, '[data-ice="target"]:nth-of-type(5)', 'public myExport4: MyExport4');
Expand All @@ -28,11 +27,6 @@ describe('MyVariable:', ()=> {
/** @test {SingleDocBuilder#_buildSingleDoc} */
it('has detail.', ()=> {
find(doc, '[data-ice="detail"]:nth-of-type(1)', (doc)=> {
assert.includes(doc, '#static-variable-myExport1', 'public myExport1: MyExport1');
assert.includes(doc, '[data-ice="importPath"]', "import myExport1 from 'esdoc-test-fixture/src/Export.js'");
});

find(doc, '[data-ice="detail"]:nth-of-type(2)', (doc)=> {
assert.includes(doc, '#static-variable-myExport10', 'public myExport10: MyExport10');
assert.includes(doc, '[data-ice="importPath"]', "import {myExport10} from 'esdoc-test-fixture/src/Export.js'");
});
Expand Down

0 comments on commit c9937c2

Please sign in to comment.