Skip to content

fix: Fixes type-widening bug when handling plugins. #53

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 14 commits into from
Mar 24, 2020

Conversation

MaximDevoir
Copy link
Contributor

@MaximDevoir MaximDevoir commented Mar 11, 2020

From the ideas that have been thrown out there to solve the issue described in #51, this is my favorite.

Instead of .plugin(arrayOfPlugins[]), which could lead to a type-widening error, this PR moves to use .plugin(plugin1, plugin2, plugin3, etc...), where each plugin is hardcoded into the type-system

Why this approach?

I think this will be easiest on plugin authors, who don't have to change anything with how they currently develop plugins - such as having to worry about whether they need to return an object, if they return void, etc.

This will also be an easy fix for users of Octokit. .plugin([paginateRest, queueWorker, logService]) becomes .plugin(paginateRest, queueWorker, logService)

TODOs

  • Continue to accept arrays as an argument (aka backward-compatible with the previous syntax)
  • Warn users that the syntax has changed. Provide an example of the changed syntax.

Example warning when using old syntax:

corejs-non-array-warning

Fixes #51

@MaximDevoir MaximDevoir changed the title WIP | Fix type-widening bug Fix type-widening bug Mar 20, 2020
@gr2m
Copy link
Contributor

gr2m commented Mar 20, 2020

I think the .plugin() method now no longer accepts an array? Can we still handle that case and add a deprecation message?

@MaximDevoir
Copy link
Contributor Author

Yeah, so it currently silently supports an array - no error or deprecation message will be shown. This PR doesn't have any breaking changes.

I can add a deprecation notice and how to migrate to the new syntax in this PR if you would like.

@gr2m
Copy link
Contributor

gr2m commented Mar 21, 2020

Ah got it. Can you leave one test that keeps testing passing an array?

@@ -342,7 +342,7 @@ See [before-after-hook](https://github.com/gr2m/before-after-hook#readme) for

## Plugins

Octokit’s functionality can be extended using plugins. THe `Octokit.plugin()` method accepts a function or an array of functions and returns a new constructor.
Octokit’s functionality can be extended using plugins. The `Octokit.plugin()` method accepts a plugin (or many) and returns a new constructor.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should add a note specifically to TypeScript users. Something like this:

Octokit.plugin() also accepts a single array parameter but we recommend against it, as it breaks the TypeScript/Intellisense support

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The or many part is meant to mean plugins applied in the .plugin(p1, p2, ...) format - not an array. I've updated the syntax throughout the document to use the new, non-array, syntax here and here.

Additionally, there is now a warning logged to the console when the method is supplied with an array.

This should be enough so as to not address TypeScript users specifically - as all users (js and ts) will now be nudged into the correct syntax.

return { baz: "daz" };
}
]);
it("supports array of plugins and warns of deprecated usage", () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In reference to #53 (comment),

We have kept the test using an array of plugins. In this test, we're also checking for the deprecation notice too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry I forgot about it. Thanks for pointing it out

Copy link
Contributor

@gr2m gr2m left a comment

Choose a reason for hiding this comment

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

Looks great! There are some conflicts, probably because of the recent Prettier update. Can you update it? If not let me know, I can take care of the conflicts

@MaximDevoir MaximDevoir changed the title Fix type-widening bug fix: Fixes type-widening bug when handling plugins. Mar 24, 2020
@gr2m gr2m merged commit ef3c4ce into octokit:master Mar 24, 2020
@github-actions
Copy link
Contributor

🎉 This PR is included in version 2.4.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

@MaximDevoir MaximDevoir deleted the fix/type-widening branch March 25, 2020 07:16
gr2m added a commit to gr2m/javascript-plugin-architecture-with-typescript-definitions that referenced this pull request Jan 11, 2021
BREAKING CHANGE: `.plugin([plugin1, plugin2])` is now `.plugin(plugin1, plugin2)` (compare octokit/core.js#53, thanks @wolfy1339)
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.

No type information added to Octokit instance when using an array of plugins.
2 participants