Class: FrontCompiler::JavaScript

Inherits:
SourceCode
  • Object
show all
Includes:
LogicCompactor, NamesCompactor, SelfBuilder
Defined in:
lib/front_compiler/java_script.rb,
lib/front_compiler/java_script/self_builder.rb,
lib/front_compiler/java_script/logic_compactor.rb,
lib/front_compiler/java_script/names_compactor.rb

Defined Under Namespace

Modules: LogicCompactor, NamesCompactor, SelfBuilder

Constant Summary

Constants included from SelfBuilder

SelfBuilder::MAXIMUM_DICTIONARY_SIZE, SelfBuilder::MINUMUM_REPLACEABLE_TOKEN_SIZE

Instance Method Summary collapse

Methods included from LogicCompactor

#compact_logic!

Methods included from NamesCompactor

#compact_names!

Methods included from SelfBuilder

#create_self_build

Methods inherited from SourceCode

#compact, #initialize

Constructor Details

This class inherits a constructor from FrontCompiler::SourceCode

Instance Method Details

#check_semicolons_in(src) ⇒ Object

checks if there’s ommited semicolons in the code



116
117
118
119
120
121
122
123
# File 'lib/front_compiler/java_script/logic_compactor.rb', line 116

def check_semicolons_in(src)
  src[/\s*\Z/m] = ";#{src[/\s*\Z/m]}" unless src.strip[-1,1].match(/[;\}]/)
  
  # removing bug causing semicolons after a function
  src.gsub!(/(\A\s*function\s*.*?\(.*?\)\s*\{.*?\});(\s*\Z)/im) do
    $1 + $2
  end
end

#compact!Object



13
14
15
16
17
18
19
20
21
# File 'lib/front_compiler/java_script.rb', line 13

def compact!
  string_safely do 
    remove_comments!.
      compact_logic!.
      compact_names!.
      remove_empty_lines!.
      remove_trailing_spaces!
  end
end

#fix_missed_semicolons!Object



125
126
127
128
129
130
131
132
133
# File 'lib/front_compiler/java_script/logic_compactor.rb', line 125

def fix_missed_semicolons!
  gsub! /([^{}()|\[&;:=,\s])([ \t]*?\n\s*?(return|if|function|while|try|do)[^a-zA-Z_$])/, '\1;\2'
  
  # (function(){....})(..) if(...)
  gsub! /(\}\)\([^)]*\))([ \t]*?\n\s*?(return|if|function|while|try|do)[^a-zA-Z_$])/, '\1;\2'
  
  # fixing wrong added semicolons before the if calls
  gsub! /([^a-zA-Z_$](else|do));(\s+(if|while|do|try)[^a-zA-Z_$])/, '\1\3'
end

#remove_comments!Object



23
24
25
26
27
28
# File 'lib/front_compiler/java_script.rb', line 23

def remove_comments!
  string_safely do 
    gsub!(/\/\*.*?\*\//im, '')
    gsub!(/\/\/.*?($)/, '\1')
  end
end

#remove_empty_lines!Object



30
31
32
33
34
# File 'lib/front_compiler/java_script.rb', line 30

def remove_empty_lines!
  string_safely do 
    gsub!(/\n\s*\n/m, "\n")
  end
end

#remove_trailing_spaces!Object



36
37
38
39
40
41
42
43
44
# File 'lib/front_compiler/java_script.rb', line 36

def remove_trailing_spaces!
  string_safely do 
    gsub!(/\s+/im, ' ') # cutting down all spaces to the minimum
    gsub!(/\s*(=|\+|\-|<|>|\?|\|\||&&|\!|\{|\}|,|\)|\(|;|\]|\[|:|\*|\/)\s*/im, '\1')
    gsub!(/;(\})/, '\1') # removing unnecessary semicolons
    gsub!(/([^a-z\d_\$]typeof)\(([^\)]+)\)\s*/im, '\1 \2') # converting the typeof calls
    strip!
  end
end