Skip to content

Commit

Permalink
test: create tools/eslint-format
Browse files Browse the repository at this point in the history
Improve JS linting and add option to fix linting errors

Fixes: #1076
PR-URL: #1080
Reviewed-By: Michael Dawson <midawson@redhat.com
  • Loading branch information
rubiagatra authored and mhdawson committed Nov 15, 2021
1 parent 7423cc5 commit 706b199
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: CLANG_FORMAT_START=refs/remotes/origin/main npm run lint
- run: FORMAT_START=refs/remotes/origin/main npm run lint
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@
"predev:incremental": "node-gyp configure build -C test --debug",
"dev:incremental": "node test",
"doc": "doxygen doc/Doxyfile",
"lint": "eslint $(git diff --name-only refs/remotes/origin/main '**/*.js' | xargs) && node tools/clang-format",
"lint:fix": "node tools/clang-format --fix && eslint --fix $(git diff --cached --name-only '**/*.js' | xargs && git diff --name-only '**/*.js' | xargs)"
"lint": "node tools/eslint-format && node tools/clang-format",
"lint:fix": "node tools/clang-format --fix && node tools/eslint-format --fix"
},
"pre-commit": "lint",
"version": "4.2.0",
Expand Down
30 changes: 15 additions & 15 deletions tools/clang-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const spawn = require('child_process').spawnSync;
const path = require('path');

const filesToCheck = ['*.h', '*.cc'];
const CLANG_FORMAT_START = process.env.CLANG_FORMAT_START || 'main';
const FORMAT_START = process.env.FORMAT_START || 'main';

function main (args) {
let fix = false;
Expand All @@ -22,19 +22,17 @@ function main (args) {
const clangFormatPath = path.dirname(require.resolve('clang-format'));
const options = ['--binary=node_modules/.bin/clang-format', '--style=file'];
if (fix) {
options.push(CLANG_FORMAT_START);
options.push(FORMAT_START);
} else {
options.push('--diff', CLANG_FORMAT_START);
options.push('--diff', FORMAT_START);
}

const gitClangFormatPath = path.join(clangFormatPath,
'bin/git-clang-format');
const result = spawn('python', [
gitClangFormatPath,
...options,
'--',
...filesToCheck
], { encoding: 'utf-8' });
const gitClangFormatPath = path.join(clangFormatPath, 'bin/git-clang-format');
const result = spawn(
'python',
[gitClangFormatPath, ...options, '--', ...filesToCheck],
{ encoding: 'utf-8' }
);

if (result.stderr) {
console.error('Error running git-clang-format:', result.stderr);
Expand All @@ -48,17 +46,19 @@ function main (args) {
return 0;
}
// Detect if there is any complains from clang-format
if (clangFormatOutput !== '' &&
clangFormatOutput !== ('no modified files to format') &&
clangFormatOutput !== ('clang-format did not modify any files')) {
if (
clangFormatOutput !== '' &&
clangFormatOutput !== 'no modified files to format' &&
clangFormatOutput !== 'clang-format did not modify any files'
) {
console.error(clangFormatOutput);
const fixCmd = 'npm run lint:fix';
console.error(`
ERROR: please run "${fixCmd}" to format changes in your commit
Note that when running the command locally, please keep your local
main branch and working branch up to date with nodejs/node-addon-api
to exclude un-related complains.
Or you can run "env CLANG_FORMAT_START=upstream/main ${fixCmd}".`);
Or you can run "env FORMAT_START=upstream/main ${fixCmd}".`);
return 1;
}
}
Expand Down
71 changes: 71 additions & 0 deletions tools/eslint-format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env node

const spawn = require('child_process').spawnSync;

const filesToCheck = '*.js';
const FORMAT_START = process.env.FORMAT_START || 'main';

function main (args) {
let fix = false;
while (args.length > 0) {
switch (args[0]) {
case '-f':
case '--fix':
fix = true;
break;
default:
}
args.shift();
}

// Check js files that change on unstaged file
const fileUnStaged = spawn(
'git',
['diff', '--name-only', FORMAT_START, filesToCheck],
{
encoding: 'utf-8'
}
);

// Check js files that change on staged file
const fileStaged = spawn(
'git',
['diff', '--name-only', '--cached', FORMAT_START, filesToCheck],
{
encoding: 'utf-8'
}
);

const options = [
...fileStaged.stdout.split('\n').filter((f) => f !== ''),
...fileUnStaged.stdout.split('\n').filter((f) => f !== '')
];

if (fix) {
options.push('--fix');
}
const result = spawn('node_modules/.bin/eslint', [...options], {
encoding: 'utf-8'
});

if (result.status === 1) {
console.error('Eslint error:', result.stdout);
const fixCmd = 'npm run lint:fix';
console.error(`ERROR: please run "${fixCmd}" to format changes in your commit
Note that when running the command locally, please keep your local
main branch and working branch up to date with nodejs/node-addon-api
to exclude un-related complains.
Or you can run "env FORMAT_START=upstream/main ${fixCmd}".
Also fix JS files by yourself if necessary.`);
return 1;
}

if (result.stderr) {
console.error('Error running eslint:', result.stderr);
return 2;
}
}

if (require.main === module) {
process.exitCode = main(process.argv.slice(2));
}

0 comments on commit 706b199

Please sign in to comment.