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

@@ -9,6 +9,7 @@
"types": "lib/index.d.ts",
"scripts": {
"lint": "tslint --format stylish --project .",
"fix": "tslint --fix --format stylish --project .",
"build": "tsc -b",
"build:ci": "yarn build",
"clean": "shx rm -rf lib"

View File

@@ -87,7 +87,7 @@ export class DocReference extends React.Component<DocReferenceProps, DocReferenc
const sortedEligibleVersions = eligibleVersions.sort(semver.rcompare.bind(semver));
const closestVersion = sortedEligibleVersions[0];
const markdownFileIfExists = this.props.docsInfo.sectionNameToMarkdownByVersion[closestVersion][sectionName];
if (!_.isUndefined(markdownFileIfExists)) {
if (markdownFileIfExists !== undefined) {
// Special-case replace the `introduction` sectionName with the package name
const isIntroductionSection = sectionName === 'introduction';
const headerSize = isIntroductionSection ? HeaderSizes.H1 : HeaderSizes.H3;
@@ -103,7 +103,7 @@ export class DocReference extends React.Component<DocReferenceProps, DocReferenc
}
const docSection = this.props.docAgnosticFormat[sectionName];
if (_.isUndefined(docSection)) {
if (docSection === undefined) {
return null;
}
@@ -194,15 +194,15 @@ export class DocReference extends React.Component<DocReferenceProps, DocReferenc
<div>{functionDefs}</div>
</div>
)}
{!_.isUndefined(docSection.events) && docSection.events.length > 0 && (
{docSection.events !== undefined && docSection.events.length > 0 && (
<div>
<h2 style={headerStyle}>Events</h2>
<div>{eventDefs}</div>
</div>
)}
{!_.isUndefined(docSection.externalExportToLink) &&
{docSection.externalExportToLink !== undefined &&
this._renderExternalExports(docSection.externalExportToLink)}
{!_.isUndefined(typeDefs) && typeDefs.length > 0 && (
{typeDefs !== undefined && typeDefs.length > 0 && (
<div>
<div>{typeDefs}</div>
</div>
@@ -247,7 +247,7 @@ export class DocReference extends React.Component<DocReferenceProps, DocReferenc
networkToAddressByContractName,
(addressByContractName: AddressByContractName, networkName: string) => {
const contractAddress = addressByContractName[sectionName];
if (_.isUndefined(contractAddress)) {
if (contractAddress === undefined) {
return null;
}
const linkIfExists = sharedUtils.getEtherScanLinkIfExists(

View File

@@ -9,7 +9,7 @@ export interface EnumProps {
export const Enum = (props: EnumProps) => {
const values = _.map(props.values, value => {
const defaultValueIfAny = !_.isUndefined(value.defaultValue) ? ` = ${value.defaultValue}` : '';
const defaultValueIfAny = value.defaultValue !== undefined ? ` = ${value.defaultValue}` : '';
return `\n\t${value.name}${defaultValueIfAny},`;
});
return (

View File

@@ -24,7 +24,7 @@ export const Interface: React.SFC<InterfaceProps> = (props: InterfaceProps): any
return (
<span key={`property-${property.name}-${property.type}-${type.name}-${i}`}>
{property.name}:{' '}
{property.type && !_.isUndefined(property.type.method) ? (
{property.type && property.type.method !== undefined ? (
<Signature
name={property.type.method.name}
returnType={property.type.method.returnType}
@@ -50,7 +50,7 @@ export const Interface: React.SFC<InterfaceProps> = (props: InterfaceProps): any
</span>
);
});
const hasIndexSignature = !_.isUndefined(type.indexSignature);
const hasIndexSignature = type.indexSignature !== undefined;
if (hasIndexSignature) {
const is = type.indexSignature;
const param = (

View File

@@ -66,15 +66,16 @@ export const Signature: React.SFC<SignatureProps> = (props: SignatureProps) => {
paramStringArray.pop();
}
const methodName = props.shouldHideMethodName ? '' : props.name;
const typeParameterIfExists = _.isUndefined(props.typeParameter)
? undefined
: renderTypeParameter(
props.typeParameter,
props.docsInfo,
sectionName,
props.isInPopover,
props.typeDefinitionByName,
);
const typeParameterIfExists =
props.typeParameter === undefined
? undefined
: renderTypeParameter(
props.typeParameter,
props.docsInfo,
sectionName,
props.isInPopover,
props.typeDefinitionByName,
);
return (
<span style={{ fontSize: 15 }}>
{props.callPath}
@@ -109,7 +110,7 @@ function renderParameters(
): React.ReactNode[] {
const params = _.map(parameters, (p: Parameter, i: number) => {
const isOptional = p.isOptional;
const hasDefaultValue = !_.isUndefined(p.defaultValue);
const hasDefaultValue = p.defaultValue !== undefined;
const type = (
<Type
type={p.type}
@@ -145,7 +146,7 @@ function renderTypeParameter(
const typeParam = (
<span>
{`<${typeParameter.name}`}
{!_.isUndefined(typeParameter.type) && (
{typeParameter.type !== undefined && (
<span>
{' extends '}
<Type

View File

@@ -44,7 +44,7 @@ export class SignatureBlock extends React.Component<SignatureBlockProps, Signatu
const method = this.props.method;
const isFallback = (method as SolidityMethod).isFallback;
const hasExclusivelyNamedParams = !_.isUndefined(_.find(method.parameters, p => !_.isEmpty(p.name)));
const hasExclusivelyNamedParams = _.find(method.parameters, p => !_.isEmpty(p.name)) !== undefined;
return (
<div
id={`${this.props.sectionName}-${method.name}`}

View File

@@ -104,7 +104,7 @@ export const Type: React.SFC<TypeProps> = (props: TypeProps): any => {
break;
case TypeDocTypes.Reflection:
if (!_.isUndefined(type.method)) {
if (type.method !== undefined) {
typeName = (
<Signature
name={type.method.name}
@@ -119,7 +119,7 @@ export const Type: React.SFC<TypeProps> = (props: TypeProps): any => {
isInPopover={props.isInPopover}
/>
);
} else if (!_.isUndefined(type.indexSignature)) {
} else if (type.indexSignature !== undefined) {
const is = type.indexSignature;
const param = (
<span key={`indexSigParams-${is.keyName}-${is.keyType}-${type.name}`}>
@@ -204,8 +204,8 @@ export const Type: React.SFC<TypeProps> = (props: TypeProps): any => {
});
const isExportedClassReference = !!props.type.isExportedClassReference;
const typeNameUrlIfExists = !_.isUndefined(props.type.externalLink) ? props.type.externalLink : undefined;
if (!_.isUndefined(typeNameUrlIfExists)) {
const typeNameUrlIfExists = props.type.externalLink !== undefined ? props.type.externalLink : undefined;
if (typeNameUrlIfExists !== undefined) {
typeName = props.isInPopover ? (
<span style={{ color: colors.lightBlueA700, cursor: 'pointer' }}>{typeName}</span>
) : (

View File

@@ -36,7 +36,7 @@ export class DocsInfo {
this.contractsByVersionByNetworkId = config.contractsByVersionByNetworkId;
}
public getTypeDefinitionsByName(docAgnosticFormat: DocAgnosticFormat): ObjectMap<TypeDefinitionByName> {
if (_.isUndefined(docAgnosticFormat[this.typeSectionName])) {
if (docAgnosticFormat[this.typeSectionName] === undefined) {
return {};
}
@@ -58,14 +58,14 @@ export class DocsInfo {
});
});
if (_.isUndefined(docAgnosticFormat)) {
if (docAgnosticFormat === undefined) {
return sectionNameToLinks;
}
const docSections = _.keys(this.sections);
_.each(docSections, sectionName => {
const docSection = docAgnosticFormat[sectionName];
if (_.isUndefined(docSection) || sectionName === constants.EXTERNAL_EXPORTS_SECTION_NAME) {
if (docSection === undefined || sectionName === constants.EXTERNAL_EXPORTS_SECTION_NAME) {
return; // no-op
}
@@ -91,7 +91,7 @@ export class DocsInfo {
// Noop so that we don't have the method listed underneath itself.
} else {
let eventNames: string[] = [];
if (!_.isUndefined(docSection.events)) {
if (docSection.events !== undefined) {
const sortedEventNames = _.sortBy(docSection.events, 'name');
eventNames = _.map(sortedEventNames, m => m.name);
}

View File

@@ -124,7 +124,7 @@ export class TypeDocUtils {
this._docsInfo.markdownMenu[sectionName] = [sectionName];
const entities = child.children;
const commentObj = child.comment;
const sectionComment = !_.isUndefined(commentObj) ? commentObj.shortText : '';
const sectionComment = commentObj !== undefined ? commentObj.shortText : '';
const isClassOrObjectLiteral = true;
const docSection = this._convertEntitiesToDocSection(
entities,
@@ -141,7 +141,7 @@ export class TypeDocUtils {
this._docsInfo.markdownMenu[sectionName] = [sectionName];
const entities = [child];
const commentObj = child.comment;
const SectionComment = !_.isUndefined(commentObj) ? commentObj.shortText : '';
const SectionComment = commentObj !== undefined ? commentObj.shortText : '';
const docSection = this._convertEntitiesToDocSection(entities, sectionName);
docSection.comment = SectionComment;
docAgnosticFormat[sectionName] = docSection;
@@ -257,41 +257,42 @@ export class TypeDocUtils {
return docSection;
}
private _convertCustomType(entity: TypeDocNode, sectionName: string): CustomType {
const typeIfExists = !_.isUndefined(entity.type) ? this._convertType(entity.type, sectionName) : undefined;
const typeIfExists = entity.type !== undefined ? this._convertType(entity.type, sectionName) : undefined;
const isConstructor = false;
const methodIfExists = !_.isUndefined(entity.declaration)
? this._convertMethod(entity.declaration, isConstructor, sectionName)
: undefined;
const doesIndexSignatureExist = !_.isUndefined(entity.indexSignature);
const methodIfExists =
entity.declaration !== undefined
? this._convertMethod(entity.declaration, isConstructor, sectionName)
: undefined;
const doesIndexSignatureExist = entity.indexSignature !== undefined;
const indexSignature = entity.indexSignature;
const indexSignatureIfExists = doesIndexSignatureExist
? this._convertIndexSignature(indexSignature, sectionName)
: undefined;
const commentIfExists =
!_.isUndefined(entity.comment) && !_.isUndefined(entity.comment.shortText)
entity.comment !== undefined && entity.comment.shortText !== undefined
? entity.comment.shortText
: undefined;
const childrenIfExist = !_.isUndefined(entity.children)
? _.map(entity.children, (child: TypeDocNode) => {
let childTypeIfExists = !_.isUndefined(child.type)
? this._convertType(child.type, sectionName)
: undefined;
if (child.kindString === KindString.Method) {
childTypeIfExists = {
const childrenIfExist =
entity.children !== undefined
? _.map(entity.children, (child: TypeDocNode) => {
let childTypeIfExists =
child.type !== undefined ? this._convertType(child.type, sectionName) : undefined;
if (child.kindString === KindString.Method) {
childTypeIfExists = {
name: child.name,
typeDocType: TypeDocTypes.Reflection,
method: this._convertMethod(child, isConstructor, sectionName),
};
}
const c: CustomTypeChild = {
name: child.name,
typeDocType: TypeDocTypes.Reflection,
method: this._convertMethod(child, isConstructor, sectionName),
type: childTypeIfExists,
defaultValue: child.defaultValue,
};
}
const c: CustomTypeChild = {
name: child.name,
type: childTypeIfExists,
defaultValue: child.defaultValue,
};
return c;
})
: undefined;
return c;
})
: undefined;
const customType = {
name: entity.name,
@@ -316,9 +317,9 @@ export class TypeDocUtils {
}
private _convertProperty(entity: TypeDocNode, sectionName: string): Property {
const source = entity.sources[0];
const commentIfExists = !_.isUndefined(entity.comment) ? entity.comment.shortText : undefined;
const commentIfExists = entity.comment !== undefined ? entity.comment.shortText : undefined;
const isConstructor = false;
const isStatic = _.isUndefined(entity.flags.isStatic) ? false : entity.flags.isStatic;
const isStatic = entity.flags.isStatic === undefined ? false : entity.flags.isStatic;
const callPath = this._getCallPath(sectionName, isStatic, isConstructor, entity.name);
const property = {
name: entity.name,
@@ -335,16 +336,17 @@ export class TypeDocUtils {
private _convertMethod(entity: TypeDocNode, isConstructor: boolean, sectionName: string): TypescriptMethod {
const signature = entity.signatures[0];
const source = entity.sources[0];
const hasComment = !_.isUndefined(signature.comment);
const isStatic = _.isUndefined(entity.flags.isStatic) ? false : entity.flags.isStatic;
const hasComment = signature.comment !== undefined;
const isStatic = entity.flags.isStatic === undefined ? false : entity.flags.isStatic;
const parameters = _.map(signature.parameters, param => {
return this._convertParameter(param, sectionName);
});
const returnType = this._convertType(signature.type, sectionName);
const typeParameter = _.isUndefined(signature.typeParameter)
? undefined
: this._convertTypeParameter(signature.typeParameter[0], sectionName);
const typeParameter =
signature.typeParameter === undefined
? undefined
: this._convertTypeParameter(signature.typeParameter[0], sectionName);
const callPath = this._getCallPath(sectionName, isStatic, isConstructor, entity.name);
const method = {
@@ -389,15 +391,16 @@ export class TypeDocUtils {
private _convertFunction(entity: TypeDocNode, sectionName: string, isObjectLiteral: boolean): TypescriptFunction {
const signature = entity.signatures[0];
const source = entity.sources[0];
const hasComment = !_.isUndefined(signature.comment);
const hasComment = signature.comment !== undefined;
const parameters = _.map(signature.parameters, param => {
return this._convertParameter(param, sectionName);
});
const returnType = this._convertType(signature.type, sectionName);
const typeParameter = _.isUndefined(signature.typeParameter)
? undefined
: this._convertTypeParameter(signature.typeParameter[0], sectionName);
const typeParameter =
signature.typeParameter === undefined
? undefined
: this._convertTypeParameter(signature.typeParameter[0], sectionName);
let callPath = '';
if (isObjectLiteral) {
@@ -422,7 +425,7 @@ export class TypeDocUtils {
}
private _convertTypeParameter(entity: TypeDocNode, sectionName: string): TypeParameter {
let type;
if (!_.isUndefined(entity.type)) {
if (entity.type !== undefined) {
type = this._convertType(entity.type, sectionName);
}
const parameter = {
@@ -439,7 +442,7 @@ export class TypeDocUtils {
comment = entity.comment.text;
}
const isOptional = !_.isUndefined(entity.flags.isOptional) ? entity.flags.isOptional : false;
const isOptional = entity.flags.isOptional !== undefined ? entity.flags.isOptional : false;
const type = this._convertType(entity.type, sectionName);
@@ -464,11 +467,11 @@ export class TypeDocUtils {
let methodIfExists;
let tupleElementsIfExists;
const doesIndexSignatureExist =
!_.isUndefined(entity.declaration) && !_.isUndefined(entity.declaration.indexSignature);
entity.declaration !== undefined && entity.declaration.indexSignature !== undefined;
if (doesIndexSignatureExist) {
const indexSignature = entity.declaration.indexSignature;
indexSignatureIfExists = this._convertIndexSignature(indexSignature, sectionName);
} else if (!_.isUndefined(entity.declaration)) {
} else if (entity.declaration !== undefined) {
const isConstructor = false;
methodIfExists = this._convertMethod(entity.declaration, isConstructor, sectionName);
} else if (entity.type === TypeDocTypes.Tuple) {
@@ -479,12 +482,13 @@ export class TypeDocUtils {
});
}
const elementTypeIfExists = !_.isUndefined(entity.elementType)
? {
name: entity.elementType.name,
typeDocType: entity.elementType.type,
}
: undefined;
const elementTypeIfExists =
entity.elementType !== undefined
? {
name: entity.elementType.name,
typeDocType: entity.elementType.type,
}
: undefined;
const type: Type = {
name: entity.name,
@@ -499,7 +503,7 @@ export class TypeDocUtils {
tupleElements: tupleElementsIfExists,
};
const externalLinkIfExists = this._externalTypeToLink[entity.name];
if (!_.isUndefined(externalLinkIfExists)) {
if (externalLinkIfExists !== undefined) {
type.externalLink = externalLinkIfExists;
}
return type;