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

Removed use of type checker from completed-docs #3557

Merged
merged 11 commits into from
Feb 23, 2019
43 changes: 28 additions & 15 deletions src/rules/completedDocsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/

import * as tsutils from "tsutils";
import * as ts from "typescript";

import * as Lint from "../index";
Expand Down Expand Up @@ -79,7 +80,7 @@ export type Visibility = All
| typeof VISIBILITY_EXPORTED
| typeof VISIBILITY_INTERNAL;

export class Rule extends Lint.Rules.TypedRule {
export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING_EXIST = "Documentation must exist for ";

public static defaultArguments: IInputExclusionDescriptors = {
Expand Down Expand Up @@ -281,17 +282,16 @@ export class Rule extends Lint.Rules.TypedRule {

type: "style",
typescriptOnly: false,
requiresTypeInfo: true,
};
/* tslint:enable:object-literal-sort-keys */

private readonly exclusionFactory = new ExclusionFactory();

public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const options = this.getOptions();
const exclusionsMap = this.getExclusionsMap(options.ruleArguments);

return this.applyWithFunction(sourceFile, walk, exclusionsMap, program.getTypeChecker());
return this.applyWithFunction(sourceFile, walk, exclusionsMap);
}

private getExclusionsMap(ruleArguments: Array<DocType | IInputExclusionDescriptors>): ExclusionsMap {
Expand All @@ -307,7 +307,22 @@ const modifierAliases: { [i: string]: string } = {
export: "exported",
};

function walk(context: Lint.WalkContext<ExclusionsMap>, typeChecker: ts.TypeChecker) {
/**
* @remarks See https://github.com/ajafff/tsutils/issues/16
*/
const getApparentJsDoc = (node: ts.Node): ts.JSDoc[] => {
if (node.kind === ts.SyntaxKind.VariableDeclaration) {
const parent = node.parent as ts.VariableDeclarationList;
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved

return node === parent.declarations[0]
? tsutils.parseJsDocOfNode(parent)
: tsutils.parseJsDocOfNode(node);
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
}

return tsutils.getJsDoc(node);
};

function walk(context: Lint.WalkContext<ExclusionsMap>) {
return ts.forEachChild(context.sourceFile, cb);

function cb(node: ts.Node): void {
Expand Down Expand Up @@ -390,18 +405,16 @@ function walk(context: Lint.WalkContext<ExclusionsMap>, typeChecker: ts.TypeChec
}
}

const symbol = typeChecker.getSymbolAtLocation(name);
if (symbol === undefined) {
return;
}

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

function checkComments(node: ts.Node, nodeDescriptor: string, comments: ts.SymbolDisplayPart[], requirementNode: ts.Node) {
if (comments.map((comment: ts.SymbolDisplayPart) => comment.text).join("").trim() === "") {
addDocumentationFailure(node, nodeDescriptor, requirementNode);
function checkNodeDocs(node: ts.Node, nodeType: DocType, docs: ts.JSDoc[], requirementNode: ts.Node) {
const comments = docs
.map((doc) => doc.comment)
.filter((comment) => comment !== undefined && comment.trim() !== "");

if (comments.length === 0) {
addDocumentationFailure(node, describeNode(nodeType), requirementNode);
}
}

Expand Down
6 changes: 4 additions & 2 deletions test/rules/completed-docs/accessors/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export class CommentsOnGetOnly {
*/
get myAccessor(): string { return this.myField; }

set myAccessor(value: string) { this.myField = value; } // Comments for the setter are inherited from the getter.
set myAccessor(value: string) { this.myField = value; }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Documentation must exist for properties.]
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved

}

Expand All @@ -56,7 +57,8 @@ export class CommentsOnSetOnly {
*/
private myField: string;

get myAccessor(): string { return this.myField; } // Comments from the getter are inherited from the setter.
get myAccessor(): string { return this.myField; }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Documentation must exist for properties.]

/**
* The set accessor.
Expand Down
3 changes: 1 addition & 2 deletions test/rules/completed-docs/defaults/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ export class Aaa {
public set prop(value) { this.bbb = value; }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Documentation must exist for public properties.]

// TODO: TypeScript API doesn't give us a symbol for this, so we must ignore it.
// https://github.com/Microsoft/TypeScript/issues/14257
[Symbol.iterator]() {}
~~~~~~~~~~~~~~~~~~~~~~ [Documentation must exist for methods.]
}

export enum Ddd { }
Expand Down
13 changes: 12 additions & 1 deletion test/rules/completed-docs/tags/content/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@ const ContentValidVariable = 4;
/**
* ...
*/
const CommentBodyVariableRofl = 5;
const CommentBodyVariableDots = 5;

/**
* ...content
*/
const CommentBodyVariableDotsAndContent = 6;

/** content */
const CommentBodyVariableSingleLineContent = 7;

/** ...content */
const CommentBodyVariableSingleLineDotsAndContent = 8;