File tree Expand file tree Collapse file tree 4 files changed +69
-5
lines changed Expand file tree Collapse file tree 4 files changed +69
-5
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,10 @@ function findTarget(path) {
10
10
path = path . node ;
11
11
}
12
12
13
+ if ( t . isExportNamedDeclaration ( path ) || t . isExportDefaultDeclaration ( path ) ) {
14
+ path = path . declaration ;
15
+ }
16
+
13
17
// var x = TARGET;
14
18
if ( t . isVariableDeclaration ( path ) ) {
15
19
return path . declarations [ 0 ] . init ;
Original file line number Diff line number Diff line change @@ -33,12 +33,17 @@ module.exports = function () {
33
33
}
34
34
}
35
35
36
- // The strategy here is to do a depth-first traversal of the AST,
37
- // looking for nodes with a "name" property, with exceptions as needed.
38
- // For example, name inference for a MemberExpression `foo.bar = baz` will
39
- // infer the named based on the `property` of the MemberExpression (`bar`)
40
- // rather than the `object` (`foo`).
41
36
if ( comment . context . ast ) {
37
+ if ( comment . context . ast . type === 'ExportDefaultDeclaration' ) {
38
+ comment . name = pathParse ( comment . context . file ) . name ;
39
+ return comment ;
40
+ }
41
+
42
+ // The strategy here is to do a depth-first traversal of the AST,
43
+ // looking for nodes with a "name" property, with exceptions as needed.
44
+ // For example, name inference for a MemberExpression `foo.bar = baz` will
45
+ // infer the named based on the `property` of the MemberExpression (`bar`)
46
+ // rather than the `object` (`foo`).
42
47
comment . context . ast . traverse ( {
43
48
Identifier : function ( path ) {
44
49
if ( inferName ( path , path . node ) ) {
Original file line number Diff line number Diff line change @@ -123,5 +123,25 @@ test('inferName', function (t) {
123
123
/** @module */
124
124
} , '/path/inferred-from-file.js' ) . name , 'inferred-from-file' ) ;
125
125
126
+ t . equal ( evaluate ( `
127
+ /** Test */
128
+ export function exported() {}
129
+ ` ) . name , 'exported' ) ;
130
+
131
+ t . equal ( evaluate ( `
132
+ /** Test */
133
+ export default function exported() {}
134
+ ` , '/path/inferred-from-file.js' ) . name , 'inferred-from-file' ) ;
135
+
136
+ t . equal ( evaluate ( `
137
+ /** Test */
138
+ export var life = 42;
139
+ ` ) . name , 'life' ) ;
140
+
141
+ t . equal ( evaluate ( `
142
+ /** Test */
143
+ export class Wizard {}
144
+ ` ) . name , 'Wizard' ) ;
145
+
126
146
t . end ( ) ;
127
147
} ) ;
Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+
3
+ var test = require ( 'tap' ) . test ,
4
+ parse = require ( '../../../lib/parsers/javascript' ) ,
5
+ inferParams = require ( '../../../lib/infer/params' ) ( ) ;
6
+
7
+ function toComment ( fn , file ) {
8
+ return parse ( {
9
+ file : file ,
10
+ source : fn instanceof Function ? '(' + fn . toString ( ) + ')' : fn
11
+ } ) [ 0 ] ;
12
+ }
13
+
14
+ function evaluate ( fn , file ) {
15
+ return inferParams ( toComment ( fn , file ) ) ;
16
+ }
17
+
18
+ test ( 'inferParams' , function ( t ) {
19
+ t . deepEqual ( evaluate ( function ( ) {
20
+ /** Test */
21
+ function f ( x ) { }
22
+ } ) . params , [ { lineNumber : 3 , name : 'x' , title : 'param' } ] ) ;
23
+
24
+ t . deepEqual ( evaluate ( `
25
+ /** Test */
26
+ export function f(x) {}
27
+ ` ) . params , [ { lineNumber : 3 , name : 'x' , title : 'param' } ] ) ;
28
+
29
+ t . deepEqual ( evaluate ( `
30
+ /** Test */
31
+ export default function f(x) {}
32
+ ` ) . params , [ { lineNumber : 3 , name : 'x' , title : 'param' } ] ) ;
33
+
34
+ t . end ( ) ;
35
+ } ) ;
You can’t perform that action at this time.
0 commit comments