Why does Eclipse complain about missing semicolon in my javascript? - javascript

I'm having an issue with Eclipse where it's complaining about missing semicolons in javascript code in a jsp file. Given the two lines below, Eclipse complains about the first line and indicates that there's a missing semicolon right before the closing curly brace. It has no complaints about the second line. I'd prefer to use the first way, but I'm annoyed with the warnings. Is there a different syntax that I should be using? I'm also using JQuery, so I don't know if this is contributing to the parsing error in Eclipse.
var isFoo = ${actionBean.isFoo}; // javascript type is boolean
var isFoo = '${actionBean.isFoo}'; // javascript type is string
To answer some of the questions people have posed in the comments...
This code is from a JSP file and actionBean refers to the Java action bean for this page.
"isFoo" is a member variable of the action bean. The syntax ${actionBean.isFoo} is JSP Expression Language (or EL for short) and it's used to evaluate a java variable in a JSP. In this case, my code takes the value from the java variable and assigns it to the javascript variable. The code works just fine, but Eclipse complains.

var isFoo = ${actionBean.isFoo};
This is not valid JavaScript, which is why you're getting a syntax error. I'm assuming the ${...} is supposed to do some interpolation in NetBeans, but Eclipse has no way of knowing that, and just tries to parse it as JavaScript. Unless there's an Eclipse plugin or setting for dealing with mixed code like this, you may just have to deal with seeing syntax errors.

Eclipse is complaining because it is not really Javascript, and it is trying to parse it like Javascript. If it is really annoying you could disable the warning in the preferences, under your own risk to not be notified in other circumstances:

var isFoo = ${actionBean.isFoo};
is not javascript, it is JSF

Related

How to access flow variables from javascript in power automate

I'm working on a power automate flow and I'm having some trouble. I copied some text to my clipboard in the flow and i asigned it to a variable in my flow. I'm trying to parse the variable using the javascript scripting feature but i'm getting a syntax error whenever I try to use the %variable% syntax in javascript. I used the variable button in the text editor they give you to add the variable to my script but even something simple as var res = %SimResults%; returns an error C:\Users\$me\AppData\Local\Temp\Robin\nkwrgcdoxrp.tmp(1, 11) Microsoft JScript compilation error: Syntax error
It seems that even though I Should be able to access my flow variables from Javascript it throws a syntax error when I try
You'll need to put quotes around it ...
var res = "%SimResults%"
... it treats the variables as more of a find and replace within your Javascript code, therefore, you need to do the work to ensure the correct encapsulation, etc. exists.
This may not solve your problem though depending on the complexity of what is contained within the variable.
If you have additional quotes, etc. you may need to escape them prior to putting them in the Javascript task IF they exist within the string ... just be mindful of that.

What's the best way to handle Eclipse errors on CouchDB Map/Reduce JavaScript functions?

