Skip to content

Commit 8d51a92

Browse files
MiodecCopilot
andauthored
refactor: update getter type (@Miodec) (#8158)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent b1727ec commit 8d51a92

8 files changed

Lines changed: 44 additions & 28 deletions

File tree

frontend/src/ts/input/helpers/word-navigation.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ export async function goToNextWord({
5757

5858
PaceCaret.handleSpace(correctInsert, TestWords.words.getCurrentText());
5959

60-
Funbox.toggleScript(TestWords.words.getText(TestState.activeWordIndex + 1));
60+
const nextWord = TestWords.words.getText(TestState.activeWordIndex + 1);
61+
if (nextWord !== undefined) Funbox.toggleScript(nextWord);
6162

6263
const lastWord = TestState.activeWordIndex >= TestWords.words.length - 1;
6364
if (lastWord) {
@@ -97,7 +98,8 @@ export function goToPreviousWord(
9798

9899
TestState.decreaseActiveWordIndex();
99100

100-
Funbox.toggleScript(TestWords.words.getText(TestState.activeWordIndex));
101+
const word = TestWords.words.getText(TestState.activeWordIndex);
102+
if (word !== undefined) Funbox.toggleScript(word);
101103

102104
const nospaceEnabled = isFunboxActiveWithProperty("nospace");
103105

frontend/src/ts/test/british-english.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { capitalizeFirstLetterOfEachWord } from "../utils/strings";
44

55
export async function replace(
66
word: string,
7-
previousWord: string,
7+
previousWord: string | undefined,
88
): Promise<string> {
99
// Convert American-style double quotes to British-style single quotes
1010
if (word.includes('"')) {
@@ -38,7 +38,11 @@ export async function replace(
3838
? [rule, []]
3939
: [rule.britishWord, rule.exceptPreviousWords];
4040

41-
if (Config.mode === "quote" && exceptions.includes(previousWord)) {
41+
if (
42+
Config.mode === "quote" &&
43+
previousWord !== undefined &&
44+
exceptions.includes(previousWord)
45+
) {
4246
return word;
4347
}
4448

frontend/src/ts/test/pace-caret.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ function incrementLetterIndex(): void {
180180
settings.currentLetterIndex++;
181181
if (
182182
settings.currentLetterIndex >=
183-
TestWords.words.getText(settings.currentWordIndex).length + 1
183+
// oxlint-disable-next-line typescript/no-non-null-assertion let it throw if undefined
184+
TestWords.words.getText(settings.currentWordIndex)!.length + 1
184185
) {
185186
//go to the next word
186187
settings.currentLetterIndex = 0;
@@ -193,7 +194,9 @@ function incrementLetterIndex(): void {
193194
if (settings.currentLetterIndex <= -2) {
194195
//go to the previous word
195196
settings.currentLetterIndex =
196-
TestWords.words.getText(settings.currentWordIndex - 1).length - 1;
197+
// oxlint-disable-next-line typescript/no-non-null-assertion let it throw if undefined
198+
TestWords.words.getText(settings.currentWordIndex - 1)!.length -
199+
1;
197200
settings.currentWordIndex--;
198201
}
199202
settings.correction++;
@@ -203,7 +206,8 @@ function incrementLetterIndex(): void {
203206
settings.currentLetterIndex++;
204207
if (
205208
settings.currentLetterIndex >=
206-
TestWords.words.getText(settings.currentWordIndex).length
209+
// oxlint-disable-next-line typescript/no-non-null-assertion let it throw if undefined
210+
TestWords.words.getText(settings.currentWordIndex)!.length
207211
) {
208212
//go to the next word
209213
settings.currentLetterIndex = 0;

frontend/src/ts/test/practise-words.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,16 @@ export function init(
6464
if (missed === "biwords") {
6565
for (let i = 0; i < TestWords.words.length; i++) {
6666
const missedWord = TestWords.words.getText(i);
67+
68+
if (missedWord === undefined) continue; // won't happen, but ts complains
69+
6770
const missedWordCount = missedWords[missedWord];
6871
if (missedWordCount !== undefined) {
69-
if (i === 0) {
70-
sortableMissedBiwords.push([missedWord, "", missedWordCount]);
71-
} else {
72-
sortableMissedBiwords.push([
73-
missedWord,
74-
TestWords.words.getText(i - 1),
75-
missedWordCount,
76-
]);
77-
}
72+
sortableMissedBiwords.push([
73+
missedWord,
74+
TestWords.words.getText(i - 1) ?? "",
75+
missedWordCount,
76+
]);
7877
}
7978
}
8079
sortableMissedBiwords.sort((a, b) => {

frontend/src/ts/test/test-logic.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,9 @@ export async function finish(difficultyFailed = false): Promise<void> {
10791079

10801080
const lastWordInputLength = history[wordIndex]?.length ?? 0;
10811081

1082-
if (lastWordInputLength < TestWords.words.getText(wordIndex).length) {
1082+
if (
1083+
lastWordInputLength < (TestWords.words.getText(wordIndex)?.length ?? 0)
1084+
) {
10831085
historyLength--;
10841086
}
10851087

frontend/src/ts/test/test-ui.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,9 @@ function showWords(): void {
501501
} else {
502502
let wordsHTML = "";
503503
for (let i = 0; i < TestWords.words.length; i++) {
504-
wordsHTML += buildWordHTML(TestWords.words.getText(i), i);
504+
const word = TestWords.words.getText(i);
505+
if (word === undefined) continue; // won't happen, but ts complains
506+
wordsHTML += buildWordHTML(word, i);
505507
}
506508
wordsEl.setHtml(wordsHTML);
507509
}
@@ -737,7 +739,7 @@ export async function updateWordLetters({
737739
async () => {
738740
pendingWordData.delete(wordIndex);
739741
const currentWord = TestWords.words.getText(wordIndex);
740-
if (!currentWord && Config.mode !== "zen") return;
742+
if (currentWord === undefined && Config.mode !== "zen") return;
741743
let ret = "";
742744
const wordAtIndex = getWordElement(wordIndex);
743745
if (!wordAtIndex) return;
@@ -767,7 +769,7 @@ export async function updateWordLetters({
767769
const funbox = findSingleActiveFunboxWithFunction("getWordHtml");
768770

769771
const inputChars = Strings.splitIntoCharacters(input);
770-
const currentWordChars = Strings.splitIntoCharacters(currentWord);
772+
const currentWordChars = Strings.splitIntoCharacters(currentWord ?? "");
771773
for (let i = 0; i < inputChars.length; i++) {
772774
const charCorrect = currentWordChars[i] === inputChars[i];
773775

@@ -867,7 +869,7 @@ export async function updateWordLetters({
867869
hintsHtml = createHintsHtml(
868870
hintIndices,
869871
wordAtIndexLetters,
870-
currentWord,
872+
currentWord ?? "",
871873
);
872874
} else {
873875
hintsHtml = createHintsHtml(hintIndices, wordAtIndexLetters, input);

frontend/src/ts/test/test-words.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Words {
1212
}
1313

1414
getText(i?: undefined, raw?: boolean): string[];
15-
getText(i: number, raw?: boolean): string;
15+
getText(i: number, raw?: boolean): string | undefined;
1616
getText(i?: number, raw = false): string | string[] | undefined {
1717
if (i === undefined) {
1818
return this.list;

frontend/src/ts/test/words-generator.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function shouldCapitalize(lastChar: string): boolean {
3838

3939
let spanishSentenceTracker = "";
4040
export async function punctuateWord(
41-
previousWord: string,
41+
previousWord: string | undefined,
4242
currentWord: string,
4343
index: number,
4444
maxindex: number,
@@ -47,7 +47,8 @@ export async function punctuateWord(
4747

4848
const currentLanguage = Config.language.split("_")[0];
4949

50-
const lastChar = Strings.getLastChar(previousWord);
50+
const lastChar =
51+
previousWord !== undefined ? Strings.getLastChar(previousWord) : undefined;
5152

5253
const funbox = findSingleActiveFunboxWithFunction("punctuateWord");
5354
if (funbox) {
@@ -56,7 +57,7 @@ export async function punctuateWord(
5657
if (
5758
currentLanguage !== "code" &&
5859
currentLanguage !== "georgian" &&
59-
(index === 0 || shouldCapitalize(lastChar))
60+
(index === 0 || (lastChar !== undefined && shouldCapitalize(lastChar)))
6061
) {
6162
//always capitalise the first word or if there was a dot unless using a code alphabet or the Georgian language
6263

@@ -371,7 +372,7 @@ function applyFunboxesToWord(
371372

372373
async function applyBritishEnglishToWord(
373374
word: string,
374-
previousWord: string,
375+
previousWord: string | undefined,
375376
): Promise<string> {
376377
if (!Config.britishEnglish) return word;
377378
if (!Config.language.includes("english")) return word;
@@ -743,7 +744,7 @@ type GetNextWordReturn = {
743744
export async function getNextWord(
744745
wordIndex: number,
745746
wordsBound: number,
746-
previousWord: string,
747+
previousWord: string | undefined,
747748
previousWord2: string | undefined,
748749
): Promise<GetNextWordReturn> {
749750
console.debug("Getting next word", {
@@ -807,7 +808,9 @@ export async function getNextWord(
807808

808809
const funboxFrequency = getFunboxWordsFrequency() ?? "normal";
809810
let randomWord = currentWordset.randomWord(funboxFrequency);
810-
const previousWordRaw = previousWord.replace(/[.?!":\-,]/g, "").toLowerCase();
811+
const previousWordRaw = previousWord
812+
?.replace(/[.?!":\-,]/g, "")
813+
.toLowerCase();
811814
const previousWord2Raw = previousWord2
812815
?.replace(/[.?!":\-,']/g, "")
813816
.toLowerCase();

0 commit comments

Comments
 (0)