Ecmascript 6 class in Webstorm 10 - javascript

I use webstorm 10, and was trying ecmascript 6 with the following code:
/**
* Class Person
*/
class Person {
/**
* Constructor
* #param lastname
* #param firstname
* #param age
* #param sexe
*/
constructor(lastname, firstname, age, sexe) {
this.lastname = lastname;
this.firstname = firstname;
this.age = age;
this.sexe = sexe;
}
/**
* Return the name as string
* #returns {string}
*/
toString() {
return this.firstname + ' ' + this.lastname;
}
/**
* Return true if is an Adult
* #returns {boolean}
*/
isAdult() {
return this.age > 18;
}
/**
*
* #param {Person} person
* #returns {*}
*/
static isAdult(person) {
return person.isAdult();
}
}
What I am doing wrong that webstorm tell me that firstname and lastname in toString are unresolved variables, so age and as well isAdult() in the static method?

I bumped into the same problem. In order to fix it you have to supply the #class annotation. This way, it knows it's a class and treats properties as class properties, without warnings.
/**
* #class Person
*/
class Person {
...
You might also want to annotate the properties, like so
/**
* #class Person
* #property lastname {String}
* #property firstname {String}
* #property age {Number}
* #property sexe {String}
*/

Related

Problems in typing eslint-local-rules with jsdoc

I'm trying to type the eslint-local-rules.js file using jsdoc, but I can't figure out how to make #template with the default value for the RuleFunction. My goal is to remove the import('#typescript-eslint/utils/dist/ts-eslint') from the #type of the ArrowFunctionExpression class, but if I use the current typedef RuleFunction, its value will be
but I want to get as from the library
/**
* #typedef {import('#typescript-eslint/utils/dist/ts-estree').TSESTree} TSESTree
* FIXME: how to make the default template TSESTree.BaseNode?
* // # template {TSESTree.BaseNode=} T
* #typedef {import('#typescript-eslint/utils/dist/ts-eslint').RuleContext} RuleContext
* #typedef {import('#typescript-eslint/utils/dist/ts-eslint').RuleFunction} RuleFunction<T>
*/
module.exports = {
'end-api-functions-with-api': {
/**
* #param {Readonly<RuleContext<string, unknown[]>>} context
* #returns
*/
create(context) {
/**
* absolute file path
* #type {string}
*/
const filename = context.getFilename()
/**
* current working directory
* #type {string | undefined}
*/
const cwd = context.getCwd?.()
const relativeFilePath = filename.replace(cwd || '', '')
const fileInApiDirectory = /src\/.*api\//.test(relativeFilePath)
return {
/**
* #type {import('#typescript-eslint/utils/dist/ts-eslint').RuleFunction<import('#typescript-eslint/utils/dist/ts-estree').TSESTree.ArrowFunctionExpression>}
* #returns
*/
ArrowFunctionExpression(node) {

Function does not push to the empty array to create list. How to fix this

I am trying to add items to the hobbies list via function call. But it's not added and i have been getting empty array all the time. There is something silly I am missing. Can you some please guide me to resolve this. I am including all code I am working on.
I have tried calling the function like.
person().addHobby('TestHobby');
function person(firstName, lastName) {
var FullName = firstName + ' ' + lastName;
var hobbies = [‘’];
return {
/**
* Returns truthy if the person already has this hobby
* #param {string} hobby
* #returns {boolean}
*/
hasHobby: function(hobby) {
return hobbies.indexOf(hobby)>=0;
},
/**
* Returns the person's hobbies after adding the given hobby
* #param {string} hobby
* #returns {array}
*/
addHobby: function(hobby) {
return hobbies.indexOf(hobby)>0 ? hobbies : hobbies.push(hobby);
},
/**
* Returns the person's hobbies after removing the given hobby
* #param {string} hobby
* #returns {array}
*/
removeHobby: function(hobby) {
if (hobbies.indexOf(hobby)>0) {
// note: filter is a standard array function that creates a new array
// with all elements that pass the test implemented by the provided function
hobbies = hobbies.filter(function(hobby) {
return hobby != hobby;
});
return hobbies;
} else {
return hobbies;
}
},
/**
* Returns the person's hobbies
* #returns {array}
*/
getHobbies: function() {
return hobbies
},
/**
* Returns the person's full name
* #returns {string}
*/
getName: function() {
return FullName;
},
};
};
Maybe you aren't sure what you've actually created here.
This is a class definition for person. To call it's methods you need to create a new instance of person like:
var gregory=new person();
gregory is your reference to your freshly created person. To give gregory a hobby:
gregory.addHobby("sleeping");
Now you can query his hobbies like:
console.log(gregory.getHobbies());
You need to create your person and then use it. Like this:
var p = new person();
p.addHobby("test");
console.log(p.getHobbies());
function person(firstName, lastName) {
var FullName = firstName + ' ' + lastName;
var hobbies = [];
return {
/**
* Returns truthy if the person already has this hobby
* #param {string} hobby
* #returns {boolean}
*/
hasHobby: function(hobby) {
return hobbies.indexOf(hobby)>=0;
},
/**
* Returns the person's hobbies after adding the given hobby
* #param {string} hobby
* #returns {array}
*/
addHobby: function(hobby) {
return hobbies.indexOf(hobby)>0 ? hobbies : hobbies.push(hobby);
},
/**
* Returns the person's hobbies after removing the given hobby
* #param {string} hobby
* #returns {array}
*/
removeHobby: function(hobby) {
if (hobbies.indexOf(hobby)>0) {
// note: filter is a standard array function that creates a new array
// with all elements that pass the test implemented by the provided function
hobbies = hobbies.filter(function(hobby) {
return hobby != hobby;
});
return hobbies;
} else {
return hobbies;
}
},
/**
* Returns the person's hobbies
* #returns {array}
*/
getHobbies: function() {
return hobbies
},
/**
* Returns the person's full name
* #returns {string}
*/
getName: function() {
return FullName;
},
};
};
var p = new person();
p.addHobby("test");
console.log(p.getHobbies());
In the addHobbies function you're returning the result of push() wich is the new length of the array and not the actual array.
Try this:
addHobby: function(hobby) {
if (hobbies.indexOf(hobby) < 0) {
hobbies.push(hobby)
}
return hobbies;
}
Also you have to create a person if you want to get its hobbies array updated later:
var fred = person();
console.log(fred.addHobby("Test hobbie"));
console.log(fred.getHobbies())

PhpStorm: Argument type is not assignable to parameter type

I have come across what appears to be a bug in how PhpStorm interprets #typedef in JSDoc3 when used with #namespace. I receive the following warning..
Argument type item is not assignable to parameter type item
..when b(item); is called below:
(function() {
'use strict';
/**
* #namespace myNamespace
*/
/**
* #typedef {Object} item
* #property {string} key
* #property {string} value
*/
/**
* #function a
* #param {item} item
* #returns {item}
*/
function a(item) {
item.key = 'key';
item.value = 'value';
return item;
}
/**
* #function b
* #memberOf myNamespace
* #param {item} item
* #returns {item}
*/
function b(item) {
item.key = 'key';
item.value = 'value';
return item;
}
/** #type item */
var item = {
key: 'hello',
value: 'world'
};
a(item);
b(item);
})();
I only receive this warning on function b because of the #memberOf definition and when I remove it the warning disappears. Any ideas what would cause this?

How do I document symbols with a depth greater than 2 in JSDoc?

I have an object that contains multiple methods and is a member of a class. How would I document this with JSDoc?
Here's my attempt. With this SomeClass#helperFunctions is documented, but both of it's methods are omitted.
/**
* #class SomeClass
* #param name
*/
var SomeClass = function(name) {};
/**
* #member SomeClass#helperFunctions
*/
SomeClass.prototype.helperFunctions = {
/**
* #method SomeClass#helperFunctions.doSomething
* #param {Array} arr
*/
doSomething: function(arr) {}
};
/**
* #method SomeClass#helperFunctions.doSomethingElse
* #param {Array} arr
*/
SomeClass.protype.helperFunctions.doSomethingElse = function(arr) {};
This is the best I could come up with.
I document methods of SomeClass#helperFunctions as globals then include them as properties of SomeClass#helperFunctions using links.
/**
* #class SomeClass
* #param {String} name
*/
var SomeClass = function(name) {};
/**
* #member SomeClass#helperFunctions
* #property {Function} doSomething [_doSomething]{#link _doSomething}
* #property {Function} doSomethingElse [_doSomethingElse]{#link _doSomethingElse}
*/
SomeClass.prototype.helperFunctions = {
doSomething: _doSomething,
doSomethingElse: _doSomethingElse
};
/**
* #function _doSomething
* #param {Array} arr
*/
_doSomething = function(arr) {};
/**
* #function _doSomethingElse
* #param {Array} arr
*/
_doSomethingElse = function(arr) {};
In my actual application SomeClass was also a module so it was written as:
/**
* #module path/to/SomeClass
*/
/**
* #class module:path/to/SomeClass
*/
var SomeClass = module.exports = function() {};
Then the links were written as {#link module:path/to/SomeClass~_doSomething} so it would link to it's spot on the module page instead of looking for them in Globals.

YUIDoc/javascript - how to document a module property

I've copied an example from here. Below is the example code, but the problem is that Store.TAX_RATE shows up in the documentation as a property of Item and not as a property of the module Store. Any suggestions why ?
Example code:
/**
* This module contains classes for running a store.
* #module Store
*/
var Store = Store || {};
/**
* `TAX_RATE` is stored as a percentage. Value is 13.
* #property TAX_RATE
* #static
* #final
* #type Number
*/
Store.TAX_RATE = 13;
/**
* #class Item
* #constructor
* #param name {String} Item name
* #param price {Number} Item price
* #param quantity {Number} Item quantity (the number available to buy)
*/
Store.Item = function (name, price, quantity) {
/**
* #property name
* #type String
*/
this.name = name;
/**
* #property price
* #type String
*/
this.price = price * 100;
/**
* #property quantity
* #type Number
*/
this.quantity = quantity;
/**
* #property id
* #type Number
*/
this.id = Store.Item._id++;
Store.Item.list[this.id] = this;
};
That's because according to YUIDoc terminology a module is just a collection of related classes, so it can't contain anything but classes.
What you could do instead is to document Store and Store.Item both as classes:
/**
* This module contains classes for running a store.
* #class Store
*/
var Store = Store || {};
/**
* `TAX_RATE` is stored as a percentage. Value is 13.
* #property TAX_RATE
* #type Number
*/
Store.TAX_RATE = 13;
/**
* #class Store.Item
*/
Store.Item = function (name, price, quantity) {
};

Categories

Resources