From 73acb50c2e422fc0a206413733eb49fe50aa0fd1 Mon Sep 17 00:00:00 2001 From: Masashi Hirano Date: Sat, 2 Mar 2019 23:07:03 +0900 Subject: [PATCH 1/2] tools: replace var to let/const --- tools/eslint-rules/crypto-check.js | 4 +- tools/eslint-rules/eslint-check.js | 4 +- tools/eslint-rules/inspector-check.js | 4 +- tools/eslint-rules/no-unescaped-regexp-dot.js | 16 ++++---- tools/eslint-rules/prefer-assert-iferror.js | 2 +- tools/eslint-rules/required-modules.js | 10 ++--- tools/eslint-rules/rules-utils.js | 4 +- tools/lint-js.js | 40 +++++++++---------- 8 files changed, 42 insertions(+), 42 deletions(-) diff --git a/tools/eslint-rules/crypto-check.js b/tools/eslint-rules/crypto-check.js index fbc319f964c696..151815cd645642 100644 --- a/tools/eslint-rules/crypto-check.js +++ b/tools/eslint-rules/crypto-check.js @@ -23,8 +23,8 @@ const bindingModules = cryptoModules.concat(['tls_wrap']); module.exports = function(context) { const missingCheckNodes = []; const requireNodes = []; - var commonModuleNode = null; - var hasSkipCall = false; + let commonModuleNode = null; + let hasSkipCall = false; function testCryptoUsage(node) { if (utils.isRequired(node, requireModules) || diff --git a/tools/eslint-rules/eslint-check.js b/tools/eslint-rules/eslint-check.js index 00a5234733ec05..340a63ab4a00da 100644 --- a/tools/eslint-rules/eslint-check.js +++ b/tools/eslint-rules/eslint-check.js @@ -14,8 +14,8 @@ const msg = 'Please add a skipIfEslintMissing() call to allow this test to ' + module.exports = function(context) { const missingCheckNodes = []; - var commonModuleNode = null; - var hasEslintCheck = false; + let commonModuleNode = null; + let hasEslintCheck = false; function testEslintUsage(context, node) { if (utils.isRequired(node, ['../../tools/node_modules/eslint'])) { diff --git a/tools/eslint-rules/inspector-check.js b/tools/eslint-rules/inspector-check.js index 189b023efc6195..3d3c97b5c631fa 100644 --- a/tools/eslint-rules/inspector-check.js +++ b/tools/eslint-rules/inspector-check.js @@ -15,8 +15,8 @@ const msg = 'Please add a skipIfInspectorDisabled() call to allow this ' + module.exports = function(context) { const missingCheckNodes = []; - var commonModuleNode = null; - var hasInspectorCheck = false; + let commonModuleNode = null; + let hasInspectorCheck = false; function testInspectorUsage(context, node) { if (utils.isRequired(node, ['inspector'])) { diff --git a/tools/eslint-rules/no-unescaped-regexp-dot.js b/tools/eslint-rules/no-unescaped-regexp-dot.js index 3af514d4bb91a9..e9349713bfa3fa 100644 --- a/tools/eslint-rules/no-unescaped-regexp-dot.js +++ b/tools/eslint-rules/no-unescaped-regexp-dot.js @@ -11,8 +11,8 @@ module.exports = function(context) { const sourceCode = context.getSourceCode(); const regexpStack = []; - var regexpBuffer = []; - var inRegExp = false; + let regexpBuffer = []; + let inRegExp = false; function report(node, startOffset) { const indexOfDot = sourceCode.getIndexFromLoc(node.loc.start) + startOffset; @@ -25,13 +25,13 @@ module.exports = function(context) { const allowedModifiers = ['+', '*', '?', '{']; function checkRegExp(nodes) { - var escaping = false; - var inCharClass = false; - for (var n = 0; n < nodes.length; ++n) { + let escaping = false; + let inCharClass = false; + for (let n = 0; n < nodes.length; ++n) { const pair = nodes[n]; const node = pair[0]; const str = pair[1]; - for (var i = 0; i < str.length; ++i) { + for (let i = 0; i < str.length; ++i) { switch (str[i]) { case '[': if (!escaping) @@ -96,7 +96,7 @@ module.exports = function(context) { node.quasis.length); if (inRegExp && (isTemplate || (typeof node.value === 'string' && node.value.length))) { - var p = node.parent; + let p = node.parent; while (p && p.type === 'BinaryExpression') { p = p.parent; } @@ -105,7 +105,7 @@ module.exports = function(context) { p.callee.name === 'RegExp') { if (isTemplate) { const quasis = node.quasis; - for (var i = 0; i < quasis.length; ++i) { + for (let i = 0; i < quasis.length; ++i) { const el = quasis[i]; if (el.type === 'TemplateElement' && el.value && el.value.cooked) regexpBuffer.push([el, el.value.cooked]); diff --git a/tools/eslint-rules/prefer-assert-iferror.js b/tools/eslint-rules/prefer-assert-iferror.js index 399ee7403a6c88..2129600e2d4eea 100644 --- a/tools/eslint-rules/prefer-assert-iferror.js +++ b/tools/eslint-rules/prefer-assert-iferror.js @@ -10,7 +10,7 @@ const utils = require('./rules-utils.js'); module.exports = { create(context) { const sourceCode = context.getSourceCode(); - var assertImported = false; + let assertImported = false; function hasSameTokens(nodeA, nodeB) { const aTokens = sourceCode.getTokens(nodeA); diff --git a/tools/eslint-rules/required-modules.js b/tools/eslint-rules/required-modules.js index a0511e0579d911..5175d4d46d7e95 100644 --- a/tools/eslint-rules/required-modules.js +++ b/tools/eslint-rules/required-modules.js @@ -12,7 +12,7 @@ const path = require('path'); module.exports = function(context) { // trim required module names - var requiredModules = context.options; + const requiredModules = context.options; const isESM = context.parserOptions.sourceType === 'module'; const foundModules = []; @@ -46,7 +46,7 @@ module.exports = function(context) { * @returns {undefined|String} required module name or undefined */ function getRequiredModuleName(str) { - var value = path.basename(str); + const value = path.basename(str); // Check if value is in required modules array return requiredModules.indexOf(value) !== -1 ? value : undefined; @@ -70,7 +70,7 @@ module.exports = function(context) { const rules = { 'Program:exit'(node) { if (foundModules.length < requiredModules.length) { - var missingModules = requiredModules.filter( + const missingModules = requiredModules.filter( (module) => foundModules.indexOf(module) === -1 ); missingModules.forEach((moduleName) => { @@ -86,7 +86,7 @@ module.exports = function(context) { if (isESM) { rules.ImportDeclaration = (node) => { - var requiredModuleName = getRequiredModuleName(node.source.value); + const requiredModuleName = getRequiredModuleName(node.source.value); if (requiredModuleName) { foundModules.push(requiredModuleName); } @@ -94,7 +94,7 @@ module.exports = function(context) { } else { rules.CallExpression = (node) => { if (isRequireCall(node)) { - var requiredModuleName = getRequiredModuleNameFromCall(node); + const requiredModuleName = getRequiredModuleNameFromCall(node); if (requiredModuleName) { foundModules.push(requiredModuleName); diff --git a/tools/eslint-rules/rules-utils.js b/tools/eslint-rules/rules-utils.js index c64b16479920dd..b85425cb50d59c 100644 --- a/tools/eslint-rules/rules-utils.js +++ b/tools/eslint-rules/rules-utils.js @@ -24,7 +24,7 @@ module.exports.isRequired = function(node, modules) { * Return true if common module is required * in AST Node under inspection */ -var commonModuleRegExp = new RegExp(/^(\.\.\/)*common(\.js)?$/); +const commonModuleRegExp = new RegExp(/^(\.\.\/)*common(\.js)?$/); module.exports.isCommonModule = function(node) { return node.callee.name === 'require' && node.arguments.length !== 0 && @@ -63,7 +63,7 @@ module.exports.usesCommonProperty = function(node, properties) { * and the block also has a call to skip. */ module.exports.inSkipBlock = function(node) { - var hasSkipBlock = false; + let hasSkipBlock = false; if (node.test && node.test.type === 'UnaryExpression' && node.test.operator === '!') { diff --git a/tools/lint-js.js b/tools/lint-js.js index 807c7d9d85f62b..45941d8ceb4971 100644 --- a/tools/lint-js.js +++ b/tools/lint-js.js @@ -25,24 +25,24 @@ if (process.argv.indexOf('-F') !== -1) const cli = new CLIEngine(cliOptions); if (cluster.isMaster) { - var numCPUs = 1; + let numCPUs = 1; const paths = []; - var files = null; - var totalPaths = 0; - var failures = 0; - var successes = 0; - var lastLineLen = 0; - var curPath = 'Starting ...'; - var showProgress = true; + let files = null; + let totalPaths = 0; + let failures = 0; + let successes = 0; + let lastLineLen = 0; + let curPath = 'Starting ...'; + let showProgress = true; const globOptions = { nodir: true }; const workerConfig = {}; - var startTime; - var formatter; - var outFn; - var fd; - var i; + let startTime; + let formatter; + let outFn; + let fd; + let i; // Check if spreading work among all cores/cpus if (process.argv.indexOf('-J') !== -1) @@ -169,7 +169,7 @@ if (cluster.isMaster) { // We either just started or we have no more files to lint for the current // path. Find the next path that has some files to be linted. while (paths.length) { - var dir = paths.shift(); + let dir = paths.shift(); curPath = dir; const patterns = cli.resolveFileGlobPatterns([dir]); dir = path.resolve(patterns[0]); @@ -188,7 +188,7 @@ if (cluster.isMaster) { // workers busy most of the time instead of only a minority doing most of // the work. const sliceLen = Math.min(maxWorkload, Math.ceil(files.length / numCPUs)); - var slice; + let slice; if (sliceLen === files.length) { // Micro-optimization to avoid splicing to an empty array slice = files; @@ -212,10 +212,10 @@ if (cluster.isMaster) { const secs = `${elapsed % 60}`.padStart(2, '0'); const passed = `${successes}`.padStart(6); const failed = `${failures}`.padStart(6); - var pct = Math.ceil(((totalPaths - paths.length) / totalPaths) * 100); - pct = `${pct}`.padStart(3); + let pct = `${Math.ceil(((totalPaths - paths.length) / totalPaths) * 100)}`; + pct = pct.padStart(3); - var line = `[${mins}:${secs}|%${pct}|+${passed}|-${failed}]: ${curPath}`; + let line = `[${mins}:${secs}|%${pct}|+${passed}|-${failed}]: ${curPath}`; // Truncate line like cpplint does in case it gets too long if (line.length > 75) @@ -229,7 +229,7 @@ if (cluster.isMaster) { } else { // Worker - var config = {}; + let config = {}; process.on('message', (files) => { if (files instanceof Array) { // Lint some files @@ -246,7 +246,7 @@ if (cluster.isMaster) { // Silence warnings for files with no errors while keeping the "ok" // status if (report.warningCount > 0) { - for (var i = 0; i < results.length; ++i) { + for (let i = 0; i < results.length; ++i) { const result = results[i]; if (result.errorCount === 0 && result.warningCount > 0) { result.warningCount = 0; From 9e8be33d5856a20402c1b0b00644703b792657cd Mon Sep 17 00:00:00 2001 From: shisama Date: Wed, 6 Mar 2019 12:21:50 +0900 Subject: [PATCH 2/2] tools: add no-var lint rule for tools directory --- tools/.eslintrc.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/.eslintrc.yaml b/tools/.eslintrc.yaml index 740056235cb01b..ea518d2afb83ad 100644 --- a/tools/.eslintrc.yaml +++ b/tools/.eslintrc.yaml @@ -7,3 +7,4 @@ rules: exports: 'only-multiline' no-unused-vars: [error, { args: 'after-used' }] prefer-arrow-callback: error + no-var: error