• Stars
    star
    116
  • Rank 303,894 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Static scope analysis and transpilation of ES6 block scoped const and let variables to ES3 vars

SO LONG AND THANKS FOR ALL THE BITS

defs is done. I recommend migrating to the TypeScript tsc compiler because it does what defs does as good or better, and it does much more.

defs.js

Static scope analysis and transpilation of ES6 block scoped const and let variables, to ES3.

Node already supports const and let so you can use that today (run node --harmony and "use strict"). defs.js enables you to do the same for browser code. While developing you can rely on the experimental support in Chrome (chrome://flags, check Enable experimental JavaScript). defs.js is also a pretty decent static scope analyzer/linter.

The talk LET's CONST together, right now (with ES3) from Front-Trends 2013 (slides) includes more information about let, const and defs.js. See also the blog post ES3 <3 block scoped const and let => defs.js.

Installation and usage

npm install -g defs

Then run it as defs file.js. The errors (if any) will go to stderr, the transpiled source to stdout, so redirect it like defs file.js > output.js. More command line options are coming.

There's also a Grunt plugin, see grunt-defs.

See BUILD.md for a description of the self-build and the browser bundle.

License

MIT, see LICENSE file.

Changes

See CHANGES.md.

Configuration

defs looks for a defs-config.json configuration file in your current directory. If not found there, it searches parent directories until it hits /. You may instead pass a custom defs-config.json using --config, i.e. defs --config path/to/defs-config.json file.js > output.js.

Example defs-config.json:

{
    "environments": ["node", "browser"],

    "globals": {
        "my": false,
        "hat": true
    },
    "loopClosures": "iife",
    "disallowVars": false,
    "disallowDuplicated": true,
    "disallowUnknownReferences": true
}

globals lets you list your program's globals, and indicate whether they are writable (true) or read-only (false), just like jshint.

environments lets you import a set of pre-defined globals, here node and browser. These default environments are borrowed from jshint (see jshint_globals/vars.js).

loopClosures (defaults to false) can be set to "iife" to enable transformation of loop-closures via immediately-invoked function expressions.

disallowVars (defaults to false) can be enabled to make usage of var an error.

disallowDuplicated (defaults to true) errors on duplicated var definitions in the same function scope.

disallowUnknownReferences (defaults to true) errors on references to unknown global variables.

ast (defaults to false) produces an AST instead of source code (experimental).

stats (defaults to false) prints const/let statistics and renames (experimental).

parse (defaults to null) lets you provide a custom parse function if you use defs as an API. By default it will use require("esprima").parse.

Example

Input example.js:

"use strict";
function fn() {
    const y = 0;
    for (let x = 0; x < 10; x++) {
        const y = x * 2;
        const z = y;
    }
    console.log(y); // prints 0
}
fn();

Output from running defs example.js:

"use strict";
function fn() {
    var y = 0;
    for (var x = 0; x < 10; x++) {
        var y$0 = x * 2;
        var z = y$0;
    }
    console.log(y); // prints 0
}
fn();

defs.js used as a library

npm install defs, then:

const defs = require("defs");
const options = {};
const src = "const x = 1";
const res = defs(src, options);
assert(res.src === "var x = 1");

// you can also pass an AST (with loc and range) instead of a string to defs
const ast = require("esprima").parse(src, {loc: true, range: true});
const res = defs(ast, {ast: true}); // AST-in, AST-out
// inspect res.ast

res object:

{
    src: string // on success
    errors: array of error messages // on errors
    stats: statistics object (toStringable)
    ast: transformed ast // when options.ast is set
}

Compatibility

defs.js strives to transpile your program as true to ES6 block scope semantics as possible while being as maximally non-intrusive as possible.

It can optionally transform loop closures via IIFE's (when possible), if you include "loopClosures": "iife" in your defs-config.json. More info in loop-closures.md.

See semantic-differences.md for other minor differences.

More Repositories

1

ng-annotate

Add, remove and rebuild AngularJS dependency injection annotations
JavaScript
2,028
star
2

jsshaper

an extensible framework for JavaScript syntax tree shaping
JavaScript
192
star
3

jekyll-references

Jekyll Markdown references plugin
Ruby
43
star
4

ast-traverse

simple but flexible AST traversal with pre and post visitors
JavaScript
38
star
5

stringmap

Fast and robust stringmap for JavaScript
JavaScript
35
star
6

dart-range

Dart
11
star
7

node-harmony-wrapper

A node --harmony shell wrapper
Shell
10
star
8

tryor

return fn() or default value (in case of exception)
JavaScript
8
star
9

dart-scanner

Dart scanner implemented in Dart, ported from the dartc Java implementation
8
star
10

alter

Alters a string by replacing multiple range fragments in one fast pass
JavaScript
7
star
11

simple-is

Maximally minimal type-testing library for JavaScript
JavaScript
6
star
12

simple-fmt

Maximally minimal string formatting library for JavaScript
JavaScript
6
star
13

ordered-ast-traverse

Simple but flexible lexically ordered AST traversal with pre and post visitors
JavaScript
6
star
14

stringset

Fast and robust stringset for JavaScript
JavaScript
6
star
15

breakable

Break out of functions, recursive or not, in a more composable way than by using exceptions explicitly. Non-local return.
JavaScript
6
star
16

ordered-esprima-props

Lexical ordering of property names per Esprima AST type
JavaScript
3
star
17

fdelta

command-line tools for creating and applying delta files in Fossil delta format
C
2
star
18

vectorspace

Vectors are fun. Written for no particular reason other than trying out Ruby. Is likely pretty redundant, overall. Written on Ruby 1.9.1-preview 1 but should be mostly Ruby 1.8.x compatible. Again, vectors are fun.
Ruby
2
star
19

hookhttp

hook node.js require() via HTTP experiment
JavaScript
1
star
20

strto

strto is a strict string-to-number conversion library
JavaScript
1
star
21

jsoncomma

JSON with trailing commas for happy diffs
JavaScript
1
star