Skip to content

Commit

Permalink
feat: show detail log when esdoc could not process code.
Browse files Browse the repository at this point in the history
  • Loading branch information
h13i32maru committed Jun 30, 2015
1 parent 932b222 commit b543e18
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/ESDoc.js
Expand Up @@ -8,6 +8,7 @@ import ESParser from './Parser/ESParser';
import PathResolver from './Util/PathResolver.js';
import DocFactory from './Factory/DocFactory.js';
import TestDocFactory from './Factory/TestDocFactory.js';
import InvalidCodeLogger from './Util/InvalidCodeLogger.js';

let logger = new Logger('ESDoc');

Expand Down Expand Up @@ -225,7 +226,12 @@ export default class ESDoc {
let factory = new DocFactory(ast, pathResolver);

ASTUtil.traverse(ast, (node, parent)=>{
factory.push(node, parent);
try {
factory.push(node, parent);
} catch(e) {
InvalidCodeLogger.show(filePath, node);
throw e;
}
});

return {results: factory.results, ast: ast};
Expand Down Expand Up @@ -253,7 +259,12 @@ export default class ESDoc {
let factory = new TestDocFactory(type, ast, pathResolver);

ASTUtil.traverse(ast, (node, parent)=>{
factory.push(node, parent);
try {
factory.push(node, parent);
} catch(e) {
InvalidCodeLogger.show(filePath, node);
throw e;
}
});

return {results: factory.results, ast: ast};
Expand Down
27 changes: 27 additions & 0 deletions src/Util/InvalidCodeLogger.js
@@ -0,0 +1,27 @@
import fs from 'fs-extra';

class InvalidCodeLogger {
show(filePath, node) {
let lines = fs.readFileSync(filePath).toString().split('\n');
let targetLines = [];
let start;
let end = node.loc.start.line;

if (node.leadingComments && node.leadingComments[0]) {
start = node.leadingComments[0].loc.start.line;
} else {
start = Math.max(0, end - 10);
}

for (let i = start - 1; i < end; i++) {
targetLines.push(`${i}| ` + lines[i]);
}

console.log('error: could not process the following code.');
console.log(filePath);
console.log(targetLines.join('\n'));
console.log('');
}
}

export default new InvalidCodeLogger();

0 comments on commit b543e18

Please sign in to comment.