Warning
Although this project should still work reasonably well, maintenance is no longer intended for this project since ESLint 2.x now supports the same functionality by using --fix
command line flag with the semi
rule.
Semi
To semicolon or not to semicolon; that is the question
Add/remove semicolons from your JavaScript. Supports full ES6!
Why???
Because style.
On a more serious note, some people (including me) feel more comfortable and productive when writing JavaScript without semicolons. (If you disagree, read this) However, this is often not viable in a team environment, or when working on a project with established code style requirements.
As a solution to this problem, Semi can add semicolons to files written in a semicolon-less style, and can also remove semicolons from those written with semicolons. This allows you to write code in the style you like and just auto convert it before committing your code.
It was originally implemented by hacking jshint and now by using a custom rule for ESLint. Semi 100% preserves your original code formatting (other than semicolons). It even takes care of special cases where a newline semicolon is needed (see below).
Usage
CLI
npm install -g semi
Usage: semi [add|rm] [files...] [--out dir] [--leading] [--silent]
Options:
--out Output directory. If not specified, will overwrite original.
--leading Always add leading semicolons for lines that start with +-[(/.
--silent Suppress output messages.
API
var semi = require('semi')
// handle errors
semi.on('error', function (err) { /* ... */ })
// semi.add(<String>)
var jsWithSemicolons = semi.add(jsWithoutSemicolons)
// semi.remove(<String>)
var jsWithoutSemicolons = semi.remove(jsWithSemicolons, {
leading: true
})
There's also Semi for SublimeText3!
It doesn't work!
If it doesn't seem to do anything, it's most likely because your code contains syntax errors.
Special Cases
Semi will automatically convert between the following two cases (also for newlines that start with [
, +
, -
or a regex literal):
// A
var a = b;
(function () {
/* ... */
})()
// B
var a = b
;(function () {
/* ... */
})()
When leading
option is true, it will add a leading semicolon for all newlines that start with one of those special tokens, even if the code is valid without the semicolon:
var a = 123;
++a;
// converts to:
var a = 123
;++a
However, it will not do anything to the following, because it is valid JavaScript and Semi cannot safely assume you wanted a semicolon there.
var a = b
(function () {
/* ... */
})()
But Semicolons Are REQUIRED!!!
Semicolons in JavaScript are indeed optional. Now before you start to talk about how semicolons improve readibility simply because you also write other C-style languages, try to realize that JavaScript is not C, nor is it Java. Its semicolons are designed to be optional and dropping them can be a productivity boon for some people, including me. In fact, languages like Go and Groovy also have optional semicolons and the convention is not using them. You can even use semicolons in Ruby and Python, but obviously nobody does that because it's actually very easy to identify EOL as the end of a statement where it makes sense.
Now, if you are adding semicolons everywhere because you fear dropping them can cause mysterious bugs in some weird browsers, break minifiers, or the rules are simply too hard to remember, you are doing it wrong. The correct way to deal with FUD is to confront them and understand the root cause. You should read these following articles: