Skip to content

Commit a3288f9

Browse files
Jantherfvictorio
andauthored
using a dictionary for the hug functions (#1417)
* using a dictionary for the hug functions * Update src/slang-nodes/MultiplicativeExpression.ts Co-authored-by: Franco Victorio <victorio.franco@gmail.com> --------- Co-authored-by: Franco Victorio <victorio.franco@gmail.com>
1 parent 654e0c5 commit a3288f9

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

src/slang-nodes/MultiplicativeExpression.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import type { AstPath, Doc, ParserOptions } from 'prettier';
1111
import type { CollectedMetadata, PrintFunction } from '../types.d.ts';
1212
import type { AstNode } from './types.d.ts';
1313

14-
const multiplicationTryToHug = createHugFunction(['/', '%']);
15-
const divisionTryToHug = createHugFunction(['*', '%']);
16-
const moduloTryToHug = createHugFunction(['*', '/', '%']);
14+
const hugFunctions = {
15+
'*': createHugFunction(['/', '%']),
16+
'/': createHugFunction(['*', '%']),
17+
'%': createHugFunction(['*', '/', '%'])
18+
};
1719

1820
const printMultiplicativeExpression = printBinaryOperation(
1921
createKindCheckFunction([
@@ -55,19 +57,13 @@ export class MultiplicativeExpression extends SlangNode {
5557

5658
this.updateMetadata(this.leftOperand, this.rightOperand);
5759

58-
switch (this.operator) {
59-
case '*':
60-
this.leftOperand = multiplicationTryToHug(this.leftOperand);
61-
break;
62-
case '/':
63-
this.leftOperand = divisionTryToHug(this.leftOperand);
64-
break;
65-
case '%':
66-
this.leftOperand = moduloTryToHug(this.leftOperand);
67-
break;
68-
default:
69-
throw new Error(`Unexpected operator: ${this.operator}`);
60+
const tryToHug = hugFunctions[this.operator as keyof typeof hugFunctions];
61+
62+
if (tryToHug === undefined) {
63+
throw new Error(`Unexpected operator: ${this.operator}`);
7064
}
65+
66+
this.leftOperand = tryToHug(this.leftOperand);
7167
}
7268

7369
print(

0 commit comments

Comments
 (0)