From 22d33a3605378de5776d4df297dbbd8db1eda55e Mon Sep 17 00:00:00 2001 From: Patrick Gotthardt Date: Sat, 15 Oct 2016 20:12:17 +0200 Subject: [PATCH 1/2] Resolve includes without extension Avoid exception when no comment was attached. --- lib/extractors/exported.js | 10 ++++++++-- lib/parsers/javascript.js | 9 +++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/extractors/exported.js b/lib/extractors/exported.js index 7ede439cb..b6b297caa 100644 --- a/lib/extractors/exported.js +++ b/lib/extractors/exported.js @@ -28,7 +28,8 @@ function walkExported(ast, data, addComment) { function getComments(data, path) { if (!hasJSDocComment(path)) { - return [addBlankComment(data, path, path.node)]; + var added = addBlankComment(data, path, path.node); + return added ? [added] : []; } return path.node.leadingComments.filter(isJSDocComment).map(function (comment) { return addComment(data, comment.value, comment.loc, path, path.node.loc, true); @@ -136,7 +137,12 @@ function traverseExportedSubtree(path, data, addComments, overrideName) { } } -function getCachedData(dataCache, path) { +function getCachedData(dataCache, filePath) { + var path = filePath; + if (!nodePath.extname(path)) { + path = require.resolve(path); + } + var value = dataCache[path]; if (!value) { var input = fs.readFileSync(path, 'utf-8'); diff --git a/lib/parsers/javascript.js b/lib/parsers/javascript.js index 73a44316b..e55434ea2 100644 --- a/lib/parsers/javascript.js +++ b/lib/parsers/javascript.js @@ -70,8 +70,13 @@ function _addComment(visited, data, commentValue, commentLoc, path, nodeLoc, inc }); if (path.parentPath && path.parentPath.node) { - context.code = data.source.substring - .apply(data.source, path.parentPath.node.range); + var parentNode = path.parentPath.node; + if (parentNode.range) { + context.code = data.source.substring + .apply(data.source, parentNode.range); + } else { + context.code = data.source.substring(parentNode.start, parentNode.end); + } } } return parse(commentValue, commentLoc, context); From 8955f26d3eca7a835950cb6fa6c0aee0acc107cc Mon Sep 17 00:00:00 2001 From: Patrick Gotthardt Date: Sat, 15 Oct 2016 21:34:54 +0200 Subject: [PATCH 2/2] Add test coverage and remove fallback for old babel api access --- lib/parsers/javascript.js | 7 +------ test/fixture/document-exported.input.js | 2 +- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/parsers/javascript.js b/lib/parsers/javascript.js index e55434ea2..9fada0ed1 100644 --- a/lib/parsers/javascript.js +++ b/lib/parsers/javascript.js @@ -71,12 +71,7 @@ function _addComment(visited, data, commentValue, commentLoc, path, nodeLoc, inc if (path.parentPath && path.parentPath.node) { var parentNode = path.parentPath.node; - if (parentNode.range) { - context.code = data.source.substring - .apply(data.source, parentNode.range); - } else { - context.code = data.source.substring(parentNode.start, parentNode.end); - } + context.code = data.source.substring(parentNode.start, parentNode.end); } } return parse(commentValue, commentLoc, context); diff --git a/test/fixture/document-exported.input.js b/test/fixture/document-exported.input.js index 5e97a6d86..5af071f09 100644 --- a/test/fixture/document-exported.input.js +++ b/test/fixture/document-exported.input.js @@ -47,7 +47,7 @@ var notExportedObject = { func: function() {}, }; -export {x, y3 as y4} from './document-exported/x.js'; +export {x, y3 as y4} from './document-exported/x'; export z from './document-exported/z.js'; export y2Default from './document-exported/y.js';