Skip to content

Commit 37a91b2

Browse files
boblannontmcw
authored andcommitted
fix: github links for @typedef comments should link to comment, not context (#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
1 parent 43682b2 commit 37a91b2

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

__tests__/lib/github.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ function evaluate(fn) {
2525
);
2626
}
2727

28+
afterEach(function() {
29+
mock.restore();
30+
});
31+
2832
test('github', function() {
2933
mock(mockRepo.master);
3034

@@ -42,8 +46,6 @@ test('github', function() {
4246
path: 'index.js',
4347
url: 'https://github.com/foo/bar/blob/this_is_the_sha/index.js#L6-L8'
4448
});
45-
46-
mock.restore();
4749
});
4850

4951
test('malformed repository', function() {
@@ -60,8 +62,6 @@ test('malformed repository', function() {
6062
}
6163
})[0].context.github
6264
).toBe(undefined);
63-
64-
mock.restore();
6565
});
6666

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

86-
mock.restore();
87+
test('typedef', function() {
88+
mock(mockRepo.master);
89+
90+
expect(
91+
evaluate(function() {
92+
/**
93+
* A number, or a string containing a number.
94+
* @typedef {(number|string)} NumberLike
95+
*/
96+
97+
/**
98+
* get one
99+
* @returns {number} one
100+
*/
101+
function getOne() {
102+
return 1;
103+
}
104+
})[0].context.github
105+
).toEqual({
106+
path: 'index.js',
107+
url: 'https://github.com/foo/bar/blob/this_is_the_sha/index.js#L2-L5'
108+
});
87109
});

declarations/comment.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ type Comment = {
106106
type?: DoctrineType,
107107

108108
context: CommentContext,
109+
loc: CommentLoc,
109110

110111
path?: Array<{
111112
name: string,

src/github.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,20 @@ module.exports = function(comment: Comment) {
2121
.join('/');
2222

2323
if (urlPrefix) {
24+
let startLine;
25+
let endLine;
26+
27+
if (comment.kind == 'typedef') {
28+
startLine = comment.loc.start.line;
29+
endLine = comment.loc.end.line;
30+
} else {
31+
startLine = comment.context.loc.start.line;
32+
endLine = comment.context.loc.end.line;
33+
}
34+
2435
comment.context.github = {
2536
url:
26-
urlPrefix +
27-
fileRelativePath +
28-
'#L' +
29-
comment.context.loc.start.line +
30-
'-' +
31-
'L' +
32-
comment.context.loc.end.line,
37+
urlPrefix + fileRelativePath + '#L' + startLine + '-' + 'L' + endLine,
3338
path: fileRelativePath
3439
};
3540
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy