Added null check to some adapters and changed type definition#150
Added null check to some adapters and changed type definition#150aminya merged 14 commits intoatom-community:masterfrom ayame113:master
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)
|
| 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.
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.
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.
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.
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.
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.