-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Make Javascript intellisense detect inheritance #13206
Comments
#12441 adds support for |
How is the state on this (apparently its not important as its getting moved from milestone to milestone) ? Kind regards |
Yes, actually this is the only one issue that stop us to use Salsa as default editor in Phaser Editor :( |
@mjbvz mentioned
JSDoc now has a way using
Typescript's jsdoc support page mentions it, but it's trivial
So vscode doesn't support the classic way of using the tags and displays this: Even if it's hard for the intellisense to detect the classic prototype chain from codes, isn't it easier to detect from JSDoc's |
Yes, it is very much easier, although detection of inheritance patterns isn't the main obstacle to this feature. The compiler doesn't treat constructor functions the same as classes internally. It has a special-case code path in checkNewExpression that lets constructor functions be newed. There is lot of work needed to convert constructor functions to be real classes. |
Okay, I see why it's being problematic. I hope there's an easier way to convert constructor functions to classes but it seems to be very dependent on implementation details. |
Blocked by #26883; changing to constructor functions to be real classes will primarily involve fixing (1) how they treat inheritance (2) making them generic, including |
Any updates on this? I'm using a library that has ES5 classes with constructor functions and would love for the intellisense to work when it extends other objects. I know this issue is blocked until #26883 is fixed, but neither of them have been updated for a while now. I also found that #18609 and #30943 reference the same issue. |
@pandawanfr 3.7 makes constructor functions into real classes (although type parameters still aren't correctly instantiated), so the main obstacle to this feature is now identifying and ranking Javascript inheritance patterns. This is a matter of corpus linguistics on code, which means that the first tool to verify your hypotheses is grep. If you're interested, you could pretty easily figure out which patterns are most common. (I don't have time to do it unless we decide to put this feature in our 3.8 or 3.9 plans.) |
This hack over prototype assignment works for me. Still isn't perfect for the constructor, but at least all other methods inherit JSDoc. import aqt from '@rqt/aqt'
import { WushInterface } from '../types/lux'
export default class Wush extends WushInterface {
constructor() {
super()
}
}
Object.defineProperties(Wush.prototype, Object.getOwnPropertyDescriptors(Wush.prototype = /** @type {!Wush} */ ({
async sendNotification(subscription, payload, options) {
const requestDetails = this.generateRequestDetails(subscription, payload, options)
const res = await aqt(requestDetails.endpoint)
const { statusCode, body, headers } = res
if (statusCode < 200 || statusCode > 299) {
throw new Error(
'Received unexpected response code',
)
}
return res
},
generateRequestDetails(subscription, payload, options) {
// implement me
},
}))) I'm doing this because I generate interfaces which are then provided an implementation with: Show interfaceimport { Readable } from 'stream'
/**
* An interface for sending web push notifications.
* @implements {_wush.Wush}
*/
export class WushInterface extends Readable {
/**
* Constructor method.
*/
constructor(...args) {
super(...args)
}
// hello() {
// return 'world'
// }
/**
* Sends a push notification to a subscription, optionally with a payload and options.
* @param {!_wush.PushSubscription} subscription The _PushSubscription_ record that will receive the notification.
* @param {(string|!Buffer)} payload The data to transmit to the user via the endpoint.
* @param {!Object} options Options for the GCM API key and VAPID keys can be passed here to override the default initialised options.
* @return {!Promise<{ body: string, status: number, headers: !Object }>}
*/
async sendNotification(subscription, payload, options) { }
/**
* Returns the details for a push message request, without sending the actual notification.
* @param {!_wush.PushSubscription} subscription The _PushSubscription_ record that will receive the notification.
* @param {(string|!Buffer)} payload The data to transmit to the user via the endpoint.
* @param {!Object} options Options for the GCM API key and VAPID keys can be passed here to override the default initialised options.
* @return {{ endpoint: string, method: string, headers: !Object, payload: (string|!Buffer) }}
*/
generateRequestDetails(subscription, payload, options) { }
}
/**
* @suppress {nonStandardJsDocs}
* @typedef {import('../').PushSubscription} _wush.PushSubscription
*/ The src/wush.js:23: WARNING - [JSC_TYPE_MISMATCH] inconsistent return type
found : null
required: (IThenable<{
body: string,
headers: Object,
status: number
}>|{
body: string,
headers: Object,
status: number
})
return null
^^^^ It's a bit of a hustle to define classes like that but if it's the only thing that works then there isn't any other solution right now. This workaround makes coding for interfaces much easy. |
JSDoc is not supported inside What can community members do to encourage ES5 support? Can we donate money towards particular issues? There has to be some way to get these things prioritized. |
Like everyone here, this issue has caused me a lot of grief. It would be nice to see it fixed one day. I'll share my workaround below for psuedo-classical inheritance. You could probably do something similar with prototypal inheritance. It's just a bit of code in the child object wrapped in a region, which is then removed from the code completely on build. /** The parent object.
* @constructor
* @param {*} arg - A parameter that has to be passed in on construction. */
function Parent(arg) {
/** Public function. In the documentation, this description will carry
* over to the child object. In the IDE, only typechecking will.
* @param {string} name - User's name.
* @returns {string} string **/
this.hello = function(name) {
return "Hello " + name;
}
}
/** The child object. 'augments' is what jsdocs uses to detect inheritance.
* 'augments' creates an IDE error, which the ts-ignore flag suppresses.
* @constructor
* @param {*} arg - A parameter that has to be passed in on construction.
* @augments Parent
* @ts-ignore */
function Child(arg) {
Parent.call(this, arg);
// Remove the IDE Hack region on build, otherwise it'll mess up your code.
//#region IDE Hack
var dummy = new Parent(null);
this.hello = dummy.hello;
//#endregion
// New code goes below . . .
} In the IDE, type checking and parameter descriptions carry over. The descriptions for the properties themselves do not carry over in the IDE, but they do in the documentation. |
Thanks @Obscerno for this pointer. This is a derived option which does not need require to edit the file during build, but will produce some useless JS code eventually (though the code is never executed):
Fortunately, TypeScript is broken enough so 1. the definitions are picked up inside the dead block and 2. one ts-ignore is enough even if you have multiple lines inside the block. (The latter is quite helpful since the silly ts-ignore has no options or block-treatment. Unbelievable ...) Edit/Update:
|
From @benjaminmillhouse on December 21, 2016 17:33
Hello,
I would love it if VS Code's Javascript intellisense was able to detect inheritance. Please let me know if there is a way to accomplish this today through jsdoc comments or anything like that. I have tried all kinds of different ways to get VS Code to detect it (definitions like below, @Augments, @extends jsdoc comments, etc) and have not gotten anything to work.
Would like the intellisense for new InheritedClass(). to show 'foo' and 'baz' as properties/methods for InheritedClass in Intellisense.
Copied from original issue: microsoft/vscode#17690
The text was updated successfully, but these errors were encountered: