Skip to content

Commit

Permalink
Update parser
Browse files Browse the repository at this point in the history
  • Loading branch information
simonseyock authored and brettz9 committed May 30, 2021
1 parent 15bc934 commit e9f6a0a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 31 deletions.
26 changes: 13 additions & 13 deletions src/rules/checkTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,32 @@ const adjustNames = (type, preferred, isGenericMatch, nodeName, node, parentNode
let ret = preferred;
if (isGenericMatch) {
if (preferred === '[]') {
parentNode.meta.brackets = '[]';
parentNode.meta.brackets = 'square';
parentNode.meta.dot = false;
ret = 'Array';
} else {
const dotBracketEnd = preferred.match(/\.(?:<>)?$/u);
if (dotBracketEnd) {
parentNode.meta.brackets = '<>';
parentNode.meta.brackets = 'angle';
parentNode.meta.dot = true;
ret = preferred.slice(0, -dotBracketEnd[0].length);
} else {
const bracketEnd = preferred.endsWith('<>');
if (bracketEnd) {
parentNode.meta.brackets = '<>';
parentNode.meta.brackets = 'angle';
parentNode.meta.dot = false;
ret = preferred.slice(0, -2);
} else if (
parentNode.meta.brackets === '[]' &&
parentNode.meta.brackets === 'square' &&
(nodeName === '[]' || nodeName === 'Array')
) {
parentNode.meta.brackets = '<>';
parentNode.meta.brackets = 'angle';
parentNode.meta.dot = false;
}
}
}
} else if (type === 'ANY') {
node.type = 'NAME';
} else if (type === 'JsdocTypeAny') {
node.type = 'JsdocTypeName';
}
node.value = ret.replace(/(?:\.|<>|\.<>|\[\])$/u, '');

Expand Down Expand Up @@ -79,12 +79,12 @@ export default iterateJsdoc(({
let isGenericMatch;
let typeName = nodeName;
if (Object.keys(preferredTypes).length) {
const isNameOfGeneric = parentNode !== undefined && parentNode.type === 'GENERIC' && property === 'left';
const isNameOfGeneric = parentNode !== undefined && parentNode.type === 'JsdocTypeGeneric' && property === 'left';
if (unifyParentAndChildTypeChecks || isNameOfGeneric) {
const brackets = parentNode?.meta?.brackets;
const dot = parentNode?.meta?.dot;

if (brackets === '<>') {
if (brackets === 'angle') {
const checkPostFixes = dot ? ['.', '.<>'] : ['<>'];
isGenericMatch = checkPostFixes.some(checkPostFix => {
if (preferredTypes?.[nodeName + checkPostFix] !== undefined) {
Expand All @@ -96,7 +96,7 @@ export default iterateJsdoc(({
}

if (!isGenericMatch && property) {
const checkPostFixes = dot ? ['.', '.<>'] : [brackets]
const checkPostFixes = dot ? ['.', '.<>'] : [brackets === 'angle' ? '<>' : '[]']

isGenericMatch = checkPostFixes.some(checkPostFix => {
if (preferredTypes?.[checkPostFix] !== undefined) {
Expand Down Expand Up @@ -132,10 +132,10 @@ export default iterateJsdoc(({

traverse(typeAst, (node, parentNode, property) => {
const {type, value} = node;
if (!['NAME', 'ANY'].includes(type)) {
if (!['JsdocTypeName', 'JsdocTypeAny'].includes(type)) {
return;
}
let nodeName = type === 'ANY' ? '*' : value;
let nodeName = type === 'JsdocTypeAny' ? '*' : value;

const [hasMatchingPreferredType, typeName, isGenericMatch] = getPreferredTypeInfo(type, nodeName, parentNode, property);

Expand Down Expand Up @@ -172,7 +172,7 @@ export default iterateJsdoc(({
!types.includes(nodeName);
})) {
invalidTypes.push([nodeName, types]);
} else if (!noDefaults && type === 'NAME') {
} else if (!noDefaults && type === 'JsdocTypeName') {
for (const strictNativeType of strictNativeTypes) {
if (strictNativeType === 'object' && mode === 'typescript') {
continue;
Expand Down
5 changes: 1 addition & 4 deletions src/rules/noUndefinedTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,7 @@ export default iterateJsdoc(({
}

traverse(parsedType, ({type, value}, parentNode, property) => {
if (type === 'NAME') {
if (parentNode !== undefined && parentNode.type === 'KEY_VALUE' && property === 'left') {
return
}
if (type === 'JsdocTypeName') {
const structuredTypes = structuredTags[tag.tag]?.type;
if (!allDefinedTypes.has(value) &&
(!Array.isArray(structuredTypes) || !structuredTypes.includes(value))
Expand Down
14 changes: 0 additions & 14 deletions test/rules/assertions/validTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,6 @@ export default {
},
},
},
{
code: `
/**
* @alias module:abc#event:foo-bar
*/
function quux() {
}
`,
errors: [{
line: 3,
message: 'Syntax error in namepath: module:abc#event:foo-bar',
}],
},
{
code: `
/**
Expand Down

0 comments on commit e9f6a0a

Please sign in to comment.