Skip to content

Commit

Permalink
Merge pull request #8 from launchql/refactor/names
Browse files Browse the repository at this point in the history
naming cleanup
  • Loading branch information
pyramation authored Apr 1, 2024
2 parents 0a735ef + b92fd8c commit 70ad5a5
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 58 deletions.
8 changes: 4 additions & 4 deletions packages/parser/src/ast/enums/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Enum } from '@launchql/protobufjs';
import * as t from '@babel/types';
import { createNamedImport } from '../../utils';

export const transformEnumToAST = (enumData: Enum) => {
export const convertEnumToTsEnumDeclaration = (enumData: Enum) => {
const members = Object.entries(enumData.values).map(([key, value]) =>
t.tsEnumMember(t.identifier(key), t.numericLiteral(value as number))
);
Expand All @@ -11,11 +11,11 @@ export const transformEnumToAST = (enumData: Enum) => {
return t.exportNamedDeclaration(enumDeclaration);
};

export const buildEnumNamedImports = (enums: Enum[], source: string) => {
export const generateEnumImports = (enums: Enum[], source: string) => {
return createNamedImport(enums.map(e=>e.name), source);
};

export const transformEnumToTypeUnionAST = (enumData: Enum) => {
export const convertEnumToTsUnionType = (enumData: Enum) => {
const literals = Object.keys(enumData.values).map(key =>
t.tsLiteralType(t.stringLiteral(key))
);
Expand All @@ -31,7 +31,7 @@ export const transformEnumToTypeUnionAST = (enumData: Enum) => {
return t.exportNamedDeclaration(typeAlias);
};

export const buildEnumValueFunctionAST = (enumData: Enum[]) => {
export const generateEnumValueFunctions = (enumData: Enum[]) => {
// Create the union type for EnumType
const enumTypeIdentifier = t.identifier('EnumType');
const enumTypeUnion = t.tsUnionType(
Expand Down
20 changes: 10 additions & 10 deletions packages/parser/src/ast/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { PgProtoParserOptions } from '../../options';
import { SPECIAL_TYPES } from '../../constants';
import { resolveTypeName } from './utils';

export const buildTypeNamedImports = (types: Type[], source: string, suffix?: string) => {
export const generateTypeImports = (types: Type[], source: string, suffix?: string) => {
return suffix ?
createNamedImportAsSuffix(types.map(e => e.name), source, suffix) :
createNamedImport(types.map(e => e.name), source);
};

export const createAstHelperMethodsAST = (types: Type[]): t.ExportDefaultDeclaration => {
export const generateAstHelperMethods = (types: Type[]): t.ExportDefaultDeclaration => {
const creators = types.map((type: Type) => {
const typeName = type.name;
const param = t.identifier('_p');
Expand Down Expand Up @@ -89,7 +89,7 @@ export const createAstHelperMethodsAST = (types: Type[]): t.ExportDefaultDeclara
}

// special for Node
export const createNodeUnionTypeAST = (types: Type[]) => {
export const generateNodeUnionType = (types: Type[]) => {
const unionTypeNames = types.map(type => t.tsTypeReference(t.identifier(type.name)));

const unionTypeAlias = t.tsTypeAliasDeclaration(
Expand All @@ -101,7 +101,7 @@ export const createNodeUnionTypeAST = (types: Type[]) => {
return t.exportNamedDeclaration(unionTypeAlias, []);
};

const getTypeFieldsAsTS = (
const extractTypeFieldsAsTsProperties = (
type: Type,
options: PgProtoParserOptions
) => {
Expand All @@ -126,12 +126,12 @@ const getTypeFieldsAsTS = (
return properties;
}

export const transformTypeToTSInterface = (
export const convertTypeToTsInterface = (
type: Type,
options: PgProtoParserOptions
) => {
const typeName = type.name;
const properties = getTypeFieldsAsTS(type, options);
const properties = extractTypeFieldsAsTsProperties(type, options);

const interfaceDecl = t.tsInterfaceDeclaration(
t.identifier(typeName),
Expand All @@ -143,14 +143,14 @@ export const transformTypeToTSInterface = (
// Wrap the interface declaration in an export statement
return t.exportNamedDeclaration(interfaceDecl, []);
}
export const transformTypeToTSWrappedInterface = (
export const convertTypeToWrappedTsInterface = (
type: Type,
options: PgProtoParserOptions
) => {
const typeName = type.name;
if (SPECIAL_TYPES.includes(typeName)) return transformTypeToTSInterface(type, options);
if (SPECIAL_TYPES.includes(typeName)) return convertTypeToTsInterface(type, options);

const properties = getTypeFieldsAsTS(type, options);
const properties = extractTypeFieldsAsTsProperties(type, options);

const nestedInterfaceBody = t.tsInterfaceBody([
t.tsPropertySignature(
Expand All @@ -169,7 +169,7 @@ export const transformTypeToTSWrappedInterface = (
return t.exportNamedDeclaration(interfaceDecl, []);
};

export const generateImportSpecifiersAST = (types: Type[], options: PgProtoParserOptions) => {
export const generateTypeImportSpecifiers = (types: Type[], options: PgProtoParserOptions) => {
const importSpecifiers = types.map(type =>
t.importSpecifier(t.identifier(type.name), t.identifier(type.name))
);
Expand Down
26 changes: 13 additions & 13 deletions packages/parser/src/store.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Service, Type, Field, Enum, Namespace, ReflectionObject } from '@launchql/protobufjs';
import { buildEnumNamedImports, createAstHelperMethodsAST, generateImportSpecifiersAST, buildEnumValueFunctionAST, transformEnumToTypeUnionAST, transformEnumToAST, createNodeUnionTypeAST, buildTypeNamedImports, transformTypeToTSInterface, transformTypeToTSWrappedInterface } from './ast';
import { generateEnumImports, generateAstHelperMethods, generateTypeImportSpecifiers, generateEnumValueFunctions, convertEnumToTsUnionType, convertEnumToTsEnumDeclaration, generateNodeUnionType, convertTypeToTsInterface, convertTypeToWrappedTsInterface } from './ast';
import { generateEnum2IntJSON, generateEnum2StrJSON } from './ast/enums/enums-json';
import { sync as mkdirp } from 'mkdirp';
import { join } from 'path';
import { defaultPgProtoParserOptions, getOptionsWithDefaults, PgProtoStoreOptions } from './options';
import { getOptionsWithDefaults, PgProtoStoreOptions } from './options';
import { cloneAndNameNode, convertAstToCode, createDefaultImport, getUndefinedKey, hasUndefinedInitialValue, stripExtension, writeFileToDisk } from './utils';
import { nestedObjCode } from './inline-helpers';
import * as t from '@babel/types';
Expand Down Expand Up @@ -125,10 +125,10 @@ export class ProtoStore implements IProtoStore {
if (this.options.types.enabled) {
const typesToProcess = this.typesToProcess();
const enumsToProcess = this.enumsToProcess();
const node = createNodeUnionTypeAST(typesToProcess);
const enumImports = buildEnumNamedImports(enumsToProcess, this.options.types.enumsSource);
const node = generateNodeUnionType(typesToProcess);
const enumImports = generateEnumImports(enumsToProcess, this.options.types.enumsSource);
const types = typesToProcess.reduce((m, type) => {
return [...m, transformTypeToTSInterface(type, this.options)]
return [...m, convertTypeToTsInterface(type, this.options)]
}, []);
this.writeCodeToFile(this.options.types.filename, [
enumImports,
Expand All @@ -141,13 +141,13 @@ export class ProtoStore implements IProtoStore {
writeWrappedTypes() {
if (this.options.types.wrapped.enabled) {
const typesToProcess = this.typesToProcess();
const enumImports = buildEnumNamedImports(
const enumImports = generateEnumImports(
this.enumsToProcess(),
this.options.types.wrapped.enumsSource
);
const node = createNodeUnionTypeAST(typesToProcess);
const node = generateNodeUnionType(typesToProcess);
const types = typesToProcess.reduce((m, type) => {
return [...m, transformTypeToTSWrappedInterface(type, this.options)]
return [...m, convertTypeToWrappedTsInterface(type, this.options)]
}, []);
this.writeCodeToFile(this.options.types.wrapped.filename, [
enumImports,
Expand All @@ -161,16 +161,16 @@ export class ProtoStore implements IProtoStore {
if (this.options.enums.enabled) {
this.writeCodeToFile(this.options.enums.filename,
this.enumsToProcess().map(enm => this.options.enums.enumsAsTypeUnion ?
transformEnumToTypeUnionAST(enm) :
transformEnumToAST(enm)
convertEnumToTsUnionType(enm) :
convertEnumToTsEnumDeclaration(enm)
)
);
}
}

writeUtilsEnums() {
if (this.options.utils.enums.enabled) {
const code = convertAstToCode(buildEnumValueFunctionAST(this.enumsToProcess()));
const code = convertAstToCode(generateEnumValueFunctions(this.enumsToProcess()));
this.writeFile(this.options.utils.enums.filename, code);
}
}
Expand All @@ -184,8 +184,8 @@ export class ProtoStore implements IProtoStore {
const typesToProcess = this.typesToProcess();
const code = convertAstToCode([
imports,
generateImportSpecifiersAST(typesToProcess, this.options),
createAstHelperMethodsAST(typesToProcess)
generateTypeImportSpecifiers(typesToProcess, this.options),
generateAstHelperMethods(typesToProcess)
]);

this.writeFile(this.options.utils.astHelpers.filename, code);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import generate from '@babel/generator';
import * as t from '@babel/types';

export const convertAstToCode = (body: any[]) => {
const ast = t.file(t.program(body));
const { code } = generate(ast);
return code;
};

export const createDefaultImport = (importName: string, source: string) => {
return t.importDeclaration(
[t.importDefaultSpecifier(t.identifier(importName))],
Expand Down
6 changes: 0 additions & 6 deletions packages/parser/src/utils/files.ts

This file was deleted.

8 changes: 0 additions & 8 deletions packages/parser/src/utils/generate.ts

This file was deleted.

6 changes: 2 additions & 4 deletions packages/parser/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export * from './proto';
export * from './files';
export * from './imports';
export * from './generate';
export * from './utils';
export * from './babel';
export * from './deps';
export * from './types';
export * from './meta';
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Type, Enum, Field, ReflectionObject } from '@launchql/protobufjs';
import { Enum, Field, ReflectionObject } from '@launchql/protobufjs';
import pkg from '../../package.json';
import { PgProtoParserOptions } from '../options';
import { writeFileSync } from 'fs';
import { extname } from 'path';
import { extname, basename } from 'path';

export const getUndefinedKey = (enumName) => {
// Split the name into parts where a lowercase letter is followed by an uppercase letter
Expand Down Expand Up @@ -64,4 +64,10 @@ export const getHeader = () => {
export const writeFileToDisk = (path: string, contents: string, options: PgProtoParserOptions) => {
const c = (options.includeHeader && extname(path) === '.ts') ? `${getHeader()}${contents}` : contents;
writeFileSync(path, c);
}
}

export const stripExtension = (filename) => {
const extension = extname(filename);
return basename(filename, extension);
}

8 changes: 4 additions & 4 deletions packages/parser/types/ast/enums/enums.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Enum } from '@launchql/protobufjs';
import * as t from '@babel/types';
export declare const transformEnumToAST: (enumData: Enum) => t.ExportNamedDeclaration;
export declare const buildEnumNamedImports: (enums: Enum[], source: string) => t.ImportDeclaration;
export declare const transformEnumToTypeUnionAST: (enumData: Enum) => t.ExportNamedDeclaration;
export declare const buildEnumValueFunctionAST: (enumData: Enum[]) => t.ExportNamedDeclaration[];
export declare const convertEnumToTsEnumDeclaration: (enumData: Enum) => t.ExportNamedDeclaration;
export declare const generateEnumImports: (enums: Enum[], source: string) => t.ImportDeclaration;
export declare const convertEnumToTsUnionType: (enumData: Enum) => t.ExportNamedDeclaration;
export declare const generateEnumValueFunctions: (enumData: Enum[]) => t.ExportNamedDeclaration[];
12 changes: 6 additions & 6 deletions packages/parser/types/ast/types/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Type } from '@launchql/protobufjs';
import * as t from '@babel/types';
import { PgProtoParserOptions } from '../../options';
export declare const buildTypeNamedImports: (types: Type[], source: string, suffix?: string) => t.ImportDeclaration;
export declare const createAstHelperMethodsAST: (types: Type[]) => t.ExportDefaultDeclaration;
export declare const createNodeUnionTypeAST: (types: Type[]) => t.ExportNamedDeclaration;
export declare const transformTypeToTSInterface: (type: Type, options: PgProtoParserOptions) => t.ExportNamedDeclaration;
export declare const transformTypeToTSWrappedInterface: (type: Type, options: PgProtoParserOptions) => t.ExportNamedDeclaration;
export declare const generateImportSpecifiersAST: (types: Type[], options: PgProtoParserOptions) => t.ImportDeclaration;
export declare const generateTypeImports: (types: Type[], source: string, suffix?: string) => t.ImportDeclaration;
export declare const generateAstHelperMethods: (types: Type[]) => t.ExportDefaultDeclaration;
export declare const generateNodeUnionType: (types: Type[]) => t.ExportNamedDeclaration;
export declare const convertTypeToTsInterface: (type: Type, options: PgProtoParserOptions) => t.ExportNamedDeclaration;
export declare const convertTypeToWrappedTsInterface: (type: Type, options: PgProtoParserOptions) => t.ExportNamedDeclaration;
export declare const generateTypeImportSpecifiers: (types: Type[], options: PgProtoParserOptions) => t.ImportDeclaration;

0 comments on commit 70ad5a5

Please sign in to comment.