Skip to content

Commit a5f2cc7

Browse files
committed
chore(lint): Add lint check for license headers
Added a tslint check to make sure all source files begin with a license header (at the very beginning or after a `#!`). Relates to angular#9380
1 parent e1e5c40 commit a5f2cc7

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {RuleWalker} from 'tslint/lib/language/walker';
2+
import {RuleFailure} from 'tslint/lib/lint';
3+
import {AbstractRule} from 'tslint/lib/rules';
4+
import * as ts from 'typescript';
5+
6+
export class Rule extends AbstractRule {
7+
public static FAILURE_STRING = 'missing copyright header';
8+
9+
public apply(sourceFile: ts.SourceFile): RuleFailure[] {
10+
const walker = new EnforceCopyrightHeaderWalker(sourceFile, this.getOptions());
11+
return this.applyWithWalker(walker);
12+
}
13+
}
14+
15+
class EnforceCopyrightHeaderWalker extends RuleWalker {
16+
private regex: RegExp = /\/\*[\s\S]*?Copyright Google Inc\.[\s\S]*?\*\//;
17+
18+
public visitSourceFile(node: ts.SourceFile) {
19+
// check for a shebang
20+
let text = node.getFullText();
21+
let offset = 0;
22+
if (text.indexOf('#!') === 0) {
23+
offset = text.indexOf('\n') + 1;
24+
text = text.substring(offset);
25+
}
26+
// look for the copyright header
27+
let match = text.match(this.regex);
28+
if (!match || match.index !== 0) {
29+
this.addFailure(this.createFailure(offset, offset + 1, Rule.FAILURE_STRING));
30+
}
31+
}
32+
}

tslint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"rules": {
33
"requireInternalWithUnderscore": true,
44
"duplicateModuleImport": true,
5+
"enforce-copyright-header": true,
56
"semicolon": true,
67
"variable-name": [true, "ban-keywords"]
78
}

0 commit comments

Comments
 (0)