Recent Changes - Search:

edit SideBar

JSFormatters

See Also

As part of software engineering in Ptolemy II, it would be good if we could clean the JS code.

Unfortunately, it seems like the Eclipse JSDT is not available as part of Eclipse Cleanup for multiple files. Instead, I have to go to each file and clean it up individually.

Features

Required

  • Easy to run as part of release engineering code cleaning either from the command line or using Eclipse

Nice to have

  • Easy for users to invoke

js-beautify

To install and run:

npm install js-beautify
svn co https://repo.eecs.berkeley.edu/svn/projects/terraswarm/accessors/trunk/accessors
cd accessors
~/node_modules/js-beautify/js/bin/js-beautify.js --end-with-newline --replace --quiet --jslint-happy ./web/audio/AudioCapture.js
svn diff web/audio/AudioCapture.js

The options are

--end-with-newline
End output with newline
--replace
Write output in-place, replacing input
--quiet
Suppress logging to stdout
--jslint-happy
Enable jslint-stricter mode

To run on all the files, first generate a list of files, see JSCleaning -> List of JS Files, then run

$PTII/adm/bin/ptIItxtfiles >& /tmp/f
cat /tmp/jss | xargs ~/node_modules/js-beautify/js/bin/js-beautify.js --end-with-newline --replace --quiet --jslint-happy

Eclipse

Eclipse has a http://eclipse.org/webtools/jsdt/ that can be installed in Eclipse:

  1. In Eclipse, go to Help -> New Software
  2. Under Work with:, select the release website, such as Luna
  3. The list of available software packages will download, the Name and Version list will state Pending
  4. After the the list of available software packages has downloaded, search for JavaScript
  5. Under Programming Development Tools, select JavaScript Development Tools
  6. Click through the license box and then restart Eclipse.

Up Eclipse Style

It is important to set up the JavaScript style to follow the Ptolemy II coding conventions.

  1. Eclipse -> Preferences -> JavaScript
    1. If JavaScript is not present, then it might be necessary to view the ptII project using Window -> Open Perspective -> Other -> JavaScript
  2. CodeStyle -> Formatter
  3. New, then enter a name like ptIIJSFormatter
  4. Under Indentation/General Settings, change Tab Policy to Spaces only
  5. Under New Lines/Insert new line, check at end of file. This is done so that we can use shell scripts on .js files.
  6. Click OK

Individual .js files can be viewed within the ptII Java project and formatted.

Cleanup

It is not clear how to clean up all the .js files automatically

  • I tried creating a JavaScript project, but that did not have a cleanup option that covered all the .js files.

Emacs

Emacs includes a Javascript mode that can be used to format .js files. For many years, we use the Emacs Java Style to format .java files, see $PTII/util/testsuite/jindent:

#! /bin/sh                                                                                                                        
# -[Mon Feb 28 08:52:07 2005 by cxh]-                                                                                              
# Indent Java code using emacs                                                                                                    
# Usage: jindent [-n] [-d]                                                                                                        
#    -n Print what would happen, but don't checkout or modify files                                                                
#                                                                                                                                  
# The Emacs  CC-mode FAQ says:                                                                                                    
#     *Q.* *How do I re-indent the whole file?*                                                                                    
#                                                                                                                                  
#     *A.* Visit the file and hit `C-x h' to mark the whole buffer. Then                                                          
#     hit `<ESC> C-\'.                                                                                                            
#                                                                                                                                  
# C-x h mark-whole-buffer                                                                                                          
# ESC C-\ indent-region                                                                                                            
#(fset 'jindent                                                                                                                    
#   "\C-xh\C-[\C-\\")                                                                                                              

printonly=no

if [ "$1" = "-n" ]; then
    printonly=yes;
    shift
fi

emacs=emacs
if [ "$PTII" = "" ]; then
    echo "$0: \$METRO is not set, exiting"
    exit 4
fi

jindent_el=$PTII/util/lisp/jindent.el

if [ ! -f "$jindent_el" ]; then
    echo "$0: '$jindent_el' not found, exiting"
    exit 5
fi

BTW - $PTII/util/lisp/jindent.el loads the Ptolemy II Java Style.

;; jindent.el, used by $PTII/util/testsuite/jindent to                                                                            
;; indent Java files to the Ptolemy II standard                                                                                    
;; Version: $Id: jindent.el 22383 2002-05-04 20:57:09Z cxh $                                                                      

(load (expand-file-name
       (concat (getenv "PTII") "/util/lisp/ptjavastyle.el")))

(defun jindent ()
  (java-mode)
  (indent-region (point-min) (point-max) 'nil)
  (save-buffer)
)

  • Back to JS
Edit - History - Print - Recent Changes - Search
Page last modified on November 22, 2016, at 01:30 PM