Skip to content

Commit

Permalink
Upgrade to Babel 7 (#300)
Browse files Browse the repository at this point in the history
* Upgrade to Babel 7

* fix lint and type check

* fix class initialization to fix test
  • Loading branch information
kpman authored and chentsulin committed Jul 9, 2018
1 parent e8ace5c commit 6f7c673
Show file tree
Hide file tree
Showing 37 changed files with 1,244 additions and 946 deletions.
19 changes: 0 additions & 19 deletions .babelrc

This file was deleted.

23 changes: 23 additions & 0 deletions .babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: '7.6',
},
},
],
'@babel/preset-flow',
],
plugins: [
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-object-rest-spread',
],
env: {
production: {
plugins: ['lodash'],
ignore: [/__tests__/],
},
},
};
29 changes: 16 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"utils.js"
],
"scripts": {
"build": "npm run clean && cross-env NODE_ENV=production babel src -d lib --ignore __tests__ --copy-files",
"build": "npm run clean && cross-env NODE_ENV=production babel src -d lib --copy-files",
"check": "flow check",
"clean": "rimraf lib",
"precommit": "lint-staged",
Expand Down Expand Up @@ -97,30 +97,33 @@
"warning": "^4.0.1"
},
"devDependencies": {
"@babel/cli": "^7.0.0-beta.52",
"@babel/core": "^7.0.0-beta.52",
"@babel/plugin-proposal-class-properties": "^7.0.0-beta.52",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0-beta.52",
"@babel/preset-env": "^7.0.0-beta.52",
"@babel/preset-flow": "^7.0.0-beta.52",
"axios-mock-adapter": "^1.15.0",
"babel-cli": "^6.26.0",
"babel-eslint": "^8.2.3",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "^8.2.5",
"babel-jest": "^23.2.0",
"babel-plugin-lodash": "^3.3.4",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-flow": "^6.23.0",
"cross-env": "^5.2.0",
"eslint": "^4.19.1",
"eslint-config-prettier": "^2.9.0",
"eslint-config-yoctol-base": "^0.15.1",
"eslint-config-yoctol-base": "^0.16.0",
"eslint-plugin-flowtype": "^2.49.3",
"eslint-plugin-import": "^2.12.0",
"eslint-plugin-prettier": "^2.6.0",
"eslint-plugin-import": "^2.13.0",
"eslint-plugin-prettier": "^2.6.2",
"flow-bin": "^0.74.0",
"husky": "^0.14.3",
"jest": "^23.1.0",
"jest": "^23.3.0",
"jest-create-mock-instance": "^1.0.3",
"jest-junit": "^5.1.0",
"lint-staged": "^7.2.0",
"ngrok": "^3.0.1",
"once": "^1.4.0",
"prettier": "^1.13.5",
"prettier": "^1.13.7",
"prettier-package-json": "^1.6.0",
"rimraf": "^2.6.2",
"supertest": "^3.1.0"
Expand Down Expand Up @@ -161,7 +164,7 @@
"testPathIgnorePatterns": [
"/node_modules/",
"/examples/",
"lib/cli/providers/sh/test.js",
"/lib/",
"src/cli/providers/sh/test.js"
],
"reporters": [
Expand Down
1 change: 0 additions & 1 deletion src/__tests__/browser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ describe('browser', () => {
});

it('export connectors', () => {
expect(core.Connector).toBeDefined();
expect(core.TestConnector).toBeDefined();
});

Expand Down
1 change: 0 additions & 1 deletion src/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ describe('core', () => {
});

it('export connectors', () => {
expect(core.Connector).toBeDefined();
expect(core.ConsoleConnector).toBeDefined();
expect(core.TestConnector).toBeDefined();
expect(core.MessengerConnector).toBeDefined();
Expand Down
6 changes: 2 additions & 4 deletions src/bot/ConsoleConnector.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
/* @flow */

import ConsoleContext from '../context/ConsoleContext';
import { type ConsoleClient } from '../context/ConsoleClient';
import ConsoleEvent, { type ConsoleRawEvent } from '../context/ConsoleEvent';
import type { Session } from '../session/Session';

import type { Connector } from './Connector';

type ConsoleRequestBody = ConsoleRawEvent;

export type ConsoleClient = {
sendText(text: string): void,
};

type ConstructorOptions = {|
client?: ConsoleClient,
fallbackMethods?: boolean,
|};

export default class ConsoleConnector implements Connector<ConsoleRequestBody> {
_client: ConsoleClient;

_fallbackMethods: boolean;

constructor({ client, fallbackMethods }: ConstructorOptions = {}) {
Expand Down
9 changes: 7 additions & 2 deletions src/bot/LineConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ type ConstructorOptions = {|

export default class LineConnector implements Connector<LineRequestBody> {
_client: LineClient;

_channelSecret: string;

_shouldBatch: ?boolean;

_sendMethod: ?string;

constructor({
Expand Down Expand Up @@ -72,9 +75,11 @@ export default class LineConnector implements Connector<LineRequestBody> {
const { source } = body.events[0];
if (source.type === 'user') {
return source.userId;
} else if (source.type === 'group') {
}
if (source.type === 'group') {
return source.groupId;
} else if (source.type === 'room') {
}
if (source.type === 'room') {
return source.roomId;
}
throw new TypeError(
Expand Down
8 changes: 7 additions & 1 deletion src/bot/MessengerConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,15 @@ type ConstructorOptions = {|
export default class MessengerConnector
implements Connector<MessengerRequestBody> {
_client: MessengerClient;

_appSecret: string;

_mapPageToAccessToken: ?(pageId: string) => ?Promise<string>;

_verifyToken: ?string;

_batchConfig: ?Object;

_batchQueue: ?Object;

constructor({
Expand Down Expand Up @@ -161,7 +166,8 @@ export default class MessengerConnector
_getPageIdFromRawEvent(rawEvent: MessengerRawEvent): ?string {
if (rawEvent.message && rawEvent.message.is_echo && rawEvent.sender) {
return rawEvent.sender.id;
} else if (rawEvent.recipient) {
}
if (rawEvent.recipient) {
return rawEvent.recipient.id;
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/bot/SlackConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type ConstructorOptions = {|

export default class SlackConnector implements Connector<SlackRequestBody> {
_client: SlackOAuthClient;

_verificationToken: string;

constructor({ accessToken, client, verificationToken }: ConstructorOptions) {
Expand All @@ -59,7 +60,8 @@ export default class SlackConnector implements Connector<SlackRequestBody> {
_getRawEventFromRequest(body: SlackRequestBody): SlackRawEvent {
if (body.event) {
return (((body: any): EventsAPIBody).event: Message);
} else if (body.payload && typeof body.payload === 'string') {
}
if (body.payload && typeof body.payload === 'string') {
return (JSON.parse(body.payload): InteractiveMessageEvent);
}
// for RTM WebSocket messages
Expand Down
1 change: 1 addition & 0 deletions src/bot/TelegramBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type PollingOptions = {

export default class TelegramBot extends Bot {
_offset: ?number;

_shouldGetUpdates: boolean = false;

constructor({
Expand Down
32 changes: 16 additions & 16 deletions src/bot/TelegramConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ import type { Session } from '../session/Session';

import type { Connector } from './Connector';

export type TelegramUser = {
id: number,
first_name: string,
last_name?: string,
username?: string,
language_code?: string,
};

export type TelegramRequestBody = TelegramRawEvent;

type ConstructorOptions = {|
Expand Down Expand Up @@ -46,24 +38,32 @@ export default class TelegramConnector
getUniqueSessionKey(body: TelegramRequestBody): string {
if (body.message) {
return `${body.message.chat.id}`;
} else if (body.edited_message) {
}
if (body.edited_message) {
return `${body.edited_message.chat.id}`;
} else if (body.channel_post) {
}
if (body.channel_post) {
return `${body.channel_post.chat.id}`;
} else if (body.edited_channel_post) {
}
if (body.edited_channel_post) {
return `${body.edited_channel_post.chat.id}`;
} else if (body.inline_query) {
}
if (body.inline_query) {
return `${body.inline_query.from.id}`;
} else if (body.chosen_inline_result) {
}
if (body.chosen_inline_result) {
return `${body.chosen_inline_result.from.id}`;
} else if (body.callback_query) {
}
if (body.callback_query) {
if (body.callback_query.message) {
return `${body.callback_query.message.chat.id}`;
}
return `${body.callback_query.from.id}`;
} else if (body.shipping_query) {
}
if (body.shipping_query) {
return `${body.shipping_query.from.id}`;
} else if (body.pre_checkout_query) {
}
if (body.pre_checkout_query) {
return `${body.pre_checkout_query.from.id}`;
}
return '';
Expand Down
11 changes: 2 additions & 9 deletions src/bot/TestConnector.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
/* @flow */

import TestContext from '../context/TestContext';
import type { TestClient } from '../context/TestClient';
import TestEvent, { type TestRawEvent } from '../context/TestEvent';
import type { Session } from '../session/Session';

import type { Connector } from './Connector';

type TestRequestBody = TestRawEvent;

export type TestClient = {
calls: Array<{
name: string,
args: Array<any>,
}>,
callMethod(name: string, args: Array<any>): void,
mockReset(): void,
};

type ConstructorOptions = {|
client?: TestClient,
fallbackMethods?: boolean,
Expand All @@ -37,6 +29,7 @@ const testClient = {

export default class TestConnector implements Connector<TestRequestBody> {
_client: TestClient;

_fallbackMethods: boolean;

constructor({ client, fallbackMethods }: ConstructorOptions = {}) {
Expand Down
1 change: 1 addition & 0 deletions src/bot/ViberConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type ConstructorOptions = {|

export default class ViberConnector implements Connector<ViberRequestBody> {
_accessToken: ?string;

_client: ViberClient;

constructor({ accessToken, client }: ConstructorOptions) {
Expand Down
1 change: 0 additions & 1 deletion src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export { default as Bot } from './bot/Bot';
export { default as TestBot } from './bot/TestBot';

/* Connector */
export { default as Connector } from './bot/Connector';
export { default as TestConnector } from './bot/TestConnector';

/* HandlerBuilder */
Expand Down
3 changes: 2 additions & 1 deletion src/cli/providers/sh/init/generateIndexFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ const newBotLines = (bot, platform, sessionStore) => {
const runBotLines = (platform, server) => {
if (platform === 'console') {
return 'bot.createRuntime();';
} else if (server === 'micro') {
}
if (server === 'micro') {
return 'module.exports = createRequestHandler(bot);';
}
return `const server = createServer(bot);
Expand Down
3 changes: 3 additions & 0 deletions src/context/ConsoleClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type ConsoleClient = {
sendText(text: string): void,
};
14 changes: 8 additions & 6 deletions src/context/ConsoleContext.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* @flow */
import sleep from 'delay';

import type { ConsoleClient } from '../bot/ConsoleConnector';
import type { Session } from '../session/Session';
import { type Session } from '../session/Session';

import Context from './Context';
import { type ConsoleClient } from './ConsoleClient';
import ConsoleEvent from './ConsoleEvent';
import type { PlatformContext } from './PlatformContext';
import { type PlatformContext } from './PlatformContext';

type Options = {|
client: ConsoleClient,
Expand All @@ -23,9 +23,11 @@ const methodBlackList = [
];

export default class ConsoleContext extends Context implements PlatformContext {
_client: ConsoleClient;
_event: ConsoleEvent;
_session: ?Session;
_client: ConsoleClient = this._client;

_event: ConsoleEvent = this._event;

_session: ?Session = this._session;

_fallbackMethods: boolean = false;

Expand Down
Loading

0 comments on commit 6f7c673

Please sign in to comment.