Skip to content
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

Merged
merged 14 commits into from
May 1, 2021
Merged

Added null check to some adapters and changed type definition #150

merged 14 commits into from
May 1, 2021

Conversation

ayame113
Copy link
Contributor

@ayame113 ayame113 commented Apr 24, 2021

Some LSP requests lack null checking and are causing errors, so I fixed them.

image

I made the following changes:

  1. Added null check (e20bba5)
  2. Fixed the type definition (85cc505)
  3. Use the vscode-languageserver-protocol package for type definition (https://github.com/atom-community/atom-languageclient/compare/85cc505..d665159)

If you don't need the third change, reverting it will still work.

@ayame113 ayame113 marked this pull request as draft April 24, 2021 12:32
@ayame113 ayame113 marked this pull request as ready for review April 24, 2021 13:54
@aminya aminya self-assigned this Apr 24, 2021
@aminya aminya added the bug Something isn't working label Apr 24, 2021
@UziTech
Copy link
Member

UziTech commented Apr 25, 2021

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 .codeAction?

@aminya
Copy link
Member

aminya commented Apr 25, 2021

I think we were discussing this in #132 (comment)

This is different though.

@UziTech
Copy link
Member

UziTech commented Apr 25, 2021

This is different though.

How? Why are these checks necessary if null or undefined are not correct outputs of .codeAction anyway?

If null or undefined are valid outputs then we should change the types to reflect that.

@aminya
Copy link
Member

aminya commented Apr 25, 2021

I meant the changes under lib/languageclient.ts which seem reasonable as they are using the original types.

@UziTech
Copy link
Member

UziTech commented Apr 25, 2021

I meant the changes under lib/languageclient.ts which seem reasonable as they are using the original types.

Ya I think those changes are good. I'm just not sure about null checks.

@ayame113
Copy link
Contributor Author

(some comment)

  • Currently, the vscode-languageserver-protocol module is used for type definition. In this PR, I also want to use it in the return value of the language-client method.
  • Since the return type is not exported directly from the module, I am using the generics of the function.
  • This will cause a type error if the wrong return type.
    image
  • Removed null from the return value of the following request according to the type definition
    • completionItem / resolve
    • codeLens / resolve
  • Added null
    • textDocument/references
    • textDocument/documentHighlight
    • textDocument/documentSymbol
    • workspace/symbol
    • textDocument/codeAction
    • textDocument/codeLens
    • textDocument/documentLink
    • textDocument/formatting
    • textDocument/rangeFormatting
    • textDocument/onTypeFormatting
    • textDocument/rename
  • I think this implementation fits the spec, but you can leave null in the return value if you want to deal with invalid null checks outside the spec.

Comment on lines 49 to 50
const actions = await connection.codeAction(params)
return actions.map((action) => CodeActionAdapter.createCodeAction(action, connection))
return (actions || []).map((action) => CodeActionAdapter.createCodeAction(action, connection))
Copy link
Member

@UziTech UziTech Apr 25, 2021

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.

Copy link
Member

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".

Copy link
Contributor Author

@ayame113 ayame113 Apr 26, 2021

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?

https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_codeAction
Request:

  • 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))
 }

Copy link
Member

@UziTech UziTech Apr 26, 2021

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. 👍

Copy link
Contributor Author

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.

@UziTech UziTech requested a review from aminya April 26, 2021 14:35
@aminya
Copy link
Member

aminya commented Apr 26, 2021

I'll review today or tomorrow

@aminya aminya merged commit 5c30200 into atom-community:master May 1, 2021
@github-actions
Copy link

github-actions bot commented May 1, 2021

🎉 This PR is included in version 1.8.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants