You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The place to start for the Punchcard JavaScript Style Guide is the Airbnb JavaScript Style Guide. We follow the Airbnb Style Guide with the following modifications, presented below. Most modifications are clarifications of existing rules where Airbnb does not specify anything, or expanding the existing rules for additional usecases we have.
Best Practices
Curly Brace Conventions
Curly braces should never omitted, even when they are optional.
//////////////////////////////// OK//////////////////////////////if(foo){foo++;}while(bar){baz();}if(foo){baz();}else{qux();}//////////////////////////////// Not OK//////////////////////////////if(foo)foo++;/*error Expected { after 'if' condition.*/while(bar)/*error Expected { after 'while' condition.*/baz();if(foo){/*error Expected { after 'else'.*/baz();}elsequx();
//////////////////////////////// OK//////////////////////////////varfoo=object.property;varbar=object.property;//////////////////////////////// Not OK//////////////////////////////varfoo=object./*error Expected dot to be on same line as property.*/property;
No Invalid This
No usage of this outside of classes or class-like objects
//////////////////////////////// OK//////////////////////////////functionFoo(){//this is in a legacy style constructor.this.a=0;baz(()=>this);}classFoo{constructor(){// this is in a constructor.this.a=0;baz(()=>this);}}varobj={foo: functionfoo(){// this is in a method (this function is on object literal).this.a=0;}};//////////////////////////////// Not OK//////////////////////////////this.a=0;/*error Unexpected `this`.*/baz(()=>this);/*error Unexpected `this`.*/(function(){this.a=0;/*error Unexpected `this`.*/baz(()=>this);/*error Unexpected `this`.*/})();functionfoo(){this.a=0;/*error Unexpected `this`.*/baz(()=>this);/*error Unexpected `this`.*/}
No Iterator
No usage of the __iterator__ property on item's prototype.
//////////////////////////////// OK//////////////////////////////var__iterator__=foo;// Not using the `__iterator__` property.//////////////////////////////// Not OK//////////////////////////////Foo.prototype.__iterator__=function(){/*error Reserved name '__iterator__'.*/returnnewFooIterator(this);};foo.__iterator__=function(){};/*error Reserved name '__iterator__'.*/foo["__iterator__"]=function(){};/*error Reserved name '__iterator__'.*/
Node
Return After Callback
Always return a callback() if it's called outside the main function body.
//////////////////////////////// OK//////////////////////////////functionfoo(){if(err){returncallback(err);}callback();}//////////////////////////////// Not OK//////////////////////////////functionfoo(){if(err){callback(err);/*error Expected return with your callback function.*/}callback();}
Global Require
All require() statements must be at the top-level module scope.
//////////////////////////////// OK//////////////////////////////// all these variations of require() are okrequire('x');vary=require('y');varz;z=require('z').initialize();// requiring a module and using it in a function is okvarfs=require('fs');functionreadFile(filename,callback){fs.readFile(filename,callback)}//////////////////////////////// Not OK//////////////////////////////// calling require() inside of a function is not allowedfunctionreadFile(filename,callback){varfs=require('fs');/*error Unexpected require().*/fs.readFile(filename,callback)}// conditional requires like this are also not allowedif(DEBUG){require('debug');}/*error Unexpected require().*/// a require() in a switch statement is also flaggedswitch(x){case'1': require('1');break;}
Handle Callback Error
All errors must be handled in function callbacks and not allowed to fall through or "get eaten".
//////////////////////////////// OK//////////////////////////////functionloadData(err,data){if(err){console.log(err.stack);}doSomething();}functiongenerateError(err){if(err){}}//////////////////////////////// Not OK//////////////////////////////functionloadData(err,data){/*error Expected error to be handled.*/doSomething();}
No Mixed Requires
Do not mix require requires statements. Group core module requires separate from requires from file module requires, installed module requires, computed module requires, uninitialized declarations, and any other declarations.
//////////////////////////////// OK//////////////////////////////// only non-require declarationsvarfoo=42,bar='baz';// always valid regardless of grouping because all declarations are of the same typevarfoo=require('foo'+VERSION),bar=require(getBarModuleName()),baz=require();//////////////////////////////// Not OK//////////////////////////////// invalid because of mixed types "core" and "file"varfs=require('fs'),/*error Do not mix core, module, file and computed requires.*/async=require('async');// invalid because of mixed types "file" and "unknown"varfoo=require('foo'),/*error Do not mix core, module, file and computed requires.*/bar=require(getBarModuleName());
Disallow New Require
Disallow the use of new directly with a require() statement.
//////////////////////////////// OK//////////////////////////////varAppHeader=require('app-header');//////////////////////////////// Not OK//////////////////////////////varappHeader=newrequire('app-header');/*error Unexpected use of new with require.*/
No Path Concat
Require the use of path.join() to create paths, disallowing simple string concatenation to do so, when using __dirname and __filename
//////////////////////////////// OK//////////////////////////////varfullPath=dirname+'/foo.js';varfullPath=path.resolve(__dirname,'foo.js');varfullPath=path.join(__filename,'foo.js');//////////////////////////////// Not OK//////////////////////////////varfullPath=__dirname+'/foo.js';/*error Use path.join() or path.resolve() instead of + to create paths.*/varfullPath=__filename+'/foo.js';/*error Use path.join() or path.resolve() instead of + to create paths.*/
//////////////////////////////// OK//////////////////////////////Process.exit();varexit=process.exit;//////////////////////////////// Not OK//////////////////////////////process.exit(1);/*error Don't use process.exit(); throw an error instead.*/process.exit(0);/*error Don't use process.exit(); throw an error instead.*/
Style
Brace Style
Always use Stroustrup style bracing, and never allow a single single line blocks.
//////////////////////////////// OK//////////////////////////////functionfoo(){returntrue;}if(foo){bar();}else{baz();}//////////////////////////////// Not OK//////////////////////////////functionfoo(){returntrue;}if(foo){bar();}else{baz();}if(foo){bar();}
Camelcase
Always require camelCase naming, even in property names
//////////////////////////////// OK//////////////////////////////varmyFavoriteColor="#112C85";var_myFavoriteColor="#112C85";varmyFavoriteColor_="#112C85";varMY_FAVORITE_COLOR="#112C85";varfoo=bar.baz_boom;varfoo={qux: bar.baz_boom};obj.do_something();//////////////////////////////// Not OK//////////////////////////////varmy_favorite_color="#112C85";/*error Identifier 'my_favorite_color' is not in camel case.*/functiondo_something(){/*error Identifier 'do_something' is not in camel case.*/// ...}obj.do_something=function(){/*error Identifier 'do_something' is not in camel case.*/// ...};varobj={my_pref: 1/*error Identifier 'my_pref' is not in camel case.*/};
Consistent This
If you need to track this for nested context, always use a variable named _this;
//////////////////////////////// OK//////////////////////////////var_this=this;varself=64;varthat=42;varthat;_this=this;foo.bar=this;//////////////////////////////// Not OK//////////////////////////////var_this=42;/*error Designated alias '_this' is not assigned to 'this'.*/varthat=this;/*error Unexpected alias 'that' for 'this'.*/_this=42;/*error Designated alias '_this' is not assigned to 'this'.*/that=this;/*error Unexpected alias 'that' for 'this'.*/
Function Style
Always declare functions as expressions, never as a declaration
//////////////////////////////// OK//////////////////////////////varfoo=function(){// ...};constbar=()=>{};//////////////////////////////// Not OK//////////////////////////////functionfoo(){/*error Expected a function expression.*/// ...}
Indentation
Always indent with two spaces. When writing multiple declarations of var and let with one keyword, and switch statement keywords, indent twice.
//////////////////////////////// OK//////////////////////////////varfoo,bar,baz;//////////////////////////////// Not OK//////////////////////////////varfoo,bar,baz;
//////////////////////////////// OK//////////////////////////////vara='a',// \nb='b';// \n// \nfunctionfoo(params){// \n// do stuff \n}// \n//////////////////////////////// Not OK//////////////////////////////vara='a';// \r\n /*error Expected linebreaks to be 'LF' but found 'CRLF'.*/vara='a',// \r\n /*error Expected linebreaks to be 'LF' but found 'CRLF'.*/b='b';// \n
Lines Around Comments
Always require a new line before a comment. Comments starting new blocks, objects, or arrays do not need a new line before them
//////////////////////////////// OK//////////////////////////////varfoo=function(){// This comment is OKbar();/* This comment is OK too */baz();// This comment is awesomequx();constwaldo={/* This is cool */shirt: stripes};letgarfield=[// As is this'fat','cat'];}//////////////////////////////// Not OK//////////////////////////////varfoo=function(){bar();/* This comment is Not OK */baz();// This comment is not awesomequx();}
Max Depth
Be cautious of nesting expressions more than 4 levels deep
//////////////////////////////// OK//////////////////////////////varfoo=function(){varbar,baz;letqux;constwaldo;}//////////////////////////////// Not OK//////////////////////////////varfoo=function(){varbar;varbaz;/*error Combine this with the previous 'let' statement.*/letqux,/*error Split 'let' declarations into multiple statements.*/quarx;constwaldo;/*error Split 'const' declarations into multiple statements.*/}
Padded Blocks
Never pad your blocks with beginning or ending spaces
//////////////////////////////// OK//////////////////////////////if(a){b();}//////////////////////////////// Not OK//////////////////////////////if(a){/*error Block must not be padded by blank lines.*/b();}/*error Block must not be padded by blank lines.*/if(a){/*error Block must not be padded by blank lines.*/b();}/*error Block must not be padded by blank lines.*/if(a){/*error Block must not be padded by blank lines.*/b();}/*error Block must not be padded by blank lines.*/
Quote Properties
Keep property quoting consistent within an object.
//////////////////////////////// OK//////////////////////////////if(foo){// ...}else{}(function(){})()<FooonClick={functionbar(){}}/>for(letfooof['bar','baz','qux']){}//////////////////////////////// Not OK//////////////////////////////if(foo){// ...}else{}/*error Missing space before keyword "else".*/constfoo='bar';letbaz='qux';/*error Missing space before keyword "let".*/varfoo=functionbar(){}/*error Missing space before keyword "function".*/functionbar(){if(foo){return;}/*error Missing space before keyword "return".*/}
Spaces Before/After Unary Operations
Always include a space around unary word operators (such as new, delete, typeof, void, yield), but not around nonword operators (such as -, +, --, ++, !, !!).
//////////////////////////////// OK//////////////////////////////// Word unary operator "delete" is followed by a whitespace.deletefoo.bar;// Word unary operator "new" is followed by a whitespace.newFoo;// Word unary operator "void" is followed by a whitespace.void0;// Unary operator "++" is not followed by whitespace.++foo;// Unary operator "--" is not preceeded by whitespace.foo--;// Unary operator "-" is not followed by whitespace.-foo;// Unary operator "+" is not followed by whitespace.+"3";//////////////////////////////// Not OK//////////////////////////////typeof!foo;/*error Unary word operator "typeof" must be followed by whitespace.*/void{foo:0};/*error Unary word operator "void" must be followed by whitespace.*/new[foo][0];/*error Unary word operator "new" must be followed by whitespace.*/delete(foo.bar);/*error Unary word operator "delete" must be followed by whitespace.*/function*foo(){yield(0)/*error Unary word operator "yield" must be followed by whitespace.*/}++foo;/*error Unexpected space after unary operator "++".*/foo--;/*error Unexpected space before unary operator "--".*/-foo;/*error Unexpected space after unary operator "-".*/+"3";/*error Unexpected space after unary operator "+".*/
Space
Always require a space after an initializing a comment with // or /*. Characters -, +, /, and * do not need any spacing and are exceptions. Characters = and ! do not need a space before them, but need a space after them, and are markers.
//////////////////////////////// OK////////////////////////////////--------------// Comment block//--------------//++++++++++++++// Comment block//++++++++++++++////////////////// Comment block/////////////////**************** * Comment block ****************///= This is a comment with a marker//! This is a comment with a marker//////////////////////////////// Not OK////////////////////////////////=This is a comment with a marker /*error Expected space or tab after // in comment.*///!This is a comment with a marker /*error Expected space or tab after // in comment.*//*-+-+-+-+-+-+-+*//*error Expected space or tab after /* in comment.*/// Comment block/*-+-+-+-+-+-+-+*//*error Expected space or tab after /* in comment.*//*------++++++++*//*error Expected exception block, space or tab after /* in comment.*//* Comment block *//*------++++++++*//*error Expected exception block, space or tab after /* in comment.*/