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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: jsdocParser errors
efrenaragon96 committed Jun 1, 2023
commit b35de5750b14f1db5fe59de7842f49d48c03a00e
2 changes: 1 addition & 1 deletion code/lib/docs-tools/src/argTypes/docgen/PropDef.ts
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ export type PropDefaultValue = PropSummaryValue;

export interface PropDef {
name: string;
type: PropType;
type: PropType | null;
sbType?: any;
required: boolean;
description?: string;
8 changes: 4 additions & 4 deletions code/lib/docs-tools/src/argTypes/docgen/extractDocgenProps.ts
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ import { getPropDefFactory } from './createPropDef';
export interface ExtractedProp {
propDef: PropDef;
docgenInfo: DocgenInfo;
jsDocTags: ExtractedJsDoc;
jsDocTags?: ExtractedJsDoc;
typeSystem: TypeSystem;
}

@@ -86,8 +86,8 @@ function extractProp(
docgenInfo: DocgenInfo,
typeSystem: TypeSystem,
createPropDef: PropDefFactory
): ExtractedProp {
const jsDocParsingResult = parseJsDoc(docgenInfo.description);
): ExtractedProp | null {
const jsDocParsingResult = parseJsDoc(docgenInfo.description, {});
const isIgnored = jsDocParsingResult.includesJsDoc && jsDocParsingResult.ignore;
efrenaragon96 marked this conversation as resolved.
Show resolved Hide resolved

if (!isIgnored) {
@@ -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
@@ -5,9 +5,9 @@ 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;

Original file line number Diff line number Diff line change
@@ -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
@@ -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;
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import type { DocgenInfo } from '../types';

import { createSummaryValue } from '../../utils';

export function createType({ tsType, required }: DocgenInfo): PropType {
export function createType({ tsType, required }: DocgenInfo): PropType | null {
// A type could be null if a defaultProp has been provided without a type definition.
if (tsType == null) {
return null;
2 changes: 1 addition & 1 deletion code/lib/docs-tools/src/argTypes/docgen/types.ts
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ export interface DocgenInfo {
flowType?: DocgenFlowType;
tsType?: DocgenTypeScriptType;
required: boolean;
description?: string;
description: string;
defaultValue?: DocgenPropDefaultValue;
}

164 changes: 82 additions & 82 deletions code/lib/docs-tools/src/argTypes/jsdocParser.test.ts

Large diffs are not rendered by default.

66 changes: 33 additions & 33 deletions code/lib/docs-tools/src/argTypes/jsdocParser.ts
Original file line number Diff line number Diff line change
@@ -2,23 +2,23 @@ import type { Annotation } from 'doctrine';
import doctrine from 'doctrine';

export interface ExtractedJsDocParam {
name: string;
name?: string;
type?: any;
description?: string;
getPrettyName: () => string;
getTypeName: () => string;
description?: string | null;
getPrettyName: () => string | undefined;
getTypeName: () => string | null;
}

export interface ExtractedJsDocReturns {
type?: any;
description?: string;
getTypeName: () => string;
description?: string | null;
getTypeName: () => string | null;
}

export interface ExtractedJsDoc {
params?: ExtractedJsDocParam[];
deprecated?: string;
returns?: ExtractedJsDocReturns;
params?: ExtractedJsDocParam[] | null;
deprecated?: string | null;
returns?: ExtractedJsDocReturns | null;
ignore: boolean;
}

@@ -33,17 +33,20 @@ export interface JsDocParsingResult {
extractedTags?: ExtractedJsDoc;
}

export type ParseJsDoc = (value?: string, options?: JsDocParsingOptions) => JsDocParsingResult;
export type ParseJsDoc = (
value: string | null,
options?: JsDocParsingOptions
) => JsDocParsingResult;

function containsJsDoc(value?: string): boolean {
function containsJsDoc(value?: string | null): boolean {
return value != null && value.includes('@');
}

function parse(content: string, tags: string[]): Annotation {
function parse(content: string | null, tags?: string[]): Annotation {
let ast;

try {
ast = doctrine.parse(content, {
ast = doctrine.parse(content ?? '', {
tags,
sloppy: true,
});
@@ -61,10 +64,7 @@ const DEFAULT_OPTIONS = {
tags: ['param', 'arg', 'argument', 'returns', 'ignore', 'deprecated'],
};

export const parseJsDoc: ParseJsDoc = (
value?: string,
options: JsDocParsingOptions = DEFAULT_OPTIONS
) => {
export const parseJsDoc: ParseJsDoc = (value, options = DEFAULT_OPTIONS) => {
if (!containsJsDoc(value)) {
return {
includesJsDoc: false,
@@ -146,7 +146,7 @@ function extractJsDocTags(ast: doctrine.Annotation): ExtractedJsDoc {
return extractedTags;
}

function extractParam(tag: doctrine.Tag): ExtractedJsDocParam {
function extractParam(tag: doctrine.Tag): ExtractedJsDocParam | null {
const paramName = tag.name;

// When the @param doesn't have a name but have a type and a description, "null-null" is returned.
@@ -174,15 +174,15 @@ function extractParam(tag: doctrine.Tag): ExtractedJsDocParam {
return null;
}

function extractDeprecated(tag: doctrine.Tag): string {
function extractDeprecated(tag: doctrine.Tag): string | null {
if (tag.title != null) {
return tag.description;
}

return null;
}

function extractReturns(tag: doctrine.Tag): ExtractedJsDocReturns {
function extractReturns(tag: doctrine.Tag): ExtractedJsDocReturns | null {
if (tag.type != null) {
return {
type: tag.type,
@@ -196,37 +196,37 @@ function extractReturns(tag: doctrine.Tag): ExtractedJsDocReturns {
return null;
}

function extractTypeName(type: doctrine.Type): string {
if (type.type === 'NameExpression') {
function extractTypeName(type?: doctrine.Type | null): string | null {
if (type?.type === 'NameExpression') {
return type.name;
}

if (type.type === 'RecordType') {
const recordFields = type.fields.map((field: doctrine.type.FieldType) => {
if (field.value != null) {
if (type?.type === 'RecordType') {
const recordFields = type.fields.map((field: doctrine.Type) => {
if (field.type === 'FieldType' && field.value != null) {
const valueTypeName = extractTypeName(field.value);

return `${field.key}: ${valueTypeName}`;
}

return field.key;
return (field as doctrine.type.FieldType).key;
});

return `({${recordFields.join(', ')}})`;
}

if (type.type === 'UnionType') {
if (type?.type === 'UnionType') {
const unionElements = type.elements.map(extractTypeName);

return `(${unionElements.join('|')})`;
}

// Only support untyped array: []. Might add more support later if required.
if (type.type === 'ArrayType') {
if (type?.type === 'ArrayType') {
return '[]';
}

if (type.type === 'TypeApplication') {
if (type?.type === 'TypeApplication') {
if (type.expression != null) {
if ((type.expression as doctrine.type.NameExpression).name === 'Array') {
const arrayType = extractTypeName(type.applications[0]);
@@ -237,14 +237,14 @@ function extractTypeName(type: doctrine.Type): string {
}

if (
type.type === 'NullableType' ||
type.type === 'NonNullableType' ||
type.type === 'OptionalType'
type?.type === 'NullableType' ||
type?.type === 'NonNullableType' ||
type?.type === 'OptionalType'
) {
return extractTypeName(type.expression);
}

if (type.type === 'AllLiteral') {
if (type?.type === 'AllLiteral') {
return 'any';
}