Skip to content

Commit

Permalink
fix: Always wrap window/global/this in a try/catch
Browse files Browse the repository at this point in the history
  • Loading branch information
valya committed Aug 31, 2023
1 parent 917a576 commit b21e4da
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/ExpressionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface ExpressionAnalysis {

const v = b.identifier('v');

const shouldAlwaysWrapList = ['window', 'global', 'this'];

const shouldWrapInTry = (node: namedTypes.ASTNode) => {
let shouldWrap = false;

Expand All @@ -32,6 +34,14 @@ const shouldWrapInTry = (node: namedTypes.ASTNode) => {
shouldWrap = true;
return false;
},
visitIdentifier(path) {
if (shouldAlwaysWrapList.includes(path.node.name)) {
shouldWrap = true;
return false;
}
this.traverse(path);
return;
},
});

return shouldWrap;
Expand Down
3 changes: 3 additions & 0 deletions test/ExpressionFixtures/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -826,4 +826,7 @@ line`,
`,
tests: [],
},
{ expression: '={{window}}', tests: [] },
{ expression: '={{global}}', tests: [] },
{ expression: '={{test = 3}}', tests: [] },
];
8 changes: 1 addition & 7 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ import type { ExpressionTestEvaluation } from './ExpressionFixtures/base';
import { baseFixtures } from './ExpressionFixtures/base';

export const testExpressionsWithEvaluator = (Evaluator: ExpressionEvaluatorClass) => {
const tourn = new Tournament(
(e) => {
console.error(e);
},
undefined,
Evaluator,
);
const tourn = new Tournament(() => {}, undefined, Evaluator);
const builtins = {
String,
parseFloat,
Expand Down

0 comments on commit b21e4da

Please sign in to comment.