Skip to content

Commit

Permalink
Added bang operator for array index usage, to prevent code breaking, …
Browse files Browse the repository at this point in the history
…after enabling strick index checking rule
  • Loading branch information
genius257 committed Mar 4, 2024
1 parent fe31fda commit 9289dd2
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 31 deletions.
6 changes: 3 additions & 3 deletions server/src/autoit/Script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
4 changes: 2 additions & 2 deletions server/src/autoit/Script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 5 additions & 6 deletions server/src/autoit/docBlock/DocBlock/DescriptionFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@ 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
//sequence "{@}" is changed to "@", and "{}" is changed to "}".
//"%" 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, '%%');
}


Expand Down Expand Up @@ -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);
}
}

Expand Down
6 changes: 3 additions & 3 deletions server/src/autoit/docBlock/DocBlock/StandardTagFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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]!;
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/src/autoit/docBlock/DocBlockFactory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion server/src/autoit/docBlock/DocBlockFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default class DocBlockFactory {
}

const dockBlockdata: Record<string, string> = docBlockItems.reduce((result, item) => {
result[item[1].trim().toLowerCase()] = item[2].trim();
result[item[1]!.trim().toLowerCase()] = item[2]!.trim();
return result;
}, {});

Expand Down
4 changes: 2 additions & 2 deletions server/src/autoit/docBlock/FqsenResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)}`);
}
Expand Down
26 changes: 13 additions & 13 deletions server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<CompletionItem[]>((completionItems, declaration) => {
switch (declaration.type) {
Expand Down Expand Up @@ -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<CompletionItem>(nativeSuggestion => ({
label: nativeSuggestions[nativeSuggestion].title || "",
kind: nativeSuggestions[nativeSuggestion].kind,
documentation: nativeSuggestions[nativeSuggestion].documentation,
detail: nativeSuggestions[nativeSuggestion].detail,
completionItems = completionItems.concat(Object.keys(nativeSuggestions).map<CompletionItem>(nativeSuggestion => ({ //FIXME: Map Object.entries instead
label: nativeSuggestions[nativeSuggestion]!.title || "",
kind: nativeSuggestions[nativeSuggestion]!.kind,
documentation: nativeSuggestions[nativeSuggestion]!.documentation,
detail: nativeSuggestions[nativeSuggestion]!.detail,
})));

return completionItems;
Expand Down Expand Up @@ -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<LocationRange>(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));
}
Expand Down

0 comments on commit 9289dd2

Please sign in to comment.