This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 889
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
file-header
rule to enforce headers matched by regexp (#1441)
- Checks for comments at the very beginning of files - Ignores #! - Matches comment contents with header regexp
- Loading branch information
Showing
15 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
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: "file-header", | ||
description: "Enforces a certain header comment for all files, matched by a regular expression.", | ||
optionsDescription: "Regular expression to match the header.", | ||
options: { | ||
type: "string", | ||
}, | ||
optionExamples: ['"true", "Copyright \\d{4}"'], | ||
type: "style", | ||
}; | ||
/* tslint:enable:object-literal-sort-keys */ | ||
|
||
public static FAILURE_STRING = "missing file header"; | ||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
const walker = new FileHeaderWalker(sourceFile, this.getOptions()); | ||
const options = this.getOptions().ruleArguments; | ||
walker.setRegexp(new RegExp(options[0].toString())); | ||
return this.applyWithWalker(walker); | ||
} | ||
} | ||
|
||
class FileHeaderWalker extends Lint.RuleWalker { | ||
// match a single line or multi line comment with leading whitespace | ||
// the wildcard dot does not match new lines - we can use [\s\S] instead | ||
private commentRegexp: RegExp = /^\s*(\/\/(.*?)|\/\*([\s\S]*?)\*\/)/; | ||
private headerRegexp: RegExp; | ||
|
||
public setRegexp(headerRegexp: RegExp) { | ||
this.headerRegexp = headerRegexp; | ||
} | ||
|
||
public visitSourceFile(node: ts.SourceFile) { | ||
if (this.headerRegexp) { | ||
let text = node.getFullText(); | ||
let offset = 0; | ||
// ignore shebang if it exists | ||
if (text.indexOf("#!") === 0) { | ||
offset = text.indexOf("\n") + 1; | ||
text = text.substring(offset); | ||
} | ||
// check for a comment | ||
const match = text.match(this.commentRegexp); | ||
if (!match) { | ||
this.addFailure(this.createFailure(offset, 0, Rule.FAILURE_STRING)); | ||
} else { | ||
// either the third or fourth capture group contains the comment contents | ||
const comment = match[2] ? match[2] : match[3]; | ||
if (comment.search(this.headerRegexp) < 0) { | ||
this.addFailure(this.createFailure(offset, 0, Rule.FAILURE_STRING)); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/env node | ||
|
||
~nil [missing file header] | ||
/* | ||
* Bad header 3 | ||
*/ | ||
|
||
export class A { | ||
public x = 1; | ||
|
||
public B() { | ||
return 2; | ||
} | ||
} | ||
|
||
/* | ||
* Good header 4 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"file-header": [true, "Good header \\d"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
"use strict"; | ||
~nil [missing file header] | ||
|
||
/* | ||
* Bad header 5 | ||
*/ | ||
|
||
export class A { | ||
public x = 1; | ||
|
||
public B() { | ||
return 2; | ||
} | ||
} | ||
|
||
/* | ||
* Good header 6 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"file-header": [true, "Good header \\d"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
~nil [missing file header] | ||
* Bad header 1 | ||
*/ | ||
|
||
export class A { | ||
public x = 1; | ||
|
||
public B() { | ||
return 2; | ||
} | ||
} | ||
|
||
/* | ||
* Good header 2 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"file-header": [true, "Good header \\d"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env node | ||
|
||
|
||
/* | ||
* Good header 2 | ||
*/ | ||
|
||
export class A { | ||
public x = 1; | ||
|
||
public B() { | ||
return 2; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"file-header": [true, "Good header \\d"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Good header 3 | ||
*/ | ||
|
||
"use strict"; | ||
|
||
export class A { | ||
public x = 1; | ||
|
||
public B() { | ||
return 2; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"file-header": [true, "Good header \\d"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
/* | ||
* Good header 1 | ||
*/ | ||
|
||
export class A { | ||
public x = 1; | ||
|
||
public B() { | ||
return 2; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"file-header": [true, "Good header \\d"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters