-
Notifications
You must be signed in to change notification settings - Fork 180
/
shared.ts
40 lines (29 loc) · 1.11 KB
/
shared.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import type { MigrationOptions } from '../../types';
import { formatParams } from '../../utils';
import type { FunctionParam } from '../functions';
import type { Name } from '../generalTypes';
export interface OperatorListDefinition {
type: 'function' | 'operator';
number: number;
name: Name;
params?: FunctionParam[];
}
export function operatorMap(
mOptions: MigrationOptions
): ({ type, number, name, params }: OperatorListDefinition) => string {
return ({ type, number, name, params = [] }) => {
const nameStr = mOptions.literal(name);
if (String(type).toLowerCase() === 'operator') {
if (params.length > 2) {
throw new Error("Operator can't have more than 2 parameters");
}
const paramsStr = params.length > 0 ? formatParams(params, mOptions) : '';
return `OPERATOR ${number} ${nameStr}${paramsStr}`;
}
if (String(type).toLowerCase() === 'function') {
const paramsStr = formatParams(params, mOptions);
return `FUNCTION ${number} ${nameStr}${paramsStr}`;
}
throw new Error('Operator "type" must be either "function" or "operator"');
};
}