diff options
author | Florian Dold <florian.dold@gmail.com> | 2019-04-03 15:43:32 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2019-04-03 15:45:57 +0200 |
commit | 71e285b94c7edaa43aa8115965cf5a36b8e0f80a (patch) | |
tree | 7d4aa9d0d5aff686b106cd5da72ba77960c4af43 /deps/node/deps/node-inspect/tools/eslint-rules | |
parent | 7dadf9356b4f3f4137ce982ea5bb960283116e9a (diff) | |
download | akono-71e285b94c7edaa43aa8115965cf5a36b8e0f80a.tar.gz akono-71e285b94c7edaa43aa8115965cf5a36b8e0f80a.tar.bz2 akono-71e285b94c7edaa43aa8115965cf5a36b8e0f80a.zip |
Node.js v11.13.0
Diffstat (limited to 'deps/node/deps/node-inspect/tools/eslint-rules')
9 files changed, 433 insertions, 0 deletions
diff --git a/deps/node/deps/node-inspect/tools/eslint-rules/align-function-arguments.js b/deps/node/deps/node-inspect/tools/eslint-rules/align-function-arguments.js new file mode 100644 index 00000000..01555248 --- /dev/null +++ b/deps/node/deps/node-inspect/tools/eslint-rules/align-function-arguments.js @@ -0,0 +1,76 @@ +/** + * @fileoverview Align arguments in multiline function calls + * @author Rich Trott + */ +'use strict'; + +//------------------------------------------------------------------------------ +// Rule Definition +//------------------------------------------------------------------------------ + +function checkArgumentAlignment(context, node) { + + function isNodeFirstInLine(node, byEndLocation) { + const firstToken = byEndLocation === true ? context.getLastToken(node, 1) : + context.getTokenBefore(node); + const startLine = byEndLocation === true ? node.loc.end.line : + node.loc.start.line; + const endLine = firstToken ? firstToken.loc.end.line : -1; + + return startLine !== endLine; + } + + if (node.arguments.length === 0) + return; + + var msg = ''; + const first = node.arguments[0]; + var currentLine = first.loc.start.line; + const firstColumn = first.loc.start.column; + + const ignoreTypes = [ + 'ArrowFunctionExpression', + 'FunctionExpression', + 'ObjectExpression', + ]; + + const args = node.arguments; + + // For now, don't bother trying to validate potentially complicating things + // like closures. Different people will have very different ideas and it's + // probably best to implement configuration options. + if (args.some((node) => { return ignoreTypes.indexOf(node.type) !== -1; })) { + return; + } + + if (!isNodeFirstInLine(node)) { + return; + } + + var misaligned; + + args.slice(1).forEach((argument) => { + if (!misaligned) { + if (argument.loc.start.line === currentLine + 1) { + if (argument.loc.start.column !== firstColumn) { + if (isNodeFirstInLine(argument)) { + msg = 'Function argument in column ' + + `${argument.loc.start.column + 1}, ` + + `expected in ${firstColumn + 1}`; + misaligned = argument; + } + } + } + } + currentLine = argument.loc.start.line; + }); + + if (msg) + context.report(misaligned, msg); +} + +module.exports = function(context) { + return { + 'CallExpression': (node) => checkArgumentAlignment(context, node) + }; +}; diff --git a/deps/node/deps/node-inspect/tools/eslint-rules/align-multiline-assignment.js b/deps/node/deps/node-inspect/tools/eslint-rules/align-multiline-assignment.js new file mode 100644 index 00000000..80896b5e --- /dev/null +++ b/deps/node/deps/node-inspect/tools/eslint-rules/align-multiline-assignment.js @@ -0,0 +1,68 @@ +/** + * @fileoverview Align multiline variable assignments + * @author Rich Trott + */ +'use strict'; + +//------------------------------------------------------------------------------ +// Rule Definition +//------------------------------------------------------------------------------ +function getBinaryExpressionStarts(binaryExpression, starts) { + function getStartsFromOneSide(side, starts) { + starts.push(side.loc.start); + if (side.type === 'BinaryExpression') { + starts = getBinaryExpressionStarts(side, starts); + } + return starts; + } + + starts = getStartsFromOneSide(binaryExpression.left, starts); + starts = getStartsFromOneSide(binaryExpression.right, starts); + return starts; +} + +function checkExpressionAlignment(expression) { + if (!expression) + return; + + var msg = ''; + + switch (expression.type) { + case 'BinaryExpression': + var starts = getBinaryExpressionStarts(expression, []); + var startLine = starts[0].line; + const startColumn = starts[0].column; + starts.forEach((loc) => { + if (loc.line > startLine) { + startLine = loc.line; + if (loc.column !== startColumn) { + msg = 'Misaligned multiline assignment'; + } + } + }); + break; + } + return msg; +} + +function testAssignment(context, node) { + const msg = checkExpressionAlignment(node.right); + if (msg) + context.report(node, msg); +} + +function testDeclaration(context, node) { + node.declarations.forEach((declaration) => { + const msg = checkExpressionAlignment(declaration.init); + // const start = declaration.init.loc.start; + if (msg) + context.report(node, msg); + }); +} + +module.exports = function(context) { + return { + 'AssignmentExpression': (node) => testAssignment(context, node), + 'VariableDeclaration': (node) => testDeclaration(context, node) + }; +}; diff --git a/deps/node/deps/node-inspect/tools/eslint-rules/assert-fail-single-argument.js b/deps/node/deps/node-inspect/tools/eslint-rules/assert-fail-single-argument.js new file mode 100644 index 00000000..4ce79023 --- /dev/null +++ b/deps/node/deps/node-inspect/tools/eslint-rules/assert-fail-single-argument.js @@ -0,0 +1,30 @@ +/** + * @fileoverview Prohibit use of a single argument only in `assert.fail()`. It + * is almost always an error. + * @author Rich Trott + */ +'use strict'; + +//------------------------------------------------------------------------------ +// Rule Definition +//------------------------------------------------------------------------------ + +const msg = 'assert.fail() message should be third argument'; + +function isAssert(node) { + return node.callee.object && node.callee.object.name === 'assert'; +} + +function isFail(node) { + return node.callee.property && node.callee.property.name === 'fail'; +} + +module.exports = function(context) { + return { + 'CallExpression': function(node) { + if (isAssert(node) && isFail(node) && node.arguments.length === 1) { + context.report(node, msg); + } + } + }; +}; diff --git a/deps/node/deps/node-inspect/tools/eslint-rules/buffer-constructor.js b/deps/node/deps/node-inspect/tools/eslint-rules/buffer-constructor.js new file mode 100644 index 00000000..938598e8 --- /dev/null +++ b/deps/node/deps/node-inspect/tools/eslint-rules/buffer-constructor.js @@ -0,0 +1,25 @@ +/** + * @fileoverview Require use of new Buffer constructor methods in lib + * @author James M Snell + */ +'use strict'; + +//------------------------------------------------------------------------------ +// Rule Definition +//------------------------------------------------------------------------------ +const msg = 'Use of the Buffer() constructor has been deprecated. ' + + 'Please use either Buffer.alloc(), Buffer.allocUnsafe(), ' + + 'or Buffer.from()'; + +function test(context, node) { + if (node.callee.name === 'Buffer') { + context.report(node, msg); + } +} + +module.exports = function(context) { + return { + 'NewExpression': (node) => test(context, node), + 'CallExpression': (node) => test(context, node) + }; +}; diff --git a/deps/node/deps/node-inspect/tools/eslint-rules/new-with-error.js b/deps/node/deps/node-inspect/tools/eslint-rules/new-with-error.js new file mode 100644 index 00000000..655f34bf --- /dev/null +++ b/deps/node/deps/node-inspect/tools/eslint-rules/new-with-error.js @@ -0,0 +1,31 @@ +/** + * @fileoverview Require `throw new Error()` rather than `throw Error()` + * @author Rich Trott + */ +'use strict'; + +//------------------------------------------------------------------------------ +// Rule Definition +//------------------------------------------------------------------------------ + +module.exports = function(context) { + + var errorList = context.options.length !== 0 ? context.options : ['Error']; + + return { + 'ThrowStatement': function(node) { + if (node.argument.type === 'CallExpression' && + errorList.indexOf(node.argument.callee.name) !== -1) { + context.report(node, 'Use new keyword when throwing.'); + } + } + }; +}; + +module.exports.schema = { + 'type': 'array', + 'additionalItems': { + 'type': 'string' + }, + 'uniqueItems': true +}; diff --git a/deps/node/deps/node-inspect/tools/eslint-rules/no-let-in-for-declaration.js b/deps/node/deps/node-inspect/tools/eslint-rules/no-let-in-for-declaration.js new file mode 100644 index 00000000..8b1a6783 --- /dev/null +++ b/deps/node/deps/node-inspect/tools/eslint-rules/no-let-in-for-declaration.js @@ -0,0 +1,46 @@ +/** + * @fileoverview Prohibit the use of `let` as the loop variable + * in the initialization of for, and the left-hand + * iterator in forIn and forOf loops. + * + * @author Jessica Quynh Tran + */ + +'use strict'; + +//------------------------------------------------------------------------------ +// Rule Definition +//------------------------------------------------------------------------------ + +module.exports = { + create(context) { + + const msg = 'Use of `let` as the loop variable in a for-loop is ' + + 'not recommended. Please use `var` instead.'; + + /** + * Report function to test if the for-loop is declared using `let`. + */ + function testForLoop(node) { + if (node.init && node.init.kind === 'let') { + context.report(node.init, msg); + } + } + + /** + * Report function to test if the for-in or for-of loop + * is declared using `let`. + */ + function testForInOfLoop(node) { + if (node.left && node.left.kind === 'let') { + context.report(node.left, msg); + } + } + + return { + 'ForStatement': testForLoop, + 'ForInStatement': testForInOfLoop, + 'ForOfStatement': testForInOfLoop + }; + } +}; diff --git a/deps/node/deps/node-inspect/tools/eslint-rules/prefer-assert-methods.js b/deps/node/deps/node-inspect/tools/eslint-rules/prefer-assert-methods.js new file mode 100644 index 00000000..fa345eb7 --- /dev/null +++ b/deps/node/deps/node-inspect/tools/eslint-rules/prefer-assert-methods.js @@ -0,0 +1,39 @@ +'use strict'; + +function isAssert(node) { + return node.expression && + node.expression.type === 'CallExpression' && + node.expression.callee && + node.expression.callee.name === 'assert'; +} + +function getFirstArg(expression) { + return expression.arguments && expression.arguments[0]; +} + +function parseError(method, op) { + return `'assert.${method}' should be used instead of '${op}'`; +} + +const preferedAssertMethod = { + '===': 'strictEqual', + '!==': 'notStrictEqual', + '==': 'equal', + '!=': 'notEqual' +}; + +module.exports = function(context) { + return { + ExpressionStatement(node) { + if (isAssert(node)) { + const arg = getFirstArg(node.expression); + if (arg && arg.type === 'BinaryExpression') { + const assertMethod = preferedAssertMethod[arg.operator]; + if (assertMethod) { + context.report(node, parseError(assertMethod, arg.operator)); + } + } + } + } + }; +}; diff --git a/deps/node/deps/node-inspect/tools/eslint-rules/require-buffer.js b/deps/node/deps/node-inspect/tools/eslint-rules/require-buffer.js new file mode 100644 index 00000000..c9818cb7 --- /dev/null +++ b/deps/node/deps/node-inspect/tools/eslint-rules/require-buffer.js @@ -0,0 +1,19 @@ +'use strict'; + +module.exports = function(context) { + function flagIt(reference) { + const msg = 'Use const Buffer = require(\'buffer\').Buffer; ' + + 'at the beginning of this file'; + context.report(reference.identifier, msg); + } + + return { + 'Program:exit': function() { + const globalScope = context.getScope(); + const variable = globalScope.set.get('Buffer'); + if (variable) { + variable.references.forEach(flagIt); + } + } + }; +}; diff --git a/deps/node/deps/node-inspect/tools/eslint-rules/required-modules.js b/deps/node/deps/node-inspect/tools/eslint-rules/required-modules.js new file mode 100644 index 00000000..3e4a8e8a --- /dev/null +++ b/deps/node/deps/node-inspect/tools/eslint-rules/required-modules.js @@ -0,0 +1,99 @@ +/** + * @fileoverview Require usage of specified node modules. + * @author Rich Trott + */ +'use strict'; + +var path = require('path'); + +//------------------------------------------------------------------------------ +// Rule Definition +//------------------------------------------------------------------------------ + +module.exports = function(context) { + // trim required module names + var requiredModules = context.options; + + var foundModules = []; + + // if no modules are required we don't need to check the CallExpressions + if (requiredModules.length === 0) { + return {}; + } + + /** + * Function to check if a node is a string literal. + * @param {ASTNode} node The node to check. + * @returns {boolean} If the node is a string literal. + */ + function isString(node) { + return node && node.type === 'Literal' && typeof node.value === 'string'; + } + + /** + * Function to check if a node is a require call. + * @param {ASTNode} node The node to check. + * @returns {boolean} If the node is a require call. + */ + function isRequireCall(node) { + return node.callee.type === 'Identifier' && node.callee.name === 'require'; + } + + /** + * Function to check if a node has an argument that is a required module and + * return its name. + * @param {ASTNode} node The node to check + * @returns {undefined|String} required module name or undefined + */ + function getRequiredModuleName(node) { + var moduleName; + + // node has arguments and first argument is string + if (node.arguments.length && isString(node.arguments[0])) { + var argValue = path.basename(node.arguments[0].value.trim()); + + // check if value is in required modules array + if (requiredModules.indexOf(argValue) !== -1) { + moduleName = argValue; + } + } + + return moduleName; + } + + return { + 'CallExpression': function(node) { + if (isRequireCall(node)) { + var requiredModuleName = getRequiredModuleName(node); + + if (requiredModuleName) { + foundModules.push(requiredModuleName); + } + } + }, + 'Program:exit': function(node) { + if (foundModules.length < requiredModules.length) { + var missingModules = requiredModules.filter( + function(module) { + return foundModules.indexOf(module === -1); + } + ); + missingModules.forEach(function(moduleName) { + context.report( + node, + 'Mandatory module "{{moduleName}}" must be loaded.', + { moduleName: moduleName } + ); + }); + } + } + }; +}; + +module.exports.schema = { + 'type': 'array', + 'additionalItems': { + 'type': 'string' + }, + 'uniqueItems': true +}; |