Add some missed underscores, update changelog and comments

This commit is contained in:
Brandon Millman
2017-12-20 18:59:46 -05:00
parent 23052a5c2e
commit 2d53b7d9a4
7 changed files with 28 additions and 34 deletions

View File

@@ -127,7 +127,7 @@ export class OrderStateWatcher {
}
delete this._orderByOrderHash[orderHash];
delete this._orderStateByOrderHashCache[orderHash];
const exchange = (this._orderFilledCancelledLazyStore as any).exchange as ExchangeWrapper;
const exchange = (this._orderFilledCancelledLazyStore as any)._exchange as ExchangeWrapper;
const zrxTokenAddress = exchange.getZRXTokenAddress();
this._removeFromDependentOrderHashes(signedOrder.maker, zrxTokenAddress, orderHash);
this._removeFromDependentOrderHashes(signedOrder.maker, signedOrder.makerTokenAddress, orderHash);
@@ -351,7 +351,7 @@ export class OrderStateWatcher {
}
}
private _getZRXTokenAddress(): string {
const exchange = (this._orderFilledCancelledLazyStore as any).exchange as ExchangeWrapper;
const exchange = (this._orderFilledCancelledLazyStore as any)._exchange as ExchangeWrapper;
const zrxTokenAddress = exchange.getZRXTokenAddress();
return zrxTokenAddress;
}

View File

