From 2b523a36c58b207a27dee4078237919d628dc588 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 12 Dec 2018 10:54:49 -0800 Subject: Check functions Async suffix --- packages/tslint-config/CHANGELOG.json | 8 +++++ .../tslint-config/rules/walkers/async_suffix.ts | 39 +++++++++++++--------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/packages/tslint-config/CHANGELOG.json b/packages/tslint-config/CHANGELOG.json index 9f504216c..9915d6f9a 100644 --- a/packages/tslint-config/CHANGELOG.json +++ b/packages/tslint-config/CHANGELOG.json @@ -1,4 +1,12 @@ [ + { + "version": "2.0.0", + "changes": [ + { + "note": "Improve async-suffix rule to check functions too, not just methods" + } + ] + }, { "version": "1.0.10", "changes": [ diff --git a/packages/tslint-config/rules/walkers/async_suffix.ts b/packages/tslint-config/rules/walkers/async_suffix.ts index eaec9c5f6..4e12152e8 100644 --- a/packages/tslint-config/rules/walkers/async_suffix.ts +++ b/packages/tslint-config/rules/walkers/async_suffix.ts @@ -3,24 +3,33 @@ import * as Lint from 'tslint'; import * as ts from 'typescript'; export class AsyncSuffixWalker extends Lint.RuleWalker { - public static FAILURE_STRING = 'async functions must have an Async suffix'; + public static FAILURE_STRING = 'async functions/methods must have an Async suffix'; + public visitFunctionDeclaration(node: ts.FunctionDeclaration): void { + this._visitFunctionOrMethodDeclaration(node); + super.visitFunctionDeclaration(node); + } public visitMethodDeclaration(node: ts.MethodDeclaration): void { - const methodNameNode = node.name; - const methodName = methodNameNode.getText(); - if (!_.isUndefined(node.type)) { - if (node.type.kind === ts.SyntaxKind.TypeReference) { - // tslint:disable-next-line:no-unnecessary-type-assertion - const returnTypeName = (node.type as ts.TypeReferenceNode).typeName.getText(); - if (returnTypeName === 'Promise' && !methodName.endsWith('Async')) { - const failure = this.createFailure( - methodNameNode.getStart(), - methodNameNode.getWidth(), - AsyncSuffixWalker.FAILURE_STRING, - ); - this.addFailure(failure); + this._visitFunctionOrMethodDeclaration(node); + super.visitMethodDeclaration(node); + } + private _visitFunctionOrMethodDeclaration(node: ts.MethodDeclaration | ts.FunctionDeclaration): void { + const nameNode = node.name; + if (!_.isUndefined(nameNode)) { + const name = nameNode.getText(); + if (!_.isUndefined(node.type)) { + if (node.type.kind === ts.SyntaxKind.TypeReference) { + // tslint:disable-next-line:no-unnecessary-type-assertion + const returnTypeName = (node.type as ts.TypeReferenceNode).typeName.getText(); + if (returnTypeName === 'Promise' && !name.endsWith('Async')) { + const failure = this.createFailure( + nameNode.getStart(), + nameNode.getWidth(), + AsyncSuffixWalker.FAILURE_STRING, + ); + this.addFailure(failure); + } } } } - super.visitMethodDeclaration(node); } } -- cgit v1.2.3 From 0e76d66f2457015bbc421e7ead2887008fb54ffc Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 12 Dec 2018 10:59:31 -0800 Subject: Fix linter errors --- contracts/test-utils/src/assertions.ts | 12 ++++++------ contracts/test-utils/src/test_with_reference.ts | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/contracts/test-utils/src/assertions.ts b/contracts/test-utils/src/assertions.ts index b1dec1281..f31651f4d 100644 --- a/contracts/test-utils/src/assertions.ts +++ b/contracts/test-utils/src/assertions.ts @@ -23,7 +23,7 @@ export type sendTransactionResult = Promise { +async function _getGanacheOrGethErrorAsync(ganacheError: string, gethError: string): Promise { if (_.isUndefined(nodeType)) { nodeType = await web3Wrapper.getNodeTypeAsync(); } @@ -38,15 +38,15 @@ async function _getGanacheOrGethError(ganacheError: string, gethError: string): } async function _getInsufficientFundsErrorMessageAsync(): Promise { - return _getGanacheOrGethError("sender doesn't have enough funds", 'insufficient funds'); + return _getGanacheOrGethErrorAsync("sender doesn't have enough funds", 'insufficient funds'); } async function _getTransactionFailedErrorMessageAsync(): Promise { - return _getGanacheOrGethError('revert', 'always failing transaction'); + return _getGanacheOrGethErrorAsync('revert', 'always failing transaction'); } async function _getContractCallFailedErrorMessageAsync(): Promise { - return _getGanacheOrGethError('revert', 'Contract call failed'); + return _getGanacheOrGethErrorAsync('revert', 'Contract call failed'); } /** @@ -54,7 +54,7 @@ async function _getContractCallFailedErrorMessageAsync(): Promise { * contract call. The exact error message depends on the backing Ethereum node. */ export async function getInvalidOpcodeErrorMessageForCallAsync(): Promise { - return _getGanacheOrGethError('invalid opcode', 'Contract call failed'); + return _getGanacheOrGethErrorAsync('invalid opcode', 'Contract call failed'); } /** @@ -65,7 +65,7 @@ export async function getInvalidOpcodeErrorMessageForCallAsync(): Promise { - return _getGanacheOrGethError(reason, 'always failing transaction'); + return _getGanacheOrGethErrorAsync(reason, 'always failing transaction'); } /** diff --git a/contracts/test-utils/src/test_with_reference.ts b/contracts/test-utils/src/test_with_reference.ts index b80be4a6c..75d15b0aa 100644 --- a/contracts/test-utils/src/test_with_reference.ts +++ b/contracts/test-utils/src/test_with_reference.ts @@ -26,7 +26,7 @@ type PromiseResult = Value | ErrorMessage; // TODO(albrow): This seems like a generic utility function that could exist in // lodash. We should replace it by a library implementation, or move it to our // own. -async function evaluatePromise(promise: Promise): Promise> { +async function evaluatePromiseAsync(promise: Promise): Promise> { try { return new Value(await promise); } catch (e) { @@ -93,10 +93,10 @@ export async function testWithReferenceFuncAsync( values: any[], ): Promise { // Measure correct behaviour - const expected = await evaluatePromise(referenceFuncAsync(...values)); + const expected = await evaluatePromiseAsync(referenceFuncAsync(...values)); // Measure actual behaviour - const actual = await evaluatePromise(testFuncAsync(...values)); + const actual = await evaluatePromiseAsync(testFuncAsync(...values)); // Compare behaviour if (expected instanceof ErrorMessage) { -- cgit v1.2.3 From a1186f052d7cff755f3076275b1d88735610b77f Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 12 Dec 2018 11:02:51 -0800 Subject: Add PR number --- packages/tslint-config/CHANGELOG.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/tslint-config/CHANGELOG.json b/packages/tslint-config/CHANGELOG.json index 9915d6f9a..eb2698a75 100644 --- a/packages/tslint-config/CHANGELOG.json +++ b/packages/tslint-config/CHANGELOG.json @@ -3,7 +3,8 @@ "version": "2.0.0", "changes": [ { - "note": "Improve async-suffix rule to check functions too, not just methods" + "note": "Improve async-suffix rule to check functions too, not just methods", + "pr": 1425 } ] }, -- cgit v1.2.3 From 1534505652aa12a385dee46f876c22cc4ce8621a Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 12 Dec 2018 15:32:41 -0800 Subject: Fix linter errors --- packages/pipeline/src/scripts/pull_competing_dex_trades.ts | 4 ++-- .../pipeline/src/scripts/pull_ddex_orderbook_snapshots.ts | 4 ++-- .../pipeline/src/scripts/pull_idex_orderbook_snapshots.ts | 4 ++-- packages/pipeline/src/scripts/pull_missing_blocks.ts | 12 ++++++------ .../pipeline/src/scripts/pull_oasis_orderbook_snapshots.ts | 4 ++-- .../pipeline/src/scripts/pull_paradex_orderbook_snapshots.ts | 4 ++-- packages/pipeline/src/scripts/pull_trusted_tokens.ts | 8 ++++---- packages/pipeline/src/scripts/update_relayer_info.ts | 4 ++-- 8 files changed, 22 insertions(+), 22 deletions(-) diff --git a/packages/pipeline/src/scripts/pull_competing_dex_trades.ts b/packages/pipeline/src/scripts/pull_competing_dex_trades.ts index 4e4c12dd0..1478d5615 100644 --- a/packages/pipeline/src/scripts/pull_competing_dex_trades.ts +++ b/packages/pipeline/src/scripts/pull_competing_dex_trades.ts @@ -15,11 +15,11 @@ let connection: Connection; (async () => { connection = await createConnection(ormConfig as ConnectionOptions); - await getAndSaveTrades(); + await getAndSaveTradesAsync(); process.exit(0); })().catch(handleError); -async function getAndSaveTrades(): Promise { +async function getAndSaveTradesAsync(): Promise { const apiKey = process.env.BLOXY_API_KEY; if (apiKey === undefined) { throw new Error('Missing required env var: BLOXY_API_KEY'); diff --git a/packages/pipeline/src/scripts/pull_ddex_orderbook_snapshots.ts b/packages/pipeline/src/scripts/pull_ddex_orderbook_snapshots.ts index 7868e9c5a..4e00f258f 100644 --- a/packages/pipeline/src/scripts/pull_ddex_orderbook_snapshots.ts +++ b/packages/pipeline/src/scripts/pull_ddex_orderbook_snapshots.ts @@ -25,7 +25,7 @@ let connection: Connection; const markets = await ddexSource.getActiveMarketsAsync(); for (const marketsChunk of R.splitEvery(MARKET_ORDERBOOK_REQUEST_BATCH_SIZE, markets)) { await Promise.all( - marketsChunk.map(async (market: DdexMarket) => getAndSaveMarketOrderbook(ddexSource, market)), + marketsChunk.map(async (market: DdexMarket) => getAndSaveMarketOrderbookAsync(ddexSource, market)), ); await new Promise(resolve => setTimeout(resolve, MILLISEC_MARKET_ORDERBOOK_REQUEST_DELAY)); } @@ -38,7 +38,7 @@ let connection: Connection; * @param ddexSource Data source which can query Ddex API. * @param market Object from Ddex API containing market data. */ -async function getAndSaveMarketOrderbook(ddexSource: DdexSource, market: DdexMarket): Promise { +async function getAndSaveMarketOrderbookAsync(ddexSource: DdexSource, market: DdexMarket): Promise { const orderBook = await ddexSource.getMarketOrderbookAsync(market.id); const observedTimestamp = Date.now(); diff --git a/packages/pipeline/src/scripts/pull_idex_orderbook_snapshots.ts b/packages/pipeline/src/scripts/pull_idex_orderbook_snapshots.ts index d47c1dd3f..490b17766 100644 --- a/packages/pipeline/src/scripts/pull_idex_orderbook_snapshots.ts +++ b/packages/pipeline/src/scripts/pull_idex_orderbook_snapshots.ts @@ -27,7 +27,7 @@ let connection: Connection; logUtils.log(`Got ${markets.length} markets.`); for (const marketsChunk of R.splitEvery(MARKET_ORDERBOOK_REQUEST_BATCH_SIZE, markets)) { await Promise.all( - marketsChunk.map(async (marketId: string) => getAndSaveMarketOrderbook(idexSource, marketId)), + marketsChunk.map(async (marketId: string) => getAndSaveMarketOrderbookAsync(idexSource, marketId)), ); await new Promise(resolve => setTimeout(resolve, MILLISEC_MARKET_ORDERBOOK_REQUEST_DELAY)); } @@ -40,7 +40,7 @@ let connection: Connection; * @param idexSource Data source which can query Idex API. * @param marketId String representing market of interest, eg. 'ETH_TIC'. */ -async function getAndSaveMarketOrderbook(idexSource: IdexSource, marketId: string): Promise { +async function getAndSaveMarketOrderbookAsync(idexSource: IdexSource, marketId: string): Promise { logUtils.log(`${marketId}: Retrieving orderbook.`); const orderBook = await idexSource.getMarketOrderbookAsync(marketId); const observedTimestamp = Date.now(); diff --git a/packages/pipeline/src/scripts/pull_missing_blocks.ts b/packages/pipeline/src/scripts/pull_missing_blocks.ts index 275141a12..a5203824c 100644 --- a/packages/pipeline/src/scripts/pull_missing_blocks.ts +++ b/packages/pipeline/src/scripts/pull_missing_blocks.ts @@ -27,7 +27,7 @@ let connection: Connection; rpcUrl: INFURA_ROOT_URL, }); const web3Source = new Web3Source(provider); - await getAllMissingBlocks(web3Source); + await getAllMissingBlocksAsync(web3Source); process.exit(0); })().catch(handleError); @@ -35,23 +35,23 @@ interface MissingBlocksResponse { block_number: string; } -async function getAllMissingBlocks(web3Source: Web3Source): Promise { +async function getAllMissingBlocksAsync(web3Source: Web3Source): Promise { const blocksRepository = connection.getRepository(Block); let fromBlock = EXCHANGE_START_BLOCK; while (true) { - const blockNumbers = await getMissingBlockNumbers(fromBlock); + const blockNumbers = await getMissingBlockNumbersAsync(fromBlock); if (blockNumbers.length === 0) { // There are no more missing blocks. We're done. break; } - await getAndSaveBlocks(web3Source, blocksRepository, blockNumbers); + await getAndSaveBlocksAsync(web3Source, blocksRepository, blockNumbers); fromBlock = Math.max(...blockNumbers) + 1; } const totalBlocks = await blocksRepository.count(); console.log(`Done saving blocks. There are now ${totalBlocks} total blocks.`); } -async function getMissingBlockNumbers(fromBlock: number): Promise { +async function getMissingBlockNumbersAsync(fromBlock: number): Promise { console.log(`Checking for missing blocks starting at ${fromBlock}...`); // Note(albrow): The easiest way to get all the blocks we need is to // consider all the events tables together in a single query. If this query @@ -76,7 +76,7 @@ async function getMissingBlockNumbers(fromBlock: number): Promise { return blockNumbers; } -async function getAndSaveBlocks( +async function getAndSaveBlocksAsync( web3Source: Web3Source, blocksRepository: Repository, blockNumbers: number[], diff --git a/packages/pipeline/src/scripts/pull_oasis_orderbook_snapshots.ts b/packages/pipeline/src/scripts/pull_oasis_orderbook_snapshots.ts index 0ffa5fd47..c4dcf6c83 100644 --- a/packages/pipeline/src/scripts/pull_oasis_orderbook_snapshots.ts +++ b/packages/pipeline/src/scripts/pull_oasis_orderbook_snapshots.ts @@ -27,7 +27,7 @@ let connection: Connection; logUtils.log(`Got ${markets.length} markets.`); for (const marketsChunk of R.splitEvery(MARKET_ORDERBOOK_REQUEST_BATCH_SIZE, markets)) { await Promise.all( - marketsChunk.map(async (market: OasisMarket) => getAndSaveMarketOrderbook(oasisSource, market)), + marketsChunk.map(async (market: OasisMarket) => getAndSaveMarketOrderbookAsync(oasisSource, market)), ); await new Promise(resolve => setTimeout(resolve, MILLISEC_MARKET_ORDERBOOK_REQUEST_DELAY)); } @@ -40,7 +40,7 @@ let connection: Connection; * @param oasisSource Data source which can query Oasis API. * @param marketId String identifying market we want data for. eg. 'REPAUG'. */ -async function getAndSaveMarketOrderbook(oasisSource: OasisSource, market: OasisMarket): Promise { +async function getAndSaveMarketOrderbookAsync(oasisSource: OasisSource, market: OasisMarket): Promise { logUtils.log(`${market.id}: Retrieving orderbook.`); const orderBook = await oasisSource.getMarketOrderbookAsync(market.id); const observedTimestamp = Date.now(); diff --git a/packages/pipeline/src/scripts/pull_paradex_orderbook_snapshots.ts b/packages/pipeline/src/scripts/pull_paradex_orderbook_snapshots.ts index bae1fbede..34345f355 100644 --- a/packages/pipeline/src/scripts/pull_paradex_orderbook_snapshots.ts +++ b/packages/pipeline/src/scripts/pull_paradex_orderbook_snapshots.ts @@ -29,7 +29,7 @@ let connection: Connection; const tokenInfoResponse = await paradexSource.getTokenInfoAsync(); const extendedMarkets = addTokenAddresses(markets, tokenInfoResponse); await Promise.all( - extendedMarkets.map(async (market: ParadexMarket) => getAndSaveMarketOrderbook(paradexSource, market)), + extendedMarkets.map(async (market: ParadexMarket) => getAndSaveMarketOrderbookAsync(paradexSource, market)), ); process.exit(0); })().catch(handleError); @@ -70,7 +70,7 @@ function addTokenAddresses( * @param paradexSource Data source which can query the Paradex API. * @param market Object from the Paradex API with information about the market in question. */ -async function getAndSaveMarketOrderbook(paradexSource: ParadexSource, market: ParadexMarket): Promise { +async function getAndSaveMarketOrderbookAsync(paradexSource: ParadexSource, market: ParadexMarket): Promise { const paradexOrderbookResponse = await paradexSource.getMarketOrderbookAsync(market.symbol); const observedTimestamp = Date.now(); diff --git a/packages/pipeline/src/scripts/pull_trusted_tokens.ts b/packages/pipeline/src/scripts/pull_trusted_tokens.ts index 1befc4437..5906deee6 100644 --- a/packages/pipeline/src/scripts/pull_trusted_tokens.ts +++ b/packages/pipeline/src/scripts/pull_trusted_tokens.ts @@ -16,12 +16,12 @@ let connection: Connection; (async () => { connection = await createConnection(ormConfig as ConnectionOptions); - await getMetamaskTrustedTokens(); - await getZeroExTrustedTokens(); + await getMetamaskTrustedTokensAsync(); + await getZeroExTrustedTokensAsync(); process.exit(0); })().catch(handleError); -async function getMetamaskTrustedTokens(): Promise { +async function getMetamaskTrustedTokensAsync(): Promise { // tslint:disable-next-line:no-console console.log('Getting latest metamask trusted tokens list ...'); const trustedTokensRepository = connection.getRepository(TokenMetadata); @@ -37,7 +37,7 @@ async function getMetamaskTrustedTokens(): Promise { console.log('Done saving metamask trusted tokens.'); } -async function getZeroExTrustedTokens(): Promise { +async function getZeroExTrustedTokensAsync(): Promise { // tslint:disable-next-line:no-console console.log('Getting latest 0x trusted tokens list ...'); const trustedTokensRepository = connection.getRepository(TokenMetadata); diff --git a/packages/pipeline/src/scripts/update_relayer_info.ts b/packages/pipeline/src/scripts/update_relayer_info.ts index f8918728d..41d29b385 100644 --- a/packages/pipeline/src/scripts/update_relayer_info.ts +++ b/packages/pipeline/src/scripts/update_relayer_info.ts @@ -17,11 +17,11 @@ let connection: Connection; (async () => { connection = await createConnection(ormConfig as ConnectionOptions); - await getRelayers(); + await getRelayersAsync(); process.exit(0); })().catch(handleError); -async function getRelayers(): Promise { +async function getRelayersAsync(): Promise { console.log('Getting latest relayer info...'); const relayerRepository = connection.getRepository(Relayer); const relayerSource = new RelayerRegistrySource(RELAYER_REGISTRY_URL); -- cgit v1.2.3