Skip to content

Incorrect execution result #25

@qbx2

Description

@qbx2

Hello! First of all, I want to thank you for open-sourcing this nice project!
I am trying to combine SandboxJS with typescript to evaluate typescript code. However, the evaluation undesirably fails when I use optional chaining operators like below (I'm not targeting ES2022 because SandboxJS does not support nullish coalescing operator (??) at the moment):

import ts from 'typescript';

const scope = { myTest: null };
const code2 = ts.transpileModule(`return myTest?.myMethod;`, {
    compilerOptions: { target: ts.ScriptTarget.ES5 },
});
console.log('transpiled', code2.outputText);
const exec2 = sandbox.compile(code2.outputText);
const result2 = exec2(scope).run();
console.log(result2);

Result:

transpiled return myTest === null || myTest === void 0 ? void 0 : myTest.myMethod;

/Users/qbx2/test/node_modules/.pnpm/@nyariv+sandboxjs@0.8.23/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:1929
        throw new TypeError(`Cannot get property ${b} of null`);
              ^

TypeError: Cannot get property myMethod of null
    at /Users/qbx2/test/node_modules/.pnpm/@nyariv+sandboxjs@0.8.23/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:1929:15
    at execSync (/Users/qbx2/test/node_modules/.pnpm/@nyariv+sandboxjs@0.8.23/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2758:28)
    at /Users/qbx2/test/node_modules/.pnpm/@nyariv+sandboxjs@0.8.23/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2447:5
    at execSync (/Users/qbx2/test/node_modules/.pnpm/@nyariv+sandboxjs@0.8.23/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2758:28)
    at /Users/qbx2/test/node_modules/.pnpm/@nyariv+sandboxjs@0.8.23/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2739:36
    at syncDone (/Users/qbx2/test/node_modules/.pnpm/@nyariv+sandboxjs@0.8.23/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2615:5)
    at execSync (/Users/qbx2/test/node_modules/.pnpm/@nyariv+sandboxjs@0.8.23/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2739:20)
    at /Users/qbx2/test/node_modules/.pnpm/@nyariv+sandboxjs@0.8.23/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2739:36
    at syncDone (/Users/qbx2/test/node_modules/.pnpm/@nyariv+sandboxjs@0.8.23/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2615:5)
    at execSync (/Users/qbx2/test/node_modules/.pnpm/@nyariv+sandboxjs@0.8.23/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2739:20)

Node.js v18.17.0

NOTE: After a small amount of digging, it seems that there is a priority issue because the evaluation succeeds when I add parentheses around the condition like return (myTest === null || myTest === void 0) ? void 0 : myTest.myMethod.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions