Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Commit

Permalink
linterの修正とコード整形
Browse files Browse the repository at this point in the history
  • Loading branch information
proshunsuke committed Mar 11, 2021
1 parent 13460c7 commit 1694b1d
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 32 deletions.
20 changes: 19 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ module.exports = {
'no-void': ['error', {allowAsStatement: true}],
'no-console': ['error', {allow: ['info', 'error']}],
'@typescript-eslint/unbound-method': 'off',
'no-restricted-syntax': ['error', {allow: ['ForOfStatement']}]
'no-restricted-syntax': [
'error',
{
selector: 'ForInStatement',
message: 'for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array.',
},
// {
// selector: 'ForOfStatement',
// message: 'iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations.',
// },
{
selector: 'LabeledStatement',
message: 'Labels are a form of GOTO; using them makes code confusing and hard to maintain and understand.',
},
{
selector: 'WithStatement',
message: '`with` is disallowed in strict mode because it makes code impossible to predict and optimize.',
},
],
},
};
16 changes: 7 additions & 9 deletions src/calendar.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
ScheduleInterface,
SiteCalendarInterface,
} from './calendarInterface';
import { ScheduleInterface, SiteCalendarInterface } from './calendarInterface';
import Retry from './lib/retry';

export default class Calendar {
Expand All @@ -23,10 +20,11 @@ export default class Calendar {
* @param {ScheduleInterface} schedule
* @param calendarIds
*/
static createEvent(schedule: ScheduleInterface, calendarIds: SiteCalendarInterface[]): void {
const siteCalendarId:
| SiteCalendarInterface
| undefined = calendarIds.find(
static createEvent(
schedule: ScheduleInterface,
calendarIds: SiteCalendarInterface[]
): void {
const siteCalendarId: SiteCalendarInterface | undefined = calendarIds.find(
(id) => id.type === schedule.type
);
if (typeof siteCalendarId === 'undefined') {
Expand All @@ -44,7 +42,7 @@ export default class Calendar {
schedule.title,
new Date(schedule.startTime),
new Date(schedule.endTime),
{description: schedule.description}
{ description: schedule.description }
);
} else {
CalendarApp.getCalendarById(calendarId).createAllDayEvent(
Expand Down
16 changes: 10 additions & 6 deletions src/oneMonthSchedule.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import dayjs from 'dayjs';
import Calendar from './calendar';
import {
ScheduleInterface,
SiteCalendarInterface,
} from './calendarInterface';
import { ScheduleInterface, SiteCalendarInterface } from './calendarInterface';
import Retry from './lib/retry';
import 'regenerator-runtime';

Expand All @@ -15,7 +12,11 @@ export default class OneMonthSchedule {
* @param siteCalendarIds
* @returns {Promise<void>}
*/
static async setSchedule(date: dayjs.Dayjs, calendarUrl: string, siteCalendarIds: SiteCalendarInterface[]): Promise<void> {
static async setSchedule(
date: dayjs.Dayjs,
calendarUrl: string,
siteCalendarIds: SiteCalendarInterface[]
): Promise<void> {
const customUrl: string = calendarUrl + date.format('YYYYMMDD');

const scheduleJson = await OneMonthSchedule.getScheduleJson(customUrl);
Expand Down Expand Up @@ -52,7 +53,10 @@ export default class OneMonthSchedule {
* @param {dayjs.Dayjs} date
* @param siteCalendarIds
*/
private static delete1MonthCalendarEvents(date: dayjs.Dayjs, siteCalendarIds: SiteCalendarInterface[]) {
private static delete1MonthCalendarEvents(
date: dayjs.Dayjs,
siteCalendarIds: SiteCalendarInterface[]
) {
let deleteEventCallCount = 0;
siteCalendarIds.forEach((siteCalendarId) => {
if (process.env.ENV !== 'production') return;
Expand Down
6 changes: 3 additions & 3 deletions src/schedule.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import KeyakiSiteSchedule from './sites/keyakizaka/keyakiSiteSchedule';
import {SiteScheduleInterface} from './sites/siteSchedule';
import { SiteScheduleInterface } from './sites/siteSchedule';

export default class Schedule {
static async setSchedule(): Promise<void> {
const siteScheduleList: SiteScheduleInterface[] = [
new KeyakiSiteSchedule,
new KeyakiSiteSchedule(),
// new KeyakiSiteSchedule
];
for await(const siteSchedule of siteScheduleList) {
for await (const siteSchedule of siteScheduleList) {
await siteSchedule.setSiteSchedule();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/sites/keyakizaka/keyakiObjects.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {SiteCalendarInterface} from '../../calendarInterface';
import { SiteCalendarInterface } from '../../calendarInterface';

export const getKeyakiCalendarUrl =
process.env.ENV === 'production'
Expand Down
4 changes: 2 additions & 2 deletions src/sites/keyakizaka/keyakiSiteSchedule.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SiteSchedule from '../siteSchedule';
import {getKeyakiCalendarUrl, keyakiCalendarIds} from './keyakiObjects';
import {SiteCalendarInterface} from '../../calendarInterface';
import { getKeyakiCalendarUrl, keyakiCalendarIds } from './keyakiObjects';
import { SiteCalendarInterface } from '../../calendarInterface';

export default class KeyakiSiteSchedule extends SiteSchedule {
// eslint-disable-next-line class-methods-use-this
Expand Down
11 changes: 7 additions & 4 deletions src/sites/siteSchedule.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import dayjs from 'dayjs';
import OneMonthSchedule from '../oneMonthSchedule';
import Trigger, { TERMINATION_MINUTES } from '../lib/trigger';
import {SiteCalendarInterface} from '../calendarInterface';
import { SiteCalendarInterface } from '../calendarInterface';

export interface SiteScheduleInterface {
setSiteSchedule(): Promise<void>;
siteCalendarUrl(): string;
siteCalendarIds(): SiteCalendarInterface[];
}

export default abstract class SiteSchedule implements SiteScheduleInterface{
export default abstract class SiteSchedule implements SiteScheduleInterface {
async setSiteSchedule(): Promise<void> {
const beginningOfNexYearMonth = dayjs().startOf('month').add(1, 'year');
let targetBeginningOfMonth = SiteSchedule.getTargetBeginningOfMonth();
const startDate = dayjs();

while (targetBeginningOfMonth.isBefore(beginningOfNexYearMonth)) {
// eslint-disable-next-line no-await-in-loop
await OneMonthSchedule.setSchedule(targetBeginningOfMonth, this.siteCalendarUrl(), this.siteCalendarIds());
await OneMonthSchedule.setSchedule(
targetBeginningOfMonth,
this.siteCalendarUrl(),
this.siteCalendarIds()
);
targetBeginningOfMonth = targetBeginningOfMonth.add(1, 'month');
if (Trigger.hasExceededTerminationMinutes(startDate)) {
Trigger.setTrigger(targetBeginningOfMonth);
Expand Down Expand Up @@ -48,4 +52,3 @@ export default abstract class SiteSchedule implements SiteScheduleInterface{

abstract siteCalendarIds(): SiteCalendarInterface[];
}

2 changes: 1 addition & 1 deletion tests/calendar.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Calendar from '../src/calendar';
import { ScheduleInterface } from '../src/calendarInterface';
import {keyakiCalendarIds} from '../src/sites/keyakizaka/keyakiObjects';
import { keyakiCalendarIds } from '../src/sites/keyakizaka/keyakiObjects';

/**
*
Expand Down
35 changes: 30 additions & 5 deletions tests/oneMonthSchedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import dayjs from 'dayjs';
import fetchMock from 'jest-fetch-mock';
import OneMonthSchedule from '../src/oneMonthSchedule';
import Calendar from '../src/calendar';
import {getKeyakiCalendarUrl, keyakiCalendarIds} from '../src/sites/keyakizaka/keyakiObjects';
import {
getKeyakiCalendarUrl,
keyakiCalendarIds,
} from '../src/sites/keyakizaka/keyakiObjects';

function getScheduleJson() {
return '[{"title":"欅坂46 こちら有楽町星空放送局","start":"2019-12-01","className":"media","description":"欅坂46 こちら有楽町星空放送局"},{"title":"テレビ東京「欅って、書けない?」","start":"2019-12-01","className":"media","description":"テレビ東京「欅って、書けない?」"}]';
Expand Down Expand Up @@ -35,7 +38,13 @@ describe('setSchedule', () => {
})) as jest.Mock;

const date = dayjs('2019-12-01');
await expect(OneMonthSchedule.setSchedule(date, getKeyakiCalendarUrl, keyakiCalendarIds)).resolves.not.toThrow();
await expect(
OneMonthSchedule.setSchedule(
date,
getKeyakiCalendarUrl,
keyakiCalendarIds
)
).resolves.not.toThrow();
expect(Calendar.deleteEvent).toBeCalledTimes(
keyakiCalendarIds.length * date.endOf('month').date()
);
Expand All @@ -61,7 +70,13 @@ describe('setSchedule', () => {
});

const date = dayjs('2019-12-01');
await expect(OneMonthSchedule.setSchedule(date, getKeyakiCalendarUrl, keyakiCalendarIds)).rejects.toThrow();
await expect(
OneMonthSchedule.setSchedule(
date,
getKeyakiCalendarUrl,
keyakiCalendarIds
)
).rejects.toThrow();
expect(Calendar.createEvent).not.toBeCalled();
});
it('カレンダーの作成に失敗した場合に例外が起きて後続の処理が止まること', async () => {
Expand All @@ -83,15 +98,25 @@ describe('setSchedule', () => {
});

const date = dayjs('2019-12-01');
await expect(OneMonthSchedule.setSchedule(date, getKeyakiCalendarUrl, keyakiCalendarIds)).rejects.toThrow();
await expect(
OneMonthSchedule.setSchedule(
date,
getKeyakiCalendarUrl,
keyakiCalendarIds
)
).rejects.toThrow();
});
it('production環境では無かった場合にUrlFetchApp.fetchが呼ばれないこと', async () => {
process.env.ENV = 'local';
UrlFetchApp.fetch = jest.fn();
fetchMock.mockOnce(getScheduleJson());

const date = dayjs('2019-12-01');
await OneMonthSchedule.setSchedule(date, getKeyakiCalendarUrl, keyakiCalendarIds);
await OneMonthSchedule.setSchedule(
date,
getKeyakiCalendarUrl,
keyakiCalendarIds
);
expect(UrlFetchApp.fetch).not.toBeCalled();
});
});

0 comments on commit 1694b1d

Please sign in to comment.