Why does Google Closure Compiler NOT rename these external variables? - minify

According to documentation (https://developers.google.com/closure/compiler/docs/api-tutorial3#externs), it seems the closure compiler should rename variables when no external declaration exists, including when using functions/variables from an external bit of code. The example they give is
function makeNoteDom(noteTitle, noteContent, noteContainer) {
// Create DOM structure to represent the note.
var headerElement = textDiv(noteTitle);
var contentElement = textDiv(noteContent);
...
}
where the textDiv function is declared in the global scope by a third-party lib of some sort. It says textDiv should be declared external to prevent renaming.
My question is - when I put this code or similar into the Closure Compiler without any extern declarations why is textDiv not renamed (which would break the code), as the documentation indicates?

The compiler assumes that calls to an undefined function are in fact calls to an external functions. Using the command line compiler, you can use --warning_level VERBOSE to have the compiler treat this condition as an error.
The Web Application is primarily built for demos and assumes this by default. While you can set a VERBOSE warning level, it will not change this functionality. See the Additional Web Service Options page for information on options. I've filed a bug report about this.
Due to the renaming algorithm for properties, undeclared properties will be renamed in a breaking way if that same property name isn't declared on an object in externs.

Related

Can you pass arguments to functions in Slate?

In Foundry's Slate application, is there a clean way to write functions that accept arguments as input using the handlebar syntax?
Instead of function arguments, inputs to a Slate function are defined by a Handlebar reference inside the Function itself; for example to access data from a query from inside a Function, you might write:
const data = {{q_myQuery}}
Defining dependencies in this way allows Slate to automatically recompute the Function outputs whenever the values of upstream dependencies change. In this way, you never "call" a function, but rather some other element in Slate references the Function output and that output is updated whenever the inputs change.
If you want to do some kind of code reuse you can use functionLibraries to write common code that you can re-use between Functions. These are standard javascript functions that are included in the global javascript scope and can be referenced simply by the function name from any Function and take function parameters using normal javascript syntax. Since these are vanilla javascript you cannot use Handlebars inside a functionLibrary - here any input must be passed in as a parameter from the parent Function.
From the documentation (Slate > Concepts > Functions):
Per-Document level function libraries
Users are able to write reusable javascript functions with parameters. This will assist in the refactoring of code and reducing the copying and pasting of code in functions. You can also re-run and update all the functions dependent on a function library using the Re-run All Function button.
Default JavaScript libraries available
For enhanced use of functions, Slate ships by default (as of Slate 2.15) with the following external JavaScript libraries: Lodash, Math.js, Moment, Numeral and es6-shim. Feel free to use these libraries when writing your functions.Do not use ES6 syntax features unless all users are mandated to use a browser supporting these features.

How do I use JSDoc type inference in WebStorm on a module with a dot in it's name? (Discord.js)

I'm trying to find a way to work with Discord.js TypeScript annotations using WebStorm's JSDoc type inference in a non-conflicting pattern.
Here's what "works"...
import Discord from 'discord.js';
/**
* #param {Client} client
* #param {Collection.<Snowflake, Message>} msgs
*/
function handleRemoveMessages(client, msgs) {}
The problem is with Client, multiple definitions exist.
While this works, I'd like to be able to namespace the imported types from the discord.js module into something like Discord.Client instead of Client, as I'd like to use other symbols named such.
So, for example... Message is referencing the correct type;
And Discord.Message appears to be referencing the correct type (but I don't believe it is);
The two Message type references are not compatible, and throw a warning when one is supplied to represent the other.
It's also not possible to reference the type via a JSDoc module path...
I also tried module:discord\.js, module:'discord\.js' and also with double quotes, all with the same results.
I know that using a dot in a JSDoc path name is a bit of a hack, and that the Closure compiler used by JetBrains may not be 100% up on JSDoc compliance.
Is this just a bug in WebStorm or Closure or is there a way to correctly make this module type reference?
according to JSDoc 3 spec, you need to quote the names with 'unusual' chanracters, like module:"discord.js". But it doesn't currently work as expected, please follow WEB-34348 for updates

ClojureScript Eval. How to use libraries included in the calling code

I have a Clojurescript program running in the browser.
It imports a number of libraries, and then I want to allow the user to enter some small clojurescript "glue-code" that calls those libraries.
I can see (from https://cljs.github.io/api/cljs.js/eval) that you call eval with four arguments, the first being the state of the environment, which is an atom. But can I actually turn my current environment with all the functions I've required from elsewhere, into an appropriate argument to eval?
Update :
I thought that maybe I could set the namesspace for the eval using the :ns option of the third, opts-map, argument. I set it to the namespace of my application :
:ns "fig-pat.core"
But no difference.
Looking at the console, it's definitely the case that it's trying to do the evaluation, but it's complaining that names referenced in the eval-ed code are NOT recognised :
WARNING: Use of undeclared Var /square
for example. (square is a function I'm requiring. It's visible in the application itself ie. the fig-pat.core namespace)
I then get :
SyntaxError: expected expression, got '.'[Learn More]
Which I'm assuming this the failure of eval-ed expression as a whole.
Update 2 :
I'm guessing this problem might actually be related to : How can I get the Clojurescript namespace I am in from within a clojurescript program?
(println *ns*)
is just printing nil. So maybe Clojurescript can't see its own namespace.
And therefore the :ns in eval doesn't work?
Calling eval inside a clojurescript program is part of what is called "self-hosted clojurescript".
In self-hosted clojurescript, namespaces are not available unless you implement a resolve policy. It means that have to let the browser know how to resolve the namespace e.g. loads a cljs file from a cdn.
It's not so trivial to implement namespace resolving properly.
This is explained in a cryptic way in the docstring of load-fn from cljs.js namespace.
Several tools support namespaces resolving in self-host cljs running in the browser e.g Klipse and crepl

F# Guid.Parse TypeInitializationException

I'm working on a WebApi project written in F#. Here a snippet:
module MyModule
open System
let MyGuid = Guid.Parse "934F0B12-D00A-491D-862D-EE745EF3C560"
let myFunction list =
list.Get(MyGuid) // --> here MyGuid has the TypeInitializationException before list.Get is called
By debugging I can see that the MyGuid actually has an error
Changing the code followings, it works:
module MyModule
open System
let MyGuid () = Guid.Parse "934F0B12-D00A-491D-862D-EE745EF3C560"
let myFunction list =
list.Get(MyGuid())
I actually know the MyGuid of the first example is a variable and the second one a function definition, but why does the first rise the exception? I my code MyGuid is used some times. so in the first example I'd have only one instance, in the second a new instance every time MyGuid is called...
I'm not 100% sure that this is the problem here, but I've seen similar behaviour when using unit test runners sometimes. My guess is that the error happens because the top-level MyGuid variable is not initialized correctly and has the default zero value (and as a result, the lookup fails).
The way global variables are initialized in F# is tricky - if you compile code as executable, this can happen from the Main method. But if you compile code as a library, the compiler inserts an initialization checks into static constructors of the types in your library (to make sure everything is initialized before you access anything).
I think this can break if you compile your code as an executable, but then load it as a library - the entry-point is not called and so the variables are not initialized. I'm not sure how exactly WebApi loads libraries, but this could be a problem - especially if you compile the F# code as an executable.
Your workaround of turning the global variable into a function fixes this, because the function is compiled as a method and so you avoid referring to an uninitialized value. Sadly, I don't think there is a better workaround for this.

Inlining functions in AS3

I'm looking for a way to inline functions in AS3.
I know that the language itself doesn't offer a native way of doing that but perhaps there is another option:
ANT precompile task
shell script
command line tool
...
Basically, anything that could eventually be integrated with ANT and run on a Hudson CI server.
You can use Joa Ebert Apparat tools to achieve such a thing and more.
You can't inline whatever function you want they are some restrictions
Basically you have to create a new class that extends Macro or Inlined following your need, and declare static function within it, then after running TDSI your function will be inlined.
Check out for example Math inlined function or Macro function
Adobe introduced native inline functions with the new ASC2 compiler in 2012. Use the -inline compiler argument to inline all getters and setters and any functions marked with the new [Inline] metadata. Inlined functions must meet these conditions:
The function is final, static or the containing scope is file or package
The function does not contain any activations
The function does not contain any try or with statements
The function does not contain any function closures
The function body contains less than 50 expressions
http://www.bytearray.org/?p=4789