Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
This reverts commit 20c5736 and PR asyncapi#1800 because of incorrect handling of enums: asyncapi#1800 (comment)
  • Loading branch information
jonaslagoni committed Feb 21, 2024
1 parent 2e443cf commit ad68a0f
Show file tree
Hide file tree
Showing 12 changed files with 16 additions and 204 deletions.
1 change: 0 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ These are all specific examples only relevant to the TypeScript generator:
- [typescript-generate-example](./typescript-generate-example) - A basic example of how to use Modelina and output a TypeScript class with an example function.
- [typescript-generate-marshalling](./typescript-generate-marshalling) - A basic example of how to use the un/marshalling functionality of the typescript class.
- [typescript-generate-comments](./typescript-generate-comments) - A basic example of how to generate TypeScript interfaces with comments from description and example fields.
- [typescript-interfaces-as-types](./typescript-interfaces-as-types) - A basic example that generate the models as interfaces and uses ESM module system syntax to import and export them as types.
- [typescript-use-esm](./typescript-use-esm) - A basic example that generate the models to use ESM module system.
- [typescript-use-cjs](./typescript-use-cjs) - A basic example that generate the models to use CJS module system.
- [typescript-generate-jsonbinpack](./typescript-generate-jsonbinpack) - A basic example showing how to generate models that include [jsonbinpack](https://github.com/sourcemeta/jsonbinpack) functionality.
Expand Down
17 changes: 0 additions & 17 deletions examples/typescript-interfaces-as-types/README.md

This file was deleted.

This file was deleted.

16 changes: 0 additions & 16 deletions examples/typescript-interfaces-as-types/index.spec.ts

This file was deleted.

34 changes: 0 additions & 34 deletions examples/typescript-interfaces-as-types/index.ts

This file was deleted.

10 changes: 0 additions & 10 deletions examples/typescript-interfaces-as-types/package-lock.json

This file was deleted.

10 changes: 0 additions & 10 deletions examples/typescript-interfaces-as-types/package.json

This file was deleted.

47 changes: 9 additions & 38 deletions src/generators/typescript/TypeScriptDependencyManager.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { AbstractDependencyManager } from '../AbstractDependencyManager';
import { renderJavaScriptDependency } from '../../helpers';
import { ConstrainedMetaModel } from '../../models';
import {
TypeScriptExportType,
TypeScriptModelType,
TypeScriptOptions
} from './TypeScriptGenerator';
import { TypeScriptExportType, TypeScriptOptions } from './TypeScriptGenerator';

export class TypeScriptDependencyManager extends AbstractDependencyManager {
constructor(
Expand All @@ -18,32 +14,19 @@ export class TypeScriptDependencyManager extends AbstractDependencyManager {
/**
* Simple helper function to add a dependency correct based on the module system that the user defines.
*/
addTypeScriptDependency(
toImport: string,
fromModule: string,
modelType: TypeScriptModelType
): void {
const dependencyImport = this.renderDependency(
toImport,
fromModule,
modelType
);
addTypeScriptDependency(toImport: string, fromModule: string): void {
const dependencyImport = this.renderDependency(toImport, fromModule);
this.addDependency(dependencyImport);
}

/**
* Simple helper function to render a dependency based on the module system that the user defines.
*/
renderDependency(
toImport: string,
fromModule: string,
modelType: TypeScriptModelType
): string {
renderDependency(toImport: string, fromModule: string): string {
return renderJavaScriptDependency(
toImport,
fromModule,
this.options.moduleSystem,
modelType === 'interface' ? 'type' : 'value'
this.options.moduleSystem
);
}

Expand All @@ -52,40 +35,28 @@ export class TypeScriptDependencyManager extends AbstractDependencyManager {
*/
renderCompleteModelDependencies(
model: ConstrainedMetaModel,
exportType: TypeScriptExportType,
modelType: TypeScriptModelType
exportType: TypeScriptExportType
): string {
const dependencyObject =
exportType === 'named' ? `{${model.name}}` : model.name;
return this.renderDependency(
dependencyObject,
`./${model.name}`,
modelType
);
return this.renderDependency(dependencyObject, `./${model.name}`);
}

/**
* Render the exported statement for the model based on the options
*/
renderExport(
model: ConstrainedMetaModel,
exportType: TypeScriptExportType,
modelType: TypeScriptModelType
exportType: TypeScriptExportType
): string {
const cjsExport =
exportType === 'default'
? `module.exports = ${model.name};`
: `exports.${model.name} = ${model.name};`;
const esmExportValue =
const esmExport =
exportType === 'default'
? `export default ${model.name};\n`
: `export { ${model.name} };`;
const esmExportType =
exportType === 'default'
? `export default ${model.name};\n`
: `export type { ${model.name} };`;
const esmExport =
modelType === 'interface' ? esmExportType : esmExportValue;
return this.options.moduleSystem === 'CJS' ? cjsExport : esmExport;
}
}
9 changes: 3 additions & 6 deletions src/generators/typescript/TypeScriptGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,13 @@ import { TypeScriptDependencyManager } from './TypeScriptDependencyManager';

export type TypeScriptModuleSystemType = 'ESM' | 'CJS';
export type TypeScriptExportType = 'named' | 'default';
export type TypeScriptModelType = 'class' | 'interface';
export interface TypeScriptOptions
extends CommonGeneratorOptions<
TypeScriptPreset,
TypeScriptDependencyManager
> {
renderTypes: boolean;
modelType: TypeScriptModelType;
modelType: 'class' | 'interface';
enumType: 'enum' | 'union';
mapType: 'indexedObject' | 'map' | 'record';
typeMapping: TypeMapping<TypeScriptOptions, TypeScriptDependencyManager>;
Expand Down Expand Up @@ -211,14 +210,12 @@ export class TypeScriptGenerator extends AbstractGenerator<
const modelDependencyImports = modelDependencies.map((model) => {
return dependencyManagerToUse.renderCompleteModelDependencies(
model,
completeModelOptionsToUse.exportType,
optionsToUse.modelType
completeModelOptionsToUse.exportType
);
});
const modelExport = dependencyManagerToUse.renderExport(
args.constrainedModel,
completeModelOptionsToUse.exportType,
optionsToUse.modelType
completeModelOptionsToUse.exportType
);

const modelCode = `${outputModel.result}\n${modelExport}`;
Expand Down
15 changes: 4 additions & 11 deletions src/helpers/DependencyHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,11 @@ import { ConstrainedMetaModel } from '../models';
export function renderJavaScriptDependency(
toImport: string,
fromModule: string,
moduleSystem: 'CJS' | 'ESM',
importType: 'type' | 'value' = 'value'
moduleSystem: 'CJS' | 'ESM'
): string {
const importValueStatement =
moduleSystem === 'CJS'
? `const ${toImport} = require('${fromModule}');`
: `import ${toImport} from '${fromModule}';`;
const importTypeStatement =
moduleSystem === 'CJS'
? `const ${toImport} = require('${fromModule}');`
: `import type ${toImport} from '${fromModule}';`;
return importType === 'type' ? importTypeStatement : importValueStatement;
return moduleSystem === 'CJS'
? `const ${toImport} = require('${fromModule}');`
: `import ${toImport} from '${fromModule}';`;
}

/**
Expand Down
13 changes: 0 additions & 13 deletions test/generators/typescript/TypeScriptGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,19 +381,6 @@ ${content}`;
expect(models[1].result).toMatchSnapshot();
});

test('should render models and their dependencies for ESM module system with named exports as interfaces', async () => {
generator = new TypeScriptGenerator({
moduleSystem: 'ESM',
modelType: 'interface'
});
const models = await generator.generateCompleteModels(doc, {
exportType: 'named'
});
expect(models).toHaveLength(2);
expect(models[0].result).toMatchSnapshot();
expect(models[1].result).toMatchSnapshot();
});

describe('AsyncAPI with polymorphism', () => {
const asyncapiDoc = {
asyncapi: '2.4.0',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -948,31 +948,6 @@ class OtherModel {
export { OtherModel };"
`;

exports[`TypeScriptGenerator should render models and their dependencies for ESM module system with named exports as interfaces 1`] = `
"import type {OtherModel} from './OtherModel';
interface Address {
streetName: string;
city: string;
state: string;
houseNumber: number;
marriage?: boolean;
members?: string | number | boolean;
arrayType: (string | number | any)[];
otherModel?: OtherModel;
additionalProperties?: Map<string, any | string>;
}
export type { Address };"
`;

exports[`TypeScriptGenerator should render models and their dependencies for ESM module system with named exports as interfaces 2`] = `
"
interface OtherModel {
streetName?: string;
additionalProperties?: Map<string, any>;
}
export type { OtherModel };"
`;

exports[`TypeScriptGenerator should render union \`enum\` values 1`] = `
"enum States {
NUMBER_2 = 2,
Expand Down

0 comments on commit ad68a0f

Please sign in to comment.