@@ -81,7 +81,7 @@ export class OrderStateUtils {
// If we pass it from the instantiator - there is no opportunity to get it there
// because JS doesn't support async constructors.
// Moreover - it's cached under the hood so it's equivalent to an async constructor.
const exchange = (this._orderFilledCancelledLazyStore as any).exchange as ExchangeWrapper;
const exchange = (this._orderFilledCancelledLazyStore as any)._exchange as ExchangeWrapper;
const zrxTokenAddress = exchange.getZRXTokenAddress();
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
const makerBalance = await this._balanceAndProxyAllowanceLazyStore.getBalanceAsync(

View File

@@ -65,7 +65,7 @@ describe('ExchangeTransferSimulator', () => {
await exchangeTransferSimulator.transferFromAsync(
exampleTokenAddress, sender, recipient, transferAmount, TradeSide.Taker, TransferType.Trade,
);
const store = (exchangeTransferSimulator as any).store;
const store = (exchangeTransferSimulator as any)._store;
const senderBalance = await store.getBalanceAsync(exampleTokenAddress, sender);
const recipientBalance = await store.getBalanceAsync(exampleTokenAddress, recipient);
const senderProxyAllowance = await store.getProxyAllowanceAsync(exampleTokenAddress, sender);
@@ -81,7 +81,7 @@ describe('ExchangeTransferSimulator', () => {
await exchangeTransferSimulator.transferFromAsync(
exampleTokenAddress, sender, recipient, transferAmount, TradeSide.Taker, TransferType.Trade,
);
const store = (exchangeTransferSimulator as any).store;
const store = (exchangeTransferSimulator as any)._store;
const senderBalance = await store.getBalanceAsync(exampleTokenAddress, sender);
const recipientBalance = await store.getBalanceAsync(exampleTokenAddress, recipient);
const senderProxyAllowance = await store.getProxyAllowanceAsync(exampleTokenAddress, sender);

View File

@@ -18,7 +18,7 @@ export class TokenUtils {
return zrxToken;
}
public getWethTokenOrThrow(): Token {
const wethToken = _.find(this.tokens, {symbol: WETH_TOKEN_SYMBOL});
const wethToken = _.find(this._tokens, {symbol: WETH_TOKEN_SYMBOL});
if (_.isUndefined(wethToken)) {
throw new Error(InternalZeroExError.WethNotInTokenRegistry);
}

View File

@@ -15,28 +15,28 @@ export class Contract implements Web3.ContractInstance {
// and we don't know their types in advance
[name: string]: any;
constructor(web3ContractInstance: Web3.ContractInstance, defaults: Partial<Web3.TxData>) {
this.contract = web3ContractInstance;
this._contract = web3ContractInstance;
this.address = web3ContractInstance.address;
this.abi = web3ContractInstance.abi;
this.defaults = defaults;
this.populateEvents();
this.populateFunctions();
this.validator = new SchemaValidator();
this._defaults = defaults;
this._populateEvents();
this._populateFunctions();
this._validator = new SchemaValidator();
}
private _populateFunctions(): void {
const functionsAbi = _.filter(this.abi, abiPart => abiPart.type === AbiType.Function);
_.forEach(functionsAbi, (functionAbi: Web3.MethodAbi) => {
if (functionAbi.constant) {
const cbStyleCallFunction = this.contract[functionAbi.name].call;
const cbStyleCallFunction = this._contract[functionAbi.name].call;
this[functionAbi.name] = {
callAsync: promisify(cbStyleCallFunction, this.contract),
callAsync: promisify(cbStyleCallFunction, this._contract),
};
} else {
const cbStyleFunction = this.contract[functionAbi.name];
const cbStyleEstimateGasFunction = this.contract[functionAbi.name].estimateGas;
const cbStyleFunction = this._contract[functionAbi.name];
const cbStyleEstimateGasFunction = this._contract[functionAbi.name].estimateGas;
this[functionAbi.name] = {
estimateGasAsync: promisify(cbStyleEstimateGasFunction, this.contract),
sendTransactionAsync: this.promisifyWithDefaultParams(cbStyleFunction),
estimateGasAsync: promisify(cbStyleEstimateGasFunction, this._contract),
sendTransactionAsync: this._promisifyWithDefaultParams(cbStyleFunction),
};
}
});
@@ -44,7 +44,7 @@ export class Contract implements Web3.ContractInstance {
private _populateEvents(): void {
const eventsAbi = _.filter(this.abi, abiPart => abiPart.type === AbiType.Event);
_.forEach(eventsAbi, (eventAbi: Web3.EventAbi) => {
this[eventAbi.name] = this.contract[eventAbi.name];
this[eventAbi.name] = this._contract[eventAbi.name];
});
}
private _promisifyWithDefaultParams(fn: (...args: any[]) => void): (...args: any[]) => Promise<any> {
@@ -52,11 +52,11 @@ export class Contract implements Web3.ContractInstance {
const promise = new Promise((resolve, reject) => {
const lastArg = args[args.length - 1];
let txData: Partial<Web3.TxData> = {};
if (this.isTxData(lastArg)) {
if (this._isTxData(lastArg)) {
txData = args.pop();
}
txData = {
...this.defaults,
...this._defaults,
...txData,
};
const callback = (err: Error, data: any) => {
@@ -68,14 +68,14 @@ export class Contract implements Web3.ContractInstance {
};
args.push(txData);
args.push(callback);
fn.apply(this.contract, args);
fn.apply(this._contract, args);
});
return promise;
};
return promisifiedWithDefaultParams;
}
private _isTxData(lastArg: any): boolean {
const isValid = this.validator.isValid(lastArg, schemas.txDataSchema);
const isValid = this._validator.isValid(lastArg, schemas.txDataSchema);
return isValid;
}
}

View File

@@ -3,6 +3,7 @@
v0.x.x - TBD
------------------------
* Added custom 'underscore-privates' rule, requiring underscores to be prepended to private variable names
* Because our tools can be used in both a TS and JS environment, we want to make the private methods of any public facing interface show up at the bottom of auto-complete lists. Additionally, we wanted to remain consistent with respect to our usage of underscores in order to enforce this rule with a linter rule, rather then manual code reviews.
v0.3.0 - _December 20, 2017_
------------------------

View File

@@ -1,7 +1,7 @@
import * as Lint from 'tslint';
import * as ts from 'typescript';
const UNDERSCORE = '_'.charCodeAt(0);
const UNDERSCORE = '_';
type RelevantClassMember =
| ts.MethodDeclaration
@@ -9,15 +9,15 @@ type RelevantClassMember =
| ts.GetAccessorDeclaration
| ts.SetAccessorDeclaration;
// Based on: https://github.com/DanielRosenwasser/underscore-privates-tslint-rule
// Copied from: https://github.com/DanielRosenwasser/underscore-privates-tslint-rule
// The version on github is not published on npm
export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING = 'private members name must be prefixed with an underscore';
public static FAILURE_STRING = 'private members must be prefixed with an underscore';
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk);
}
}
function walk(ctx: Lint.WalkContext<void>): void {
traverse(ctx.sourceFile);
@@ -26,23 +26,19 @@ function walk(ctx: Lint.WalkContext<void>): void {
return ts.forEachChild(node, traverse);
}
}
function checkNodeForViolations(ctx: Lint.WalkContext<void>, node: ts.Node): void {
if (!isRelevantClassMember(node)) {
return;
}
// The declaration might have a computed property name or a numeric name.
const name = node.name;
if (!nameIsIdentifier(name)) {
return;
}
if (!nameStartsWithUnderscore(name.text) && memberIsPrivate(node)) {
ctx.addFailureAtNode(name, Rule.FAILURE_STRING);
}
}
function isRelevantClassMember(node: ts.Node): node is RelevantClassMember {
switch (node.kind) {
case ts.SyntaxKind.MethodDeclaration:
@@ -54,15 +50,12 @@ function isRelevantClassMember(node: ts.Node): node is RelevantClassMember {
return false;
}
}
function nameStartsWithUnderscore(text: string) {
return text.charCodeAt(0) === UNDERSCORE;
return text.charCodeAt(0) === UNDERSCORE.charCodeAt(0);
}
function memberIsPrivate(node: ts.Declaration) {
return Lint.hasModifier(node.modifiers, ts.SyntaxKind.PrivateKeyword);
}
function nameIsIdentifier(node: ts.Node): node is ts.Identifier {
return node.kind === ts.SyntaxKind.Identifier;
}