Skip to content

Commit

Permalink
refactor: address pr comments on template parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip committed Jun 3, 2024
1 parent 644f7aa commit 0625be4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
26 changes: 14 additions & 12 deletions src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { JsonTemplateParser } from './parser';
import { JsonTemplateReverseTranslator } from './reverse_translator';
import { JsonTemplateTranslator } from './translator';
import { EngineOptions, Expression, FlatMappingPaths } from './types';
import { CreateAsyncFunction, convertToObjectMapping } from './utils';
import { CreateAsyncFunction, convertToObjectMapping, isExpression } from './utils';

export class JsonTemplateEngine {
private readonly fn: Function;
Expand Down Expand Up @@ -63,24 +63,26 @@ export class JsonTemplateEngine {
return new JsonTemplateEngine(this.compileAsSync(templateOrExpr, options));
}

static parse(template: string | FlatMappingPaths[], options?: EngineOptions): Expression {
if (Array.isArray(template)) {
return this.parseMappingPaths(template, options);
static parse(
template: string | Expression | FlatMappingPaths[],
options?: EngineOptions,
): Expression {
if (isExpression(template)) {
return template as Expression;
}
const lexer = new JsonTemplateLexer(template);
const parser = new JsonTemplateParser(lexer, options);
return parser.parse();
if (typeof template === 'string') {
const lexer = new JsonTemplateLexer(template);
const parser = new JsonTemplateParser(lexer, options);
return parser.parse();
}
return this.parseMappingPaths(template as FlatMappingPaths[], options);
}

static translate(
template: string | Expression | FlatMappingPaths[],
options?: EngineOptions,
): string {
let templateExpr = template as Expression;
if (typeof template === 'string' || Array.isArray(template)) {
templateExpr = this.parse(template, options);
}
return this.translateExpression(templateExpr);
return this.translateExpression(this.parse(template, options));
}

static reverseTranslate(expr: Expression, options?: EngineOptions): string {
Expand Down
5 changes: 2 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BINDINGS_PARAM_KEY, DATA_PARAM_KEY, EMPTY_EXPR } from './constants';
import { JsonTemplateEngine } from './engine';

Check failure on line 2 in src/parser.ts

View workflow job for this annotation

GitHub Actions / Check for formatting & lint errors

Dependency cycle detected
import { JsonTemplateLexerError, JsonTemplateParserError } from './errors';
import { JsonTemplateLexer } from './lexer';
import {
Expand Down Expand Up @@ -1235,14 +1236,12 @@ export class JsonTemplateParser {
const expr = skipJsonify ? this.parseCompileTimeBaseExpr() : this.parseBaseExpr();
this.lexer.expect('}');
this.lexer.expect('}');
// eslint-disable-next-line global-require
const { JsonTemplateEngine } = require('./engine');
const exprVal = JsonTemplateEngine.createAsSync(expr).evaluate(
{},
this.options?.compileTimeBindings,
);
const template = skipJsonify ? exprVal : JSON.stringify(exprVal);
return JsonTemplateParser.parseBaseExprFromTemplate(template);
return JsonTemplateParser.parseBaseExprFromTemplate(template as string);
}

private parseNumber(): LiteralExpression {
Expand Down
14 changes: 13 additions & 1 deletion src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { type Expression, type StatementsExpression, SyntaxType, BlockExpression } from '../types';
import {
type Expression,
type StatementsExpression,
SyntaxType,
BlockExpression,
FlatMappingPaths,
} from '../types';

export function toArray<T>(val: T | T[] | undefined): T[] | undefined {
if (val === undefined || val === null) {
Expand Down Expand Up @@ -33,6 +39,12 @@ export function CreateAsyncFunction(...args) {
return async function () {}.constructor(...args);
}

export function isExpression(val: string | Expression | FlatMappingPaths[]): boolean {
return (
typeof val === 'object' && !Array.isArray(val) && Object.values(SyntaxType).includes(val.type)
);
}

export function escapeStr(s?: string): string {
if (typeof s !== 'string') {
return '';
Expand Down
4 changes: 0 additions & 4 deletions test/scenarios/return/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ export const data: Scenario[] = [
templatePath: 'return_no_value.jt',
input: 2,
},
{
templatePath: 'return_no_value.jt',
input: 2,
},
{
templatePath: 'return_value.jt',
input: 3,
Expand Down

0 comments on commit 0625be4

Please sign in to comment.