Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Setup jshint and pre-commit checks
node_modules
public/components
public/dist
{
"bitwise": false,
"immed": true,
"newcap": true,
"noarg": true,
"noempty": true,
"nonew": true,
"trailing": true,
"boss": true,
"eqnull": true,
"expr": true,
"globalstrict": true,
"laxbreak": true,
"loopfunc": true,
"sub": true,
"undef": true,
"eqeqeq": true,
"indent": 2,
"freeze": true,
"latedef": true,
"quotmark": "single",
"unused": true,
"maxparams": 7,
"maxdepth": 3,
"maxstatements": 20,
"maxlen": 100,
"curly": true,
"node": true,
"es5": true,
"browser": true,
"globals": {
"angular": true
}
}

Setup jshint, pre-commit hooks and sublime

  1. Add Sublime-JSHint plugin for sublime.
  2. Add pre-commit hooks
$ gem install pre-commit && \
  gem install execjs && \
  pre-commit install && \
  pre-commit enable yaml checks jshint console_log white_space json tabs

This will write a file to config/pre_commit.yml. 3. Add the .jshintrc and .jshintignore files to your project root. 4. Try to commit a js file that has console.log for example. 5. Please add these sublime preferences 6. Add this 'use strict'; sublime snippet. Just copy and paste it in

~/Library/Application\ Support/Sublime\ Text\ 3/Packages/User/
{
"draw_indent_guides": true,
"ensure_newline_at_eof_on_save": true,
"find_selected_text": true,
"font_face": "Menlo",
"font_options":
[
"subpixel_antialias"
],
"font_size": 13,
"highlight_line": true,
"highlight_modified_tabs": true,
"move_to_limit_on_up_down": true,
"rulers":
[
0,
80
],
"scroll_past_end": true,
"shift_tab_unindent": true,
"tab_size": 2,
"translate_tabs_to_spaces": true,
"trim_trailing_white_space_on_save": true,
"word_wrap": true
}

Style

  1. Recommend to follow this style https://github.com/stephenplusplus/javascript-style
  2. Chained methods should be split into multiple lines
// Not preferred
survey.sync()
  .filter(function (survey) {
    return record.expired;
  })
  .map(function (survey) {
    survey.records = survey.data.length;
    return survey;
  })
  .save();
// Prerred 
var expired = function (survey) {
  return record.expired;
};
var counts = function (survey) {
  survey.records = survey.data.length;
  return survey;
};

survey.sync()
  .filter(expired)
  .map(counts)
  .save();
  1. Multiple var statements
  2. if else
if (err) {
  return res.json(400, { errors: ['There are no incidents']  });
}

// if you have a large expression to evaluate, write a method
var executable = function () {
  return notChanged &&
    !survey.changed && 
    (!survey.expired || survey.expires === 'never');
};

if (survey.changed) {
  // do something
} else if (executable()) {
  // do this
} else {
  // do something else
}
  1. Ternary operators
var exp = this.val1 < this.val2
  ? 'good'
  : 'bad';
  1. Spacing and other preferred practices
// Good
if (err)  
// Bad
if(err)   

// Good
function () {}  
// Bad
function(){}    

// Good
var sum = val1 + val2     
// Bad
var sum = val1+val2       

// Bad
var path = '/organizations/' + organization._id + '/surveys/' + survey._id 
// Good
var path = [
  '/organizations',
  organization._id,
  'surveys',
  survey._id
].join('/');

// Good
function expired () {
  return survey.expires < new Date();
}
// Bad
function expired () {
  return survey.expires < new Date();
  ? true
  : false;
}
  1. Precendence for requiring
var http = require('http');                       // native modules first
var mongoose = require('mongoose');               // npm modules second
var config = require('config');                   // modules that are required via NODE_PATH third
var fmt = require('./format-date');               // local modules fourth
var oauthHelper = require('../../oauth-helper');  // relative modules last
  1. snake_case for naming database attributes
  2. camelCase for variables and methods.
  3. Constructors start with caps.
  4. file-names and folder-names must use hyphens and not underscores or caps.
<snippet>
<content><![CDATA[
'use strict';
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>us</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<scope>source.js</scope>
</snippet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.