Skip to content

Commit aabfcfb

Browse files
author
Andy
authored
Merge pull request #11737 from Microsoft/not_implemented
Add noop, notImplemented, and notYetImplemented helpers
2 parents 4fbbbed + ca97006 commit aabfcfb

18 files changed

+88
-100
lines changed

src/compiler/core.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,14 @@ namespace ts {
869869
return Array.isArray ? Array.isArray(value) : value instanceof Array;
870870
}
871871

872+
/** Does nothing. */
873+
export function noop(): void {}
874+
875+
/** Throws an error because a function is not implemented. */
876+
export function notImplemented(): never {
877+
throw new Error("Not implemented");
878+
}
879+
872880
export function memoize<T>(callback: () => T): () => T {
873881
let value: T;
874882
return () => {

src/compiler/declarationEmitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ namespace ts {
6363
let isCurrentFileExternalModule: boolean;
6464
let reportedDeclarationError = false;
6565
let errorNameNode: DeclarationName;
66-
const emitJsDocComments = compilerOptions.removeComments ? () => {} : writeJsDocComments;
66+
const emitJsDocComments = compilerOptions.removeComments ? noop : writeJsDocComments;
6767
const emit = compilerOptions.stripInternal ? stripInternal : emitNode;
6868
let noDeclare: boolean;
6969

src/compiler/program.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -950,8 +950,7 @@ namespace ts {
950950
return runWithCancellationToken(() => {
951951
const resolver = getDiagnosticsProducingTypeChecker().getEmitResolver(sourceFile, cancellationToken);
952952
// Don't actually write any files since we're just getting diagnostics.
953-
const writeFile: WriteFileCallback = () => { };
954-
return ts.getDeclarationDiagnostics(getEmitHost(writeFile), resolver, sourceFile);
953+
return ts.getDeclarationDiagnostics(getEmitHost(noop), resolver, sourceFile);
955954
});
956955
}
957956

src/compiler/utilities.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ namespace ts {
6363
// Completely ignore indentation for string writers. And map newlines to
6464
// a single space.
6565
writeLine: () => str += " ",
66-
increaseIndent: () => { },
67-
decreaseIndent: () => { },
66+
increaseIndent: noop,
67+
decreaseIndent: noop,
6868
clear: () => str = "",
69-
trackSymbol: () => { },
70-
reportInaccessibleThisError: () => { }
69+
trackSymbol: noop,
70+
reportInaccessibleThisError: noop
7171
};
7272
}
7373

src/compiler/visitor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,18 +1331,18 @@ namespace ts {
13311331
export namespace Debug {
13321332
export const failNotOptional = shouldAssert(AssertionLevel.Normal)
13331333
? (message?: string) => assert(false, message || "Node not optional.")
1334-
: () => {};
1334+
: noop;
13351335

13361336
export const failBadSyntaxKind = shouldAssert(AssertionLevel.Normal)
13371337
? (node: Node, message?: string) => assert(false, message || "Unexpected node.", () => `Node ${formatSyntaxKind(node.kind)} was unexpected.`)
1338-
: () => {};
1338+
: noop;
13391339

13401340
export const assertNode = shouldAssert(AssertionLevel.Normal)
13411341
? (node: Node, test: (node: Node) => boolean, message?: string) => assert(
13421342
test === undefined || test(node),
13431343
message || "Unexpected node.",
13441344
() => `Node ${formatSyntaxKind(node.kind)} did not pass test '${getFunctionName(test)}'.`)
1345-
: () => {};
1345+
: noop;
13461346

13471347
function getFunctionName(func: Function) {
13481348
if (typeof func !== "function") {

src/harness/harness.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ namespace Harness {
690690
export const getCurrentDirectory = () => "";
691691
export const args = () => <string[]>[];
692692
export const getExecutingFilePath = () => "";
693-
export const exit = () => { };
693+
export const exit = ts.noop;
694694
export const getDirectories = () => <string[]>[];
695695

696696
export let log = (s: string) => console.log(s);
@@ -1668,7 +1668,7 @@ namespace Harness {
16681668
// This does not need to exist strictly speaking, but many tests will need to be updated if it's removed
16691669
export function compileString(_code: string, _unitName: string, _callback: (result: CompilerResult) => void) {
16701670
// NEWTODO: Re-implement 'compileString'
1671-
throw new Error("compileString NYI");
1671+
return ts.notImplemented();
16721672
}
16731673

16741674
export interface GeneratedFile {

src/harness/harnessLanguageService.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -313,14 +313,10 @@ namespace Harness.LanguageService {
313313
getLocalizedDiagnosticMessages(): string { return JSON.stringify({}); }
314314

315315
readDirectory(_rootDir: string, _extension: string): string {
316-
throw new Error("NYI");
317-
}
318-
readDirectoryNames(_path: string): string {
319-
throw new Error("Not implemented.");
320-
}
321-
readFileNames(_path: string): string {
322-
throw new Error("Not implemented.");
316+
return ts.notImplemented();
323317
}
318+
readDirectoryNames = ts.notImplemented;
319+
readFileNames = ts.notImplemented;
324320
fileExists(fileName: string) { return this.getScriptInfo(fileName) !== undefined; }
325321
readFile(fileName: string) {
326322
const snapshot = this.nativeHost.getScriptSnapshot(fileName);
@@ -339,7 +335,7 @@ namespace Harness.LanguageService {
339335
constructor(private shim: ts.ClassifierShim) {
340336
}
341337
getEncodedLexicalClassifications(_text: string, _lexState: ts.EndOfLineState, _classifyKeywordsInGenerics?: boolean): ts.Classifications {
342-
throw new Error("NYI");
338+
return ts.notImplemented();
343339
}
344340
getClassificationsForLine(text: string, lexState: ts.EndOfLineState, classifyKeywordsInGenerics?: boolean): ts.ClassificationResult {
345341
const result = this.shim.getClassificationsForLine(text, lexState, classifyKeywordsInGenerics).split("\n");
@@ -648,7 +644,7 @@ namespace Harness.LanguageService {
648644
}
649645

650646
createDirectory(_directoryName: string): void {
651-
throw new Error("Not Implemented Yet.");
647+
return ts.notImplemented();
652648
}
653649

654650
getCurrentDirectory(): string {
@@ -664,15 +660,15 @@ namespace Harness.LanguageService {
664660
}
665661

666662
readDirectory(_path: string, _extension?: string[], _exclude?: string[], _include?: string[]): string[] {
667-
throw new Error("Not implemented Yet.");
663+
return ts.notImplemented();
668664
}
669665

670666
watchFile(): ts.FileWatcher {
671-
return { close() { } };
667+
return { close: ts.noop };
672668
}
673669

674670
watchDirectory(): ts.FileWatcher {
675-
return { close() { } };
671+
return { close: ts.noop };
676672
}
677673

678674
close(): void {

src/harness/runner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,5 +222,5 @@ else {
222222
}
223223
if (!runUnitTests) {
224224
// patch `describe` to skip unit tests
225-
describe = <any>(function () { });
225+
describe = ts.noop as any;
226226
}

src/harness/unittests/cachingInServerLSHost.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@ namespace ts {
2121
args: <string[]>[],
2222
newLine: "\r\n",
2323
useCaseSensitiveFileNames: false,
24-
write: () => { },
24+
write: noop,
2525
readFile: (path: string): string => {
2626
return path in fileMap ? fileMap[path].content : undefined;
2727
},
2828
writeFile: (_path: string, _data: string, _writeByteOrderMark?: boolean) => {
29-
throw new Error("NYI");
29+
return ts.notImplemented();
3030
},
3131
resolvePath: (_path: string): string => {
32-
throw new Error("NYI");
32+
return ts.notImplemented();
3333
},
3434
fileExists: (path: string): boolean => {
3535
return path in fileMap;
3636
},
3737
directoryExists: (path: string): boolean => {
3838
return existingDirectories[path] || false;
3939
},
40-
createDirectory: () => { },
40+
createDirectory: noop,
4141
getExecutingFilePath: (): string => {
4242
return "";
4343
},
@@ -47,14 +47,14 @@ namespace ts {
4747
getDirectories: () => [],
4848
getEnvironmentVariable: () => "",
4949
readDirectory: (_path: string, _extension?: string[], _exclude?: string[], _include?: string[]): string[] => {
50-
throw new Error("NYI");
50+
return ts.notImplemented();
5151
},
52-
exit: () => { },
52+
exit: noop,
5353
watchFile: () => ({
54-
close: () => { }
54+
close: noop
5555
}),
5656
watchDirectory: () => ({
57-
close: () => { }
57+
close: noop
5858
}),
5959
setTimeout,
6060
clearTimeout,
@@ -65,14 +65,14 @@ namespace ts {
6565

6666
function createProject(rootFile: string, serverHost: server.ServerHost): { project: server.Project, rootScriptInfo: server.ScriptInfo } {
6767
const logger: server.Logger = {
68-
close() { },
68+
close: noop,
6969
hasLevel: () => false,
7070
loggingEnabled: () => false,
71-
perftrc: () => { },
72-
info: () => { },
73-
startGroup: () => { },
74-
endGroup: () => { },
75-
msg: () => { },
71+
perftrc: noop,
72+
info: noop,
73+
startGroup: noop,
74+
endGroup: noop,
75+
msg: noop,
7676
getLogFileName: (): string => undefined
7777
};
7878

@@ -109,10 +109,7 @@ namespace ts {
109109
const originalFileExists = serverHost.fileExists;
110110
{
111111
// patch fileExists to make sure that disk is not touched
112-
serverHost.fileExists = (): boolean => {
113-
assert.isTrue(false, "fileExists should not be called");
114-
return false;
115-
};
112+
serverHost.fileExists = notImplemented;
116113

117114
const newContent = `import {x} from "f1"
118115
var x: string = 1;`;

src/harness/unittests/moduleResolution.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ namespace ts {
290290
return path in files ? createSourceFile(fileName, files[path], languageVersion) : undefined;
291291
},
292292
getDefaultLibFileName: () => "lib.d.ts",
293-
writeFile: (): void => { throw new Error("NotImplemented"); },
293+
writeFile: notImplemented,
294294
getCurrentDirectory: () => currentDirectory,
295295
getDirectories: () => [],
296296
getCanonicalFileName: fileName => fileName.toLowerCase(),
@@ -300,7 +300,7 @@ namespace ts {
300300
const path = normalizePath(combinePaths(currentDirectory, fileName));
301301
return path in files;
302302
},
303-
readFile: (): string => { throw new Error("NotImplemented"); }
303+
readFile: notImplemented
304304
};
305305

306306
const program = createProgram(rootFiles, options, host);
@@ -370,7 +370,7 @@ export = C;
370370
return path in files ? createSourceFile(fileName, files[path], languageVersion) : undefined;
371371
},
372372
getDefaultLibFileName: () => "lib.d.ts",
373-
writeFile: (): void => { throw new Error("NotImplemented"); },
373+
writeFile: notImplemented,
374374
getCurrentDirectory: () => currentDirectory,
375375
getDirectories: () => [],
376376
getCanonicalFileName,
@@ -380,7 +380,7 @@ export = C;
380380
const path = getCanonicalFileName(normalizePath(combinePaths(currentDirectory, fileName)));
381381
return path in files;
382382
},
383-
readFile: (): string => { throw new Error("NotImplemented"); }
383+
readFile: notImplemented
384384
};
385385
const program = createProgram(rootFiles, options, host);
386386
const diagnostics = sortAndDeduplicateDiagnostics(program.getSemanticDiagnostics().concat(program.getOptionsDiagnostics()));
@@ -911,15 +911,11 @@ import b = require("./moduleB");
911911
});
912912
});
913913

914-
function notImplemented(name: string): () => any {
915-
return () => assert(`${name} is not implemented and should not be called`);
916-
}
917-
918914
describe("ModuleResolutionHost.directoryExists", () => {
919915
it("No 'fileExists' calls if containing directory is missing", () => {
920916
const host: ModuleResolutionHost = {
921-
readFile: notImplemented("readFile"),
922-
fileExists: notImplemented("fileExists"),
917+
readFile: notImplemented,
918+
fileExists: notImplemented,
923919
directoryExists: _ => false
924920
};
925921

@@ -1022,9 +1018,7 @@ import b = require("./moduleB");
10221018
fileExists : fileName => fileName in sourceFiles,
10231019
getSourceFile: fileName => sourceFiles[fileName],
10241020
getDefaultLibFileName: () => "lib.d.ts",
1025-
writeFile(_file, _text) {
1026-
throw new Error("NYI");
1027-
},
1021+
writeFile: notImplemented,
10281022
getCurrentDirectory: () => "/",
10291023
getDirectories: () => [],
10301024
getCanonicalFileName: f => f.toLowerCase(),

src/harness/unittests/reuseProgramStructure.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,7 @@ namespace ts {
111111
getDefaultLibFileName(): string {
112112
return "lib.d.ts";
113113
},
114-
writeFile() {
115-
throw new Error("NYI");
116-
},
114+
writeFile: notImplemented,
117115
getCurrentDirectory(): string {
118116
return "";
119117
},
@@ -244,13 +242,13 @@ namespace ts {
244242

245243
it("fails if change affects type references", () => {
246244
const program_1 = newProgram(files, ["a.ts"], { types: ["a"] });
247-
updateProgram(program_1, ["a.ts"], { types: ["b"] }, () => { });
245+
updateProgram(program_1, ["a.ts"], { types: ["b"] }, noop);
248246
assert.isTrue(!program_1.structureIsReused);
249247
});
250248

251249
it("succeeds if change doesn't affect type references", () => {
252250
const program_1 = newProgram(files, ["a.ts"], { types: ["a"] });
253-
updateProgram(program_1, ["a.ts"], { types: ["a"] }, () => { });
251+
updateProgram(program_1, ["a.ts"], { types: ["a"] }, noop);
254252
assert.isTrue(program_1.structureIsReused);
255253
});
256254

@@ -276,19 +274,19 @@ namespace ts {
276274

277275
it("fails if module kind changes", () => {
278276
const program_1 = newProgram(files, ["a.ts"], { target, module: ModuleKind.CommonJS });
279-
updateProgram(program_1, ["a.ts"], { target, module: ModuleKind.AMD }, () => { });
277+
updateProgram(program_1, ["a.ts"], { target, module: ModuleKind.AMD }, noop);
280278
assert.isTrue(!program_1.structureIsReused);
281279
});
282280

283281
it("fails if rootdir changes", () => {
284282
const program_1 = newProgram(files, ["a.ts"], { target, module: ModuleKind.CommonJS, rootDir: "/a/b" });
285-
updateProgram(program_1, ["a.ts"], { target, module: ModuleKind.CommonJS, rootDir: "/a/c" }, () => { });
283+
updateProgram(program_1, ["a.ts"], { target, module: ModuleKind.CommonJS, rootDir: "/a/c" }, noop);
286284
assert.isTrue(!program_1.structureIsReused);
287285
});
288286

289287
it("fails if config path changes", () => {
290288
const program_1 = newProgram(files, ["a.ts"], { target, module: ModuleKind.CommonJS, configFilePath: "/a/b/tsconfig.json" });
291-
updateProgram(program_1, ["a.ts"], { target, module: ModuleKind.CommonJS, configFilePath: "/a/c/tsconfig.json" }, () => { });
289+
updateProgram(program_1, ["a.ts"], { target, module: ModuleKind.CommonJS, configFilePath: "/a/c/tsconfig.json" }, noop);
292290
assert.isTrue(!program_1.structureIsReused);
293291
});
294292

src/harness/unittests/session.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,32 @@ namespace ts.server {
1010
useCaseSensitiveFileNames: true,
1111
write(s): void { lastWrittenToHost = s; },
1212
readFile(): string { return void 0; },
13-
writeFile(): void {},
13+
writeFile: noop,
1414
resolvePath(): string { return void 0; },
1515
fileExists: () => false,
1616
directoryExists: () => false,
1717
getDirectories: () => [],
18-
createDirectory(): void {},
18+
createDirectory: noop,
1919
getExecutingFilePath(): string { return void 0; },
2020
getCurrentDirectory(): string { return void 0; },
2121
getEnvironmentVariable(): string { return ""; },
2222
readDirectory(): string[] { return []; },
23-
exit(): void { },
23+
exit: noop,
2424
setTimeout() { return 0; },
25-
clearTimeout() { },
25+
clearTimeout: noop,
2626
setImmediate: () => 0,
27-
clearImmediate() {}
27+
clearImmediate: noop
2828
};
2929
const nullCancellationToken: HostCancellationToken = { isCancellationRequested: () => false };
3030
const mockLogger: Logger = {
31-
close(): void {},
31+
close: noop,
3232
hasLevel(): boolean { return false; },
3333
loggingEnabled(): boolean { return false; },
34-
perftrc(): void {},
35-
info(): void {},
36-
startGroup(): void {},
37-
endGroup(): void {},
38-
msg(): void {},
34+
perftrc: noop,
35+
info: noop,
36+
startGroup: noop,
37+
endGroup: noop,
38+
msg: noop,
3939
getLogFileName: (): string => undefined
4040
};
4141

0 commit comments

Comments
 (0)