Skip to content

A JavaScript beautifier that can infer coding style from a sample

License

Notifications You must be signed in to change notification settings

s7eph4n/codepainter

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Code Painter

Code Painter is a JavaScript beautifier that instead of asking you to manually specify the desired formatting style, can infer it from a code sample provided by you. This could, for instance, be a code snippet from the same project that your new code is supposed to be integrated with.

It uses the excellent Esprima parser by Ariya Hidayat (thanks!).

The name is inspired by Word's Format Painter, which does a similar job for rich text.

Usage

For now, it can only be used as a command-line tool but a Web version is in the works.

./bin/codepaint -i input.js -s sample.js -o output.js

transforms the input.js file using formatting style from sample.js and writes the output to output.js

-i and -o can both be ommitted, in that case standard I/O streams will be used.

./bin/codepaint -s sample.js < input.js > output.js

The style can still be specified manually with a JSON string as the --style argument:

./bin/codepaint --style '{ "QuoteType": "double" }' < input.js > output.js

Or specify one of the predefined styles (currently only 'mediawiki' supported):

./bin/codepaint --style mediawiki < input.js > output.js

Or a file containing a JSON string:

./bin/codepaint --stylefile < style.json > < input.js > output.js

Supported style properties

  1. Indentation: { character: '?', width: ? }

    Specifies the indentation used for scopes.

    character should be one of: ' ', '\t' and width should be a positive integer, e.g.:

    { character: ' ', width: 4 } or { character: '\t', width: 1 }

  2. LastEmptyLine: present, omitted

    Specifies if there should always be an empty line at the end of a file.

  3. QuoteType: single, double

    Specifies what kind of quoting you would like to use for string literals:

    console.log("Hello world!") -> console.log('Hello world!')

    Adds proper escaping when necessary, obviously.

    console.log('Foo "Bar" Baz') -> console.log("Foo \"Bar\" Baz")

  4. SpaceAfterControlStatements: present, omitted

    Specifies whether or not there should be a space between if/for/while and the following (.

    if(x === 4) -> if (x === 4) or while (foo()) { -> while(foo()) {

  5. SpaceAfterAnonymousFunctions: present, omitted

    Specifies whether or not there should be a space between function and () in anonymous functions.

    function(x) { } -> function (x) { }

  6. SpacesAroundOperators: present, omitted

    Specifies whether or not there should be spaces around operators such as +,=,+=,>=,!==.

    var x = 4; -> var x=4; or a>=b -> a >= b or a>>2 -> a >> 2

  7. TrailingWhitespaces: strip

    Specifies whether trailing whitespaces should be stripped from the end of the lines.

  8. SpacesInParens: present, omitted

    Specifies whether or not there should be spaces inside parenthesis. Empty pairs of parenthesis will always be shortened.

    (x===4) -> ( x===4 ) or ( ) -> ()

About

A JavaScript beautifier that can infer coding style from a sample

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%