Skip to content

Commit 5103cc7

Browse files
committed
Merge pull request #350 from documentationjs/to-six
Babel 6 upgrade, round 2
2 parents 77d9cd4 + 70fbded commit 5103cc7

File tree

13 files changed

+180
-160
lines changed

13 files changed

+180
-160
lines changed

lib/flow_doctrine.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var oneToOne = {
1313

1414
var literalTypes = {
1515
'StringLiteralTypeAnnotation': 'StringLiteral',
16-
'NumberLiteralTypeAnnotation': 'NumberLiteral',
16+
'NumericLiteralTypeAnnotation': 'NumberLiteral',
1717
'BooleanLiteralTypeAnnotation': 'BooleanLiteral'
1818
};
1919

lib/infer/finders.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
var n = require('ast-types').namedTypes;
22

3-
function findTarget(node) {
3+
function findTarget(path) {
44

5-
if (!node) {
6-
return node;
5+
if (!path) {
6+
return path;
77
}
88

9-
if (node.value) {
10-
node = node.value;
9+
if (path.node) {
10+
path = path.node;
1111
}
1212

1313
// var x = TARGET;
14-
if (n.VariableDeclaration.check(node)) {
15-
return node.declarations[0].init;
14+
if (n.VariableDeclaration.check(path)) {
15+
return path.declarations[0].init;
1616
}
1717

1818
// foo.x = TARGET
19-
if (n.ExpressionStatement.check(node)) {
20-
return node.expression.right;
19+
if (n.ExpressionStatement.check(path)) {
20+
return path.expression.right;
2121
}
2222

23-
return node;
23+
return path;
2424
}
2525

2626
function findType(node, type) {

lib/infer/kind.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

3-
var types = require('ast-types'),
4-
shouldSkipInference = require('./should_skip_inference');
3+
var shouldSkipInference = require('./should_skip_inference');
4+
var t = require('babel-types');
55

66
var kindShorthands = ['class', 'constant', 'event', 'external', 'file',
77
'function', 'member', 'mixin', 'module', 'namespace', 'typedef'];
@@ -28,33 +28,37 @@ module.exports = function () {
2828
}
2929
}
3030

31-
types.visit(comment.context.ast, {
32-
visitClassDeclaration: function () {
31+
function findKind(path) {
32+
if (!path) {
33+
return comment;
34+
} else if (t.isClassDeclaration(path)) {
3335
comment.kind = 'class';
34-
this.abort();
35-
},
36-
visitFunction: function (path) {
37-
if (path.value && path.value.id && path.value.id.name && !!/^[A-Z]/.exec(path.value.id.name)) {
36+
} else if (t.isFunction(path)) {
37+
if (path.node && path.node.id && path.node.id.name && !!/^[A-Z]/.exec(path.node.id.name)) {
3838
comment.kind = 'class';
39-
this.abort();
4039
} else {
4140
comment.kind = 'function';
42-
this.abort();
4341
}
44-
},
45-
visitTypeAlias: function () {
42+
} else if (t.isTypeAlias(path)) {
4643
comment.kind = 'typedef';
47-
this.abort();
48-
},
49-
visitVariableDeclaration: function (path) {
50-
if (path.value.kind === 'const') {
44+
} else if (t.isVariableDeclaration(path)) {
45+
if (path.node.kind === 'const') {
5146
comment.kind = 'constant';
52-
this.abort();
5347
} else {
54-
this.traverse(path);
48+
// This behavior is in need of fixing https://github.com/documentationjs/documentation/issues/351
49+
findKind(path.node.declarations[0].init);
50+
}
51+
} else if (t.isExportNamedDeclaration(path)) {
52+
if (path.node.declaration.kind === 'const') {
53+
comment.kind = 'constant';
5554
}
55+
} else if (t.isExpressionStatement(path)) {
56+
// module.exports = function() {}
57+
findKind(path.node.expression.right);
5658
}
57-
});
59+
}
60+
61+
findKind(comment.context.ast);
5862

5963
return comment;
6064
});

lib/infer/membership.js

Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
'use strict';
22

3-
var types = require('ast-types'),
3+
var n = require('babel-types'),
44
pathParse = require('parse-filepath'),
55
shouldSkipInference = require('./should_skip_inference'),
66
isJSDocComment = require('../../lib/is_jsdoc_comment'),
77
parse = require('../../lib/parse');
88

9-
var n = types.namedTypes;
10-
119
function findLendsIdentifiers(node) {
1210
if (!node || !node.leadingComments) {
1311
return;
@@ -35,22 +33,9 @@ function findLendsIdentifiers(node) {
3533
function extractIdentifiers(path) {
3634
var identifiers = [];
3735

38-
types.visit(path, {
39-
visitNode: function () {
40-
return false;
41-
},
42-
43-
visitAssignmentExpression: function (path) {
44-
this.traverse(path);
45-
},
46-
47-
visitMemberExpression: function (path) {
48-
this.traverse(path);
49-
},
50-
51-
visitIdentifier: function (path) {
36+
path.traverse({
37+
Identifier: function (path) {
5238
identifiers.push(path.node.name);
53-
return false;
5439
}
5540
});
5641

@@ -148,45 +133,49 @@ module.exports = function () {
148133
return comment;
149134
}
150135

136+
if (!comment.context.ast) {
137+
return comment;
138+
}
139+
151140
var path = comment.context.ast;
152141
var identifiers;
153142

154143
/*
155144
* Deal with an oddity of espree: the jsdoc comment is attached to a different
156145
* node in the two expressions `a.b = c` vs `a.b = function () {}`.
157146
*/
158-
if (n.ExpressionStatement.check(path.node) &&
159-
n.AssignmentExpression.check(path.node.expression) &&
160-
n.MemberExpression.check(path.node.expression.left)) {
147+
if (n.isExpressionStatement(path.node) &&
148+
n.isAssignmentExpression(path.node.expression) &&
149+
n.isMemberExpression(path.node.expression.left)) {
161150
path = path.get('expression').get('left');
162151
}
163152

164153
/*
165154
* Same as above but for `b: c` vs `b: function () {}`.
166155
*/
167-
if (n.Property.check(path.node) &&
168-
n.Identifier.check(path.node.key)) {
156+
if (n.isProperty(path.node) &&
157+
n.isIdentifier(path.node.key)) {
169158
path = path.get('key');
170159
}
171160

172161
// Foo.bar = ...;
173162
// Foo.prototype.bar = ...;
174163
// Foo.bar.baz = ...;
175-
if (n.MemberExpression.check(path.node)) {
164+
if (n.isMemberExpression(path.node)) {
176165
identifiers = extractIdentifiers(path);
177166
if (identifiers.length >= 2) {
178167
inferMembershipFromIdentifiers(comment, identifiers.slice(0, -1));
179168
}
180169
}
181170

182171
// /** @lends Foo */{ bar: ... }
183-
if (n.Identifier.check(path.node) &&
184-
n.Property.check(path.parent.node) &&
185-
n.ObjectExpression.check(path.parent.parent.node)) {
172+
if (n.isIdentifier(path.node) &&
173+
n.isObjectProperty(path.parentPath) &&
174+
n.isObjectExpression(path.parentPath.parentPath)) {
186175
// The @lends comment is sometimes attached to the first property rather than
187176
// the object expression itself.
188-
identifiers = findLendsIdentifiers(path.parent.parent.node) ||
189-
findLendsIdentifiers(path.parent.parent.node.properties[0]);
177+
identifiers = findLendsIdentifiers(path.parentPath.parentPath.node) ||
178+
findLendsIdentifiers(path.parentPath.parentPath.node.properties[0]);
190179
if (identifiers) {
191180
inferMembershipFromIdentifiers(comment, identifiers);
192181
}
@@ -195,30 +184,33 @@ module.exports = function () {
195184
// Foo = { bar: ... };
196185
// Foo.prototype = { bar: ... };
197186
// Foo.bar = { baz: ... };
198-
if (n.Identifier.check(path.node) &&
199-
n.Property.check(path.parent.node) &&
200-
n.ObjectExpression.check(path.parent.parent.node) &&
201-
n.AssignmentExpression.check(path.parent.parent.parent.node)) {
202-
identifiers = extractIdentifiers(path.parent.parent.parent);
187+
if (n.isIdentifier(path.node) &&
188+
n.isObjectProperty(path.parentPath) &&
189+
n.isObjectExpression(path.parentPath.parentPath) &&
190+
n.isAssignmentExpression(path.parentPath.parentPath.parentPath)) {
191+
identifiers = extractIdentifiers(path.parentPath.parentPath.parentPath);
192+
// The last identifier is the thing itself, so throw it away
193+
// TODO: is this safe?
194+
identifiers.pop();
203195
if (identifiers.length >= 1) {
204196
inferMembershipFromIdentifiers(comment, identifiers);
205197
}
206198
}
207199

208200
// var Foo = { bar: ... }
209-
if (n.Identifier.check(path.node) &&
210-
n.Property.check(path.parent.node) &&
211-
n.ObjectExpression.check(path.parent.parent.node) &&
212-
n.VariableDeclarator.check(path.parent.parent.parent.node)) {
213-
identifiers = [path.parent.parent.parent.node.id.name];
201+
if (n.isIdentifier(path) &&
202+
n.isObjectProperty(path.parentPath) &&
203+
n.isObjectExpression(path.parentPath.parentPath) &&
204+
n.isVariableDeclarator(path.parentPath.parentPath.parentPath)) {
205+
identifiers = [path.parentPath.parentPath.parentPath.node.id.name];
214206
inferMembershipFromIdentifiers(comment, identifiers);
215207
}
216208

217209
// class Foo { bar() { } }
218-
if (n.MethodDefinition.check(path.node) &&
219-
n.ClassBody.check(path.parent.node) &&
220-
n.ClassDeclaration.check(path.parent.parent.node)) {
221-
identifiers = [path.parent.parent.node.id.name];
210+
if (n.isClassMethod(path) &&
211+
n.isClassBody(path.parentPath) &&
212+
n.isClassDeclaration(path.parentPath.parentPath)) {
213+
identifiers = [path.parentPath.parentPath.node.id.name];
222214
var scope = 'instance';
223215
if (path.node.static == true) {
224216
scope = 'static';
@@ -227,24 +219,28 @@ module.exports = function () {
227219
}
228220

229221
// var Foo = class { bar() { } }
230-
if (n.MethodDefinition.check(path.node) &&
231-
n.ClassBody.check(path.parent.node) &&
232-
n.ClassExpression.check(path.parent.parent.node) &&
233-
n.AssignmentExpression.check(path.parent.parent.parent.node)) {
234-
identifiers = extractIdentifiers(path.parent.parent.parent.node);
222+
if (n.isClassMethod(path) &&
223+
n.isClassBody(path.parentPath) &&
224+
n.isClassExpression(path.parentPath.parentPath)) {
225+
identifiers = extractIdentifiers(path.parentPath.parentPath.parentPath.get('left'));
235226
scope = 'instance';
236227
if (path.node.static == true) {
237228
scope = 'static';
238229
}
239230
inferMembershipFromIdentifiers(comment, identifiers, scope);
240231
}
241232

242-
// var function Foo(){ function bar(){} return { bar: bar }; }
243-
if (n.FunctionDeclaration.check(path.node) &&
244-
n.BlockStatement.check(path.parent.node) &&
245-
n.FunctionDeclaration.check(path.parent.parent.node)) {
246-
inferMembershipFromIdentifiers(comment, [path.parent.parent.value.id.name]);
233+
// var function Foo() {
234+
// function bar() {}
235+
// return { bar: bar };
236+
// }
237+
/*
238+
if (n.isFunctionDeclaration(path) &&
239+
n.isBlockStatement(path.parentPath) &&
240+
n.isFunction(path.parentPath.parentPath)) {
241+
inferMembershipFromIdentifiers(comment, [path.parentPath.parentPath.node.id.name]);
247242
}
243+
*/
248244

249245
return comment;
250246
});

lib/infer/name.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

3-
var types = require('ast-types'),
4-
shouldSkipInference = require('./should_skip_inference'),
3+
var shouldSkipInference = require('./should_skip_inference'),
54
pathParse = require('parse-filepath');
65

76
/**
@@ -45,29 +44,32 @@ module.exports = function () {
4544
return comment;
4645
}
4746

47+
function inferName(path, node) {
48+
if (node && node.name) {
49+
comment.name = node.name;
50+
return true;
51+
}
52+
}
53+
4854
// The strategy here is to do a depth-first traversal of the AST,
4955
// looking for nodes with a "name" property, with exceptions as needed.
5056
// For example, name inference for a MemberExpression `foo.bar = baz` will
5157
// infer the named based on the `property` of the MemberExpression (`bar`)
5258
// rather than the `object` (`foo`).
53-
types.visit(comment.context.ast, {
54-
inferName: function (path, value) {
55-
if (value && value.name) {
56-
comment.name = value.name;
57-
this.abort();
58-
} else {
59-
this.traverse(path);
59+
if (comment.context.ast) {
60+
comment.context.ast.traverse({
61+
Identifier: function (path) {
62+
if (inferName(path, path.node)) {
63+
path.stop();
64+
}
65+
},
66+
MemberExpression: function (path) {
67+
if (inferName(path, path.node.property)) {
68+
path.stop();
69+
}
6070
}
61-
},
62-
63-
visitNode: function (path) {
64-
this.inferName(path, path.value);
65-
},
66-
67-
visitMemberExpression: function (path) {
68-
this.inferName(path, path.value.property);
69-
}
70-
});
71+
});
72+
}
7173

7274
return comment;
7375
});

lib/infer/params.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ function paramToDoc(param, comment, i, prefix) {
5454
}
5555

5656
function destructuringPropertyToDoc(property) {
57-
if (property.type === 'Property') {
57+
if (property.type === 'ObjectProperty') {
5858
return paramToDoc(property.value, comment, i, prefix + '$' + i + '.');
5959
} else if (property.type === 'Identifier') {
6060
// if the destructuring type is an array, the elements
6161
// in it are identifiers
6262
return paramToDoc(property, comment, i, prefix + '$' + i + '.');
63-
} else if (property.type === 'SpreadProperty') {
63+
} else if (property.type === 'RestProperty') {
6464
return paramToDoc(property, comment, i, prefix + '$' + i + '.');
6565
}
6666
}
@@ -115,7 +115,7 @@ function paramToDoc(param, comment, i, prefix) {
115115
return addPrefix(destructuringArrayParamToDoc(param));
116116
}
117117

118-
if (param.type === 'SpreadProperty' || param.type === 'RestElement') {
118+
if (param.type === 'RestProperty' || param.type === 'RestElement') {
119119
return addPrefix(restParamToDoc(param));
120120
}
121121

@@ -142,7 +142,7 @@ function paramToDoc(param, comment, i, prefix) {
142142
*/
143143
module.exports = function () {
144144
return shouldSkipInference(function inferParams(comment) {
145-
var node = finders.findType(comment.context.ast.value, 'Function');
145+
var node = finders.findType(comment.context.ast, 'Function');
146146

147147
if (!node) {
148148
return comment;

0 commit comments

Comments
 (0)