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

Enable strict typescript #614

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module.exports = {
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-floating-promises': 'error',
'@typescript-eslint/no-misused-new': 'error',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-unnecessary-qualifier': 'error',
'@typescript-eslint/no-unnecessary-type-assertion': 'error',
'@typescript-eslint/no-unused-expressions': [
Expand Down Expand Up @@ -94,6 +95,7 @@ module.exports = {
'no-caller': 'error',
'no-case-declarations': 'off',
'no-cond-assign': 'error',
'no-console': 'error',
'no-constant-condition': 'error',
'no-control-regex': 'error',
'no-duplicate-imports': 'error',
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testRegex: '(\\.|/)(test|spec)\\.(j|t)s(x?)$',
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
"lint": "yarn eslint . --fix --config .eslintrc.js",
"format": "yarn prettier --write .",
"format-check": "yarn prettier --check .",
"typecheck": "tsc --noEmit",
"typecheck:watch": "tsc --noEmit --watch",
"run-ts": "TS_NODE_PROJECT=tsconfig.json node --loader ts-node/esm",
"test": "jest **/*.test.ts",
"test-ci": "yarn run-ts ./node_modules/.bin/nyc jest **/*.test.ts"
"test": "jest",
"test-ci": "yarn run-ts ./node_modules/.bin/nyc jest"
},
"nyc": {
"extension": [
Expand Down
5 changes: 3 additions & 2 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class Cache {
if (what === 'all') {
this.all = value as Date[]
} else {
args = args || {}
args._value = value
this[what].push(args as IterArgs)
}
Expand All @@ -65,7 +66,7 @@ export class Cache {
const findCacheDiff = function (item: IterArgs) {
for (let i = 0; i < argsKeys.length; i++) {
const key = argsKeys[i]
if (!argsMatch(args[key], item[key])) {
if (!argsMatch(args?.[key], item[key])) {
return true
}
}
Expand All @@ -89,7 +90,7 @@ export class Cache {
if (!cached && this.all) {
// Not in the cache, but we already know all the occurrences,
// so we can find the correct dates from the cached ones.
const iterResult = new IterResult(what, args)
const iterResult = new IterResult(what, args ?? {})
for (let i = 0; i < (this.all as Date[]).length; i++) {
if (!iterResult.accept((this.all as Date[])[i])) break
}
Expand Down
2 changes: 1 addition & 1 deletion src/dateutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ const dateTZtoISO8601 = function (date: Date, timeZone: string) {
return dateStr.replace(' ', 'T') + 'Z'
}

export const dateInTimeZone = function (date: Date, timeZone: string) {
export const dateInTimeZone = function (date: Date, timeZone?: string) {
const localTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone
// Date constructor can only reliably parse dates in ISO8601 format
const dateInLocalTZ = new Date(dateTZtoISO8601(date, localTimeZone))
Expand Down
2 changes: 1 addition & 1 deletion src/datewithzone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class DateWithZone {
}

public rezonedDate() {
if (this.isUTC) {
if (this.isUTC || !this.tzid) {
return this.date
}

Expand Down
13 changes: 7 additions & 6 deletions src/iter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ export function iter<M extends QueryMethodTypes>(
}

const date = fromOrdinal(ii.yearordinal + currentDay)
for (let k = 0; k < timeset.length; k++) {
const time = timeset[k]
for (const time of timeset) {
const res = combine(date, time)
if (until && res > until) {
return emitResult(iterResult)
Expand Down Expand Up @@ -131,7 +130,7 @@ function isFiltered(

return (
(notEmpty(bymonth) && !includes(bymonth, ii.mmask[currentDay])) ||
(notEmpty(byweekno) && !ii.wnomask[currentDay]) ||
(notEmpty(byweekno) && !ii.wnomask?.[currentDay]) ||
(notEmpty(byweekday) && !includes(byweekday, ii.wdaymask[currentDay])) ||
(notEmpty(ii.nwdaymask) && !ii.nwdaymask[currentDay]) ||
(byeaster !== null && !includes(ii.eastermask, currentDay)) ||
Expand Down Expand Up @@ -167,9 +166,11 @@ function removeFilteredDays(
for (let dayCounter = start; dayCounter < end; dayCounter++) {
const currentDay = dayset[dayCounter]

filtered = isFiltered(ii, currentDay, options)
if (currentDay !== null) {
filtered = isFiltered(ii, currentDay, options)

if (filtered) dayset[currentDay] = null
if (filtered) dayset[currentDay] = null
}
}

return filtered
Expand All @@ -179,7 +180,7 @@ function makeTimeset(
ii: Iterinfo,
counterDate: DateTime,
options: ParsedOptions
): Time[] | null {
): Time[] {
const { freq, byhour, byminute, bysecond } = options

if (freqIsDailyOrGreater(freq)) {
Expand Down
23 changes: 14 additions & 9 deletions src/iterinfo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ export type GetDayset = () => DaySet
// =============================================================================

export default class Iterinfo {
public yearinfo: YearInfo
public monthinfo: MonthInfo
public eastermask: number[] | null
public yearinfo!: YearInfo
public monthinfo!: MonthInfo
public eastermask!: number[]

// eslint-disable-next-line no-empty-function
constructor(private options: ParsedOptions) {}
Expand All @@ -32,7 +32,7 @@ export default class Iterinfo {
notEmpty(options.bynweekday) &&
(month !== this.lastmonth || year !== this.lastyear)
) {
const { yearlen, mrange, wdaymask } = this.yearinfo
const { yearlen, mrange, wdaymask } = this.yearinfo ?? {}
this.monthinfo = rebuildMonth(
year,
month,
Expand Down Expand Up @@ -153,15 +153,20 @@ export default class Iterinfo {
getdayset(freq: Frequency): (y: number, m: number, d: number) => DaySet {
switch (freq) {
case Frequency.YEARLY:
return this.ydayset.bind(this)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return this.ydayset.bind(this) as any
case Frequency.MONTHLY:
return this.mdayset.bind(this)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return this.mdayset.bind(this) as any
case Frequency.WEEKLY:
return this.wdayset.bind(this)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return this.wdayset.bind(this) as any
case Frequency.DAILY:
return this.ddayset.bind(this)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return this.ddayset.bind(this) as any
default:
return this.ddayset.bind(this)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return this.ddayset.bind(this) as any
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/iterinfo/monthinfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ export function rebuildMonth(
const first = rang[0]
const last = rang[1] - 1

for (let k = 0; k < options.bynweekday.length; k++) {
for (const [wday, n] of options.bynweekday ?? []) {
let i
const [wday, n] = options.bynweekday[k]
if (n < 0) {
i = last + (n + 1) * 7
i -= pymod(wdaymask[i] - wday, 7)
Expand Down
12 changes: 7 additions & 5 deletions src/iterresult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ export default class IterResult<M extends QueryMethodTypes> {

if (method === 'between') {
this.maxDate = args.inc
? args.before
: new Date(args.before.getTime() - 1)
this.minDate = args.inc ? args.after : new Date(args.after.getTime() + 1)
? args.before!
: new Date(args.before!.getTime() - 1)
this.minDate = args.inc
? args.after!
: new Date(args.after!.getTime() + 1)
} else if (method === 'before') {
this.maxDate = args.inc ? args.dt : new Date(args.dt.getTime() - 1)
this.maxDate = args.inc ? args.dt! : new Date(args.dt!.getTime() - 1)
} else if (method === 'after') {
this.minDate = args.inc ? args.dt : new Date(args.dt.getTime() + 1)
this.minDate = args.inc ? args.dt! : new Date(args.dt!.getTime() + 1)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/iterset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function iterSet<M extends QueryMethodTypes>(
}

if (iterResult.method === 'between') {
evalExdate(iterResult.args.after, iterResult.args.before)
evalExdate(iterResult.args.after!, iterResult.args.before!)
iterResult.accept = function (date) {
const dt = Number(date)
if (!_exdateHash[dt]) {
Expand Down
12 changes: 6 additions & 6 deletions src/nlp/parsetext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { WeekdayStr } from '../weekday'

class Parser {
private readonly rules: { [k: string]: RegExp }
public text: string
public symbol: string | null
public value: RegExpExecArray | null
public text!: string
public symbol!: string | null
public value!: RegExpExecArray | null
private done = true

constructor(rules: { [k: string]: RegExp }) {
Expand All @@ -30,7 +30,7 @@ class Parser {

nextSymbol() {
let best: RegExpExecArray | null
let bestSymbol: string
let bestSymbol = ''

this.symbol = null
this.value = null
Expand Down Expand Up @@ -390,7 +390,7 @@ export default function parseText(text: string, language: Language = ENGLISH) {
ttr.nextSymbol()
return ttr.accept('last') ? -3 : 3
case 'nth':
const v = parseInt(ttr.value[1], 10)
const v = parseInt(ttr.value![1], 10)
if (v < -366 || v > 366) throw new Error('Nth out of range: ' + v)

ttr.nextSymbol()
Expand Down Expand Up @@ -431,7 +431,7 @@ export default function parseText(text: string, language: Language = ENGLISH) {
if (!date) throw new Error('Cannot parse until date:' + ttr.text)
options.until = new Date(date)
} else if (ttr.accept('for')) {
options.count = parseInt(ttr.value[0], 10)
options.count = parseInt(ttr.value![0], 10)
ttr.expect('number')
// ttr.expect('times')
}
Expand Down
Loading