forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingleEofLineRule.ts
59 lines (51 loc) · 1.77 KB
/
singleEofLineRule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as Lint from 'tslint';
import * as ts from 'typescript';
export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: 'single-eof-line',
type: 'style',
description: `Ensure the file ends with a single new line.`,
rationale: `This is similar to eofline, but ensure an exact count instead of just any new
line.`,
options: null,
optionsDescription: `Two integers indicating minimum and maximum number of new lines.`,
typescriptOnly: false,
};
public static FAILURE_STRING = 'You need to have a single blank line at end of file.';
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const length = sourceFile.text.length;
if (length === 0) {
// Allow empty files.
return [];
}
const matchEof = /\r?\n((\r?\n)*)$/.exec(sourceFile.text);
if (!matchEof) {
const lines = sourceFile.getLineStarts();
const fix = Lint.Replacement.appendText(
length,
sourceFile.text[lines[1] - 2] === '\r' ? '\r\n' : '\n',
);
return [
new Lint.RuleFailure(sourceFile, length, length, Rule.FAILURE_STRING, this.ruleName, fix),
];
} else if (matchEof[1]) {
const lines = sourceFile.getLineStarts();
const fix = Lint.Replacement.replaceFromTo(
matchEof.index,
length,
sourceFile.text[lines[1] - 2] === '\r' ? '\r\n' : '\n',
);
return [
new Lint.RuleFailure(sourceFile, length, length, Rule.FAILURE_STRING, this.ruleName, fix),
];
}
return [];
}
}