Skip to content

Coding Style

Motohiko Makino edited this page Feb 21, 2019 · 1 revision

Table of Contents

ESLint

Configuration File

  • .eslintrc.js (or .eslintrc) is placed under the project home (Project-2) directory
    • This file can be generated by running ./node_modules/.bin/eslint --init for the first time
    • TO-DO
      • In order to have consistent style and formatting, we probably want to discuss and agree on style and share the same .eslintrc.js kept in the git repository.
        1. Single or Double quote
        2. CR/LF(Windows) or LF(Unix/MacOS)
        3. ... and so on
     $cat .eslintrc.js
     module.exports = {
         "env": {
             "browser": true,
             "es6": false
         },
         "extends": "eslint:recommended",
         "parserOptions": {
             "ecmaVersion": 2015
         },
         "rules": {
             "indent": [
                 "error",
                 "tab"
             ],
             "linebreak-style": [
                 "error",
                 "unix"
             ],
             "quotes": [
                 "error",
                 "single"
             ],
             "semi": [
                 "error",
                 "always"
             ]
         }
     };

Configuring package.json for Running ESLint

  • package.json needs to be revised
    • Current
       "scripts": {
         "start": "node server.js",
         "lint": "eslint **/*.js --quiet",
         "fix": "eslint --fix .",
         "test": "npm run lint && cross-env NODE_ENV=test mocha -u tdd --reporter spec --exit"
       },
    • Revise
       "scripts": {
         "start": "node server.js",
         "lint": "./node_modules/.bin/eslint *.js config models routes public/js --quiet",
         "fix": "./node_modules/.bin/eslint --fix .",
         "test": "npm run lint && node ./node_modules/cross-env/dist/bin/cross-env-shell.js NODE_ENV=test node_modules/mocha/bin/mocha --recursive -u tdd --reporter spec --exit",
         "test-only": "node ./node_modules/cross-env/dist/bin/cross-env-shell.js NODE_ENV=test node_modules/mocha/bin/mocha --recursive -u tdd --reporter spec --exit"
       },

Running ESLint on Terminal

  • npm run lint -- Run ESLint on javascript files of the project.
  • npm run fix -- Run ESLint's auto error fix



Git Auto Line-Ending Correction

  • When ESLint is run, it might produce a bunch of error messages about line ending.
   1:3   error  Expected linebreaks to be 'LF' but found 'CRLF'    linebreak-style 
  • This is a well know issue from Windows and Unix/MacOS difference. Git can be configured to auto fix this issue.
  • If you are on Windows
    • $ git config --global core.autocrlf true
      • Git can auto-convert CRLF line endings into LF when you add a file to the index, and vice versa when it checks out code onto your filesystem. You can turn on this functionality with the core.autocrlf setting. If you’re on a Windows machine, set it to true — this converts LF endings into CRLF when you check out code.
  • If you are on MacOS
    • $ git config --global core.autocrlf input
      • If you’re on a Linux or Mac system that uses LF line endings, then you don’t want Git to automatically convert them when you check out files; however, if a file with CRLF endings accidentally gets introduced, then you may want Git to fix it. You can tell Git to convert CRLF to LF on commit but not the other way around by setting core.autocrlf to input.