Skip to content

Commit 50452a1

Browse files
committed
feat(es2017): support async
1 parent 1e83c50 commit 50452a1

File tree

11 files changed

+104
-4
lines changed

11 files changed

+104
-4
lines changed

src/Doc/FunctionDoc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ export default class FunctionDoc extends AbstractDoc {
4040
this._value.generator = this._node.generator;
4141
}
4242

43+
/**
44+
* use async property of self node.
45+
*/
46+
_$async() {
47+
super._$async();
48+
this._value.async = this._node.async;
49+
}
50+
4351
/** if @param is not exists, guess type of param by using self node. */
4452
_$param() {
4553
super._$param();

src/Doc/MethodDoc.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,13 @@ export default class MethodDoc extends AbstractDoc {
102102

103103
this._value.generator = this._node.generator;
104104
}
105+
106+
/**
107+
* use async property of self node.
108+
*/
109+
_$async() {
110+
super._$async();
111+
112+
this._value.async = this._node.async;
113+
}
105114
}

src/Factory/DocFactory.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,8 @@ export default class DocFactory {
422422
return this._decideExpressionStatementType(node);
423423
case 'FunctionDeclaration':
424424
return this._decideFunctionDeclarationType(node);
425+
case 'FunctionExpression':
426+
return this._decideFunctionExpressionType(node);
425427
case 'VariableDeclaration':
426428
return this._decideVariableType(node);
427429
case 'AssignmentExpression':
@@ -471,6 +473,23 @@ export default class DocFactory {
471473
return {type: 'Function', node: node};
472474
}
473475

476+
/**
477+
* decide Doc type from function expression node.
478+
* babylon 6.11.2 judges`export default async function foo(){}` to be `FunctionExpression`.
479+
* I expect `FunctionDeclaration`. this behavior may be bug of babylon.
480+
* for now, workaround for it with this method.
481+
* @param {ASTNode} node - target node that is function expression node.
482+
* @returns {{type: string, node: ASTNode}} decided type.
483+
* @private
484+
* @todo inspect with newer babylon.
485+
*/
486+
_decideFunctionExpressionType(node) {
487+
if (!node.async) return {type: null, node: null};
488+
if (!this._isTopDepthInBody(node, this._ast.program.body)) return {type: null, node: null};
489+
490+
return {type: 'Function', node: node};
491+
}
492+
474493
/**
475494
* decide Doc type from expression statement node.
476495
* @param {ASTNode} node - target node that is expression statement node.

src/Publisher/Builder/DocBuilder.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ export default class DocBuilder {
373373
ice.text('title', title);
374374
ice.loop('target', docs, (i, doc, ice)=>{
375375
ice.text('generator', doc.generator ? '*' : '');
376+
ice.text('async', doc.async ? 'async' : '');
376377
ice.load('name', this._buildDocLinkHTML(doc.longname, null, innerLink, doc.kind));
377378
ice.load('signature', this._buildSignatureHTML(doc));
378379
ice.load('description', shorten(doc, true));
@@ -444,6 +445,7 @@ export default class DocBuilder {
444445
const scope = doc.static ? 'static' : 'instance';
445446
ice.attr('anchor', 'id', `${scope}-${doc.kind}-${doc.name}`);
446447
ice.text('generator', doc.generator ? '*' : '');
448+
ice.text('async', doc.async ? 'async' : '');
447449
ice.text('name', doc.name);
448450
ice.load('signature', this._buildSignatureHTML(doc));
449451
ice.load('description', doc.description);

src/Publisher/Builder/template/details.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ <h3 data-ice="anchor">
66
<span class="kind" data-ice="kind"></span>
77
<span class="abstract" data-ice="abstract"></span>
88
<span data-ice="generator"></span>
9+
<span data-ice="async"></span>
910
<span data-ice="name"></span><span data-ice="signature"></span>
1011
<span class="right-info">
1112
<span class="version" data-ice="version">version </span>

src/Publisher/Builder/template/summary.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
<td>
1313
<div>
1414
<p>
15-
<span data-ice="generator"></span> <span data-ice="name"></span><span data-ice="signature"></span>
15+
<span data-ice="generator"></span>
16+
<span data-ice="async"></span>
17+
<span data-ice="name"></span><span data-ice="signature"></span>
1618
</p>
1719
</div>
1820
<div>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* this is testAsyncFunction.
3+
*/
4+
export default async function testAsyncFunction(){}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* this is TestAsyncMethod.
3+
*/
4+
export default class TestAsyncMethod {
5+
/**
6+
* this is async method1.
7+
*/
8+
async method1(){}
9+
}

test/src/HTMLTest/CoverageTest/CoverageTest.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ describe('test coverage', ()=> {
99
it('has coverage summary', ()=> {
1010
assert(badge.includes('79%'));
1111
assert.includes(doc, '[data-ice="coverageBadge"]', './badge.svg', 'src');
12-
assert.includes(doc, '[data-ice="totalCoverageCount"]', '255/322');
13-
assert.equal(doc.find('[data-ice="file"] [data-ice="coverage"]').length, 112);
12+
assert.includes(doc, '[data-ice="totalCoverageCount"]', '258/325');
13+
assert.equal(doc.find('[data-ice="file"] [data-ice="coverage"]').length, 114);
1414
});
1515

1616
/* eslint-disable max-statements */
@@ -30,6 +30,8 @@ describe('test coverage', ()=> {
3030
test('file/src/Access/Method.js.html', '100 %5/5');
3131
test('file/src/Access/Property.js.html#errorLines=5', '83 %5/6');
3232
test('file/src/Access/Variable.js.html', '100 %4/4');
33+
test('file/src/Async/Function.js.html', '100 %1/1');
34+
test('file/src/Async/Method.js.html', '100 %2/2');
3335
test('file/src/Class/Definition.js.html', '100 %8/8');
3436
test('file/src/Computed/Method.js.html', '100 %11/11');
3537
test('file/src/Computed/Property.js.html', '100 %12/12');
@@ -136,6 +138,6 @@ describe('test coverage', ()=> {
136138
test('file/src/Version/Function.js.html', '100 %1/1');
137139
test('file/src/Version/Variable.js.html', '100 %1/1');
138140

139-
assert.equal(count, 112);
141+
assert.equal(count, 114);
140142
});
141143
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {readDoc, assert, findParent} from './../../../util.js';
2+
3+
/** @test {FunctionDoc#_$async} */
4+
describe('testAsyncFunction', ()=> {
5+
const doc = readDoc('function/index.html');
6+
7+
describe('in summary', ()=> {
8+
it('has async mark', ()=> {
9+
findParent(doc, '[data-ice="summary"] [href$="#static-function-testAsyncFunction"]', '[data-ice="target"]', (doc)=> {
10+
assert.includes(doc, null, 'public async testAsyncFunction()');
11+
});
12+
});
13+
});
14+
15+
describe('in details', ()=>{
16+
it('has async mark.', ()=>{
17+
findParent(doc, '[id="static-function-testAsyncFunction"]', '[data-ice="detail"]', (doc)=>{
18+
assert.includes(doc, 'h3', 'public async testAsyncFunction()');
19+
});
20+
});
21+
});
22+
});

0 commit comments

Comments
 (0)