Skip to content

Commit 476fa63

Browse files
committed
build: add linting rule for single new line at eof
and fix the lint errors.
1 parent e8738e3 commit 476fa63

File tree

11 files changed

+61
-9
lines changed

11 files changed

+61
-9
lines changed

packages/angular_devkit/build_optimizer/src/purify/webpack-plugin.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,3 @@ export class PurifyPlugin {
3333
});
3434
}
3535
}
36-

packages/angular_devkit/build_optimizer/src/transforms/scrub-file_spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,3 @@ describe('scrub-file', () => {
203203
});
204204
});
205205
});
206-

packages/angular_devkit/schematics/src/rules/call.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,3 @@ export function callRule(rule: Rule,
6868
}
6969
});
7070
}
71-

packages/angular_devkit/schematics/src/sink/dryrun.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,3 @@ export class DryRunSink extends FileSystemSink {
114114
return Observable.empty<void>();
115115
}
116116
}
117-

packages/angular_devkit/schematics/src/tree/action_spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,3 @@ describe('Action', () => {
7777
});
7878
});
7979
});
80-

packages/angular_devkit/schematics/tools/description.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,3 @@ export declare type FileSystemSchematicDesc
4949
= SchematicDescription<FileSystemCollectionDescription, FileSystemSchematicDescription>;
5050
export declare type FileSystemSchematicContext
5151
= TypedSchematicContext<FileSystemCollectionDescription, FileSystemSchematicDescription>;
52-

packages/angular_devkit/schematics/tools/registry-engine-host.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,3 @@ export class RegistryEngineHost extends FileSystemEngineHostBase {
118118
return desc as FileSystemSchematicDesc;
119119
}
120120
}
121-

packages/schematics/angular/utility/ast-utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,4 +399,3 @@ export function addBootstrapToModule(source: ts.SourceFile,
399399
importPath: string): Change[] {
400400
return _addSymbolToNgModuleMetadata(source, modulePath, 'bootstrap', classifiedName, importPath);
401401
}
402-

packages/schematics/angular/utility/route-utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,3 @@ export function insertImport(source: ts.SourceFile, fileToEdit: string, symbolNa
8787
ts.SyntaxKind.StringLiteral,
8888
);
8989
}
90-

rules/singleEofLineRule.ts

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

tslint.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
"import-groups": true,
77
"non-null-operator": true,
88
"no-global-tslint-disable": true,
9+
"single-eof-line": true,
10+
911

1012
"import-blacklist": [
1113
true,

0 commit comments

Comments
 (0)