Skip to content

Commit 1916cb9

Browse files
authored
chore: fixup linter commands (#940)
1 parent fc4585f commit 1916cb9

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@
344344
"predev:incremental": "node-gyp configure build -C test --debug",
345345
"dev:incremental": "node test",
346346
"doc": "doxygen doc/Doxyfile",
347-
"lint": "node tools/clang-format.js",
348-
"lint:fix": "git-clang-format '*.h', '*.cc'"
347+
"lint": "node tools/clang-format",
348+
"lint:fix": "node tools/clang-format --fix"
349349
},
350350
"pre-commit": "lint",
351351
"version": "3.1.0",

tools/clang-format.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,31 @@ const filesToCheck = ['*.h', '*.cc'];
77
const CLANG_FORMAT_START = process.env.CLANG_FORMAT_START || 'main';
88

99
function main(args) {
10+
let fix = false;
11+
while (args.length > 0) {
12+
switch (args[0]) {
13+
case '-f':
14+
case '--fix':
15+
fix = true;
16+
default:
17+
}
18+
args.shift();
19+
}
20+
1021
let clangFormatPath = path.dirname(require.resolve('clang-format'));
1122
const options = ['--binary=node_modules/.bin/clang-format', '--style=file'];
23+
if (fix) {
24+
options.push(CLANG_FORMAT_START);
25+
} else {
26+
options.push('--diff', CLANG_FORMAT_START);
27+
}
1228

1329
const gitClangFormatPath = path.join(clangFormatPath,
1430
'bin/git-clang-format');
1531
const result = spawn('python', [
1632
gitClangFormatPath,
1733
...options,
18-
'--diff',
19-
CLANG_FORMAT_START,
20-
'HEAD',
34+
'--',
2135
...filesToCheck
2236
], { encoding: 'utf-8' });
2337

@@ -27,13 +41,19 @@ function main(args) {
2741
}
2842

2943
const clangFormatOutput = result.stdout.trim();
44+
// Bail fast if in fix mode.
45+
if (fix) {
46+
console.log(clangFormatOutput);
47+
return 0;
48+
}
49+
// Detect if there is any complains from clang-format
3050
if (clangFormatOutput !== '' &&
3151
clangFormatOutput !== ('no modified files to format') &&
3252
clangFormatOutput !== ('clang-format did not modify any files')) {
3353
console.error(clangFormatOutput);
34-
const fixCmd = '"npm run lint:fix"';
54+
const fixCmd = 'npm run lint:fix';
3555
console.error(`
36-
ERROR: please run ${fixCmd} to format changes in your commit
56+
ERROR: please run "${fixCmd}" to format changes in your commit
3757
Note that when running the command locally, please keep your local
3858
main branch and working branch up to date with nodejs/node-addon-api
3959
to exclude un-related complains.

0 commit comments

Comments
 (0)