Skip to content

Commit

Permalink
feat(eslint-plugin): add rule no-rename-imports
Browse files Browse the repository at this point in the history
  • Loading branch information
zanminkian committed Sep 5, 2024
1 parent 4b93f1c commit 9f7bb83
Show file tree
Hide file tree
Showing 48 changed files with 126 additions and 84 deletions.
6 changes: 6 additions & 0 deletions .changeset/thick-birds-grab.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(eslint-plugin): add rule `no-rename-imports`
1 change: 1 addition & 0 deletions packages/eslint-config/src/config/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ export function javascript() {
"@git-validator/no-instanceof-builtin": "error",
"@git-validator/no-phantom-dep-imports": "error",
"@git-validator/no-relative-parent-imports": "error",
"@git-validator/no-rename-imports": "error",
"@git-validator/no-side-effect-import": "error",
"@git-validator/no-ts-file-imports": "error",
"@git-validator/no-unnecessary-template-string": "error",
Expand Down
12 changes: 6 additions & 6 deletions packages/eslint-plugin-ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { rule as exactMapSetType } from "./rules/exact-map-set-type.js";
import { rule as noConstEnum } from "./rules/no-const-enum.js";
import { rule as noDeclaresInTsFile } from "./rules/no-declares-in-ts-file.js";
import { rule as noExportAssignment } from "./rules/no-export-assignment.js";
import { rule as noPropertyDecorator } from "./rules/no-property-decorator.js";
import { rule as noUntypedEmptyArray } from "./rules/no-untyped-empty-array.js";
import { exactMapSetType } from "./rules/exact-map-set-type.js";
import { noConstEnum } from "./rules/no-const-enum.js";
import { noDeclaresInTsFile } from "./rules/no-declares-in-ts-file.js";
import { noExportAssignment } from "./rules/no-export-assignment.js";
import { noPropertyDecorator } from "./rules/no-property-decorator.js";
import { noUntypedEmptyArray } from "./rules/no-untyped-empty-array.js";

export const rules = {
[exactMapSetType.name]: exactMapSetType.rule,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from "../test.spec.js";
import { rule } from "./exact-map-set-type.js";
import { exactMapSetType } from "./exact-map-set-type.js";

const valid = [
"const m = new Map([['a','b']])",
Expand Down Expand Up @@ -35,4 +35,4 @@ const invalid = [
"type A = Map",
];

test({ valid, invalid, ...rule });
test({ valid, invalid, ...exactMapSetType });
2 changes: 1 addition & 1 deletion packages/eslint-plugin-ts/src/rules/exact-map-set-type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createSimpleRule, getRuleName } from "../utils.js";

export const rule = createSimpleRule({
export const exactMapSetType = createSimpleRule({
name: getRuleName(import.meta.url),
message: "Disallow using Map and Set without type arguments.",
create: (context) => ({
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin-ts/src/rules/no-const-enum.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { test } from "../test.spec.js";
import { rule } from "./no-const-enum.js";
import { noConstEnum } from "./no-const-enum.js";

const valid = ["enum E {}"];

const invalid = ["const enum E {}"];

test({ valid, invalid, ...rule });
test({ valid, invalid, ...noConstEnum });
2 changes: 1 addition & 1 deletion packages/eslint-plugin-ts/src/rules/no-const-enum.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createSimpleRule, getRuleName } from "../utils.js";

export const rule = createSimpleRule({
export const noConstEnum = createSimpleRule({
name: getRuleName(import.meta.url),
message: "Disallow using `const enum` expression.",
create: (context) => ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from "../test.spec.js";
import { rule } from "./no-declares-in-ts-file.js";
import { noDeclaresInTsFile } from "./no-declares-in-ts-file.js";

const codes = [
"declare class A {}",
Expand Down Expand Up @@ -37,4 +37,4 @@ const valid = [...codes, ...propertyCodes]
})),
);

test({ valid, invalid, ...rule });
test({ valid, invalid, ...noDeclaresInTsFile });
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { TSESTree } from "@typescript-eslint/utils";
import { createSimpleRule, getRuleName } from "../utils.js";

export const rule = createSimpleRule({
export const noDeclaresInTsFile = createSimpleRule({
name: getRuleName(import.meta.url),
message: "Disallow using `declare` statement in ts file.",
schema: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from "../test.spec.js";
import { rule } from "./no-export-assignment.js";
import { noExportAssignment } from "./no-export-assignment.js";

const valid = [
{ code: "export default {}", filename: "test.ts" },
Expand All @@ -8,4 +8,4 @@ const valid = [

const invalid = [{ code: "export = {}", filename: "test.ts" }];

test({ valid, invalid, ...rule });
test({ valid, invalid, ...noExportAssignment });
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createSimpleRule, getRuleName } from "../utils.js";

export const rule = createSimpleRule({
export const noExportAssignment = createSimpleRule({
name: getRuleName(import.meta.url),
message: "Disallow using `export =` statement.",
create: (context) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from "../test.spec.js";
import { rule } from "./no-property-decorator.js";
import { noPropertyDecorator } from "./no-property-decorator.js";

const valid = [
`class A {
Expand Down Expand Up @@ -37,4 +37,4 @@ const invalid = [
}`,
];

test({ valid, invalid, ...rule });
test({ valid, invalid, ...noPropertyDecorator });
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { TSESTree } from "@typescript-eslint/utils";
import { createSimpleRule, getRuleName } from "../utils.js";

export const rule = createSimpleRule({
export const noPropertyDecorator = createSimpleRule({
name: getRuleName(import.meta.url),
message:
"Disallow using property decorator. Consider adding `declare` keyword in front of the property to fix it.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from "../test.spec.js";
import { rule } from "./no-untyped-empty-array.js";
import { noUntypedEmptyArray } from "./no-untyped-empty-array.js";

const valid = [
"const arr: number[] = []",
Expand All @@ -20,4 +20,4 @@ const invalid = [
"var arr1,arr2=[]",
];

test({ valid, invalid, ...rule });
test({ valid, invalid, ...noUntypedEmptyArray });
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createSimpleRule, getRuleName } from "../utils.js";

export const rule = createSimpleRule({
export const noUntypedEmptyArray = createSimpleRule({
name: getRuleName(import.meta.url),
message:
"Defining a variable with an empty array should annotate the array type",
Expand Down
32 changes: 17 additions & 15 deletions packages/eslint-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { rule as banTsComment } from "./rules/ban-ts-comment.js";
import { rule as newParens } from "./rules/new-parens.js";
import { rule as noDirectoryImports } from "./rules/no-directory-imports.js";
import { rule as noDynamicImport } from "./rules/no-dynamic-import.js";
import { rule as noForIn } from "./rules/no-for-in.js";
import { rule as noGitIgnoredImports } from "./rules/no-git-ignored-imports.js";
import { rule as noInstanceofBuiltin } from "./rules/no-instanceof-builtin.js";
import { rule as noPhantomDepImports } from "./rules/no-phantom-dep-imports.js";
import { rule as noRelativeParentImports } from "./rules/no-relative-parent-imports.js";
import { rule as noSideEffectImport } from "./rules/no-side-effect-import.js";
import { rule as noTsFileImports } from "./rules/no-ts-file-imports.js";
import { rule as noUnnecessaryTemplateString } from "./rules/no-unnecessary-template-string.js";
import { rule as preferGlobalThis } from "./rules/prefer-global-this.js";
import { rule as preferShortestRelativePath } from "./rules/prefer-shortest-relative-path.js";
import { rule as requireReduceInitialValue } from "./rules/require-reduce-initial-value.js";
import { banTsComment } from "./rules/ban-ts-comment.js";
import { newParens } from "./rules/new-parens.js";
import { noDirectoryImports } from "./rules/no-directory-imports.js";
import { noDynamicImport } from "./rules/no-dynamic-import.js";
import { noForIn } from "./rules/no-for-in.js";
import { noGitIgnoredImports } from "./rules/no-git-ignored-imports.js";
import { noInstanceofBuiltin } from "./rules/no-instanceof-builtin.js";
import { noPhantomDepImports } from "./rules/no-phantom-dep-imports.js";
import { noRelativeParentImports } from "./rules/no-relative-parent-imports.js";
import { noRenameImports } from "./rules/no-rename-imports.js";
import { noSideEffectImport } from "./rules/no-side-effect-import.js";
import { noTsFileImports } from "./rules/no-ts-file-imports.js";
import { noUnnecessaryTemplateString } from "./rules/no-unnecessary-template-string.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";

export const rules = {
[banTsComment.name]: banTsComment.rule,
Expand All @@ -24,6 +25,7 @@ export const rules = {
[noInstanceofBuiltin.name]: noInstanceofBuiltin.rule,
[noPhantomDepImports.name]: noPhantomDepImports.rule,
[noRelativeParentImports.name]: noRelativeParentImports.rule,
[noRenameImports.name]: noRenameImports.rule,
[noSideEffectImport.name]: noSideEffectImport.rule,
[noTsFileImports.name]: noTsFileImports.rule,
[noUnnecessaryTemplateString.name]: noUnnecessaryTemplateString.rule,
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/ban-ts-comment.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from "../test.spec.js";
import { rule } from "./ban-ts-comment.js";
import { banTsComment } from "./ban-ts-comment.js";

const directives = ["@ts-ignore", "@ts-expect-error", "@ts-nocheck"];

Expand All @@ -19,4 +19,4 @@ const invalid = directives.flatMap((d) => [
`/** ${d} "${d}" */`,
]);

test({ valid, invalid, ...rule });
test({ valid, invalid, ...banTsComment });
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/ban-ts-comment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createSimpleRule, getRuleName } from "../utils.js";

export const rule = createSimpleRule({
export const banTsComment = createSimpleRule({
name: getRuleName(import.meta.url),
message: "Disallow using ts comment to suppress compilation errors.",
create: (context) => ({
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/new-parens.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { test } from "../test.spec.js";
import { rule } from "./new-parens.js";
import { newParens } from "./new-parens.js";

const valid = ["const a = new Student();", "const a = new Student('jim');"];

const invalid = ["const a = new Student;"];

test({ valid, invalid, ...rule });
test({ valid, invalid, ...newParens });
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/new-parens.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createSimpleRule, getRuleName } from "../utils.js";

export const rule = createSimpleRule({
export const newParens = createSimpleRule({
name: getRuleName(import.meta.url),
message: "When invoking a constructor, parentheses are required.",
create: (context) => ({
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/no-directory-imports.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import process from "node:process";
import { fileURLToPath } from "node:url";
import { test } from "../test.spec.js";
import { rule } from "./no-directory-imports.js";
import { noDirectoryImports } from "./no-directory-imports.js";

const valid = [
"import foo from 'foo'",
Expand All @@ -28,4 +28,4 @@ const invalid = [
filename: fileURLToPath(import.meta.url),
}));

test({ valid, invalid, ...rule });
test({ valid, invalid, ...noDirectoryImports });
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/no-directory-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from "node:path";
import { create, isRelativeImport } from "../check-import.js";
import { createSimpleRule, getRuleName } from "../utils.js";

export const rule = createSimpleRule({
export const noDirectoryImports = createSimpleRule({
name: getRuleName(import.meta.url),
message: "Disallow importing from a directory.",
create: (context) => create(context, check),
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/no-dynamic-import.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from "../test.spec.js";
import { rule } from "./no-dynamic-import.js";
import { noDynamicImport } from "./no-dynamic-import.js";

const valid = [
"import('foo')",
Expand All @@ -24,4 +24,4 @@ const invalid = [
// 'import("foo", {})', // only ts support it, not ecmascript
];

test({ valid, invalid, ...rule });
test({ valid, invalid, ...noDynamicImport });
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/no-dynamic-import.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Node } from "estree";
import { createSimpleRule, getRuleName } from "../utils.js";

export const rule = createSimpleRule({
export const noDynamicImport = createSimpleRule({
name: getRuleName(import.meta.url),
message: "`import()` should be called with string literal.",
create: (context) => ({
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/no-for-in.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from "../test.spec.js";
import { rule } from "./no-for-in.js";
import { noForIn } from "./no-for-in.js";

const valid = [
"for(const i of arr) {}",
Expand All @@ -8,4 +8,4 @@ const valid = [

const invalid = ["for(const i in arr) {}"];

test({ valid, invalid, ...rule });
test({ valid, invalid, ...noForIn });
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/no-for-in.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createSimpleRule, getRuleName } from "../utils.js";

export const rule = createSimpleRule({
export const noForIn = createSimpleRule({
name: getRuleName(import.meta.url),
message: "Disallow using for-in statement.",
create: (context) => ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import process from "node:process";
import { fileURLToPath } from "node:url";
import { test } from "../test.spec.js";
import { rule } from "./no-git-ignored-imports.js";
import { noGitIgnoredImports } from "./no-git-ignored-imports.js";

const valid = [
"import foo from 'foo'",
Expand Down Expand Up @@ -38,4 +38,4 @@ const invalid = [
filename: fileURLToPath(import.meta.url),
}));

test({ valid, invalid, ...rule });
test({ valid, invalid, ...noGitIgnoredImports });
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/no-git-ignored-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { isNativeError } from "node:util/types";
import { create, isRelativeImport } from "../check-import.js";
import { createSimpleRule, getRuleName } from "../utils.js";

export const rule = createSimpleRule({
export const noGitIgnoredImports = createSimpleRule({
name: getRuleName(import.meta.url),
message: "Disallow to import module from git-ignored path.",
create: (context) => create(context, checkIgnored),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from "../test.spec.js";
import { rule } from "./no-instanceof-builtin.js";
import { noInstanceofBuiltin } from "./no-instanceof-builtin.js";

const invalid = [
// Primitive
Expand Down Expand Up @@ -45,4 +45,4 @@ const valid = [
"const i = {} instanceof await import('http')",
];

test({ valid, invalid, ...rule });
test({ valid, invalid, ...noInstanceofBuiltin });
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/no-instanceof-builtin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createSimpleRule, getRuleName } from "../utils.js";

export const rule = createSimpleRule({
export const noInstanceofBuiltin = createSimpleRule({
name: getRuleName(import.meta.url),
message: "Right hand of `instanceof` can't be a builtin class.",
create: (context) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from "node:path";
import process from "node:process";
import { fileURLToPath } from "node:url";
import { test } from "../test.spec.js";
import { rule } from "./no-phantom-dep-imports.js";
import { noPhantomDepImports } from "./no-phantom-dep-imports.js";

const valid = [
{ code: "import foo from '/foo'" },
Expand All @@ -24,4 +24,4 @@ const invalid = [
},
];

test({ valid, invalid, ...rule });
test({ valid, invalid, ...noPhantomDepImports });
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/no-phantom-dep-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function getPkgJson(
return getPkgJson(path.join(dir, ".."));
}

export const rule = createSimpleRule({
export const noPhantomDepImports = createSimpleRule({
name: getRuleName(import.meta.url),
message: "The nearest `package.json` doesn't have such dependency.",
create: (context) => create(context, check),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from "../test.spec.js";
import { rule } from "./no-relative-parent-imports.js";
import { noRelativeParentImports } from "./no-relative-parent-imports.js";

const valid = [
"import foo from 'foo'",
Expand All @@ -26,4 +26,4 @@ const invalid = [
"import foo from '../../../../foo'",
];

test({ valid, invalid, ...rule });
test({ valid, invalid, ...noRelativeParentImports });
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createSimpleRule, getRuleName } from "../utils.js";

const depth = 3;

export const rule = createSimpleRule({
export const noRelativeParentImports = createSimpleRule({
name: getRuleName(import.meta.url),
message: "Disallow to import module from relative parent path too deeply.",
create: (context) => create(context, checkDepth),
Expand Down
Loading

0 comments on commit 9f7bb83

Please sign in to comment.