Skip to content

Commit e0779c3

Browse files
committed
small refactoring of token extracters
1 parent e0de6f6 commit e0779c3

File tree

2 files changed

+57
-27
lines changed

2 files changed

+57
-27
lines changed

__tests__/extractToken.test.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,23 @@ describe('extractToken', () => {
2727
args: [],
2828
});
2929

30-
expect(extractToken.extractFunction(class_constructor_line)).toEqual({
30+
expect(extractToken.extractFunction(variable_line)).toBeNull();
31+
expect(extractToken.extractFunction(class_constructor_line)).toBeNull();
32+
expect(extractToken.extractFunction(class_method_line)).toBeNull();
33+
expect(extractToken.extractFunction(base_class_line)).toBeNull();
34+
});
35+
36+
it('extracts class', () => {
37+
expect(extractToken.extractClass(class_constructor_line)).toEqual({
3138
type: 'class_constructor',
3239
class: 'Class',
3340
args: ['args'],
3441
});
3542

36-
expect(extractToken.extractFunction(variable_line)).toBeNull();
37-
expect(extractToken.extractFunction(class_method_line)).toBeNull();
38-
expect(extractToken.extractFunction(base_class_line)).toBeNull();
43+
expect(extractToken.extractClass(variable_line)).toBeNull();
44+
expect(extractToken.extractClass(function_line)).toBeNull();
45+
expect(extractToken.extractClass(class_method_line)).toBeNull();
46+
expect(extractToken.extractClass(base_class_line)).toBeNull();
3947
});
4048

