Skip to content

Commit

Permalink
feat: Add support for template literals (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
yokljo authored Jul 31, 2024
1 parent a4b895c commit 6daec99
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/nodes/TemplateLiteral.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type ESTree from 'estree';
import BaseJSNode from './BaseJSNode.js';

export default class TemplateLiteral extends BaseJSNode<ESTree.TemplateLiteral> {
public run() {
let result: string = "";

Check failure on line 6 in src/nodes/TemplateLiteral.ts

View workflow job for this annotation

GitHub Actions / lint

Type string trivially inferred from a string literal, remove type annotation

Check failure on line 6 in src/nodes/TemplateLiteral.ts

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote

for (let i = 0; i < this.node.quasis.length; ++i) {
let quasi = this.node.quasis[i];

Check failure on line 9 in src/nodes/TemplateLiteral.ts

View workflow job for this annotation

GitHub Actions / lint

'quasi' is never reassigned. Use 'const' instead

if (quasi.type === 'TemplateElement') {
if (quasi.value.cooked === null) {
throw new Error(`Invalid template literal: ${quasi.value.raw}`);
}

if (quasi.value.cooked !== undefined) {
result += quasi.value.cooked;
}

if (!quasi.tail) {
let expr = this.node.expressions[i];

Check failure on line 21 in src/nodes/TemplateLiteral.ts

View workflow job for this annotation

GitHub Actions / lint

'expr' is never reassigned. Use 'const' instead
if (expr !== undefined) {
// This will automatically stringify the node's return value, since result is a string.
result += this.visitor.visitNode(expr);
} else {
throw new Error(`Expected expression after: ${quasi.value}`);
}
}
} else {
throw new Error(`Unhandled quasi type: ${quasi.type}`);
}
}

return result;
}
}
1 change: 1 addition & 0 deletions src/nodes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export { default as ReturnStatement } from './ReturnStatement.js';
export { default as SequenceExpression } from './SequenceExpression.js';
export { default as SwitchCase } from './SwitchCase.js';
export { default as SwitchStatement } from './SwitchStatement.js';
export { default as TemplateLiteral } from './TemplateLiteral.js';
export { default as ThisExpression } from './ThisExpression.js';
export { default as ThrowStatement } from './ThrowStatement.js';
export { default as TryStatement } from './TryStatement.js';
Expand Down
13 changes: 13 additions & 0 deletions test/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,5 +608,18 @@ describe('Jinter Tests', () => {

expect(jinter.scope.get('result')).toBeTruthy();
});

it('should support template literals', () => {
const code = `
let a = \`world\`;
let b = 1234;
const result = \`hello \${a}\${a}, \\u{55} \${b * 2}! \${'test'}\`;
`;

const jinter = new Jinter();
jinter.evaluate(code)

expect(jinter.scope.get('result')).toBe('hello worldworld, U 2468! test');
});
});
});

0 comments on commit 6daec99

Please sign in to comment.