Closure Tools

Error and Warning Reference

  1. Closure Compiler Errors
  2. Closure Compiler Warnings

Closure Compiler Errors

JSC_BITWISE_OPERAND_OUT_OF_RANGE:

Operand out of range, bitwise operation will lose information: NUMBER {value of left-hand operand of bitwise operation} :

This error means that the value of the left-hand operand of a bitwise operation will not fit into 32 bits. For example, the following code produces this error because the binary representation of 1024 * 1024 * 1024 * 2 has more than 32 bits:

// Produces JSC_BITWISE_OPERAND_OUT_OF_RANGE error:
var y = 1024 * 1024 * 1024 * 2 >> 2;

JavaScript allows you to supply more than 32 bits as the left-hand operand of a bitwise operation, but it silently truncates the value to fit into 32 bits. The compiler throws this error to prevent you from accidentally losing information.

JSC_CONSTANT_REASSIGNED_VALUE_ERROR:

constant {name of constant} assigned a value more than once.

This error means that you are treating a constant like a normal variable by assigning a value to it more than once. The compiler assumes that any variable at least two characters long with a name in all capital letters should be treated as a constant. After a constant is initialized, its value can never change. For example, the following code produces an error:

// Produces JSC_CONSTANT_REASSIGNED_VALUE_ERROR error:
var MY_CONSTANT = 1;
MY_CONSTANT = 2;

Properties with all-caps names are also treated as constants, so the following code produces the same error:

// Produces JSC_CONSTANT_REASSIGNED_VALUE_ERROR error:
var myObject = {};
myObject.MY_CONSTANT = 1;
myObject.MY_CONSTANT = 2;

To fix this error, first determine whether the variable should be a constant or not.

If you intend the variable to be used as a constant and you get this error, then the compiler has helped you find a place where you unintentionally initialized the constant twice. That's what this check is for! Naming a variable in all-caps is a handy way of making sure that its value never changes.

If you didn't intend to make the variable unchangeable, just change the variable's name so that it includes at least one lower-case letter.

JSC_DIVIDE_BY_0_ERROR:

Divide by 0

This error means that you an arithmetical division expression with a denominator of 0. Dividing by zero at runtime produces a runtime error.

JSC_DUPLICATE_EXTERN_INPUT:

Duplicate extern input: {URL}

This error means that you have supplied the same externs file more than once. For example, you can produce this error by specifying two externs_url parameters with the same URL.

Fix this error by removing one of the duplicate externs_url parameters.

See Advanced Compilation and Externs for more information about externs.

JSC_DUPLICATE_INPUT:

Duplicate input: {File location}

This error means that you have supplied the same source of JavaScript to the compiler more than once. For example, you can produce this error by specifying two code_url parameters with the same URL.

Fix this error by removing one of the duplicate code_url parameters.

JSC_FRACTIONAL_BITWISE_OPERAND:

Fractional bitwise operand: NUMBER {non-integer value}

This error means that one of the arguments of a bitwise operator in your code is a floating point value rather than an integer. For example, the following code produces this error:

// Produces JSC_FRACTIONAL_BITWISE_OPERAND error:
var x = 5 >> 2.5;
alert(x);
JSC_INDEX_OUT_OF_BOUNDS_ERROR:

Array index out of bounds: {value of bad index}.

This error means that you are trying to access a position in an array that doesn't exist. For example, the following code produces an error because 2 is the highest possible index in the array, but the expression x[3] uses an index of 3:

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

A negative array index also produces this error:

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

Array index not integer: {non-integer value}.

This error means that you are using a string, a double, or some other non-integer as an array index. For example, the following code produces this error:

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

Can't negate non-numeric value: {non-numeric value}

This error means that you have a negative sign in front of an expression that isn't a number. For example:

// Produces JSC_NEGATING_A_NON_NUMBER_ERROR:
var x = -'hello';
// Produces JSC_NEGATING_A_NON_NUMBER_ERROR:
var x = -{foo: 'bar'};
JSC_PARSE_ERROR:

