-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make literal sort configurable (#25)
- Loading branch information
Showing
6 changed files
with
136 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 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
16 changes: 16 additions & 0 deletions
16
packages/typescript-to-proptypes/test/sort-unions/input.d.ts
This file contains 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,16 @@ | ||
import * as React from 'react'; | ||
|
||
type Breakpoint = 'xs' | 'md' | 'xl'; | ||
|
||
export interface Props { | ||
/** | ||
* will be sorted alphanumeric | ||
*/ | ||
color?: 'inherit' | 'default' | 'primary' | 'secondary'; | ||
/** | ||
* will be sorted by viewport size descending | ||
*/ | ||
only?: Breakpoint | Breakpoint[]; | ||
} | ||
|
||
export default function Hidden(props: Props): JSX.Element; |
This file contains 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,7 @@ | ||
import * as React from 'react'; | ||
|
||
export default function Hidden(props) { | ||
const { color, only } = props; | ||
|
||
return <div color={color} hidden={only !== 'xs'} />; | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/typescript-to-proptypes/test/sort-unions/options.ts
This file contains 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,21 @@ | ||
import { TestOptions } from '../types'; | ||
|
||
const options: TestOptions = { | ||
injector: { | ||
getSortLiteralUnions: (component, node) => { | ||
if (component.name === 'Hidden' && node.name === 'only') { | ||
return (a, b) => { | ||
// descending here to check that we actually change the order of the typings | ||
// It's unclear why TypeScript changes order of union members sometimes so we need to be sure | ||
const breakpointOrder: unknown[] = ['"xl"', '"md"', '"xs"']; | ||
|
||
return breakpointOrder.indexOf(a.value) - breakpointOrder.indexOf(b.value); | ||
}; | ||
} | ||
// default sort | ||
return undefined; | ||
}, | ||
}, | ||
}; | ||
|
||
export default options; |
24 changes: 24 additions & 0 deletions
24
packages/typescript-to-proptypes/test/sort-unions/output.js
This file contains 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,24 @@ | ||
import * as React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
function Hidden(props) { | ||
const { color, only } = props; | ||
|
||
return <div color={color} hidden={only !== 'xs'} />; | ||
} | ||
|
||
Hidden.propTypes = { | ||
/** | ||
* will be sorted alphanumeric | ||
*/ | ||
color: PropTypes.oneOf(['default', 'inherit', 'primary', 'secondary']), | ||
/** | ||
* will be sorted by viewport size descending | ||
*/ | ||
only: PropTypes.oneOfType([ | ||
PropTypes.oneOf(['xl', 'md', 'xs']), | ||
PropTypes.arrayOf(PropTypes.oneOf(['xl', 'md', 'xs'])), | ||
]), | ||
}; | ||
|
||
export default Hidden; |