Skip to content

Commit 9e6bcef

Browse files
pagoarv
authored andcommitted
Resolve imports without extension (#569)
For `--document-exported` we now use Node.js's `require.resolve` method to resolve the path for modules referenced by other modules. This fixes issues where we failed to load imported modules which lead to a null comment being attached which caused an exception.
1 parent 9c0adba commit 9e6bcef

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

lib/extractors/exported.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ function walkExported(ast, data, addComment) {
2828

2929
function getComments(data, path) {
3030
if (!hasJSDocComment(path)) {
31-
return [addBlankComment(data, path, path.node)];
31+
var added = addBlankComment(data, path, path.node);
32+
return added ? [added] : [];
3233
}
3334
return path.node.leadingComments.filter(isJSDocComment).map(function (comment) {
3435
return addComment(data, comment.value, comment.loc, path, path.node.loc, true);
@@ -136,7 +137,12 @@ function traverseExportedSubtree(path, data, addComments, overrideName) {
136137
}
137138
}
138139

139-
function getCachedData(dataCache, path) {
140+
function getCachedData(dataCache, filePath) {
141+
var path = filePath;
142+
if (!nodePath.extname(path)) {
143+
path = require.resolve(path);
144+
}
145+
140146
var value = dataCache[path];
141147
if (!value) {
142148
var input = fs.readFileSync(path, 'utf-8');

lib/parsers/javascript.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ function _addComment(visited, data, commentValue, commentLoc, path, nodeLoc, inc
7070
});
7171

7272
if (path.parentPath && path.parentPath.node) {
73-
context.code = data.source.substring
74-
.apply(data.source, path.parentPath.node.range);
73+
var parentNode = path.parentPath.node;
74+
context.code = data.source.substring(parentNode.start, parentNode.end);
7575
}
7676
}
7777
return parse(commentValue, commentLoc, context);

test/fixture/document-exported.input.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ var notExportedObject = {
4747
func: function() {},
4848
};
4949

50-
export {x, y3 as y4} from './document-exported/x.js';
50+
export {x, y3 as y4} from './document-exported/x';
5151
export z from './document-exported/z.js';
5252
export y2Default from './document-exported/y.js';
5353

0 commit comments

Comments
 (0)