Skip to content

Commit 2af88a2

Browse files
committed
refactor: clean code
1 parent cd56f7e commit 2af88a2

3 files changed

Lines changed: 39 additions & 37 deletions

File tree

packages/query-parser/src/stringify.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ const BSON_TO_JS_STRING = {
112112
ISODate: function (v: Date) {
113113
try {
114114
return `ISODate('${v.toISOString()}')`;
115-
} catch (ex) {
115+
} catch {
116116
return `ISODate('${v.toString()}')`;
117117
}
118118
},
@@ -139,7 +139,7 @@ const BSON_TO_JS_STRING = {
139139
const hasOptions = v.options && v.options?.length > 0;
140140
let ctor = 'RegExp';
141141
try {
142-
RegExp(v.pattern, v.options || undefined);
142+
new RegExp(v.pattern, v.options || undefined);
143143
} catch {
144144
ctor = 'BSONRegExp';
145145
}

packages/shell-bson-parser/src/check.ts

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,30 @@ class Checker {
1818
node.arguments.every(this.checkSafeExpression)
1919
);
2020
}
21-
if (allowMethods) {
22-
if (node.callee.type === 'MemberExpression') {
23-
const object = node.callee.object;
24-
const property = node.callee.property as Identifier;
25-
// If we're only referring to identifiers, we don't need to check deeply.
26-
if (object.type === 'Identifier' && property.type === 'Identifier') {
27-
return (
28-
isMethodWhitelisted(object.name, property.name) &&
29-
node.arguments.every(this.checkSafeExpression)
30-
);
31-
} else if (
32-
(object.type === 'NewExpression' ||
33-
object.type === 'CallExpression') &&
34-
object.callee.type === 'Identifier'
35-
) {
36-
const callee = object.callee;
37-
return (
38-
isMethodWhitelisted(callee.name, property.name) &&
39-
node.arguments.every(this.checkSafeExpression)
40-
);
41-
} else {
42-
return (
43-
this.checkSafeExpression(object) &&
44-
node.arguments.every(this.checkSafeExpression)
45-
);
46-
}
21+
if (allowMethods && node.callee.type === 'MemberExpression') {
22+
const object = node.callee.object;
23+
const property = node.callee.property as Identifier;
24+
// If we're only referring to identifiers, we don't need to check deeply.
25+
if (object.type === 'Identifier' && property.type === 'Identifier') {
26+
return (
27+
isMethodWhitelisted(object.name, property.name) &&
28+
node.arguments.every(this.checkSafeExpression)
29+
);
30+
} else if (
31+
(object.type === 'NewExpression' ||
32+
object.type === 'CallExpression') &&
33+
object.callee.type === 'Identifier'
34+
) {
35+
const callee = object.callee;
36+
return (
37+
isMethodWhitelisted(callee.name, property.name) &&
38+
node.arguments.every(this.checkSafeExpression)
39+
);
40+
} else {
41+
return (
42+
this.checkSafeExpression(object) &&
43+
node.arguments.every(this.checkSafeExpression)
44+
);
4745
}
4846
}
4947
return false;
@@ -106,11 +104,13 @@ class Checker {
106104
}
107105

108106
export const checkTree = (node: Node, options: Options) => {
109-
if (node.type === 'Program') {
110-
if (node.body.length === 1 && node.body[0].type === 'ExpressionStatement') {
111-
const checker = new Checker(options);
112-
return checker.checkSafeExpression(node.body[0].expression);
113-
}
107+
if (
108+
node.type === 'Program' &&
109+
node.body.length === 1 &&
110+
node.body[0].type === 'ExpressionStatement'
111+
) {
112+
const checker = new Checker(options);
113+
return checker.checkSafeExpression(node.body[0].expression);
114114
}
115115
return false;
116116
};

packages/shell-bson-parser/src/eval.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,12 @@ const walk = (node: Node | null): any => {
158158
};
159159

160160
export const executeAST = (node: Node) => {
161-
if (node.type === 'Program') {
162-
if (node.body.length === 1 && node.body[0].type === 'ExpressionStatement') {
163-
return walk(node.body[0].expression);
164-
}
161+
if (
162+
node.type === 'Program' &&
163+
node.body.length === 1 &&
164+
node.body[0].type === 'ExpressionStatement'
165+
) {
166+
return walk(node.body[0].expression);
165167
}
166168
throw new Error('Invalid AST Found');
167169
};

0 commit comments

Comments
 (0)