| 1 |
#!/usr/bin/env node |
| 2 |
/* |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
*/ |
| 7 |
|
| 8 |
/* |
| 9 |
* Copyright (c) 2014, Joyent, Inc. |
| 10 |
*/ |
| 11 |
|
| 12 |
/* |
| 13 |
* bashstyle: check bash scripts for adherence to style guidelines, including: |
| 14 |
* |
| 15 |
* o no lines longer than 80 characters |
| 16 |
* o file does not end with a blank line |
| 17 |
* o Do not use 'local' and var initialization *using a subshell* in the |
| 18 |
* same statement. See |
| 19 |
* <http://www.tldp.org/LDP/abs/html/localvar.html#EXITVALANOMALY01> |
| 20 |
* for why not. Arguably this belongs in a separate 'bashlint'. |
| 21 |
* |
| 22 |
* Future enhancements could include: |
| 23 |
* o indents consistent with respect to tabs, spaces |
| 24 |
* o indents consistently sized (all are some multiple of the smallest |
| 25 |
* indent, which must be a tab or 4 or 8 spaces) |
| 26 |
*/ |
| 27 |
|
| 28 |
var VERSION = '2.0.0'; |
| 29 |
|
| 30 |
var mod_assert = require('assert'); |
| 31 |
var mod_fs = require('fs'); |
| 32 |
|
| 33 |
var nerrors = 0; |
| 34 |
|
| 35 |
main(); |
| 36 |
process.exit(0); |
| 37 |
|
| 38 |
function main() |
| 39 |
{ |
| 40 |
var files = process.argv.slice(2); |
| 41 |
|
| 42 |
if (files.length === 0) { |
| 43 |
console.error('usage: %s file1 [...]', |
| 44 |
process.argv.slice(0, 2).join(' ')); |
| 45 |
process.exit(2); |
| 46 |
} |
| 47 |
|
| 48 |
files.forEach(checkFile); |
| 49 |
|
| 50 |
if (nerrors != 0) |
| 51 |
process.exit(1); |
| 52 |
} |
| 53 |
|
| 54 |
function checkFile(filename) |
| 55 |
{ |
| 56 |
var text = mod_fs.readFileSync(filename, 'utf-8'); |
| 57 |
var lines = text.split('\n'); |
| 58 |
var i; |
| 59 |
var styled = false; |
| 60 |
var styleStart; |
| 61 |
|
| 62 |
mod_assert.ok(lines.length > 0); |
| 63 |
|
| 64 |
/* |
| 65 |
* Expand tabs in each line and check for long lines. |
| 66 |
*/ |
| 67 |
for (i = 1; i <= lines.length; i++) { |
| 68 |
var line = expandTabs(lines[i - 1]); |
| 69 |
|
| 70 |
if (i > 1 && lines[i-2].match(/# BASHSTYLED/)) { |
| 71 |
continue; |
| 72 |
} |
| 73 |
|
| 74 |
if (line.match(/# BEGIN BASHSTYLED/)) { |
| 75 |
styleStart = i; |
| 76 |
styled = true; |
| 77 |
} |
| 78 |
|
| 79 |
if (line.match(/# END BASHSTYLED/)) { |
| 80 |
if (styled != true) { |
| 81 |
nerrors++; |
| 82 |
console.log('%s: %d: END BASHSTYLED w/o corresponding BEGIN', |
| 83 |
filename, i); |
| 84 |
} |
| 85 |
styled = false; |
| 86 |
} |
| 87 |
|
| 88 |
if (!styled && line.match(/^\s*local\s+(\w+)\s*=.*\$\(/)) { |
| 89 |
nerrors++; |
| 90 |
var m = line.match(/^\s*local\s+(\w+)\s*=/); |
| 91 |
console.log('%s: %d: declaring and setting a "local" ' + |
| 92 |
'var in the same statement ' + |
| 93 |
'ignores a subshell return code ' + |
| 94 |
'<http://www.tldp.org/LDP/abs/html/localvar.html#EXITVALANOMALY01>: ' + |
| 95 |
'local %s=...', |
| 96 |
filename, i, m[1]); |
| 97 |
} |
| 98 |
|
| 99 |
// Regexplanation: non-[, [, space (contents) space, ], non-] |
| 100 |
// groups before and after brackets to ease search/replace. |
| 101 |
if (!styled && line.match(/(^|[^\[])\[(\s.+\s)\]([^\]])/)) { |
| 102 |
nerrors++; |
| 103 |
console.log('%s: %d: prefer [[ to [ for tests.', filename, i); |
| 104 |
} |
| 105 |
|
| 106 |
if (!styled && line.length > 80) { |
| 107 |
nerrors++; |
| 108 |
console.log('%s: %d: line exceeds 80 columns', |
| 109 |
filename, i); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
if (styled) { |
| 114 |
nerrors++; |
| 115 |
console.log('%s: %d: BEGIN BASHSTYLED that does not END', |
| 116 |
filename, styleStart); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
/* |
| 121 |
* No sane editor lets you save a file without a newline at the very end. |
| 122 |
*/ |
| 123 |
if (lines[lines.length - 1].length !== 0) { |
| 124 |
nerrors++; |
| 125 |
console.log('%s: %d: file does not end with newline', |
| 126 |
filename, lines.length); |
| 127 |
} |
| 128 |
|
| 129 |
/* |
| 130 |
* Since the file will always end with a newline, the last entry of |
| 131 |
* "lines" will actually be blank. |
| 132 |
*/ |
| 133 |
if (lines.length > 1 && lines[lines.length - 2].length === 0) { |
| 134 |
nerrors++; |
| 135 |
console.log('%s: %d: file ends with a blank line', |
| 136 |
filename, lines.length - 1); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
function expandTabs(text) |
| 141 |
{ |
| 142 |
var out = ''; |
| 143 |
var col = 0; |
| 144 |
var j, k; |
| 145 |
|
| 146 |
for (j = 0; j < text.length; j++) { |
| 147 |
if (text[j] != '\t') { |
| 148 |
out += text[j]; |
| 149 |
col++; |
| 150 |
continue; |
| 151 |
} |
| 152 |
|
| 153 |
k = 8 - (col % 8); |
| 154 |
col += k; |
| 155 |
|
| 156 |
do { |
| 157 |
out += ' '; |
| 158 |
} while (--k > 0); |
| 159 |
|
| 160 |
col += k; |
| 161 |
} |
| 162 |
|
| 163 |
return (out); |
| 164 |
} |