Class: Lanes::API::CoffeeScriptWrapper

Inherits:
JsAssetCompiler show all
Defined in:
lib/lanes/api/javascript_processor.rb

Constant Summary collapse

CONSTRUCTOR =
/constructor\s*:/
EXTENDING_CLASS =
/class\s+([\w|\.]+)\s+extends\s+([\w|\.]+)\s*?\n/

Instance Method Summary collapse

Methods inherited from JsAssetCompiler

#prepare, register, #wrap_js

Instance Method Details

#cleanedObject

Coffeescript has two shortcomings with regards to Lanes

The first is that it’s extends format is incompatible with AmpersandState, State does quite a bit more via it’s own .extend methods. Accordingly, we substitute our own “extend” call whenever we encounter a coffeescript extends

The second issue is that if a constructor isn’t present, coffeescript with generate it’s own blank implementation that fails to call “super”. We add a constructor that does call super if one’s missing.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/lanes/api/javascript_processor.rb', line 58

def cleaned
    contents = data.dup
    data.scan(EXTENDING_CLASS) do |match|
        (name,extends) = match

        contents.gsub!(/class\s+#{name}\s+.*?\n/,"class #{name}\n")
        definition = contents[/(class #{name}\n.*?)(\n\w|\Z)/m,1]
        # is it missing a constructor?
        if definition !~ /constructor:/
            # figure out how much to indent, sigh.
            indent = definition[/\n(\s+)(\w+):/,1] || '    '
            contents.gsub!(/class #{name}\n/,"\\0#{indent}constructor: -> super\n")

        end
        contents.gsub!( /(class #{name}\n.*?)(\n\w|\Z)/m, "\\1\n#{extends}.extend(#{name})\n\\2" )

        contents
        #contents << "#{extends}.extend(#{name})\n"
    end
    contents
end

#evaluate(scope, locals, &block) ⇒ Object



80
81
82
# File 'lib/lanes/api/javascript_processor.rb', line 80

def evaluate(scope, locals, &block)
    wrap_js scope, ::CoffeeScript.compile(cleaned, bare: true)
end