Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make #visit method work for null and undefined
- Loading branch information
1 parent
cbebdaf
commit 78474bc
Showing
2 changed files
with
25 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,24 @@ | ||
Monarch.Util.Visitor = | ||
visit: (object, args...) -> | ||
throw new Error("Cannot visit #{object}") unless object | ||
if object.acceptVisitor? | ||
object.acceptVisitor(this, args...) | ||
if object?.acceptVisitor? | ||
object.acceptVisitor(this, args) | ||
else | ||
name = object.constructor.name | ||
method = this['visit_' + name] | ||
throw new Error("Cannot visit #{name}") unless method | ||
method.apply(this, arguments) | ||
visitPrimitive.apply(this, arguments) | ||
|
||
for moduleName in ["Expressions", "Relations"] | ||
_.each Monarch[moduleName], (klass, klassName) -> | ||
methodName = "visit_#{moduleName}_#{klassName}" | ||
klass.prototype.acceptVisitor = (visitor, args...) -> | ||
klass.prototype.acceptVisitor = (visitor, args) -> | ||
visitor[methodName](this, args...) | ||
|
||
visitPrimitive = (object) -> | ||
name = visiteeName(object) | ||
method = this['visit_' + name] | ||
throw new Error("Cannot visit #{name}") unless method | ||
method.apply(this, arguments) | ||
|
||
visiteeName = (object) -> | ||
switch object | ||
when null then "null" | ||
when undefined then "undefined" | ||
else object.constructor.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters