Static code analysis

Intro

A few days ago we merged a little framework for describing and jobs and assingments, and there were already a few mistakes found, created either by typos or easily detectable by a static code analysis. For example, VS Code highlighted them to me. And that would be nice if only VS Code does not highlight a zillion of other things it believes to be mistakes. Most of them (maybe even all of them) comes from the fact that we declare global functons in the special way to overcome SugarCube (SC2) script isolation trick (eval() inside a closure). I proposed to overcome that limitation with introducing the global App object, that is exported from the SC2 to the browser, but this solution is far from ideal. For example, it's convinient to have global functions for Twee passages, and no one want to move everything inside the App namespace.

Problem

Can we write regular JavaScript where var and function do what they are supposed to do in JavaScript and escape the SC2 limitations?

Possible solutions

  1. Tweego joins all the .js files and put them inside <script role="script" id="twine-user-script" type="text/twine-javascript"> tag. If we modify our build script to make that a regular <script> tag, out JS will become "normal". However, Tweego puts the format (SC2) code last in the output file (so when SC2 executes, all the rest of the scripts are loaded) and thus we can't refer to SC2 objects in initialisation code. That means we would need to put all Macro.add and Config. calls in passages (and the accordion code too).

  2. Use a more complex build system to create a separate .js script and load it from the file. Example: https://github.com/ChapelR/tweego-setup

  3. A simpler variation of the previous one: split contents of the src/ dir in two subdirs, one with the Twee code, the other one with the pure js (which does not require SC2 at run-time). When we use a slight modification of the script we have for collecting the .js code for Twine1, and collect contents of the src/js subdir into a file in the bin dir (suppose it is bin/fc.js). A simple sed command (sed -i 's/<\/body>/<script src="fc.js"><\/script><\/body>/' FC_pregmod.html) will insert a <script src="fc.js"></script> into the resulting file before moving it in place.

Pros

  1. We can replace all the ugly window.XXX = function with simple function declaration. Code analysers are happy and provide us with nice checks and help a lot.
  2. Solution 2 would allow to use JS minifier.

Cons

  1. In this new JS code we shall use SC2 only via the SugarCube object, as we do in the browser console. Not all SC2 objects are available there.
  2. Solution 3 requires a lot of file moves.

I would greatly appreciate your responses.