Skip to content

Commit 706b199

Browse files
rubiagatramhdawson
authored andcommitted
test: create tools/eslint-format
Improve JS linting and add option to fix linting errors Fixes: #1076 PR-URL: #1080 Reviewed-By: Michael Dawson <[email protected]
1 parent 7423cc5 commit 706b199

File tree

4 files changed

+89
-18
lines changed

4 files changed

+89
-18
lines changed

.github/workflows/linter.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ jobs:
2121
with:
2222
node-version: ${{ matrix.node-version }}
2323
- run: npm install
24-
- run: CLANG_FORMAT_START=refs/remotes/origin/main npm run lint
24+
- run: FORMAT_START=refs/remotes/origin/main npm run lint

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,8 @@
377377
"predev:incremental": "node-gyp configure build -C test --debug",
378378
"dev:incremental": "node test",
379379
"doc": "doxygen doc/Doxyfile",
380-
"lint": "eslint $(git diff --name-only refs/remotes/origin/main '**/*.js' | xargs) && node tools/clang-format",
381-
"lint:fix": "node tools/clang-format --fix && eslint --fix $(git diff --cached --name-only '**/*.js' | xargs && git diff --name-only '**/*.js' | xargs)"
380+
"lint": "node tools/eslint-format && node tools/clang-format",
381+
"lint:fix": "node tools/clang-format --fix && node tools/eslint-format --fix"
382382
},
383383
"pre-commit": "lint",
384384
"version": "4.2.0",

tools/clang-format.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const spawn = require('child_process').spawnSync;
44
const path = require('path');
55

66
const filesToCheck = ['*.h', '*.cc'];
7-
const CLANG_FORMAT_START = process.env.CLANG_FORMAT_START || 'main';
7+
const FORMAT_START = process.env.FORMAT_START || 'main';
88

99
function main (args) {
1010
let fix = false;
@@ -22,19 +22,17 @@ function main (args) {
2222
const clangFormatPath = path.dirname(require.resolve('clang-format'));
2323
const options = ['--binary=node_modules/.bin/clang-format', '--style=file'];
2424
if (fix) {
25-
options.push(CLANG_FORMAT_START);
25+
options.push(FORMAT_START);
2626
} else {
27-
options.push('--diff', CLANG_FORMAT_START);
27+
options.push('--diff', FORMAT_START);
2828
}
2929

30-
const gitClangFormatPath = path.join(clangFormatPath,
31-
'bin/git-clang-format');
32-
const result = spawn('python', [
33-
gitClangFormatPath,
34-
...options,
35-
'--',
36-
...filesToCheck
37-
], { encoding: 'utf-8' });
30+
const gitClangFormatPath = path.join(clangFormatPath, 'bin/git-clang-format');
31+
const result = spawn(
32+
'python',
33+
[gitClangFormatPath, ...options, '--', ...filesToCheck],
34+
{ encoding: 'utf-8' }
35+
);
3836

3937
if (result.stderr) {
4038
console.error('Error running git-clang-format:', result.stderr);
@@ -48,17 +46,19 @@ function main (args) {
4846
return 0;
4947
}
5048
// Detect if there is any complains from clang-format
51-
if (clangFormatOutput !== '' &&
52-
clangFormatOutput !== ('no modified files to format') &&
53-
clangFormatOutput !== ('clang-format did not modify any files')) {
49+
if (
50+
clangFormatOutput !== '' &&
51+
clangFormatOutput !== 'no modified files to format' &&
52+
clangFormatOutput !== 'clang-format did not modify any files'
53+
) {
5454
console.error(clangFormatOutput);
5555
const fixCmd = 'npm run lint:fix';
5656
console.error(`
5757
ERROR: please run "${fixCmd}" to format changes in your commit
5858
Note that when running the command locally, please keep your local
5959
main branch and working branch up to date with nodejs/node-addon-api
6060
to exclude un-related complains.
61-
Or you can run "env CLANG_FORMAT_START=upstream/main ${fixCmd}".`);
61+
Or you can run "env FORMAT_START=upstream/main ${fixCmd}".`);
6262
return 1;
6363
}
6464
}

tools/eslint-format.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env node
2+
3+
const spawn = require('child_process').spawnSync;
4+
5+
const filesToCheck = '*.js';
6+
const FORMAT_START = process.env.FORMAT_START || 'main';
7+
8+
function main (args) {
9+
let fix = false;
10+
while (args.length > 0) {
11+
switch (args[0]) {
12+
case '-f':
13+
case '--fix':
14+
fix = true;
15+
break;
16+
default:
17+
}
18+
args.shift();
19+
}
20+
21+
// Check js files that change on unstaged file
22+
const fileUnStaged = spawn(
23+
'git',
24+
['diff', '--name-only', FORMAT_START, filesToCheck],
25+
{
26+
encoding: 'utf-8'
27+
}
28+
);
29+
30+
// Check js files that change on staged file
31+
const fileStaged = spawn(
32+
'git',
33+
['diff', '--name-only', '--cached', FORMAT_START, filesToCheck],
34+
{
35+
encoding: 'utf-8'
36+
}
37+
);
38+
39+
const options = [
40+
...fileStaged.stdout.split('\n').filter((f) => f !== ''),
41+
...fileUnStaged.stdout.split('\n').filter((f) => f !== '')
42+
];
43+
44+
if (fix) {
45+
options.push('--fix');
46+
}
47+
const result = spawn('node_modules/.bin/eslint', [...options], {
48+
encoding: 'utf-8'
49+
});
50+
51+
if (result.status === 1) {
52+
console.error('Eslint error:', result.stdout);
53+
const fixCmd = 'npm run lint:fix';
54+
console.error(`ERROR: please run "${fixCmd}" to format changes in your commit
55+
Note that when running the command locally, please keep your local
56+
main branch and working branch up to date with nodejs/node-addon-api
57+
to exclude un-related complains.
58+
Or you can run "env FORMAT_START=upstream/main ${fixCmd}".
59+
Also fix JS files by yourself if necessary.`);
60+
return 1;
61+
}
62+
63+
if (result.stderr) {
64+
console.error('Error running eslint:', result.stderr);
65+
return 2;
66+
}
67+
}
68+
69+
if (require.main === module) {
70+
process.exitCode = main(process.argv.slice(2));
71+
}

0 commit comments

Comments
 (0)