| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6c454af commit 728f968
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,46 +14,45 @@ const astSelector = 'NewExpression[callee.property.name="TypeError"]' + | |||
| 14 | 14 | ||
| 15 | 15 | const primitives = [ 'number', 'string', 'boolean', 'null', 'undefined' ]; | |
| 16 | 16 | ||
| 17 | - module.exports = function(context) { | ||
| 18 | - function checkNamesArgument(node) { | ||
| 19 | - const names = node.arguments[2]; | ||
| 20 | - | ||
| 21 | - switch (names.type) { | ||
| 22 | - case 'Literal': | ||
| 23 | - checkName(names); | ||
| 24 | - break; | ||
| 25 | - case 'ArrayExpression': | ||
| 26 | - names.elements.forEach((name) => { | ||
| 27 | - checkName(name); | ||
| 28 | - }); | ||
| 29 | - break; | ||
| 30 | - } | ||
| 31 | - } | ||
| 32 | - | ||
| 33 | - function checkName(node) { | ||
| 34 | - const name = node.value; | ||
| 35 | - const lowercaseName = name.toLowerCase(); | ||
| 36 | - if (name !== lowercaseName && primitives.includes(lowercaseName)) { | ||
| 37 | - const msg = `primitive should use lowercase: ${name}`; | ||
| 38 | - context.report({ | ||
| 39 | - node, | ||
| 40 | - message: msg, | ||
| 41 | - fix: (fixer) => { | ||
| 42 | - return fixer.replaceText( | ||
| 43 | - node, | ||
| 44 | - `'${lowercaseName}'`, | ||
| 45 | - ); | ||
| 46 | - }, | ||
| 47 | - }); | ||
| 17 | + module.exports = { | ||
| 18 | + meta: { fixable: 'code' }, | ||
| 19 | + create(context) { | ||
| 20 | + function checkNamesArgument(node) { | ||
| 21 | + const names = node.arguments[2]; | ||
| 22 | + | ||
| 23 | + switch (names.type) { | ||
| 24 | + case 'Literal': | ||
| 25 | + checkName(names); | ||
| 26 | + break; | ||
| 27 | + case 'ArrayExpression': | ||
| 28 | + names.elements.forEach((name) => { | ||
| 29 | + checkName(name); | ||
| 30 | + }); | ||
| 31 | + break; | ||
| 32 | + } | ||
| 48 | 33 | } | |
| 49 | 34 | ||
| 50 | - } | ||
| 35 | + function checkName(node) { | ||
| 36 | + const name = node.value; | ||
| 37 | + const lowercaseName = name.toLowerCase(); | ||
| 38 | + if (name !== lowercaseName && primitives.includes(lowercaseName)) { | ||
| 39 | + const msg = `primitive should use lowercase: ${name}`; | ||
| 40 | + context.report({ | ||
| 41 | + node, | ||
| 42 | + message: msg, | ||
| 43 | + fix: (fixer) => { | ||
| 44 | + return fixer.replaceText( | ||
| 45 | + node, | ||
| 46 | + `'${lowercaseName}'`, | ||
| 47 | + ); | ||
| 48 | + }, | ||
| 49 | + }); | ||
| 50 | + } | ||
| 51 | 51 | ||
| 52 | - return { | ||
| 53 | - [astSelector]: (node) => checkNamesArgument(node), | ||
| 54 | - }; | ||
| 55 | - }; | ||
| 52 | + } | ||
| 56 | 53 | ||
| 57 | - module.exports.meta = { | ||
| 58 | - fixable: 'code', | ||
| 54 | + return { | ||
| 55 | + [astSelector]: (node) => checkNamesArgument(node), | ||
| 56 | + }; | ||
| 57 | + }, | ||
| 59 | 58 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,30 +19,29 @@ const preferredAssertMethod = { | |||
| 19 | 19 | '!=': 'notEqual', | |
| 20 | 20 | }; | |
| 21 | 21 | ||
| 22 | - module.exports = function(context) { | ||
| 23 | - return { | ||
| 24 | - [astSelector]: function(node) { | ||
| 25 | - const arg = node.expression.arguments[0]; | ||
| 26 | - const assertMethod = preferredAssertMethod[arg.operator]; | ||
| 27 | - if (assertMethod) { | ||
| 28 | - context.report({ | ||
| 29 | - node, | ||
| 30 | - message: parseError(assertMethod, arg.operator), | ||
| 31 | - fix: (fixer) => { | ||
| 32 | - const sourceCode = context.getSourceCode(); | ||
| 33 | - const left = sourceCode.getText(arg.left); | ||
| 34 | - const right = sourceCode.getText(arg.right); | ||
| 35 | - return fixer.replaceText( | ||
| 36 | - node, | ||
| 37 | - `assert.${assertMethod}(${left}, ${right});`, | ||
| 38 | - ); | ||
| 39 | - }, | ||
| 40 | - }); | ||
| 41 | - } | ||
| 42 | - }, | ||
| 43 | - }; | ||
| 44 | - }; | ||
| 45 | - | ||
| 46 | - module.exports.meta = { | ||
| 47 | - fixable: 'code', | ||
| 22 | + module.exports = { | ||
| 23 | + meta: { fixable: 'code' }, | ||
| 24 | + create(context) { | ||
| 25 | + return { | ||
| 26 | + [astSelector]: function(node) { | ||
| 27 | + const arg = node.expression.arguments[0]; | ||
| 28 | + const assertMethod = preferredAssertMethod[arg.operator]; | ||
| 29 | + if (assertMethod) { | ||
| 30 | + context.report({ | ||
| 31 | + node, | ||
| 32 | + message: parseError(assertMethod, arg.operator), | ||
| 33 | + fix: (fixer) => { | ||
| 34 | + const sourceCode = context.getSourceCode(); | ||
| 35 | + const left = sourceCode.getText(arg.left); | ||
| 36 | + const right = sourceCode.getText(arg.right); | ||
| 37 | + return fixer.replaceText( | ||
| 38 | + node, | ||
| 39 | + `assert.${assertMethod}(${left}, ${right});`, | ||
| 40 | + ); | ||
| 41 | + }, | ||
| 42 | + }); | ||
| 43 | + } | ||
| 44 | + }, | ||
| 45 | + }; | ||
| 46 | + }, | ||
| 48 | 47 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,16 +15,18 @@ const mustCallSelector = 'CallExpression[callee.object.name="common"]' + | |||
| 15 | 15 | const arg0Selector = `${mustCallSelector}[arguments.0.value=0]`; | |
| 16 | 16 | const arg1Selector = `${mustCallSelector}[arguments.1.value=0]`; | |
| 17 | 17 | ||
| 18 | - module.exports = function(context) { | ||
| 19 | - function report(node) { | ||
| 20 | - context.report(node, msg); | ||
| 21 | - } | ||
| 18 | + module.exports = { | ||
| 19 | + create(context) { | ||
| 20 | + function report(node) { | ||
| 21 | + context.report(node, msg); | ||
| 22 | + } | ||
| 22 | 23 | ||
| 23 | - return { | ||
| 24 | + return { | ||
| 24 | 25 | // Catch common.mustCall(0) | |
| 25 | - [arg0Selector]: report, | ||
| 26 | + [arg0Selector]: report, | ||
| 26 | 27 | ||
| 27 | - // Catch common.mustCall(fn, 0) | ||
| 28 | - [arg1Selector]: report, | ||
| 29 | - }; | ||
| 28 | + // Catch common.mustCall(fn, 0) | ||
| 29 | + [arg1Selector]: report, | ||
| 30 | + }; | ||
| 31 | + }, | ||
| 30 | 32 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,72 +10,74 @@ const { isRequireCall, isString } = require('./rules-utils.js'); | |||
| 10 | 10 | // Rule Definition | |
| 11 | 11 | //------------------------------------------------------------------------------ | |
| 12 | 12 | ||
| 13 | - module.exports = function(context) { | ||
| 14 | - const requiredModule = 'common'; | ||
| 15 | - const isESM = context.parserOptions.sourceType === 'module'; | ||
| 16 | - const foundModules = []; | ||
| 13 | + module.exports = { | ||
| 14 | + create(context) { | ||
| 15 | + const requiredModule = 'common'; | ||
| 16 | + const isESM = context.parserOptions.sourceType === 'module'; | ||
| 17 | + const foundModules = []; | ||
| 17 | 18 | ||
| 18 | - /** | ||
| 19 | - * Function to check if the path is a module and return its name. | ||
| 20 | - * @param {string} str The path to check | ||
| 21 | - * @returns {string} module name | ||
| 22 | - */ | ||
| 23 | - function getModuleName(str) { | ||
| 24 | - if (str === '../common/index.mjs') { | ||
| 25 | - return 'common'; | ||
| 26 | - } | ||
| 19 | + /** | ||
| 20 | + * Function to check if the path is a module and return its name. | ||
| 21 | + * @param {string} str The path to check | ||
| 22 | + * @returns {string} module name | ||
| 23 | + */ | ||
| 24 | + function getModuleName(str) { | ||
| 25 | + if (str === '../common/index.mjs') { | ||
| 26 | + return 'common'; | ||
| 27 | + } | ||
| 27 | 28 | ||
| 28 | - return path.basename(str); | ||
| 29 | - } | ||
| 29 | + return path.basename(str); | ||
| 30 | + } | ||
| 30 | 31 | ||
| 31 | - /** | ||
| 32 | - * Function to check if a node has an argument that is a module and | ||
| 33 | - * return its name. | ||
| 34 | - * @param {ASTNode} node The node to check | ||
| 35 | - * @returns {undefined | string} module name or undefined | ||
| 36 | - */ | ||
| 37 | - function getModuleNameFromCall(node) { | ||
| 32 | + /** | ||
| 33 | + * Function to check if a node has an argument that is a module and | ||
| 34 | + * return its name. | ||
| 35 | + * @param {ASTNode} node The node to check | ||
| 36 | + * @returns {undefined | string} module name or undefined | ||
| 37 | + */ | ||
| 38 | + function getModuleNameFromCall(node) { | ||
| 38 | 39 | // Node has arguments and first argument is string | |
| 39 | - if (node.arguments.length && isString(node.arguments[0])) { | ||
| 40 | - return getModuleName(node.arguments[0].value.trim()); | ||
| 41 | - } | ||
| 40 | + if (node.arguments.length && isString(node.arguments[0])) { | ||
| 41 | + return getModuleName(node.arguments[0].value.trim()); | ||
| 42 | + } | ||
| 42 | 43 | ||
| 43 | - return undefined; | ||
| 44 | - } | ||
| 44 | + return undefined; | ||
| 45 | + } | ||
| 45 | 46 | ||
| 46 | - const rules = { | ||
| 47 | - 'Program:exit'(node) { | ||
| 47 | + const rules = { | ||
| 48 | + 'Program:exit'(node) { | ||
| 48 | 49 | // The common module should be loaded in the first place. | |
| 49 | - const notLoadedFirst = foundModules.indexOf(requiredModule) !== 0; | ||
| 50 | - if (notLoadedFirst) { | ||
| 51 | - context.report( | ||
| 52 | - node, | ||
| 53 | - 'Mandatory module "{{moduleName}}" must be loaded ' + | ||
| 50 | + const notLoadedFirst = foundModules.indexOf(requiredModule) !== 0; | ||
| 51 | + if (notLoadedFirst) { | ||
| 52 | + context.report( | ||
| 53 | + node, | ||
| 54 | + 'Mandatory module "{{moduleName}}" must be loaded ' + | ||
| 54 | 55 | 'before any other modules.', | |
| 55 | - { moduleName: requiredModule }, | ||
| 56 | - ); | ||
| 57 | - } | ||
| 58 | - }, | ||
| 59 | - }; | ||
| 60 | - | ||
| 61 | - if (isESM) { | ||
| 62 | - rules.ImportDeclaration = (node) => { | ||
| 63 | - const moduleName = getModuleName(node.source.value); | ||
| 64 | - if (moduleName) { | ||
| 65 | - foundModules.push(moduleName); | ||
| 66 | - } | ||
| 56 | + { moduleName: requiredModule }, | ||
| 57 | + ); | ||
| 58 | + } | ||
| 59 | + }, | ||
| 67 | 60 | }; | |
| 68 | - } else { | ||
| 69 | - rules.CallExpression = (node) => { | ||
| 70 | - if (isRequireCall(node)) { | ||
| 71 | - const moduleName = getModuleNameFromCall(node); | ||
| 72 | 61 | ||
| 62 | + if (isESM) { | ||
| 63 | + rules.ImportDeclaration = (node) => { | ||
| 64 | + const moduleName = getModuleName(node.source.value); | ||
| 73 | 65 | if (moduleName) { | |
| 74 | 66 | foundModules.push(moduleName); | |
| 75 | 67 | } | |
| 76 | - } | ||
| 77 | - }; | ||
| 78 | - } | ||
| 68 | + }; | ||
| 69 | + } else { | ||
| 70 | + rules.CallExpression = (node) => { | ||
| 71 | + if (isRequireCall(node)) { | ||
| 72 | + const moduleName = getModuleNameFromCall(node); | ||
| 73 | + | ||
| 74 | + if (moduleName) { | ||
| 75 | + foundModules.push(moduleName); | ||
| 76 | + } | ||
| 77 | + } | ||
| 78 | + }; | ||
| 79 | + } | ||
| 79 | 80 | ||
| 80 | - return rules; | ||
| 81 | + return rules; | ||
| 82 | + }, | ||
| 81 | 83 | }; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments