mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-12-11 17:41:26 -06:00
Apply 'no-unnecessary-initializer' lint rule (#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
This commit is contained in:
parent
5e593acad9
commit
7826b38426
@ -3200,7 +3200,7 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
let entityName: EntityName = undefined;
|
||||
let entityName: EntityName;
|
||||
const nameIdentifier = symbolToTypeReferenceName(type.symbol);
|
||||
if (qualifiedName) {
|
||||
Debug.assert(!qualifiedName.right);
|
||||
@ -5695,7 +5695,7 @@ namespace ts {
|
||||
}
|
||||
return [signature];
|
||||
}
|
||||
let result: Signature[] = undefined;
|
||||
let result: Signature[];
|
||||
for (let i = 0; i < signatureLists.length; i++) {
|
||||
// Allow matching non-generic signatures to have excess parameters and different return types
|
||||
const match = i === listIndex ? signature : findMatchingSignature(signatureLists[i], signature, /*partialMatch*/ true, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true);
|
||||
@ -5713,7 +5713,7 @@ namespace ts {
|
||||
// type is the union of the constituent return types.
|
||||
function getUnionSignatures(types: Type[], kind: SignatureKind): Signature[] {
|
||||
const signatureLists = map(types, t => getSignaturesOfType(t, kind));
|
||||
let result: Signature[] = undefined;
|
||||
let result: Signature[];
|
||||
for (let i = 0; i < signatureLists.length; i++) {
|
||||
for (const signature of signatureLists[i]) {
|
||||
// Only process signatures with parameter lists that aren't already in the result list
|
||||
@ -5849,7 +5849,7 @@ namespace ts {
|
||||
else {
|
||||
// Combinations of function, class, enum and module
|
||||
let members = emptySymbols;
|
||||
let stringIndexInfo: IndexInfo = undefined;
|
||||
let stringIndexInfo: IndexInfo;
|
||||
if (symbol.exports) {
|
||||
members = getExportsOfSymbol(symbol);
|
||||
}
|
||||
@ -6401,7 +6401,7 @@ namespace ts {
|
||||
}
|
||||
const propTypes: Type[] = [];
|
||||
const declarations: Declaration[] = [];
|
||||
let commonType: Type = undefined;
|
||||
let commonType: Type;
|
||||
for (const prop of props) {
|
||||
if (prop.declarations) {
|
||||
addRange(declarations, prop.declarations);
|
||||
@ -6682,7 +6682,7 @@ namespace ts {
|
||||
const parameters: Symbol[] = [];
|
||||
let hasLiteralTypes = false;
|
||||
let minArgumentCount = 0;
|
||||
let thisParameter: Symbol = undefined;
|
||||
let thisParameter: Symbol;
|
||||
let hasThisParameter: boolean;
|
||||
const iife = getImmediatelyInvokedFunctionExpression(declaration);
|
||||
const isJSConstructSignature = isJSDocConstructSignature(declaration);
|
||||
|
||||
@ -1590,7 +1590,7 @@ namespace ts {
|
||||
return text.replace(/{(\d+)}/g, (_match, index?) => args[+index + baseIndex]);
|
||||
}
|
||||
|
||||
export let localizedDiagnosticMessages: MapLike<string> = undefined;
|
||||
export let localizedDiagnosticMessages: MapLike<string>;
|
||||
|
||||
export function getLocaleSpecificMessage(message: DiagnosticMessage) {
|
||||
return localizedDiagnosticMessages && localizedDiagnosticMessages[message.key] || message.message;
|
||||
@ -3049,7 +3049,7 @@ namespace ts {
|
||||
|
||||
/** Return the object corresponding to the best pattern to match `candidate`. */
|
||||
export function findBestPatternMatch<T>(values: ReadonlyArray<T>, getPattern: (value: T) => Pattern, candidate: string): T | undefined {
|
||||
let matchedValue: T | undefined = undefined;
|
||||
let matchedValue: T | undefined;
|
||||
// use length of prefix as betterness criteria
|
||||
let longestMatchPrefixLength = -1;
|
||||
|
||||
|
||||
@ -674,7 +674,7 @@ namespace ts {
|
||||
}
|
||||
|
||||
// string is for exact match
|
||||
let matchedPattern: Pattern | string | undefined = undefined;
|
||||
let matchedPattern: Pattern | string | undefined;
|
||||
if (state.compilerOptions.paths) {
|
||||
if (state.traceEnabled) {
|
||||
trace(state.host, Diagnostics.paths_option_is_specified_looking_for_a_pattern_to_match_module_name_0, moduleName);
|
||||
|
||||
@ -4773,7 +4773,7 @@ namespace ts {
|
||||
const awaitToken = parseOptionalToken(SyntaxKind.AwaitKeyword);
|
||||
parseExpected(SyntaxKind.OpenParenToken);
|
||||
|
||||
let initializer: VariableDeclarationList | Expression = undefined;
|
||||
let initializer: VariableDeclarationList | Expression;
|
||||
if (token() !== SyntaxKind.SemicolonToken) {
|
||||
if (token() === SyntaxKind.VarKeyword || token() === SyntaxKind.LetKeyword || token() === SyntaxKind.ConstKeyword) {
|
||||
initializer = parseVariableDeclarationList(/*inForStatementInitializer*/ true);
|
||||
@ -6235,7 +6235,7 @@ namespace ts {
|
||||
// Initially we can parse out a tag. We also have seen a starting asterisk.
|
||||
// This is so that /** * @type */ doesn't parse.
|
||||
let state = JSDocState.SawAsterisk;
|
||||
let margin: number | undefined = undefined;
|
||||
let margin: number | undefined;
|
||||
// + 4 for leading '/** '
|
||||
let indent = start - Math.max(content.lastIndexOf("\n", start), 0) + 4;
|
||||
function pushComment(text: string) {
|
||||
@ -7259,7 +7259,7 @@ namespace ts {
|
||||
}
|
||||
|
||||
function getLastChildWorker(node: Node): Node | undefined {
|
||||
let last: Node = undefined;
|
||||
let last: Node;
|
||||
forEachChild(node, child => {
|
||||
if (nodeIsPresent(child)) {
|
||||
last = child;
|
||||
|
||||
@ -48,7 +48,7 @@ namespace ts {
|
||||
const uniqueExports = createMap<boolean>();
|
||||
let exportedNames: Identifier[];
|
||||
let hasExportDefault = false;
|
||||
let exportEquals: ExportAssignment = undefined;
|
||||
let exportEquals: ExportAssignment;
|
||||
let hasExportStarsToExportValues = false;
|
||||
let hasImportStarOrImportDefault = false;
|
||||
|
||||
|
||||
@ -3329,7 +3329,7 @@ ${code}
|
||||
const ranges: Range[] = [];
|
||||
|
||||
// Stuff related to the subfile we're parsing
|
||||
let currentFileContent: string = undefined;
|
||||
let currentFileContent: string;
|
||||
let currentFileName = fileName;
|
||||
let currentFileSymlinks: string[] | undefined;
|
||||
let currentFileOptions: { [s: string]: string } = {};
|
||||
@ -3464,7 +3464,7 @@ ${code}
|
||||
}
|
||||
|
||||
function recordObjectMarker(fileName: string, location: LocationInformation, text: string, markerMap: ts.Map<Marker>, markers: Marker[]): Marker {
|
||||
let markerValue: any = undefined;
|
||||
let markerValue: any;
|
||||
try {
|
||||
// Attempt to parse the marker value as JSON
|
||||
markerValue = JSON.parse("{ " + text + " }");
|
||||
@ -3523,7 +3523,7 @@ ${code}
|
||||
let output = "";
|
||||
|
||||
/// The current marker (or maybe multi-line comment?) we're parsing, possibly
|
||||
let openMarker: LocationInformation = undefined;
|
||||
let openMarker: LocationInformation;
|
||||
|
||||
/// A stack of the open range markers that are still unclosed
|
||||
const openRanges: RangeLocationInformation[] = [];
|
||||
|
||||
@ -145,7 +145,7 @@ namespace Utils {
|
||||
path = "tests/" + path;
|
||||
}
|
||||
|
||||
let content: string = undefined;
|
||||
let content: string;
|
||||
try {
|
||||
content = Harness.IO.readFile(Harness.userSpecifiedRoot + path);
|
||||
}
|
||||
@ -1891,9 +1891,9 @@ namespace Harness {
|
||||
const lines = Utils.splitContentByNewlines(code);
|
||||
|
||||
// Stuff related to the subfile we're parsing
|
||||
let currentFileContent: string = undefined;
|
||||
let currentFileContent: string;
|
||||
let currentFileOptions: any = {};
|
||||
let currentFileName: any = undefined;
|
||||
let currentFileName: any;
|
||||
let refs: string[] = [];
|
||||
|
||||
for (const line of lines) {
|
||||
|
||||
@ -87,9 +87,9 @@ interface PlaybackControl {
|
||||
}
|
||||
|
||||
namespace Playback {
|
||||
let recordLog: IoLog = undefined;
|
||||
let replayLog: IoLog = undefined;
|
||||
let replayFilesRead: ts.Map<IoLogFile> | undefined = undefined;
|
||||
let recordLog: IoLog;
|
||||
let replayLog: IoLog;
|
||||
let replayFilesRead: ts.Map<IoLogFile> | undefined;
|
||||
let recordLogFileNameBase = "";
|
||||
|
||||
interface Memoized<T> {
|
||||
|
||||
@ -54,8 +54,8 @@ namespace Harness.Parallel.Worker {
|
||||
const fakeContext: Mocha.ISuiteCallbackContext = {
|
||||
retries() { return this; },
|
||||
slow() { return this; },
|
||||
timeout(n) {
|
||||
timeout = n as number;
|
||||
timeout(n: number) {
|
||||
timeout = n;
|
||||
return this;
|
||||
},
|
||||
};
|
||||
@ -126,8 +126,8 @@ namespace Harness.Parallel.Worker {
|
||||
let timeout: number;
|
||||
const fakeContext: Mocha.ITestCallbackContext = {
|
||||
skip() { return this; },
|
||||
timeout(n) {
|
||||
timeout = n as number;
|
||||
timeout(n: number) {
|
||||
timeout = n;
|
||||
const timeoutMsg: ParallelTimeoutChangeMessage = { type: "timeout", payload: { duration: timeout } };
|
||||
process.send(timeoutMsg);
|
||||
return this;
|
||||
|
||||
@ -163,7 +163,7 @@ class ProjectRunner extends RunnerBase {
|
||||
}
|
||||
|
||||
function getSourceFile(fileName: string, languageVersion: ts.ScriptTarget): ts.SourceFile {
|
||||
let sourceFile: ts.SourceFile = undefined;
|
||||
let sourceFile: ts.SourceFile;
|
||||
if (fileName === Harness.Compiler.defaultLibFileName) {
|
||||
sourceFile = Harness.Compiler.getDefaultLibrarySourceFile(Harness.Compiler.getDefaultLibFileName(compilerOptions));
|
||||
}
|
||||
@ -294,7 +294,7 @@ class ProjectRunner extends RunnerBase {
|
||||
}
|
||||
|
||||
function getSourceFileText(fileName: string): string {
|
||||
let text: string = undefined;
|
||||
let text: string;
|
||||
try {
|
||||
text = Harness.IO.readFile(getFileNameInTheProjectTest(fileName));
|
||||
}
|
||||
@ -370,7 +370,7 @@ class ProjectRunner extends RunnerBase {
|
||||
allInputFiles.unshift({ emittedFileName: sourceFile.fileName, code: sourceFile.text });
|
||||
}
|
||||
else if (!(compilerOptions.outFile || compilerOptions.out)) {
|
||||
let emitOutputFilePathWithoutExtension: string = undefined;
|
||||
let emitOutputFilePathWithoutExtension: string;
|
||||
if (compilerOptions.outDir) {
|
||||
let sourceFilePath = ts.getNormalizedAbsolutePath(sourceFile.fileName, compilerResult.program.getCurrentDirectory());
|
||||
sourceFilePath = sourceFilePath.replace(compilerResult.program.getCommonSourceDirectory(), "");
|
||||
|
||||
@ -607,7 +607,7 @@ namespace ts.server {
|
||||
const edits: FileTextChanges[] = this.convertCodeEditsToTextChanges(response.body.edits);
|
||||
|
||||
const renameFilename: string | undefined = response.body.renameFilename;
|
||||
let renameLocation: number | undefined = undefined;
|
||||
let renameLocation: number | undefined;
|
||||
if (renameFilename !== undefined) {
|
||||
renameLocation = this.lineOffsetToPosition(renameFilename, response.body.renameLocation);
|
||||
}
|
||||
|
||||
@ -1560,7 +1560,7 @@ namespace ts.server {
|
||||
}
|
||||
|
||||
private extractPositionAndRange(args: protocol.FileLocationOrRangeRequestArgs, scriptInfo: ScriptInfo): { position: number, textRange: TextRange } {
|
||||
let position: number = undefined;
|
||||
let position: number;
|
||||
let textRange: TextRange;
|
||||
if (this.isLocation(args)) {
|
||||
position = getPosition(args);
|
||||
@ -1676,7 +1676,7 @@ namespace ts.server {
|
||||
}
|
||||
|
||||
private getStartAndEndPosition(args: protocol.FileRangeRequestArgs, scriptInfo: ScriptInfo) {
|
||||
let startPosition: number = undefined, endPosition: number = undefined;
|
||||
let startPosition: number, endPosition: number;
|
||||
if (args.startPosition !== undefined) {
|
||||
startPosition = args.startPosition;
|
||||
}
|
||||
|
||||
@ -509,7 +509,7 @@ namespace ts.FindAllReferences.Core {
|
||||
// here appears to be intentional).
|
||||
const {
|
||||
text = stripQuotes(unescapeLeadingUnderscores((getLocalSymbolForExportDefault(symbol) || symbol).escapedName)),
|
||||
allSearchSymbols = undefined,
|
||||
allSearchSymbols,
|
||||
} = searchOptions;
|
||||
const escapedText = escapeLeadingUnderscores(text);
|
||||
const parents = this.options.implementations && getParentSymbolsOfPropertyAccess(location, symbol, this.checker);
|
||||
@ -1146,7 +1146,7 @@ namespace ts.FindAllReferences.Core {
|
||||
}
|
||||
|
||||
function getContainingTypeReference(node: Node): Node {
|
||||
let topLevelTypeReference: Node = undefined;
|
||||
let topLevelTypeReference: Node;
|
||||
|
||||
while (node) {
|
||||
if (isTypeNode(node)) {
|
||||
|
||||
@ -336,7 +336,7 @@ namespace ts {
|
||||
// Only if all words have some sort of match is the pattern considered matched.
|
||||
|
||||
const subWordTextChunks = segment.subWordTextChunks;
|
||||
let matches: PatternMatch[] = undefined;
|
||||
let matches: PatternMatch[];
|
||||
|
||||
for (const subWordTextChunk of subWordTextChunks) {
|
||||
// Try to match the candidate with this word
|
||||
@ -393,8 +393,8 @@ namespace ts {
|
||||
|
||||
let currentCandidate = 0;
|
||||
let currentChunkSpan = 0;
|
||||
let firstMatch: number = undefined;
|
||||
let contiguous: boolean = undefined;
|
||||
let firstMatch: number;
|
||||
let contiguous: boolean;
|
||||
|
||||
while (true) {
|
||||
// Let's consider our termination cases
|
||||
|
||||
@ -274,7 +274,7 @@ namespace ts.refactor.extractSymbol {
|
||||
}
|
||||
else if (isVariableStatement(node)) {
|
||||
let numInitializers = 0;
|
||||
let lastInitializer: Expression | undefined = undefined;
|
||||
let lastInitializer: Expression | undefined;
|
||||
for (const declaration of node.declarationList.declarations) {
|
||||
if (declaration.initializer) {
|
||||
numInitializers++;
|
||||
@ -730,12 +730,12 @@ namespace ts.refactor.extractSymbol {
|
||||
|
||||
const functionName = createIdentifier(functionNameText);
|
||||
|
||||
let returnType: TypeNode = undefined;
|
||||
let returnType: TypeNode;
|
||||
const parameters: ParameterDeclaration[] = [];
|
||||
const callArguments: Identifier[] = [];
|
||||
let writes: UsageEntry[];
|
||||
usagesInScope.forEach((usage, name) => {
|
||||
let typeNode: TypeNode = undefined;
|
||||
let typeNode: TypeNode;
|
||||
if (!isJS) {
|
||||
let type = checker.getTypeOfSymbolAtLocation(usage.symbol, usage.node);
|
||||
// Widen the type so we don't emit nonsense annotations like "function fn(x: 3) {"
|
||||
@ -1114,7 +1114,7 @@ namespace ts.refactor.extractSymbol {
|
||||
}
|
||||
|
||||
function getContainingVariableDeclarationIfInList(node: Node, scope: Scope) {
|
||||
let prevNode = undefined;
|
||||
let prevNode;
|
||||
while (node !== undefined && node !== scope) {
|
||||
if (isVariableDeclaration(node) &&
|
||||
node.initializer === prevNode &&
|
||||
@ -1161,7 +1161,7 @@ namespace ts.refactor.extractSymbol {
|
||||
}
|
||||
|
||||
function getFirstDeclaration(type: Type): Declaration | undefined {
|
||||
let firstDeclaration = undefined;
|
||||
let firstDeclaration;
|
||||
|
||||
const symbol = type.symbol;
|
||||
if (symbol && symbol.declarations) {
|
||||
@ -1297,7 +1297,7 @@ namespace ts.refactor.extractSymbol {
|
||||
const members = scope.members;
|
||||
Debug.assert(members.length > 0); // There must be at least one child, since we extracted from one.
|
||||
|
||||
let prevMember: ClassElement | undefined = undefined;
|
||||
let prevMember: ClassElement | undefined;
|
||||
let allProperties = true;
|
||||
for (const member of members) {
|
||||
if (member.pos > maxPos) {
|
||||
@ -1322,7 +1322,7 @@ namespace ts.refactor.extractSymbol {
|
||||
function getNodeToInsertConstantBefore(node: Node, scope: Scope): Statement {
|
||||
Debug.assert(!isClassLike(scope));
|
||||
|
||||
let prevScope: Scope | undefined = undefined;
|
||||
let prevScope: Scope | undefined;
|
||||
for (let curr = node; curr !== scope; curr = curr.parent) {
|
||||
if (isScope(curr)) {
|
||||
prevScope = curr;
|
||||
@ -1331,7 +1331,7 @@ namespace ts.refactor.extractSymbol {
|
||||
|
||||
for (let curr = (prevScope || node).parent; ; curr = curr.parent) {
|
||||
if (isBlockLike(curr)) {
|
||||
let prevStatement = undefined;
|
||||
let prevStatement;
|
||||
for (const statement of curr.statements) {
|
||||
if (statement.pos > node.pos) {
|
||||
break;
|
||||
@ -1432,7 +1432,7 @@ namespace ts.refactor.extractSymbol {
|
||||
const visibleDeclarationsInExtractedRange: NamedDeclaration[] = [];
|
||||
const exposedVariableSymbolSet = createMap<true>(); // Key is symbol ID
|
||||
const exposedVariableDeclarations: VariableDeclaration[] = [];
|
||||
let firstExposedNonVariableDeclaration: NamedDeclaration | undefined = undefined;
|
||||
let firstExposedNonVariableDeclaration: NamedDeclaration | undefined;
|
||||
|
||||
const expression = !isReadonlyArray(targetRange.range)
|
||||
? targetRange.range
|
||||
@ -1440,7 +1440,7 @@ namespace ts.refactor.extractSymbol {
|
||||
? (targetRange.range[0] as ExpressionStatement).expression
|
||||
: undefined;
|
||||
|
||||
let expressionDiagnostic: Diagnostic | undefined = undefined;
|
||||
let expressionDiagnostic: Diagnostic | undefined;
|
||||
if (expression === undefined) {
|
||||
const statements = targetRange.range as ReadonlyArray<Statement>;
|
||||
const start = first(statements).getStart();
|
||||
@ -1542,7 +1542,7 @@ namespace ts.refactor.extractSymbol {
|
||||
}
|
||||
|
||||
let hasWrite = false;
|
||||
let readonlyClassPropertyWrite: Declaration | undefined = undefined;
|
||||
let readonlyClassPropertyWrite: Declaration | undefined;
|
||||
usagesPerScope[i].usages.forEach(value => {
|
||||
if (value.usage === Usage.Write) {
|
||||
hasWrite = true;
|
||||
|
||||
@ -1971,7 +1971,7 @@ namespace ts {
|
||||
continue;
|
||||
}
|
||||
|
||||
let descriptor: TodoCommentDescriptor = undefined;
|
||||
let descriptor: TodoCommentDescriptor;
|
||||
for (let i = 0; i < descriptors.length; i++) {
|
||||
if (matchArray[i + firstDescriptorCaptureIndex]) {
|
||||
descriptor = descriptors[i];
|
||||
|
||||
@ -1403,7 +1403,7 @@ namespace ts {
|
||||
function suppressTrailing(node: Node) {
|
||||
addEmitFlags(node, EmitFlags.NoTrailingComments);
|
||||
|
||||
let lastChild: Node = undefined;
|
||||
let lastChild: Node;
|
||||
forEachChild(
|
||||
node,
|
||||
child => (lastChild = child, undefined),
|
||||
|
||||
@ -104,7 +104,6 @@
|
||||
"no-object-literal-type-assertion": false,
|
||||
"no-shadowed-variable": false,
|
||||
"no-submodule-imports": false,
|
||||
"no-unnecessary-initializer": false,
|
||||
"no-var-requires": false,
|
||||
"ordered-imports": false,
|
||||
"prefer-conditional-expression": false,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user