4149
it('extracts class method', () => {
@@ -113,7 +121,10 @@ describe('extractToken', () => {
113121
{
114122
type: 'global_variable',
115123
name: 'x',
116-
comment: { brief: 'Variable description', type: 'String' },
124+
comment: {
125+
brief: 'Variable description',
126+
type: 'String',
127+
},
117128
},
118129
]);
119130

src/extractToken.js

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const extractVariable = line => {
44
const variable_expr = /^var\s+(\w+)/;
55
const variable_match = line.match(variable_expr);
66
if (variable_match) {
7-
const [_, variable_name] = variable_match;
7+
const [, variable_name] = variable_match;
88
return {
99
type: 'global_variable',
1010
name: variable_name,
@@ -18,20 +18,31 @@ const extractFunction = line => {
1818
const function_expr = /^function\s+(\w+)\s*\((.*)\)/;
1919
const function_match = line.match(function_expr);
2020
if (function_match) {
21-
const [_, function_name, function_args] = function_match;
21+
const [, function_name, function_args] = function_match;
22+
if (!utils.isClassName(function_name)) {
23+
return {
24+
type: 'global_function',
25+
name: function_name,
26+
args: utils.argsArray(function_args),
27+
};
28+
}
29+
}
30+
31+
return null;
32+
};
33+
34+
const extractClass = line => {
35+
const function_expr = /^function\s+(\w+)\s*\((.*)\)/;
36+
const function_match = line.match(function_expr);
37+
if (function_match) {
38+
const [, function_name, function_args] = function_match;
2239
if (utils.isClassName(function_name)) {
2340
return {
2441
type: 'class_constructor',
2542
class: function_name,
2643
args: utils.argsArray(function_args),
2744
};
2845
}
29-
30-
return {
31-
type: 'global_function',
32-
name: function_name,
33-
args: utils.argsArray(function_args),
34-
};
3546
}
3647

3748
return null;
@@ -42,7 +53,7 @@ const extractBaseClass = line => {
4253
const base_class_match = line.match(base_class_expr);
4354

4455
if (base_class_match) {
45-
const [_, class_name, base_class_name] = base_class_match;
56+
const [, class_name, base_class_name] = base_class_match;
4657
return {
4758
type: 'base_class',
4859
class: class_name,
@@ -57,7 +68,7 @@ const extractClassMethod = line => {
5768
const class_method_expr = /^(\w+)\.prototype\.(\w+)\s*\=\s*function\((.*)\)/;
5869
const class_method_match = line.match(class_method_expr);
5970
if (class_method_match) {
60-
const [_, class_name, method_name, method_args] = class_method_match;
71+
const [, class_name, method_name, method_args] = class_method_match;
6172
return {
6273
type: 'class_method',
6374
class: class_name,
@@ -75,18 +86,18 @@ const extractComment = comment_array => {
7586
const param_expr = /^\s*\*\s*@param\s*(type\:(\w+))?\s*(\w+)\s*(.*)$/;
7687
const return_expr = /^\s*\*\s*@return\s*(type\:(\w+))?\s*(.*)$/;
7788

78-
return comment_array.reduce((comment, line) => {
89+
const comment = comment_array.reduce((comment, line) => {
7990
const oneliner_match = line.match(oneliner_expr);
8091
if (oneliner_match) {
81-
const [_, __, type, brief] = oneliner_match;
92+
const [, , type, brief] = oneliner_match;
8293
comment.brief = brief;
8394
comment.type = type;
8495
return comment;
8596
}
8697

8798
const brief_match = line.match(brief_expr);
8899
if (brief_match) {
89-
const [_, brief] = brief_match;
100+
const [, brief] = brief_match;
90101
comment.brief = brief;
91102
return comment;
92103
}
@@ -96,60 +107,67 @@ const extractComment = comment_array => {
96107
if (!comment.params) {
97108
comment.params = {};
98109
}
99-
const [_, __, type, name, brief] = param_match;
110+
const [, , type, name, brief] = param_match;
100111
comment.params[name] = { brief, name, type };
101112
return comment;
102113
}
103114

104115
const return_match = line.match(return_expr);
105116
if (return_match) {
106-
const [_, __, type, brief] = return_match;
117+
const [, , type, brief] = return_match;
107118
comment.return = { brief, type };
108119
}
109120

110121
return comment;
111122
}, {});
123+
124+
return comment;
112125
};
113126

114127
var reading_comment = false;
115128
var comment_ready = false;
116-
var comment = [];
129+
var comment_lines = [];
130+
var comment = null;
117131

118132
const extractToken = (tokens, line) => {
119133
if (utils.isSingleLineComment(line)) {
134+
comment_lines = [line];
135+
comment = extractComment(comment_lines);
120136
comment_ready = true;
121-
comment = [line];
122137
return tokens;
123138
}
124139

125140
if (utils.isCommentEnd(line)) {
126-
reading_comment = false;
141+
comment = extractComment(comment_lines);
127142
comment_ready = true;
143+
reading_comment = false;
128144
return tokens;
129145
}
130146

131147
if (reading_comment) {
132-
comment.push(line);
148+
comment_lines.push(line);
133149
return tokens;
134150
}
135151

136152
if (utils.isCommentStart(line)) {
137-
reading_comment = true;
153+
comment_lines = [];
154+
comment = null;
138155
comment_ready = false;
139-
comment = [];
156+
reading_comment = true;
140157

141158
return tokens;
142159
}
143160

144161
var token =
145162
extractVariable(line) ||
146163
extractFunction(line) ||
164+
extractClass(line) ||
147165
extractClassMethod(line) ||
148166
extractBaseClass(line);
149167

150168
if (token) {
151169
if (comment_ready) {
152-
token.comment = extractComment(comment);
170+
token.comment = comment;
153171
comment_ready = false;
154172
}
155173

@@ -163,6 +181,7 @@ exports = module.exports = extractToken;
163181

164182
exports.extractVariable = extractVariable;
165183
exports.extractFunction = extractFunction;
184+
exports.extractClass = extractClass;
166185
exports.extractClassMethod = extractClassMethod;
167186
exports.extractBaseClass = extractBaseClass;
168187
exports.extractComment = extractComment;

0 commit comments

Comments
 (0)