Skip to content

Commit

Permalink
Merge pull request #9787 from lgraziani2712/fix-9778
Browse files Browse the repository at this point in the history
Replace lodash named imports with specific imports
  • Loading branch information
shilman authored Feb 7, 2020
2 parents 45c0400 + b610440 commit aed5276
Show file tree
Hide file tree
Showing 24 changed files with 91 additions and 113 deletions.
3 changes: 1 addition & 2 deletions addons/docs/src/blocks/Props.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { FunctionComponent, useContext } from 'react';
import { isNil } from 'lodash';

import {
PropsTable,
Expand Down Expand Up @@ -60,7 +59,7 @@ export const getComponentProps = (
throw new Error(PropsTableError.PROPS_UNSUPPORTED);
}
let props = extractProps(component);
if (!isNil(exclude)) {
if (exclude != null) {
const { rows } = props as PropsTableRowsProps;
const { sections } = props as PropsTableSectionsProps;
if (rows) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isNil } from 'lodash';
import { PropDefaultValue } from '@storybook/components';
import { FUNCTION_CAPTION, ELEMENT_CAPTION } from '../captions';
import {
Expand All @@ -19,7 +18,7 @@ import { getPrettyIdentifier } from './prettyIdentifier';
function generateFunc({ inferedType, ast }: InspectionResult): PropDefaultValue {
const { identifier } = inferedType as InspectionFunction;

if (!isNil(identifier)) {
if (identifier != null) {
return createSummaryValue(
getPrettyIdentifier(inferedType as InspectionIdentifiableInferedType),
generateCode(ast)
Expand All @@ -42,7 +41,7 @@ function generateElement(
const { inferedType } = inspectionResult;
const { identifier } = inferedType as InspectionElement;

if (!isNil(identifier)) {
if (identifier != null) {
if (!isHtmlTag(identifier)) {
const prettyIdentifier = getPrettyIdentifier(
inferedType as InspectionIdentifiableInferedType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { PropDefaultValue, PropDef } from '@storybook/components';
import { isNil, isPlainObject, isArray, isFunction, isString } from 'lodash';
import isPlainObject from 'lodash/isPlainObject';
import isFunction from 'lodash/isFunction';
import isString from 'lodash/isString';
// @ts-ignore
import reactElementToJSXString from 'react-element-to-jsx-string';
import { createSummaryValue, isTooLongForDefaultValueSummary } from '../../../../lib';
Expand All @@ -20,7 +22,7 @@ export interface TypeResolvers {
}

function isReactElement(element: any): boolean {
return !isNil(element.$$typeof);
return element.$$typeof != null;
}

export function extractFunctionName(func: Function, propName: string): string {
Expand All @@ -44,7 +46,7 @@ function generateReactObject(rawDefaultProp: any) {

const jsx = reactElementToJSXString(rawDefaultProp);

if (!isNil(displayName)) {
if (displayName != null) {
const prettyIdentifier = getPrettyElementIdentifier(displayName);

return createSummaryValue(prettyIdentifier, prettyIdentifier !== jsx ? jsx : undefined);
Expand All @@ -66,7 +68,7 @@ function generateReactObject(rawDefaultProp: any) {
}

const objectResolver: TypeResolver = rawDefaultProp => {
if (isReactElement(rawDefaultProp) && !isNil(rawDefaultProp.type)) {
if (isReactElement(rawDefaultProp) && rawDefaultProp.type != null) {
return generateReactObject(rawDefaultProp);
}

Expand All @@ -76,7 +78,7 @@ const objectResolver: TypeResolver = rawDefaultProp => {
return generateObject(inspectionResult);
}

if (isArray(rawDefaultProp)) {
if (Array.isArray(rawDefaultProp)) {
const inspectionResult = inspectValue(JSON.stringify(rawDefaultProp));

return generateArray(inspectionResult);
Expand All @@ -92,7 +94,7 @@ const functionResolver: TypeResolver = (rawDefaultProp, propDef) => {
// Try to display the name of the component. The body of the component is ommited since the code has been transpiled.
if (isFunction(rawDefaultProp.render)) {
isElement = true;
} else if (!isNil(rawDefaultProp.prototype) && isFunction(rawDefaultProp.prototype.render)) {
} else if (rawDefaultProp.prototype != null && isFunction(rawDefaultProp.prototype.render)) {
isElement = true;
} else {
let innerElement;
Expand All @@ -110,7 +112,7 @@ const functionResolver: TypeResolver = (rawDefaultProp, propDef) => {
innerElement = rawDefaultProp();
}

if (!isNil(innerElement)) {
if (innerElement != null) {
if (isReactElement(innerElement)) {
isElement = true;
}
Expand All @@ -121,12 +123,12 @@ const functionResolver: TypeResolver = (rawDefaultProp, propDef) => {
}

const funcName = extractFunctionName(rawDefaultProp, propDef.name);
if (!isNil(funcName)) {
if (funcName != null) {
if (isElement) {
return createSummaryValue(getPrettyElementIdentifier(funcName));
}

if (!isNil(inspectionResult)) {
if (inspectionResult != null) {
inspectionResult = inspectValue(rawDefaultProp.toString());
}

Expand Down Expand Up @@ -168,8 +170,7 @@ export function createDefaultValueFromRawDefaultProp(
): PropDefaultValue {
try {
// Keep the extra () otherwise it will fail for functions.
// eslint-disable-next-line prettier/prettier
switch (typeof (rawDefaultProp)) {
switch (typeof rawDefaultProp) {
case 'string':
return typeResolvers.string(rawDefaultProp, propDef);
case 'object':
Expand Down
15 changes: 7 additions & 8 deletions addons/docs/src/frameworks/react/lib/inspection/acornParser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Parser } from 'acorn';
// @ts-ignore
import jsx from 'acorn-jsx';
import { isNil } from 'lodash';
import estree from 'estree';
// @ts-ignore
import * as acornWalk from 'acorn-walk';
Expand Down Expand Up @@ -32,7 +31,7 @@ const acornParser = Parser.extend(jsx());

// Cannot use "estree.Identifier" type because this function also support "JSXIdentifier".
function extractIdentifierName(identifierNode: any) {
return !isNil(identifierNode) ? identifierNode.name : null;
return identifierNode != null ? identifierNode.name : null;
}

function filterAncestors(ancestors: estree.Node[]): estree.Node[] {
Expand Down Expand Up @@ -91,7 +90,7 @@ function parseFunction(
ACORN_WALK_VISITORS
);

const isJsx = !isNil(innerJsxElementNode);
const isJsx = innerJsxElementNode != null;

const inferedType: InspectionFunction | InspectionElement = {
type: isJsx ? InspectionType.ELEMENT : InspectionType.FUNCTION,
Expand All @@ -100,7 +99,7 @@ function parseFunction(
};

const identifierName = extractIdentifierName((funcNode as estree.FunctionExpression).id);
if (!isNil(identifierName)) {
if (identifierName != null) {
inferedType.identifier = identifierName;
}

Expand All @@ -127,7 +126,7 @@ function parseClass(
);

const inferedType: any = {
type: !isNil(innerJsxElementNode) ? InspectionType.ELEMENT : InspectionType.CLASS,
type: innerJsxElementNode != null ? InspectionType.ELEMENT : InspectionType.CLASS,
identifier: extractIdentifierName(classNode.id),
};

Expand All @@ -143,7 +142,7 @@ function parseJsxElement(jsxElementNode: any): ParsingResult<InspectionElement>
};

const identifierName = extractIdentifierName(jsxElementNode.openingElement.name);
if (!isNil(identifierName)) {
if (identifierName != null) {
inferedType.identifier = identifierName;
}

Expand Down Expand Up @@ -212,13 +211,13 @@ export function parse(value: string): ParsingResult<InspectionInferedType> {
ast,
};

if (!isNil(ast.body[0])) {
if (ast.body[0] != null) {
const rootNode = ast.body[0];

switch (rootNode.type) {
case 'ExpressionStatement': {
const expressionResult = parseExpression(rootNode.expression);
if (!isNil(expressionResult)) {
if (expressionResult != null) {
parsingResult = expressionResult as any;
}
break;
Expand Down
23 changes: 11 additions & 12 deletions addons/docs/src/frameworks/react/propTypes/createType.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isNil } from 'lodash';
import { PropType } from '@storybook/components';
import { createSummaryValue, isTooLongForTypeSummary } from '../../../lib';
import { ExtractedProp, DocgenPropType } from '../../../lib/docgen';
Expand Down Expand Up @@ -74,7 +73,7 @@ function createTypeDef({
name,
short,
compact,
full: !isNil(full) ? full : short,
full: full != null ? full : short,
inferedType,
};
}
Expand Down Expand Up @@ -137,7 +136,7 @@ function generateTypeFromString(value: string, originalTypeName: string): TypeDe
case InspectionType.ELEMENT: {
const { identifier } = inferedType as InspectionElement;

short = !isNil(identifier) && !isHtmlTag(identifier) ? identifier : ELEMENT_CAPTION;
short = identifier != null && !isHtmlTag(identifier) ? identifier : ELEMENT_CAPTION;
compact = splitIntoLines(value).length === 1 ? value : null;
full = value;
break;
Expand Down Expand Up @@ -167,7 +166,7 @@ function generateTypeFromString(value: string, originalTypeName: string): TypeDe
}

function generateCustom({ raw }: DocgenPropType): TypeDef {
if (!isNil(raw)) {
if (raw != null) {
return generateTypeFromString(raw, PropTypesType.CUSTOM);
}

Expand All @@ -181,8 +180,8 @@ function generateCustom({ raw }: DocgenPropType): TypeDef {
function generateFunc(extractedProp: ExtractedProp): TypeDef {
const { jsDocTags } = extractedProp;

if (!isNil(jsDocTags)) {
if (!isNil(jsDocTags.params) || !isNil(jsDocTags.returns)) {
if (jsDocTags != null) {
if (jsDocTags.params != null || jsDocTags.returns != null) {
return createTypeDef({
name: PropTypesType.FUNC,
short: generateShortFuncSignature(jsDocTags.params, jsDocTags.returns),
Expand Down Expand Up @@ -225,7 +224,7 @@ function generateObjectOf(type: DocgenPropType, extractedProp: ExtractedProp): T
return createTypeDef({
name: PropTypesType.OBJECTOF,
short: objectOf(short),
compact: !isNil(compact) ? objectOf(compact) : null,
compact: compact != null ? objectOf(compact) : null,
full: objectOf(full),
});
}
Expand All @@ -248,7 +247,7 @@ function generateUnion(type: DocgenPropType, extractedProp: ExtractedProp): Type
return createTypeDef({
name: PropTypesType.UNION,
short: values.short.join(' | '),
compact: values.compact.every((x: string) => !isNil(x)) ? values.compact.join(' | ') : null,
compact: values.compact.every((x: string) => x != null) ? values.compact.join(' | ') : null,
full: values.full.join(' | '),
});
}
Expand Down Expand Up @@ -280,7 +279,7 @@ function generateEnum(type: DocgenPropType): TypeDef {
return createTypeDef({
name: PropTypesType.ENUM,
short: values.short.join(' | '),
compact: values.compact.every((x: string) => !isNil(x)) ? values.compact.join(' | ') : null,
compact: values.compact.every((x: string) => x != null) ? values.compact.join(' | ') : null,
full: values.full.join(' | '),
});
}
Expand All @@ -300,7 +299,7 @@ function createArrayOfObjectTypeDef(short: string, compact: string, full: string
return createTypeDef({
name: PropTypesType.ARRAYOF,
short: braceAfter(short),
compact: !isNil(compact) ? braceAround(compact) : null,
compact: compact != null ? braceAround(compact) : null,
full: braceAround(full),
});
}
Expand Down Expand Up @@ -361,7 +360,7 @@ export function createType(extractedProp: ExtractedProp): PropType {
const { type } = extractedProp.docgenInfo;

// A type could be null if a defaultProp has been provided without a type definition.
if (isNil(type)) {
if (type == null) {
return null;
}

Expand All @@ -376,7 +375,7 @@ export function createType(extractedProp: ExtractedProp): PropType {
case PropTypesType.ARRAYOF: {
const { short, compact, full } = generateType(type, extractedProp);

if (!isNil(compact)) {
if (compact != null) {
if (!isTooLongForTypeSummary(compact)) {
return createSummaryValue(compact);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { isNil } from 'lodash';
import { ExtractedJsDocParam, ExtractedJsDocReturns } from '../../../lib/jsdocParser';

export function generateFuncSignature(
params: ExtractedJsDocParam[],
returns: ExtractedJsDocReturns
): string {
const hasParams = !isNil(params);
const hasReturns = !isNil(returns);
const hasParams = params != null;
const hasReturns = returns != null;

if (!hasParams && !hasReturns) {
return '';
Expand All @@ -19,7 +18,7 @@ export function generateFuncSignature(
const prettyName = x.getPrettyName();
const typeName = x.getTypeName();

if (!isNil(typeName)) {
if (typeName != null) {
return `${prettyName}: ${typeName}`;
}

Expand All @@ -42,8 +41,8 @@ export function generateShortFuncSignature(
params: ExtractedJsDocParam[],
returns: ExtractedJsDocReturns
): string {
const hasParams = !isNil(params);
const hasReturns = !isNil(returns);
const hasParams = params != null;
const hasReturns = returns != null;

if (!hasParams && !hasReturns) {
return '';
Expand Down
13 changes: 6 additions & 7 deletions addons/docs/src/frameworks/react/propTypes/handleProp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isNil } from 'lodash';
import { PropDef } from '@storybook/components';
import { ExtractedProp } from '../../../lib/docgen';
import { createType } from './createType';
Expand All @@ -11,25 +10,25 @@ export function enhancePropTypesProp(extractedProp: ExtractedProp, rawDefaultPro
const { propDef } = extractedProp;

const newtype = createType(extractedProp);
if (!isNil(newtype)) {
if (newtype != null) {
propDef.type = newtype;
}

const { defaultValue } = extractedProp.docgenInfo;
if (!isNil(defaultValue) && !isNil(defaultValue.value)) {
if (defaultValue != null && defaultValue.value != null) {
const newDefaultValue = createDefaultValue(defaultValue.value);

if (!isNil(newDefaultValue)) {
if (newDefaultValue != null) {
propDef.defaultValue = newDefaultValue;
}
} else if (!isNil(rawDefaultProp)) {
} else if (rawDefaultProp != null) {
const newDefaultValue = createDefaultValueFromRawDefaultProp(
rawDefaultProp,
propDef,
rawDefaultPropTypeResolvers
);

if (!isNil(newDefaultValue)) {
if (newDefaultValue != null) {
propDef.defaultValue = newDefaultValue;
}
}
Expand All @@ -41,7 +40,7 @@ export function enhancePropTypesProps(
extractedProps: ExtractedProp[],
component: Component
): PropDef[] {
const rawDefaultProps = !isNil(component.defaultProps) ? component.defaultProps : {};
const rawDefaultProps = component.defaultProps != null ? component.defaultProps : {};
const enhancedProps = extractedProps.map(x =>
enhancePropTypesProp(x, rawDefaultProps[x.propDef.name])
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isNil } from 'lodash';
import { TypeResolver, extractFunctionName, createTypeResolvers } from '../lib/defaultValues';
import { createSummaryValue } from '../../../lib';
import { FUNCTION_CAPTION, ELEMENT_CAPTION } from '../lib';
Expand All @@ -12,7 +11,7 @@ const funcResolver: TypeResolver = (rawDefaultProp, { name, type }) => {
const isElement = type.summary === 'element' || type.summary === 'elementType';

const funcName = extractFunctionName(rawDefaultProp, name);
if (!isNil(funcName)) {
if (funcName != null) {
// Try to display the name of the component. The body of the component is ommited since the code has been transpiled.
if (isElement) {
return createSummaryValue(getPrettyElementIdentifier(funcName));
Expand Down
Loading

0 comments on commit aed5276

Please sign in to comment.