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

minor jsdoc improvements #1910

Closed
wants to merge 2 commits into from

Conversation

jimmywarting
Copy link

I use this settings on by default in VS code

    "javascript.suggest.completeFunctionCalls": true,
    "javascript.suggest.completeJSDocs": true,
    "javascript.suggest.autoImports": true,
    "js/ts.implicitProjectConfig.checkJs": true,

It was showing red lines a bit here and there. With this changes it could figure out more what things where with autocompletion

Could improve some bits even further if i may use private fields, But it requires node v12 and you are testing v8...
could we perhaps drop some older versions and align a bit more with current targeted support versions?

@@ -586,7 +584,7 @@ module.exports = Receiver;
/**
* Builds an error object.
*
* @param {(Error|RangeError)} ErrorCtor The error constructor
* @param {(typeof Error|typeof RangeError)} ErrorCtor The error constructor
Copy link
Member

Choose a reason for hiding this comment

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

Why typeof? It should be the Error or RangeError constructor.

Copy link
Author

Choose a reason for hiding this comment

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

from VS code (TypeScript) perspective it looks at @param {Error} to be a instance of a Error class, when using typeof then it knows that it can be a Class

@@ -9,14 +9,17 @@ const { mask: applyMask, toBuffer } = require('./buffer-util');

const mask = Buffer.alloc(4);


/** @typedef {import('net').Socket} Socket */
Copy link
Member

Choose a reason for hiding this comment

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

This could also be a tls.Socket. Also, no TypeScript-specific syntax please.

Copy link
Author

Choose a reason for hiding this comment

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

i tried tls.Socket, didn't work so well... it could not figure out the correct NodeJS namespace
also typedef is a jsdoc thing

I don't like TypeScript syntax either, very much prefer checkJs + jsDoc combo

if you are running VS-code you can try adding a comment on top and see if you get any benefit from using
// @ts-check at the top of the file

https://www.dandoescode.com/blog/using-typescript-without-typescript/

Copy link
Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

From the comments:

This is TypeScript-specific syntax. Vote for github.com/jsdoc/jsdoc/issues/1645.

Copy link
Author

Choose a reason for hiding this comment

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

oh, look at that...
anyway another way could be to import a unused variable

var http = require('http')

* @param {http.Server}
function foo() {
}

Best if you have treeshaking support for this case

Copy link
Member

Choose a reason for hiding this comment

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

That's acceptable. However we might need to add an eslint comment for unused variables.

Copy link
Member

Choose a reason for hiding this comment

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

How should we proceed here? Using {net.Socket} seems more popular than {import('net').Socket} even if it doesn't work perfectly (when net is not required).

I would prefer to just improve the existing type with tls.Socket:

@param {(net.Socket|tls.Socket)} socket The connection socket

@@ -9,13 +9,14 @@ const WebSocket = require('./websocket');
const { format, parse } = require('./extension');
const { GUID, kWebSocket } = require('./constants');


/** @typedef {import('http').Server} Server */
Copy link
Member

Choose a reason for hiding this comment

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

Ditto.

Comment on lines +395 to +399
WebSocket.CONNECTING = 0
WebSocket.OPEN = 1
WebSocket.CLOSING = 2
WebSocket.CLOSED = 3

Copy link
Member

Choose a reason for hiding this comment

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

Why this change? It is already done by Object.defineProperty(WebSocket, readyState, descriptor); below.

Copy link
Author

Choose a reason for hiding this comment

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

VS inteligens could not figure out what dynamically assigned properties meant

Copy link
Member

Choose a reason for hiding this comment

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

I prefer to not have changes only required for TypeScript / Visual Studio Code IntelliSense.

Copy link
Author

Choose a reason for hiding this comment

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

Do you have any other jsDoc solution up your sleeve that describes WebSocket to have this properties?

Copy link
Member

@lpinca lpinca Jul 5, 2021

Choose a reason for hiding this comment

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

It's weird but something like this should work:

/**
 * Class representing a WebSocket.
 *
 * @property {Number} CONNECTING description
 */

Copy link
Author

Choose a reason for hiding this comment

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

forgot about that thing

readyStates.forEach((readyState, i) => {
const descriptor = { enumerable: true, value: i };
const descriptor = { enumerable: true, value: i, configurable: false, writable: false };
Copy link
Member

Choose a reason for hiding this comment

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

Why this change? false is the default value for configurable and writable.

Copy link
Author

Choose a reason for hiding this comment

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

when i assigned the WebSocket.CLOSING = 2 it probably changed the configurable to true, so i had to override it again - test was failing if i didn't do this

Copy link
Member

Choose a reason for hiding this comment

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

What test? Behavior is tested here

ws/test/websocket.test.js

Lines 102 to 111 in 0ad1f9d

it('is enumerable property of class', () => {
const descriptor = Object.getOwnPropertyDescriptor(WebSocket, state);
assert.deepStrictEqual(descriptor, {
configurable: false,
enumerable: true,
value: readyStates[state],
writable: false
});
});
.

This comment was marked as spam.

Copy link
Member

Choose a reason for hiding this comment

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

Ah I understand now, this happens due to #1910 (comment). I guess that is another reason to avoid that change.

@@ -586,7 +584,7 @@ module.exports = Receiver;
/**
* Builds an error object.
*
* @param {(Error|RangeError)} ErrorCtor The error constructor
* @param {(typeof Error|typeof RangeError)} ErrorCtor The error constructor
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* @param {(typeof Error|typeof RangeError)} ErrorCtor The error constructor
* @param {function(new:Error|RangeError)} ErrorCtor The error constructor

Copy link
Author

Choose a reason for hiding this comment

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

👍

lpinca pushed a commit that referenced this pull request Jul 6, 2021
@lpinca
Copy link
Member

lpinca commented Jul 6, 2021

@jimmywarting are you ok with #1912?

lpinca pushed a commit that referenced this pull request Jul 6, 2021
lpinca pushed a commit that referenced this pull request Jul 6, 2021
@jimmywarting
Copy link
Author

closing mine b/c it's superseeded and i'm overwhelmed with other work right now

lpinca pushed a commit that referenced this pull request Jul 7, 2021
lpinca pushed a commit that referenced this pull request Jul 7, 2021
lpinca added a commit that referenced this pull request Jul 7, 2021
th37rose added a commit to th37rose/websocket_server that referenced this pull request May 24, 2022
@jimmywarting jimmywarting deleted the improve-jsdoc branch December 1, 2022 19:00
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.

None yet

2 participants