Google closure compiler error "Variable COMPILED first declared in {SyntheticVarsDeclar}" - javascript

When using the Google closure compiler to try and compile a load of closure dependencies taken from their editor demo (no external code) using calcdeps.py I get the following error when then running the compiler on the produced code (which runs fine):
{SyntheticVarsDeclar}: ERROR - Variable COMPILED first declared in {SyntheticVarsDeclar}
The variable COMPILED is only used in 2 places within the file that calculated dependencies produces and isn't declared anywhere in there. The only place I see it declared is in base.js.

This used to be a bug in the closure compiler. Apparently it has already been fixed. You should download a current version of the closure compiler.
There is a closed bug report for this issue in the closure-compiler issue tracker.

Try declaring it at the start and use this tag like this:
/**
* #define {boolean} Overridden to true by the compiler when --closure_pass
* or --mark_as_compiled is specified.
*/
var COMPILED = false;
I'm almost sure that it will fix the problem, in fact I even think that you don't need the tag. Also try downloading latest compiler or compile it from svn because there was suspiciously similar bug reported earlier and it got fixed.

Related

VSCode debugger not stopping on Typescript breakpoints

I have a Lerna monorepo with 2 modules(packages): ps and cli.
ps just exports a function whatever which cli imports.
When I try to debug this code using VSCode, however, my breakpoints stop on the generated Javascript files instead of my source Typescript files.
This has bothered me for hours and I have extensively played around with my tsconfig.json and launch.json as well as using vscode-pwa-analyzer to see that VSCode is able to detect my source TS code but I cannot figure out a fix.
EDIT: I am using the following setup:
macOS Big Sur v11.1
Node.js v14.8.0
Typescript v4.1.3
And here is a dump file of my above debug you can load to vscode-pwa-analyzer.
I can see here that I get some Unbound Breakpoint errors.
I replicated the problem, but I would say that this is the expected behavior.
Actually setting a breakpoint on a function declaration makes no sense.
If you change your whatever function as a sync function (i.e. function whatever(): void) you will see that VSCode doesn't let you to break on that line.
In the special case of an async function we know that the function body is actually wrapped in a Promise. Probably in this special case the typescript debugger let us to set a breakpoint on a function declaration line to let us check what happens outside the Promise wrap.

Closure Compiler with Advanced Optimizations and Externs

I have downloaded the latest closure compiler jar file and followed the instructions here to see how I can get it to preserve references to JS functions defined elsewhere.
My test script, hello.js is as follows
function hello(nom)
{
alert(nom + familyName());
}
My externs are defined in the file externs.js which contains the code
function familyName(){}
I then issue the command
java -jar closure.jar --js hello.js --externs externs.js --js_output_file hello-compiled.js --compilation_level ADVANCED_OPTIMIZATIONS
Without ADVANED_OPTIMIZATIONS everything works correctly - effectively a spot of code minification. However, as soon as I put in the advanced flag the output hello_compiled.js comes out as an empty 1 byte file. Clearly, I am misunderstanding something somewhere. I'd be much obliged to anyone who might be able to put me on the right track here.
I suspect your hello.js only defines the hello function? If so, you need to add:
hello("foo");
so that something actually happens. You can try this out with the online closure compiler. The default code there is:
function hello(name) {
alert('Hello, ' + name);
}
hello('New user');
If you comment out the last line and click the "advanced" button and compile, the result is successful but it is zero bytes. This is because that code effectively does nothing, so the compiler is doing the right thing.

PhpStorm: Unresolved variables, methods & functions for NodeJS modules

I just started a new NodeJS application in PhpStorm which uses some external packages. Unfortunately I see a lot of Unresolved variable or Unresolved function or method warnings:
Obviously this is very distracting. Can I somehow manually tell the IDE to load some definition files of the external packages I use (e.g. mongoose in this example)?
If I find no other solution I'd simply disable those inspections which is kind of a bad "fallback" since they usually provide some good hints for typos etc.
Solution:
I had 2 different node_modules directories in my project:
project\client\node_modules\...
project\server\node_modules\...
Both were marked as excluded. I simply removed the excluded mark (right-click on the folder in the project tree). PhpStorm shows correct auto completion now and the Unresolved warnings are gone.
If you have jetbrains suite, It's better to use webstorm for nodejs application.
However, if you want to keep phpstorm. You can use a jshint module for global variables declaration, and use jsdoc for variable instance :
/**
* Description
* #type {Schema}
* #property {object} methods
*/
var Schema;
-- EDIT --
Have you set up your phpstorm as the jetbrains doc ?
Work on nodejs with phpstorm

Plovr externs don't load when using test-template option

