Skip to content

Commit a46d419

Browse files
committed
don't apply implicit setup() when setup/draw defined as var. Fixes #36.
1 parent cd88f7d commit a46d419

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

lib/implicit-sketch.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ export default function makeImplicitSketch(code: string) {
1818
return code;
1919
}
2020
}
21+
22+
if (statement.type === esprima.Syntax.VariableDeclaration) {
23+
let varDecl = statement as ESTree.VariableDeclaration;
24+
25+
for (let j = 0; j < varDecl.declarations.length; j++) {
26+
// This is a bit odd because our ESTree typings indicate
27+
// that a VariableDeclarator.id is a Pattern, but it
28+
// seems to actually be an Identifier, so we'll forcibly
29+
// typecast it as such.
30+
let id = varDecl.declarations[j].id as ESTree.Identifier;
31+
32+
if (id.name === "setup" || id.name === "draw") {
33+
return code;
34+
}
35+
}
36+
}
2137
}
2238

2339
return "function setup() { " + code + " }";

test/test-implicit-sketch.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,24 @@ describe("makeImplicitSketch()", () => {
1515
ensureUnchanged("#W$OTN#$ROGK$#%OKRr<T$R>");
1616
});
1717

18-
it("does nothing to code w/ existing setup()", () => {
18+
it("does nothing to code w/ setup() func decl", () => {
1919
ensureUnchanged("function setup() {}");
2020
});
2121

22-
it("does nothing to code w/ existing draw()", () => {
22+
it("does nothing to code w/ setup var decl", () => {
23+
ensureUnchanged("var setup = function() {}");
24+
ensureUnchanged("let setup = function() {}");
25+
});
26+
27+
it("does nothing to code w/ draw() func decl", () => {
2328
ensureUnchanged("function draw() {}");
2429
});
2530

31+
it("does nothing to code w/ draw var decl", () => {
32+
ensureUnchanged("var draw = function() {}");
33+
ensureUnchanged("let draw = function() {}");
34+
});
35+
2636
it("wraps code that lacks setup/draw in setup()", () => {
2737
expect(makeImplicitSketch("foo()"))
2838
.to.eql("function setup() { foo() }");

0 commit comments

Comments
 (0)