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

Address 3180: Rule file-header should have an option to allow // comments #4560

Merged
merged 4 commits into from
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 35 additions & 7 deletions src/rules/fileHeaderRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as ts from "typescript";
import * as Lint from "../index";

const ENFORCE_TRAILING_NEWLINE = "enforce-trailing-newline";
const ALLOW_MULTILINE = "allow-multiline";
rrogowski marked this conversation as resolved.
Show resolved Hide resolved

export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
Expand All @@ -32,7 +33,9 @@ export class Rule extends Lint.Rules.AbstractRule {
The second argument, which is optional, is a string that should be inserted as a header comment
if fixing is enabled and no header that matches the first argument is found.
The third argument, which is optional, is a string that denotes whether or not a newline should
exist on the header.`,
exist on the header.
The fourth argument, which is optional, is a string that denotes whether or not a header is
allowed to consist of consecutive single-line comments.`,
options: {
rrogowski marked this conversation as resolved.
Show resolved Hide resolved
type: "array",
items: [
Expand All @@ -45,12 +48,17 @@ export class Rule extends Lint.Rules.AbstractRule {
{
type: "string",
},
{
type: "string",
},
],
additionalItems: false,
minLength: 1,
maxLength: 3,
maxLength: 4,
},
optionExamples: [[true, "Copyright \\d{4}", "Copyright 2018", ENFORCE_TRAILING_NEWLINE]],
optionExamples: [
[true, "Copyright \\d{4}", "Copyright 2018", ENFORCE_TRAILING_NEWLINE, ALLOW_MULTILINE],
],
hasFix: true,
type: "style",
typescriptOnly: false,
Expand All @@ -66,13 +74,11 @@ export class Rule extends Lint.Rules.AbstractRule {
const textToInsert = this.ruleArguments[1] as string | undefined;
const enforceExtraTrailingLine =
this.ruleArguments.indexOf(ENFORCE_TRAILING_NEWLINE) !== -1;
const allowMultiline = this.ruleArguments.indexOf(ALLOW_MULTILINE) !== -1;

// ignore shebang if it exists
let offset = text.startsWith("#!") ? text.indexOf("\n") : 0;
// returns the text of the first comment or undefined
const commentText = ts.forEachLeadingCommentRange(text, offset, (pos, end, kind) =>
text.substring(pos + 2, kind === ts.SyntaxKind.SingleLineCommentTrivia ? end : end - 2),
);
const commentText = this.getFileHeaderText(text, offset, allowMultiline);

if (commentText === undefined || !headerFormat.test(commentText)) {
const isErrorAtStart = offset === 0;
Expand Down Expand Up @@ -172,4 +178,26 @@ export class Rule extends Lint.Rules.AbstractRule {
entireComment !== undefined && NEW_LINE_FOLLOWING_HEADER.test(entireComment) !== null
);
}

private getFileHeaderText(
text: string,
offset: number,
allowMultiline: boolean,
): string | undefined {
const ranges = ts.getLeadingCommentRanges(text, offset);
if (ranges === undefined || ranges.length === 0) {
return undefined;
}

const fileHeaderRanges = !allowMultiline ? ranges.slice(0, 1) : ranges;
return fileHeaderRanges
.map(range => {
const { pos, kind, end } = range;
return text.substring(
pos + 2,
kind === ts.SyntaxKind.SingleLineCommentTrivia ? end : end - 2,
);
})
.join("\n");
}
}
4 changes: 4 additions & 0 deletions test/rules/file-header/allow-multiline/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// **********************************
// Good header
// **********************************

5 changes: 5 additions & 0 deletions test/rules/file-header/allow-multiline/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"file-header": [true, "Good header", "", "allow-multiline"]
}
}
7 changes: 7 additions & 0 deletions test/rules/file-header/bad/test2.ts.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*!
* Good header 2
*/

// **********************************
// Bad header
// **********************************
4 changes: 4 additions & 0 deletions test/rules/file-header/bad/test2.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// **********************************
~nil [missing file header]
// Bad header
// **********************************