Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript: Migrate @storybook/docs-tools to strict TS #22567

Merged
merged 26 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
fa90910
feat: activate strict option in ts config
efrenaragon96 May 15, 2023
b35de57
fix: jsdocParser errors
efrenaragon96 May 15, 2023
7b1be45
fix: ts error in argTypes converter
efrenaragon96 May 15, 2023
39404a0
fix: ts error on src/argTypes/docgen folder
efrenaragon96 May 16, 2023
d95b4c3
fix: ts error on src/argTypes/docgen/flow folder
efrenaragon96 May 16, 2023
6498a92
fix: ts error on src/argTypes/docgen/typeScript folder
efrenaragon96 May 16, 2023
e49d5fd
fix: ts error in utils files
efrenaragon96 May 16, 2023
3f791b5
fix: call parseJsDoc without second param
efrenaragon96 Jun 1, 2023
0dff3fe
Prefer `satisfies` over `as`
kasperpeulen May 31, 2023
e12279a
fix: change satisfies by static type
efrenaragon96 Jun 5, 2023
305d5da
fix: use only the fields for the JsDocTags type
efrenaragon96 Jun 5, 2023
61ce834
Merge branch 'next' into ts-migrate/lib-docs-tools
ndelangen Jun 7, 2023
8382350
Merge branch 'next' into ts-migrate/lib-docs-tools
kasperpeulen Jun 9, 2023
3b54eac
Merge branch 'next' into ts-migrate/lib-docs-tools
efrenaragon96 Jun 27, 2023
96afbdc
Merge branch 'next' into ts-migrate/lib-docs-tools
JReinhold Jul 3, 2023
d78e200
fix: doc tools types
efrenaragon96 Jul 3, 2023
f0d68a3
Merge branch 'next' into ts-migrate/lib-docs-tools
efrenaragon96 Aug 2, 2023
2356aad
Merge branch 'next' into ts-migrate/lib-docs-tools
kasperpeulen Aug 4, 2023
325b0d7
Merge branch 'next' into ts-migrate/lib-docs-tools
yannbf Sep 19, 2023
f40ec49
Merge branch 'next' into ts-migrate/lib-docs-tools
ndelangen Sep 19, 2023
9ade666
fix: check possible undefined or null values
efrenaragon96 Oct 9, 2023
cf83e3f
fix: use tiny-invariant in vue docs
efrenaragon96 Oct 9, 2023
fc0b847
Merge branch 'next' into ts-migrate/lib-docs-tools
ndelangen Oct 11, 2023
804cb50
Merge branch 'next' into pr/efrenaragon96/22567
ndelangen Nov 28, 2023
f8b3d2b
regen lockfile
ndelangen Nov 28, 2023
5a74d6e
fix
ndelangen Nov 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions code/lib/docs-tools/src/argTypes/convert/convert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,8 +805,8 @@ const transformToModule = (inputCode: string) => {
],
],
};
const { code } = transformSync(inputCode, options);
return normalizeNewlines(code);
const codeTransform = transformSync(inputCode, options);
return codeTransform && normalizeNewlines(codeTransform.code ?? '');
};

const annotateWithDocgen = (inputCode: string, filename: string) => {
Expand All @@ -816,13 +816,13 @@ const annotateWithDocgen = (inputCode: string, filename: string) => {
babelrc: false,
filename,
};
const { code } = transformSync(inputCode, options);
return normalizeNewlines(code);
const codeTransform = transformSync(inputCode, options);
return codeTransform && normalizeNewlines(codeTransform.code ?? '');
};

