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.
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
-
Indentation:
{ character: '?', width: ? }
Specifies the indentation used for scopes.
character
should be one of: ' ', '\t' andwidth
should be a positive integer, e.g.:{ character: ' ', width: 4 }
or{ character: '\t', width: 1 }
-
LastEmptyLine: present, omitted
Specifies if there should always be an empty line at the end of a file.
-
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")
-
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)
orwhile (foo()) {
->while(foo()) {
-
SpaceAfterAnonymousFunctions: present, omitted
Specifies whether or not there should be a space between function and () in anonymous functions.
function(x) { }
->function (x) { }
-
SpacesAroundOperators: present, omitted
Specifies whether or not there should be spaces around operators such as +,=,+=,>=,!==.
var x = 4;
->var x=4;
ora>=b
->a >= b
ora>>2
->a >> 2
-
TrailingWhitespaces: strip
Specifies whether trailing whitespaces should be stripped from the end of the lines.
-
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( )
->()