Skip to content

Commit 7826b38

Browse files
Josh GoldbergAndy
authored andcommitted
Apply 'no-unnecessary-initializer' lint rule (microsoft#22014)
* Apply 'no-unnecessary-initializer' lint rule Forbids `let`/`const` statements to be initialized to `undefined`, since that's the initial value by default anyway. The auto-fixer also happened to remove two unnecessary `as number` casts in `src/harness/parallel/worker.ts`. For historical data: to run with `--fix`, I modified the line in `Jakefile.js` that declared the `cmd` for running TSLint. * Moved worker.ts type assertions to parameters
1 parent 5e593ac commit 7826b38

18 files changed

Lines changed: 50 additions & 51 deletions

src/compiler/checker.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3200,7 +3200,7 @@ namespace ts {
32003200
}
32013201
}
32023202

3203-
let entityName: EntityName = undefined;
3203+
let entityName: EntityName;
32043204
const nameIdentifier = symbolToTypeReferenceName(type.symbol);
32053205
if (qualifiedName) {
32063206
Debug.assert(!qualifiedName.right);
@@ -5695,7 +5695,7 @@ namespace ts {
56955695
}
56965696
return [signature];
56975697
}
5698-
let result: Signature[] = undefined;
5698+
let result: Signature[];
56995699
for (let i = 0; i < signatureLists.length; i++) {
57005700
// Allow matching non-generic signatures to have excess parameters and different return types
57015701
const match = i === listIndex ? signature : findMatchingSignature(signatureLists[i], signature, /*partialMatch*/ true, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true);
@@ -5713,7 +5713,7 @@ namespace ts {
57135713
// type is the union of the constituent return types.
57145714
function getUnionSignatures(types: Type[], kind: SignatureKind): Signature[] {
57155715
const signatureLists = map(types, t => getSignaturesOfType(t, kind));
5716-
let result: Signature[] = undefined;
5716+
let result: Signature[];
57175717
for (let i = 0; i < signatureLists.length; i++) {
57185718
for (const signature of signatureLists[i]) {
57195719
// Only process signatures with parameter lists that aren't already in the result list
@@ -5849,7 +5849,7 @@ namespace ts {
58495849
else {
58505850
// Combinations of function, class, enum and module
58515851
let members = emptySymbols;
5852-
let stringIndexInfo: IndexInfo = undefined;
5852+
let stringIndexInfo: IndexInfo;
58535853
if (symbol.exports) {
58545854
members = getExportsOfSymbol(symbol);
58555855
}
@@ -6401,7 +6401,7 @@ namespace ts {
64016401
}
64026402
const propTypes: Type[] = [];
64036403
const declarations: Declaration[] = [];
6404-
let commonType: Type = undefined;
6404+
let commonType: Type;
64056405
for (const prop of props) {
64066406
if (prop.declarations) {
64076407
addRange(declarations, prop.declarations);
@@ -6682,7 +6682,7 @@ namespace ts {
66826682
const parameters: Symbol[] = [];
66836683
let hasLiteralTypes = false;
66846684
let minArgumentCount = 0;
6685-
let thisParameter: Symbol = undefined;
6685+
let thisParameter: Symbol;
66866686
let hasThisParameter: boolean;
66876687
const iife = getImmediatelyInvokedFunctionExpression(declaration);
66886688
const isJSConstructSignature = isJSDocConstructSignature(declaration);

src/compiler/core.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,7 +1590,7 @@ namespace ts {
15901590
return text.replace(/{(\d+)}/g, (_match, index?) => args[+index + baseIndex]);
15911591
}
15921592

1593-
export let localizedDiagnosticMessages: MapLike<string> = undefined;
1593+
export let localizedDiagnosticMessages: MapLike<string>;
15941594

15951595
export function getLocaleSpecificMessage(message: DiagnosticMessage) {
15961596
return localizedDiagnosticMessages && localizedDiagnosticMessages[message.key] || message.message;
@@ -3049,7 +3049,7 @@ namespace ts {
30493049

30503050
/** Return the object corresponding to the best pattern to match `candidate`. */
30513051
export function findBestPatternMatch<T>(values: ReadonlyArray<T>, getPattern: (value: T) => Pattern, candidate: string): T | undefined {
3052-
let matchedValue: T | undefined = undefined;
3052+
let matchedValue: T | undefined;
30533053
// use length of prefix as betterness criteria
30543054
let longestMatchPrefixLength = -1;
30553055

src/compiler/moduleNameResolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ namespace ts {
674674
}
675675

676676
// string is for exact match
677-
let matchedPattern: Pattern | string | undefined = undefined;
677+
let matchedPattern: Pattern | string | undefined;
678678
if (state.compilerOptions.paths) {
679679
if (state.traceEnabled) {
680680
trace(state.host, Diagnostics.paths_option_is_specified_looking_for_a_pattern_to_match_module_name_0, moduleName);

src/compiler/parser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4773,7 +4773,7 @@ namespace ts {
47734773
const awaitToken = parseOptionalToken(SyntaxKind.AwaitKeyword);
47744774
parseExpected(SyntaxKind.OpenParenToken);
47754775

4776-
let initializer: VariableDeclarationList | Expression = undefined;
4776+
let initializer: VariableDeclarationList | Expression;
47774777
if (token() !== SyntaxKind.SemicolonToken) {
47784778
if (token() === SyntaxKind.VarKeyword || token() === SyntaxKind.LetKeyword || token() === SyntaxKind.ConstKeyword) {
47794779
initializer = parseVariableDeclarationList(/*inForStatementInitializer*/ true);
@@ -6235,7 +6235,7 @@ namespace ts {
62356235
// Initially we can parse out a tag. We also have seen a starting asterisk.
62366236
// This is so that /** * @type */ doesn't parse.
62376237
let state = JSDocState.SawAsterisk;
6238-
let margin: number | undefined = undefined;
6238+
let margin: number | undefined;
62396239
// + 4 for leading '/** '
62406240
let indent = start - Math.max(content.lastIndexOf("\n", start), 0) + 4;
62416241
function pushComment(text: string) {
@@ -7259,7 +7259,7 @@ namespace ts {
72597259
}
72607260

72617261
function getLastChildWorker(node: Node): Node | undefined {
7262-
let last: Node = undefined;
7262+
let last: Node;
72637263
forEachChild(node, child => {
72647264
if (nodeIsPresent(child)) {
72657265
last = child;

src/compiler/transformers/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ namespace ts {
4848
const uniqueExports = createMap<boolean>();
4949
let exportedNames: Identifier[];
5050
let hasExportDefault = false;
51-
let exportEquals: ExportAssignment = undefined;
51+
let exportEquals: ExportAssignment;
5252
let hasExportStarsToExportValues = false;
5353
let hasImportStarOrImportDefault = false;
5454

src/harness/fourslash.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3329,7 +3329,7 @@ ${code}
33293329
const ranges: Range[] = [];
33303330

33313331
// Stuff related to the subfile we're parsing
3332-
let currentFileContent: string = undefined;
3332+
let currentFileContent: string;
33333333
let currentFileName = fileName;
33343334
let currentFileSymlinks: string[] | undefined;
33353335
let currentFileOptions: { [s: string]: string } = {};
@@ -3464,7 +3464,7 @@ ${code}
34643464
}
34653465

34663466
function recordObjectMarker(fileName: string, location: LocationInformation, text: string, markerMap: ts.Map<Marker>, markers: Marker[]): Marker {
3467-
let markerValue: any = undefined;
3467+
let markerValue: any;
34683468
try {
34693469
// Attempt to parse the marker value as JSON
34703470
markerValue = JSON.parse("{ " + text + " }");
@@ -3523,7 +3523,7 @@ ${code}
35233523
let output = "";
35243524

35253525
/// The current marker (or maybe multi-line comment?) we're parsing, possibly
3526-
let openMarker: LocationInformation = undefined;
3526+
let openMarker: LocationInformation;
35273527

35283528
/// A stack of the open range markers that are still unclosed
35293529
const openRanges: RangeLocationInformation[] = [];

src/harness/harness.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ namespace Utils {
145145
path = "tests/" + path;
146146
}
147147

148-
let content: string = undefined;
148+
let content: string;
149149
try {
150150
content = Harness.IO.readFile(Harness.userSpecifiedRoot + path);
151151
}
@@ -1891,9 +1891,9 @@ namespace Harness {
18911891
const lines = Utils.splitContentByNewlines(code);
18921892

18931893
// Stuff related to the subfile we're parsing
1894-
let currentFileContent: string = undefined;
1894+
let currentFileContent: string;
18951895
let currentFileOptions: any = {};
1896-
let currentFileName: any = undefined;
1896+
let currentFileName: any;
18971897
let refs: string[] = [];
18981898

18991899
for (const line of lines) {

src/harness/loggedIO.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ interface PlaybackControl {
8787
}
8888

8989
namespace Playback {
90-
let recordLog: IoLog = undefined;
91-
let replayLog: IoLog = undefined;
92-
let replayFilesRead: ts.Map<IoLogFile> | undefined = undefined;
90+
let recordLog: IoLog;
91+
let replayLog: IoLog;
92+
let replayFilesRead: ts.Map<IoLogFile> | undefined;
9393
let recordLogFileNameBase = "";
9494

9595
interface Memoized<T> {

src/harness/parallel/worker.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ namespace Harness.Parallel.Worker {
5454
const fakeContext: Mocha.ISuiteCallbackContext = {
5555
retries() { return this; },
5656
slow() { return this; },
57-
timeout(n) {
58-
timeout = n as number;
57+
timeout(n: number) {
58+
timeout = n;
5959
return this;
6060
},
6161
};
@@ -126,8 +126,8 @@ namespace Harness.Parallel.Worker {
126126
let timeout: number;
127127
const fakeContext: Mocha.ITestCallbackContext = {
128128
skip() { return this; },
129-
timeout(n) {
130-
timeout = n as number;
129+
timeout(n: number) {
130+
timeout = n;
131131
const timeoutMsg: ParallelTimeoutChangeMessage = { type: "timeout", payload: { duration: timeout } };
132132
process.send(timeoutMsg);
133133
return this;

src/harness/projectsRunner.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class ProjectRunner extends RunnerBase {
163163
}
164164

165165
function getSourceFile(fileName: string, languageVersion: ts.ScriptTarget): ts.SourceFile {
166-
let sourceFile: ts.SourceFile = undefined;
166+
let sourceFile: ts.SourceFile;
167167
if (fileName === Harness.Compiler.defaultLibFileName) {
168168
sourceFile = Harness.Compiler.getDefaultLibrarySourceFile(Harness.Compiler.getDefaultLibFileName(compilerOptions));
169169
}
@@ -294,7 +294,7 @@ class ProjectRunner extends RunnerBase {
294294
}
295295

296296
function getSourceFileText(fileName: string): string {
297-
let text: string = undefined;
297+
let text: string;
298298
try {
299299
text = Harness.IO.readFile(getFileNameInTheProjectTest(fileName));
300300
}
@@ -370,7 +370,7 @@ class ProjectRunner extends RunnerBase {
370370
allInputFiles.unshift({ emittedFileName: sourceFile.fileName, code: sourceFile.text });
371371
}
372372
else if (!(compilerOptions.outFile || compilerOptions.out)) {
373-
let emitOutputFilePathWithoutExtension: string = undefined;
373+
let emitOutputFilePathWithoutExtension: string;
374374
if (compilerOptions.outDir) {
375375
let sourceFilePath = ts.getNormalizedAbsolutePath(sourceFile.fileName, compilerResult.program.getCurrentDirectory());
376376
sourceFilePath = sourceFilePath.replace(compilerResult.program.getCommonSourceDirectory(), "");

0 commit comments

Comments
 (0)