Parse error. {Description of syntax error}

This error means that you have tried to compile code that is not syntactically valid JavaScript. Look on the indicated line for typos or other syntax errors that could keep the code from parsing.

JSC_SHIFT_AMOUNT_OUT_OF_BOUNDS:

Shift amount out of bounds: NUMBER {shift amount}.

This error means that the second operand of a bit shift operator (<<, >>, or >>>) is either less than 0 or greater than 31. This second operand indicates the amount of the shift, and must always be between 0 and 31. For example, the following code produces this error:

// Produces JSC_SHIFT_AMOUNT_OUT_OF_BOUNDS:
var x = 5 >> 32;
alert(x);
JSC_TRAILING_COMMA:

Parse error. Internet Explorer has a non-standard interpretation of trailing commas. Arrays will have the wrong length and objects will not parse at all.

or

Parse error. Trailing comma is not legal in an ECMA-262 object initializer.

This error means that the last item of a list has a comma after it, like this:

// Produces JSC_TRAILING_COMMA error:
var x = [1,2,3,];

Trailing commas in the property lists of Object literals produce the same error:

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

Although many browsers would run this code without complaining, it is not valid ECMAScript and may cause unexpected behavior or errors in Internet Explorer. Correct the error by deleting the trailing comma:

var x = [1,2,3];
var x = { 
  foo: 'foo', 
  bar: 'bar'
};

Closure Compiler Warnings

JSC_BAD_DELETE_OPERAND:

delete operator needs a reference operand

This warning means that you have applied the delete operator to something other than an object property, a variable, or an array element. For example, you can produce this error by applying delete to a string literal or a number:

delete 1;
delete 'abc';
JSC_BAD_TYPE_FOR_BIT_OPERATION:

{Name of bad operand} :

This warning means that one of the operands of a bitwise operator is something other than an integer. Both of the last two lines in the following code produce a JSC_BAD_TYPE_FOR_BIT_OPERATION warning:

var obj = {};
var x = 5 >> obj; // obj is not an integer
var x = obj >> 5; // obj is still not an integer

The bitwise operators in JavaScript are:

  • &: Bitwise AND
  • |: Bitwise OR
  • ^: Bitwise XOR
  • ~: Bitwise NOT
  • <<: Shift left
  • >>: Shift right with sign
  • >>>: Shift right with zero fill
JSC_CONSTRUCTOR_NOT_CALLABLE:

Constructor function (this:{Name of constructor}): ? should be called with the "new" keyword.

This warning means that you call a constructor as if it were a normal function, without the new keyword. The compiler only treats a function as a constructor if it is marked with the @constructor JSDoc annotation. The following code produces the warning:

// Produces JSC_CONSTRUCTOR_NOT_CALLABLE warning:
/**
 * @constructor
 */
function MyClass() {
  this.foo = 'bar';
}
var y = MyClass();

In contrast, however, this code does not produce the warning:

function MyClass() {
  this.foo = 'bar';
}
var y = MyClass();

If you get this warning, check to see whether you left out a new from the line producing the error.

JSC_FUNCTION_MASKS_VARIABLE:

function {name of function} masks variable.

This warning means that a function in your code has the same name as a variable. For example, the following code produces this warning:

// Produces JSC_FUNCTION_MASKS_VARIABLE warning:
var hello = 1; 
function hello() {
  alert('hi');
}

This sort of duplicate name not only leads to confusing code, but also violates assumptions on which the compiler relies for some of its code transformations. To eliminate this warning, rename either the function or the variable.

JSC_INVALID_FUNCTION_DECL:

function declaration must have a name

This warning means that you declare a function with nothing between the function keyword and the argument list, as in the following example:

// Produces JSC_INVALID_FUNCTION_DECL warning:
function (x) {
  return x + 1;
}

The compiler assumes that this is a mistake.

Note that this sort of function declaration differs from an anonymous function. An anonymous function is assigned to a variable or passed as an argument. The following example does not produce the warning:

var y = function (x) {
  return x + 1;
}

