VSCode IntelliSense does not autocomplete JavaScript DOM events and methods - visual-studio-code

I am using Visual Studio Code version 1.17.1.
In *.js file when I type document.querySelector("#elementId").style. I have no IntelliSense hints for styles (like margin, display, etc.).
Even no onclick event hint after document.querySelector("#elementId").
I don't use any npm packages. It is just simple html\css\js project.
How to turn on correct IntelliSense hints? Thanks.

Because result of the querySelector is either:
Element - the most general base class or null
If you already know id you can use document.getElementById() - which returns instance of more specific class - HTMLElement - autocomplete will work as expected.
document.getElementById('elementId').
If you don't know id, but want autocomplete you can use JSDoc type annotations:
/** #type {HTMLElement} */
var el = document.querySelector(".myclass");
el.
// or without extra variable:
/** #type {HTMLElement} */
(document.querySelector(".myclass")).
I haven't really tested it but you can try something like that:
/**
* #type {function(string): HTMLElement}
*/
var querySelector = document.querySelector.bind(document);
querySelector('.myclass').
Another choice would be alter typescript types:
Create file dom.d.ts
Append to it:
interface NodeSelector {
querySelector<K extends keyof ElementTagNameMap>(selectors: K): ElementTagNameMap[K] | null;
querySelector<E extends HTMLElement = HTMLElement>(selectors: string): E | null;
querySelectorAll<K extends keyof ElementListTagNameMap>(selectors: K): ElementListTagNameMap[K];
querySelectorAll<E extends HTMLElement = HTMLElement>(selectors: string): NodeListOf<E>;
}
Now querySelector returns HTMLElement.

The other answer points to the correct answer – type casting with jsdoc – but I've found that this only works consistently when you do it exactly as typescript wants when dealing with union types: you need to wrap the casted expression in parentheses and place the type cast doc on the same line. The snippet from a permalink to the wiki:
// We can "cast" types to other types using a JSDoc type assertion
// by adding an `#type` tag around any parenthesized expression.
/**
* #type {number | string}
*/
var numberOrString = Math.random() < 0.5 ? "hello" : 100;
var typeAssertedNumber = /** #type {number} */ (numberOrString)

Related

Correct JSDoc signature for is_string function?

I have the following function of type (obj:any)=>boolean that determines whether an object is string or not.
I want the JsDoc to be "smart" so that whenever I use if(isString(x)){ ...block... }, the highlighter treats x inside the block as a string.
So far I tried this without success:
/** #type {((obj:string)=>true)|((obj:any)=>false)} */
function isString(obj) {
return Object.prototype.toString.call(obj) === "[object String]";
}
How can I do it properly?
The following should do the trick. It uses the Typescript's narrowing with type predicates.
/** #type {(obj: any) => obj is String} */
function isString(obj) {
return Object.prototype.toString.call(obj) === "[object String]";
}

Doxygen missed entries (depending on order)

