Skip to content

Commit

Permalink
ci: add require formly documentation rule
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKless committed Feb 17, 2022
1 parent 6db7f7d commit af10662
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@
"ish-custom-rules/use-camel-case-environment-properties": "error",
"ish-custom-rules/use-component-change-detection": "warn",
"ish-custom-rules/use-jest-extended-matchers-in-tests": "warn",
"ish-custom-rules/require-formly-code-documentation": "warn",
"jest/no-commented-out-tests": "warn",
"jest/no-disabled-tests": "warn",
"jest/no-focused-tests": "warn",
Expand Down
2 changes: 2 additions & 0 deletions eslint-rules/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { noVarBeforeReturnRule } from './rules/no-var-before-return';
import { orderedImportsRule } from './rules/ordered-imports';
import { privateDestroyFieldRule } from './rules/private-destroy-field';
import { projectStructureRule } from './rules/project-structure';
import { requireFormlyCodeDocumentationRule } from './rules/require-formly-code-documentation';
import { useAliasImportsRule } from './rules/use-alias-imports';
import { useAsyncSynchronizationInTestsRule } from './rules/use-async-synchronization-in-tests';
import { useCamelCaseEnvironmentPropertiesRule } from './rules/use-camel-case-environment-properties';
Expand Down Expand Up @@ -40,6 +41,7 @@ const rules = {
'project-structure': projectStructureRule,
'newline-before-root-members': newlineBeforeRootMembersRule,
'no-var-before-return': noVarBeforeReturnRule,
'require-formly-code-documentation': requireFormlyCodeDocumentationRule,
};

module.exports = {
Expand Down
66 changes: 66 additions & 0 deletions eslint-rules/src/rules/require-formly-code-documentation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { AST_NODE_TYPES, TSESLint, TSESTree } from '@typescript-eslint/experimental-utils';

import { getClosestAncestorByKind } from '../helpers';

export const requireFormlyCodeDocumentationRule: TSESLint.RuleModule<string, []> = {
meta: {
messages: {
missingDocumentationError: `Missing documentation for {{ artifactName }}. \n Please provide documentation for all Formly types, wrappers and extensions.`,
},
type: 'problem',
schema: [],
},
create: context => {
function hasPrecedingComment(node: TSESTree.ClassDeclaration) {
return (
context.getSourceCode().getCommentsBefore(node)?.length > 0 ||
context.getSourceCode().getCommentsBefore(node.decorators?.[0])?.length > 0
);
}
if (!context.getFilename().includes('formly')) {
return {};
}
return {
ClassDeclaration(node) {
if (isFormlyArtifactClass(node) && !hasPrecedingComment(node)) {
context.report({
node,
messageId: 'missingDocumentationError',
data: {
artifactName: node.id.name,
},
});
}
},
'ExportNamedDeclaration Identifier'(node: TSESTree.Identifier) {
if (
node.typeAnnotation?.typeAnnotation?.type === AST_NODE_TYPES.TSTypeReference &&
node.typeAnnotation.typeAnnotation.typeName.type === AST_NODE_TYPES.Identifier &&
node.typeAnnotation.typeAnnotation.typeName.name === 'FormlyExtension' &&
context
.getSourceCode()
.getCommentsBefore(getClosestAncestorByKind(context, AST_NODE_TYPES.ExportNamedDeclaration)).length === 0
) {
context.report({
node,
messageId: 'missingDocumentationError',
data: {
artifactName: node.name,
},
});
}
},
};
},
};

function isFormlyArtifactClass(node: TSESTree.ClassDeclaration): boolean {
return (
['FieldType', 'FieldWrapper'].some(
superClass => node.superClass?.type === AST_NODE_TYPES.Identifier && node.superClass?.name === superClass
) ||
node.implements?.some(
impl => impl.expression.type === AST_NODE_TYPES.Identifier && impl.expression.name === 'FormlyExtension'
)
);
}
14 changes: 14 additions & 0 deletions eslint-rules/tests/require-formly-code-documentation.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { requireFormlyCodeDocumentationRule } from '../src/rules/require-formly-code-documentation';

import { RuleTestConfig } from './_execute-tests';

const config: RuleTestConfig = {
ruleName: 'require-formly-code-documentation',
rule: requireFormlyCodeDocumentationRule,
tests: {
valid: [],
invalid: [],
},
};

export default config;

1 comment on commit af10662

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Azure Demo Servers are available:

Please sign in to comment.