mark-hahn / jsw

A translator for an alternate Javascript syntax that uses significant whitespace
MIT License
29 stars 1 forks source link

JSW

An alternate version of Javascript syntax that uses significant whitespace

JSW (Javascript with Significant Whitespace) is a syntax for Javascript, and especially ES6, that allows editing real JS with Coffeescript-like syntax.

The utility in this repository is a bi-directional translator between JS and JSW.

Unlike Coffeescript, JSW does not provide a new language with differing semantics. It is a thin skin over Javascript that only changes the syntax. This JSW utility is a syntax translator, not a compiler or transpiler.

JSW is a great way for Coffeescript users to migrate to real Javascript with ES6.

Sample JSW with translated Javascript

# JSW
-> func1 arg1, arg2                 
  var hash1 = key1: val1            
              key2: val2            
              key3:                 
  block                             
    let x = y                       
    func2 x                         
    if q and z                      
      func1 "This is text spread    
             over two lines."       
// Javascript                                    
function func1 (arg1, arg2) {
  var hash1 = {
    key1: val1,
    key2: val2,
    key3: key3
  }
  {
    let x = y;
    func2(x);
    if (q && z) {
      func1("This is text spread " +
            "over two lines.");
    }
  }
}

Installation

This is not published in npm yet but installation is easy. Just clone this repo and run npm install in the repo folder.

Usage

From the repo folder, run scripts/run -tm test.js to translate test.js to test.jsw with a map. Run scripts/run -fm test.js to translate from the new test.jsw to test.js. Run scripts/run -h to see all run options.

JSW Overall Features

JSW features not in Coffeescript

Coffeescript features not in JSW

JSW to JS Text Mapping

When JS is converted to JSW, a compact set of metadata is added to the bottom of JSW as a comment. This is very similar to how Coffeescript provides a source map. If support for exact JS text matching is not required this can be disabled. In general it can be ignored and the Atom package JSW will hide that metadata when editing.

Status is Pre-Alpha

Not useful yet. The round-trip translation works with format preservation. But the only language translation so far is -> to/from function.

The only thing left to do is hack the parser/generator to finish the new syntax. I use the term "only" liberally (grin).

Helping Development

Clone this repo and run npm install in the repo folder. Edit the code in the src or uglify folder. When you run scripts/run using the Usage instructions above it will automatically compile the coffeescript to javascript before running. This is obviously temporary until the pre-publish build process is added.

Use scripts/runw (run and watch) instead of scripts/run and the test will be run again whenever and source file changes.

Worried about developing in Coffeescript?

Don't be! The whole point of this project is to allow you to work in either coffeescript-like syntax or in real Javascript. As soon as this utility is mature, it will be used to change this project to pure Javascript. Everyone will then be able to work on it the way they want.

License

Copyright by Mark Hahn using the MIT license.