Lab7: Add Static Analysis Tools

Introduction

In this week, I started working on adding static analysis tools into my link checker command-line tool named url-fi following the lab7 instruction. You can see the detailed instruction about the lab 7 here: https://github.com/Seneca-CDOT/topics-in-open-source-2020/wiki/lab-7. It said these are the learning goal from this lab:

  • an automatic source code formatter
  • a source code linter
  • command-line or project build scripts to run your static analysis tooling
  • editor/IDE integration of your static analysis tooling
  • write contributor documentation to setup and use your tools


Automatic Source Code Formatter

As the language I tool for my command-line tool is Node.js (JavaScript), I picked Prettier as an automatic source code formatter. Following the document on the Prettier official website, I installed the prettier by entering 'npm install --save-dev --save-exact prettier'. I added '.prettierrc.json' file to let editors and other tooling know I'm using Prettier and write the rule I want to apply on my project on the file. I created '.prettierignore' file to let Prettier know which files I do not want to format.

After I've done all of them, I can format the files using prettier by entering 'npx prettier --write *' or can check whether the files are formatted by entering 'npx prettier --check *'. However, I think it would be good if I create a npm script for running Prettier on the entire project from the command line. Therefore, I added `"prettier": "prettier --write *"` and `"prettier-check": "prettier --check *"` on the 'package.json' file so the user can run the Prettier simply entering 'npm prettier' or 'npm prettier-check'

  "scripts": {
    "prettier""prettier --write *",
    "prettier-check""prettier --check *",
  },

When I've formatted the files, I saw lots of changes on the index.js, README.md, and CONTRIBUTING.md file and most of them are space or semi-colon issues! I was surprised how many spaces and semi-colons I missed following the Prettier rule.


 Linter

The Linter I've chosen is ESLint as my programming language. I followed the installation document on the ESLint official website. I've installed ESlint entering 'npm install eslint --save-dev' and set up a configuration file entering 'npx eslint --init'. Also, I created '.eslintignore' file to let ESLint know I want to ignore the files on the ignore file. When I enter 'npx eslint *', ESLint spots some mistakes I did on my project. I've found a variable I have not used (so I can erase it) and an unnecessary regular expression I wrote (for example, I wrote '/\-\-good/' for goodRegex but I did not need \ for - symbol. Therefore, I can write '--good' for the goodRegex). There are not many 'spots' on my project but it was really interesting!

I also create a npm script for running ESLint on the package.json file so the users can simply run ESLint when they enter 'npm eslint'

  "scripts": {
    "prettier""prettier --write *",
    "prettier-check""prettier --check *",
    "eslint""eslint --ignore-path .eslintignore *"
  },


Editor/IDE integration

I would be nice if the program automatically format using Prettier or find spots using ESLint. I created '.vscode' folder and created 'extensions.json' and 'settings.json' files to do this.

This is the code I wrote on extensions.json file

{
  "recommendations": ["dbaeumer.vscode-eslint""esbenp.prettier-vscode"]
}

This is the code I wrote on settings.json file

{
  "editor.defaultFormatter""esbenp.prettier-vscode",
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint"true
  }
}

It was challenging to integrate Prettier into my editor. When I wrote "editor.defaultFormatter""esbenp.prettier-vscode", it kept saying "Defines a default formatter which takes precedence over all other formatter settings.Must be the identifier of an extension contributing a formatter." with the underline. I've searched how to fix it and got the reason. I needed to install the Prettier extension on the VSCode and let VSCode know I want to use 'esbenp.prettier-vscode' as a default formatter! When I've installed 'Prettier' extension andy set the default formatter (file > preferences > settings > find 'default formatter' > pick 'esbenp.prettier-vscode', it solved!


What I've learned

Learning these tools were really amazing! All the practices using Prettier, ESLint, and Editor Integration were useful and interesting. If I need to pick one tool I got the biggest interest in, I'll pick Prettier. I knew the Prettier extension on the VSCode and usually use this extension for my personal website project but I did not know I can use it with npm command or can use it automatically. When I had a group project, the formatting was one issue I've got. I got the confidence I will not have the same issue in the future thanks to this week's practice!

Comments

Popular posts from this blog

Open Source Project: Lab2 (Pull Request)

Release 4.0 - Part 3: Release

Lab 8: Automated Testing and Continuous Integration