Skip to content

Commit

Permalink
chore: update eslint-config-canonical and update linting accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Nov 17, 2019
1 parent e058efa commit 8ca5030
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 116 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"babel-plugin-istanbul": "^5.2.0",
"chai": "^4.2.0",
"eslint": "6.6.0",
"eslint-config-canonical": "^17.8.0",
"eslint-config-canonical": "^18.1.0",
"gitdown": "^3.1.2",
"glob": "^7.1.6",
"husky": "^3.0.9",
Expand Down
20 changes: 10 additions & 10 deletions src/iterateJsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import _ from 'lodash';
import jsdocUtils from './jsdocUtils';
import getJSDocComment from './eslint/getJSDocComment';

const skipSeeLink = (parser) => {
return (str, data) => {
if (data.tag === 'see' && str.match(/\{@link.+?\}/u)) {
return null;
}

return parser(str, data);
};
};

/**
*
* @param {object} commentNode
Expand All @@ -12,16 +22,6 @@ import getJSDocComment from './eslint/getJSDocComment';
* @returns {object}
*/
const parseComment = (commentNode, indent, trim = true) => {
const skipSeeLink = (parser) => {
return (str, data) => {
if (data.tag === 'see' && str.match(/\{@link.+?\}/u)) {
return null;
}

return parser(str, data);
};
};

// Preserve JSDoc block start/end indentation.
return commentParser(`${indent}/*${commentNode.value}${indent}*/`, {
// @see https://github.com/yavorskiy/comment-parser/issues/21
Expand Down
150 changes: 75 additions & 75 deletions src/rules/checkTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,36 @@ const strictNativeTypes = [
'RegExp',
];

const adjustNames = (type, preferred, isGenericMatch, nodeName, node, parentNode) => {
let ret = preferred;
if (isGenericMatch) {
if (preferred === '[]') {
parentNode.meta.syntax = 'SQUARE_BRACKET';
ret = 'Array';
} else {
const dotBracketEnd = preferred.match(/\.(?:<>)?$/u);
if (dotBracketEnd) {
parentNode.meta.syntax = 'ANGLE_BRACKET_WITH_DOT';
ret = preferred.slice(0, -dotBracketEnd[0].length);
} else {
const bracketEnd = preferred.endsWith('<>');
if (bracketEnd) {
parentNode.meta.syntax = 'ANGLE_BRACKET';
ret = preferred.slice(0, -2);
}
}
}
} else if (type === 'ANY') {
node.type = 'NAME';
}
node.name = ret.replace(/(?:\.|<>|\.<>|\[\])$/u, '');

// For bare pseudo-types like `<>`
if (!ret) {
node.name = nodeName;
}
};

export default iterateJsdoc(({
jsdocNode,
sourceCode,
Expand All @@ -34,98 +64,68 @@ export default iterateJsdoc(({
const noDefaults = _.get(optionObj, 'noDefaults');
const unifyParentAndChildTypeChecks = _.get(optionObj, 'unifyParentAndChildTypeChecks');

jsdocTagsWithPossibleType.forEach((jsdocTag) => {
const invalidTypes = [];
let typeAst;

try {
typeAst = parse(jsdocTag.type);
} catch (error) {
return;
}

const getPreferredTypeInfo = (type, nodeName, parentName, parentNode) => {
let hasMatchingPreferredType;
let isGenericMatch;
let typeName = nodeName;
if (Object.keys(preferredTypes).length) {
const parentType = parentName === 'subject';
if (unifyParentAndChildTypeChecks || parentType) {
const syntax = _.get(parentNode, 'meta.syntax');
const getPreferredTypeInfo = (type, nodeName, parentName, parentNode) => {
let hasMatchingPreferredType;
let isGenericMatch;
let typeName = nodeName;
if (Object.keys(preferredTypes).length) {
const parentType = parentName === 'subject';
if (unifyParentAndChildTypeChecks || parentType) {
const syntax = _.get(parentNode, 'meta.syntax');

[
['.', 'ANGLE_BRACKET_WITH_DOT'],
['.<>', 'ANGLE_BRACKET_WITH_DOT'],
['<>', 'ANGLE_BRACKET'],
].some(([checkPostFix, syn]) => {
isGenericMatch = _.get(
preferredTypes,
nodeName + checkPostFix,
) !== undefined &&
syntax === syn;
if (isGenericMatch) {
typeName += checkPostFix;
}

return isGenericMatch;
});
if (!isGenericMatch && parentType) {
[
['[]', 'SQUARE_BRACKET'],
['.', 'ANGLE_BRACKET_WITH_DOT'],
['.<>', 'ANGLE_BRACKET_WITH_DOT'],
['<>', 'ANGLE_BRACKET'],
].some(([checkPostFix, syn]) => {
isGenericMatch = _.get(
preferredTypes,
nodeName + checkPostFix,
) !== undefined &&
isGenericMatch = _.get(preferredTypes, checkPostFix) !== undefined &&
syntax === syn;
if (isGenericMatch) {
typeName += checkPostFix;
typeName = checkPostFix;
}

return isGenericMatch;
});
if (!isGenericMatch && parentType) {
[
['[]', 'SQUARE_BRACKET'],
['.', 'ANGLE_BRACKET_WITH_DOT'],
['.<>', 'ANGLE_BRACKET_WITH_DOT'],
['<>', 'ANGLE_BRACKET'],
].some(([checkPostFix, syn]) => {
isGenericMatch = _.get(preferredTypes, checkPostFix) !== undefined &&
syntax === syn;
if (isGenericMatch) {
typeName = checkPostFix;
}

return isGenericMatch;
});
}
}
const directNameMatch = _.get(preferredTypes, nodeName) !== undefined;
const unifiedSyntaxParentMatch = parentType && directNameMatch && unifyParentAndChildTypeChecks;
isGenericMatch = isGenericMatch || unifiedSyntaxParentMatch;

hasMatchingPreferredType = isGenericMatch ||
directNameMatch && !parentType;
}
const directNameMatch = _.get(preferredTypes, nodeName) !== undefined;
const unifiedSyntaxParentMatch = parentType && directNameMatch && unifyParentAndChildTypeChecks;
isGenericMatch = isGenericMatch || unifiedSyntaxParentMatch;

return [hasMatchingPreferredType, typeName, isGenericMatch];
};
hasMatchingPreferredType = isGenericMatch ||
directNameMatch && !parentType;
}

const adjustNames = (type, preferred, isGenericMatch, nodeName, node, parentNode) => {
let ret = preferred;
if (isGenericMatch) {
if (preferred === '[]') {
parentNode.meta.syntax = 'SQUARE_BRACKET';
ret = 'Array';
} else {
const dotBracketEnd = preferred.match(/\.(?:<>)?$/u);
if (dotBracketEnd) {
parentNode.meta.syntax = 'ANGLE_BRACKET_WITH_DOT';
ret = preferred.slice(0, -dotBracketEnd[0].length);
} else {
const bracketEnd = preferred.endsWith('<>');
if (bracketEnd) {
parentNode.meta.syntax = 'ANGLE_BRACKET';
ret = preferred.slice(0, -2);
}
}
}
} else if (type === 'ANY') {
node.type = 'NAME';
}
node.name = ret.replace(/(?:\.|<>|\.<>|\[\])$/u, '');
return [hasMatchingPreferredType, typeName, isGenericMatch];
};

// For bare pseudo-types like `<>`
if (!ret) {
node.name = nodeName;
}
};
jsdocTagsWithPossibleType.forEach((jsdocTag) => {
const invalidTypes = [];
let typeAst;

try {
typeAst = parse(jsdocTag.type);
} catch (error) {
return;
}

traverse(typeAst, (node, parentName, parentNode) => {
const {type, name} = node;
Expand Down
2 changes: 1 addition & 1 deletion src/rules/requireHyphenBeforeParamDescription.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default iterateJsdoc(({
const descriptionIndex = sourceLines[lineIndex].lastIndexOf(description);

const replacementLine = sourceLines[lineIndex]
.substring(0, descriptionIndex) + '- ' + description;
.slice(0, descriptionIndex) + '- ' + description;
sourceLines.splice(lineIndex, 1, replacementLine);
const replacement = sourceLines.join('\n');

Expand Down
43 changes: 21 additions & 22 deletions src/rules/requireJsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,28 +115,7 @@ const getOptions = (context) => {
};
};

/* eslint-disable sort-keys */
export default {
meta: {
doc: {
category: 'Stylistic Issues',
description: 'Require JSDoc comments',
recommended: 'true',
url: 'https://github.com/gajus/eslint-plugin-jsdoc',
},

fixable: 'code',

messages: {
missingJsDoc: 'Missing JSDoc comment.',
},

schema: [
OPTIONS_SCHEMA,
],

type: 'suggestion',
},
create (context) {
warnRemovedSettings(context, 'require-jsdoc');

Expand Down Expand Up @@ -179,9 +158,9 @@ export default {
};
context.report({
fix,
loc,
messageId: 'missingJsDoc',
node,
loc,
});
};

Expand Down Expand Up @@ -263,4 +242,24 @@ export default {
},
);
},
meta: {
doc: {
category: 'Stylistic Issues',
description: 'Require JSDoc comments',
recommended: 'true',
url: 'https://github.com/gajus/eslint-plugin-jsdoc',
},

fixable: 'code',

messages: {
missingJsDoc: 'Missing JSDoc comment.',
},

schema: [
OPTIONS_SCHEMA,
],

type: 'suggestion',
},
};
13 changes: 6 additions & 7 deletions test/eslint/getJSDocComment.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@ import {
import getJSDocComment from '../../src/eslint/getJSDocComment';
import {getSettings} from '../../src/iterateJsdoc';

/* eslint-disable sort-keys */
const rule = {
meta: {
messages: {
missingJsDoc: 'Missing JSDoc comment.',
},
type: 'layout',
},
create (context) {
const sourceCode = context.getSourceCode();
const settings = getSettings(context);
Expand All @@ -29,6 +22,12 @@ const rule = {
},
};
},
meta: {
messages: {
missingJsDoc: 'Missing JSDoc comment.',
},
type: 'layout',
},
};

const ruleTester = new RuleTester();
Expand Down

0 comments on commit 8ca5030

Please sign in to comment.