forked from palantir/tslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsdocFormatRule.ts
126 lines (113 loc) · 6.04 KB
/
jsdocFormatRule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* @license
* Copyright 2013 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as ts from "typescript";
import * as Lint from "../lint";
export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "jsdoc-format",
description: "Enforces basic format rules for JSDoc comments.",
descriptionDetails: Lint.Utils.dedent`
The following rules are enforced for JSDoc comments (comments starting with \`/**\`):
* each line contains an asterisk and asterisks must be aligned
* each asterisk must be followed by either a space or a newline (except for the first and the last)
* the only characters before the asterisk on each line must be whitespace characters
* one line comments must start with \`/** \` and end with \`*/\``,
rationale: "Helps maintain a consistent, readable style for JSDoc comments.",
optionsDescription: "Not configurable.",
options: null,
optionExamples: ["true"],
type: "style",
};
/* tslint:enable:object-literal-sort-keys */
public static ALIGNMENT_FAILURE_STRING = "asterisks in jsdoc must be aligned";
public static FORMAT_FAILURE_STRING = "jsdoc is not formatted correctly on this line";
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new JsdocWalker(sourceFile, this.getOptions()));
}
}
class JsdocWalker extends Lint.SkippableTokenAwareRuleWalker {
public visitSourceFile(node: ts.SourceFile) {
super.visitSourceFile(node);
Lint.scanAllTokens(ts.createScanner(ts.ScriptTarget.ES5, false, ts.LanguageVariant.Standard, node.text), (scanner: ts.Scanner) => {
const startPos = scanner.getStartPos();
if (this.tokensToSkipStartEndMap[startPos] != null) {
// tokens to skip are places where the scanner gets confused about what the token is, without the proper context
// (specifically, regex, identifiers, and templates). So skip those tokens.
scanner.setTextPos(this.tokensToSkipStartEndMap[startPos]);
return;
}
if (scanner.getToken() === ts.SyntaxKind.MultiLineCommentTrivia) {
const commentText = scanner.getTokenText();
const startPosition = scanner.getTokenPos();
this.findFailuresForJsdocComment(commentText, startPosition, node);
}
});
}
private findFailuresForJsdocComment(commentText: string, startingPosition: number, sourceFile: ts.SourceFile) {
const currentPosition = startingPosition;
// the file may be different depending on the OS it was originally authored on
// can't rely on require('os').EOL or process.platform as that is the execution env
// regex is: split optionally on \r\n, but alwasy split on \n if no \r exists
const lines = commentText.split(/\r?\n/);
const firstLine = lines[0];
let jsdocPosition = currentPosition;
// regex is: start of string, followed by any amount of whitespace, followed by /**
const isJsdocMatch = firstLine.match(/^\s*\/\*\*/);
if (isJsdocMatch != null) {
if (lines.length === 1) {
const firstLineMatch = firstLine.match(/^\s*\/\*\* (.* )?\*\/$/);
if (firstLineMatch == null) {
this.addFailureAt(jsdocPosition, firstLine.length, Rule.FORMAT_FAILURE_STRING);
}
return;
}
const indexToMatch = firstLine.indexOf("**") + sourceFile.getLineAndCharacterOfPosition(currentPosition).character;
// all lines but the first and last
const otherLines = lines.splice(1, lines.length - 2);
jsdocPosition += firstLine.length + 1; // + 1 for the splitted-out newline
for (let line of otherLines) {
// regex is: start of string, followed by any amount of whitespace, followed by *,
// followed by either a space or the end of the string
const asteriskMatch = line.match(/^\s*\*( |$)/);
if (asteriskMatch == null) {
this.addFailureAt(jsdocPosition, line.length, Rule.FORMAT_FAILURE_STRING);
}
const asteriskIndex = line.indexOf("*");
if (asteriskIndex !== indexToMatch) {
this.addFailureAt(jsdocPosition, line.length, Rule.ALIGNMENT_FAILURE_STRING);
}
jsdocPosition += line.length + 1; // + 1 for the splitted-out newline
}
const lastLine = lines[lines.length - 1];
// regex is: start of string, followed by any amount of whitespace, followed by */,
// followed by the end of the string
const endBlockCommentMatch = lastLine.match(/^\s*\*\/$/);
if (endBlockCommentMatch == null) {
this.addFailureAt(jsdocPosition, lastLine.length, Rule.FORMAT_FAILURE_STRING);
}
const lastAsteriskIndex = lastLine.indexOf("*");
if (lastAsteriskIndex !== indexToMatch) {
this.addFailureAt(jsdocPosition, lastLine.length, Rule.ALIGNMENT_FAILURE_STRING);
}
}
}
private addFailureAt(currentPosition: number, width: number, failureString: string) {
const failure = this.createFailure(currentPosition, width, failureString);
this.addFailure(failure);
}
}