I am using plovr to run unit tests for my JavaScript code that uses Google Closure Library. My setup was working fine until I needed to run some asynchronous tests. I found out from this discussion that I needed to replace the default org.plovr.test.soy file by using plovr's test-template option.
I updated the test.soy file so I could use goog.testing.ContinuationTestCase:
<body>
<script src="{$baseJsUrl}"></script>
//This script tag was added.
<script>
goog.require('goog.testing.jsunit');
goog.require('goog.testing.ContinuationTestCase');
</script>
<script src="{$testJsUrl}"></script>
</body>
Now my unit tests using ContinuationTestCase work, but an a different unexpected problem has popped up. My externs are not being loaded! See my plovr configuration below:
{
"id": "peders-app",
"inputs": "src/peder/application.js",
"paths": "src/peder",
"output-file": "compiled.js",
"externs": "src/peder/kinetic-externs.js",
"test-template":"test/org.plovr.test.soy"
}
When I didn't have test-template in my config, my externs loaded fine, but my unit tests using ContinuationTestCase fail.
When I add the test-template option, my unit tests using ContinuationTestCase pass, but many other tests fail since the externs are not loaded.
Below are the error that make me believe the externs aren't working:
12:25:39.398 Start
12:25:39.400 testAddLine : PASSED
12:25:39.402 testCreateUndoCommand : PASSED
12:25:39.403 testUpdateLocalData : PASSED
12:25:39.404 testUpdateServerData : PASSED
12:25:39.404 Done
JS ERROR: Uncaught ReferenceError: Kinetic is not defined
URL: http://localhost:9810/input/peders-app/src/peder/smartPoint.js
Line: 143
JS ERROR: Uncaught TypeError: Cannot read property 'prototype' of undefined
URL: http://localhost:9810/input/peders-app/src/peder/modifyLine.js
Line: 24
JS ERROR: Uncaught ReferenceError: Kinetic is not defined
URL: http://localhost:9810/input/peders-app/src/peder/shapes/line.js
Line: 154
Does anyone know why using a custom test-template would stop my externs from being loaded?
It turns out that the externs were never being loaded when running tests for some reason. The errors from where functions from the externs were being called just weren't causing the tests to fail. I still have no clue why using the test-template option made the errors fail the tests, but luckily I found a workaround.
I simply added my externs directly into the plovr jar. To do this just add your extern into the externs folder inside plovr.jar. Then update externs_manifest.txt at the top level of the plovr jar to include whatever files you added. This will make plovr treat your extern as a default extern.
This probably isn't the "right" way to fix this problem, but it gets the job done.

Webstorm 7 cannot recognize node API methods

I just installed WebStorm. I'm working on a small Node.js app.
I've attached the Node.js source code, and when I click on the Node.js settings, I can see that it can recognize my various node modules, etc.
I'm having two issues:
Unresolved variable or type: WebStorm doesn't seem to recognize simple API methods (exports, require).
No code insight for…: If I call require('winston'), it tells me that it has no code insight. (Is there a way I can add the source code?)
For 2018 and later versions of WebStorm:
In Settings -> Languages & Frameworks -> Node.js and NPM, check Coding assistance for Node.js:
In older Webstorm versions, this was called Enable Node.js Core library.
If you still see unrecognized Node symbols even with that option enabled, unckeck it, restart WebStorm, then right click on the warning and choose Enable Node.js coding assistance or just check the option again. Watch for WebStorm to show it's Indexing files. (Just had this happen today - looks like a WebStorm bug, and what I just wrote fixed the situation.)
For WebStorm 7 thru 10 (on OSX)…
WebStorm → Preferences → Languages & Frameworks → Javascript → Libraries
Select "Node.js Globals" and "Node.js vXXX Core Modules".
I use WebStorm 2020 and I had everything enabled but WebStill though showed that module.exports is unknown function. Then I turned off NodeJS.core library and NodeJS code assistance, applied and then turned them on again. And suddenly it started to work.
As I've answered on the WebStorm says console is an unresolved variable question, to solve these problems on the new Webstorm versions, you need to enable the Coding assistance for Node.js.
To do this, go on the Settings > Languages & Frameworks > Node.js and NPM and click on the Coding assistance for Node.js option, and then click OK to save:
This will all Node.js unresolved variables and functions.
Update
On the new Webstorm versions, just going above error and clicking in More Actions... (or ALT+ENTER) and selecting
Enable Node.js coding assistance will solve this.
Working with WebStorm and Node.js these two code-segments regularly gave me "false positive" warnings:
FALSE POSITIVE WARNING CASE 1:
module.exports = ...
That gave me the warning "Element is not exported". I was able to get rid of the warning caused by that by putting this above the "module.exports = ..."
/** #namespace module.exports **/
FALSE POSITIVE WARNING CASE 2:
let something = global.something ;
That gave me the warning "Unresolved variable or type global". I was able to eliminate the warning caused by that by putting this above it:
/** #namespace global **/
I now put the following on top of my .js- or .mjs-files to be executed by Node.js, and get rid of warnings referring to these variables or properties:
/** #namespace global **/
/** #namespace console **/
/** #namespace process **/
/** #namespace Buffer **/
/** #namespace process.stdin **/
/** #namespace module.exports **/
I am using WebStorm 2022.2.3
Updating to Webstorm 8 or higher will fix your require methods problem. As posted earlier by checking if Settings > JavaScript > Libraries > Node.js are all checked will fix your problem

Categories