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

Migrate Some Type Literals to Interfaces #138

Merged
merged 4 commits into from
Nov 26, 2020
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
93 changes: 40 additions & 53 deletions src/ts/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ import {
factory,
DeclarationStatement,
ModifierFlags,
NodeFlags,
Statement,
TypeAliasDeclaration,
TypeNode,
VariableStatement,
SyntaxKind,
InterfaceDeclaration,
} from 'typescript';

import {Log} from '../logging';
Expand Down Expand Up @@ -219,66 +218,53 @@ export class Class {
return this.namedParents().length === 1 && this._props.size === 0;
}

private baseNode(
private baseDecl(
skipDeprecatedProperties: boolean,
context: Context
): TypeNode | undefined {
): InterfaceDeclaration | undefined {
if (this.skipBase()) {
return undefined;
}

const baseName = this.baseName();
assert(baseName, 'If a baseNode is defined, baseName must be defined.');

const parentTypes = this.namedParents().map(p =>
factory.createTypeReferenceNode(p, [])
factory.createExpressionWithTypeArguments(factory.createIdentifier(p), [])
);
const parentNode =

const heritage = factory.createHeritageClause(
SyntaxKind.ExtendsKeyword,
parentTypes.length === 0
? factory.createTypeReferenceNode('Partial', [
factory.createTypeReferenceNode(
IdReferenceName,
/*typeArguments=*/ []
? [
factory.createExpressionWithTypeArguments(
factory.createIdentifier('Partial'),
/*typeArguments=*/ [
factory.createTypeReferenceNode(
IdReferenceName,
/*typeArguments=*/ []
),
]
),
])
: parentTypes.length === 1
? parentTypes[0]
: factory.createParenthesizedType(
factory.createIntersectionTypeNode(parentTypes)
);

// Properties part.
const propLiteral = factory.createTypeLiteralNode([
// ... then everything else.
...this.properties()
.filter(property => !property.deprecated || !skipDeprecatedProperties)
.map(prop => prop.toNode(context)),
]);

if (propLiteral.members.length > 0) {
return factory.createIntersectionTypeNode([parentNode, propLiteral]);
} else {
return parentNode;
}
}

private baseDecl(
skipDeprecatedProperties: boolean,
context: Context
): TypeAliasDeclaration | undefined {
const baseNode = this.baseNode(skipDeprecatedProperties, context);
]
: parentTypes
);

if (!baseNode) return undefined;
const baseName = this.baseName();
assert(baseName, 'If a baseNode is defined, baseName must be defined.');
const members = this.properties()
.filter(property => !property.deprecated || !skipDeprecatedProperties)
.map(prop => prop.toNode(context));

return factory.createTypeAliasDeclaration(
return factory.createInterfaceDeclaration(
/*decorators=*/ [],
/*modifiers=*/ [],
baseName,
/*typeParameters=*/ [],
baseNode
/*heritageClause=*/ [heritage],
/*members=*/ members
);
}

protected leafDecl(context: Context): TypeAliasDeclaration | undefined {
protected leafDecl(context: Context): InterfaceDeclaration | undefined {
const leafName = this.leafName();
if (!leafName) return undefined;

Expand All @@ -293,19 +279,20 @@ export class Class {
/*typeArguments=*/ []
);

const thisType = factory.createIntersectionTypeNode([
factory.createTypeLiteralNode([
new TypeProperty(this.subject).toNode(context),
]),
baseTypeReference,
]);

return factory.createTypeAliasDeclaration(
return factory.createInterfaceDeclaration(
/*decorators=*/ [],
/*modifiers=*/ [],
leafName,
/*typeParameters=*/ [],
thisType
/*heritage=*/ [
factory.createHeritageClause(SyntaxKind.ExtendsKeyword, [
factory.createExpressionWithTypeArguments(
factory.createIdentifier(baseName),
/*typeArguments=*/ []
),
]),
],
/*members=*/ [new TypeProperty(this.subject).toNode(context)]
);
}

Expand Down
12 changes: 6 additions & 6 deletions test/baselines/category_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,18 @@ test(`baseine_${basename(__filename)}`, async () => {

export type Text = string;

type DistilleryLeaf = {
interface DistilleryLeaf extends ThingBase {
\\"@type\\": \\"Distillery\\";
} & ThingBase;
}
/** A distillery. */
export type Distillery = DistilleryLeaf;

type ThingBase = Partial<IdReference> & {
interface ThingBase extends Partial<IdReference> {
\\"name\\"?: SchemaValue<Text>;
};
type ThingLeaf = {
}
interface ThingLeaf extends ThingBase {
\\"@type\\": \\"Thing\\";
} & ThingBase;
}
export type Thing = ThingLeaf | Distillery;

"
Expand Down
8 changes: 4 additions & 4 deletions test/baselines/comments_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ test(`baseine_${basename(__filename)}`, async () => {
/** Data type: Text. */
export type Text = string;

type ThingBase = Partial<IdReference> & {
interface ThingBase extends Partial<IdReference> {
/**
* Reminds me of this quote:
* \`FooBar\`
Expand All @@ -69,10 +69,10 @@ test(`baseine_${basename(__filename)}`, async () => {
\\"knows\\"?: SchemaValue<Text>;
/** Names are great! {@link X Y} */
\\"name\\"?: SchemaValue<Text>;
};
type ThingLeaf = {
}
interface ThingLeaf extends ThingBase {
\\"@type\\": \\"Thing\\";
} & ThingBase;
}
/**
* Things are amazing!
*
Expand Down
8 changes: 4 additions & 4 deletions test/baselines/data_type_union_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ test(`baseine_${basename(__filename)}`, async () => {
/** The basic data types such as Integers, Strings, etc. */
export type DataType = Number | Text;

type ThingBase = Partial<IdReference> & {
interface ThingBase extends Partial<IdReference> {
\\"age\\"?: SchemaValue<Number>;
\\"name\\"?: SchemaValue<Text>;
};
type ThingLeaf = {
}
interface ThingLeaf extends ThingBase {
\\"@type\\": \\"Thing\\";
} & ThingBase;
}
export type Thing = ThingLeaf;

"
Expand Down
7 changes: 4 additions & 3 deletions test/baselines/default_ontology_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ test(`baseine_${basename(__filename)}`, async () => {
\\"@id\\": string;
};

type ThingBase = Partial<IdReference>;
type ThingLeaf = {
interface ThingBase extends Partial<IdReference> {
}
interface ThingLeaf extends ThingBase {
\\"@type\\": \\"Thing\\";
} & ThingBase;
}
export type Thing = ThingLeaf;

"
Expand Down
32 changes: 16 additions & 16 deletions test/baselines/deprecated_objects_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,43 +79,43 @@ test(`baseine_${basename(__filename)}`, async () => {

export type Text = string;

type CarBase = ThingBase & {
interface CarBase extends ThingBase {
\\"doorNumber\\"?: SchemaValue<Number>;
};
type CarLeaf = {
}
interface CarLeaf extends CarBase {
\\"@type\\": \\"Car\\";
} & CarBase;
}
export type Car = CarLeaf;

type PersonLikeBase = ThingBase & {
interface PersonLikeBase extends ThingBase {
\\"height\\"?: SchemaValue<Number>;
};
type PersonLikeLeaf = {
}
interface PersonLikeLeaf extends PersonLikeBase {
\\"@type\\": \\"PersonLike\\";
} & PersonLikeBase;
}
export type PersonLike = PersonLikeLeaf;

type ThingBase = Partial<IdReference> & {
interface ThingBase extends Partial<IdReference> {
\\"name\\"?: SchemaValue<Text>;
/**
* Names are great! {@link X Y}
* @deprecated Consider using http://schema.org/name or http://schema.org/height instead.
*/
\\"names\\"?: SchemaValue<Text>;
};
type ThingLeaf = {
}
interface ThingLeaf extends ThingBase {
\\"@type\\": \\"Thing\\";
} & ThingBase;
}
export type Thing = ThingLeaf | Car | PersonLike | Vehicle;

type VehicleBase = ThingBase & {
interface VehicleBase extends ThingBase {
\\"doorNumber\\"?: SchemaValue<Number>;
/** @deprecated Consider using http://schema.org/doorNumber instead. */
\\"doors\\"?: SchemaValue<Number>;
};
type VehicleLeaf = {
}
interface VehicleLeaf extends VehicleBase {
\\"@type\\": \\"Vehicle\\";
} & VehicleBase;
}
/** @deprecated Use Car instead. */
export type Vehicle = VehicleLeaf;

Expand Down
8 changes: 4 additions & 4 deletions test/baselines/duplicate_comments_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ test(`baseine_${basename(__filename)}`, async () => {

export type Text = string;

type ThingBase = Partial<IdReference> & {
interface ThingBase extends Partial<IdReference> {
/** Names are great! {@link X Y} */
\\"name\\"?: SchemaValue<Text>;
};
type ThingLeaf = {
}
interface ThingLeaf extends ThingBase {
\\"@type\\": \\"Thing\\";
} & ThingBase;
}
/**
* Things are amazing!
*
Expand Down
7 changes: 4 additions & 3 deletions test/baselines/enum_skipped_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ test(`baseine_${basename(__filename)}`, async () => {
\\"@id\\": string;
};

type ThingBase = Partial<IdReference>;
type ThingLeaf = {
interface ThingBase extends Partial<IdReference> {
}
interface ThingLeaf extends ThingBase {
\\"@type\\": \\"Thing\\";
} & ThingBase;
}
/** A Thing! */
export type Thing = \\"http://schema.org/a\\" | \\"https://schema.org/a\\" | \\"a\\" | \\"http://schema.org/b\\" | \\"https://schema.org/b\\" | \\"b\\" | \\"http://schema.org/c\\" | \\"https://schema.org/c\\" | \\"c\\" | \\"http://schema.org/d\\" | \\"https://schema.org/d\\" | \\"d\\" | \\"https://schema.org/e\\" | \\"e\\" | \\"http://google.com/f\\" | \\"https://google.com/f\\" | ThingLeaf;

Expand Down
24 changes: 12 additions & 12 deletions test/baselines/inheritance_multiple_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,28 @@ test(`baseine_${basename(__filename)}`, async () => {

export type Text = string;

type PersonLikeBase = ThingBase & {
interface PersonLikeBase extends ThingBase {
\\"height\\"?: SchemaValue<Number>;
};
type PersonLikeLeaf = {
}
interface PersonLikeLeaf extends PersonLikeBase {
\\"@type\\": \\"PersonLike\\";
} & PersonLikeBase;
}
export type PersonLike = PersonLikeLeaf;

type ThingBase = Partial<IdReference> & {
interface ThingBase extends Partial<IdReference> {
\\"name\\"?: SchemaValue<Text>;
};
type ThingLeaf = {
}
interface ThingLeaf extends ThingBase {
\\"@type\\": \\"Thing\\";
} & ThingBase;
}
export type Thing = ThingLeaf | PersonLike | Vehicle;

type VehicleBase = ThingBase & {
interface VehicleBase extends ThingBase {
\\"doors\\"?: SchemaValue<Number>;
};
type VehicleLeaf = {
}
interface VehicleLeaf extends VehicleBase {
\\"@type\\": \\"Vehicle\\";
} & VehicleBase;
}
export type Vehicle = VehicleLeaf;

"
Expand Down
16 changes: 8 additions & 8 deletions test/baselines/inheritance_one_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,20 @@ test(`baseine_${basename(__filename)}`, async () => {

export type Text = string;

type PersonLikeBase = ThingBase & {
interface PersonLikeBase extends ThingBase {
\\"height\\"?: SchemaValue<Number>;
};
type PersonLikeLeaf = {
}
interface PersonLikeLeaf extends PersonLikeBase {
\\"@type\\": \\"PersonLike\\";
} & PersonLikeBase;
}
export type PersonLike = PersonLikeLeaf;

type ThingBase = Partial<IdReference> & {
interface ThingBase extends Partial<IdReference> {
\\"name\\"?: SchemaValue<Text>;
};
type ThingLeaf = {
}
interface ThingLeaf extends ThingBase {
\\"@type\\": \\"Thing\\";
} & ThingBase;
}
export type Thing = ThingLeaf | PersonLike;

"
Expand Down
Loading