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

fix(INT-833): conditional expression parsing is failing parse objects #46

Merged
merged 5 commits into from
Oct 18, 2023
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
21 changes: 16 additions & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,24 @@
}
},
{
"name": "Test Scenario",
"program": "${workspaceFolder}/test/test_scenario.ts",
"runtimeExecutable": "/usr/local/bin/node",
"name": "Jest Scenario",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"request": "launch",
"runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"],
"skipFiles": ["<node_internals>/**"],
"args": ["--scenario=${input:scenario}", "--index=${input:index}"],
"type": "node"
"args": [
"scenario.test",
"--config",
"jest.config.ts",
"--scenario=${input:scenario}",
"--index=${input:index}"
],
"type": "node",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
}
},
{
"runtimeExecutable": "/usr/local/bin/node",
Expand Down
13 changes: 8 additions & 5 deletions src/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export class JsonTemplateLexer {
private readonly codeChars: string[];
private buf: Token[];
private idx = 0;
private lastParsedToken?: Token;

constructor(template: string) {
this.buf = [];
Expand All @@ -29,6 +28,11 @@ export class JsonTemplateLexer {
return this.idx;
}

reset(idx: number) {
this.idx = idx;
this.buf = [];
}

getCode(start: number, end: number): string {
return this.codeChars.slice(start, end).join('');
}
Expand Down Expand Up @@ -272,12 +276,11 @@ export class JsonTemplateLexer {
lex(): Token {
if (this.buf[0]) {
this.idx = this.buf[0].range[1];
this.lastParsedToken = this.buf[0];
let token = this.buf[0];
this.buf = this.buf.slice(1);
return this.lastParsedToken;
return token;
}
this.lastParsedToken = this.advance();
return this.lastParsedToken;
return this.advance();
}

static isLiteralToken(token: Token) {
Expand Down
12 changes: 10 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
LoopExpression,
IncrementExpression,
} from './types';
import { JsonTemplateParserError } from './errors';
import { JsonTemplateLexerError, JsonTemplateParserError } from './errors';
import { BINDINGS_PARAM_KEY, DATA_PARAM_KEY } from './constants';
import { JsonTemplateLexer } from './lexer';
import { CommonUtils } from './utils';
Expand Down Expand Up @@ -471,8 +471,16 @@ export class JsonTemplateParser {
}

private parseConditionalBodyExpr(): Expression {
const currentIndex = this.lexer.currentIndex();
if (this.lexer.match('{')) {
return this.parseCurlyBlockExpr({ blockEnd: '}', parentType: SyntaxType.CONDITIONAL_EXPR });
try {
return this.parseObjectExpr();
} catch (error: any) {
if (error instanceof JsonTemplateLexerError) {
this.lexer.reset(currentIndex);
}
return this.parseCurlyBlockExpr({ blockEnd: '}', parentType: SyntaxType.CONDITIONAL_EXPR });
}
}
return this.parseBaseExpr();
}
Expand Down
22 changes: 14 additions & 8 deletions test/scenarios/conditions/data.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { Scenario } from '../../types';

export const data: Scenario[] = [
{
templatePath: 'empty_if.jt',
error: 'Empty statements are not allowed in loop and condtional expressions',
},
{
templatePath: 'empty_then.jt',
error: 'Empty statements are not allowed in loop and condtional expressions',
},
{
templatePath: 'if_block.jt',
input: {
Expand Down Expand Up @@ -58,6 +50,20 @@ export const data: Scenario[] = [
},
output: 5,
},
{
templatePath: 'objects.jt',
input: {
a: 5,
},
output: { message: 'a > 1' },
},
{
templatePath: 'objects.jt',
input: {
a: 0,
},
output: { message: 'a <= 1' },
},
{
input: {
a: 5,
Expand Down
1 change: 0 additions & 1 deletion test/scenarios/conditions/empty_if.jt

This file was deleted.

1 change: 0 additions & 1 deletion test/scenarios/conditions/empty_then.jt

This file was deleted.

1 change: 1 addition & 0 deletions test/scenarios/conditions/objects.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.a > 1 ? { message: "a > 1" } : { message: "a <= 1" }
6 changes: 6 additions & 0 deletions test_engine_local1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { JsonTemplateEngine } from './src';

console.log(
JsonTemplateEngine.translate(`
.output ? .output : { "foo": "bar" }`),
);
Loading