Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow use of implicit any type on variable declarations. #2943

Merged
merged 10 commits into from
May 27, 2024
6 changes: 3 additions & 3 deletions @navikt/core/react/src/date/utils/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { setYear } from "date-fns";
import { Matcher, isMatch } from "./is-match";

export const nextEnabled = (
months,
months: Date[],
key: string,
disabled: Matcher[],
currentMonth: Date,
Expand Down Expand Up @@ -258,13 +258,13 @@ const isOutOfRange = (

const nextOnRow = (
currentIndex: number,
months,
months: Date[],
yearState: Date,
disabled: Matcher[],
mode: "home" | "end",
) => {
const row = getRow(currentIndex);
let monthsOfRow;
let monthsOfRow: Date[] = [];

switch (row) {
case 1:
Expand Down
11 changes: 8 additions & 3 deletions @navikt/core/react/src/date/utils/parse-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const parseDate = (
type: "date" | "month",
allowTwoDigitYear: boolean,
): Date => {
let parsed;
let parsed: Date;
const ALLOWED_FORMATS =
type === "date" ? ALLOWED_INPUT_FORMATS_DATE : ALLOWED_INPUT_FORMATS_MONTH;

Expand Down Expand Up @@ -76,8 +76,13 @@ export const parseDate = (
return new Date("Invalid date");
};

function isTwoDigitYear(dateString, today, locale, formats) {
let parsed;
function isTwoDigitYear(
dateString: string,
today: Date,
locale: Locale,
formats: string[],
) {
let parsed: Date;
const newFormat = formats.map((x) => x.replace("yyyy", "yy"));
for (const format of newFormat) {
parsed = parse(dateString, format, today, { locale });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ const FilteredOptionsProvider = ({
);

const ariaDescribedBy = useMemo(() => {
let activeOption;
let activeOption: string = "";
if (!isLoading && filteredOptions.length === 0 && !allowNewValues) {
activeOption = filteredOptionsUtils.getNoHitsId(id);
} else if (value || isLoading) {
Expand All @@ -162,6 +162,7 @@ const FilteredOptionsProvider = ({
const maybeMaxSelectedOptionsId =
maxSelected?.isLimitReached &&
filteredOptionsUtils.getMaxSelectedOptionsId(id);

return (
cl(activeOption, maybeMaxSelectedOptionsId, partialAriaDescribedBy) ||
undefined
Expand Down
5 changes: 4 additions & 1 deletion @navikt/core/react/src/modal/dialog-polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ function dialogPolyfillInfo(dialog) {
removed ? this.downgradeModal() : this.maybeHideModal();
removed = false;
}.bind(this);
var timeout;
var timeout: ReturnType<typeof setTimeout>;
var delayModel = function (ev) {
if (ev.target !== dialog) {
return;
Expand Down Expand Up @@ -631,6 +631,7 @@ dialogPolyfill.DialogManager = function () {
this.mo_ = new MutationObserver(function (records) {
var removed = [];
records.forEach(function (rec) {
// biome-ignore lint/suspicious/noImplicitAnyLet: Reduntant to type c in this scenario
for (var i = 0, c; (c = rec.removedNodes[i]); ++i) {
if (!(c instanceof Element)) {
continue;
Expand Down Expand Up @@ -678,6 +679,7 @@ dialogPolyfill.DialogManager.prototype.unblockDocument = function () {
dialogPolyfill.DialogManager.prototype.updateStacking = function () {
var zIndex = this.zIndexHigh_;

// biome-ignore lint/suspicious/noImplicitAnyLet: Reduntant to type dpi in this scenario
for (var i = 0, dpi; (dpi = this.pendingDialogStack[i]); ++i) {
dpi.updateZIndex(--zIndex, --zIndex);
if (i === 0) {
Expand All @@ -703,6 +705,7 @@ dialogPolyfill.DialogManager.prototype.containedByTopDialog_ = function (
candidate,
) {
while ((candidate = findNearestDialog(candidate))) {
// biome-ignore lint/suspicious/noImplicitAnyLet: Reduntant to type dpi in this scenario
for (var i = 0, dpi; (dpi = this.pendingDialogStack[i]); ++i) {
if (dpi.dialog === candidate) {
return i === 0; // only valid if top-most
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export function useScrollButtons(listRef: React.RefObject<HTMLDivElement>) {
const win = listRef.current?.ownerDocument ?? document ?? window;
win.addEventListener("resize", handleResize);

let resizeObserver;
let resizeObserver: ResizeObserver;

if (typeof ResizeObserver !== "undefined") {
if (typeof ResizeObserver !== "undefined" && listRef.current) {
resizeObserver = new ResizeObserver(handleResize);
resizeObserver.observe(listRef.current);
}
Expand Down
2 changes: 1 addition & 1 deletion @navikt/core/react/src/util/debounce.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";
// https://github.com/mui/material-ui/blob/master/packages/mui-utils/src/debounce.js
export default function debounce(func, wait = 166) {
let timeout;
let timeout: ReturnType<typeof setTimeout>;
function debounced(this: any, ...args) {
const later = () => {
func.apply(this, args);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,73 +1,84 @@
import { FuseResult } from "fuse.js";
import { beforeEach, describe, expect, test } from "vitest";
import { SearchResultsT } from "@/types";
import type { FuseItemT, SearchHitT, SearchResultsT } from "@/types";
import { createSearchResult } from "../utils";

describe("createSearchResult", () => {
let result;
let rawResults;
let result: SearchHitT[];
let rawResults: FuseResult<FuseItemT>[];

const placeholders = { heading: "", slug: "", content: [] };

beforeEach(() => {
result = [
{
item: { _type: "komponent_artikkel" },
score: 0.05,
matches: [],
},
{
item: { _type: "aksel_artikkel" },
score: 0.1,
matches: [],
},
{
item: { _type: "ds_artikkel" },
score: 0.15,
matches: [],
},
{
item: { _type: "aksel_blogg" },
score: 0.2,
matches: [],
},
{
item: { _type: "aksel_prinsipp" },
score: 0.25,
matches: [],
},
{
item: { _type: "aksel_standalone" },
score: 0.3,
matches: [],
const items = {
komponent_artikkel: { score: 0.05 },
aksel_artikkel: { score: 0.1 },
ds_artikkel: { score: 0.15 },
aksel_blogg: { score: 0.2 },
aksel_prinsipp: { score: 0.25 },
aksel_standalone: { score: 0.3 },
};

result = Object.entries(items).map(([key, value]) => ({
item: {
_type: key as keyof typeof items,
...placeholders,
},
];
rawResults = [
{ item: { _type: "komponent_artikkel" }, score: 0.05 },
{ item: { _type: "aksel_artikkel" }, score: 0.1 },
{ item: { _type: "ds_artikkel" }, score: 0.15 },
{ item: { _type: "aksel_blogg" }, score: 0.2 },
{ item: { _type: "aksel_prinsipp" }, score: 0.25 },
{ item: { _type: "aksel_standalone" }, score: 0.3 },
];
score: value.score,
matches: [],
}));

rawResults = Object.entries(items).map(([key, value]) => ({
item: { _type: key as keyof typeof items, ...placeholders },
score: value.score,
refIndex: 0,
matches: [],
}));
});

test("should group hits by type", () => {
const expected = {
komponent_artikkel: [
{ item: { _type: "komponent_artikkel" }, score: 0.05, matches: [] },
{
item: { _type: "komponent_artikkel", ...placeholders },
score: 0.05,
matches: [],
},
],
aksel_artikkel: [
{ item: { _type: "aksel_artikkel" }, score: 0.1, matches: [] },
{
item: { _type: "aksel_artikkel", ...placeholders },
score: 0.1,
matches: [],
},
],
ds_artikkel: [
{ item: { _type: "ds_artikkel" }, score: 0.15, matches: [] },
{
item: { _type: "ds_artikkel", ...placeholders },
score: 0.15,
matches: [],
},
],
aksel_blogg: [
{ item: { _type: "aksel_blogg" }, score: 0.2, matches: [] },
{
item: { _type: "aksel_blogg", ...placeholders },
score: 0.2,
matches: [],
},
],
aksel_prinsipp: [
{ item: { _type: "aksel_prinsipp" }, score: 0.25, matches: [] },
{
item: { _type: "aksel_prinsipp", ...placeholders },
score: 0.25,
matches: [],
},
],
aksel_standalone: [
{ item: { _type: "aksel_standalone" }, score: 0.3, matches: [] },
{
item: { _type: "aksel_standalone", ...placeholders },
score: 0.3,
matches: [],
},
],
};
const actual: SearchResultsT = createSearchResult(result, rawResults);
Expand All @@ -76,7 +87,11 @@ describe("createSearchResult", () => {

test("should return top 3 results with score < 0.1", () => {
const expected = [
{ item: { _type: "komponent_artikkel" }, score: 0.05, matches: [] },
{
item: { _type: "komponent_artikkel", ...placeholders },
score: 0.05,
matches: [],
},
];
const actual: SearchResultsT = createSearchResult(result, rawResults);
expect(actual.topResults).toEqual(expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const SearchNavigationProvider = ({

/* Add a small delay to get a precieved smoother navigation */
useEffect(() => {
let timeout: NodeJS.Timeout;
let timeout: ReturnType<typeof setTimeout>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change this?

Copy link
Collaborator Author

@KenAJoh KenAJoh May 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since setTimeout is implemented by the js runtime (engine implements it, but runtime exposes the function), it could in theory differ between nodejs, deno, hermes etc. So ReturnType<typeof setTimeout> will be more correct in this scenario (was my assumption atleast).

Found this since let timeout; caused errors elsewhere and thus lead me to this instance.

const handler = () => {
timeout && clearTimeout(timeout);

Expand Down
1 change: 0 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"noShadowRestrictedNames": "off",
"noArrayIndexKey": "off",
"noAssignInExpressions": "off",
"noImplicitAnyLet": "off",
"noDoubleEquals": "off",
"noMisleadingCharacterClass": "off"
},
Expand Down
Loading