Skip to content

Commit

Permalink
refactor(codegen): remove some dead code in ast/builders
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Feb 5, 2024
1 parent 450012c commit f434ad8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
12 changes: 10 additions & 2 deletions src/__tests__/codegen.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('Code Generator', () => {
"$.bar['children']",
"$.bar['0']",
"$.bar['children.bar']",
"$.paths[*]['400']",
'$.paths[*][404,202]',
'$.channels[*][publish,subscribe][?(@.schemaFormat === void 0)].payload',
]),
Expand All @@ -38,8 +39,8 @@ const zones = {
}
}, {
zone: {
keys: [404, 202],
zones: [{}, {}]
keys: ["400", 404, 202],
zones: [{}, {}, {}]
}
}, {
zone: {
Expand Down Expand Up @@ -123,6 +124,12 @@ const tree = {
if (scope === null) return;
scope.emit("$.bar['children.bar']", 0, false);
},
"$.paths[*]['400']": function (scope) {
if (scope.path.length !== 3) return;
if (scope.path[0] !== "paths") return;
if (String(scope.path[2]) !== "400") return;
scope.emit("$.paths[*]['400']", 0, false);
},
"$.paths[*][404,202]": function (scope) {
if (scope.path.length !== 3) return;
if (scope.path[0] !== "paths") return;
Expand Down Expand Up @@ -160,6 +167,7 @@ export default function (input, callbacks) {
tree["$.servers[*].url"](scope);
tree["$.servers[0:2]"](scope);
tree["$.servers[:5]"](scope);
tree["$.paths[*]['400']"](scope);
tree["$.paths[*][404,202]"](scope);
tree["$.channels[*][publish,subscribe][?(@.schemaFormat === void 0)].payload"](scope, state0);
}, zones);
Expand Down
21 changes: 7 additions & 14 deletions src/codegen/ast/builders.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,9 @@ export function expressionStatement(expression) {
}

export function literal(value) {
switch (typeof value) {
case 'number':
return numericLiteral(value);
case 'string':
return stringLiteral(value);
case 'boolean':
return booleanLiteral(value);
}
return typeof value === 'number'
? numericLiteral(value)
: stringLiteral(value);
}

export function stringLiteral(value) {
Expand Down Expand Up @@ -89,7 +84,6 @@ export function logicalExpression(operator, left, right) {
}

export function ifStatement(test, consequent, alternate) {
if (!consequent) throw new Error('abc');
return {
type: 'IfStatement',
test,
Expand All @@ -110,11 +104,10 @@ export function binaryExpression(operator, left, right) {
export function safeBinaryExpression(operator, left, right) {
let actualRight = right;

if (right.type === 'NumericLiteral') {
actualRight = stringLiteral(String(right.value));
} else if (
right.type === 'StringLiteral' &&
Number.isSafeInteger(Number(right.value))
if (
right.type === 'NumericLiteral' ||
(right.type === 'StringLiteral' &&
Number.isSafeInteger(Number(right.value)))
) {
actualRight = stringLiteral(String(right.value));
}
Expand Down

0 comments on commit f434ad8

Please sign in to comment.