diff --git a/README.md b/README.md index 2310575..5842e3d 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,57 @@ # Clean Code Rules -- `no-number`: Avoid using numbers at the end of variable or function names. - - extensions: `.ts`, `.js` (default) +# `no-number` + +Avoid using numbers at the end of variable or function names. + +## Rule Details + +Examples of **incorrect** code for this rule: + +```js +const foo1 = 1; +const bar2 = 2; + +function baz3() { + return 3; +} +``` + +Examples of **correct** code for this rule: + +```js + +const foo = 1; +const bar = 2; + +function baz() { + return 3; +} +``` + +### Options + +You can pass an `allow` option to specify a list of variable or function names that are allowed to have numbers at the end. +By default, the rule allows `s3` and `v4` as they are common in the JavaScript world. (e.g. `s3` for AWS S3, `v4` for UUID v4) + +```json +{ + "clean-code/no-number": ["error", { + "allow": ["s3", "v4"] + }] +} +``` + +You can pass an `extensions` option to specify a list of file extensions that will be checked. By default, the rule checks `.js`, `.jsx`, `.ts`, and `.tsx` files. + +```json +{ + "clean-code/no-number": ["error", { + "extensions": [".js", ".jsx", ".ts", ".tsx"] + }] +} +``` # Installation diff --git a/package.json b/package.json index 9a0daf8..6184b10 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kougen/eslint-plugin-clean-code", - "version": "1.1.6", + "version": "1.2.0", "description": "Custom eslint rules for clean code.", "main": "dist/index.js", "readme": "README.md", diff --git a/src/rules/no-variable-with-number-at-end.test.ts b/src/rules/no-variable-with-number-at-end.test.ts index 8b9e680..d11602d 100644 --- a/src/rules/no-variable-with-number-at-end.test.ts +++ b/src/rules/no-variable-with-number-at-end.test.ts @@ -29,16 +29,61 @@ ruleTester.run( noVariableWithNumberAtEnd, { valid: [ - {code: "const foo = 'bar';", filename: "file.ts", options: [{extensions: [".ts"]}]}, - {code: "const foo123 = 'bar';", filename: "file.js", options: [{extensions: [".ts"]}]}, + { + code: "const foo = 'bar';", + filename: "file.ts", + options: [{extensions: [".ts"], allow: ["s3", "v4"]}] + }, + { + code: "const foo123 = 'bar';", + filename: "file.js", + options: [{extensions: [".ts"], allow: ["s3", "v4"]}] + }, + ], + invalid: [ + { + code: "const foo2 = 'bar';", + filename: "file.ts", + options: [{extensions: [".ts"], allow: ["s3", "v4"]}], + errors: [{messageId: "noNumberEnding"}], + } + ], + } +); + +ruleTester.run( + "no-number (custom allow)", + noVariableWithNumberAtEnd, + { + valid: [ + { + code: "const foo = 'bar';", + filename: "file.ts", + options: [{extensions: [".ts", ".js"], allow: ["s3", "v4"]}] + }, + { + code: "const foo123 = 'bar';", + filename: "file.ts", + options: [{extensions: [".ts", ".js"], allow: ["foo123"]}] + }, + { + code: "const s3 = 'bar';", + filename: "file.ts", + options: [{extensions: [".ts", ".js"], allow: ["s3", "v4"]}] + }, + { + code: "const v4 = 'bar';", + filename: "file.ts", + options: [{extensions: [".ts", ".js"], allow: ["s3", "v4"]}] + }, ], invalid: [ { code: "const foo2 = 'bar';", filename: "file.ts", - options: [{extensions: [".ts"]}], + options: [{extensions: [".ts", ".js"], allow: ["s3", "v4"]}], errors: [{messageId: "noNumberEnding"}], } ], } -); \ No newline at end of file +) \ No newline at end of file diff --git a/src/rules/no-variable-with-number-at-end.ts b/src/rules/no-variable-with-number-at-end.ts index 3f0e243..d2c01ff 100644 --- a/src/rules/no-variable-with-number-at-end.ts +++ b/src/rules/no-variable-with-number-at-end.ts @@ -28,14 +28,20 @@ export default createRule({ extensions: { type: "array", items: {type: "string"}, + default: [".ts", ".js"], + }, + allow: { + type: "array", + items: {type: "string"}, + default: ["s3", "v4"], } }, additionalProperties: false - } + }, ], }, - defaultOptions: [{extensions: [".ts", ".js"]}], - create(context, [{extensions}]) { + defaultOptions: [{extensions: [".ts", ".js"], allow: ["s3", "v4"]}], + create(context, [{extensions, allow}]) { const filename = context.filename; const ext = path.extname(filename); @@ -45,7 +51,10 @@ export default createRule({ return { Identifier(node) { - const variableName = node.name; + const variableName = node.name.toLowerCase(); + if (allow.includes(variableName)) { + return; + } if (/\d$/.test(variableName)) { context.report({ node,