Skip to content
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

Fix github links for @typedef comments #1024

Merged
merged 6 commits into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions __tests__/lib/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ function evaluate(fn) {
);
}

afterEach(function() {
mock.restore();
});

test('github', function() {
mock(mockRepo.master);

Expand All @@ -42,8 +46,6 @@ test('github', function() {
path: 'index.js',
url: 'https://github.com/foo/bar/blob/this_is_the_sha/index.js#L6-L8'
});

mock.restore();
});

test('malformed repository', function() {
Expand All @@ -60,8 +62,6 @@ test('malformed repository', function() {
}
})[0].context.github
).toBe(undefined);

mock.restore();
});

test('enterprise repository', function() {
Expand All @@ -82,6 +82,28 @@ test('enterprise repository', function() {
url:
'https://github.enterprise.com/foo/bar/blob/this_is_the_sha/index.js#L6-L8'
});
});

mock.restore();
test('typedef', function() {
mock(mockRepo.master);

expect(
evaluate(function() {
/**
* A number, or a string containing a number.
* @typedef {(number|string)} NumberLike
*/

/**
* get one
* @returns {number} one
*/
function getOne() {
return 1;
}
})[0].context.github
).toEqual({
path: 'index.js',
url: 'https://github.com/foo/bar/blob/this_is_the_sha/index.js#L2-L5'
});
});
1 change: 1 addition & 0 deletions declarations/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ type Comment = {
type?: DoctrineType,

context: CommentContext,
loc: CommentLoc,

path?: Array<{
name: string,
Expand Down
19 changes: 12 additions & 7 deletions src/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ module.exports = function(comment: Comment) {
.join('/');

if (urlPrefix) {
let startLine;
let endLine;

if (comment.kind == 'typedef') {
startLine = comment.loc.start.line;
endLine = comment.loc.end.line;
} else {
startLine = comment.context.loc.start.line;
endLine = comment.context.loc.end.line;
}

comment.context.github = {
url:
urlPrefix +
fileRelativePath +
'#L' +
comment.context.loc.start.line +
'-' +
'L' +
comment.context.loc.end.line,
urlPrefix + fileRelativePath + '#L' + startLine + '-' + 'L' + endLine,
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 didn't intend to re-format this, fwiw. Looks like a commit hook re-formatted.

Copy link
Member

Choose a reason for hiding this comment

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

Yep, that's the intended way - we use husky to add a commit hook and then lint-staged to run run prettier on source files. It's a mouthful but it totally eliminates stylistic eslint rules, the computer decides what's best in terms of formatting. And since this changes comment.context.loc.end.line to endLine, the computer decided that it's short enough to fit on one line, so it did :)

path: fileRelativePath
};
}
Expand Down