Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
53 changes: 49 additions & 4 deletions src/rules/no-variable-with-number-at-end.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"}],
}
],
}
);
)
17 changes: 13 additions & 4 deletions src/rules/no-variable-with-number-at-end.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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,
Expand Down