Skip to content

Commit f4fc83c

Browse files
committed
add a new scanDocs options and use scan* options
1 parent 17bdc88 commit f4fc83c

5 files changed

Lines changed: 90 additions & 44 deletions

File tree

bin/bench.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ var os = require('os');
1212
workspace = new repository(
1313
__dirname + '/../test/workspaces/magento2', {
1414
forkWorker: process.argv.indexOf('--fork') > -1,
15-
exclude: ['bin']
15+
exclude: ['bin'],
16+
scanVars: process.argv.indexOf('--disable-vars') === -1,
17+
scanExpr: process.argv.indexOf('--disable-expr') === -1,
18+
scanDocs: process.argv.indexOf('--disable-docs') === -1
1619
});
20+
console.log('Configuration', workspace.options);
1721
var i = 0;
1822
workspace.on('error', function(e) {
1923
console.error(e);
@@ -49,6 +53,23 @@ workspace.scan().then(function() {
4953
elapsed_time('List of constants : ' + items.length);
5054
var ns = workspace.getNamespace('\\Magento\\Catalog\\Ui\\DataProvider\\Product\\Form\\Modifier');
5155
elapsed_time('Found Namespace : ' + (ns ? ns.name : 'KO'));
56+
console.log('Read ' + workspace.counter.loaded + ' files / ' + workspace.counter.total + ' total files');
57+
console.log('Parsed ' + Math.round(workspace.counter.size / 1024 / 1024) + 'Mb');
58+
console.log('Extracted ' + workspace.counter.symbols + ' symbols');
59+
var kindStats = {};
60+
for(var f in workspace.files) {
61+
var file = workspace.files[f];
62+
for(var i = 0; i < file.nodes.length; i++) {
63+
var node = file.nodes[i];
64+
if (!(node.type in kindStats)) {
65+
kindStats[node.type] = 1;
66+
} else {
67+
kindStats[node.type] ++;
68+
}
69+
}
70+
}
71+
console.log('Symbols stats : \n\n', kindStats);
5272
// tada (force workers to stop)
73+
// console.log(JSON.stringify(workspace.cache(), null, 1));
5374
process.exit(0);
5475
});

src/block.js

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ block.prototype.consumeChild = function(ast) {
7373

7474
// handle class definition
7575
if (ast.kind === 'doc') {
76-
this._lastDoc = item;
76+
if (this.getRepository().options.scanDocs) {
77+
this._lastDoc = item;
78+
} else {
79+
this._lastDoc = null;
80+
}
7781
} else {
7882

7983
// attach last doc node to current node
@@ -121,42 +125,54 @@ block.prototype.consumeChild = function(ast) {
121125

122126
// consume IF nodes
123127
else if (ast.kind === 'if') {
124-
// IF BODY
125-
if (ast.body) {
126-
this.blocks.push(
127-
ptr.create('block', this, ast.body)
128-
);
129-
}
130-
// ELSE STATEMENT
131-
if (ast.alternate) {
132-
this.blocks.push(
133-
ptr.create('block', this, ast.alternate)
134-
);
128+
if (this.getRepository().options.scanExpr) {
129+
130+
// IF BODY
131+
if (ast.body) {
132+
this.blocks.push(
133+
ptr.create('block', this, ast.body)
134+
);
135+
}
136+
// ELSE STATEMENT
137+
if (ast.alternate) {
138+
this.blocks.push(
139+
ptr.create('block', this, ast.alternate)
140+
);
141+
}
142+
} else {
143+
// inner scan only (ignore blocks)
144+
if (ast.body) this.scanForChilds(ast.body);
145+
if (ast.alternate) this.scanForChilds(ast.alternate);
135146
}
136147
}
137148

138149
// try nodes
139150
else if (ast.kind === 'try') {
140151

141-
// BODY
142-
this.blocks.push(
143-
ptr.create('block', this, ast.body)
144-
);
145-
146-
// CATCH
147-
if (Array.isArray(ast.catches)) {
148-
ast.catches.forEach(function(item) {
149-
this.blocks.push(
150-
ptr.create('block', this, item.body)
151-
);
152-
}.bind(this));
153-
}
154-
155-
// FINALLY
156-
if (ast.allways) {
152+
if (this.getRepository().options.scanExpr) {
153+
// BODY
157154
this.blocks.push(
158-
ptr.create('block', this, ast.allways)
155+
ptr.create('block', this, ast.body)
159156
);
157+
// CATCH
158+
if (Array.isArray(ast.catches)) {
159+
ast.catches.forEach(function(item) {
160+
this.blocks.push(
161+
ptr.create('block', this, item.body)
162+
);
163+
}.bind(this));
164+
}
165+
// FINALLY
166+
if (ast.allways) {
167+
this.blocks.push(
168+
ptr.create('block', this, ast.allways)
169+
);
170+
}
171+
} else {
172+
// inner scan only (ignore blocks)
173+
this.scanForChilds(ast.body);
174+
if (Array.isArray(ast.catches)) this.scanForChilds(ast.catches);
175+
if (ast.allways) this.scanForChilds(ast.allways);
160176
}
161177
}
162178

@@ -170,7 +186,11 @@ block.prototype.consumeChild = function(ast) {
170186
}
171187

172188
// variables (by assignment)
173-
else if (ast.kind === 'assign' && ast.left.kind === 'variable') {
189+
else if (
190+
ast.kind === 'assign' &&
191+
ast.left.kind === 'variable' &&
192+
this.getRepository().options.scanVars
193+
) {
174194
this.variables.push(
175195
ptr.create('variable', this, ast)
176196
);

src/class.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ _class.prototype.consume = function(ast) {
6767
for(var i = 0; i < ast.body.length; i++) {
6868
var item = ast.body[i];
6969
if (item.kind === 'doc') {
70-
lastDoc = item;
70+
if (this.getRepository().options.scanDocs) {
71+
lastDoc = item;
72+
}
7173
} else {
7274
item.doc = lastDoc;
7375
if (item.kind === 'classconstant') {

src/repository.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ var repository = function(directory, options) {
5757
scanVars: true,
5858
// extract scopes from
5959
scanExpr: true,
60+
// extract documentation from
61+
scanDocs: true,
6062
// default parsing encoding
6163
encoding: 'utf8',
6264
// should spawn a worker process to avoir blocking
@@ -97,7 +99,9 @@ var repository = function(directory, options) {
9799
total: 0,
98100
loading: 0,
99101
loaded: 0,
100-
error: 0
102+
error: 0,
103+
symbols: 0,
104+
size: 0
101105
};
102106
this.directory = path.resolve(directory);
103107

@@ -223,9 +227,11 @@ repository.prototype.scan = function(directory) {
223227
filename,
224228
self.options.encoding,
225229
stat
226-
).then(function() {
230+
).then(function(file) {
227231
self.counter.loading--;
228232
self.counter.loaded++;
233+
self.counter.size += file.size;
234+
self.counter.symbols += file.nodes.length;
229235
self.emit('progress', self.counter);
230236
done();
231237
}, function(e) {
@@ -333,19 +339,18 @@ repository.prototype.parse = function(filename, encoding, stat) {
333339
var ast;
334340
if (!err) {
335341
try {
336-
//console.log('start', filename);
337342
var reader = new parser({
338343
ast: {
339344
withPositions: true
340345
},
341346
parser: {
342-
extractDoc: true,
347+
extractDoc: self.options.scanDocs,
343348
suppressErrors: true
344349
}
345350
});
346-
data = data.toString(encoding);
347-
ast = reader.parseCode(data);
348-
//console.log('done', filename);
351+
ast = reader.parseCode(
352+
data.toString(encoding)
353+
);
349354
} catch (e) {
350355
err = e;
351356
}
@@ -357,11 +362,8 @@ repository.prototype.parse = function(filename, encoding, stat) {
357362
} else {
358363
try {
359364
self.files[filename] = new file(self, filename, ast);
360-
//console.log('parsed', filename);
361365
self.files[filename].refresh();
362-
if (self.options.cacheByFileSize) {
363-
self.files[filename].size = data.length;
364-
}
366+
self.files[filename].size = data.length;
365367
if (self.options.cacheByFileDate) {
366368
self.files[filename].mtime = data.length;
367369
}

src/worker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ module.exports = function(filename, crc32, directory, options) {
151151
cacheByFileHash: options.cacheByFileHash,
152152
encoding: options.encoding,
153153
scanExpr: options.scanExpr,
154-
scanVars: options.scanVars
154+
scanVars: options.scanVars,
155+
scanDocs: options.scanDocs
155156
});
156157
};

0 commit comments

Comments
 (0)