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

Support inheritance when parsing interfaces #395

Merged
merged 2 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
JsxExpression,
TypeAliasDeclaration,
JsxAttribute,
LeftHandSideExpression,
} from "ts-morph";
import {
ArrayProp,
Expand Down Expand Up @@ -203,7 +204,11 @@ export default class StaticParsingHelpers {
}

static getEscapedName(
p: PropertySignature | PropertyAssignment | TypeAliasDeclaration
p:
| PropertySignature
| PropertyAssignment
| TypeAliasDeclaration
| LeftHandSideExpression
): string {
return p.getSymbolOrThrow().getEscapedName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,49 @@ export default class TypeNodeParsingHelper {
}
}

private static getInheritedShape(
shapeDeclaration: InterfaceDeclaration | TypeLiteralNode,
parseTypeReference: (identifier: string) => ParsedType | undefined
): ParsedShape {
if (!shapeDeclaration.isKind(SyntaxKind.InterfaceDeclaration)) {
return {};
}

const heritageClause = shapeDeclaration.getHeritageClauseByKind(
SyntaxKind.ExtendsKeyword
);
if (!heritageClause) {
return {};
}

const inheritedTypes = heritageClause.getTypeNodes();
return inheritedTypes.reduce((inheritedShape, inheritedType) => {
const typeName = StaticParsingHelpers.getEscapedName(
inheritedType.getExpression()
);
const parsedType = parseTypeReference(typeName);
if (parsedType?.kind === ParsedTypeKind.Object) {
return {
...inheritedShape,
...parsedType.type,
};
} else {
throw new Error(
tmeyer2115 marked this conversation as resolved.
Show resolved Hide resolved
`Error parsing interface: Unable to resolve inherited type ${typeName} to an object type.`
);
}
}, {} as ParsedShape);
}

private static handleObjectType(
shapeDeclaration: InterfaceDeclaration | TypeLiteralNode,
parseTypeReference: (identifier: string) => ParsedType | undefined
): ObjectParsedType {
const properties = shapeDeclaration.getProperties();
const parsedShape: ParsedShape = {};
const parsedShape = this.getInheritedShape(
shapeDeclaration,
parseTypeReference
);

properties.forEach((p) => {
const propertyName = StaticParsingHelpers.getEscapedName(p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,59 @@ describe("parseShape", () => {
});
});

it("can parse an interface with inherited properties", () => {
const parser = createParser(
`interface BaseProps {
title?: string
}

interface HelloProps {
hello: {
world?: string
}
}

export interface Props extends BaseProps, HelloProps {
hello: {
world: string
}
}`
);

expect(parser.parseTypeReference("Props")?.type).toEqual({
title: {
kind: ParsedTypeKind.Simple,
required: false,
type: "string",
},
hello: {
kind: ParsedTypeKind.Object,
required: true,
type: {
world: {
kind: ParsedTypeKind.Simple,
required: true,
type: "string",
},
},
},
});
});

it("throws an error if the inherited type is not an object type", () => {
const parser = createParser(
`import { MyString } from "../__fixtures__/StudioSourceFileParser/exportedTypes";

export interface Props extends MyString {
title: string;
}`
);

expect(() => parser.parseTypeReference("Props")).toThrowError(
"Error parsing interface: Unable to resolve inherited type MyString to an object type."
);
});

it("can parse a sub-property with a renamed nested type from an external package", () => {
const parser = createParser(
`import { ApplyFiltersButtonProps } from "@yext/search-ui-react";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SyntaxKind } from "ts-morph";
import TypeNodeParsingHelper, {
ParsedTypeKind,
} from "../../src/parsers/helpers/TypeNodeParsingHelper";
import createTestSourceFile from "../__utils__/createTestSourceFile";
import { PropValueType } from "../../src/types";
} from "../../../src/parsers/helpers/TypeNodeParsingHelper";
import createTestSourceFile from "../../__utils__/createTestSourceFile";
import { PropValueType } from "../../../src/types";

const externalShapeParser = jest.fn();

Expand Down
Loading