Skip to content

Commit

Permalink
fix: allow non-string literals (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored May 15, 2020
1 parent b2c44be commit 546e7ad
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,23 @@ export function generate(node: t.Node | t.PropTypeNode[], options: GenerateOptio
if (t.isUnionNode(node)) {
let [literals, rest] = _.partition(t.uniqueUnionTypes(node).types, t.isLiteralNode);

literals = literals.sort((a, b) => a.value.localeCompare(b.value));
literals = literals.sort((a, b) => {
const { value: valueA } = a;
const { value: valueB } = b;
// numbers ascending
if (typeof valueA === 'number' && typeof valueB === 'number') {
return valueA - valueB;
}
// numbers last
if (typeof valueA === 'number') {
return 1;
}
if (typeof valueB === 'number') {
return -1;
}
// sort anything else by their stringified value
return String(valueA).localeCompare(String(valueB));
});

const nodeToStringName = (obj: t.Node): string => {
if (t.isInstanceOfNode(obj)) {
Expand Down
4 changes: 2 additions & 2 deletions src/types/props/literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { Node } from '../nodes/baseNodes';
const typeString = 'LiteralNode';

export interface LiteralNode extends Node {
value: any;
value: unknown;
jsDoc?: string;
}

export function literalNode(value: any, jsDoc?: string): LiteralNode {
export function literalNode(value: unknown, jsDoc?: string): LiteralNode {
return {
type: typeString,
value,
Expand Down
8 changes: 8 additions & 0 deletions test/mixed-literals/input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
interface GridProps {
spacing?: 'initial' | 1 | 2 | 3 | 4 | 5 | 'auto';
}

export default function Grid(props: GridProps) {
const { spacing } = props;
return <div>spacing: {spacing}</div>;
}
11 changes: 11 additions & 0 deletions test/mixed-literals/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import PropTypes from 'prop-types';
function Grid(props) {
const { spacing } = props;
return <div>spacing: {spacing}</div>;
}

Grid.propTypes = {
spacing: PropTypes.oneOf(['auto', 'initial', 1, 2, 3, 4, 5]),
};

export default Grid;
29 changes: 29 additions & 0 deletions test/mixed-literals/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"type": "ProgramNode",
"body": [
{
"type": "ComponentNode",
"name": "Grid",
"types": [
{
"type": "PropTypeNode",
"name": "spacing",
"propType": {
"type": "UnionNode",
"types": [
{ "type": "UndefinedNode" },
{ "type": "LiteralNode", "value": "\"initial\"" },
{ "type": "LiteralNode", "value": 1 },
{ "type": "LiteralNode", "value": 2 },
{ "type": "LiteralNode", "value": 3 },
{ "type": "LiteralNode", "value": 4 },
{ "type": "LiteralNode", "value": 5 },
{ "type": "LiteralNode", "value": "\"auto\"" }
]
},
"filenames": {}
}
]
}
]
}

0 comments on commit 546e7ad

Please sign in to comment.