Skip to content

Commit

Permalink
Merge pull request #519 from jakubroztocil/fix-precommit
Browse files Browse the repository at this point in the history
Fix precommit & fix lint warnings
  • Loading branch information
davidgoli authored Jun 9, 2022
2 parents d352fb3 + ac84245 commit d7decd7
Show file tree
Hide file tree
Showing 12 changed files with 322 additions and 35 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module.exports = {
'@typescript-eslint/naming-convention': 'off',
'@typescript-eslint/no-empty-function': 'error',
'@typescript-eslint/no-extra-semi': 'off',
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-floating-promises': 'error',
'@typescript-eslint/no-misused-new': 'error',
'@typescript-eslint/no-unnecessary-qualifier': 'error',
Expand Down
3 changes: 1 addition & 2 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

yarn lint
yarn format
yarn lint-staged
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
],
"all": true
},
"lint-staged": {
"*.ts": [
"yarn lint",
"yarn format"
]
},
"devDependencies": {
"@types/assert": "^1.4.3",
"@types/chai": "^4.2.7",
Expand All @@ -59,6 +65,7 @@
"eslint-plugin-jsdoc": "^39.3.2",
"eslint-plugin-prettier": "^4.0.0",
"husky": "^8.0.1",
"lint-staged": "^13.0.1",
"mocha": "^10.0.0",
"mockdate": "^3.0.5",
"nyc": "^15.1.0",
Expand Down
4 changes: 2 additions & 2 deletions src/dateutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ export namespace dateutil {
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0
}

export const isDate = function (value: any): value is Date {
export const isDate = function (value: unknown): value is Date {
return value instanceof Date
}

export const isValidDate = function (value: any): value is Date {
export const isValidDate = function (value: unknown): value is Date {
return isDate(value) && !isNaN(value.getTime())
}

Expand Down
6 changes: 3 additions & 3 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ export const isPresent = function <T>(
return value !== null && value !== undefined
}

export const isNumber = function (value?: any): value is number {
export const isNumber = function (value: unknown): value is number {
return typeof value === 'number'
}

export const isWeekdayStr = function (value?: any): value is WeekdayStr {
return ALL_WEEKDAYS.indexOf(value) >= 0
export const isWeekdayStr = function (value: unknown): value is WeekdayStr {
return typeof value === 'string' && ALL_WEEKDAYS.includes(value as WeekdayStr)
}

export const isArray = Array.isArray
Expand Down
2 changes: 1 addition & 1 deletion src/iterinfo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export default class Iterinfo {
return [range(this.yearlen), 0, this.yearlen]
}

mdayset(_: any, month: number, __: any) {
mdayset(_: unknown, month: number) {
const start = this.mrange[month - 1]
const end = this.mrange[month]
const set = repeat<number | null>(null, this.yearlen)
Expand Down
2 changes: 1 addition & 1 deletion src/masks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { range, repeat, clone } from './helpers'
import { range, repeat } from './helpers'

// =============================================================================
// Date masks
Expand Down
10 changes: 6 additions & 4 deletions src/optionstostring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function optionsToString(options: Partial<Options>) {
if (!includes(defaultKeys, keys[i])) continue

let key = keys[i].toUpperCase()
const value: any = options[keys[i]]
const value = options[keys[i]]
let outValue = ''

if (!isPresent(value) || (isArray(value) && !value.length)) continue
Expand Down Expand Up @@ -45,7 +45,9 @@ export function optionsToString(options: Partial<Options>) {
*/
key = 'BYDAY'
outValue = toArray<Weekday | number[] | number>(value)
outValue = toArray<Weekday | number[] | number>(
value as Weekday | number[] | number
)
.map((wday) => {
if (wday instanceof Weekday) {
return wday
Expand All @@ -61,11 +63,11 @@ export function optionsToString(options: Partial<Options>) {

break
case 'DTSTART':
dtstart = buildDtstart(value, options.tzid)
dtstart = buildDtstart(value as number, options.tzid)
break

case 'UNTIL':
outValue = dateutil.timeToUntilString(value, !options.tzid)
outValue = dateutil.timeToUntilString(value as number, !options.tzid)
break

default:
Expand Down
9 changes: 6 additions & 3 deletions src/parsestring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function parseDtstart(line: string) {
return options
}

const [_, tzid, dtstart] = dtstartWithZone
const [, tzid, dtstart] = dtstartWithZone

if (tzid) {
options.tzid = tzid
Expand All @@ -40,7 +40,7 @@ function parseLine(rfcString: string) {
return parseRrule(rfcString)
}

const [_, key] = header
const [, key] = header
switch (key.toUpperCase()) {
case 'RRULE':
case 'EXRULE':
Expand Down Expand Up @@ -135,7 +135,10 @@ function parseWeekday(value: string) {
}

// -1MO, +3FR, 1SO, 13TU ...
const parts = day.match(/^([+-]?\d{1,2})([A-Z]{2})$/)!
const parts = day.match(/^([+-]?\d{1,2})([A-Z]{2})$/)
if (!parts || parts.length < 3) {
throw new SyntaxError(`Invalid weekday string: ${day}`)
}
const n = Number(parts[1])
const wdaypart = parts[2] as keyof typeof Days
const wday = Days[wdaypart].weekday
Expand Down
28 changes: 21 additions & 7 deletions test/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const datetimeUTC = function (
*/
export const parse = function (str: string) {
const parts = str.match(/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})/)
const [_, y, m, d, h, i, s] = parts
const [, y, m, d, h, i, s] = parts
const year = Number(y)
const month = Number(m[0] === '0' ? m[1] : m) - 1
const day = Number(d[0] === '0' ? d[1] : d)
Expand All @@ -75,19 +75,25 @@ export const parse = function (str: string) {
}

interface TestRecurring {
(m: string, testObj: any, expectedDates: Date | Date[]): void
only: (...args: any[]) => void
skip: (...args: any[]) => void
(m: string, testObj: unknown, expectedDates: Date | Date[]): void
only: (...args: unknown[]) => void
skip: (...args: unknown[]) => void
}

interface TestObj {
rrule: RRule
method: 'all' | 'between' | 'before' | 'after'
args: unknown[]
}

export const testRecurring = function (
msg: string,
testObj: any,
testObj: TestObj | RRule | (() => TestObj),
expectedDates: Date | Date[],
itFunc: TestFunction | ExclusiveTestFunction = it
) {
let rule: any
let method: string
let rule: RRule
let method: 'all' | 'before' | 'between' | 'after'
let args: unknown[]

if (typeof testObj === 'function') {
Expand Down Expand Up @@ -115,12 +121,16 @@ export const testRecurring = function (
rule.toString() +
']'
} else {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
msg = msg + ' ' + rule.toString()
}

itFunc(msg, function () {
const ctx = this.test.ctx
let time = Date.now()
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
let actualDates = rule[method](...args)
time = Date.now() - time

Expand Down Expand Up @@ -166,11 +176,15 @@ export const testRecurring = function (
// Test fromText()/toText().
const str = rule.toString()
const text = rule.toText()
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const rrule2 = RRule.fromText(text, rule.options.dtstart)
const text2 = rrule2.toText()
expect(text2).to.equal(text, 'toText() == fromText(toText()).toText()')

// Test fromText()/toString().
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const rrule3 = RRule.fromText(text, rule.options.dtstart)
expect(rrule3.toString()).to.equal(
str,
Expand Down
10 changes: 5 additions & 5 deletions test/rruleset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ describe('RRuleSet', function () {
rrule: set,
method: 'all',
args: [
function (_: any, count: number) {
function (_: unknown, count: number) {
return count < 3
},
],
Expand Down Expand Up @@ -813,15 +813,15 @@ describe('RRuleSet', function () {
it('throws an error if non-rrules are added via rrule or exrule', () => {
const set = new RRuleSet()

expect(() => set.rrule('foo' as any)).to.throw()
expect(() => set.exrule('foo' as any)).to.throw()
expect(() => set.rrule('foo' as unknown as RRule)).to.throw()
expect(() => set.exrule('foo' as unknown as RRule)).to.throw()
})

it('throws an error if non-dates are added via rdate or exdate', () => {
const set = new RRuleSet()

expect(() => set.rdate('foo' as any)).to.throw()
expect(() => set.exdate('foo' as any)).to.throw()
expect(() => set.rdate('foo' as unknown as Date)).to.throw()
expect(() => set.exdate('foo' as unknown as Date)).to.throw()
})

describe('getters', () => {
Expand Down
Loading

0 comments on commit d7decd7

Please sign in to comment.