Skip to content

Commit

Permalink
Fix: #11. Support export class/function expression
Browse files Browse the repository at this point in the history
  • Loading branch information
Joouis committed Nov 24, 2017
1 parent a928793 commit f2f014b
Showing 1 changed file with 79 additions and 15 deletions.
94 changes: 79 additions & 15 deletions lib/tag-generators/javascript.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
'use babel';

// More types support please refer to esprima source code
const SINGLE_TAG_TYPE = [
'ClassDeclaration',
'ExpressionStatement',
'ExportDefaultDeclaration',
'ExportNamedDeclaration',
'FunctionDeclaration',
'MethodDefinition'
];
const MULTI_TAGS_TYPE = [
'ImportDeclaration',
'VariableDeclaration'
];

export default {

init() {
Expand Down Expand Up @@ -45,12 +59,9 @@ export default {
child = null,
name, id;

// More types support please refer to esprima source code
const oneTagType = ['ClassDeclaration', 'ExpressionStatement', 'ExportDefaultDeclaration', 'FunctionDeclaration', 'MethodDefinition'];
const multiTagsType = ['ImportDeclaration', 'VariableDeclaration'];

if (oneTagType.includes(type)) {
if (SINGLE_TAG_TYPE.includes(type)) {
const line = i.loc.start.line;

if ('ClassDeclaration' === type) {
name = i.id.name;
id = `${line}-${name}`;
Expand All @@ -61,6 +72,7 @@ export default {
self.parseDeclar(child, i.body.body);
}
}

// Only for `module.exports` now
else if ('ExpressionStatement' === type) {
let left = i.expression.left,
Expand All @@ -77,24 +89,70 @@ export default {

if ('ClassExpression' === right.type) self.parseDeclar(child, right.body.body);
else if ('ObjectExpression' === right.type) self.parseExpr(child, right.properties);
} else if ('ExportDefaultDeclaration' === type) {
name = 'exports';
}

/*
* Pattern: export default expression;
*/
else if ('ExportDefaultDeclaration' === type) {
// TODO: name should be more specific!
name = 'export';
id = `${line}-${name}`;
type = 'class';

let dec = i.declaration;
if ('ObjectExpression' === dec.type && dec.properties.length > 0) {
if ('ObjectExpression' === dec.type &&
dec.properties.length > 0)
{
child = {};
self.parseExpr(child, dec.properties);
} else if ('ClassDeclaration' === dec.type && dec.body.body.length > 0) {
}
else if ('ClassDeclaration' === dec.type &&
dec.body.body.length > 0)
{
child = {};
self.parseExpr(child, dec.body.body)
} else if ('FunctionDeclaration' === dec.type && dec.body.body.length > 0) {
self.parseExpr(child, dec.body.body);
}
else if ('FunctionDeclaration' === dec.type &&
dec.body.body.length > 0)
{
type = 'function';
child = {};
self.parseDeclar(child, dec.body.body);
}
} else if ('FunctionDeclaration' === type) {
}

/*
* Pattern: export declaration. Declarations could be:
* - class Foo {}
* - function Foo {}
*/
else if ('ExportNamedDeclaration' === type) {
let dec = i.declaration;
name = dec.id.name;
id = `${line}-${name}`;
type = 'class';

// Do not support variables now
if (!name) return;

if ('ClassDeclaration' === dec.type &&
dec.body.body.length > 0)
{
child = {};
self.parseExpr(child, dec.body.body);
}
else if ('FunctionDeclaration' === dec.type &&
dec.body.body.length > 0)
{
type = 'function';
child = {};
self.parseDeclar(child, dec.body.body);
}

}

else if ('FunctionDeclaration' === type) {
let params = [];
i.params.forEach(p => {
params.push(p.name);
Expand All @@ -107,7 +165,9 @@ export default {
child = {};
self.parseDeclar(child, i.body.body);
}
} else if ('MethodDefinition' === type) {
}

else if ('MethodDefinition' === type) {
let params = [];
i.value.params.forEach(p => {
params.push(p.name);
Expand All @@ -130,7 +190,9 @@ export default {
child: child,
id: id
};
} else if (multiTagsType.includes(type)) {

} else if (MULTI_TAGS_TYPE.includes(type)) {

if ('ImportDeclaration' === type) {
i.specifiers.forEach(sp => {
let line = sp.loc.start.line;
Expand All @@ -147,7 +209,9 @@ export default {
id: id
}
});
} else if ('VariableDeclaration' === type) {
}

else if ('VariableDeclaration' === type) {
i.declarations.forEach(v => {
let line = v.loc.start.line;
name = v.id.name;
Expand Down

0 comments on commit f2f014b

Please sign in to comment.