And so is this:

doStuff(15, function(x) {return x + 1;});
JSC_NAMESPACE_REDEFINED:

namespace {name of object} should not be redefined

This warning means that you may be erasing important properties of an object by reinitializing that object. The word "namespace" in the error message refers to an object that is used to store and organize a collection of functions and values. For example, in the following code the function b is stored in a property of the namespace object a:

// Produces JSC_NAMESPACE_REDEFINED warning:
var a = {};
/** @constructor */
a.b = function() {
  this.msg = 'hi';
};
a = {};
var x = new a.b();

The statement a = {} on the fifth line of this code overwrites the existing value of a, including the property b.

If you get this warning, make sure that you really intended to erase all of the indicated object's existing properties.

JSC_NOT_A_CONSTRUCTOR:

cannot instantiate non-constructor

This warning means you use the new keyword with something that is not a constructor. For example, the following code produces this warning because it tries to instantiate the number 4:

// Produces JSC_NOT_A_CONSTRUCTOR warning:
var x = new 4;

The compiler expects all constructors to be marked with the JSDoc tag @constructor, like this:

/**
 * @constructor
 */
function MyClass() {
  this.foo = 'bar';
}
var obj = new MyClass();
alert(obj.foo);

The same code without the @constructor tag would produce a JSC_NOT_A_CONSTRUCTOR warning:

// Produces JSC_NOT_A_CONSTRUCTOR warning:
function MyClass() {
  this.foo = 'bar';
}
var obj = new MyClass();
alert(obj.foo);
JSC_NOT_FUNCTION_TYPE:

{expression type} expressions are not callable.

This warning means that you are trying to call something other than a function. For example, the following code produces this warning:

// Produces JSC_NOT_FUNCTION_TYPE warning:
var nonfunction = 4; 
var resultOfCallingNonfunction = nonfunction();
JSC_REDECLARED_VARIABLE:

Redeclared variable: {variable name} :

This warning means that you use the var keyword to declare a variable with a name that is already in use, as in the following code:

// Produces JSC_REDECLARED_VARIABLE warning:
var a = 'First declaration';
var a = 'Second declaration';

Using the name of a function parameter in the declaration of a variable within the function produces the same warning:

function f(a) { 
  var a = 2; 
}

Keep in mind that JavaScript does not create a separate scope for an if/then or loop block. The following code thus also produces the warning:

function x(y) { 
  if (true) { 
    var y; 
  }
}

This sort of duplicate name not only leads to confusing code, but also violates assumptions on which the compiler relies for some of its code transformations. To eliminate this warning, rename one of the instances of the name.

JSC_REFERENCE_BEFORE_DECLARE:

Variable referenced before declaration: {name of variable}.

This warning means that you use a variable and then later declare that variable with the var keyword. For example, the following code produces the warning:

// Produces JSC_REFERENCE_BEFORE_DECLARE warning:
x = 1;
var x = 2;
JSC_SET_WITHOUT_READ:

{Name of property}

This warning means that you assign a value to some property of an object, but never access that property. For example, the following code produces this warning:

// Produces JSC_SET_WITHOUT_READ warning:
var obj = {goodProp: 1}; 
obj.unusedProp = 3; 
alert(obj.goodProp);

If you get this warning, check to see whether you really need the unused property, and delete the assignment if you don't need it. If you believe that the property is indeed read somewhere in your code, check to make sure you don't have a typo that is causing a different property to be accessed instead.

JSC_SUSPICIOUS_SEMICOLON:

If this if/for/while really shouldn't have a body, use {}.

This warning means that you have a semi-colon immediately following an if, for, or while statement. For example:

// Produces JSC_SUSPICIOUS_SEMICOLON warning:
if (true);
else alert('no');

The compiler assumes that this is a mistake, and that you meant to have a statement between if (true) and the semi-colon.

If you really do want an if, for, or while with an empty body, use empty braces instead of a semi-colon:

if (true) {}
else alert('no');
JSC_TYPE_MISMATCH:

