JSLint - problems importing packages - javascript

I am scripting java using rhino. I have few classes written in java that I am importing in javascript.
But, when I am validating javascript against JSLint, it invalidates javascript saying:
Problem at line 9 character 1: 'importPackage' was used before it was defined.
Here is the sample from my script:
importPackage(Packages.org.raj.test);
var test = "123";
I have selected the option: "Assume Rhino" as well but still, it shows up same error.
How should I deal with this problem?
Note that the ECMA standard doesn't cover communication with Java (or with any external object system for that matter).

I have explicitly added following line on top of my script
/*global importPackage: true */
and it works!

It looks like the "Assume Rhino" flag (aka rhino: true in the options directive) only predefines a few global variables for you, and importPackage isn't one of them. You can see the complete list in the code.
Paul's answer is basically the correct workaround – you just manually declare each extra global you use. This code passes JSLint cleanly:
/*global importPackage, Packages */
importPackage(Packages.org.raj.test);
var test = "123";

Related

How to get Visual Studio Code to recognize Java Script functions?

In NetBeans I just added a comment with global, e.g.
/* global myLibrary */
in order to get it to recognize my functions.
However, this seems not to work in VS Code. For example, if I have a function named myFunction in the myLibrary module, when I click on "Go To Definition", it tells me that there was "No definition found for myFunction".
So how do I get VS Code to recognize my function?
I believe VSCode does not provide this feature by default. You will have to define the configuration yourself if your project has mixed content in it. I use VSCode for Angular 2 (using ng-cli) so it has all the setup generated for me (i can goto definition if it is valid).
have a look at these two links, hope this helps you:
https://code.visualstudio.com/docs/languages/javascript#_automatic-type-acquisition
https://code.visualstudio.com/docs/languages/jsconfig#_what-is-jsconfigjson

qx.log.appender Syntax

When declaring qx.log.appender.Native or qx.log.appender.Console, my IDE (PyCharm) complains about the syntax:
// Enable logging in debug variant
if (qx.core.Environment.get("qx.debug"))
{
qx.log.appender.Native;
qx.log.appender.Console;
}
(as documented here)
The warning I get is
Expression statement is not assignment or call
Is this preprocessor magic or a feature of JavaScript syntax I'm not aware yet?
Clarification as my question is ambiguous:
I know that this is perfectly fine JavaScript syntax. From the comments I conclude that here's no magic JS behavior that causes the log appenders to be attached, but rather some preprocessor feature?!
But how does this work? Is it an hardcoded handling or is this syntax available for all classes which follow a specific convention?
The hints how to turn off linter warnings are useful, but I rather wanted to know how this "magic" works
Although what's there by default is legal code, I find it to be somewhat ugly since it's a "useless statement" (result is ignored), aside from the fact that my editor complains about it too. In my code I always change it to something like this:
var appender;
appender = qx.log.appender.Native;
appender = qx.log.appender.Console;
Derrell
The generator reads your code to determine what classes are required by your application, so that it can produce an optimised application with only the minimum classes.
Those two lines are valid Javascript syntax, and exist in order to create a reference to the two classes so that the generator knows to include them - without them, you wouldn't have any logging in your application.
Another way to create the references is to use the #use compiler hint in a class comment, eg:
/**
* #use(qx.log.appender.Native)
* #use(qx.log.appender.Console)
*/
qx.Class.define("mypackage.Application", {
extend: qx.application.Standalone,
members: {
main: function() {
this.base(arguments);
this.debug("Hello world");
}
}
});
This works just as well and there is no unusual syntax - however, in this version your app will always refer to the those log appenders, whereas in the skeleton you are using the references to qx.log.appender.Native/Console were surrounded by if (qx.core.Environment.get("qx.debug")) {...} which means that in the non-debug, ./generate.py build version of your app the log appenders would normally be excluded.
Whether you think this is a good thing or not is up to you - personally, these days I ship all applications with the log appenders enabled and working so that if someone has a problem I can look at the logs (you can write your own appender that sends the logs to the server, or just remote control the user's computer)
EDIT: One other detail is that when a class is created, it can have a defer function that does extra initialisation - in this case, the generator detects qx.log.appender.Console is needed so it makes sure the class is loaded; the class' defer method then adds itself as an appender to the Qooxdoo logging system
This is a valid JS syntax, so most likely it's linter's/preprocessor's warning (looks like something similar to ESLint's no-unused-expressions).
Edit:
For the other part of the question - this syntax most likely uses getters or (rather unlikely as it is a new feature) Proxies. MDN provides simple examples of how this works under the hood.
Btw: there is no such thing as "native" JS preprocessor. There are compilers like Babel or TypeScript's compiler, but they are separate projects, not related to the vanilla JavaScript.

Is JSHint a Node.js syntax validator?

I have come across JSHint but was hoping it would validate node.js syntax but it doesn't, for example if I do:
var server = http.createServerfunctionThatDoesNotExists(function(request, response) {....
The test passes even though there isn't a function called createServerfunctionThatDoesNotExists
What am I missing with JSHint and the node.js option?
I think it doesn't dig into CommonJS modules as they can contain virtually everything.
So even if you are require'ing http it could be your own module with any interface.
'Assume Node' means it tolerates global variables like module or exports
JSHint is basically JSLint and is more for syntax/style/JS-nonos rather than a deep-scan product that executes your JS. Fully vetting JS code (or any dynamic language, really, depending on your definition of dynamic) pretty much requires running it, since things like methods can be defined more or less anywhere, including at runtime.
JSHint does go a bit further and allow the definition of high-level constructs used by some JS libraries.

Require() function in JavaScript

When I open console of Chrome 14 and type...
require (or require(), if that matters)
I get: ReferenceError.
This means that JavaScript doesn't have that function by default, right? At least on web browsers.
Why I'm talking about that?
I needed Markdown parser for JavaScript.
What to do?
I, as usually, opened GitHub and searched for it. The first results that matched my needs was this and this.
Usually (I'm not that good with JavaScript) I include script I want to use before my code using <script /> tag and then... well - use it. But this time I don't get what's happening... :(
Usage for #1 script:
var input = "# Heading\n\nParagraph";
var output = require( "markdown" ).toHTML( input );
print( output );
Usage for #2 script:
var marked = require('marked');
console.log(marked('i am using __markdown__.'));
Where does that require() came from? Thanks in an advice! :)
It's a way to include node.js packages. Luckily, the first package you linked to, markdown-js, is very smart. It checks whether it is included as a node package, and if not, will set the markdown object to window.markdown. So all you have to do is include this file in a <script> tag and you should be able to use the markdown object from the global scope.
From the page you link to:
The simple way to use it with CommonJS is:
Looks like require comes from CommonJS

JavaScript-friendly alternative to the f(x) = y JScript idiom that's used when setting CDO.Message options

I have an ASP page written in JScript that sends e-mails using CDO.Message. For specifying an SMTP server (and other options) I'm doing something like this:
mail.Configuration.Fields.Item(
"http://schemas.microsoft.com/cdo/configuration/smtpserver") =
"smtp.example.com";
Now, here comes the catch. I have this code in a stand-alone include file that I include in an HTML page as JavaScript so that I can run unit tests against it in a browser (using JsUnit etc.). I have JavaScript mock objects (Server, Request, etc.) that create a mock ASP environment for the included JScript code. The only problem I have left is with the CDO.Message option setting. Since the f(x) = y syntax that's used in the above code excerpt is not valid JavaScript (invalid left-hand operand), I can't run this piece of code (as it is) within a browser. I'm currently simply bypassing it in my unit test with a conditional that detects whether the environment is truly ASP.
I don't think that there's a JavaScript workaround to this. I'm looking for an alternative syntax (that may use the ActiveX interfaces differently) to setting CDO.Message options that would also be syntactically valid JavaScript.
I figured out the answer when looking at the C++ code example at http://msdn.microsoft.com/en-us/library/ms526318(EXCHG.10).aspx.
The solution is to make the assignment explicitly to the Value property:
mail.Configuration.Fields.Item(
"http://schemas.microsoft.com/cdo/configuration/smtpserver").Value =
"smtp.example.com";
This way, the code above is valid JavaScript than can be tested with a mock Configuration object.
I've been having the same problem when writing server-side Javascript for IIS, That f(x) = y syntax was failing in my IDE's syntax checker. The solution I found helpful was JScript conditional comments like so:
f(x)/*#cc_on#if(0)*/[0]/*#end#*/ = y;
It puts the subscript index [0] on the end except when running in Microsoft's JScript engine. But, admittedly my solution is a bit hacky. I think in most cases yours is cleaner, so thanks for sharing it.
-Simon

Categories

Resources