using closure compiler api from java to generate errors and warnings

47 views
Skip to first unread message

pequalsnp

unread,
Jul 2, 2010, 6:47:35 AM7/2/10
to Closure Compiler Discuss
hi,

i'm trying to generate all the errors and warnings mentioned on this
page http://code.google.com/closure/compiler/docs/error-ref.html using
the closure compiler api from java.

these are the options i'm using:

options.aggressiveVarCheck = CheckLevel.WARNING;
options.checkControlStructures = true;
options.checkDuplicateMessages = true;
options.checkMethods = CheckLevel.WARNING;
// options.checkSymbols = true;
options.checkShadowVars = CheckLevel.WARNING;
options.checkProvides = CheckLevel.WARNING;
options.checkRequires = CheckLevel.WARNING;

options.checkGlobalNamesLevel = CheckLevel.WARNING;
options.checkUnreachableCode = CheckLevel.WARNING;
options.foldConstants = true;
options.checkTypes = true;
options.checkSuspiciousCode = true;
options.checkGlobalNamesLevel = CheckLevel.WARNING;
options.checkFunctions = CheckLevel.WARNING;
options.checkGlobalThisLevel = CheckLevel.WARNING;
options.collapseProperties = true;

options.inlineConstantVars = true;
options.foldConstants = true;
compiler.setPassConfig(new DefaultPassConfig(options));

it appears that there should be an additional call to
compiler.optimize(), however that results in an internal compiler
error

could somebody advise me on the options to use?

if you want further information regarding the internal compiler error,
i'd be happy to do what i can to find out more.


regards

John Lenz

unread,
Jul 2, 2010, 10:42:39 AM7/2/10
to closure-comp...@googlegroups.com

Can you provide a stack trace?

pequalsnp

unread,
Jul 3, 2010, 7:45:15 AM7/3/10
to Closure Compiler Discuss


On Jul 2, 4:42 pm, John Lenz <concavel...@gmail.com> wrote:
> Can you provide a stack trace?

!ENTRY org.eclipse.core.jobs 4 2 2010-07-03 13:41:13.698
!MESSAGE An internal error occurred during: "Compiling...".
!STACK 0
java.lang.RuntimeException: INTERNAL COMPILER ERROR.
Please report this problem.
null
at com.google.common.base.Preconditions.checkState(Preconditions.java:
131)
at
com.google.javascript.jscomp.NodeTraversal.traverseRoots(NodeTraversal.java:
271)
at
com.google.javascript.jscomp.NodeTraversal.traverseRoots(NodeTraversal.java:
261)
at com.google.javascript.jscomp.Normalize.process(Normalize.java:111)
at com.google.javascript.jscomp.Compiler.process(Compiler.java:701)
at com.google.javascript.jscomp.Compiler.normalize(Compiler.java:
1481)
at com.google.javascript.jscomp.Compiler.optimize(Compiler.java:1427)
at
org.mondula.eclipse.closure.compiler.CompilerRunner.run(CompilerRunner.java:
159)
at org.mondula.eclipse.closure.jobs.LaunchJob.run(LaunchJob.java:111)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
Caused by: java.lang.IllegalStateException
... 10 more

pequalsnp

unread,
Jul 6, 2010, 4:21:33 AM7/6/10
to Closure Compiler Discuss
hi,

i narrowed it down. it's actually this piece of code

// Produces JSC_TRAILING_COMMA error:
var i = {
foo: 'foo',
bar: 'bar',
};

that causes the error, if you call compiler.compile() and then
compiler.optimize(), even without setting any options.

could you still give me some hints on how to get the closure compiler
to report all the warnings and errors it should be able to detect?


regards

John Lenz

unread,
Jul 7, 2010, 11:10:27 AM7/7/10
to closure-comp...@googlegroups.com
Yes, that is a parse error.  The trailing comma is a very common problem that people run into, it won't parse on Internet Explorer, but does on some other browsers.   Closure Compiler rejects it to allow it to generate browser neutral code.

Nick Santos

unread,
Jul 7, 2010, 11:24:13 AM7/7/10
to closure-comp...@googlegroups.com
On Tue, Jul 6, 2010 at 4:21 AM, pequalsnp
<f.alexan...@googlemail.com> wrote:
> hi,
>
> i narrowed it down. it's actually this piece of code
>
> // Produces JSC_TRAILING_COMMA error:
>        var i = {
>          foo: 'foo',
>          bar: 'bar',
>        };
>
> that causes the error, if you call compiler.compile() and then
> compiler.optimize(), even without setting any options.

I don't really understand why you're calling optimize() separately?
The compile() method already calls optimize() directly, but it does
all the messy work of checking if there are any errors.

>
> could you still give me some hints on how to get the closure compiler
> to report all the warnings and errors it should be able to detect?

It sounds like you already figured that part out though? You basically
just want to look at CompilerOptions and turn on everything you want.

Nick

pequalsnp

unread,
Jul 10, 2010, 7:08:04 AM7/10/10
to Closure Compiler Discuss
hi,

from looking at the compiler sources i got the idea that some errors
are only detected when the closure compiler tries to optimize the js
code.

on a different note, i'm not able to get the closure compiler to
generate the following warnings

// Produces JSC_INDEX_OUT_OF_BOUNDS_ERROR error:
var x = [0, 1, 2];
alert(x[3]);

and

// Produces JSC_INVALID_GETELEM_INDEX_ERROR:
var x = [1,2,3];
alert(x['a']);

any advice?

regards
On Jul 7, 5:24 pm, Nick Santos <nicksan...@google.com> wrote:
> On Tue, Jul 6, 2010 at 4:21 AM, pequalsnp
>

Devin Coughlin

unread,
Jul 10, 2010, 2:45:25 PM7/10/10
to closure-comp...@googlegroups.com

On Jul 10, 2010, at 4:08 AM, pequalsnp wrote:

> hi,
>
> from looking at the compiler sources i got the idea that some errors
> are only detected when the closure compiler tries to optimize the js
> code.

That's correct; some of the warnings are only produced because the compiler noticed something fishy while trying to optimize.
If the optimizations are off, or if the compiler couldn't optimize a particular piece of code to the point where it would notice the problem, then it won't produce a warning.

> on a different note, i'm not able to get the closure compiler to
> generate the following warnings
>
> // Produces JSC_INDEX_OUT_OF_BOUNDS_ERROR error:
> var x = [0, 1, 2];
> alert(x[3]);

In this case, you'll need --compilation_level ADVANCED_OPTIMIZATIONS for this error to be reported.
The compiler only reports this error when attempting folding, but won't t even detect this as a folding opportunity unless inlining of global variables is turned on, which requires ADVANCED_OPTIMIZATIONS.

The compiler will detect this error in non-global scope (i.e. inside a function) with only SIMPLE_OPTIMIZATIONS.

There are several other optimizations that the compiler applies inside functions, but not in global scope -- this can be confusing because short little programs for testing/exploration are often only in the global scope.

> // Produces JSC_INVALID_GETELEM_INDEX_ERROR:
> var x = [1,2,3];
> alert(x['a']);

I believe this is a mistake in the documentation. With ADVANCED_OPTIMIZATIONS, the compiler reports this error for:

var x = [1,2,3];
alert(x[2.3])

but does not do so for string indices (because, for example, x['length'] would be fine).

If distinguishing between x['a'] and x['length'] is important t to you, please file an issue:

http://code.google.com/p/closure-compiler/issues/list

Devin


Reply all
Reply to author
Forward
0 new messages