Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

completed-docs: Add support for enum members #2911

Merged
merged 1 commit into from
Jun 23, 2017
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
8 changes: 8 additions & 0 deletions src/language/walker/syntaxWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ export class SyntaxWalker {
this.walkChildren(node);
}

protected visitEnumMember(node: ts.EnumMember) {
this.walkChildren(node);
}

protected visitExportAssignment(node: ts.ExportAssignment) {
this.walkChildren(node);
}
Expand Down Expand Up @@ -456,6 +460,10 @@ export class SyntaxWalker {
this.visitEnumDeclaration(node as ts.EnumDeclaration);
break;

case ts.SyntaxKind.EnumMember:
this.visitEnumMember(node as ts.EnumMember);
break;

case ts.SyntaxKind.ExportAssignment:
this.visitExportAssignment(node as ts.ExportAssignment);
break;
Expand Down
29 changes: 22 additions & 7 deletions src/rules/completedDocsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const ALL = "all";

export const ARGUMENT_CLASSES = "classes";
export const ARGUMENT_ENUMS = "enums";
export const ARGUMENT_ENUM_MEMBERS = "enum-members";
export const ARGUMENT_FUNCTIONS = "functions";
export const ARGUMENT_INTERFACES = "interfaces";
export const ARGUMENT_METHODS = "methods";
Expand Down Expand Up @@ -66,6 +67,7 @@ export type All = typeof ALL;
export type DocType = All
| typeof ARGUMENT_CLASSES
| typeof ARGUMENT_ENUMS
| typeof ARGUMENT_ENUM_MEMBERS
| typeof ARGUMENT_FUNCTIONS
| typeof ARGUMENT_INTERFACES
| typeof ARGUMENT_METHODS
Expand Down Expand Up @@ -165,6 +167,7 @@ export class Rule extends Lint.Rules.TypedRule {

* \`"${ARGUMENT_CLASSES}"\`
* \`"${ARGUMENT_ENUMS}"\`
* \`"${ARGUMENT_ENUM_MEMBERS}"\`
* \`"${ARGUMENT_FUNCTIONS}"\`
* \`"${ARGUMENT_INTERFACES}"\`
* \`"${ARGUMENT_METHODS}"\`
Expand All @@ -185,6 +188,7 @@ export class Rule extends Lint.Rules.TypedRule {
properties: {
[ARGUMENT_CLASSES]: Rule.ARGUMENT_DESCRIPTOR_BLOCK,
[ARGUMENT_ENUMS]: Rule.ARGUMENT_DESCRIPTOR_BLOCK,
[ARGUMENT_ENUM_MEMBERS]: Rule.ARGUMENT_DESCRIPTOR_BLOCK,
[ARGUMENT_FUNCTIONS]: Rule.ARGUMENT_DESCRIPTOR_BLOCK,
[ARGUMENT_INTERFACES]: Rule.ARGUMENT_DESCRIPTOR_BLOCK,
[ARGUMENT_METHODS]: Rule.ARGUMENT_DESCRIPTOR_CLASS,
Expand Down Expand Up @@ -353,6 +357,13 @@ class CompletedDocsWalker extends Lint.ProgramAwareRuleWalker {
super.visitEnumDeclaration(node);
}

public visitEnumMember(node: ts.EnumMember): void {
// Enum members don't have modifiers, so use the parent
// enum declaration when checking the requirements.
this.checkNode(node, ARGUMENT_ENUM_MEMBERS, node.parent);
super.visitEnumMember(node);
}

public visitFunctionDeclaration(node: ts.FunctionDeclaration): void {
this.checkNode(node, ARGUMENT_FUNCTIONS);
super.visitFunctionDeclaration(node);
Expand Down Expand Up @@ -388,14 +399,14 @@ class CompletedDocsWalker extends Lint.ProgramAwareRuleWalker {
super.visitVariableDeclaration(node);
}

private checkNode(node: ts.NamedDeclaration, nodeType: DocType): void {
private checkNode(node: ts.NamedDeclaration, nodeType: DocType, requirementNode: ts.Declaration = node): void {
const { name } = node;
if (name === undefined) {
return;
}

const requirement = this.requirements.get(nodeType);
if (requirement === undefined || !requirement.shouldNodeBeDocumented(node)) {
if (requirement === undefined || !requirement.shouldNodeBeDocumented(requirementNode)) {
return;
}

Expand All @@ -405,19 +416,23 @@ class CompletedDocsWalker extends Lint.ProgramAwareRuleWalker {
}

const comments = symbol.getDocumentationComment();
this.checkComments(node, nodeType, comments);
this.checkComments(node, this.describeNode(nodeType), comments, requirementNode);
}

private describeNode(nodeType: DocType): string {
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems a little hardcody to just do this to ARGUMENT_ENUM_MEMBERS. How about generalizing the logic by taking in both node and requirementNode? If node is an ancestor of requirementNode, append " members" to the description.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I thought about some sort of camel-case-to-words thing, but it seemed like overkill. Now that I've changed enumMembers to enum-members, I could just do:

return nodeType.replace("-", " ");

return nodeType.replace("-", " ");
}

private checkComments(node: ts.Declaration, nodeDescriptor: string, comments: ts.SymbolDisplayPart[]) {
private checkComments(node: ts.Declaration, nodeDescriptor: string, comments: ts.SymbolDisplayPart[], requirementNode: ts.Declaration) {
if (comments.map((comment: ts.SymbolDisplayPart) => comment.text).join("").trim() === "") {
this.addDocumentationFailure(node, nodeDescriptor);
this.addDocumentationFailure(node, nodeDescriptor, requirementNode);
}
}

private addDocumentationFailure(node: ts.Declaration, nodeType: string): void {
private addDocumentationFailure(node: ts.Declaration, nodeType: string, requirementNode: ts.Declaration): void {
const start = node.getStart();
const width = node.getText().split(/\r|\n/g)[0].length;
const description = this.describeDocumentationFailure(node, nodeType);
const description = this.describeDocumentationFailure(requirementNode, nodeType);

this.addFailureAt(start, width, description);
}
Expand Down
13 changes: 13 additions & 0 deletions test/rules/completed-docs/types/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ enum EmptyEnum { }
*/
enum GoodEnum { }

/**
* ...
*/
enum EnumWithMembers {
BadEnumMember
~~~~~~~~~~~~~ [Documentation must exist for enum members.]

/**
* ...
*/
GoodEnumMember
}

function BadFunction() { }
~~~~~~~~~~~~~~~~~~~~~~~~~~ [Documentation must exist for functions.]

Expand Down
1 change: 1 addition & 0 deletions test/rules/completed-docs/types/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
true,
"classes",
"enums",
"enum-members",
"functions",
"interfaces",
"methods",
Expand Down
19 changes: 17 additions & 2 deletions test/rules/completed-docs/visibilities/test.ts.lint
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
export enum BadExportedEnum { }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Documentation must exist for exported enums.]

/**
* ...
*/
export enum ExportedEnum {
BadExportedMember
~~~~~~~~~~~~~~~~~ [Documentation must exist for exported enum members.]
}

/**
* ...
*/
export enum GoodExportedEnum { }

enum BadInternalEnum { }
enum BadInternalEnum {
InternalMember
}

/**
* ...
*/
enum GoodInternalEnum { }
enum GoodInternalEnum {
/**
* ...
*/
GoodInternalMember
}

export interface BadExportedInterface { }

Expand Down
3 changes: 3 additions & 0 deletions test/rules/completed-docs/visibilities/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"enums": {
"visibilities": ["exported"]
},
"enum-members": {
"visibilities": ["exported"]
},
"interfaces": {
"visibilities": ["internal"]
}
Expand Down