Skip to content

Commit e8b29a9

Browse files
committed
refactor: add a linting rule to disallow "!" non-null op
And fix all files that used it.
1 parent 03aaf58 commit e8b29a9

File tree

24 files changed

+195
-45
lines changed

24 files changed

+195
-45
lines changed

lib/packages.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,14 @@ function _getHashOf(pkg: PackageInfo): string {
5757
hashCache[pkg.name] = (md5Stream.read() as Buffer).toString('hex');
5858
}
5959

60-
if (!hashCache[pkg.name]) {
60+
const value = hashCache[pkg.name];
61+
if (!value) {
6162
// Protect against circular dependency.
6263
throw new Error('Circular dependency detected between the following packages: '
6364
+ Object.keys(hashCache).filter(key => hashCache[key] == null).join(', '));
6465
}
6566

66-
return hashCache[pkg.name] !;
67+
return value;
6768
}
6869

6970

packages/angular_devkit/build_optimizer/src/helpers/transform-javascript.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,29 @@ export function transformJavascript(
8383
k, ts.createSourceFile(k, v, ts.ScriptTarget.ES2015)));
8484

8585
const host: ts.CompilerHost = {
86-
getSourceFile: (fileName) => sourcesMap.get(fileName)!,
86+
getSourceFile: (fileName) => {
87+
const sourceFile = sourcesMap.get(fileName);
88+
if (!sourceFile) {
89+
throw new Error(`File ${fileName} does not have a sourceFile.`);
90+
}
91+
92+
return sourceFile;
93+
},
8794
getDefaultLibFileName: () => defaultLibFileName,
8895
getCurrentDirectory: () => '',
8996
getDirectories: () => [],
9097
getCanonicalFileName: (fileName) => fileName,
9198
useCaseSensitiveFileNames: () => true,
9299
getNewLine: () => '\n',
93100
fileExists: (fileName) => fileMap.has(fileName),
94-
readFile: (fileName) => fileMap.has(fileName) ? fileMap.get(fileName)! : '',
101+
readFile: (fileName) => {
102+
const content = fileMap.get(fileName);
103+
if (!content) {
104+
throw new Error(`File ${fileName} does not exist.`);
105+
}
106+
107+
return content;
108+
},
95109
writeFile: (fileName, text) => outputs.set(fileName, text),
96110
};
97111

packages/angular_devkit/schematics/bin/schematics.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ function usage(exitCode = 0): never {
6868
function parseSchematicName(str: string | null): { collection: string, schematic: string } {
6969
let collection = '@schematics/angular';
7070

71-
if (!str) {
71+
if (!str || str === null) {
7272
usage(1);
7373
}
7474

75-
let schematic: string = str !;
75+
let schematic: string = str as string;
7676
if (schematic.indexOf(':') != -1) {
7777
[collection, schematic] = schematic.split(':', 2);
7878

@@ -112,8 +112,8 @@ const engine = new SchematicEngine(engineHost);
112112

113113
// Add support for schemaJson.
114114
engineHost.registerOptionsTransform((schematic: FileSystemSchematicDesc, options: {}) => {
115-
if (schematic.schema) {
116-
const SchemaMetaClass = SchemaClassFactory<{}>(schematic.schemaJson !);
115+
if (schematic.schema && schematic.schemaJson) {
116+
const SchemaMetaClass = SchemaClassFactory<{}>(schematic.schemaJson);
117117
const schemaClass = new SchemaMetaClass(options);
118118

119119
return schemaClass.$$root();

packages/angular_devkit/schematics/src/engine/schematic_spec.ts

Lines changed: 1 addition & 0 deletions
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:non-null-operator
89
import { Observable } from 'rxjs/Observable';
910
import 'rxjs/add/operator/toArray';
1011
import 'rxjs/add/operator/toPromise';

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

Lines changed: 1 addition & 0 deletions
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:non-null-operator
89
import { Observable } from 'rxjs/Observable';
910
import 'rxjs/add/operator/toPromise';
1011
import { Rule, SchematicContext, Source } from '../engine/interface';

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

Lines changed: 1 addition & 0 deletions
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:non-null-operator
89
import { Observable } from 'rxjs/Observable';
910
import 'rxjs/add/operator/toPromise';
1011
import { SchematicContext } from '../engine/interface';

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ export class VirtualTree implements Tree {
154154
this.set(new SimpleFileEntry(path, content as Buffer));
155155
}
156156
protected _rename(path: SchematicPath, to: SchematicPath, action?: Action, force = false) {
157-
if (!this._cacheMap.has(path)) {
157+
const entry = this.get(path);
158+
if (!entry) {
158159
throw new FileDoesNotExistException(path);
159160
}
160161
if (this._cacheMap.has(to) && !force) {
@@ -167,7 +168,6 @@ export class VirtualTree implements Tree {
167168
this._actions.rename(path, to);
168169
}
169170

170-
const entry = this.get(path) !;
171171
this.set(new SimpleFileEntry(to, entry.content));
172172
this._cacheMap.delete(path);
173173
}

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

Lines changed: 1 addition & 0 deletions
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:non-null-operator
89
import { FileAlreadyExistException, FileDoesNotExistException } from '../exception/exception';
910
import { FileSystemTree } from './filesystem';
1011
import { FileEntry, MergeStrategy } from './interface';

packages/angular_devkit/schematics/src/utility/update-buffer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,10 @@ export class UpdateBuffer {
210210
}
211211

212212
if (start == h.end && h.next !== null) {
213-
return [h !, h.next !];
213+
return [h, h.next];
214214
}
215215

216-
return [h !, h.slice(start) !];
216+
return [h, h.slice(start)];
217217
}
218218

219219
get length(): number {

packages/schematics/angular/application/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export default function (options: ApplicationOptions): Rule {
9898
'dot': '.',
9999
...options as object,
100100
}),
101-
move(options.directory),
101+
move(options.directory),
102102
])),
103103
schematic('module', {
104104
name: 'app',
@@ -115,7 +115,7 @@ export default function (options: ApplicationOptions): Rule {
115115
flat: true,
116116
...componentOptions,
117117
}),
118-
addBootstrapToNgModule(options.directory),
118+
addBootstrapToNgModule(options.directory),
119119
mergeWith(
120120
apply(url('./other-files'), [
121121
componentOptions.inlineTemplate ? filter(path => !path.endsWith('.html')) : noop(),

0 commit comments

Comments
 (0)