1 | # defs.js
|
2 | Static scope analysis and transpilation of ES6 block scoped `const` and `let`
|
3 | variables, to ES3.
|
4 |
|
5 | Node already supports `const` and `let` so you can use that today
|
6 | (run `node --harmony` and `"use strict"`). `defs.js` enables you to do the same
|
7 | for browser code. While developing you can rely on the experimental support
|
8 | in Chrome (chrome://flags, check Enable experimental JavaScript). `defs.js` is
|
9 | also a pretty decent static scope analyzer/linter.
|
10 |
|
11 |
|
12 | ## Installation and usage
|
13 | npm install -g defs
|
14 |
|
15 | Then run it as `defs file.js`. The errors (if any) will go to stderr,
|
16 | the transpiled source to `stdout`, so redirect it like `defs file.js > output.js`.
|
17 | More command line options is coming.
|
18 |
|
19 |
|
20 | ## Configuration
|
21 | `defs` looks for a `defs-config.json` configuration file in your current
|
22 | directory. It will search for it in parent directories soon as you'd expect.
|
23 |
|
24 | Example `defs-config.json`:
|
25 |
|
26 | {
|
27 | "environments": ["node", "browser"],
|
28 |
|
29 | "globals": {
|
30 | "my": false,
|
31 | "hat": true
|
32 | },
|
33 | "disallowVars": false,
|
34 | "disallowDuplicated": true,
|
35 | "disallowUnknownReferences": true
|
36 | }
|
37 |
|
38 | `globals` lets you list your program's globals, and indicate whether they are
|
39 | writable (`true`) or read-only (`false`), just like `jshint`.
|
40 |
|
41 | `environments` lets you import a set of pre-defined globals, here `node` and
|
42 | `browser`. These default environments are borrowed from `jshint` (see
|
43 | `jshint_globals/vars.js`).
|
44 |
|
45 | `disallowVars` (defaults to `false`) can be enabled to make
|
46 | usage of `var` an error.
|
47 |
|
48 | `disallowDuplicated` (defaults to `true`) errors on duplicated
|
49 | `var` definitions in the same function scope.
|
50 |
|
51 | `disallowUnknownReferences` (defaults to `true`) errors on references to
|
52 | unknown global variables.
|
53 |
|
54 |
|
55 | ## Example
|
56 |
|
57 | Input `example.js`:
|
58 |
|
59 | ```javascript
|
60 | "use strict";
|
61 | function fn() {
|
62 | const y = 0;
|
63 | for (let x = 0; x < 10; x++) {
|
64 | const y = x * 2;
|
65 | const z = y;
|
66 | }
|
67 | console.log(y); // prints 0
|
68 | }
|
69 | fn();
|
70 | ```
|
71 |
|
72 | Output from running `defs example.js`:
|
73 |
|
74 | ```javascript
|
75 | "use strict";
|
76 | function fn() {
|
77 | var y = 0;
|
78 | for (var x = 0; x < 10; x++) {
|
79 | var y$0 = x * 2;
|
80 | var z = y$0;
|
81 | }
|
82 | console.log(y); // prints 0
|
83 | }
|
84 | fn();
|
85 | ```
|
86 |
|
87 |
|
88 | ## License
|
89 | `MIT`, see LICENSE file.
|