Closure Compiler: what are the --language_in and --language_out options?
Google Closure Compiler has the following CLI options:
-
--language_in
- establishes the language specification with which the source sources conform. -
--language_out
- Sets in which language the result should match.
Language in
What is the meaning --language_in
? I assume it is one of the following:
- Declaration / promise that the source code is in accordance with the specified language. The compiler does not check what it is doing; he just believes it to be so.
- Instructions for the compiler to check if the source code matches the specified language. The compiler will raise an error if it is not.
This documentation implies the meaning of # 1:
Projects can indicate which version of the execript language they intend by using a flag
--language_in
.
However, this documentation indicates value # 2 (I'm assuming the language
webservice is wired to CLI --language_in
option):
The parameter
language
refers to which version of ECMAScript to accept when checking for errors in your code.
ECMASCRIPT3
- validates code that presumes ECMAScript 3 conformance and gives errors to the code using functions that are only present in ECMAScript 5.
If I compile this code using webservice :
// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level ADVANCED_OPTIMIZATIONS
// @language ECMASCRIPT3
// ==/ClosureCompiler==
var x = Object.freeze({
y: 3,
z: 4
});
console.log(x);
it doesn't give an error, which Object.freeze()
doesn't exist in ECMAScript 3 . Why not?
Tongue
Likewise, what's the point --language_out
? I guess what this means:
- Instructions to the compiler that it should produce output that matches the specified language.
But if I compile this code:
var x = Object.freeze({
y: 3,
z: 4
});
console.log(x);
using this CLI command specifying --language_out ECMASCRIPT3
:
java -jar compiler.jar \
--js test.js \
--js_output_file test.min.js \
--language_out ECMASCRIPT3 \
--compilation_level ADVANCED_OPTIMIZATIONS \
--warning_level VERBOSE \
--summary_detail_level 3
no errors or warnings:
0 error(s), 0 warning(s), 100.0% typed
and the compiled code:
var a=Object.freeze({y:3,z:4});console.log(a);
So the output doesn't match ECMASCRIPT3
(as it contains Object.freeze()
). Why not?
source to share
The call is Object.freeze()
not a syntax error. The compiler doesn't know if your code will extend the object prototype at some point. Thus, it Object.freeze()
is perfectly valid ECMAScript 3 code.
Similarly,
var x = {};
x.something();
is a perfectly valid code. It will throw an exception at runtime, but this is not a syntax error.
The ES5 feature that should illustrate these options is to use reserved words as property names in object literals:
var x = { if: "hello world" };
Others would be getter / setter initializers:
var x = { get hello() { return "world"; } };
source to share