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

Improve typescript story #541

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions ember-power-calendar/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ export default ts.config(
},
extends: [...ts.configs.recommendedTypeChecked, ember.configs.gts],
rules: {
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-misused-promises': 'off',
'@typescript-eslint/unbound-method': 'off',
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/await-thenable': 'off',
'ember/no-runloop': 0,
},
},
Expand Down
12 changes: 8 additions & 4 deletions ember-power-calendar/src/-private/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
PowerCalendarActions,
TPowerCalendarOnSelect,
} from '../components/power-calendar';
import { add } from '../utils.ts';
import { add, type NormalizeCalendarValue } from '../utils.ts';
import type { TPowerCalendarRangeOnSelect } from '../components/power-calendar-range';
import type { TPowerCalendarMultipleOnSelect } from '../components/power-calendar-multiple.ts';

Expand All @@ -17,7 +17,11 @@ export function publicActionsObject(
| undefined,
select: (day: CalendarDay, calendar: CalendarAPI, e: MouseEvent) => void,
onCenterChange:
| ((newCenter: any, calendar: CalendarAPI, event: MouseEvent) => void)
| ((
newCenter: NormalizeCalendarValue,
calendar: CalendarAPI,
event: MouseEvent,
) => Promise<void>)
| undefined,
changeCenterTask: TaskForAsyncTaskFunction<
unknown,
Expand All @@ -38,9 +42,9 @@ export function publicActionsObject(
return changeCenterTask.perform(newCenter, calendar, e);
};
actions.changeCenter = changeCenter;
actions.moveCenter = (step, unit, calendar, e) => {
actions.moveCenter = async (step, unit, calendar, e) => {
const newCenter = add(currentCenter, step, unit);
return changeCenter(newCenter, calendar, e);
return await changeCenter(newCenter, calendar, e);
};
}

Expand Down
24 changes: 14 additions & 10 deletions ember-power-calendar/src/components/power-calendar-multiple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import type {
PowerCalendarAPI,
PowerCalendarActions,
PowerCalendarArgs,
PowerCalendarSignature,
SelectedDays,
TCalendarType,
} from './power-calendar.ts';
Expand All @@ -32,8 +31,9 @@ import type { ComponentLike } from '@glint/template';
import type PowerCalendarService from '../services/power-calendar.ts';

export interface PowerCalendarMultipleAPI
extends Omit<PowerCalendarAPI, 'selected'> {
extends Omit<PowerCalendarAPI, 'selected' | 'DaysComponent'> {
selected?: Date[];
DaysComponent: ComponentLike<PowerCalendarMultipleDaysComponent>;
}

export type TPowerCalendarMultipleOnSelect = (
Expand All @@ -43,18 +43,19 @@ export type TPowerCalendarMultipleOnSelect = (
) => void;

interface PowerCalendarMultipleArgs
extends Omit<PowerCalendarArgs, 'selected' | 'onSelect'> {
extends Omit<PowerCalendarArgs, 'selected' | 'daysComponent' | 'onSelect'> {
selected?: Date[];
daysComponent?: string | ComponentLike<PowerCalendarMultipleDaysComponent>;
onSelect?: TPowerCalendarMultipleOnSelect;
}

interface PowerCalendarMultipleDefaultBlock extends PowerCalendarMultipleAPI {
NavComponent: ComponentLike<any>;
DaysComponent: ComponentLike<any>;
NavComponent: ComponentLike<PowerCalendarNavComponent>;
DaysComponent: ComponentLike<PowerCalendarMultipleDaysComponent>;
}

interface PowerCalendarMultipleSignature
extends Omit<PowerCalendarSignature, 'Args'> {
interface PowerCalendarMultipleSignature {
Element: HTMLElement;
Args: PowerCalendarMultipleArgs;
Blocks: {
default: [PowerCalendarMultipleDefaultBlock];
Expand All @@ -68,8 +69,8 @@ export default class PowerCalendarMultipleComponent extends Component<PowerCalen
@tracked _calendarType: TCalendarType = 'multiple';
@tracked _selected?: SelectedDays;

navComponent: ComponentLike<any> = PowerCalendarNavComponent;
daysComponent: ComponentLike<any> = PowerCalendarMultipleDaysComponent;
navComponent = PowerCalendarNavComponent;
daysComponent = PowerCalendarMultipleDaysComponent;

// Lifecycle hooks
constructor(owner: Owner, args: PowerCalendarMultipleArgs) {
Expand All @@ -88,7 +89,7 @@ export default class PowerCalendarMultipleComponent extends Component<PowerCalen
get publicActions(): PowerCalendarActions {
return publicActionsObject(
this.args.onSelect,
this.select,
this.select.bind(this),
this.args.onCenterChange,
this.changeCenterTask,
this.currentCenter,
Expand Down Expand Up @@ -213,14 +214,17 @@ export default class PowerCalendarMultipleComponent extends Component<PowerCalen
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
window.__powerCalendars = window.__powerCalendars || {}; // TODO: weakmap??
// @ts-expect-error Property '__powerCalendars'
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
window.__powerCalendars[this.publicAPI.uniqueId] = this;
}
}

unregisterCalendar() {
// @ts-expect-error Property '__powerCalendars'
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (window && window.__powerCalendars?.[guidFor(this)]) {
// @ts-expect-error Property '__powerCalendars'
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
delete window.__powerCalendars[guidFor(this)];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@ export default class PowerCalendarMultipleDaysComponent extends Component<PowerC
scheduleOnce(
'actions',
this,
this._updateFocused,
this._updateFocused.bind(this),
(e.target as HTMLElement).dataset['date'],
);
}

@action
handleDayBlur(): void {
scheduleOnce('actions', this, this._updateFocused, null);
scheduleOnce('actions', this, this._updateFocused.bind(this), null);
}

@action
Expand Down
20 changes: 11 additions & 9 deletions ember-power-calendar/src/components/power-calendar-range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
} from '../utils.ts';
import type {
PowerCalendarAPI,
PowerCalendarSignature,
PowerCalendarArgs,
TCalendarType,
SelectedDays,
Expand Down Expand Up @@ -56,12 +55,12 @@ interface PowerCalendarRangeArgs
}

export interface PowerCalendarRangeDefaultBlock extends PowerCalendarRangeAPI {
NavComponent: ComponentLike<any>;
DaysComponent: ComponentLike<any>;
NavComponent: ComponentLike<PowerCalendarNavComponent>;
DaysComponent: ComponentLike<PowerCalendarRangeDaysComponent>;
}

interface PowerCalendarRangeSignature
extends Omit<PowerCalendarSignature, 'Args' | 'Blocks'> {
interface PowerCalendarRangeSignature {
Element: HTMLElement;
Args: PowerCalendarRangeArgs;
Blocks: {
default: [PowerCalendarRangeDefaultBlock];
Expand All @@ -82,8 +81,8 @@ export default class PowerCalendarRangeComponent extends Component<PowerCalendar
@tracked _calendarType: TCalendarType = 'range';
@tracked _selected?: SelectedDays;

navComponent: ComponentLike<any> = PowerCalendarNavComponent;
daysComponent: ComponentLike<any> = PowerCalendarRangeDaysComponent;
navComponent = PowerCalendarNavComponent;
daysComponent = PowerCalendarRangeDaysComponent;

// Lifecycle hooks
constructor(owner: Owner, args: PowerCalendarRangeArgs) {
Expand All @@ -102,7 +101,7 @@ export default class PowerCalendarRangeComponent extends Component<PowerCalendar
get publicActions(): PowerCalendarActions {
return publicActionsObject(
this.args.onSelect,
this.select,
this.select.bind(this),
this.args.onCenterChange,
this.changeCenterTask,
this.currentCenter,
Expand Down Expand Up @@ -306,19 +305,22 @@ export default class PowerCalendarRangeComponent extends Component<PowerCalendar
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
window.__powerCalendars = window.__powerCalendars || {}; // TODO: weakmap??
// @ts-expect-error Property '__powerCalendars'
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
window.__powerCalendars[this.publicAPI.uniqueId] = this;
}
}

unregisterCalendar() {
// @ts-expect-error Property '__powerCalendars'
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (window && window.__powerCalendars?.[guidFor(this)]) {
// @ts-expect-error Property '__powerCalendars'
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
delete window.__powerCalendars[guidFor(this)];
}
}
}

function ownProp<T = { [key: string | number]: any }>(obj: T, prop: keyof T) {
function ownProp<T = { [key: string | number]: never }>(obj: T, prop: keyof T) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ export default class PowerCalendarRangeDaysComponent extends Component<PowerCale
scheduleOnce(
'actions',
this,
this._updateFocused,
this._updateFocused.bind(this),
(e.target as HTMLElement).dataset['date'],
);
}

@action
handleDayBlur(): void {
scheduleOnce('actions', this, this._updateFocused, null);
scheduleOnce('actions', this, this._updateFocused.bind(this), null);
}

@action
Expand Down
25 changes: 14 additions & 11 deletions ember-power-calendar/src/components/power-calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { guidFor } from '@ember/object/internals';
import { inject as service } from '@ember/service';
import { task } from 'ember-concurrency';
import { task, type TaskInstance } from 'ember-concurrency';
import { assert } from '@ember/debug';
import type { ComponentLike } from '@glint/template';
import {
Expand Down Expand Up @@ -52,13 +52,13 @@ export interface PowerCalendarActions {
newCenter: Date,
calendar: CalendarAPI,
event: MouseEvent,
) => void;
) => TaskInstance<void>;
moveCenter?: (
step: number,
unit: TPowerCalendarMoveCenterUnit,
calendar: PowerCalendarAPI,
event: MouseEvent,
) => void;
) => Promise<void>;
select?: (day: CalendarDay, calendar: CalendarAPI, event: MouseEvent) => void;
}

Expand All @@ -69,14 +69,14 @@ export type TPowerCalendarOnSelect = (
) => void;

export interface PowerCalendarArgs {
daysComponent?: string | ComponentLike<any>;
daysComponent?: string | ComponentLike<PowerCalendarDaysComponent>;
locale: string;
navComponent?: string | ComponentLike<any>;
navComponent?: string | ComponentLike<PowerCalendarNavComponent>;
onCenterChange?: (
newCenter: NormalizeCalendarValue,
calendar: PowerCalendarAPI,
event: MouseEvent,
) => void;
) => Promise<void>;
onInit?: (calendar: PowerCalendarAPI) => void;
onSelect?: TPowerCalendarOnSelect;
selected?: SelectedDays;
Expand All @@ -85,8 +85,8 @@ export interface PowerCalendarArgs {
}

export interface PowerCalendarDefaultBlock extends PowerCalendarAPI {
NavComponent: ComponentLike<any>;
DaysComponent: ComponentLike<any>;
NavComponent: ComponentLike<PowerCalendarNavComponent>;
DaysComponent: ComponentLike<PowerCalendarDaysComponent>;
}

export type CalendarDay =
Expand All @@ -109,8 +109,8 @@ export default class PowerCalendarComponent extends Component<PowerCalendarSigna
@tracked _calendarType: TCalendarType = 'single';
@tracked _selected?: SelectedDays;

navComponent: ComponentLike<any> = PowerCalendarNavComponent;
daysComponent: ComponentLike<any> = PowerCalendarDaysComponent;
navComponent = PowerCalendarNavComponent;
daysComponent = PowerCalendarDaysComponent;

// Lifecycle hooks
constructor(owner: Owner, args: PowerCalendarArgs) {
Expand All @@ -129,7 +129,7 @@ export default class PowerCalendarComponent extends Component<PowerCalendarSigna
get publicActions(): PowerCalendarActions {
return publicActionsObject(
this.args.onSelect,
this.select,
this.select.bind(this),
this.args.onCenterChange,
this.changeCenterTask,
this.currentCenter,
Expand Down Expand Up @@ -206,14 +206,17 @@ export default class PowerCalendarComponent extends Component<PowerCalendarSigna
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
window.__powerCalendars = window.__powerCalendars || {}; // TODO: weakmap??
// @ts-expect-error Property '__powerCalendars'
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
window.__powerCalendars[this.publicAPI.uniqueId] = this;
}
}

unregisterCalendar() {
// @ts-expect-error Property '__powerCalendars'
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (window && window.__powerCalendars?.[guidFor(this)]) {
// @ts-expect-error Property '__powerCalendars'
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
delete window.__powerCalendars[guidFor(this)];
}
}
Expand Down
4 changes: 2 additions & 2 deletions ember-power-calendar/src/components/power-calendar/days.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@ export default class PowerCalendarDaysComponent extends Component<PowerCalendarD
scheduleOnce(
'actions',
this,
this._updateFocused,
this._updateFocused.bind(this),
(e.target as HTMLElement).dataset['date'],
);
}

@action
handleDayBlur(): void {
scheduleOnce('actions', this, this._updateFocused, null);
scheduleOnce('actions', this, this._updateFocused.bind(this), null);
}

@action
Expand Down
7 changes: 2 additions & 5 deletions ember-power-calendar/src/test-support/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { run } from '@ember/runloop';
import { assert } from '@ember/debug';
import { click, settled, find } from '@ember/test-helpers';
import { formatDate } from '../utils.ts';
Expand Down Expand Up @@ -49,7 +48,7 @@ function findComponentInstance(
calendarGuid,
);
// @ts-expect-error Property '__powerCalendars'
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access
return window.__powerCalendars[calendarGuid];
}

Expand All @@ -68,9 +67,7 @@ export async function calendarCenter(
!!onCenterChange,
);
const publicAPI = calendarComponent.publicAPI;
run(() =>
publicAPI.actions.changeCenter!(newCenter, publicAPI, {} as MouseEvent),
);
await publicAPI.actions.changeCenter!(newCenter, publicAPI, {} as MouseEvent);
return settled();
}

Expand Down
2 changes: 2 additions & 0 deletions ember-power-calendar/src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import type EmberTruthRegistry from 'ember-truth-helpers/template-registry';
import type { EmbroiderUtilRegistry } from '@embroider/util';

export interface AssignRegistry {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
}

export interface ReadonlyRegistry {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
}

Expand Down