Skip to content

Commit 7ec13b3

Browse files
author
Andy
committed
Merge pull request microsoft#8828 from Microsoft/no_null_harness
Remove many uses of 'null' in harness
2 parents 6173696 + 5a627ad commit 7ec13b3

File tree

11 files changed

+51
-55
lines changed

11 files changed

+51
-55
lines changed

src/harness/compilerRunner.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// <reference path="harness.ts" />
22
/// <reference path="runnerbase.ts" />
33
/// <reference path="typeWriter.ts" />
4+
// In harness baselines, null is different than undefined. See `generateActual` in `harness.ts`.
45
/* tslint:disable:no-null-keyword */
56

67
const enum CompilerTestType {

src/harness/external/chai.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ declare module chai {
169169
function notEqual(actual: any, expected: any, message?: string): void;
170170
function isTrue(value: any, message?: string): void;
171171
function isFalse(value: any, message?: string): void;
172-
function isNull(value: any, message?: string): void;
173-
function isNotNull(value: any, message?: string): void;
172+
function isOk(actual: any, message?: string): void;
174173
}
175174
}

src/harness/fourslash.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
/// <reference path="harnessLanguageService.ts" />
1919
/// <reference path="harness.ts" />
2020
/// <reference path="fourslashRunner.ts" />
21-
/* tslint:disable:no-null-keyword */
2221

