andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 1 | # Closure Compilation |
| 2 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 3 | ## What is type safety? |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 4 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 5 | [Strongly-typed languages](https://en.wikipedia.org/wiki/Strong_and_weak_typing) |
| 6 | like C++ and Java have the notion of variable types. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 7 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 8 | This is typically baked into how you declare variables: |
dbeam | 707cf27 | 2016-03-10 04:12:13 | [diff] [blame] | 9 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 10 | ```c++ |
| 11 | const int32 kUniversalAnswer = 42; // type = 32-bit integer |
dbeam | e3d03b7c | 2016-02-06 03:08:24 | [diff] [blame] | 12 | ``` |
| 13 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 14 | or as [templates](https://en.wikipedia.org/wiki/Template_metaprogramming) for |
| 15 | containers or generics: |
dbeam | e3d03b7c | 2016-02-06 03:08:24 | [diff] [blame] | 16 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 17 | ```c++ |
| 18 | std::vector<int64> fibonacci_numbers; // a vector of 64-bit integers |
dbeam | 43f31dd | 2016-03-09 22:05:59 | [diff] [blame] | 19 | ``` |
| 20 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 21 | When differently-typed variables interact with each other, the compiler can warn |
| 22 | you if there's no sane default action to take. |
dbeam | 707cf27 | 2016-03-10 04:12:13 | [diff] [blame] | 23 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 24 | Typing can also be manually annotated via mechanisms like `dynamic_cast` and |
| 25 | `static_cast` or older C-style casts (i.e. `(Type)`). |
| 26 | |
| 27 | Using stongly-typed languages provide _some_ level of protection against |
| 28 | accidentally using variables in the wrong context. |
| 29 | |
| 30 | JavaScript is weakly-typed and doesn't offer this safety by default. This makes |
| 31 | writing JavaScript more error prone, and various type errors have resulted in |
| 32 | real bugs seen by many users. |
| 33 | |
| 34 | ## Chrome's solution to typechecking JavaScript |
| 35 | |
| 36 | Enter [Closure Compiler](https://developers.google.com/closure/compiler/), a |
| 37 | tool for analyzing JavaScript and checking for syntax errors, variable |
| 38 | references, and other common JavaScript pitfalls. |
| 39 | |
| 40 | To get the fullest type safety possible, it's often required to annotate your |
| 41 | JavaScript explicitly with [Closure-flavored @jsdoc |
| 42 | tags](https://developers.google.com/closure/compiler/docs/js-for-compiler) |
| 43 | |
| 44 | ```js |
| 45 | /** |
| 46 | * @param {string} version A software version number (i.e. "50.0.2661.94"). |
qyearsley | c0dc6f4 | 2016-12-02 22:13:39 | [diff] [blame] | 47 | * @return {!Array<number>} Numbers corresponding to |version| (i.e. [50, 0, 2661, 94]). |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 48 | */ |
| 49 | function versionSplit(version) { |
| 50 | return version.split('.').map(Number); |
| 51 | } |
dbeam | 43f31dd | 2016-03-09 22:05:59 | [diff] [blame] | 52 | ``` |
| 53 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 54 | See also: |
| 55 | [the design doc](https://docs.google.com/a/chromium.org/document/d/1Ee9ggmp6U-lM-w9WmxN5cSLkK9B5YAq14939Woo-JY0/edit). |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 56 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 57 | ## Typechecking Your Javascript |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 58 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 59 | Given an example file structure of: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 60 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 61 | + lib/does_the_hard_stuff.js |
| 62 | + ui/makes_things_pretty.js |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 63 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 64 | `lib/does_the_hard_stuff.js`: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 65 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 66 | ```javascript |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 67 | var wit = 100; |
| 68 | |
| 69 | // ... later on, sneakily ... |
| 70 | |
| 71 | wit += ' IQ'; // '100 IQ' |
| 72 | ``` |
| 73 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 74 | `ui/makes_things_pretty.js`: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 75 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 76 | ```javascript |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 77 | /** @type {number} */ var mensa = wit + 50; |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 78 | |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 79 | alert(mensa); // '100 IQ50' instead of 150 |
| 80 | ``` |
| 81 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 82 | Closure compiler can notify us if we're using `string`s and `number`s in |
| 83 | dangerous ways. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 84 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 85 | To do this, we can create: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 86 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 87 | + ui/compiled_resources2.gyp |
| 88 | |
| 89 | With these contents: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 90 | |
| 91 | ``` |
dbeam | 1ff3458 | 2016-03-10 18:30:36 | [diff] [blame] | 92 | # Copyright 2016 The Chromium Authors. All rights reserved. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 93 | # Use of this source code is governed by a BSD-style license that can be |
| 94 | # found in the LICENSE file. |
| 95 | { |
| 96 | 'targets': [ |
| 97 | { |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 98 | # Target names is typically just without ".js" |
| 99 | 'target_name': 'makes_things_pretty', |
| 100 | |
| 101 | 'dependencies': [ |
| 102 | '../lib/compiled_resources2.gyp:does_the_hard_stuff', |
| 103 | |
| 104 | # Teaches closure about non-standard environments/APIs, e.g. |
| 105 | # chrome.send(), chrome.app.window, etc. |
| 106 | '<(EXTERNS_GYP):extern_name_goes_here' |
dbeam | 1ff3458 | 2016-03-10 18:30:36 | [diff] [blame] | 107 | ], |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 108 | |
| 109 | 'includes': ['../path/to/third_party/closure_compiler/compile_js2.gypi'], |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 110 | }, |
| 111 | ], |
| 112 | } |
| 113 | ``` |
| 114 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 115 | ## Running Closure compiler locally |
| 116 | |
| 117 | You can locally test that your code compiles on Linux or Mac. This requires |
| 118 | [Java](http://www.oracle.com/technetwork/java/javase/downloads/index.html) and a |
xiaoyin.l | 1003c0b | 2016-12-06 02:51:17 | [diff] [blame] | 119 | [Chrome checkout](https://www.chromium.org/developers/how-tos/get-the-code) (i.e. |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 120 | python, depot_tools). Note: on Ubuntu, you can probably just run `sudo apt-get |
| 121 | install openjdk-7-jre`. |
| 122 | |
| 123 | Now you should be able to run: |
| 124 | |
| 125 | ```shell |
| 126 | third_party/closure_compiler/run_compiler |
| 127 | ``` |
| 128 | |
| 129 | and should see output like this: |
| 130 | |
| 131 | ```shell |
| 132 | ninja: Entering directory `out/Default/' |
| 133 | [0/1] ACTION Compiling ui/makes_things_pretty.js |
| 134 | ``` |
| 135 | |
| 136 | To compile only a specific target, add an argument after the script name: |
| 137 | |
| 138 | ```shell |
| 139 | third_party/closure_compiler/run_compiler makes_things_pretty |
| 140 | ``` |
| 141 | |
| 142 | In our example code, this error should appear: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 143 | |
| 144 | ``` |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 145 | (ERROR) Error in: ui/makes_things_pretty.js |
| 146 | ## /my/home/chromium/src/ui/makes_things_pretty.js:1: ERROR - initializing variable |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 147 | ## found : string |
| 148 | ## required: number |
| 149 | ## /** @type {number} */ var mensa = wit + 50; |
| 150 | ## ^ |
| 151 | ``` |
| 152 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 153 | Hooray! We can catch type errors in JavaScript! |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 154 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 155 | ## Trying your change |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 156 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 157 | Closure compilation also has [try |
| 158 | bots](https://build.chromium.org/p/tryserver.chromium.linux/builders/closure_compilation) |
| 159 | which can check whether you could *would* break the build if it was committed. |
| 160 | |
| 161 | From the command line, you try your change with: |
| 162 | |
| 163 | ```shell |
| 164 | git cl try -b closure_compilation |
| 165 | ``` |
| 166 | |
| 167 | To automatically check that your code typechecks cleanly before submitting, you |
| 168 | can add this line to your CL description: |
| 169 | |
| 170 | ``` |
| 171 | CQ_INCLUDE_TRYBOTS=tryserver.chromium.linux:closure_compilation |
| 172 | ``` |
| 173 | |
| 174 | Working in common resource directories in Chrome automatically adds this line |
| 175 | for you. |
| 176 | |
| 177 | ## Integrating with the continuous build |
| 178 | |
| 179 | To compile your code on every commit, add your file to the `'dependencies'` list |
| 180 | in `src/third_party/closure_compiler/compiled_resources2.gyp`: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 181 | |
| 182 | ``` |
| 183 | { |
| 184 | 'targets': [ |
| 185 | { |
| 186 | 'target_name': 'compile_all_resources', |
| 187 | 'dependencies': [ |
| 188 | # ... other projects ... |
dbeam | 1ff3458 | 2016-03-10 18:30:36 | [diff] [blame] | 189 | ++ '../my_project/compiled_resources2.gyp:*', |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 190 | ], |
| 191 | } |
| 192 | ] |
| 193 | } |
| 194 | ``` |
| 195 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 196 | This file is used by the |
xiaoyin.l | 1003c0b | 2016-12-06 02:51:17 | [diff] [blame] | 197 | [Closure compiler bot](https://build.chromium.org/p/chromium.fyi/builders/Closure%20Compilation%20Linux) |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 198 | to automatically compile your code on every commit. |
michaelpg | c1e2d1e | 2017-05-01 23:40:59 | [diff] [blame] | 199 | |
| 200 | ## Externs |
| 201 | |
| 202 | [Externs files](https://github.com/google/closure-compiler/wiki/FAQ#how-do-i-write-an-externs-file) |
| 203 | define APIs external to your JavaScript. They provide the compiler with the type |
| 204 | information needed to check usage of these APIs in your JavaScript, much like |
| 205 | forward declarations do in C++. |
| 206 | |
| 207 | Third-party libraries like Polymer often provide externs. Chrome must also |
| 208 | provide externs for its extension APIs. Whenever an extension API's `idl` or |
| 209 | `json` schema is updated in Chrome, the corresponding externs file must be |
| 210 | regenerated: |
| 211 | |
| 212 | ```shell |
| 213 | ./tools/json_schema_compiler/compiler.py -g externs \ |
| 214 | extensions/common/api/your_api_here.idl \ |
| 215 | > third_party/closure_compiler/externs/your_api_here.js |
| 216 | ``` |