-
Notifications
You must be signed in to change notification settings - Fork 13
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
Added null check to some adapters and changed type definition #150
Conversation
I think we were discussing this in #132 (comment) I'm not sure if it is better to throw an error if the lsp returns something invalid or fail silently like this PR. Is null or undefined a valid return for |
This is different though. |
How? Why are these checks necessary if If |
I meant the changes under |
Ya I think those changes are good. I'm just not sure about null checks. |
(some comment)
|
lib/adapters/code-action-adapter.ts
Outdated
const actions = await connection.codeAction(params) | ||
return actions.map((action) => CodeActionAdapter.createCodeAction(action, connection)) | ||
return (actions || []).map((action) => CodeActionAdapter.createCodeAction(action, connection)) |
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.
According to codeAction
it returns Promise<Array<lsp.Command | lsp.CodeAction>>
so a promise resolving to null
or undefined
is an invalid return type and should probably throw an error (as it currently does) instead of being handled the same as an empty array. Or the return type should be updated to include null
and undefined
as valid returns.
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 tend to prefer throwing a valid error instead of failing silently like this. @aminya This is what I was talking about in #132 (comment) when I said "types do not catch runtime errors".
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.
Thank you for your review!
The spec seems to say that codeAction returns (Command | CodeAction) [] | null
. Is it not necessary to raise an error if the language server returns valid null?
- method: ‘textDocument/codeAction’
Response:
- result: (Command | CodeAction)[] | null
By the way, such a change is also an option.
public static async getCodeActions(
...
-): Promise<atomIde.CodeAction[]> {
+): Promise<atomIde.CodeAction[] | null> {
if (linterAdapter == null) {
return []
}
assert(serverCapabilities.codeActionProvider, "Must have the textDocument/codeAction capability")
const params = CodeActionAdapter.createCodeActionParams(linterAdapter, editor, range, diagnostics)
const actions = await connection.codeAction(params)
- return (actions || []).map((action) => CodeActionAdapter.createCodeAction(action, connection))
+ if (!actions) {return null}
+ return actions.map((action) => CodeActionAdapter.createCodeAction(action, connection))
}
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 like the early null check rather than still mapping over an empty array.
We should change the return type of codeAction if null is a valid response according to the spec.
NVM I see you already did that. 👍
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.
At e7c26b4, I changed it to return null instead of an empty array when actions
value is null.
I'll review today or tomorrow |
🎉 This PR is included in version 1.8.3 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Some LSP requests lack null checking and are causing errors, so I fixed them.
I made the following changes:
If you don't need the third change, reverting it will still work.