Replace lodash with built-ins where possible to reduce bundle size (#1766)

* add tslint rule to disallow lodash.isUndefined

* add tslint rule to disallow lodash.isNull

* apply fixes
This commit is contained in:
Xianny
2019-04-10 09:36:32 -07:00
committed by GitHub
parent 49d951b7be
commit 7423028fea
299 changed files with 1249 additions and 1038 deletions

View File

@@ -20,6 +20,7 @@
"clean": "shx rm -rf lib generated_docs",
"migrate": "npm run build; node lib/src/cli.js migrate",
"lint": "tslint --format stylish --project .",
"fix": "tslint --fix --format stylish --project .",
"test:circleci": "yarn test:coverage",
"docs:json": "typedoc --excludePrivate --excludeExternals --target ES5 --tsconfig typedoc-tsconfig.json --json $JSON_FILE_PATH $PROJECT_FILES"
},

View File

@@ -30,11 +30,12 @@ const SEPARATOR = ',';
default: false,
})
.help().argv;
const contracts = _.isUndefined(argv.contracts)
? undefined
: argv.contracts === DEFAULT_CONTRACTS_LIST
? DEFAULT_CONTRACTS_LIST
: argv.contracts.split(SEPARATOR);
const contracts =
argv.contracts === undefined
? undefined
: argv.contracts === DEFAULT_CONTRACTS_LIST
? DEFAULT_CONTRACTS_LIST
: argv.contracts.split(SEPARATOR);
const opts = {
contractsDir: argv.contractsDir,
artifactsDir: argv.artifactsDir,

View File

@@ -233,17 +233,18 @@ export class Compiler {
continue;
}
contractPathToData[contractSource.path] = contractData;
const solcVersion = _.isUndefined(this._solcVersionIfExists)
? semver.maxSatisfying(_.keys(solcJSReleases), parseSolidityVersionRange(contractSource.source))
: this._solcVersionIfExists;
if (_.isNull(solcVersion)) {
const solcVersion =
this._solcVersionIfExists === undefined
? semver.maxSatisfying(_.keys(solcJSReleases), parseSolidityVersionRange(contractSource.source))
: this._solcVersionIfExists;
if (solcVersion === null) {
throw new Error(
`Couldn't find any solidity version satisfying the constraint ${parseSolidityVersionRange(
contractSource.source,
)}`,
);
}
const isFirstContractWithThisVersion = _.isUndefined(versionToInputs[solcVersion]);
const isFirstContractWithThisVersion = versionToInputs[solcVersion] === undefined;
if (isFirstContractWithThisVersion) {
versionToInputs[solcVersion] = {
standardInput: {
@@ -290,7 +291,7 @@ export class Compiler {
fullSolcVersion = solcJSReleases[solcVersion];
compilerOutput = await compileSolcJSAsync(solcVersion, input.standardInput, this._isOfflineMode);
}
if (!_.isUndefined(compilerOutput.errors)) {
if (compilerOutput.errors !== undefined) {
printCompilationErrorsAndWarnings(compilerOutput.errors);
}
compilerOutput.sources = makeContractPathsRelative(
@@ -308,7 +309,7 @@ export class Compiler {
const contractName = contractPathToData[contractPath].contractName;
const compiledContract = compilerOutput.contracts[contractPath][contractName];
if (_.isUndefined(compiledContract)) {
if (compiledContract === undefined) {
throw new Error(
`Contract ${contractName} not found in ${contractPath}. Please make sure your contract has the same name as it's file name`,
);
@@ -334,7 +335,7 @@ export class Compiler {
return compilerOutputs;
}
private _shouldCompile(contractData: ContractData): boolean {
if (_.isUndefined(contractData.currentArtifactIfExists)) {
if (contractData.currentArtifactIfExists === undefined) {
return true;
} else {
const currentArtifact = contractData.currentArtifactIfExists as ContractArtifact;
@@ -380,7 +381,7 @@ export class Compiler {
};
let newArtifact: ContractArtifact;
if (!_.isUndefined(currentArtifactIfExists)) {
if (currentArtifactIfExists !== undefined) {
const currentArtifact = currentArtifactIfExists as ContractArtifact;
newArtifact = {
...currentArtifact,

View File

@@ -59,7 +59,7 @@ export async function createDirIfDoesNotExistAsync(dirPath: string): Promise<voi
export function parseSolidityVersionRange(source: string): string {
const SOLIDITY_VERSION_RANGE_REGEX = /pragma\s+solidity\s+(.*);/;
const solcVersionRangeMatch = source.match(SOLIDITY_VERSION_RANGE_REGEX);
if (_.isNull(solcVersionRangeMatch)) {
if (solcVersionRangeMatch === null) {
throw new Error('Could not find Solidity version range in source');
}
const solcVersionRange = solcVersionRangeMatch[1];
@@ -78,7 +78,7 @@ export function parseSolidityVersionRange(source: string): string {
export function getNormalizedErrMsg(errMsg: string): string {
const SOLIDITY_FILE_EXTENSION_REGEX = /(.*\.sol):/;
const errPathMatch = errMsg.match(SOLIDITY_FILE_EXTENSION_REGEX);
if (_.isNull(errPathMatch)) {
if (errPathMatch === null) {
// This can occur if solidity outputs a general warning, e.g
// Warning: This is a pre-release compiler version, please do not use it in production.
return errMsg;
@@ -102,9 +102,9 @@ export function parseDependencies(contractSource: ContractSource): string[] {
const dependencies: string[] = [];
const lines = source.split('\n');
_.forEach(lines, line => {
if (!_.isNull(line.match(IMPORT_REGEX))) {
if (line.match(IMPORT_REGEX) !== null) {
const dependencyMatch = line.match(DEPENDENCY_PATH_REGEX);
if (!_.isNull(dependencyMatch)) {
if (dependencyMatch !== null) {
let dependencyPath = dependencyMatch[1];
if (dependencyPath.startsWith('.')) {
dependencyPath = path.join(path.dirname(contractSource.path), dependencyPath);
@@ -126,7 +126,7 @@ export async function getSolcJSReleasesAsync(isOfflineMode: boolean): Promise<Bi
if (isOfflineMode) {
return constants.SOLC_BIN_PATHS;
}
if (_.isUndefined(solcJSReleasesCache)) {
if (solcJSReleasesCache === undefined) {
const versionList = await fetch('https://ethereum.github.io/solc-bin/bin/list.json');
const versionListJSON = await versionList.json();
solcJSReleasesCache = versionListJSON.releases;
@@ -314,7 +314,7 @@ function recursivelyGatherDependencySources(
importPath = path.resolve(`/${contractFolder}`, importPath).replace('/', '');
}
if (_.isUndefined(sourcesToAppendTo[importPath])) {
if (sourcesToAppendTo[importPath] === undefined) {
sourcesToAppendTo[importPath] = { id: fullSources[importPath].id };
sourceCodesToAppendTo[importPath] = resolver.resolve(importPath).source;
@@ -339,7 +339,7 @@ function recursivelyGatherDependencySources(
export async function getSolcJSAsync(solcVersion: string, isOfflineMode: boolean): Promise<solc.SolcInstance> {
const solcJSReleases = await getSolcJSReleasesAsync(isOfflineMode);
const fullSolcVersion = solcJSReleases[solcVersion];
if (_.isUndefined(fullSolcVersion)) {
if (fullSolcVersion === undefined) {
throw new Error(`${solcVersion} is not a known compiler version`);
}
const compilerBinFilename = path.join(constants.SOLC_BIN_DIR, fullSolcVersion);
@@ -369,13 +369,13 @@ export async function getSolcJSAsync(solcVersion: string, isOfflineMode: boolean
* @param compiledContract The standard JSON output section for a contract. Geth modified in place.
*/
export function addHexPrefixToContractBytecode(compiledContract: solc.StandardContractOutput): void {
if (!_.isUndefined(compiledContract.evm)) {
if (!_.isUndefined(compiledContract.evm.bytecode) && !_.isUndefined(compiledContract.evm.bytecode.object)) {
if (compiledContract.evm !== undefined) {
if (compiledContract.evm.bytecode !== undefined && compiledContract.evm.bytecode.object !== undefined) {
compiledContract.evm.bytecode.object = ethUtil.addHexPrefix(compiledContract.evm.bytecode.object);
}
if (
!_.isUndefined(compiledContract.evm.deployedBytecode) &&
!_.isUndefined(compiledContract.evm.deployedBytecode.object)
compiledContract.evm.deployedBytecode !== undefined &&
compiledContract.evm.deployedBytecode.object !== undefined
) {
compiledContract.evm.deployedBytecode.object = ethUtil.addHexPrefix(
compiledContract.evm.deployedBytecode.object,