-
Notifications
You must be signed in to change notification settings - Fork 965
refactor(react, docs): derive component props from the API schema, drop react-docgen #10621
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
Open
luvkapur
wants to merge
7
commits into
master
Choose a base branch
from
remove-react-docgen
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,126
−370
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f5da0a5
refactor(react, docs): derive component props from the API schema, dr…
luvkapur ce927e0
Merge branch 'master' into remove-react-docgen
luvkapur 89bbe61
refactor(semantic-schema, react): resolve props through the schema's …
luvkapur ab51bb7
fix(semantic-schema): resolve file-internal refs within their file; u…
luvkapur c51c71c
fix(typescript, react): keep inline binding defaults; hydrate schemas…
luvkapur 8b96fce
feat(react, semantic-schema): class components, exported aliases/refe…
luvkapur 7157077
fix(semantic-schema, react): union members merge their types; class b…
luvkapur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
207 changes: 207 additions & 0 deletions
207
components/entities/semantic-schema/schema-members.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| import { expect } from 'chai'; | ||
| import { ComponentID } from '@teambit/component-id'; | ||
| import type { SchemaLocation, SchemaNode } from './schema-node'; | ||
| import { APISchema } from './api-schema'; | ||
| import { | ||
| ExportSchema, | ||
| ExpressionWithTypeArgumentsSchema, | ||
| InferenceTypeSchema, | ||
| InterfaceSchema, | ||
| KeywordTypeSchema, | ||
| ModuleSchema, | ||
| ParameterSchema, | ||
| ParenthesizedTypeSchema, | ||
| TypeIntersectionSchema, | ||
| TypeLiteralSchema, | ||
| TypeRefSchema, | ||
| TypeSchema, | ||
| TypeUnionSchema, | ||
| VariableLikeSchema, | ||
| } from './schemas'; | ||
|
|
||
| const loc: SchemaLocation = { filePath: 'index.ts', line: 0, character: 0 }; | ||
| const compId = ComponentID.fromString('org.scope/button'); | ||
|
|
||
| function member(name: string, type = 'string', isOptional = true): VariableLikeSchema { | ||
| return new VariableLikeSchema(loc, name, `${name}: ${type}`, new KeywordTypeSchema(loc, type), isOptional); | ||
| } | ||
|
|
||
| function iface(name: string, members: SchemaNode[], extendsNodes: ExpressionWithTypeArgumentsSchema[] = []) { | ||
| return new InterfaceSchema(loc, name, `interface ${name}`, extendsNodes, members); | ||
| } | ||
|
|
||
| function literal(...members: SchemaNode[]) { | ||
| return new TypeLiteralSchema(loc, members); | ||
| } | ||
|
|
||
| function extendsRef(name: string) { | ||
| return new ExpressionWithTypeArgumentsSchema([], new TypeRefSchema(loc, name), name, loc); | ||
| } | ||
|
|
||
| const names = (nodes: SchemaNode[]) => nodes.map((node) => node.name); | ||
|
|
||
| describe('SchemaNode.getMembers()', () => { | ||
| it('returns the members of an interface', () => { | ||
| expect(names(iface('Props', [member('a'), member('b')]).getMembers())).to.deep.equal(['a', 'b']); | ||
| }); | ||
|
|
||
| it('returns the members of a type literal', () => { | ||
| expect(names(literal(member('a')).getMembers())).to.deep.equal(['a']); | ||
| }); | ||
|
|
||
| it('contributes nothing for a type that is not object-like', () => { | ||
| expect(new KeywordTypeSchema(loc, 'string').getMembers()).to.deep.equal([]); | ||
| }); | ||
|
|
||
| it('combines the members of an intersection', () => { | ||
| const intersection = new TypeIntersectionSchema(loc, [literal(member('a')), iface('B', [member('b')])]); | ||
| expect(names(intersection.getMembers())).to.deep.equal(['a', 'b']); | ||
| }); | ||
|
|
||
| it('lists the members of every alternative of a union', () => { | ||
| const union = new TypeUnionSchema(loc, [literal(member('a'), member('shared')), literal(member('b'))]); | ||
| expect(names(union.getMembers())).to.deep.equal(['a', 'shared', 'b']); | ||
| }); | ||
|
|
||
| it('follows a type alias and parentheses to the underlying type', () => { | ||
| const alias = new TypeSchema(loc, 'Props', new ParenthesizedTypeSchema(loc, literal(member('a'))), 'type Props'); | ||
| expect(names(alias.getMembers())).to.deep.equal(['a']); | ||
| }); | ||
|
|
||
| it('follows an export wrapper to the exported declaration', () => { | ||
| expect(names(new ExportSchema(loc, 'Props', iface('Props', [member('a')])).getMembers())).to.deep.equal(['a']); | ||
| }); | ||
|
|
||
| it('resolves a reference through the context, and contributes nothing without one', () => { | ||
| const ref = new TypeRefSchema(loc, 'Props'); | ||
| const target = iface('Props', [member('a')]); | ||
| expect(names(ref.getMembers({ resolveRef: () => target }))).to.deep.equal(['a']); | ||
| expect(ref.getMembers()).to.deep.equal([]); | ||
| }); | ||
|
|
||
| it('includes the members an interface inherits, after its own', () => { | ||
| const base = iface('Base', [member('a')]); | ||
| const derived = iface('Props', [member('b')], [extendsRef('Base')]); | ||
| const resolveRef = (ref: TypeRefSchema) => (ref.name === 'Base' ? base : undefined); | ||
| expect(names(derived.getMembers({ resolveRef }))).to.deep.equal(['b', 'a']); | ||
| }); | ||
|
|
||
| it('terminates on a self-referencing type', () => { | ||
| const self = new TypeRefSchema(loc, 'Props'); | ||
| const alias = new TypeSchema( | ||
| loc, | ||
| 'Props', | ||
| new TypeIntersectionSchema(loc, [self, literal(member('a'))]), | ||
| 'type Props' | ||
| ); | ||
| expect(names(alias.getMembers({ resolveRef: () => alias }))).to.deep.equal(['a']); | ||
| }); | ||
| }); | ||
|
|
||
| describe('ModuleSchema.listExports() / listDeclarations()', () => { | ||
| const namespace = new ModuleSchema(loc, [iface('Inner', [])], []); | ||
| namespace.namespace = 'ns'; | ||
| const mod = new ModuleSchema( | ||
| loc, | ||
| [new ExportSchema(loc, 'Props', iface('Props', [])), namespace, iface('Other', [])], | ||
| [iface('Internal', [])] | ||
| ); | ||
|
|
||
| it('lists exports with wrappers and nested namespaces unwrapped', () => { | ||
| expect(names(mod.listExports())).to.deep.equal(['Props', 'Inner', 'Other']); | ||
| }); | ||
|
|
||
| it('lists internals after the exports', () => { | ||
| expect(names(mod.listDeclarations())).to.deep.equal(['Props', 'Inner', 'Other', 'Internal']); | ||
| }); | ||
|
|
||
| it('does not mutate the module', () => { | ||
| mod.listExports(); | ||
| expect(mod.exports).to.have.lengthOf(3); | ||
| expect(mod.exports[0]).to.be.instanceOf(ExportSchema); | ||
| }); | ||
| }); | ||
|
|
||
| describe('APISchema.getMembersOf()', () => { | ||
| function api(exports: SchemaNode[], internals: SchemaNode[] = [], internalModules: ModuleSchema[] = []) { | ||
| return new APISchema(loc, new ModuleSchema(loc, exports, internals), internalModules, compId); | ||
| } | ||
|
|
||
| it('resolves references to exported and internal declarations of the component', () => { | ||
| const schema = api( | ||
| [new ExportSchema(loc, 'Props', iface('Props', [member('a')]))], | ||
| [new TypeSchema(loc, 'FileInternal', literal(member('b')), 'type FileInternal')], | ||
| [new ModuleSchema(loc, [], [new TypeSchema(loc, 'ModuleInternal', literal(member('c')), 'type ModuleInternal')])] | ||
| ); | ||
| const props = new TypeIntersectionSchema(loc, [ | ||
| new TypeRefSchema(loc, 'Props'), | ||
| new TypeRefSchema(loc, 'FileInternal'), | ||
| new TypeRefSchema(loc, 'ModuleInternal'), | ||
| ]); | ||
| expect(names(schema.getMembersOf(props))).to.deep.equal(['a', 'b', 'c']); | ||
| }); | ||
|
|
||
| it('resolves through an alias of a reference', () => { | ||
| const schema = api([ | ||
| new TypeSchema(loc, 'Props', new TypeRefSchema(loc, 'Base'), 'type Props'), | ||
| iface('Base', [member('a')]), | ||
| ]); | ||
| expect(names(schema.getMembersOf(new TypeRefSchema(loc, 'Props')))).to.deep.equal(['a']); | ||
| }); | ||
|
|
||
| it('does not resolve references to other components or packages, even with a matching name', () => { | ||
| const schema = api([iface('Props', [member('a')])]); | ||
| const fromComponent = new TypeRefSchema(loc, 'Props', ComponentID.fromString('org.scope/other')); | ||
| const fromPackage = new TypeRefSchema(loc, 'Props', undefined, 'react'); | ||
| expect(schema.getMembersOf(fromComponent)).to.deep.equal([]); | ||
| expect(schema.getMembersOf(fromPackage)).to.deep.equal([]); | ||
| }); | ||
|
|
||
| it('contributes nothing for an unknown reference', () => { | ||
| expect(api([]).getMembersOf(new TypeRefSchema(loc, 'Missing'))).to.deep.equal([]); | ||
| }); | ||
|
|
||
| it('resolves a file-internal reference only within its file', () => { | ||
| const inButton: SchemaLocation = { filePath: 'button.tsx', line: 1, character: 1 }; | ||
| const inCard: SchemaLocation = { filePath: 'card.tsx', line: 1, character: 1 }; | ||
| const buttonProps = new TypeSchema(inButton, 'Props', literal(member('label')), 'type Props'); | ||
| const cardProps = new TypeSchema(inCard, 'Props', literal(member('title')), 'type Props'); | ||
| const schema = api( | ||
| [], | ||
| [], | ||
| [new ModuleSchema(inCard, [], [cardProps]), new ModuleSchema(inButton, [], [buttonProps])] | ||
| ); | ||
|
|
||
| const toButton = new TypeRefSchema(loc, 'Props', undefined, undefined, 'button.tsx'); | ||
| const toElsewhere = new TypeRefSchema(loc, 'Props', undefined, undefined, 'missing.tsx'); | ||
| expect(names(schema.getMembersOf(toButton))).to.deep.equal(['label']); | ||
| expect(schema.getMembersOf(toElsewhere)).to.deep.equal([]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('ParameterSchema.getBindingDefaults()', () => { | ||
| it('collects default values from destructured bindings, whatever node describes them', () => { | ||
| const param = new ParameterSchema(loc, 'props', new TypeRefSchema(loc, 'Props'), false, undefined, undefined, [ | ||
| new InferenceTypeSchema(loc, 'number', 'size', '32'), | ||
| new VariableLikeSchema( | ||
| loc, | ||
| 'label', | ||
| 'label: string', | ||
| new KeywordTypeSchema(loc, 'string'), | ||
| true, | ||
| undefined, | ||
| "'hi'" | ||
| ), | ||
| new InferenceTypeSchema(loc, 'string', 'other'), | ||
| ]); | ||
| expect([...param.getBindingDefaults()]).to.deep.equal([ | ||
| ['size', '32'], | ||
| ['label', "'hi'"], | ||
| ]); | ||
| }); | ||
|
|
||
| it('is empty for a parameter without bindings', () => { | ||
| const param = new ParameterSchema(loc, 'props', new TypeRefSchema(loc, 'Props'), false); | ||
| expect(param.getBindingDefaults().size).to.equal(0); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.