Page 1 of 1

Remove unreachable code

Posted: Wed Jan 31, 2018 3:47 pm
by Dirk Geppert
I prefer to integrate the CGI Server code into the Spidebasic project and use Peter's SpiderBite.
In the compiled Javascript occurs double return calls at the end of functions and the browser always displays a lot of error messages due to "unreachable code".

With the following PureBasic code snippet you can eliminate them afterwards.
It only works without "[ ] Optimized Javascript Output".

Code: Select all
EnableExplicit

Global SourceFile.s

#AppName = "RemoveUnreachableCode"
#AppVersion = "2018-01-26"

Procedure Main()
  Protected NewList l.s()
  Protected t.s
  Protected ff
  Protected flag
  Protected Count
 
  ; After Create App
  SourceFile.s = ProgramParameter() ; %COMPILEFILE!
  SourceFile = GetPathPart(SourceFile) + "spiderbasic.js"
 
  SetCurrentDirectory( GetPathPart(SourceFile))
  SourceFile = OpenFileRequester(#AppName, GetFilePart(SourceFile), "JavaScript (*.js)|*.js;|Alle Dateien (*.*)|*.*", 0)
 
  If SourceFile = ""
    End
  EndIf
 
 
  If OpenFile(0, SourceFile)
    ff = ReadStringFormat(0)
    While Not Eof(0)
    t = ReadString(0, ff)
   
    ; return "";
   
    ; Debug t
   
    If Mid(Trim(t), 1, 6) = "return"
      If flag = 0
        flag = 1
      Else
        t = ""
        Count + 1
      EndIf
    Else
      flag = 0
    EndIf
   
    If t <> ""
      AddElement(l())
      l() = t
    EndIf 
   
    Wend
    CloseFile(0)
  EndIf
 
  If CreateFile(0, SourceFile, ff)
    ForEach l()
      WriteStringN(0, l(), ff)
    Next
    CloseFile(0)
  EndIf
 
  If Count > 0
    MessageRequester( #AppName, Str(Count) + " lines of code removed.")
  EndIf
EndProcedure

Main()