Skip to content

Commit c38cd8b

Browse files
committed
0.4.0 - breaking changes
refactored to only parse code context, no comments. API is the same.
1 parent 6d85c95 commit c38cd8b

File tree

1 file changed

+8
-119
lines changed

1 file changed

+8
-119
lines changed

index.js

+8-119
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,21 @@
11
/*!
22
* code-context <https://github.com/jonschlinkert/code-context>
33
*
4-
* Copyright (c) 2014 Jon Schlinkert, contributors.
4+
* Copyright (c) 2014-2015 Jon Schlinkert.
55
* Regex sourced from https://github.com/visionmedia/dox
66
* Licensed under the MIT License
77
*/
88

99
'use strict';
1010

11-
var extractComments = require('extract-comments');
11+
var parse = require('parse-code-context');
1212

1313
module.exports = function (str) {
14-
str = str.replace(/\r/g, '');
15-
var context = [];
16-
17-
var comments = extractComments(str);
18-
19-
str.split(/\n/g).forEach(function(line, i) {
20-
var strict = line.replace(/^\s+/, '');
21-
var match;
22-
i = i + 1;
23-
24-
// Code comments
25-
if (match = /^\/\*/.exec(strict)) {
26-
if (i === 1) {
27-
context.push(comments[0]);
28-
} else {
29-
context.push(comments[i]);
30-
}
31-
32-
// function statement
33-
} else if (match = /^function[ \t]([\w$]+)[ \t]*([\w\W]+)?/.exec(strict)) {
34-
context.push({
35-
begin: i,
36-
type: 'function statement',
37-
name: match[1],
38-
params: (match[2]).split(/\W/g).filter(Boolean),
39-
string: match[1] + '()',
40-
original: strict
41-
});
42-
// function expression
43-
} else if (match = /^var[ \t]*([\w$]+)[ \t]*=[ \t]*function([\w\W]+)?/.exec(strict)) {
44-
context.push({
45-
begin: i,
46-
type: 'function expression',
47-
name: match[1],
48-
params: (match[2]).split(/\W/g).filter(Boolean),
49-
string: match[1] + '()',
50-
original: strict
51-
});
52-
// module.exports expression
53-
} else if (match = /^(module\.exports)[ \t]*=[ \t]*function[ \t]([\w$]+)[ \t]*([\w\W]+)?/.exec(strict)) {
54-
context.push({
55-
begin: i,
56-
type: 'function expression',
57-
receiver: match[1],
58-
name: match[2],
59-
params: (match[3]).split(/\W/g).filter(Boolean),
60-
string: match[1] + '()',
61-
original: strict
62-
});
63-
// module.exports method
64-
} else if (match = /^(module\.exports)[ \t]*=[ \t]*function([\w\W]+)?/.exec(strict)) {
65-
context.push({
66-
begin: i,
67-
type: 'method',
68-
receiver: match[1],
69-
name: '',
70-
params: (match[2]).split(/\W/g).filter(Boolean),
71-
string: match[1] + '.' + match[2] + '()',
72-
original: strict
73-
});
74-
// prototype method
75-
} else if (match = /^([\w$]+)\.prototype\.([\w$]+)[ \t]*=[ \t]*function([\w\W]+)?/.exec(strict)) {
76-
context.push({
77-
begin: i,
78-
type: 'prototype method',
79-
class: match[1],
80-
name: match[2],
81-
params: (match[3]).split(/\W/g).filter(Boolean),
82-
string: match[1] + '.prototype.' + match[2] + '()',
83-
original: strict
84-
});
85-
// prototype property
86-
} else if (match = /^([\w$]+)\.prototype\.([\w$]+)[ \t]*=[ \t]*([^\n;]+)/.exec(strict)) {
87-
context.push({
88-
begin: i,
89-
type: 'prototype property',
90-
class: match[1],
91-
name: match[2],
92-
value: match[3],
93-
string: match[1] + '.prototype.' + match[2],
94-
original: strict
95-
});
96-
// method
97-
} else if (match = /^([\w$.]+)\.([\w$]+)[ \t]*=[ \t]*function([\w\W]+)?/.exec(strict)) {
98-
context.push({
99-
begin: i,
100-
type: 'method',
101-
receiver: match[1],
102-
name: match[2],
103-
params: (match[3]).split(/\W/g).filter(Boolean),
104-
string: match[1] + '.' + match[2] + '()',
105-
original: strict
106-
});
107-
// property
108-
} else if (match = /^([\w$]+)\.([\w$]+)[ \t]*=[ \t]*([^\n;]+)/.exec(strict)) {
109-
context.push({
110-
begin: i,
111-
type: 'property',
112-
receiver: match[1],
113-
name: match[2],
114-
value: match[3],
115-
string: match[1] + '.' + match[2],
116-
original: strict
117-
});
118-
// declaration
119-
} else if (match = /^var[ \t]+([\w$]+)[ \t]*=[ \t]*([^\n;]+)/.exec(line)) {
120-
context.push({
121-
begin: i,
122-
type: 'declaration',
123-
name: match[1],
124-
value: match[2],
125-
string: match[1],
126-
original: line
127-
});
14+
return str.split(/[\r\n]/).reduce(function(acc, line, i) {
15+
var res = parse(line.replace(/^\s+/, ''), i + 1);
16+
if (res) {
17+
acc.push(res);
12818
}
129-
});
130-
131-
return context.filter(Boolean);
19+
return acc;
20+
}, []);
13221
};

0 commit comments

Comments
 (0)