const convertCommon = (code: string, fileExt: string) => {
const docgenPretty = annotateWithDocgen(code, `temp.${fileExt}`);
const { Component } = requireFromString(transformToModule(docgenPretty));
const { Component } = requireFromString(transformToModule(docgenPretty ?? ''));
// eslint-disable-next-line no-underscore-dangle
const { props = {} } = Component.__docgenInfo || {};
const types = mapValues(props, (prop) => convert(prop));
Expand Down
2 changes: 1 addition & 1 deletion code/lib/docs-tools/src/argTypes/convert/flow/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type { SBType } from '@storybook/types';
import type { FlowType, FlowSigType, FlowLiteralType } from './types';

const isLiteral = (type: FlowType) => type.name === 'literal';
const isLiteral = (type: FlowType): type is FlowLiteralType => type.name === 'literal';
const toEnumOption = (element: FlowLiteralType) => element.value.replace(/['|"]/g, '');

const convertSig = (type: FlowSigType) => {
Expand Down
16 changes: 8 additions & 8 deletions code/lib/docs-tools/src/argTypes/docgen/PropDef.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// FIXME: this is legacy code that needs to be updated & simplified with ArgType refactor

export interface JsDocParam {
name: string;
description?: string;
name: string | undefined | null;
description?: string | null;
}

export interface JsDocReturns {
description?: string;
description?: string | null;
}

export interface JsDocTags {
params?: JsDocParam[];
returns?: JsDocReturns;
params?: JsDocParam[] | null;
returns?: JsDocReturns | null;
}

export interface PropSummaryValue {
summary: string;
summary?: string;
detail?: string;
}

Expand All @@ -24,10 +24,10 @@ export type PropDefaultValue = PropSummaryValue;

export interface PropDef {
name: string;
type: PropType;
type: PropType | null;
sbType?: any;
required: boolean;
description?: string;
defaultValue?: PropDefaultValue;
defaultValue?: PropDefaultValue | null;
jsDocTags?: JsDocTags;
}
12 changes: 6 additions & 6 deletions code/lib/docs-tools/src/argTypes/docgen/createPropDef.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PropDefaultValue } from './PropDef';
import type { JsDocTags, PropDefaultValue } from './PropDef';
import type { PropDef, DocgenInfo, DocgenType, DocgenPropDefaultValue } from './types';
import { TypeSystem } from './types';
import type { JsDocParsingResult } from '../jsdocParser';
Expand Down Expand Up @@ -51,7 +51,7 @@ function isStringValued(type?: DocgenType) {
function createDefaultValue(
defaultValue: DocgenPropDefaultValue,
type: DocgenType
): PropDefaultValue {
): PropDefaultValue | null {
if (defaultValue != null) {
const { value } = defaultValue;

Expand Down Expand Up @@ -81,17 +81,17 @@ function createBasicPropDef(name: string, type: DocgenType, docgenInfo: DocgenIn
};
}

function applyJsDocResult(propDef: PropDef, jsDocParsingResult: JsDocParsingResult): PropDef {
if (jsDocParsingResult.includesJsDoc) {
function applyJsDocResult(propDef: PropDef, jsDocParsingResult?: JsDocParsingResult): PropDef {
if (jsDocParsingResult?.includesJsDoc) {
const { description, extractedTags } = jsDocParsingResult;

if (description != null) {
// eslint-disable-next-line no-param-reassign
propDef.description = jsDocParsingResult.description;
}

const value = {
...extractedTags,
const value: JsDocTags = {
returns: extractedTags?.returns,
params: extractedTags?.params?.map((x) => ({
name: x.getPrettyName(),
description: x.description,
Expand Down
28 changes: 14 additions & 14 deletions code/lib/docs-tools/src/argTypes/docgen/extractDocgenProps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const PROP_NAME = 'propName';

interface TypeSystemDef {
name: string;
typeProperty?: string;
typeProperty: string;
}

const TypeSystems: TypeSystemDef[] = [
Expand Down Expand Up @@ -67,10 +67,10 @@ TypeSystems.forEach((x) => {
const { propDef } = extractComponentProps(component, DOCGEN_SECTION)[0];

expect(propDef.name).toBe(PROP_NAME);
expect(propDef.type.summary).toBe('string');
expect(propDef.type?.summary).toBe('string');
expect(propDef.description).toBe('Hey! Hey!');
expect(propDef.required).toBe(false);
expect(propDef.defaultValue.summary).toBe("'Default'");
expect(propDef.defaultValue?.summary).toBe("'Default'");
});

if (x === TypeSystems[0]) {
Expand All @@ -87,10 +87,10 @@ TypeSystems.forEach((x) => {
const { propDef } = extractComponentProps(component, DOCGEN_SECTION)[0];

expect(propDef.name).toBe(PROP_NAME);
expect(propDef.type.summary).toBe('string');
expect(propDef.type?.summary).toBe('string');
expect(propDef.description).toBe('Hey! Hey!');
expect(propDef.required).toBe(false);
expect(propDef.defaultValue.summary).toBe('"Default"');
expect(propDef.defaultValue?.summary).toBe('"Default"');
});

it('should map defaults docgen info properly, RDT broken enums', () => {
Expand All @@ -107,10 +107,10 @@ TypeSystems.forEach((x) => {
const { propDef } = extractComponentProps(component, DOCGEN_SECTION)[0];

expect(propDef.name).toBe(PROP_NAME);
expect(propDef.type.summary).toBe('enum');
expect(propDef.type?.summary).toBe('enum');
expect(propDef.description).toBe('Hey! Hey!');
expect(propDef.required).toBe(false);
expect(propDef.defaultValue.summary).toBe('"Default"');
expect(propDef.defaultValue?.summary).toBe('"Default"');
});

it('should map defaults docgen info properly, vue', () => {
Expand All @@ -126,10 +126,10 @@ TypeSystems.forEach((x) => {
const { propDef } = extractComponentProps(component, DOCGEN_SECTION)[0];

expect(propDef.name).toBe(PROP_NAME);
expect(propDef.type.summary).toBe('string');
expect(propDef.type?.summary).toBe('string');
expect(propDef.description).toBe('Hey! Hey!');
expect(propDef.required).toBe(false);
expect(propDef.defaultValue.summary).toBe("'Default'");
expect(propDef.defaultValue?.summary).toBe("'Default'");
});
}

Expand Down Expand Up @@ -208,11 +208,11 @@ TypeSystems.forEach((x) => {

expect(propDef.description).toBe('onClick description');
expect(propDef.jsDocTags).toBeDefined();
expect(propDef.jsDocTags.params).toBeDefined();
expect(propDef.jsDocTags.params[0].name).toBe('event');
expect(propDef.jsDocTags.params[0].description).toBe('Original event.');
expect(propDef.jsDocTags.params[1].name).toBe('value');
expect(propDef.jsDocTags.params[1].description).toBeNull();
expect(propDef.jsDocTags?.params).toBeDefined();
expect(propDef.jsDocTags?.params?.[0].name).toBe('event');
expect(propDef.jsDocTags?.params?.[0].description).toBe('Original event.');
expect(propDef.jsDocTags?.params?.[1].name).toBe('value');
expect(propDef.jsDocTags?.params?.[1].description).toBeNull();
});

it("should not return 'null' default value", () => {
Expand Down
6 changes: 3 additions & 3 deletions code/lib/docs-tools/src/argTypes/docgen/extractDocgenProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getPropDefFactory } from './createPropDef';
export interface ExtractedProp {
propDef: PropDef;
docgenInfo: DocgenInfo;
jsDocTags: ExtractedJsDoc;
jsDocTags?: ExtractedJsDoc;
typeSystem: TypeSystem;
}

Expand Down Expand Up @@ -86,7 +86,7 @@ function extractProp(
docgenInfo: DocgenInfo,
typeSystem: TypeSystem,
createPropDef: PropDefFactory
): ExtractedProp {
): ExtractedProp | null {
const jsDocParsingResult = parseJsDoc(docgenInfo.description);
const isIgnored = jsDocParsingResult.includesJsDoc && jsDocParsingResult.ignore;

Expand All @@ -105,5 +105,5 @@ function extractProp(
}

export function extractComponentDescription(component?: Component): string {
return component != null && getDocgenDescription(component);
return component != null ? getDocgenDescription(component) : '';
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { createSummaryValue, isTooLongForDefaultValueSummary } from '../../utils
import { isDefaultValueBlacklisted } from '../utils/defaultValue';

export function createDefaultValue(
defaultValue: DocgenPropDefaultValue,
type: DocgenPropType
): PropDefaultValue {
defaultValue: DocgenPropDefaultValue | null,
type: DocgenPropType | null
): PropDefaultValue | null {
if (defaultValue != null) {
const { value } = defaultValue;

if (!isDefaultValueBlacklisted(value)) {
return !isTooLongForDefaultValueSummary(value)
? createSummaryValue(value)
: createSummaryValue(type.name, value);
: createSummaryValue(type?.name, value);
}
}

Expand Down
40 changes: 20 additions & 20 deletions code/lib/docs-tools/src/argTypes/docgen/flow/createPropDef.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function createDocgenInfo({ flowType, ...others }: Partial<DocgenInfo>): DocgenI
flowType,
required: false,
...others,
};
} as DocgenInfo;
}

describe('type', () => {
Expand All @@ -21,8 +21,8 @@ describe('type', () => {

const { type } = createFlowPropDef(PROP_NAME, docgenInfo);

expect(type.summary).toBe(x);
expect(type.detail).toBeUndefined();
expect(type?.summary).toBe(x);
expect(type?.detail).toBeUndefined();
});
}
);
Expand All @@ -35,8 +35,8 @@ describe('type', () => {

const { type } = createFlowPropDef(PROP_NAME, docgenInfo);

expect(type.summary).toBe(x);
expect(type.detail).toBeUndefined();
expect(type?.summary).toBe(x);
expect(type?.detail).toBeUndefined();
});

it(`should support typed ${x}`, () => {
Expand All @@ -54,8 +54,8 @@ describe('type', () => {

const { type } = createFlowPropDef(PROP_NAME, docgenInfo);

expect(type.summary).toBe(`${x}<any>`);
expect(type.detail).toBeUndefined();
expect(type?.summary).toBe(`${x}<any>`);
expect(type?.detail).toBeUndefined();
});
});

Expand Down Expand Up @@ -88,8 +88,8 @@ describe('type', () => {

const { type } = createFlowPropDef(PROP_NAME, docgenInfo);

expect(type.summary).toBe('{ foo: string, bar?: mixed }');
expect(type.detail).toBeUndefined();
expect(type?.summary).toBe('{ foo: string, bar?: mixed }');
expect(type?.detail).toBeUndefined();
});

it('should support long object signature', () => {
Expand Down Expand Up @@ -188,8 +188,8 @@ describe('type', () => {

const { type } = createFlowPropDef(PROP_NAME, docgenInfo);

expect(type.summary).toBe('object');
expect(type.detail).toBe(
expect(type?.summary).toBe('object');
expect(type?.detail).toBe(
'{ (x: string): void, prop1: string, prop2: string, prop3: string, prop4: string, prop5: string, prop6: string, prop7: string, prop8: string }'
);
});
Expand Down Expand Up @@ -218,8 +218,8 @@ describe('type', () => {

const { type } = createFlowPropDef(PROP_NAME, docgenInfo);

expect(type.summary).toBe('(x: string) => void');
expect(type.detail).toBeUndefined();
expect(type?.summary).toBe('(x: string) => void');
expect(type?.detail).toBeUndefined();
});

it('should support tuple', () => {
Expand All @@ -244,7 +244,7 @@ describe('type', () => {

const { type } = createFlowPropDef(PROP_NAME, docgenInfo);

expect(type.summary).toBe('[foo, "value", number]');
expect(type?.summary).toBe('[foo, "value", number]');
});

it('should support union', () => {
Expand All @@ -265,7 +265,7 @@ describe('type', () => {

const { type } = createFlowPropDef(PROP_NAME, docgenInfo);

expect(type.summary).toBe('number | string');
expect(type?.summary).toBe('number | string');
});

it('should support nested union elements', () => {
Expand Down Expand Up @@ -300,7 +300,7 @@ describe('type', () => {

const { type } = createFlowPropDef(PROP_NAME, docgenInfo);

expect(type.summary).toBe('"minimum" | "maximum" | number | string');
expect(type?.summary).toBe('"minimum" | "maximum" | number | string');
});

it('uses raw union value if elements are missing', () => {
Expand All @@ -313,7 +313,7 @@ describe('type', () => {

const { type } = createFlowPropDef(PROP_NAME, docgenInfo);

expect(type.summary).toBe('"minimum" | "maximum" | UserSize');
expect(type?.summary).toBe('"minimum" | "maximum" | UserSize');
});

it('removes a leading | if raw union value is used', () => {
Expand All @@ -326,7 +326,7 @@ describe('type', () => {

const { type } = createFlowPropDef(PROP_NAME, docgenInfo);

expect(type.summary).toBe('"minimum" | "maximum" | UserSize');
expect(type?.summary).toBe('"minimum" | "maximum" | UserSize');
});

it('even removes extra spaces after a leading | if raw union value is used', () => {
Expand All @@ -339,7 +339,7 @@ describe('type', () => {

const { type } = createFlowPropDef(PROP_NAME, docgenInfo);

expect(type.summary).toBe('"minimum" | "maximum" | UserSize');
expect(type?.summary).toBe('"minimum" | "maximum" | UserSize');
});

it('should support intersection', () => {
Expand All @@ -360,6 +360,6 @@ describe('type', () => {

const { type } = createFlowPropDef(PROP_NAME, docgenInfo);

expect(type.summary).toBe('number & string');
expect(type?.summary).toBe('number & string');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export const createFlowPropDef: PropDefFactory = (propName, docgenInfo) => {
type: createType(flowType),
required,
description,
defaultValue: createDefaultValue(defaultValue, flowType),
defaultValue: createDefaultValue(defaultValue ?? null, flowType ?? null),
};
};
2 changes: 1 addition & 1 deletion code/lib/docs-tools/src/argTypes/docgen/flow/createType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function generateDefault({ name, raw }: DocgenFlowType): PropType {
return createSummaryValue(name);
}

export function createType(type: DocgenFlowType): PropType {
export function createType(type?: DocgenFlowType): PropType | null {
// A type could be null if a defaultProp has been provided without a type definition.
if (type == null) {
return null;
Expand Down
Loading