{description of mismatch}
found : {type of mismatched expression}

This warning means that an expression has an unexpected type, given its context. For example, the following code produces a JSC_TYPE_MISMATCH because it compares the object literal {foo: bar} with a number:

// Produces JSC_TYPE_MISMATCH warning:
if ({foo: bar} >= 42) {
  alert('yes');
}

Similarly, the following code produces the warning because it uses an object literal as the argument of a bitwise AND operator:

// Produces JSC_TYPE_MISMATCH warning:
var obj = {};
var x = 5 & obj; 

To find the source of a JSC_TYPE_MISMATCH warning, look on the indicated line for an expression that matches the type listed in the found : portion of the error message.

JSC_UNDEFINED_NAME:

Warning: "{Name of property} is never defined"

This warning means that you access a property without ever having initialized it. For example:

// Produces JSC_UNDEFINED_NAME warning:
var root = {};
var reference = root.child;

To fix this warning, set the property before reading it:

var root = {};
root.child = 1;
var reference = root.child;
JSC_UNDEFINED_VARIABLE:

variable {name of variable} is undefined

This error means that you have used a variable without declaring it using the var keyword. For example, the compiler gives you this error if the following line is the first appearance of the variable firstUseOfName in your code:

// Produces JSC_UNDEFINED_VARIABLE warning:
firstUseOfName = 1;

To fix the error, insert the word var before the variable:

var firstUseOfName = 1;
JSC_UNSAFE_NAMESPACE:

incomplete alias created for namespace {name of object}.

This warning means that your code relies on an object property that may not be available after Advanced optimization. The word "namespace" in the error message refers to an object that is used to store and organize a collection of functions and values. For example, in the following code the function c is stored in a property of the namespace object a.b:

// Produces JSC_UNSAFE_NAMESPACE warning:
var a = {};
a.b = {};
/** @constructor */
a.b.c = function(){
  this.msg = 'hi';
}; 
var d = a.b;

The last line of this code contains an unsafe reference to the namespace a.b. This line assigns a.b to d. But Advanced optimization may replace the property structure a.b.c with a single short symbol, and then get rid of a.b. For this reason the compiler emits a warning about this unsafe use of the namespace a.b.

JSC_UNSAFE_THIS:

dangerous use of this in static method {name of method}.

This warning means that you use this in a function that is neither a prototype method nor marked as a constructor with the JSDoc @constructor tag. As a result, after Advanced optimization this may no longer refer to the object on which the function is defined. For example, the function obj.myMethod() in the following code contains a dangerous this:

// Produces JSC_UNSAFE_THIS warning:
var obj = {};
obj.myMethod = function() {
  this.myProperty = 1;
};
obj.myMethod();
alert(obj.myProperty); // We want this to say '1'

In this source code, the this within obj.myMethod refers to obj. However, with Advanced optimization turned on the compiler collapses obj.myMethod into the single symbol obj$myMethod to prepare for further compression. After this transformation, the function is no longer the property of an object, but is instead a simple global function. In this context, this refers to the global this (the window object, if the code is in a standard web page).

Property collapsing does not cause the same problem for constructor functions, however. A constructor is a function that is only invoked with the new keyword. To prevent the JSC_UNSAFE_THIS warning for constructors, mark all constructors with the @constructor JSDoc annotation. The following code does not produce the warning, for example:

var obj = {};
/**
 * @constructor
 */
obj.myMethod = function() {
  this.myProperty = 1;
};
var instance = new obj.myMethod();
alert(instance.myProperty); // We want this to say '1'

In this version of the code, myMethod is used as a constructor (i.e., it is invoked with the new keyword). Within this constructor, this refers to the object created with the statement new obj.myMethod();, not to obj. The this thus means the same thing whether the function is obj.myMethod() or obj$myMethod().

JSC_USED_GLOBAL_THIS:

dangerous use of the global this object.

This warning means that you use the keyword this outside of prototype function or a constructor function. For example, the following code will produce this warning:

// Produces JSC_USED_GLOBAL_THIS warning:
this.foo = 1;