Want to generate html-documenation from a documented c-header. But I have a strange problem with doxygen:
Some entries (enums, structs, ..) are missed in the html. If I reorder one of the missed entries (put them among two others that are already displayed, than it will shown too ?!?
Is there a rule for the order of entries? If so, can I disable this rule?
Use 1.8.11 in Linux and latest (1.8.14) in Windows.
As for example I have a few structs a,b,c,d , and struct d struct is part of c.
In "C" I need to write d before c, otherwise I get compiler error. But doxygen - for some strange reason lists c only if it is located before d. So either I can compile or have a complete documentation.
I created a small example and with this example I do see all elements:
/** \file */
/** docu structure a */
struct a
{
/** docu member a */
int mem_a;
};
/** docu structure b */
struct b
{
/** docu member b */
int mem_b;
};
/** docu structure d */
struct d
{
/** docu member d */
int mem_d;
};
/** docu structure c */
struct c
{
/** docu member c */
int mem_c;
/** docu structure inside c */
struct d str_d;
};
I have used a default Doxyfile (doxygen -g).
The header file was quite large - to large to post it here - so I tried to reduce it and found the issue. The struct that will not displayed has a #code blabla statement in its doxygen header but it needs an #endcode (that was missed). Sorry for this false alarm. Thanks to albert, for this help. Asking the right questions helps too!

Writing #value in javadoc brackets remove - Eclipse

I would like to write javadoc in Eclipse.
I have this piece of code:
private int variable;
When I want to add #value parametar in Eclipse above line I get {#value} instead of #value. When I want to add #author, I get output without {} brackets, as I want. How can I get #value without brackets?
Since the #value tag appears in running text, it should be in braces. It is not a tag like #author, which occurs at the end and basically considers the text up to the next (braceless) tag as its parameters.
So Eclipse's suggestion is correct. Why do you want to use that tag without braces?
The Javadoc tool will replace #value tags with the value of the constant it documents (if no parameter is given) or with the value of the constant mentioned in the parameter. For example:
/** The value {#value}. */
public static int aConstant = 1;
/**
* Does something.
* #param aValue should be {#value #aConstant}.
* #return 42
*/
public int aMethod(int aValue) {
...
}

How to declare unlimited/variadic parameters in DocBlock?

Lets say I have a function (obviously a trivial example):
public function dot(){
return implode('.', func_get_args());
}
Now I know I could modify this to be
public function dot(array $items){
return implode('.', $array);
}
but with some functions that is not an option. So, how would you document the first version of the function with a docBlock so an IDE can interpret that it can receive unlimited parameters?
I have seen some methods that use:
/**
* Joins one or more strings together with a . (dot)
* #param string $string1
* #param string $string2
* #param string $_ [optional]
* #return string
*/
public function dot($string1, $string2, $_ = null) {
return implode('.', func_get_args());
}
Which in an IDE looks like
But that feels like a hack to me, is there no way to do it just with docBlock?
[UPDATED 2015-01-08]
Old way to do this in PHPDoc was:
http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_tags.param.pkg.html
/**
* #param int $param,...
**/
However, this is no longer supported. As of PHP 5.6 Variadic Method Parameters are a part of the PHP language, and the PHPDoc's have been updated to reflect this as of PHPDoc 2.4 if I recall correctly. This is also in the PhpStorm IDE as of EAP 139.659 (should be in 8.0.2 and up). Not sure about implementation of other IDEs.
https://youtrack.jetbrains.com/issue/WI-20157
In any case, proper syntax for DocBlocks going forward for variadic parameters is:
/**
* #param int ...$param
**/
As Variadics are implemented in PHP 5.6 PHPDocumentor should support the following syntax as of version 2.4.
/**
* #param Type ...$value
* Note: PHP 5.6+ syntax equal to func_get_args()
*/
public function abc(Type ...$value) {}
This should be the correct way to describe such a signature. This will likely be included in PSR-5. Once that is accepted IDE's should follow to support this "official" recommendation.
However, in the mean time some IDE's have an improved understanding of what they consider correct. Hit hard on the IDE vendor to support the offical PHP syntax that is supported as of 5.6 or use whatever works in the meantime.
In php the concept of valist or list of "optional arguments" does not exist.
the $_ variable will just contain, here the third string you give.
The only way to allow an array OR a string is to test the first argument with is_array()
public function dot($arg1){
if(is_array($arg1)){
return implode('.',$arg1);
}
else if $arg1 instanceof \Traversable){
return implode('.',iterator_to_array($arg1));
}
else{
return implode('.',func_get_args());
}
}
Now that you handled the behaviour you want, you have to document it. In php, as overloading is not allowed, a convention is to use "mixed" as a type if you want to provide multiple types.
/**
*#param mixed $arg1 an array, iterator that will be joined OR first string of the list
*#return string a string with all strings of the list joined with a point
*#example dot("1","2","3"); returns 1.2.3 dot(array(1,2,3)); returns 1.2.3
*/
Moreover, according to phpdocumentor documentation you can declare sort of valist with
/**
*#param string ... list of strings
*/

Make Doxygen document a struct/class defined inside a macro call

I have this PACKED macro, that receives a struct definition and returns it with a compiler annotation to make it packed.
For example:
/**
* ...
*/
PACKED(struct A {
/**
* ...
*/
int x;
});
I have tried several Doxygen options to include that documentation, but I've had no success so far. Closest I've come up with is this:
ENABLE_PREPROCESSING = YES
PREDEFINED = PACKED(type)=type
MACRO_EXPANSION = YES
But that messes up the struct and members' documentation (confirmed via doxygen -d Preprocessor).
Ideas?
Turns out it's a bug in Doxygen.
One possible workaround is to use #class, and so on.