Skip to content

Commit

Permalink
feat: add @git-validator/no-untyped-empty-array, close #229
Browse files Browse the repository at this point in the history
  • Loading branch information
zanminkian committed Jul 6, 2024
1 parent fecda4a commit be32605
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/wise-moose-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@git-validator/eslint-config": patch
"@git-validator/eslint-plugin": patch
---

feat: add `@git-validator/no-untyped-empty-array`
1 change: 1 addition & 0 deletions packages/eslint-config/src/ts-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ const mainConfig = {
"@git-validator/no-declares-in-ts-file": "error",
"@git-validator/no-export-assignment": "error",
"@git-validator/no-property-decorator": "error",
"@git-validator/no-untyped-empty-array": "error",
// typescript
"@typescript-eslint/await-thenable": "error",
"@typescript-eslint/ban-ts-comment": [
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import noInstanceofBuiltin from "./rules/no-instanceof-builtin.js";
import noLegacyGetterSetter from "./rules/no-legacy-getter-setter.js";
import noPropertyDecorator from "./rules/no-property-decorator.js";
import noRelativeParentImports from "./rules/no-relative-parent-imports.js";
import noUntypedEmptyArray from "./rules/no-untyped-empty-array.js";
import preferGlobalThis from "./rules/prefer-global-this.js";
import preferShortestRelativePath from "./rules/prefer-shortest-relative-path.js";
import requireReduceInitialValue from "./rules/require-reduce-initial-value.js";
Expand All @@ -30,6 +31,7 @@ export default {
[noLegacyGetterSetter.name]: noLegacyGetterSetter.rule,
[noPropertyDecorator.name]: noPropertyDecorator.rule,
[noRelativeParentImports.name]: noRelativeParentImports.rule,
[noUntypedEmptyArray.name]: noUntypedEmptyArray.rule,
[preferGlobalThis.name]: preferGlobalThis.rule,
[preferShortestRelativePath.name]: preferShortestRelativePath.rule,
[requireReduceInitialValue.name]: requireReduceInitialValue.rule,
Expand Down
22 changes: 22 additions & 0 deletions packages/eslint-plugin/src/rules/no-untyped-empty-array.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import rule from "./no-untyped-empty-array.js";
import { test } from "./utils.spec.js";

const valid = [
"const arr: number[] = []",
"const arr: any[] = []",
"const arr = [] as unknown[]",
"const arr = ['foo']",
"const arr = [[]]",
"const arr = [{}]",
"class A {names = []}",
];

const invalid = [
"const arr = []",
"let arr = []",
"var arr = []",
"let arr1=[],arr2",
"var arr1,arr2=[]",
];

test({ valid, invalid, ...rule });
24 changes: 24 additions & 0 deletions packages/eslint-plugin/src/rules/no-untyped-empty-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createSimpleRule, getRuleName } from "../utils.js";

export default createSimpleRule({
name: getRuleName(import.meta.url),
message:
"Defining a variable with an empty array should annotate the array type",
create: (context) => ({
VariableDeclaration: (node) => {
node.declarations.forEach((declaration) => {
const { init, id } = declaration;
if (init?.type !== "ArrayExpression") {
return;
}
if (init.elements.length > 0) {
return;
}
if (id.typeAnnotation) {
return;
}
context.reportNode(declaration);
});
},
}),
});

0 comments on commit be32605

Please sign in to comment.