File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change
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 ] * ?C o p y r i g h t G o o g l e I n c \. [ \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
+ }
Original file line number Diff line number Diff line change 2
2
"rules" : {
3
3
"requireInternalWithUnderscore" : true ,
4
4
"duplicateModuleImport" : true ,
5
+ "enforce-copyright-header" : true ,
5
6
"semicolon" : true ,
6
7
"variable-name" : [true , " ban-keywords" ]
7
8
}
You can’t perform that action at this time.
0 commit comments