Skip to content

Commit f434ad8

Browse files
committed
refactor(codegen): remove some dead code in ast/builders
1 parent 450012c commit f434ad8

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

src/__tests__/codegen.test.mjs

+10-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe('Code Generator', () => {
2020
"$.bar['children']",
2121
"$.bar['0']",
2222
"$.bar['children.bar']",
23+
"$.paths[*]['400']",
2324
'$.paths[*][404,202]',
2425
'$.channels[*][publish,subscribe][?(@.schemaFormat === void 0)].payload',
2526
]),
@@ -38,8 +39,8 @@ const zones = {
3839
}
3940
}, {
4041
zone: {
41-
keys: [404, 202],
42-
zones: [{}, {}]
42+
keys: ["400", 404, 202],
43+
zones: [{}, {}, {}]
4344
}
4445
}, {
4546
zone: {
@@ -123,6 +124,12 @@ const tree = {
123124
if (scope === null) return;
124125
scope.emit("$.bar['children.bar']", 0, false);
125126
},
127+
"$.paths[*]['400']": function (scope) {
128+
if (scope.path.length !== 3) return;
129+
if (scope.path[0] !== "paths") return;
130+
if (String(scope.path[2]) !== "400") return;
131+
scope.emit("$.paths[*]['400']", 0, false);
132+
},
126133
"$.paths[*][404,202]": function (scope) {
127134
if (scope.path.length !== 3) return;
128135
if (scope.path[0] !== "paths") return;
@@ -160,6 +167,7 @@ export default function (input, callbacks) {
160167
tree["$.servers[*].url"](scope);
161168
tree["$.servers[0:2]"](scope);
162169
tree["$.servers[:5]"](scope);
170+
tree["$.paths[*]['400']"](scope);
163171
tree["$.paths[*][404,202]"](scope);
164172
tree["$.channels[*][publish,subscribe][?(@.schemaFormat === void 0)].payload"](scope, state0);
165173
}, zones);

src/codegen/ast/builders.mjs

+7-14
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,9 @@ export function expressionStatement(expression) {
2626
}
2727

2828
export function literal(value) {
29-
switch (typeof value) {
30-
case 'number':
31-
return numericLiteral(value);
32-
case 'string':
33-
return stringLiteral(value);
34-
case 'boolean':
35-
return booleanLiteral(value);
36-
}
29+
return typeof value === 'number'
30+
? numericLiteral(value)
31+
: stringLiteral(value);
3732
}
3833

3934
export function stringLiteral(value) {
@@ -89,7 +84,6 @@ export function logicalExpression(operator, left, right) {
8984
}
9085

9186
export function ifStatement(test, consequent, alternate) {
92-
if (!consequent) throw new Error('abc');
9387
return {
9488
type: 'IfStatement',
9589
test,
@@ -110,11 +104,10 @@ export function binaryExpression(operator, left, right) {
110104
export function safeBinaryExpression(operator, left, right) {
111105
let actualRight = right;
112106

113-
if (right.type === 'NumericLiteral') {
114-
actualRight = stringLiteral(String(right.value));
115-
} else if (
116-
right.type === 'StringLiteral' &&
117-
Number.isSafeInteger(Number(right.value))
107+
if (
108+
right.type === 'NumericLiteral' ||
109+
(right.type === 'StringLiteral' &&
110+
Number.isSafeInteger(Number(right.value)))
118111
) {
119112
actualRight = stringLiteral(String(right.value));
120113
}

0 commit comments

Comments
 (0)