From ed697aa414b743ed8f8d1ae012ae2222cf0ca4b7 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Fri, 1 Mar 2019 22:47:47 -0800 Subject: [PATCH 1/2] tools: refactor tools JS code * standardize on arrow functions for callbacks * standaradize on trailing commas for multiline arrays --- tools/doc/addon-verify.js | 2 +- tools/doc/generate.js | 2 +- tools/doc/html.js | 2 +- tools/eslint-rules/required-modules.js | 6 ++---- tools/eslint-rules/rules-utils.js | 2 +- tools/license2rtf.js | 16 ++++------------ tools/lint-js.js | 12 +++++------- tools/node-lint-md-cli-rollup/src/cli-entry.js | 4 ++-- 8 files changed, 17 insertions(+), 29 deletions(-) diff --git a/tools/doc/addon-verify.js b/tools/doc/addon-verify.js index 2ff61c21f5c22e..c4dfdba9c4d3ad 100644 --- a/tools/doc/addon-verify.js +++ b/tools/doc/addon-verify.js @@ -81,7 +81,7 @@ ${files[name].replace( target_name: 'addon', sources: files.map(({ name }) => name), includes: ['../common.gypi'], - } + }, ] }) }); diff --git a/tools/doc/generate.js b/tools/doc/generate.js index dd213a35a6bbc4..8e3e733e52a1a1 100644 --- a/tools/doc/generate.js +++ b/tools/doc/generate.js @@ -41,7 +41,7 @@ let nodeVersion = null; let outputDir = null; let apilinks = {}; -args.forEach(function(arg) { +args.forEach((arg) => { if (!arg.startsWith('--')) { filename = arg; } else if (arg.startsWith('--node-version=')) { diff --git a/tools/doc/html.js b/tools/doc/html.js index 00077569d8a85d..ef15cbded87f6a 100644 --- a/tools/doc/html.js +++ b/tools/doc/html.js @@ -413,7 +413,7 @@ function altDocs(filename, docCreated) { { num: '5.x' }, { num: '4.x' }, { num: '0.12.x' }, - { num: '0.10.x' } + { num: '0.10.x' }, ]; const getHref = (versionNum) => diff --git a/tools/eslint-rules/required-modules.js b/tools/eslint-rules/required-modules.js index a7a87662ac21e6..a0511e0579d911 100644 --- a/tools/eslint-rules/required-modules.js +++ b/tools/eslint-rules/required-modules.js @@ -71,11 +71,9 @@ module.exports = function(context) { 'Program:exit'(node) { if (foundModules.length < requiredModules.length) { var missingModules = requiredModules.filter( - function(module) { - return foundModules.indexOf(module) === -1; - } + (module) => foundModules.indexOf(module) === -1 ); - missingModules.forEach(function(moduleName) { + missingModules.forEach((moduleName) => { context.report( node, 'Mandatory module "{{moduleName}}" must be loaded.', diff --git a/tools/eslint-rules/rules-utils.js b/tools/eslint-rules/rules-utils.js index 315e7ca0adf2ef..c64b16479920dd 100644 --- a/tools/eslint-rules/rules-utils.js +++ b/tools/eslint-rules/rules-utils.js @@ -69,7 +69,7 @@ module.exports.inSkipBlock = function(node) { node.test.operator === '!') { const consequent = node.consequent; if (consequent.body) { - consequent.body.some(function(expressionStatement) { + consequent.body.some((expressionStatement) => { if (hasSkip(expressionStatement.expression)) { return hasSkipBlock = true; } diff --git a/tools/license2rtf.js b/tools/license2rtf.js index e08cf1e5c25be6..817da81d7a6ada 100644 --- a/tools/license2rtf.js +++ b/tools/license2rtf.js @@ -222,20 +222,12 @@ function rtfEscape(string) { } return string - .replace(/[\\{}]/g, function(m) { - return `\\${m}`; - }) - .replace(/\t/g, function() { - return '\\tab '; - }) + .replace(/[\\{}]/g, (m) => `\\${m}`) + .replace(/\t/g, () => '\\tab ') // eslint-disable-next-line no-control-regex - .replace(/[\x00-\x1f\x7f-\xff]/g, function(m) { - return `\\'${toHex(m.charCodeAt(0), 2)}`; - }) + .replace(/[\x00-\x1f\x7f-\xff]/g, (m) => `\\'${toHex(m.charCodeAt(0), 2)}`) .replace(/\ufeff/g, '') - .replace(/[\u0100-\uffff]/g, function(m) { - return `\\u${toHex(m.charCodeAt(0), 4)}?`; - }); + .replace(/[\u0100-\uffff]/g, (m) => `\\u${toHex(m.charCodeAt(0), 4)}?`); } /* diff --git a/tools/lint-js.js b/tools/lint-js.js index 4a7c2f08c324c8..807c7d9d85f62b 100644 --- a/tools/lint-js.js +++ b/tools/lint-js.js @@ -86,9 +86,7 @@ if (cluster.isMaster) { outFn = function(str) { fs.writeSync(fd, str, 'utf8'); }; - process.on('exit', function() { - fs.closeSync(fd); - }); + process.on('exit', () => { fs.closeSync(fd); }); } else { outFn = function(str) { process.stdout.write(str); @@ -117,20 +115,20 @@ if (cluster.isMaster) { if (showProgress) { // Start the progress display update timer when the first worker is ready - cluster.once('online', function() { + cluster.once('online', () => { startTime = process.hrtime(); setInterval(printProgress, 1000).unref(); printProgress(); }); } - cluster.on('online', function(worker) { + cluster.on('online', (worker) => { // Configure worker and give it some initial work to do worker.send(workerConfig); sendWork(worker); }); - process.on('exit', function(code) { + process.on('exit', (code) => { if (showProgress) { curPath = 'Done'; printProgress(); @@ -232,7 +230,7 @@ if (cluster.isMaster) { // Worker var config = {}; - process.on('message', function(files) { + process.on('message', (files) => { if (files instanceof Array) { // Lint some files const report = cli.executeOnFiles(files); diff --git a/tools/node-lint-md-cli-rollup/src/cli-entry.js b/tools/node-lint-md-cli-rollup/src/cli-entry.js index 0d204b15a99954..616ac357742aee 100644 --- a/tools/node-lint-md-cli-rollup/src/cli-entry.js +++ b/tools/node-lint-md-cli-rollup/src/cli-entry.js @@ -14,7 +14,7 @@ const args = { description: cli.description, version: [ proc.name + ': ' + proc.version, - cli.name + ': ' + cli.version + cli.name + ': ' + cli.version, ].join(', '), ignoreName: '.' + proc.name + 'ignore', extensions: extensions @@ -23,7 +23,7 @@ const config = options(process.argv.slice(2), args); config.detectConfig = false; config.plugins = plugins; -engine(config, function done(err, code) { +engine(config, (err, code) => { if (err) console.error(err); process.exit(code); }); From f75b22f4b6f70bb9bc6343ca55a09377541131c3 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Fri, 1 Mar 2019 22:47:58 -0800 Subject: [PATCH 2/2] tools: apply stricter linting to tools directory Enable ESLint rules for trailing commas and arrow callbacks in tools directory. These rules are also in place in the benchmark directory. --- tools/.eslintrc.yaml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tools/.eslintrc.yaml b/tools/.eslintrc.yaml index f8b7cc4e41a747..740056235cb01b 100644 --- a/tools/.eslintrc.yaml +++ b/tools/.eslintrc.yaml @@ -1,4 +1,9 @@ rules: - # Variables - # http://eslint.org/docs/rules/#variables + comma-dangle: + - error + - arrays: 'always-multiline' + objects: 'only-multiline' + imports: 'only-multiline' + exports: 'only-multiline' no-unused-vars: [error, { args: 'after-used' }] + prefer-arrow-callback: error