From 9289dd27905238c8487c483b7cce5803a21d7df3 Mon Sep 17 00:00:00 2001 From: Anders Pedersen Date: Mon, 4 Mar 2024 23:29:57 +0000 Subject: [PATCH] Added bang operator for array index usage, to prevent code breaking, after enabling strick index checking rule --- server/src/autoit/Script.test.ts | 6 ++--- server/src/autoit/Script.ts | 4 +-- .../docBlock/DocBlock/DescriptionFactory.ts | 11 ++++---- .../docBlock/DocBlock/StandardTagFactory.ts | 6 ++--- .../autoit/docBlock/DocBlockFactory.test.ts | 2 +- server/src/autoit/docBlock/DocBlockFactory.ts | 2 +- server/src/autoit/docBlock/FqsenResolver.ts | 4 +-- server/src/main.ts | 26 +++++++++---------- 8 files changed, 30 insertions(+), 31 deletions(-) diff --git a/server/src/autoit/Script.test.ts b/server/src/autoit/Script.test.ts index ae5f6a6..68e68c3 100644 --- a/server/src/autoit/Script.test.ts +++ b/server/src/autoit/Script.test.ts @@ -6,8 +6,8 @@ describe("Script", function () { EndIf`); test("getNodesAt", function() { - expect(script.getNodesAt({character: 1, line: 0}).reverse()[0].type).toBe("IfStatement"); - expect(script.getNodesAt({character: 6, line: 1}).reverse()[0].type).toBe("ExitStatement"); - expect(script.getNodesAt({character: 3, line: 2}).reverse()[0].type).toBe("IfStatement"); + expect(script.getNodesAt({character: 1, line: 0}).reverse()[0]!.type).toBe("IfStatement"); + expect(script.getNodesAt({character: 6, line: 1}).reverse()[0]!.type).toBe("ExitStatement"); + expect(script.getNodesAt({character: 3, line: 2}).reverse()[0]!.type).toBe("IfStatement"); }); }); \ No newline at end of file diff --git a/server/src/autoit/Script.ts b/server/src/autoit/Script.ts index ce7f9cc..176d745 100644 --- a/server/src/autoit/Script.ts +++ b/server/src/autoit/Script.ts @@ -145,8 +145,8 @@ export default class Script { this.includes = currrentIncludes?.map((include) => { const cacheIndex = this.includeCache.findIndex(cacheItem => cacheItem.statement.file === include.file && cacheItem.statement.library === include.library); if (cacheIndex > -1) { - this.includeCache[cacheIndex].statement = include; - return this.includeCache[cacheIndex]; + this.includeCache[cacheIndex]!.statement = include; + return this.includeCache[cacheIndex]!; } const newInclude = this.createInclude(include); diff --git a/server/src/autoit/docBlock/DocBlock/DescriptionFactory.ts b/server/src/autoit/docBlock/DocBlock/DescriptionFactory.ts index a41741b..65b0e13 100644 --- a/server/src/autoit/docBlock/DocBlock/DescriptionFactory.ts +++ b/server/src/autoit/docBlock/DocBlock/DescriptionFactory.ts @@ -17,9 +17,8 @@ export default class DescriptionFactory { const tags: Tag[] = []; for (let index = 1; index < count; index+=2) { - tags.push(this.tagFactory.create(tokens[index], context)); + tags.push(this.tagFactory.create(tokens[index]!, context)); tokens[index] = `%${++tagCount}$s`; - } //In order to allow "literal" inline tags, the otherwise invalid @@ -27,7 +26,7 @@ export default class DescriptionFactory { //"%" is escaped to "%%" because of vsprintf. //See unit tests for examples. for (let index = 0; index < count; index+=2) { - tokens[index] = tokens[index].replace(/{@}/g, '@').replace(/{}/g, '}').replace(/%/g, '%%'); + tokens[index] = tokens[index]!.replace(/{@}/g, '@').replace(/{}/g, '}').replace(/%/g, '%%'); } @@ -59,19 +58,19 @@ export default class DescriptionFactory { let startingSpaceCount = 9999999; for (let index = 1, iMax = lines.length; index < iMax; ++index) { // lines with a no length do not count as they are not indented at all - if (lines[index].trim() === '') { + if (lines[index]!.trim() === '') { continue; } // determine the number of prefixing spaces by checking the difference in line length before and after // an ltrim - startingSpaceCount = Math.min(startingSpaceCount, lines[index].length - lines[index].trimStart().length); + startingSpaceCount = Math.min(startingSpaceCount, lines[index]!.length - lines[index]!.trimStart().length); } // strip the number of spaces from each line if (startingSpaceCount > 0) { for (let index = 1, iMax = lines.length; index < iMax; ++index) { - lines[index] = lines[index].substring(startingSpaceCount); + lines[index] = lines[index]!.substring(startingSpaceCount); } } diff --git a/server/src/autoit/docBlock/DocBlock/StandardTagFactory.ts b/server/src/autoit/docBlock/DocBlock/StandardTagFactory.ts index 74cf7ff..968f641 100644 --- a/server/src/autoit/docBlock/DocBlock/StandardTagFactory.ts +++ b/server/src/autoit/docBlock/DocBlock/StandardTagFactory.ts @@ -68,7 +68,7 @@ export default class StandardTagFactory extends TagFactory { const [tagName, tagBody] = this.extractTagParts(tagLine); - return this.createTag(tagBody.trim(), tagName, context); + return this.createTag(tagBody!.trim(), tagName!, context); } public addParameter(name: string, value: unknown): void { @@ -131,12 +131,12 @@ export default class StandardTagFactory extends TagFactory { private findHandlerClassName(tagName: string, context: TypeContext): TagLike|Factory { let handlerClassName: TagLike|Factory = Generic; if (tagName in this.tagHandlerMappings) { - handlerClassName = this.tagHandlerMappings[tagName]; + handlerClassName = this.tagHandlerMappings[tagName]!; } else if (this.isAnnotation(tagName)) { // TODO: Annotation support is planned for a later stage and as such is disabled for now tagName = this.fqsenResolver.resolve(tagName, context).toString(); if (tagName in this.annotationMappings) { - handlerClassName = this.annotationMappings[tagName]; + handlerClassName = this.annotationMappings[tagName]!; } } diff --git a/server/src/autoit/docBlock/DocBlockFactory.test.ts b/server/src/autoit/docBlock/DocBlockFactory.test.ts index f5fc6c2..99cfe80 100644 --- a/server/src/autoit/docBlock/DocBlockFactory.test.ts +++ b/server/src/autoit/docBlock/DocBlockFactory.test.ts @@ -49,7 +49,7 @@ test('createFromMultilineComment', () => { expect(x.summary).toBe('Summary.'); expect(x.description.toString()).toBe('Description'); expect(x.tags).toHaveLength(1); - expect(x.tags[0].render()).toBe('@see something'); + expect(x.tags[0]!.render()).toBe('@see something'); }); test('createFromLegacySingleLineComments', () => { diff --git a/server/src/autoit/docBlock/DocBlockFactory.ts b/server/src/autoit/docBlock/DocBlockFactory.ts index 2de9d43..dae3d48 100644 --- a/server/src/autoit/docBlock/DocBlockFactory.ts +++ b/server/src/autoit/docBlock/DocBlockFactory.ts @@ -73,7 +73,7 @@ export default class DocBlockFactory { } const dockBlockdata: Record = docBlockItems.reduce((result, item) => { - result[item[1].trim().toLowerCase()] = item[2].trim(); + result[item[1]!.trim().toLowerCase()] = item[2]!.trim(); return result; }, {}); diff --git a/server/src/autoit/docBlock/FqsenResolver.ts b/server/src/autoit/docBlock/FqsenResolver.ts index 03e3672..1ba81e3 100644 --- a/server/src/autoit/docBlock/FqsenResolver.ts +++ b/server/src/autoit/docBlock/FqsenResolver.ts @@ -25,7 +25,7 @@ export default class FqsenResolver { const namespaceAliases = context.getNamespaceAliases; - if (!(typeParts[0] in namespaceAliases)) { + if (!(typeParts[0]! in namespaceAliases)) { let namespace = context.getNamespace(); if (namespace !== '') { namespace += FqsenResolver.OPERATOR_NAMESPACE; @@ -34,7 +34,7 @@ export default class FqsenResolver { return new Fqsen(`${FqsenResolver.OPERATOR_NAMESPACE}${namespace}${type}`); } - typeParts[0] = namespaceAliases[typeParts[0]]; + typeParts[0] = namespaceAliases[typeParts[0]!]; return new Fqsen(`${FqsenResolver.OPERATOR_NAMESPACE}${typeParts.join(FqsenResolver.OPERATOR_NAMESPACE)}`); } diff --git a/server/src/main.ts b/server/src/main.ts index f058bd4..1d1c469 100644 --- a/server/src/main.ts +++ b/server/src/main.ts @@ -221,7 +221,7 @@ connection.onHover((hoverParams, token, workDoneProgress):Hover|null => { const comments: SingleLineComment[] = [previousIdentifierSibling]; for (let index = precedingIdentifierSiblings.length - 2; index >= 0; index--) { - const element = precedingIdentifierSiblings[index]; + const element = precedingIdentifierSiblings[index]!; if (element.type !== "SingleLineComment") { break; } @@ -312,7 +312,7 @@ function getCompletionItems(params: CompletionParams): CompletionItem[] { // Loop though all unique included files and add completion items found in each. for (let index = 0; index < includes.length; index++) { - let script = workspace.get(includes[index]); + let script = workspace.get(includes[index]!); if (script !== undefined) { let _completionItems = script.declarations.reduce((completionItems, declaration) => { switch (declaration.type) { @@ -359,11 +359,11 @@ function getCompletionItems(params: CompletionParams): CompletionItem[] { completionItems = completionItems.filter((completionItem, index, array) => array.findIndex(x => x.label.toLowerCase() === completionItem.label.toLowerCase()) === index); //Add all native suggestions - completionItems = completionItems.concat(Object.keys(nativeSuggestions).map(nativeSuggestion => ({ - label: nativeSuggestions[nativeSuggestion].title || "", - kind: nativeSuggestions[nativeSuggestion].kind, - documentation: nativeSuggestions[nativeSuggestion].documentation, - detail: nativeSuggestions[nativeSuggestion].detail, + completionItems = completionItems.concat(Object.keys(nativeSuggestions).map(nativeSuggestion => ({ //FIXME: Map Object.entries instead + label: nativeSuggestions[nativeSuggestion]!.title || "", + kind: nativeSuggestions[nativeSuggestion]!.kind, + documentation: nativeSuggestions[nativeSuggestion]!.documentation, + detail: nativeSuggestions[nativeSuggestion]!.detail, }))); return completionItems; @@ -396,22 +396,22 @@ function getSignatureHelp(params: SignatureHelpParams): SignatureHelp | null if (callExpression.arguments.length > 0) { // Make new array of deep cloned location ranges, to prevent modifying original AST location values. const argumentLocations = callExpression.arguments.map(argument => JSON.parse(JSON.stringify(argument.location))); - let textBetween = text.substring(callExpression.callee.location.end.offset, argumentLocations[0].start.offset); + let textBetween = text.substring(callExpression.callee.location.end.offset, argumentLocations[0]!.start.offset); let parenthesisIndex = textBetween.indexOf('('); - argumentLocations[0].start = PositionHelper.offsetToLocation(argumentLocations[0].start.offset - Math.abs((textBetween.length - 1) - parenthesisIndex), text); + argumentLocations[0]!.start = PositionHelper.offsetToLocation(argumentLocations[0]!.start.offset - Math.abs((textBetween.length - 1) - parenthesisIndex), text); if (argumentLocations.length > 1) { for (let index = 0; index < argumentLocations.length - 1; index++) { - const argumentLeft = argumentLocations[index]; - const argumentRight = argumentLocations[index + 1]; + const argumentLeft = argumentLocations[index]!; + const argumentRight = argumentLocations[index + 1]!; const textBetween = text.substring(argumentLeft.end.offset, argumentRight.start.offset); const commaIndex = textBetween.indexOf(','); argumentLeft.end = PositionHelper.offsetToLocation(argumentLeft.end.offset + commaIndex, text); argumentRight.start = PositionHelper.offsetToLocation(argumentRight.start.offset - Math.abs((textBetween.length - 1) - commaIndex), text); } } - textBetween = text.substring(argumentLocations[argumentLocations.length - 1].end.offset, callExpression.location.end.offset); + textBetween = text.substring(argumentLocations[argumentLocations.length - 1]!.end.offset, callExpression.location.end.offset); parenthesisIndex = textBetween.indexOf(')'); - argumentLocations[argumentLocations.length - 1].end = PositionHelper.offsetToLocation(argumentLocations[argumentLocations.length - 1].end.offset + parenthesisIndex, text); + argumentLocations[argumentLocations.length - 1]!.end = PositionHelper.offsetToLocation(argumentLocations[argumentLocations.length - 1]!.end.offset + parenthesisIndex, text); parameterIndex = argumentLocations.findIndex(location => PositionHelper.isPositonWithinLocationRange(params.position, location)); }