Skip to content

no-undefined-types: Treat GCC generic types as defined #283

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

Merged
merged 7 commits into from
Jun 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2784,6 +2784,35 @@ function quux(foo, bar, baz) {
// Settings: {"jsdoc":{"preferredTypes":{"hertype":{"replacement":false},"histype":"HisType"}}}
// Options: [{"definedTypes":["MyType"],"preferredTypesDefined":true}]
// Message: The type 'HerType' is undefined.

class Foo {
/**
* @return {TEMPLATE_TYPE}
*/
bar () {
}
}
// Message: The type 'TEMPLATE_TYPE' is undefined.

class Foo {
/**
* @return {TEMPLATE_TYPE}
*/
invalidTemplateReference () {
}
}

/**
* @template TEMPLATE_TYPE
*/
class Bar {
/**
* @return {TEMPLATE_TYPE}
*/
validTemplateReference () {
}
}
// Message: The type 'TEMPLATE_TYPE' is undefined.
````

The following patterns are not considered problems:
Expand Down Expand Up @@ -2934,6 +2963,29 @@ function quux(foo, bar, baz) {
}
// Settings: {"jsdoc":{"preferredTypes":{"hertype":{"replacement":"HerType<>"},"histype":"HisType.<>"}}}
// Options: [{"definedTypes":["MyType"],"preferredTypesDefined":true}]

/**
* @template TEMPLATE_TYPE
*/
class Foo {
/**
* @return {TEMPLATE_TYPE}
*/
bar () {
}
}

/**
* @template TEMPLATE_TYPE_A, TEMPLATE_TYPE_B
*/
class Foo {
/**
* @param {TEMPLATE_TYPE_A} baz
* @return {TEMPLATE_TYPE_B}
*/
bar (baz) {
}
}
````


Expand Down
15 changes: 9 additions & 6 deletions src/iterateJsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,20 +189,23 @@ const curryUtils = (
return false;
};

utils.classHasTag = (tagName) => {
utils.getClassJsdoc = () => {
const classNode = utils.getClassNode();
const classJsdocNode = getJSDocComment(sourceCode, classNode);

if (classJsdocNode) {
const indent = _.repeat(' ', classJsdocNode.loc.start.column);
const classJsdoc = parseComment(classJsdocNode, indent);

if (jsdocUtils.hasTag(classJsdoc, tagName)) {
return true;
}
return parseComment(classJsdocNode, indent);
}

return false;
return null;
};

utils.classHasTag = (tagName) => {
const classJsdoc = utils.getClassJsdoc();

return classJsdoc && jsdocUtils.hasTag(classJsdoc, tagName);
};

utils.forEachTag = (tagName, arrayHandler) => {
Expand Down
19 changes: 18 additions & 1 deletion src/jsdocUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,22 @@ const isInlineTag = (tag) => {
};
*/

/**
* Parses GCC Generic/Template types
*
* @see {https://github.com/google/closure-compiler/wiki/Generic-Types}
* @param {JsDocTag} tag
* @returns {Array<string>}
*/
const parseClosureTemplateTag = (tag) => {
return tag.source
.split('@template')[1]
.split(',')
.map((type) => {
return type.trim();
});
};

export default {
getFunctionParameterNames,
getJsdocParameterNames,
Expand All @@ -474,5 +490,6 @@ export default {
isNamepathTag,
isPotentiallyEmptyNamepathTag,
isTagWithType,
isValidTag
isValidTag,
parseClosureTemplateTag
};
16 changes: 15 additions & 1 deletion src/rules/noUndefinedTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'flat-map-polyfill';
import _ from 'lodash';
import {parse as parseType, traverse} from 'jsdoctypeparser';
import iterateJsdoc, {parseComment} from '../iterateJsdoc';
import jsdocUtils from '../jsdocUtils';

const extraTypes = [
'null', 'undefined', 'string', 'boolean', 'object',
Expand Down Expand Up @@ -67,6 +68,18 @@ export default iterateJsdoc(({
})
.value();

let closureGenericTypes = [];
const classJsdoc = utils.getClassJsdoc();
if (classJsdoc && classJsdoc.tags) {
closureGenericTypes = classJsdoc.tags
.filter((tag) => {
return tag.tag === 'template';
})
.flatMap((tag) => {
return jsdocUtils.parseClosureTemplateTag(tag);
});
}

const allDefinedTypes = globalScope.variables.map((variable) => {
return variable.name;
})
Expand All @@ -89,7 +102,8 @@ export default iterateJsdoc(({
.concat(extraTypes)
.concat(typedefDeclarations)
.concat(definedTypes)
.concat(definedPreferredTypes);
.concat(definedPreferredTypes)
.concat(closureGenericTypes);

const jsdocTags = utils.filterTags((tag) => {
return utils.isTagWithType(tag.tag);
Expand Down
74 changes: 74 additions & 0 deletions test/rules/assertions/noUndefinedTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,51 @@ export default {
}
}
}
},
{
code: `
class Foo {
/**
* @return {TEMPLATE_TYPE}
*/
bar () {
}
}
`,
errors: [
{
line: 4,
message: 'The type \'TEMPLATE_TYPE\' is undefined.'
}
]
},
{
code: `
class Foo {
/**
* @return {TEMPLATE_TYPE}
*/
invalidTemplateReference () {
}
}

/**
* @template TEMPLATE_TYPE
*/
class Bar {
/**
* @return {TEMPLATE_TYPE}
*/
validTemplateReference () {
}
}
`,
errors: [
{
line: 4,
message: 'The type \'TEMPLATE_TYPE\' is undefined.'
}
]
}
],
valid: [
Expand Down Expand Up @@ -393,6 +438,35 @@ export default {
}
}
}
},
{
code: `
/**
* @template TEMPLATE_TYPE
*/
class Foo {
/**
* @return {TEMPLATE_TYPE}
*/
bar () {
}
}
`
},
{
code: `
/**
* @template TEMPLATE_TYPE_A, TEMPLATE_TYPE_B
*/
class Foo {
/**
* @param {TEMPLATE_TYPE_A} baz
* @return {TEMPLATE_TYPE_B}
*/
bar (baz) {
}
}
`
}
]
};