Skip to content

Commit

Permalink
fix: github links for @typedef comments should link to comment, not c…
Browse files Browse the repository at this point in the history
…ontext (#1024)

* github link to typedef points to comment lines

* adding test for typedef github links

* adding comment.loc for flow check

* reverting to original formatting

* use afterEach to restore mock even if tests fail

* fixing line numbers in expected value
  • Loading branch information
boblannon authored and tmcw committed Mar 1, 2018
1 parent 43682b2 commit 37a91b2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
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,
path: fileRelativePath
};
}
Expand Down

0 comments on commit 37a91b2

Please sign in to comment.