In this case this actually refers to the global this object. For a standard web page, the global object is the same thing as the window object. If you get this warning, make sure that you really intended to refer to the global object.

Note that the compiler only recognizes a function as a constructor if it is annotated with the @constructor JSDoc annotation, like this:

/**
 * @constructor
 */
function MyFunction() {
  this.foo = 1;
}
JSC_USELESS_CODE:

Suspicious code. This code lacks side-effects. Is there a bug?

This warning means that your code includes a statement that has no effect. There are several ways to accidentally create a useless JavaScript statement with a single mistyped character. For example, the second line in the following code consists of a single string literal. This is legal JavaScript, but it does nothing:

// Produces JSC_USELESS_CODE warning:
var s = "this string is "
  "continued on the next line but you forgot the +";

The author intended to put a + at the end of the first line, which would make both lines into a single statement. Since JavaScript allows you omit the semicolon at the end of a line, leaving out the + breaks the code into two statements, the second of which has no effect.

Here's another easy way to accidentally create useless code. The author of the following code meant to assign 1 to the variable x, but included an extra = character:

// Produces JSC_USELESS_CODE warning:
x == 1;

Instead of assigning 1 to the x, this statement is now a boolean expression that has no effect.

If you get a JSC_USELESS_CODE warning, look for typos that might change a useful statement into a useless statement.

JSC_VAR_ARGS_MUST_BE_LAST:

variable length argument must be last.

This warning means that you use a variable-length function parameter, but include other parameters after it. A variable-length function parameter is a special annotation that indicates to the compiler that a function can take a variable number of arguments. It must always be the last parameter.

For example, the following code produces this warning:

// Produces JSC_VAR_ARGS_MUST_BE_LAST warning:
/**
 * @param {...number} var_args
 */
function hi(a, var_args, b) {
  alert(a);
}
JSC_WRONG_ARGUMENT_COUNT:

Function {function name}: called with {number of arguments in call} argument(s). Function requires at least {number of required arguments in function definition} argument(s) [and no more than {number of arguments in function definition} argument(s)].

This warning means that you pass the wrong number of arguments in a function call. For example, the following code produces this error because the function hi() takes two arguments, but the call on the last line only passes one.

// Produces JSC_WRONG_ARGUMENT_COUNT warning:
function hi(a, b){ 
  alert(a + b); 
} 
hi('hello');

JavaScript allows functions to be called with variable numbers of arguments, so this warning does not prevent compilation. It can be useful for detecting mistakes in your code, however.

If you want to use variable-length argument lists and also use JSC_WRONG_ARGUMENT_COUNT warnings to detect problems, you can use the variable-length parameter annotation to explicitly tell the compiler that a function can take a variable number of arguments. When the compiler sees a variable-length parameter like @param {...number} as the last parameter of a function, it knows that the function can accept a variable number of arguments. For example, the following code does NOT produce an error:

/** @param {...string} var_args */
function hi(a, var_args){
  var msg = a;
  if (arguments.length >= 3) {
    msg += arguments[2];
  }
  alert(msg); 
} 
hi('hello');
hi('hello', 'goodbye', 'what?');

Note the use of the JavaScript built-in identifier arguments to access the extra arguments in the above example. Although var_args has a special meaning to the compiler, it still just contains the second argument passed to the function when the function is called. To access extra arguments you need to use arguments, which works like an array containing the arguments passed to the function.

If a function has a variable-length parameter, the compiler still produces a warning if a call passes fewer arguments than the number of parameters that precede the variable-length parameter. Thus the following code does produce a warning:

// Produces JSC_WRONG_ARGUMENT_COUNT:
/** @param {...string} var_args */
function hi(a, b, var_args){ 
  alert(a + b); 
} 
hi('hello');

This function requires at least two arguments, although it has no maximum number of arguments.

Authentifizierung erforderlich

Dazu müssen Sie in Google+ angemeldet sein.

Anmeldung...

Google Developers braucht hierfür Ihre Erlaubnis.