Skip to content

Commit c7aa09c

Browse files
committed
more accurately detect the new jsdoc ES6 class data structure
1 parent 319185f commit c7aa09c

2 files changed

Lines changed: 57 additions & 8 deletions

File tree

es5/transform.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var o = require('object-tools');
44
var a = require('array-tools');
5+
var testValue = require('test-value');
56

67
var data;
78
exports.createConstructor = createConstructor;
@@ -96,22 +97,23 @@ function insertConstructors(data) {
9697
var toDelete = [];
9798

9899
data.forEach(function (identifier, index) {
99-
if (identifier.kind === 'class' && identifier.scope !== 'static') {
100-
var es6constructor = a.findWhere(data, { kind: 'class', scope: 'static', memberof: identifier.longname });
100+
if (isES5Class(identifier)) {
101+
replacements.push({ index: index, items: createConstructor(identifier) });
102+
} else if (isES6Class(identifier)) {
103+
var es6constructor = getEs6Constructor(data, identifier);
101104
if (es6constructor) {
102105
if (!(es6constructor.description || es6constructor.params && es6constructor.params.length)) {
103106
toDelete.push(es6constructor);
104107
}
105108
es6constructor.kind = 'constructor';
109+
es6constructor.memberof = identifier.longname;
106110
var constructorChildren = a.where(data, { memberof: es6constructor.longname });
107111
constructorChildren.forEach(function (i) {
108112
i.memberof = identifier.longname;
109113
});
110114
identifier.description = identifier.classdesc;
111115
delete identifier.classdesc;
112116
delete identifier.params;
113-
} else {
114-
replacements.push({ index: index, items: createConstructor(identifier) });
115117
}
116118
}
117119
});
@@ -128,6 +130,28 @@ function insertConstructors(data) {
128130
return data;
129131
}
130132

133+
function getEs6Constructor(data, identifier) {
134+
return data.find(isES6Constructor);
135+
}
136+
function isES5Class(identifier) {
137+
return testValue(identifier, {
138+
kind: 'class',
139+
meta: { code: { type: 'FunctionDeclaration' } }
140+
});
141+
}
142+
function isES6Class(identifier) {
143+
return testValue(identifier, {
144+
kind: 'class',
145+
meta: { code: { type: 'ClassDeclaration' } }
146+
});
147+
}
148+
function isES6Constructor(identifier) {
149+
return testValue(identifier, {
150+
kind: 'class',
151+
meta: { code: { type: 'MethodDefinition' } }
152+
});
153+
}
154+
131155
function updateIDReferences(identifier, newID) {
132156
var oldID = newID.split('--')[0];
133157
if (oldID && !identifier.isExported) {

lib/transform.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22
var o = require('object-tools')
33
var a = require('array-tools')
4+
const testValue = require('test-value')
45

56
var data
67
exports.createConstructor = createConstructor
@@ -109,22 +110,23 @@ function insertConstructors (data) {
109110
var toDelete = []
110111

111112
data.forEach(function (identifier, index) {
112-
if (identifier.kind === 'class' && identifier.scope !== 'static') {
113-
const es6constructor = a.findWhere(data, { kind: 'class', scope: 'static', memberof: identifier.longname })
113+
if (isES5Class(identifier)) {
114+
replacements.push({ index: index, items: createConstructor(identifier) })
115+
} else if (isES6Class(identifier)) {
116+
const es6constructor = getEs6Constructor(data, identifier)
114117
if (es6constructor) {
115118
if (!(es6constructor.description || (es6constructor.params && es6constructor.params.length))) {
116119
toDelete.push(es6constructor)
117120
}
118121
es6constructor.kind = 'constructor'
122+
es6constructor.memberof = identifier.longname
119123
const constructorChildren = a.where(data, { memberof: es6constructor.longname })
120124
constructorChildren.forEach(i => {
121125
i.memberof = identifier.longname
122126
})
123127
identifier.description = identifier.classdesc
124128
delete identifier.classdesc
125129
delete identifier.params
126-
} else {
127-
replacements.push({ index: index, items: createConstructor(identifier) })
128130
}
129131
}
130132
})
@@ -141,6 +143,29 @@ function insertConstructors (data) {
141143
return data
142144
}
143145

146+
/* unfortunately the jsdoc data structure differs between es5 and es6 classes, hence the need for these four functions */
147+
function getEs6Constructor (data, identifier) {
148+
return data.find(isES6Constructor)
149+
}
150+
function isES5Class (identifier) {
151+
return testValue(identifier, {
152+
kind: 'class',
153+
meta: { code: { type: 'FunctionDeclaration' } }
154+
})
155+
}
156+
function isES6Class (identifier) {
157+
return testValue(identifier, {
158+
kind: 'class',
159+
meta: { code: { type: 'ClassDeclaration' } }
160+
})
161+
}
162+
function isES6Constructor (identifier) {
163+
return testValue(identifier, {
164+
kind: 'class',
165+
meta: { code: { type: 'MethodDefinition' } }
166+
})
167+
}
168+
144169
function updateIDReferences (identifier, newID) {
145170
var oldID = newID.split('--')[0]
146171
if (oldID && !identifier.isExported) {

0 commit comments

Comments
 (0)