Skip to content

Commit

Permalink
fix: import skeleton for links in v10 types (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
veu authored Apr 26, 2023
1 parent 9148671 commit ebf85f7
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/property-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const propertyImports = (
const moduleImport = (module: string): OptionalKind<ImportDeclarationStructure> => {
return {
moduleSpecifier: `./${context.moduleName(module)}`,
namedImports: [context.moduleFieldsName(module)],
namedImports: [context.moduleReferenceName(module)],
isTypeOnly: true,
};
};
Expand Down
2 changes: 2 additions & 0 deletions src/renderer/type/create-default-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type RenderContext = {
getFieldRenderer: <FType extends ContentTypeFieldType>(fieldType: FType) => FieldRenderer<FType>;
moduleName: (id: string) => string;
moduleFieldsName: (id: string) => string;
moduleReferenceName: (id: string) => string;
moduleSkeletonName: (id: string) => string;
imports: Set<OptionalKind<ImportDeclarationStructure>>;
};
Expand All @@ -16,6 +17,7 @@ export const createDefaultContext = (): RenderContext => {
moduleName,
moduleFieldsName,
moduleSkeletonName,
moduleReferenceName: moduleFieldsName,
getFieldRenderer: <FType extends ContentTypeFieldType>(fieldType: FType) =>
defaultRenderers[fieldType] as FieldRenderer<FType>,
imports: new Set(),
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/type/create-v10-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ import { ContentTypeFieldType } from 'contentful';
import { ImportDeclarationStructure, OptionalKind } from 'ts-morph';
import { FieldRenderer, v10Renderers } from '../field';
import { createDefaultContext } from './create-default-context';
import { moduleSkeletonName } from '../../module-name';

export type RenderContext = {
getFieldRenderer: <FType extends ContentTypeFieldType>(fieldType: FType) => FieldRenderer<FType>;
moduleName: (id: string) => string;
moduleFieldsName: (id: string) => string;
moduleReferenceName: (id: string) => string;
moduleSkeletonName: (id: string) => string;
imports: Set<OptionalKind<ImportDeclarationStructure>>;
};

export const createV10Context = (): RenderContext => {
return {
...createDefaultContext(),
moduleReferenceName: moduleSkeletonName,
getFieldRenderer: <FType extends ContentTypeFieldType>(fieldType: FType) =>
v10Renderers[fieldType] as FieldRenderer<FType>,
};
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/type/default-content-type-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export class DefaultContentTypeRenderer extends BaseContentTypeRenderer {
for (const structure of context.imports) {
file.addImportDeclaration(structure);
}

file.organizeImports({
ensureNewLineAtEndOfFile: true,
});
}

protected renderFieldsInterface(
Expand Down
66 changes: 63 additions & 3 deletions test/renderer/type/content-type-renderer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,66 @@ import {
} from '../../../src';
import stripIndent = require('strip-indent');

describe('The default content type renderer', () => {
let project: Project;
let testFile: SourceFile;

beforeEach(() => {
project = new Project({
useInMemoryFileSystem: true,
compilerOptions: {
target: ScriptTarget.ES5,
declaration: true,
},
});
testFile = project.createSourceFile('test.ts');
});

it('adds import for field type', () => {
const renderer = new DefaultContentTypeRenderer();

const contentType: CFContentType = {
name: 'unused-name',
sys: {
id: 'test',
type: 'Symbol',
},
fields: [
{
id: 'linkFieldId',
name: 'Linked entry Field',
type: 'Link',
localized: false,
required: true,
validations: [
{
linkContentType: ['linkedType'],
},
],
disabled: false,
omitted: false,
linkType: 'Entry',
},
],
};

renderer.render(contentType, testFile);

expect('\n' + testFile.getFullText()).toEqual(
stripIndent(`
import type { Entry } from "contentful";
import type { TypeLinkedTypeFields } from "./TypeLinkedType";
export interface TypeTestFields {
linkFieldId: Entry<TypeLinkedTypeFields>;
}
export type TypeTest = Entry<TypeTestFields>;
`),
);
});
});

describe('A derived content type renderer class', () => {
let project: Project;
let testFile: SourceFile;
Expand All @@ -39,6 +99,7 @@ describe('A derived content type renderer class', () => {
return {
moduleName,
moduleFieldsName,
moduleReferenceName: moduleFieldsName,
moduleSkeletonName,
getFieldRenderer: <FType extends ContentTypeFieldType>(fieldType: FType) => {
if (fieldType === 'Symbol') {
Expand Down Expand Up @@ -139,8 +200,7 @@ describe('A derived content type renderer class', () => {

expect('\n' + testFile.getFullText()).toEqual(
stripIndent(`
import type { EntryFields } from "contentful";
import type { Entry } from "contentful";
import type { Entry, EntryFields } from "contentful";
export interface TypeTestFields {
/** Field of type "Symbol" */
Expand Down Expand Up @@ -194,8 +254,8 @@ describe('A derived content type renderer class', () => {

expect('\n' + testFile.getFullText()).toEqual(
stripIndent(`
import type { EntryFields } from "contentful";
import type { IdScopedEntry } from "@custom";
import type { EntryFields } from "contentful";
export interface TypeTestFields {
field_id: EntryFields.Symbol;
Expand Down
15 changes: 5 additions & 10 deletions test/renderer/type/js-doc-renderer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ describe('A JSDoc content type renderer class', () => {

expect('\n' + testFile.getFullText()).toEqual(
stripIndent(`
import type { EntryFields } from "contentful";
import type { Entry } from "contentful";
import type { Entry, EntryFields } from "contentful";
/**
* Fields type definition for content type 'TypeAnimal'
Expand Down Expand Up @@ -148,8 +147,7 @@ describe('A JSDoc content type renderer class', () => {

expect('\n' + testFile.getFullText()).toEqual(
stripIndent(`
import type { EntryFields } from "contentful";
import type { Entry } from "contentful";
import type { Entry, EntryFields } from "contentful";
/**
* Fields type definition for content type 'TypeAnimal'
Expand Down Expand Up @@ -191,8 +189,7 @@ describe('A JSDoc content type renderer class', () => {

expect('\n' + testFile.getFullText()).toEqual(
stripIndent(`
import type { EntryFields } from "contentful";
import type { Entry } from "contentful";
import type { Entry, EntryFields } from "contentful";
/**
* Fields type definition for content type 'TypeAnimal'
Expand Down Expand Up @@ -234,8 +231,7 @@ describe('A JSDoc content type renderer class', () => {

expect('\n' + testFile.getFullText()).toEqual(
stripIndent(`
import type { EntryFields } from "contentful";
import type { Entry } from "contentful";
import type { Entry, EntryFields } from "contentful";
/**
* Fields type definition for content type 'TypeAnimal'
Expand Down Expand Up @@ -285,8 +281,7 @@ describe('A JSDoc content type renderer class', () => {

expect('\n' + testFile.getFullText()).toEqual(
stripIndent(`
import type { EntryFields } from "contentful";
import type { Entry } from "contentful";
import type { Entry, EntryFields } from "contentful";
/**
* Fields type definition for content type 'TypeAnimal'
Expand Down
62 changes: 62 additions & 0 deletions test/renderer/type/v10-content-type-renderer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,67 @@ import {
} from '../../../src';
import stripIndent = require('strip-indent');

describe('The v10 content type renderer', () => {
let project: Project;
let testFile: SourceFile;

beforeEach(() => {
project = new Project({
useInMemoryFileSystem: true,
compilerOptions: {
target: ScriptTarget.ES5,
declaration: true,
},
});
testFile = project.createSourceFile('test.ts');
});

it('adds import for skeleton type', () => {
const renderer = new V10ContentTypeRenderer();

const contentType: CFContentType = {
name: 'unused-name',
sys: {
id: 'test',
type: 'Symbol',
},
fields: [
{
id: 'linkFieldId',
name: 'Linked entry Field',
type: 'Link',
localized: false,
required: true,
validations: [
{
linkContentType: ['linkedType'],
},
],
disabled: false,
omitted: false,
linkType: 'Entry',
},
],
};

renderer.render(contentType, testFile);

expect('\n' + testFile.getFullText()).toEqual(
stripIndent(`
import type { ChainModifiers, Entry, EntryFieldTypes, EntrySkeletonType, LocaleCode } from "contentful";
import type { TypeLinkedTypeSkeleton } from "./TypeLinkedType";
export interface TypeTestFields {
linkFieldId: EntryFieldTypes.EntryLink<TypeLinkedTypeSkeleton>;
}
export type TypeTestSkeleton = EntrySkeletonType<TypeTestFields, "test">;
export type TypeTest<Modifiers extends ChainModifiers, Locales extends LocaleCode> = Entry<TypeTestSkeleton, Modifiers, Locales>;
`),
);
});
});

describe('A derived content type renderer class', () => {
let project: Project;
let testFile: SourceFile;
Expand All @@ -39,6 +100,7 @@ describe('A derived content type renderer class', () => {
return {
moduleName,
moduleFieldsName,
moduleReferenceName: moduleSkeletonName,
moduleSkeletonName,
getFieldRenderer: <FType extends ContentTypeFieldType>(fieldType: FType) => {
if (fieldType === 'Symbol') {
Expand Down

0 comments on commit ebf85f7

Please sign in to comment.