Skip to content

Resolve imports without extension #569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 16, 2016

Conversation

pago
Copy link
Contributor

@pago pago commented Oct 15, 2016

In my use case, I have a project where imports are written without a file extension, i.e.

import { foo } from './foo'; // => foo.js or foo/index.js

This PR uses require.resolve to figure out the actual file name if no extension was specified.
Most probably, a more advanced resolution would be better but this should be good enough and avoids some confusion.

Additionally, I have fixed a few problems I ran into.

Avoid exception when no comment was attached.
@@ -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] : [];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This avoids running into a NPE later on when addBlankComment does not return a value.

context.code = data.source.substring
.apply(data.source, path.parentPath.node.range);
var parentNode = path.parentPath.node;
if (parentNode.range) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure when Babel dropped range but it might be possible to remove this branch entirely and just use start and end directly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We updated our babel deps pretty recently. I think we should be able to remove this.

Copy link
Contributor

@arv arv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to test this?

function getCachedData(dataCache, filePath) {
var path = filePath;
if (!nodePath.extname(path)) {
path = require.resolve(path);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes sense. A lot of people use node's resolve mechanism today.

context.code = data.source.substring
.apply(data.source, path.parentPath.node.range);
var parentNode = path.parentPath.node;
if (parentNode.range) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We updated our babel deps pretty recently. I think we should be able to remove this.

@pago
Copy link
Contributor Author

pago commented Oct 15, 2016

@arv I've added test coverage and removed the fallback for the old Babel API.
Test is dead simple (just removed .js from the test fixture in one occurance) but I guess its ok.

Copy link
Contributor

@arv arv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great.

I'll merge this.

@arv arv merged commit 9e6bcef into documentationjs:master Oct 16, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants