-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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
Add types annotations #12102
Add types annotations #12102
Conversation
4e015aa
to
4e5e4bb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do need a Gulp task here to generate the type annotations like in the other WIP PR because it should be automated and end up in each release. Another question I have is: given that we're not TypeScript users here, how can we be sure that the generated type annotations are valid (not broken by any changes we make)? Is there a validator for that that we can use?
Hmm - the only idea I have is to create some smoke-screen tests that make sure all the types are still correct and can be at least compiled. Because once |
I'm new to gulp, so I'd need some guidance here. Honestly, I was never able to understand where the different versions of the project come from. Also, it's the first time I see the idea of converting jsdoc to ts-types ;) So any help from @oBusk and @tamuratak is very welcome! I'm currently not even sure how to merge all the Is there some documentation that describes how the pdfjs-dist build is done? I stopped at trying to understand where the |
I'm not sure I'm doing the right thing here ;) But this nearly works:
It complains about the If I remove
in So there's still a lot of work to do. |
So this is starting to be nearly usable ;) After a
and then importing the local pdf.js in my project using
|
I think the Gulp task was already made in https://github.com/mozilla/pdf.js/pull/10575/files and should already take care of bundling it in the distribution correctly. |
OK - this latest commits add the I chose to focus on
But this means that all types will need to be exported by |
f75cbf3
to
5a8a6f0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once you're getting this closer to done, please remember https://github.com/mozilla/pdf.js/wiki/Squashing-Commits
(Also, note that it's possible to list multiple authors of a commit, see e.g. here.)
One commit a day keeps the doctor away... I included all comments from @Snuffleupagus , except those I'm not sure what to do. Currently you can change the
in
or
My TODO-list from above didn't get shorter, unfortunately. And while JSDoc seems to give better documentation, I cannot use it. TSC gives me better documentation, but currently some of it is missing :( And yes, commits will be squashed, co-authors added. But I feel it's still some way to go... |
0ece522
to
4388616
Compare
@Snuffleupagus - two questions from my TODO-list where I'd like to have your input:
|
d936979
to
d232312
Compare
Unfortunately I don't really know anything about TypeScript, sorry! My interest here is essentially limited to making sure that a solution, whatever it may end up looking like, is reasonable/maintainable from a general PDF.js-library perspective. And also, that we don't accidentally end up exposing unintended functionality through the public API (i.e. as defined in the |
@ineiti This PR, ineiti/pull/1, makes the return type of |
I added ineiti#2 to compile the definitions using typescript via gulp-typescript rather than |
thanks - merged. |
thanks - merged... |
@oBusk - I think I merged too fast. The PR you gave only creates one file, and I cannot use it in my project afterwards... So now I'm playing with gulp-typescript, trying to create a valid I also removed the So this latest commit doesn't work :( |
41c0e33
to
3cca747
Compare
OK, merged the commits, added the contributors, went again through all comments on the code.
The last task is how to test this thing - if I use typescript, I'll have to build it first before I can test it - is that a good idea? |
3cca747
to
7af740d
Compare
Hmm - that's strange. I was able to re-write the callbacks in api.js like this: and then it passes However, there are other places like that happily accept callback definitions: But, if I copy the line from |
@timvandermeij - I closed the trivial comments that have been fixed, but left the other comments open where I think you need to make sure that my proposition is correct. I hope that matches your normal workflow. |
/botio-linux preview |
From: Bot.io (Linux m4)ReceivedCommand cmd_preview from @timvandermeij received. Current queue size: 0 Live output at: http://54.67.70.0:8877/4f1cef8ce16b21a/output.txt |
From: Bot.io (Linux m4)SuccessFull output at http://54.67.70.0:8877/4f1cef8ce16b21a/output.txt Total script time: 3.40 mins Published |
/botio test |
From: Bot.io (Linux m4)ReceivedCommand cmd_test from @timvandermeij received. Current queue size: 0 Live output at: http://54.67.70.0:8877/574214bb517ba83/output.txt |
From: Bot.io (Windows)ReceivedCommand cmd_test from @timvandermeij received. Current queue size: 0 Live output at: http://54.215.176.217:8877/d5786cf652fe66b/output.txt |
From: Bot.io (Linux m4)FailedFull output at http://54.67.70.0:8877/574214bb517ba83/output.txt Total script time: 26.62 mins
Image differences available at: http://54.67.70.0:8877/574214bb517ba83/reftest-analyzer.html#web=eq.log |
From: Bot.io (Windows)FailedFull output at http://54.215.176.217:8877/d5786cf652fe66b/output.txt Total script time: 31.87 mins
Image differences available at: http://54.215.176.217:8877/d5786cf652fe66b/reftest-analyzer.html#web=eq.log |
I see some regressions failed - but it looks like its in the rendering part, which I think we didn't touch. Is there anything we can do to fix this? Or is this expected? |
No worries about those; those are known intermittent failures that have nothing to do with your patch and should be fixed once we upgrade Puppeteer in #12123. |
So excited pdfjs is on the way to TypeScript! 🍺 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The most important acceptance criteria for TypeScript definitions were that the public API may not be changed (so no changes in src/pdf.js
), that the solution is maintainable for PDF.js developers (by not having the types separated from the code but generated from the JSDoc comments) and that the solution is tested automatically (so that we know the types will work and get a failing test otherwise).
This pull request checks all those boxes. It improves the existing documentation quite a bit and gives an extra incentive to keep the documentation up-to-date because a test now fails if for example a new parameter is not added to the corresponding JSDoc comment.
All in all, I think this is nice work from everyone involved and we can now merge it! We are open for contributions from the community to improve the documentation even further, not only for better type annotations but also for the better API documentation.
Thank you for your work on this!
A couple of questions:
I'd really like to get the above points addressed rather quickly, one way or another, since especially the second one may quickly become an issue. (Or this patch backed-out, pending the resolution of the above.) |
Yes, I looked at the bot output and also ran it locally after rebasing onto the current master before merging, also because this was commented as not working in an earlier version of the patch. I also ran all test commands locally after rebasing to make sure it works locally as well as after the most recently merged patches.
The runtime impact from generating the TypeScript definitions is minimal in my testing (at most few seconds). Moreover, it only seems to fail if there is a mismatch between the actual code and the JSDoc comments. This showed up in an earlier version of the patch where the
Agreed, but I must note that consistency was already an issue in that file even before this patch to begin with, and can be dealt with in a follow-up. Even before I had already seen JSDoc comments where the descriptions are separated with a dash versus without, are with/without two spaces on the next line, are aligned on the next line, et cetera. |
Based on some of the changes seen in this patch, I do have some doubts about that; hence why this worries me! As long as we're still going to, on principle, accept patches that fail the |
* @returns {Promise<Object>} A promise that is resolved with an {Object} | ||
* containing the viewer preferences. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This removed part of the existing comment, why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I missed this in the review and will put that back as part of the upcoming patch. Thank you for noticing!
* @returns {Promise<Array<string> | null>} A promise that is | ||
* resolved with an {Array} containing the page labels that correspond to | ||
* the page indexes, or `null` when no page labels are present in the PDF | ||
* file. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first line is now weirdly short, compared to the rest of them, it looks like this was incorrectly re-formatted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so. I'll address that in the upcoming patch too.
Definitely. My reasoning is that everything that we distribute should be tested, and preferably automatically and in the earliest stage possible so we quickly find out about potential problems. If this PR did not have such a test I would rather not accept it especially because this is not a TypeScript project, but a (vanilla) JavaScript one. That would mean that we would distribute TypeScript definitions without having a clue if they actually work, which is not great. Having a test at least tells us that a patch is going to break the TypeScript definitions. allowing us to consider that and make a decision on what to do with that. Most if not all the time it's likely to turn out to be a missing JSDoc comment update, which is not only useful for the TypeScript definitions but more importantly also for the consistency of our own documentation (since we've seen that this tends to be forgotten sometimes). I therefore see I can't really imagine any failures from Hopefully this helps a bit with the concerns here! |
Moreover, I'll make some time today to address the comments above and work on overall comment consistency within the |
function () { | ||
var packageJsonSrc = packageBowerJson()[0]; | ||
var TYPESTEST_DIR = BUILD_DIR + "typestest/"; | ||
|
||
return merge([ | ||
packageJsonSrc.pipe(gulp.dest(TYPESTEST_DIR)), | ||
gulp | ||
.src([ | ||
GENERIC_DIR + "build/pdf.js", | ||
GENERIC_DIR + "build/pdf.worker.js", | ||
SRC_DIR + "pdf.worker.entry.js", | ||
]) | ||
.pipe(gulp.dest(TYPESTEST_DIR + "build/")), | ||
gulp | ||
.src(TYPES_BUILD_DIR + "**/**") | ||
.pipe(gulp.dest(TYPESTEST_DIR + "build/")), | ||
]); | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, defining this inline isn't really helping readability of the overall task, and it really ought to be moved into a helper function/task (e.g. typestest-pre
) similar to what's done in other parts of the gulpfile.
}, | ||
function (done) { | ||
exec(`node_modules/.bin/tsc -p test/types`, function (err, stdout) { | ||
if (err !== null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably just be if (err) {
, right?
@timvandermeij Any ETA on when these changes will be reflected in the pdfjs-dist repository? |
No, there is no release date planned yet. |
If someone can build it, and post the types here, we can manually list the file in our projects until it's released. |
The types are available in the current pre-release (beta) version of PDF.js; see https://github.com/mozilla/pdf.js/releases and https://www.npmjs.com/package/pdfjs-dist/v/next. |
Follow-up of #10575:
I build it using:
And then trying to run the example from https://github.com/ineiti/pdf_example - I hope that's not too complex example to start from...