Skip to content

Commit

Permalink
Fix nested scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
littledan committed Dec 26, 2018
1 parent f0eb94c commit 0c0fd79
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/transform/package.json
Expand Up @@ -27,6 +27,6 @@
"dependencies": {
"@babel/core": "^7.2.2",
"@babel/helper-plugin-utils": "^7.0.0",
"@littledan/operator-overloading-shim": "0.0.3"
"@littledan/operator-overloading-shim": "0.0.4"
}
}
4 changes: 1 addition & 3 deletions src/transform/plugin.js
Expand Up @@ -93,10 +93,8 @@ const visitBlockStatementLike = {
prelude.push(requireShimTemplate({SHIM: this.shim}));
}
const operators = path.scope.generateUidIdentifier("operators");
const outer = this.inactive() ? undefined : this.peek().operators;
this.stack.push({operators, path});
const outer = this.inactive()
? t.Identifier("undefined")
: this.peek().operator;
prelude.push(declareOperatorsTemplate({
OPERATORS: operators,
SHIM: this.shim,
Expand Down
43 changes: 41 additions & 2 deletions src/transform/plugin.spec.js
Expand Up @@ -30,7 +30,7 @@ describe("overloading + works", () => {
let val;
eval(code);
expect(val).toBe(6);
});
});
});

describe("test everything on a full wrapper of Numbers (no interoperation)", () => {
Expand Down Expand Up @@ -142,7 +142,7 @@ describe("nested scopes", () => {
withOperatorsFrom(OpsB);
expect(() => +a).toThrowError(TypeError);
expect(+b).toBe(1);
expect(+b).toBe(2);
expect(() => a+b).toThrowError(TypeError);
withOperatorsFrom(OpsA);
Expand All @@ -151,5 +151,44 @@ describe("nested scopes", () => {
expect(+b).toBe(2);
expect(a+b).toBe(3);
`));

eval(transform(`
expect(() => +a).toThrowError(TypeError);
expect(() => +b).toThrowError(TypeError);
expect(() => a+b).toThrowError(TypeError);
withOperatorsFrom(OpsA, OpsB);
expect(+a).toBe(1);
expect(+b).toBe(2);
expect(a+b).toBe(3);
`));
});

it("throws appropriate errors in nested code", () => {
eval(transform(`
expect(() => +a).toThrowError(TypeError);
expect(() => +b).toThrowError(TypeError);
expect(() => a+b).toThrowError(TypeError);
withOperatorsFrom(OpsA);
{
expect(+a).toBe(1);
expect(() => +b).toThrowError(TypeError);
expect(() => a+b).toThrowError(TypeError);
withOperatorsFrom(OpsB);
expect(+a).toBe(1);
expect(+b).toBe(2);
expect(a+b).toBe(3);
}
expect(+a).toBe(1);
expect(() => +b).toThrowError(TypeError);
expect(() => a+b).toThrowError(TypeError);
`));
});

});

0 comments on commit 0c0fd79

Please sign in to comment.