As noted in Where to write and store mongoDB map/reduce functions in java project - Eclipse doesn't like a JavaScript (.js) file to only contain the following:
function(doc) {
if(doc.somekey) emit(doc._id, doc);
}
The keyword function is marked with an error:
Syntax error on token "function", Identifier expected after this token
This form is used in MapReduce, but perhaps it's not exactly valid JavaScript (I'm not a lawyer). Is there any way to allow this form?
edit it looks like it's a function expression instead of a function statement. ( MDN, ECMA-262 )
Non-solution: "Just add a function name" according to https://stackoverflow.com/a/11258388/185799 it seems that it's not important to economize on the size of these functions. However, adding a function name results in: {"error":"compilation_error","reason":"Compilation of the map function in the 'myView' view failed: Expression does not eval to a function."}
Possible solution? Add a function name or "module.exports = " at the top, and then remove it during a build phase, using grunt/gulp/yeoman or something related.
For now, what I am actually doing is using the literal form function anonymous(... and then doing a string replace just before calling synchronizeWithDb() to replace function anonymous( with function(. This doesn't answer my question, but works around it.
You have three bad options:
You can ignore the error since CouchDB can process this file (it will wrap it later to make it valid).
You can change the file extension but then you lose syntax highlight, code completion and error checks.
You can delete the error in the error view. It will stay deleted until you change the file the next time or you do a full build.
You may be able to configure Eclipse to stop validating the file.
There are two ways to implement #4:
You can ignore the resource. That makes this file invisible to Eclipse and all plugins. Which means you can't edit it anymore inside of Eclipse. Use Resource Filters for that.
You can check the per-project preferences for Validation rules for JavaScript. Maybe you can exclude the file.

Is TypeScript really a superset of JavaScript?

I just started using TypeScript and sometimes get compiler errors "use of undeclared variable". For example the following works in plain JavaScript :
var foo = {};
foo.bar = 42;
If I try to do the same in TypeScript it won't work and give me the mentioned error above. I have to write it like that:
var foo :any = {};
foo.bar = 42;
In plain JavaScript the type definition with any is neither required nor valid, but in TypeScript this seems to be mandatory. I understand the error and the reason for it, but I always heard in Videos and read in the documentation:
typescriptlang.org:
"TypeScript is a typed superset of JavaScript [...]"
Introduction Video #minute 3:20:
"All JavaScript code is TypeScript code, simply copy and paste"
Is that a thing that changed during the development of TypeScript or do I have to pass a specific compiler setting to make this work?
The reason for TypeScript's existence is to have a compiler and language which can enforce types better than vanilla Javascript does. Any regular Javascript is valid TypeScript, syntactically. That does not mean that the compiler must be entirely happy with it. Vanilla Javascript often contains code which is problematic in terms of type security. That doesn't make it invalid TypeScript code, but it's exactly the reason why TypeScript exists and it's exactly the compiler's job to point out those problems to you.
The languages as such are still sub/supersets of one another.
Theorem: TypeScript is neither a subset nor a superset of JavaScript.
Proof:
When we say language A is a subset of language B, we mean all valid A-programs are also valid B-programs.
Here is a valid TypeScript program that is not a valid JavaScript program:
let x: number = 3;
You identified a valid JavaScript program that is not a valid TypeScript program:
var foo = {};
foo.bar = 42;
Complicating factor 1: TypeScript is almost a superset. TypeScript is intended to be a near superset of JavaScript. Most valid JS is also valid TS. What JS is not can usually be easily tweaked to compile without errors in TS. In other words, most valid JS is also valid TS.
Complicating factor 2: non-fatal errors The TypeScript compiler generates the JavaScript code you intend sometimes even if there are errors. The your example that I referenced earlier emits this error
error TS2339: Property 'bar' does not exist on type '{}'.
but also this JS code
var foo = {};
foo.bar = 42;
The TS documentation notes
You can use TypeScript even if there are errors in your code. But in this case, TypeScript is warning that your code will likely not run as expected.
I think we can call this a failed compilation (and thus invalid TypeScript) for the following reasons:
The compiler seems to use the term warning in the conventional sense, so we should interpret error in the conventional sense too: an error indicates the compilation failed.
The documentation indicates that the resulting JavaScript is not necessarily correct as to what was intended. An incorrect output seems just as bad as (if not worse than) no output. They should both be considered failed.
The TypeScript compiler exits with a non-zero status code, which conventionally indicates that the process failed in some way.
If we call any TypeScript program that outputs JavaScript "valid", then we would have to call the following TypeScript program valid, because it a dot compiles to the empty string after issuing errors:
.
Complicating factor 3: TS accepts JS files: The TypeScript compiler can passthrough files ending in .js (see compiler documentation for --allowJs). In this sense TypeScript is a superset of JS. All .js files can be compiled with TypeScript. This is probably not what people who visit this question are meaning to ask.
I think complicating factor 1 is the thing that Anders Hejlsberg is getting at. It might also justify the misleading marketing on TypeScript's homepage. The other answers have fallen prey to complicating factor 2. However the general advice given in the other answers is correct: TypeScript is a layer on top of JavaScript designed to tell you when you do something bad. They are different tools for different purposes.
No. In other answers I believe the technical reason has been well explained, but I notice an example that could immediately serve as a contradiction to the claim in the question (different semantics):
// In TypeScript
function f<A>(a: A) { return a; };
console.log(f<Function>(f)); // <-- This line. It will print the function f since it is an identify function that in this case takes self and returns self.
Comparing to the below JavaScript example
// In JavaScript
function f(a) { return a; };
console.log(f<Function>(f)); // <-- This line. This is VALID JavaScript
At first glance you might think there should be a syntax error for the JavaScript example. HOWEVER, once you examine it closely, you'll notice that actually the line is executed as
console.log((f < Function) > f); // Evaluate to false
which is completely valid in JavaScript. This essentially means the same line of code resulted in 2 completely different interpretation in JavaScript and TypeScript, therefore a counterexample to the question.
The definition
"All JavaScript code is TypeScript code, simply copy and paste"
is true. Because any JavaScript code can passed to the TypeScript compiler.
So it's sort of a Layer on top of JavaScript. So, of course the underlaying Layer (JavaScript) can be passed through the layers to the top (TypeScript), but not the other way around.
Why not?
Think of it as a bike (JavaScript) and a motorcycle (TypeScript). The basics are the same (two wheels, a frame), but the motorcycle as an engine and some enhanced features.
So, you can use your motorcycle (TypeScript) as a bike (JavaScript), but you cannot use a bike as a motorcycle.
EDIT:
If your compiler throws a warning, why does it make the
statement wrong? It just says: Hey, you are using TypeScript, and it's
more strict than what you gave me.
See this example, it compiles perfectly to JavaScript, but throws a warning.

javascript: way to get the last value returned in the interpreter

I'm trying to find a way to programatically get the last value returned by the Javascript interpreter. Ruby's interpreter, to name an example, has the "_":
1 + 2 #=> 3
_ #=> 3
I would like to know if the same thing exists in Javascript.
EDIT:
Another way to maybe achieve this. Is there any syntax that supports the continuation of an expression in a newline? Something like this:
var a = \&
1 + 2;
a #=> 3
Some sort of combination of characters that tell the interpreter the expression continues in a newline (like the + for string concatenation).
PURPOSE:
Purpose of this research is to find if I can load a JSON data structure using a script tag and successfully assign it from outside of its scope, something like this:
<script> var json_struct = </script>
<script src="http://domain.com/myjsonfile.json" type='application/json' ></script>
which, by the way, doesn't work. Surprisingly :)
PURPOSE:
Purpose of this research is to find if I can load a JSON data structure
using a script tag and successfully assign it from outside of its scope
There is no construct in browser based javascript that can do this.
The reason is that browsers, since the earliest Netscape days, have always initiated the script compiler upon the closing of the script tag. Regardless if it's javascript, VBscript (IE only) or Tcl (with the appropriate plugin).
Which means that any statement that is incomplete will simply be treated as a syntax error. Each <script> tag is basically treated as a single file.
What you're trying to do is similar to this in Ruby:
a = require 'one_plus_two.rb'
which I don't think works in Ruby.
However, in non-browser environments that support modules like Node.js, the method that imports module does in fact return a value (usually an object). So you can do something like this in node.js:
var a = require('my_data_file.js');
Unfortunately, the require function only works on local files. But Node.js is open source so you can always fork it and modify require to be able to source from http:// like PHP.
Alas, if what you're trying to do is browser scripting then the above point is moot.

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