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

ArgsFromMeta utility and generic ArgsStoryFn RT #51

Merged
merged 3 commits into from
Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
6 changes: 0 additions & 6 deletions .babelrc.js

This file was deleted.

1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
29 changes: 29 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Test

on: [push]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Use Node.js 14.x
uses: actions/setup-node@v1
with:
node-version: 14.x

- name: Install dependencies
uses: bahmutov/npm-install@v1

- name: Check typescript
run: |
yarn run check

- name: Check linter
run: |
yarn lint

- name: Run tests
run: |
yarn test
26 changes: 14 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
"*.d.ts"
],
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"scripts": {
"build": "babel src --out-dir dist --extensions \".ts\" && tsc --emitDeclarationOnly",
"lint": "eslint src --ext .js,.ts",
"build": "tsup ./src/index.ts --format esm,cjs --dts",
"check": "tsc",
"lint": "eslint src --ext .ts",
"test": "jest",
"release": "yarn build && auto shipit"
},
Expand All @@ -38,31 +40,31 @@
},
"prettier": "@storybook/linter-config/prettier.config",
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
"roots": [
"<rootDir>/src"
]
},
"dependencies": {
"lodash": "^4.17.15"
"lodash": "^4.17.15",
"type-fest": "^2.19.0"
},
"devDependencies": {
"@auto-it/released": "^10.37.1",
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.5",
"@babel/preset-typescript": "^7.9.0",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😍

"@storybook/eslint-config-storybook": "^2.1.0",
"@types/jest": "^24.0.23",
"@types/jest": "^29.2.0",
"@types/lodash": "^4.14.149",
"@types/node": "^18.11.0",
"@typescript-eslint/parser": "^4.33.0",
"auto": "^10.31.0",
"babel-core": "7.0.0-bridge.0",
"babel-jest": "^24.9.0",
"common-tags": "^1.8.0",
"eslint": "^6.7.1",
"jest": "^24.9.0",
"jest": "^29.2.0",
"prettier": "^2.7.1",
"typescript": "^3.7.2"
"ts-jest": "^29.0.3",
"tsup": "^6.3.0",
"typescript": "^4.8.4"
},
"auto": {
"plugins": [
Expand Down
21 changes: 8 additions & 13 deletions src/includeConditionalArg.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/ban-ts-ignore */
import { includeConditionalArg, testValue } from './includeConditionalArg';
import type { Conditional } from './story';

describe('testValue', () => {
describe('truthy', () => {
Expand Down Expand Up @@ -60,33 +61,27 @@ describe('testValue', () => {
describe('includeConditionalArg', () => {
describe('errors', () => {
it('should throw if neither arg nor global is specified', () => {
expect(() => includeConditionalArg({ if: {} }, {}, {})).toThrowErrorMatchingInlineSnapshot(
`"Invalid conditional value {}"`
);
expect(() =>
includeConditionalArg({ if: {} as Conditional }, {}, {})
).toThrowErrorMatchingInlineSnapshot(`"Invalid conditional value {}"`);
});
it('should throw if arg and global are both specified', () => {
expect(() =>
includeConditionalArg({ if: { arg: 'a', global: 'b' } }, {}, {})
).toThrowErrorMatchingInlineSnapshot(
`"Invalid conditional value {\\"arg\\":\\"a\\",\\"global\\":\\"b\\"}"`
);
).toThrowErrorMatchingInlineSnapshot(`"Invalid conditional value {"arg":"a","global":"b"}"`);
});
it('should throw if mulitiple exists / eq / neq are specified', () => {
expect(() =>
includeConditionalArg({ if: { arg: 'a', exists: true, eq: 1 } }, {}, {})
).toThrowErrorMatchingInlineSnapshot(
`"Invalid conditional test {\\"exists\\":true,\\"eq\\":1}"`
);
).toThrowErrorMatchingInlineSnapshot(`"Invalid conditional test {"exists":true,"eq":1}"`);

expect(() =>
includeConditionalArg({ if: { arg: 'a', exists: false, neq: 0 } }, {}, {})
).toThrowErrorMatchingInlineSnapshot(
`"Invalid conditional test {\\"exists\\":false,\\"neq\\":0}"`
);
).toThrowErrorMatchingInlineSnapshot(`"Invalid conditional test {"exists":false,"neq":0}"`);

expect(() =>
includeConditionalArg({ if: { arg: 'a', eq: 1, neq: 0 } }, {}, {})
).toThrowErrorMatchingInlineSnapshot(`"Invalid conditional test {\\"eq\\":1,\\"neq\\":0}"`);
).toThrowErrorMatchingInlineSnapshot(`"Invalid conditional test {"eq":1,"neq":0}"`);
});
});

Expand Down
3 changes: 2 additions & 1 deletion src/includeConditionalArg.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import isEqual from 'lodash/isEqual';
import { Args, Globals, InputType, Conditional } from './story';

const count = (vals: any[]) => vals.map(v => typeof v !== 'undefined').filter(Boolean).length;
const count = (vals: any[]) => vals.map((v) => typeof v !== 'undefined').filter(Boolean).length;

export const testValue = (cond: Omit<Conditional, 'arg' | 'global'>, value: any) => {
const { exists, eq, neq, truthy } = cond as any;
Expand Down Expand Up @@ -35,5 +35,6 @@ export const includeConditionalArg = (argType: InputType, args: Args, globals: G
}

const value = arg ? args[arg] : globals[global];
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return testValue(argType.if!, value);
};
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export interface SeparatorOptions {
*/
export const parseKind = (kind: string, { rootSeparator, groupSeparator }: SeparatorOptions) => {
const [root, remainder] = kind.split(rootSeparator, 2);
const groups = (remainder || kind).split(groupSeparator).filter(i => !!i);
const groups = (remainder || kind).split(groupSeparator).filter((i) => !!i);

// when there's no remainder, it means the root wasn't found/split
return {
Expand Down
20 changes: 19 additions & 1 deletion src/story.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global HTMLElement, AbortSignal */
import { Simplify, UnionToIntersection } from 'type-fest';
import { SBType, SBScalarType } from './SBType';

export * from './SBType';
Expand Down Expand Up @@ -142,7 +144,7 @@ export type LegacyStoryFn<TFramework extends AnyFramework = AnyFramework, TArgs
export type ArgsStoryFn<TFramework extends AnyFramework = AnyFramework, TArgs = Args> = (
args: TArgs,
context: StoryContext<TFramework, TArgs>
) => TFramework['storyResult'];
) => (TFramework & { T: TArgs })['storyResult'];

// This is either type of user story function
export type StoryFn<TFramework extends AnyFramework = AnyFramework, TArgs = Args> =
Expand Down Expand Up @@ -332,3 +334,19 @@ export type AnnotatedStoryFn<
export type StoryAnnotationsOrFn<TFramework extends AnyFramework = AnyFramework, TArgs = Args> =
| AnnotatedStoryFn<TFramework, TArgs>
| StoryAnnotations<TFramework, TArgs>;

export type ArgsFromMeta<TFramework extends AnyFramework, Meta> = Meta extends {
render?: ArgsStoryFn<TFramework, infer RArgs>;
loaders?: (infer Loaders)[];
decorators?: (infer Decorators)[];
}
Comment on lines +338 to +342
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a TS "test" for this? At the very least it would document what it is for :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hehe, yeah, good point, I have tests for it in this PR:
https://github.com/storybookjs/storybook/pull/19512/files
But maybe good to put a simple test here as well.

I was not really sure where to put this. I could also have put it somewhere in the monorepo. Because StoryObj will now use Meta(OrArgs) as generic (typeof meta), I will need something like this in the public types of every renderer (we use it now only for react and svelte). But because there is also framework-specific inference (to infer the args of the component or specific stuff for action arg inference) I can not put all of StoryObj in this package.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a test now

? Simplify<RArgs & DecoratorsArgs<TFramework, Decorators> & LoaderArgs<TFramework, Loaders>>
: unknown;

type DecoratorsArgs<TFramework extends AnyFramework, Decorators> = UnionToIntersection<
Decorators extends DecoratorFunction<TFramework, infer TArgs> ? TArgs : unknown
>;

type LoaderArgs<TFramework extends AnyFramework, Loaders> = UnionToIntersection<
Loaders extends LoaderFunction<TFramework, infer TArgs> ? TArgs : unknown
>;
13 changes: 7 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"compilerOptions": {
"target": "es5",
"target": "ES2022",
"lib": ["es2015", "es2017", "dom"],
"module": "commonjs",
"moduleResolution": "Node",
"module": "ES2022",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The storybook monorepo is still using:

 "target": "ES2020",
 "module": "CommonJS",

This seems like quite a jump. Is this going to impact our chrome 100+ support target?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the storybook monorepo the tsconfig isn't used to determine the compile target at all

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it being used in this case? If so, is this the correct target?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might not matter at all, depending on the source code. but let's play it safe and target 2020?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tomorrow, was a long day πŸ˜…

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"declaration": true,
"removeComments": true,
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
"outDir": "dist"
"outDir": "dist",
"skipLibCheck": true,
"noEmit": true
},
"include": ["src"],
"exclude": ["src/**/*.test.ts", "dist"]
"exclude": ["dist", "src/includeConditionalArg.test.ts"]
}
Loading