Skip to content

Commit 12f3d0d

Browse files
author
Andy
authored
Enable --strictPropertyInitialization (microsoft#27558)
* Enable --strictPropertyInitialization * Code review
1 parent 6bb1ff2 commit 12f3d0d

26 files changed

+171
-168
lines changed

src/harness/client.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace ts.server {
3737
private sequence = 0;
3838
private lineMaps: Map<number[]> = createMap<number[]>();
3939
private messages: string[] = [];
40-
private lastRenameEntry: RenameEntry;
40+
private lastRenameEntry: RenameEntry | undefined;
4141

4242
constructor(private host: SessionClientHost) {
4343
}
@@ -431,7 +431,7 @@ namespace ts.server {
431431
this.getRenameInfo(fileName, position, findInStrings, findInComments);
432432
}
433433

434-
return this.lastRenameEntry.locations;
434+
return this.lastRenameEntry!.locations;
435435
}
436436

437437
private decodeNavigationBarItems(items: protocol.NavigationBarItem[] | undefined, fileName: string, lineMap: number[]): NavigationBarItem[] {

src/harness/fakes.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ namespace fakes {
215215

216216
private _setParentNodes: boolean;
217217
private _sourceFiles: collections.SortedMap<string, ts.SourceFile>;
218-
private _parseConfigHost: ParseConfigHost;
218+
private _parseConfigHost: ParseConfigHost | undefined;
219219
private _newLine: string;
220220

221221
constructor(sys: System | vfs.FileSystem, options = ts.getDefaultCompilerOptions(), setParentNodes = false) {

src/harness/fourslash.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ namespace FourSlash {
158158
public lastKnownMarker = "";
159159

160160
// The file that's currently 'opened'
161-
public activeFile: FourSlashFile;
161+
public activeFile!: FourSlashFile;
162162

163163
// Whether or not we should format on keystrokes
164164
public enableFormatting = true;
@@ -855,9 +855,9 @@ namespace FourSlash {
855855
}
856856

857857
/** Use `getProgram` instead of accessing this directly. */
858-
private _program: ts.Program;
858+
private _program: ts.Program | undefined;
859859
/** Use `getChecker` instead of accessing this directly. */
860-
private _checker: ts.TypeChecker;
860+
private _checker: ts.TypeChecker | undefined;
861861

862862
private getProgram(): ts.Program {
863863
return this._program || (this._program = this.languageService.getProgram()!); // TODO: GH#18217
@@ -3734,7 +3734,7 @@ namespace FourSlashInterface {
37343734
}
37353735

37363736
export class VerifyNegatable {
3737-
public not: VerifyNegatable;
3737+
public not: VerifyNegatable | undefined;
37383738

37393739
constructor(protected state: FourSlash.TestState, private negative = false) {
37403740
if (!negative) {

src/harness/harnessLanguageService.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ namespace Harness.LanguageService {
289289
class ShimLanguageServiceHost extends LanguageServiceAdapterHost implements ts.LanguageServiceShimHost, ts.CoreServicesShimHost {
290290
private nativeHost: NativeLanguageServiceHost;
291291

292-
public getModuleResolutionsForFile: (fileName: string) => string;
293-
public getTypeReferenceDirectiveResolutionsForFile: (fileName: string) => string;
292+
public getModuleResolutionsForFile: ((fileName: string) => string) | undefined;
293+
public getTypeReferenceDirectiveResolutionsForFile: ((fileName: string) => string) | undefined;
294294

295295
constructor(preprocessToResolve: boolean, cancellationToken?: ts.HostCancellationToken, options?: ts.CompilerOptions) {
296296
super(cancellationToken, options);
@@ -639,7 +639,7 @@ namespace Harness.LanguageService {
639639

640640
// Server adapter
641641
class SessionClientHost extends NativeLanguageServiceHost implements ts.server.SessionClientHost {
642-
private client: ts.server.SessionClient;
642+
private client!: ts.server.SessionClient;
643643

644644
constructor(cancellationToken: ts.HostCancellationToken | undefined, settings: ts.CompilerOptions | undefined) {
645645
super(cancellationToken, settings);

src/harness/typeWriter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface TypeWriterResult {
2121
}
2222

2323
class TypeWriterWalker {
24-
currentSourceFile: ts.SourceFile;
24+
currentSourceFile!: ts.SourceFile;
2525

2626
private checker: ts.TypeChecker;
2727

src/harness/virtualFileSystemWithWatch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ interface Array<T> {}`
341341
private readonly currentDirectory: string;
342342
private readonly dynamicPriorityWatchFile: HostWatchFile | undefined;
343343
private readonly customRecursiveWatchDirectory: HostWatchDirectory | undefined;
344-
public require: (initialPath: string, moduleName: string) => server.RequireResult;
344+
public require: ((initialPath: string, moduleName: string) => server.RequireResult) | undefined;
345345

346346
constructor(public withSafeList: boolean, public useCaseSensitiveFileNames: boolean, executingFilePath: string, currentDirectory: string, fileOrFolderorSymLinkList: ReadonlyArray<FileOrFolderOrSymLink>, public readonly newLine = "\n", public readonly useWindowsStylePath?: boolean, private readonly environmentVariables?: Map<string>) {
347347
this.getCanonicalFileName = createGetCanonicalFileName(useCaseSensitiveFileNames);

src/server/editorServices.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ namespace ts.server {
479479
*/
480480
private readonly openFilesWithNonRootedDiskPath = createMap<ScriptInfo>();
481481

482-
private compilerOptionsForInferredProjects: CompilerOptions;
482+
private compilerOptionsForInferredProjects: CompilerOptions | undefined;
483483
private compilerOptionsForInferredProjectsPerProjectRoot = createMap<CompilerOptions>();
484484
/**
485485
* Project size for configured or external projects
@@ -501,7 +501,7 @@ namespace ts.server {
501501

502502
private pendingProjectUpdates = createMap<Project>();
503503
/* @internal */
504-
pendingEnsureProjectForOpenFiles: boolean;
504+
pendingEnsureProjectForOpenFiles = false;
505505

506506
readonly currentDirectory: NormalizedPath;
507507
readonly toCanonicalFileName: (f: string) => string;
@@ -1936,7 +1936,7 @@ namespace ts.server {
19361936
}
19371937

19381938
private createInferredProject(currentDirectory: string | undefined, isSingleInferredProject?: boolean, projectRootPath?: NormalizedPath): InferredProject {
1939-
const compilerOptions = projectRootPath && this.compilerOptionsForInferredProjectsPerProjectRoot.get(projectRootPath) || this.compilerOptionsForInferredProjects;
1939+
const compilerOptions = projectRootPath && this.compilerOptionsForInferredProjectsPerProjectRoot.get(projectRootPath) || this.compilerOptionsForInferredProjects!; // TODO: GH#18217
19401940
const project = new InferredProject(this, this.documentRegistry, compilerOptions, projectRootPath, currentDirectory, this.currentPluginConfigOverrides);
19411941
if (isSingleInferredProject) {
19421942
this.inferredProjects.unshift(project);

src/server/project.ts

+20-20
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ namespace ts.server {
112112
export abstract class Project implements LanguageServiceHost, ModuleResolutionHost {
113113
private rootFiles: ScriptInfo[] = [];
114114
private rootFilesMap: Map<ProjectRoot> = createMap<ProjectRoot>();
115-
private program: Program;
116-
private externalFiles: SortedReadonlyArray<string>;
117-
private missingFilesMap: Map<FileWatcher>;
115+
private program: Program | undefined;
116+
private externalFiles: SortedReadonlyArray<string> | undefined;
117+
private missingFilesMap: Map<FileWatcher> | undefined;
118118
private plugins: PluginModuleWithName[] = [];
119119

120120
/*@internal*/
@@ -141,7 +141,7 @@ namespace ts.server {
141141
readonly realpath?: (path: string) => string;
142142

143143
/*@internal*/
144-
hasInvalidatedResolution: HasInvalidatedResolution;
144+
hasInvalidatedResolution: HasInvalidatedResolution | undefined;
145145

146146
/*@internal*/
147147
resolutionCache: ResolutionCache;
@@ -154,7 +154,7 @@ namespace ts.server {
154154
/**
155155
* Set of files that was returned from the last call to getChangesSinceVersion.
156156
*/
157-
private lastReportedFileNames: Map<true>;
157+
private lastReportedFileNames: Map<true> | undefined;
158158
/**
159159
* Last version that was reported.
160160
*/
@@ -512,8 +512,8 @@ namespace ts.server {
512512
return [];
513513
}
514514
updateProjectIfDirty(this);
515-
this.builderState = BuilderState.create(this.program, this.projectService.toCanonicalFileName, this.builderState);
516-
return mapDefined(BuilderState.getFilesAffectedBy(this.builderState, this.program, scriptInfo.path, this.cancellationToken, data => this.projectService.host.createHash!(data)), // TODO: GH#18217
515+
this.builderState = BuilderState.create(this.program!, this.projectService.toCanonicalFileName, this.builderState);
516+
return mapDefined(BuilderState.getFilesAffectedBy(this.builderState, this.program!, scriptInfo.path, this.cancellationToken, data => this.projectService.host.createHash!(data)), // TODO: GH#18217
517517
sourceFile => this.shouldEmitFile(this.projectService.getScriptInfoForPath(sourceFile.path)!) ? sourceFile.fileName : undefined);
518518
}
519519

@@ -594,7 +594,7 @@ namespace ts.server {
594594

595595
/* @internal */
596596
getSourceFileOrConfigFile(path: Path): SourceFile | undefined {
597-
const options = this.program.getCompilerOptions();
597+
const options = this.program!.getCompilerOptions();
598598
return path === options.configFilePath ? options.configFile : this.getSourceFile(path);
599599
}
600600

@@ -681,7 +681,7 @@ namespace ts.server {
681681
// if language service is not enabled - return just root files
682682
return this.rootFiles;
683683
}
684-
return map(this.program.getSourceFiles(), sourceFile => {
684+
return map(this.program!.getSourceFiles(), sourceFile => {
685685
const scriptInfo = this.projectService.getScriptInfoForPath(sourceFile.resolvedPath);
686686
Debug.assert(!!scriptInfo, "getScriptInfo", () => `scriptInfo for a file '${sourceFile.fileName}' Path: '${sourceFile.path}' / '${sourceFile.resolvedPath}' is missing.`);
687687
return scriptInfo!;
@@ -749,7 +749,7 @@ namespace ts.server {
749749
}
750750

751751
containsScriptInfo(info: ScriptInfo): boolean {
752-
return this.isRoot(info) || (this.program && this.program.getSourceFileByPath(info.path) !== undefined);
752+
return this.isRoot(info) || (!!this.program && this.program.getSourceFileByPath(info.path) !== undefined);
753753
}
754754

755755
containsFile(filename: NormalizedPath, requireOpen?: boolean): boolean {
@@ -845,7 +845,7 @@ namespace ts.server {
845845
// (can reuse cached imports for files that were not changed)
846846
// 4. compilation settings were changed in the way that might affect module resolution - drop all caches and collect all data from the scratch
847847
if (hasNewProgram || changedFiles.length) {
848-
this.lastCachedUnresolvedImportsList = getUnresolvedImports(this.program, this.cachedUnresolvedImportsPerFile);
848+
this.lastCachedUnresolvedImportsList = getUnresolvedImports(this.program!, this.cachedUnresolvedImportsPerFile);
849849
}
850850

851851
this.projectService.typingsCache.enqueueInstallTypingsForProject(this, this.lastCachedUnresolvedImportsList, hasAddedorRemovedFiles);
@@ -872,7 +872,7 @@ namespace ts.server {
872872
}
873873

874874
/* @internal */
875-
getCurrentProgram() {
875+
getCurrentProgram(): Program | undefined {
876876
return this.program;
877877
}
878878

@@ -911,7 +911,7 @@ namespace ts.server {
911911
}
912912

913913
oldProgram.forEachResolvedProjectReference((resolvedProjectReference, resolvedProjectReferencePath) => {
914-
if (resolvedProjectReference && !this.program.getResolvedProjectReferenceByPath(resolvedProjectReferencePath)) {
914+
if (resolvedProjectReference && !this.program!.getResolvedProjectReferenceByPath(resolvedProjectReferencePath)) {
915915
this.detachScriptInfoFromProject(resolvedProjectReference.sourceFile.fileName);
916916
}
917917
});
@@ -967,8 +967,8 @@ namespace ts.server {
967967
this.getCachedDirectoryStructureHost().addOrDeleteFile(fileName, missingFilePath, eventKind);
968968
}
969969

970-
if (eventKind === FileWatcherEventKind.Created && this.missingFilesMap.has(missingFilePath)) {
971-
this.missingFilesMap.delete(missingFilePath);
970+
if (eventKind === FileWatcherEventKind.Created && this.missingFilesMap!.has(missingFilePath)) {
971+
this.missingFilesMap!.delete(missingFilePath);
972972
fileWatcher.close();
973973

974974
// When a missing file is created, we should update the graph.
@@ -983,7 +983,7 @@ namespace ts.server {
983983
}
984984

985985
private isWatchedMissingFile(path: Path) {
986-
return this.missingFilesMap && this.missingFilesMap.has(path);
986+
return !!this.missingFilesMap && this.missingFilesMap.has(path);
987987
}
988988

989989
getScriptInfoForNormalizedPath(fileName: NormalizedPath): ScriptInfo | undefined {
@@ -1344,22 +1344,22 @@ namespace ts.server {
13441344
* Otherwise it will create an InferredProject.
13451345
*/
13461346
export class ConfiguredProject extends Project {
1347-
private typeAcquisition: TypeAcquisition;
1347+
private typeAcquisition!: TypeAcquisition; // TODO: GH#18217
13481348
/* @internal */
13491349
configFileWatcher: FileWatcher | undefined;
13501350
private directoriesWatchedForWildcards: Map<WildcardDirectoryWatcher> | undefined;
13511351
readonly canonicalConfigFilePath: NormalizedPath;
13521352

13531353
/* @internal */
1354-
pendingReload: ConfigFileProgramReloadLevel;
1354+
pendingReload: ConfigFileProgramReloadLevel | undefined;
13551355
/* @internal */
13561356
pendingReloadReason: string | undefined;
13571357

13581358
/*@internal*/
13591359
configFileSpecs: ConfigFileSpecs | undefined;
13601360

13611361
/*@internal*/
1362-
canConfigFileJsonReportNoInputFiles: boolean;
1362+
canConfigFileJsonReportNoInputFiles = false;
13631363

13641364
/** Ref count to the project when opened from external project */
13651365
private externalProjectRefCount = 0;
@@ -1590,7 +1590,7 @@ namespace ts.server {
15901590
*/
15911591
export class ExternalProject extends Project {
15921592
excludedFiles: ReadonlyArray<NormalizedPath> = [];
1593-
private typeAcquisition: TypeAcquisition;
1593+
private typeAcquisition!: TypeAcquisition; // TODO: GH#18217
15941594
/*@internal*/
15951595
constructor(public externalProjectName: string,
15961596
projectService: ProjectService,

src/server/scriptInfo.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ namespace ts.server {
3636
/**
3737
* True if the text is for the file thats open in the editor
3838
*/
39-
public isOpen: boolean;
39+
public isOpen = false;
4040
/**
4141
* True if the text present is the text from the file on the disk
4242
*/
43-
private ownFileText: boolean;
43+
private ownFileText = false;
4444
/**
4545
* True when reloading contents of file from the disk is pending
4646
*/
47-
private pendingReloadFromDisk: boolean;
47+
private pendingReloadFromDisk = false;
4848

4949
constructor(private readonly host: ServerHost, private readonly fileName: NormalizedPath, initialVersion: ScriptInfoVersion | undefined, private readonly info: ScriptInfo) {
5050
this.version = initialVersion || { svc: 0, text: 0 };
@@ -279,7 +279,7 @@ namespace ts.server {
279279
private realpath: Path | undefined;
280280

281281
/*@internal*/
282-
cacheSourceFile: DocumentRegistrySourceFileCache;
282+
cacheSourceFile: DocumentRegistrySourceFileCache | undefined;
283283

284284
/*@internal*/
285285
mTime?: number;

src/server/scriptVersionCache.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ namespace ts.server {
4141
// path to start of range
4242
private readonly startPath: LineCollection[];
4343
private readonly endBranch: LineCollection[] = [];
44-
private branchNode: LineNode;
44+
private branchNode: LineNode | undefined;
4545
// path to current node
4646
private readonly stack: LineNode[];
4747
private state = CharRangeSection.Entire;
48-
private lineCollectionAtBranch: LineCollection;
48+
private lineCollectionAtBranch: LineCollection | undefined;
4949
private initialText = "";
5050
private trailingText = "";
5151

@@ -383,7 +383,7 @@ namespace ts.server {
383383
}
384384

385385
export class LineIndex {
386-
root: LineNode;
386+
root!: LineNode;
387387
// set this to true to check each edit for accuracy
388388
checkEdits = false;
389389

src/server/session.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ namespace ts.server {
516516
protected projectService: ProjectService;
517517
private changeSeq = 0;
518518

519-
private currentRequestId: number;
519+
private currentRequestId!: number;
520520
private errorCheck: MultistepOperation;
521521

522522
protected host: ServerHost;

src/services/formatting/formattingContext.ts

+10-16
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ namespace ts.formatting {
1010
}
1111

1212
export class FormattingContext {
13-
public currentTokenSpan: TextRangeWithKind;
14-
public nextTokenSpan: TextRangeWithKind;
15-
public contextNode: Node;
16-
public currentTokenParent: Node;
17-
public nextTokenParent: Node;
13+
public currentTokenSpan!: TextRangeWithKind;
14+
public nextTokenSpan!: TextRangeWithKind;
15+
public contextNode!: Node;
16+
public currentTokenParent!: Node;
17+
public nextTokenParent!: Node;
1818

1919
private contextNodeAllOnSameLine: boolean | undefined;
2020
private nextNodeAllOnSameLine: boolean | undefined;
@@ -26,17 +26,11 @@ namespace ts.formatting {
2626
}
2727

2828
public updateContext(currentRange: TextRangeWithKind, currentTokenParent: Node, nextRange: TextRangeWithKind, nextTokenParent: Node, commonParent: Node) {
29-
Debug.assert(currentRange !== undefined, "currentTokenSpan is null");
30-
Debug.assert(currentTokenParent !== undefined, "currentTokenParent is null");
31-
Debug.assert(nextRange !== undefined, "nextTokenSpan is null");
32-
Debug.assert(nextTokenParent !== undefined, "nextTokenParent is null");
33-
Debug.assert(commonParent !== undefined, "commonParent is null");
34-
35-
this.currentTokenSpan = currentRange;
36-
this.currentTokenParent = currentTokenParent;
37-
this.nextTokenSpan = nextRange;
38-
this.nextTokenParent = nextTokenParent;
39-
this.contextNode = commonParent;
29+
this.currentTokenSpan = Debug.assertDefined(currentRange);
30+
this.currentTokenParent = Debug.assertDefined(currentTokenParent);
31+
this.nextTokenSpan = Debug.assertDefined(nextRange);
32+
this.nextTokenParent = Debug.assertDefined(nextTokenParent);
33+
this.contextNode = Debug.assertDefined(commonParent);
4034

4135
// drop cached results
4236
this.contextNodeAllOnSameLine = undefined;

0 commit comments

Comments
 (0)