2322
namespace FourSlash {
2423
ts.disableIncrementalParsing = false;
@@ -198,7 +197,7 @@ namespace FourSlash {
198197
public lastKnownMarker: string = "";
199198

200199
// The file that's currently 'opened'
201-
public activeFile: FourSlashFile = null;
200+
public activeFile: FourSlashFile;
202201

203202
// Whether or not we should format on keystrokes
204203
public enableFormatting = true;
@@ -922,7 +921,7 @@ namespace FourSlash {
922921

923922
public verifyCurrentParameterIsletiable(isVariable: boolean) {
924923
const signature = this.getActiveSignatureHelpItem();
925-
assert.isNotNull(signature);
924+
assert.isOk(signature);
926925
assert.equal(isVariable, signature.isVariadic);
927926
}
928927

@@ -1911,7 +1910,7 @@ namespace FourSlash {
19111910
public verifyNavigationItemsCount(expected: number, searchValue: string, matchKind?: string) {
19121911
const items = this.languageService.getNavigateToItems(searchValue);
19131912
let actual = 0;
1914-
let item: ts.NavigateToItem = null;
1913+
let item: ts.NavigateToItem;
19151914

19161915
// Count only the match that match the same MatchKind
19171916
for (let i = 0; i < items.length; i++) {
@@ -2183,7 +2182,7 @@ namespace FourSlash {
21832182
}
21842183

21852184
private findFile(indexOrName: any) {
2186-
let result: FourSlashFile = null;
2185+
let result: FourSlashFile;
21872186
if (typeof indexOrName === "number") {
21882187
const index = <number>indexOrName;
21892188
if (index >= this.testData.files.length) {
@@ -2352,10 +2351,16 @@ ${code}
23522351
const ranges: Range[] = [];
23532352

23542353
// Stuff related to the subfile we're parsing
2355-
let currentFileContent: string = null;
2354+
let currentFileContent: string = undefined;
23562355
let currentFileName = fileName;
23572356
let currentFileOptions: { [s: string]: string } = {};
23582357

2358+
function resetLocalData() {
2359+
currentFileContent = undefined;
2360+
currentFileOptions = {};
2361+
currentFileName = fileName;
2362+
}
2363+
23592364
for (let i = 0; i < lines.length; i++) {
23602365
let line = lines[i];
23612366
const lineLength = line.length;
@@ -2368,7 +2373,7 @@ ${code}
23682373
// Subfile content line
23692374

23702375
// Append to the current subfile content, inserting a newline needed
2371-
if (currentFileContent === null) {
2376+
if (currentFileContent === undefined) {
23722377
currentFileContent = "";
23732378
}
23742379
else {
@@ -2400,10 +2405,7 @@ ${code}
24002405
// Store result file
24012406
files.push(file);
24022407

2403-
// Reset local data
2404-
currentFileContent = null;
2405-
currentFileOptions = {};
2406-
currentFileName = fileName;
2408+
resetLocalData();
24072409
}
24082410

24092411
currentFileName = basePath + "/" + match[2];
@@ -2430,10 +2432,7 @@ ${code}
24302432
// Store result file
24312433
files.push(file);
24322434

2433-
// Reset local data
2434-
currentFileContent = null;
2435-
currentFileOptions = {};
2436-
currentFileName = fileName;
2435+
resetLocalData();
24372436
}
24382437
}
24392438
}
@@ -2498,7 +2497,7 @@ ${code}
24982497

24992498
if (markerValue === undefined) {
25002499
reportError(fileName, location.sourceLine, location.sourceColumn, "Object markers can not be empty");
2501-
return null;
2500+
return undefined;
25022501
}
25032502

25042503
const marker: Marker = {
@@ -2527,7 +2526,7 @@ ${code}
25272526
if (markerMap[name] !== undefined) {
25282527
const message = "Marker '" + name + "' is duplicated in the source file contents.";
25292528
reportError(marker.fileName, location.sourceLine, location.sourceColumn, message);
2530-
return null;
2529+
return undefined;
25312530
}
25322531
else {
25332532
markerMap[name] = marker;
@@ -2546,7 +2545,7 @@ ${code}
25462545
let output = "";
25472546

25482547
/// The current marker (or maybe multi-line comment?) we're parsing, possibly
2549-
let openMarker: LocationInformation = null;
2548+
let openMarker: LocationInformation = undefined;
25502549

25512550
/// A stack of the open range markers that are still unclosed
25522551
const openRanges: RangeLocationInformation[] = [];
@@ -2654,7 +2653,7 @@ ${code}
26542653
difference += i + 1 - openMarker.sourcePosition;
26552654

26562655
// Reset the state
2657-
openMarker = null;
2656+
openMarker = undefined;
26582657
state = State.none;
26592658
}
26602659
break;
@@ -2676,7 +2675,7 @@ ${code}
26762675
difference += i + 1 - openMarker.sourcePosition;
26772676

26782677
// Reset the state
2679-
openMarker = null;
2678+
openMarker = undefined;
26802679
state = State.none;
26812680
}
26822681
else if (validMarkerChars.indexOf(currentChar) < 0) {
@@ -2688,7 +2687,7 @@ ${code}
26882687
// Bail out the text we've gathered so far back into the output
26892688
flush(i);
26902689
lastNormalCharPosition = i;
2691-
openMarker = null;
2690+
openMarker = undefined;
26922691

26932692
state = State.none;
26942693
}
@@ -2719,7 +2718,7 @@ ${code}
27192718
reportError(fileName, openRange.sourceLine, openRange.sourceColumn, "Unterminated range.");
27202719
}
27212720

2722-
if (openMarker !== null) {
2721+
if (openMarker) {
27232722
reportError(fileName, openMarker.sourceLine, openMarker.sourceColumn, "Unterminated marker.");
27242723
}
27252724

src/harness/fourslashRunner.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
///<reference path="fourslash.ts" />
22
///<reference path="harness.ts"/>
33
///<reference path="runnerbase.ts" />
4-
/* tslint:disable:no-null-keyword */
54

65
const enum FourSlashTestType {
76
Native,

src/harness/harness.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
/// <reference path="external\chai.d.ts"/>
2424
/// <reference path="sourceMapRecorder.ts"/>
2525
/// <reference path="runnerbase.ts"/>
26-
/* tslint:disable:no-null-keyword */
2726

2827
// Block scoped definitions work poorly for global variables, temporarily enable var
2928
/* tslint:disable:no-var-keyword */
@@ -32,7 +31,7 @@
3231
var _chai: typeof chai = require("chai");
3332
var assert: typeof _chai.assert = _chai.assert;
3433
declare var __dirname: string; // Node-specific
35-
var global = <any>Function("return this").call(null);
34+
var global = <any>Function("return this").call(undefined);
3635
/* tslint:enable:no-var-keyword */
3736

3837
namespace Utils {
@@ -558,15 +557,9 @@ namespace Harness {
558557
}
559558

560559
export function directoryName(path: string) {
561-
let dirPath = pathModule.dirname(path);
562-
560+
const dirPath = pathModule.dirname(path);
563561
// Node will just continue to repeat the root path, rather than return null
564-
if (dirPath === path) {
565-
dirPath = null;
566-
}
567-
else {
568-
return dirPath;
569-
}
562+
return dirPath === path ? undefined : dirPath;
570563
}
571564

572565
export let listFiles: typeof IO.listFiles = (path, spec?, options?) => {
@@ -634,7 +627,7 @@ namespace Harness {
634627
xhr.send();
635628
}
636629
catch (e) {
637-
return { status: 404, responseText: null };
630+
return { status: 404, responseText: undefined };
638631
}
639632

640633
return waitForXHR(xhr);
@@ -651,7 +644,7 @@ namespace Harness {
651644
}
652645
catch (e) {
653646
log(`XHR Error: ${e}`);
654-
return { status: 500, responseText: null };
647+
return { status: 500, responseText: undefined };
655648
}
656649

657650
return waitForXHR(xhr);
@@ -663,7 +656,7 @@ namespace Harness {
663656
}
664657

665658
export function deleteFile(path: string) {
666-
Http.writeToServerSync(serverRoot + path, "DELETE", null);
659+
Http.writeToServerSync(serverRoot + path, "DELETE");
667660
}
668661

669662
export function directoryExists(path: string): boolean {
@@ -674,7 +667,7 @@ namespace Harness {
674667
let dirPath = path;
675668
// root of the server
676669
if (dirPath.match(/localhost:\d+$/) || dirPath.match(/localhost:\d+\/$/)) {
677-
dirPath = null;
670+
dirPath = undefined;
678671
// path + fileName
679672
}
680673
else if (dirPath.indexOf(".") === -1) {
@@ -722,7 +715,7 @@ namespace Harness {
722715
return response.responseText;
723716
}
724717
else {
725-
return null;
718+
return undefined;
726719
}
727720
}
728721

@@ -1418,7 +1411,9 @@ namespace Harness {
14181411
const opts: CompilerSettings = {};
14191412

14201413
let match: RegExpExecArray;
1421-
while ((match = optionRegex.exec(content)) != null) {
1414+
/* tslint:disable:no-null-keyword */
1415+
while ((match = optionRegex.exec(content)) !== null) {
1416+
/* tslint:enable:no-null-keyword */
14221417
opts[match[1]] = match[2];
14231418
}
14241419

@@ -1435,9 +1430,9 @@ namespace Harness {
14351430
const lines = Utils.splitContentByNewlines(code);
14361431

14371432
// Stuff related to the subfile we're parsing
1438-
let currentFileContent: string = null;
1433+
let currentFileContent: string = undefined;
14391434
let currentFileOptions: any = {};
1440-
let currentFileName: any = null;
1435+
let currentFileName: any = undefined;
14411436
let refs: string[] = [];
14421437

14431438
for (let i = 0; i < lines.length; i++) {
@@ -1465,7 +1460,7 @@ namespace Harness {
14651460
testUnitData.push(newTestFile);
14661461

14671462
// Reset local data
1468-
currentFileContent = null;
1463+
currentFileContent = undefined;
14691464
currentFileOptions = {};
14701465
currentFileName = testMetaData[2];
14711466
refs = [];
@@ -1478,7 +1473,7 @@ namespace Harness {
14781473
else {
14791474
// Subfile content line
14801475
// Append to the current subfile content, inserting a newline needed
1481-
if (currentFileContent === null) {
1476+
if (currentFileContent === undefined) {
14821477
currentFileContent = "";
14831478
}
14841479
else {
@@ -1601,7 +1596,9 @@ namespace Harness {
16011596

16021597
// Store the content in the 'local' folder so we
16031598
// can accept it later (manually)
1599+
/* tslint:disable:no-null-keyword */
16041600
if (actual !== null) {
1601+
/* tslint:enable:no-null-keyword */
16051602
IO.writeFile(actualFileName, actual);
16061603
}
16071604

@@ -1618,7 +1615,9 @@ namespace Harness {
16181615

16191616
const refFileName = referencePath(relativeFileName, opts && opts.Baselinefolder, opts && opts.Subfolder);
16201617

1618+
/* tslint:disable:no-null-keyword */
16211619
if (actual === null) {
1620+
/* tslint:enable:no-null-keyword */
16221621
actual = "<no content>";
16231622
}
16241623

src/harness/harnessLanguageService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ namespace Harness.LanguageService {
172172
*/
173173
public positionToLineAndCharacter(fileName: string, position: number): ts.LineAndCharacter {
174174
const script: ScriptInfo = this.fileNameToScript[fileName];
175-
assert.isNotNull(script);
175+
assert.isOk(script);
176176

177177
return ts.computeLineAndCharacterOfPosition(script.getLineMap(), position);
178178
}

src/harness/loggedIO.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/// <reference path="..\..\src\compiler\sys.ts" />
22
/// <reference path="..\..\src\harness\harness.ts" />
33
/// <reference path="..\..\src\harness\runnerbase.ts" />
4-
/* tslint:disable:no-null-keyword */
54

65
interface FileInformation {
76
contents: string;
@@ -94,7 +93,7 @@ namespace Playback {
9493
return lookup[s] = func(s);
9594
});
9695
run.reset = () => {
97-
lookup = null;
96+
lookup = undefined;
9897
};
9998

10099
return run;
@@ -170,7 +169,8 @@ namespace Playback {
170169
path => callAndRecord(underlying.fileExists(path), recordLog.fileExists, { path }),
171170
memoize(path => {
172171
// If we read from the file, it must exist
173-
if (findResultByPath(wrapper, replayLog.filesRead, path, null) !== null) {
172+
const noResult = {};
173+
if (findResultByPath(wrapper, replayLog.filesRead, path, noResult) !== noResult) {
174174
return true;
175175
}
176176
else {

src/harness/projectsRunner.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
///<reference path="harness.ts" />
22
///<reference path="runnerbase.ts" />
3-
/* tslint:disable:no-null-keyword */
43

54
// Test case is json of below type in tests/cases/project/
65
interface ProjectRunnerTestCase {
@@ -53,7 +52,7 @@ class ProjectRunner extends RunnerBase {
5352
private runProjectTestCase(testCaseFileName: string) {
5453
let testCase: ProjectRunnerTestCase & ts.CompilerOptions;
5554

56-
let testFileText: string = null;
55+
let testFileText: string;
5756
try {
5857
testFileText = Harness.IO.readFile(testCaseFileName);
5958
}

src/harness/runner.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
/// <reference path="rwcRunner.ts" />
2121
/// <reference path="harness.ts" />
2222

23-
/* tslint:disable:no-null-keyword */
24-
2523
let runners: RunnerBase[] = [];
2624
let iterations = 1;
2725

0 commit comments

Comments
 (0)