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 2 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
129 changes: 110 additions & 19 deletions src/rules/fileHeaderRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,90 @@ import * as Lint from "../index";

const ENFORCE_TRAILING_NEWLINE = "enforce-trailing-newline";

interface FileHeaderRuleOptions {
match: string;
allowSingleLineComments?: boolean;
default?: string;
enforceTrailingNewline?: boolean;
}

export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "file-header",
description:
"Enforces a certain header comment for all files, matched by a regular expression.",
optionsDescription: Lint.Utils.dedent`
A single object may be passed in for configuration that must contain:

* \`match\`: a regular expression that all headers should match

Any of the following optional fields may also be provided:

* \`allowSingleLineComments\`: a boolean for whether \`//\` should be considered file headers in addition to \`/*\` comments
* \`default\`: text to add for file headers when running in \`--fix\` mode
* \`enforceTrailingNewline\`: a boolean for whether a newline must follow the header
rrogowski marked this conversation as resolved.
Show resolved Hide resolved

The rule will also accept array of strings as a legacy form of options, though the object form is recommended.
The first option, which is mandatory, is a regular expression that all headers should match.
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.`,
options: {
rrogowski marked this conversation as resolved.
Show resolved Hide resolved
type: "array",
items: [
oneOf: [
{
type: "string",
type: "array",
items: {
type: "object",
properties: {
match: {
type: "string",
},
allowSingleLineComments: {
type: "boolean",
},
default: {
type: "string",
},
enforceTrailingNewline: {
type: "boolean",
},
},
additionalProperties: false,
},
},
{
type: "string",
type: "array",
items: [
{
type: "string",
},
{
type: "string",
},
{
type: "string",
},
],
additionalItems: false,
minLength: 1,
maxLength: 3,
},
],
},
optionExamples: [
[
true,
{
type: "string",
match: "Copyright \\d{4}",
allowSingleLineComments: true,
default: "Copyright 2018",
enforceTrailingNewline: true,
},
],
additionalItems: false,
minLength: 1,
maxLength: 3,
},
optionExamples: [[true, "Copyright \\d{4}", "Copyright 2018", ENFORCE_TRAILING_NEWLINE]],
[true, "Copyright \\d{4}", "Copyright 2018", ENFORCE_TRAILING_NEWLINE],
],
hasFix: true,
type: "style",
typescriptOnly: false,
Expand All @@ -61,18 +115,15 @@ export class Rule extends Lint.Rules.AbstractRule {
public static MISSING_NEW_LINE_FAILURE_STRING = "missing new line following the file header";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const options = this.getRuleOptions();

const { text } = sourceFile;
const headerFormat = new RegExp(this.ruleArguments[0] as string);
const textToInsert = this.ruleArguments[1] as string | undefined;
const enforceExtraTrailingLine =
this.ruleArguments.indexOf(ENFORCE_TRAILING_NEWLINE) !== -1;
const headerFormat = new RegExp(options.match);
const textToInsert = options.default;

// 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, !!options.allowSingleLineComments);

if (commentText === undefined || !headerFormat.test(commentText)) {
const isErrorAtStart = offset === 0;
Expand Down Expand Up @@ -107,7 +158,7 @@ export class Rule extends Lint.Rules.AbstractRule {
}

const trailingNewLineViolation =
enforceExtraTrailingLine &&
options.enforceTrailingNewline &&
headerFormat.test(commentText) &&
this.doesNewLineEndingViolationExist(text, offset);

Expand Down Expand Up @@ -135,6 +186,24 @@ export class Rule extends Lint.Rules.AbstractRule {
return [];
}

private getRuleOptions(): FileHeaderRuleOptions {
const options = this.ruleArguments;
if (options.length === 1 && typeof options[0] === "object") {
return options[0] as FileHeaderRuleOptions;
}

// Legacy options
const args = this.ruleArguments as string[];
return {
default: args[1],
enforceTrailingNewline:
args[2] !== undefined
? args[2].indexOf(ENFORCE_TRAILING_NEWLINE) !== -1
: undefined,
match: args[0],
};
}

private createComment(
sourceFile: ts.SourceFile,
commentText: string,
Expand Down Expand Up @@ -172,4 +241,26 @@ export class Rule extends Lint.Rules.AbstractRule {
entireComment !== undefined && NEW_LINE_FOLLOWING_HEADER.test(entireComment) !== null
);
}

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

const fileHeaderRanges = !allowSingleLineComments ? 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");
}
}
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
// **********************************
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// **********************************
// Good header
// **********************************

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"rules": {
"file-header": [true, {
"match": "Good header",
"allowSingleLineComments": true
rrogowski marked this conversation as resolved.
Show resolved Hide resolved
}]
}
}