Skip to content

Commit 483cbe2

Browse files
committed
refactor: add tslint-sonarts and a lot of tslint rules
This should clean up the code a bit. Note: at first I added the no-useless-cast rule, but after getting frustrated with it (as it has many false positive), I decided to remove the rule but some useless casts were removed so I let those in the PR.
1 parent c1702d3 commit 483cbe2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2471
-2360
lines changed

Diff for: package-lock.json

+2,319-2,306
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
"tree-kill": "^1.2.0",
131131
"ts-node": "^5.0.0",
132132
"tslint": "^5.9.1",
133+
"tslint-sonarts": "^1.7.0",
133134
"tsutils": "~2.22.2",
134135
"typescript": "~2.7.2",
135136
"uglifyjs-webpack-plugin": "^1.2.5",

Diff for: packages/angular/cli/commands/version.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ export default class VersionCommand extends Command {
2222
public readonly options: Option[] = [];
2323

2424
public run() {
25-
let angularCoreVersion = '';
26-
const angularSameAsCore: string[] = [];
2725
const pkg = require(path.resolve(__dirname, '..', 'package.json'));
2826
let projPkg;
2927
try {
@@ -95,6 +93,8 @@ export default class VersionCommand extends Command {
9593

9694
ngCliVersion = `local (v${pkg.version}, branch: ${gitBranch})`;
9795
}
96+
let angularCoreVersion = '';
97+
const angularSameAsCore: string[] = [];
9898

9999
if (projPkg) {
100100
// Filter all angular versions that are the same as core.
@@ -107,6 +107,9 @@ export default class VersionCommand extends Command {
107107
delete versions[angularPackage];
108108
}
109109
}
110+
111+
// Make sure we list them in alphabetical order.
112+
angularSameAsCore.sort();
110113
}
111114
}
112115

@@ -128,7 +131,7 @@ export default class VersionCommand extends Command {
128131
Node: ${process.versions.node}
129132
OS: ${process.platform} ${process.arch}
130133
Angular: ${angularCoreVersion}
131-
... ${angularSameAsCore.sort().reduce<string[]>((acc, name) => {
134+
... ${angularSameAsCore.reduce<string[]>((acc, name) => {
132135
// Perform a simple word wrap around 60.
133136
if (acc.length == 0) {
134137
return [name];

Diff for: packages/angular/cli/tasks/npm-install.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export type NpmInstall = (packageName: string,
1515
logger: logging.Logger,
1616
packageManager: string,
1717
projectRoot: string,
18-
save?: boolean) => void;
18+
save?: boolean) => Promise<void>;
1919

2020
export default async function (packageName: string,
2121
logger: logging.Logger,

Diff for: packages/angular/cli/utilities/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export function getWorkspaceRaw(
119119
throw new Error('Invalid JSON');
120120
}
121121

122-
return [ast as JsonAstObject, configPath];
122+
return [ast, configPath];
123123
}
124124

125125
export function validateWorkspace(json: JsonObject) {

Diff for: packages/angular_devkit/architect/testing/test-project-host.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,15 @@ export class TestProjectHost extends NodeJsSyncHost {
3434
throw new Error('TestProjectHost must be initialized before being used.');
3535
}
3636

37-
// tslint:disable-next-line:non-null-operator
38-
return this._currentRoot!;
37+
return this._currentRoot;
3938
}
4039

4140
scopedSync(): virtualFs.SyncDelegateHost<Stats> {
4241
if (this._currentRoot === null || this._scopedSyncHost === null) {
4342
throw new Error('TestProjectHost must be initialized before being used.');
4443
}
4544

46-
// tslint:disable-next-line:non-null-operator
47-
return this._scopedSyncHost!;
45+
return this._scopedSyncHost;
4846
}
4947

5048
initialize(): Observable<void> {
@@ -100,7 +98,7 @@ export class TestProjectHost extends NodeJsSyncHost {
10098
}
10199

102100
writeMultipleFiles(files: { [path: string]: string | ArrayBufferLike | Buffer }): void {
103-
Object.keys(files).map(fileName => {
101+
Object.keys(files).forEach(fileName => {
104102
let content = files[fileName];
105103
if (typeof content == 'string') {
106104
content = virtualFs.stringToFileBuffer(content);

Diff for: packages/angular_devkit/build_angular/src/dev-server/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,13 @@ export class DevServerBuilder implements Builder<DevServerBuilderOptions> {
290290
let sslKey: string | undefined = undefined;
291291
let sslCert: string | undefined = undefined;
292292
if (options.sslKey) {
293-
const keyPath = path.resolve(root, options.sslKey as string);
293+
const keyPath = path.resolve(root, options.sslKey);
294294
if (existsSync(keyPath)) {
295295
sslKey = readFileSync(keyPath, 'utf-8');
296296
}
297297
}
298298
if (options.sslCert) {
299-
const certPath = path.resolve(root, options.sslCert as string);
299+
const certPath = path.resolve(root, options.sslCert);
300300
if (existsSync(certPath)) {
301301
sslCert = readFileSync(certPath, 'utf-8');
302302
}

Diff for: packages/angular_devkit/build_angular/src/karma/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class KarmaBuilder implements Builder<KarmaBuilderSchema> {
8383

8484
// TODO: inside the configs, always use the project root and not the workspace root.
8585
// Until then we pretend the app root is relative (``) but the same as `projectRoot`.
86-
(karmaOptions.buildWebpack.options as any).root = ''; // tslint:disable-line:no-any
86+
karmaOptions.buildWebpack.options.root = '';
8787

8888
// Assign additional karmaConfig options to the local ngapp config
8989
karmaOptions.configFile = karmaConfig;
@@ -112,7 +112,7 @@ export class KarmaBuilder implements Builder<KarmaBuilderSchema> {
112112
) {
113113
let wco: WebpackConfigOptions;
114114

115-
const tsConfigPath = getSystemPath(resolve(root, normalize(options.tsConfig as string)));
115+
const tsConfigPath = getSystemPath(resolve(root, normalize(options.tsConfig)));
116116
const tsConfig = readTsconfig(tsConfigPath);
117117

118118
const projectTs = requireProjectModule(getSystemPath(projectRoot), 'typescript') as typeof ts;

Diff for: packages/angular_devkit/build_angular/src/server/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ export class ServerBuilder implements Builder<BuildWebpackServerSchema> {
106106

107107
if (wco.buildOptions.main || wco.buildOptions.polyfills) {
108108
const typescriptConfigPartial = wco.buildOptions.aot
109-
? getAotConfig(wco, host as virtualFs.Host<Stats>)
110-
: getNonAotConfig(wco, host as virtualFs.Host<Stats>);
109+
? getAotConfig(wco, host)
110+
: getNonAotConfig(wco, host);
111111
webpackConfigs.push(typescriptConfigPartial);
112112
}
113113

Diff for: packages/angular_devkit/build_angular/test/browser/rebuild_spec_large.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8+
// tslint:disable:no-big-function
89

910
import { DefaultTimeout, TestLogger, runTargetSpec } from '@angular-devkit/architect/testing';
1011
import { join, normalize, virtualFs } from '@angular-devkit/core';

Diff for: packages/angular_devkit/build_angular/test/browser/styles_spec_large.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8+
// tslint:disable:no-big-function
89

910
import { DefaultTimeout, runTargetSpec } from '@angular-devkit/architect/testing';
1011
import { normalize, tags, virtualFs } from '@angular-devkit/core';

Diff for: packages/angular_devkit/build_optimizer/src/build-optimizer/build-optimizer_spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8+
// tslint:disable:no-big-function
89
// tslint:disable-next-line:no-implicit-dependencies
910
import { tags } from '@angular-devkit/core';
1011
import { RawSourceMap } from 'source-map';

Diff for: packages/angular_devkit/build_optimizer/src/helpers/ast-utils.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,22 @@ export function collectDeepNodes<T extends ts.Node>(node: ts.Node, kind: ts.Synt
2222
}
2323

2424
export function drilldownNodes<T extends ts.Node>(
25-
startingNode: ts.Node,
26-
path: { prop: string | null, kind: ts.SyntaxKind }[],
25+
startingNode: T,
26+
path: { prop: keyof T, kind: ts.SyntaxKind }[],
2727
): T | null {
28-
let currentNode: T | ts.Node | undefined = startingNode;
28+
let currentNode: T = startingNode;
2929
for (const segment of path) {
3030
if (segment.prop) {
3131
// ts.Node has no index signature, so we need to cast it as any.
32+
const tempNode = currentNode[segment.prop];
33+
if (!tempNode || typeof tempNode != 'object' || currentNode.kind !== segment.kind) {
34+
return null;
35+
}
36+
3237
// tslint:disable-next-line:no-any
33-
currentNode = (currentNode as any)[segment.prop];
34-
}
35-
if (!currentNode || currentNode.kind !== segment.kind) {
36-
return null;
38+
currentNode = tempNode as any as T;
3739
}
3840
}
3941

40-
return currentNode as T;
42+
return currentNode;
4143
}

Diff for: packages/angular_devkit/build_optimizer/src/transforms/class-fold.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function getFoldFileTransformer(program: ts.Program): ts.TransformerFacto
3838
// Check if node is a class to add statements to.
3939
const clazz = classes.find((cl) => cl.classFunction === node);
4040
if (clazz) {
41-
const functionExpression = node as ts.FunctionExpression;
41+
const functionExpression: ts.FunctionExpression = node as ts.FunctionExpression;
4242

4343
const newExpressions = clazz.statements.map((st) =>
4444
ts.createStatement(st.expressionStatement.expression));

Diff for: packages/angular_devkit/build_optimizer/src/transforms/prefix-classes_spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8+
// tslint:disable:no-big-function
89
// tslint:disable-next-line:no-implicit-dependencies
910
import { tags } from '@angular-devkit/core';
1011
import { transformJavascript } from '../helpers/transform-javascript';

Diff for: packages/angular_devkit/build_optimizer/src/transforms/scrub-file_spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8+
// tslint:disable:no-big-function
89
// tslint:disable-next-line:no-implicit-dependencies
910
import { tags } from '@angular-devkit/core';
1011
import { transformJavascript } from '../helpers/transform-javascript';

Diff for: packages/angular_devkit/core/src/json/parser.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -542,13 +542,13 @@ function _readBlanks(context: JsonParserContext): (JsonAstComment | JsonAstMulti
542542
if ((context.mode & JsonParseMode.CommentsAllowed) != 0) {
543543
const comments: (JsonAstComment | JsonAstMultilineComment)[] = [];
544544
while (true) {
545-
let char = context.original[context.position.offset];
545+
const char = context.original[context.position.offset];
546546
if (char == '/' && context.original[context.position.offset + 1] == '*') {
547547
const start = context.position;
548548
// Multi line comment.
549549
_next(context);
550550
_next(context);
551-
char = context.original[context.position.offset];
551+
552552
while (context.original[context.position.offset] != '*'
553553
|| context.original[context.position.offset + 1] != '/') {
554554
_next(context);
@@ -572,7 +572,7 @@ function _readBlanks(context: JsonParserContext): (JsonAstComment | JsonAstMulti
572572
// Multi line comment.
573573
_next(context);
574574
_next(context);
575-
char = context.original[context.position.offset];
575+
576576
while (context.original[context.position.offset] != '\n') {
577577
_next(context);
578578
if (context.position.offset >= context.original.length) {

Diff for: packages/angular_devkit/core/src/json/parser_spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8+
// tslint:disable:no-big-function
89
import { JsonParseMode, parseJson, parseJsonAst } from './parser';
910

1011

Diff for: packages/angular_devkit/core/src/json/schema/registry_spec.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
// tslint:disable:no-any
9-
// tslint:disable:non-null-operator
8+
// tslint:disable:no-any non-null-operator no-big-function
109
import { of as observableOf } from 'rxjs';
1110
import { map, mergeMap } from 'rxjs/operators';
1211
import { CoreSchemaRegistry } from './registry';

Diff for: packages/angular_devkit/core/src/utils/template.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export function templateParser(sourceText: string, fileName: string): TemplateAs
138138
// Optimization that uses the fact that the end of a node is always the beginning of the next
139139
// node, so we keep the positioning of the nodes in memory.
140140
let start = _positionFor(sourceText, offset);
141-
let end = null as Position | null;
141+
let end: Position | null;
142142

143143
const increment = reExpressions.length + 1;
144144
for (let i = 0; i < parsed.length; i += increment) {

Diff for: packages/angular_devkit/core/src/virtual-fs/host/memory.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export class SimpleMemoryHost implements Host<{}> {
179179
protected _delete(path: Path): void {
180180
path = this._toAbsolute(path);
181181
if (this._isDirectory(path)) {
182-
for (const [cachePath, _] of this._cache.entries()) {
182+
for (const [cachePath] of this._cache.entries()) {
183183
if (path.startsWith(cachePath + NormalizedSep)) {
184184
this._cache.delete(cachePath);
185185
}

Diff for: packages/angular_devkit/core/src/virtual-fs/host/record_spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
// tslint:disable:no-any
8+
// tslint:disable:no-any no-big-function
99
import { path } from '../path';
1010
import { fileBuffer } from './buffer';
1111
import { CordHost } from './record';

Diff for: packages/angular_devkit/core/src/workspace/workspace_spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8+
// tslint:disable:no-big-function
89

910
import { tap } from 'rxjs/operators';
1011
import { schema } from '..';

Diff for: packages/angular_devkit/schematics/src/sink/dryrun.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export class DryRunSink extends HostSink {
9494

9595
this._filesToDelete.forEach(path => {
9696
// Check if this is a renaming.
97-
for (const [from, _] of this._filesToRename) {
97+
for (const [from] of this._filesToRename) {
9898
if (from == path) {
9999
// The event is sent later on.
100100
return;
@@ -108,7 +108,7 @@ export class DryRunSink extends HostSink {
108108
});
109109
this._filesToCreate.forEach((content, path) => {
110110
// Check if this is a renaming.
111-
for (const [_, to] of this._filesToRename) {
111+
for (const [, to] of this._filesToRename) {
112112
if (to == path) {
113113
// The event is sent later on.
114114
return;

Diff for: packages/angular_devkit/schematics/src/sink/dryrun_spec.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ describe('DryRunSink', () => {
3333
const files = ['/hello', '/sub/directory/file2', '/sub/file1', '/test'];
3434
const treeFiles: Path[] = [];
3535
tree.visit(path => treeFiles.push(path));
36-
expect(treeFiles.sort()).toEqual(files.map(normalize));
36+
treeFiles.sort();
37+
expect(treeFiles).toEqual(files.map(normalize));
3738

3839
const sink = new DryRunSink(new virtualFs.SimpleMemoryHost());
3940
sink.reporter.pipe(toArray())
@@ -62,7 +63,8 @@ describe('DryRunSink', () => {
6263
const files = ['/hello', '/sub/directory/file2', '/sub/file1', '/test'];
6364
const treeFiles: Path[] = [];
6465
tree.visit(path => treeFiles.push(path));
65-
expect(treeFiles.sort()).toEqual(files.map(normalize));
66+
treeFiles.sort();
67+
expect(treeFiles).toEqual(files.map(normalize));
6668

6769
// Need to create this file on the filesystem, otherwise the commit phase will fail.
6870
const outputHost = new virtualFs.SimpleMemoryHost();

Diff for: packages/angular_devkit/schematics/src/sink/host_spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ describe('FileSystemSink', () => {
3030
const files = ['/hello', '/sub/directory/file2', '/sub/file1', '/test'];
3131
const treeFiles: string[] = [];
3232
tree.visit(path => treeFiles.push(path));
33-
expect(treeFiles.sort()).toEqual(files);
33+
treeFiles.sort();
34+
expect(treeFiles).toEqual(files);
3435

3536
const outputHost = new virtualFs.test.TestHost();
3637
const sink = new HostSink(outputHost);

Diff for: packages/angular_devkit/schematics/src/tree/host-tree_spec.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ describe('FilterHostTree', () => {
4747
const filtered = new FilterHostTree(tree, p => p != '/file2');
4848
const filteredFiles: string[] = [];
4949
filtered.visit(path => filteredFiles.push(path));
50-
expect(filteredFiles.sort()).toEqual(['/file1', '/file3'].map(normalize));
50+
filteredFiles.sort();
51+
expect(filteredFiles).toEqual(['/file1', '/file3'].map(normalize));
5152
expect(filtered.actions.length).toEqual(2);
5253
});
5354

@@ -61,7 +62,8 @@ describe('FilterHostTree', () => {
6162
const filtered2 = new FilterHostTree(filtered, p => p != '/file3');
6263
const filteredFiles: string[] = [];
6364
filtered2.visit(path => filteredFiles.push(path));
64-
expect(filteredFiles.sort()).toEqual(['/file1'].map(normalize));
65+
filteredFiles.sort();
66+
expect(filteredFiles).toEqual(['/file1'].map(normalize));
6567
expect(filtered2.actions.map(a => a.kind)).toEqual(['c']);
6668
});
6769

@@ -77,7 +79,8 @@ describe('FilterHostTree', () => {
7779
const filtered2 = new FilterHostTree(filtered, p => p != '/file3');
7880
const filteredFiles: string[] = [];
7981
filtered2.visit(path => filteredFiles.push(path));
80-
expect(filteredFiles.sort()).toEqual(['/file1'].map(normalize));
82+
filteredFiles.sort();
83+
expect(filteredFiles).toEqual(['/file1'].map(normalize));
8184
expect(filtered2.actions.map(a => a.kind)).toEqual([]);
8285
});
8386

@@ -91,7 +94,8 @@ describe('FilterHostTree', () => {
9194
const filtered2 = new FilterHostTree(filtered, p => p != '/file3');
9295
const filteredFiles: string[] = [];
9396
filtered2.visit(path => filteredFiles.push(path));
94-
expect(filteredFiles.sort()).toEqual(['/dir1/file1'].map(normalize));
97+
filteredFiles.sort();
98+
expect(filteredFiles).toEqual(['/dir1/file1'].map(normalize));
9599
expect(filtered2.actions.map(a => a.kind)).toEqual(['c']);
96100
});
97101

@@ -107,7 +111,8 @@ describe('FilterHostTree', () => {
107111
const filtered2 = new FilterHostTree(filtered, p => p != '/file3');
108112
const filteredFiles: string[] = [];
109113
filtered2.visit(path => filteredFiles.push(path));
110-
expect(filteredFiles.sort()).toEqual(['/dir1/file1'].map(normalize));
114+
filteredFiles.sort();
115+
expect(filteredFiles).toEqual(['/dir1/file1'].map(normalize));
111116
expect(filtered2.actions.map(a => a.kind)).toEqual([]);
112117
});
113118
});

0 commit comments

Comments
 (0)