Skip to content

Commit

Permalink
Fixes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Asbjørn Hegdahl committed Feb 14, 2020
1 parent b142b7f commit e867ce5
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.0.1

- FIX: Fixes [#8](https://github.com/asbjornh/view-models/issues/8) - `typescriptReact` parser crashing when parsing components with no arguments.

## 2.0.0

- BREAKING: Changes the default file extension for generated TypeScript files from `.ts` to `.d.ts`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "view-models",
"version": "2.0.0",
"version": "2.0.1",
"description": "Generate viewmodels from react components",
"main": "lib/index.js",
"bin": {
Expand Down
5 changes: 3 additions & 2 deletions source/parsers/prop-types/resolve-references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as t from "@babel/types";

import { MetaTypeTree } from "../../node-types";

import first from "../../utils/first";
import isObjectMethod from "../utils/is-object-method";
import isMemberExpression from "../utils/is-member-expression";

Expand All @@ -29,7 +30,7 @@ export default function resolveReferences(
>;
const propName: string = propPath.node.key.name;
const metaType = meta[propName] ? meta[propName].type : "";
const argument = path.node.arguments[0];
const argument = first(path.node.arguments);

if (!argument) {
throw new Error(
Expand Down Expand Up @@ -71,7 +72,7 @@ export default function resolveReferences(
// Object.keys, Object.values
else if (t.isCallExpression(argument) && isObjectMethod(argument)) {
const objectMethod = argument;
const methodArg = objectMethod.arguments[0];
const methodArg = first(objectMethod.arguments);

if (!methodArg) {
throw new Error(
Expand Down
13 changes: 7 additions & 6 deletions source/parsers/typescript-react/get-prop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as t from "@babel/types";

import getDefinitionName from "./get-definition-name";
import filter from "../../utils/filter";
import first from "../../utils/first";

export default function getPropTypes(ast: t.File, componentName: string) {
let typeName: string | undefined;
Expand Down Expand Up @@ -46,16 +47,16 @@ export default function getPropTypes(ast: t.File, componentName: string) {
if (!t.isIdentifier(path.parent.id)) return;
if (path.parent.id.name !== componentName) return;

const arg = path.node.params[0];
const firstArg = first(path.node.params);

if (t.isTSParameterProperty(arg))
if (t.isTSParameterProperty(firstArg))
throw new Error(`Unexpected parameter property`);

if (t.isNoop(arg.typeAnnotation))
if (t.isNoop(firstArg?.typeAnnotation))
throw new Error(`Unexpected noop in type annotation`);

if (arg.typeAnnotation) {
const argType = arg.typeAnnotation.typeAnnotation;
if (firstArg?.typeAnnotation) {
const argType = firstArg.typeAnnotation.typeAnnotation;
if (t.isTSTypeReference(argType)) {
const name = getDefinitionName(argType.typeName);
types = typeDeclarations[name];
Expand All @@ -76,7 +77,7 @@ export default function getPropTypes(ast: t.File, componentName: string) {
if (!t.isTSTypeReference(funcType)) return;

if (funcType.typeParameters !== null) {
const type = funcType.typeParameters.params[0];
const type = first(funcType.typeParameters.params);
if (t.isTSTypeReference(type)) {
const name = getDefinitionName(type.typeName);
types = typeDeclarations[name];
Expand Down
11 changes: 11 additions & 0 deletions source/parsers/typescript-react/typescript-react.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ test(
classes.funcComponent
);

test(
"Component without props argument",
template,
`const Component: React.FunctionComponent<{}> = () => null;
export default Component;`,
`${csharpImports}
public class Component
{
}`
);

test(
"Empty component",
template,
Expand Down
4 changes: 4 additions & 0 deletions source/utils/first.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Utility function for safely getting the first element of an array */
export default function first<T>(arr: T[]): T | undefined {
return arr[0];
}

0 comments on commit e867ce5

Please sign in to comment.