Skip to content

Commit

Permalink
refactor: remove createSimpleRule
Browse files Browse the repository at this point in the history
  • Loading branch information
zanminkian committed Dec 28, 2024
1 parent 3f45e5f commit b82d9ee
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 83 deletions.
19 changes: 13 additions & 6 deletions packages/eslint-plugin-ts/src/rules/exact-map-set-type.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import type { Rule } from "eslint";
import type { Node } from "estree";
import { createSimpleRule, getRuleName } from "../utils.js";
import { getRuleName } from "../utils.js";

export const exactMapSetType = createSimpleRule({
name: getRuleName(import.meta.url),
message: "Disallow using Map and Set without type arguments.",
const name = getRuleName(import.meta.url);
const rule: Rule.RuleModule = {
meta: {
messages: {
default: "Disallow using Map and Set without type arguments.",
},
},
create: (context) => {
const selectors = [
// new Set();
Expand All @@ -13,8 +18,10 @@ export const exactMapSetType = createSimpleRule({
];
return {
[`:matches(${selectors.join(", ")})`]: (node: Node) => {
context.reportNode(node);
context.report({ node, messageId: "default" });
},
};
},
});
};

export const exactMapSetType = { name, rule };
19 changes: 13 additions & 6 deletions packages/eslint-plugin-ts/src/rules/no-const-enum.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import type { Rule } from "eslint";
import type { Node } from "estree";
import { createSimpleRule, getRuleName } from "../utils.js";
import { getRuleName } from "../utils.js";

export const noConstEnum = createSimpleRule({
name: getRuleName(import.meta.url),
message: "Disallow using `const enum` expression.",
const name = getRuleName(import.meta.url);
const rule: Rule.RuleModule = {
meta: {
messages: {
default: "Disallow using `const enum` expression.",
},
},
create: (context) => ({
"TSEnumDeclaration[const=true]": (node: Node) => {
context.reportNode(node);
context.report({ node, messageId: "default" });
},
}),
});
};

export const noConstEnum = { name, rule };
34 changes: 20 additions & 14 deletions packages/eslint-plugin-ts/src/rules/no-declares.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import type { Rule } from "eslint";
import type { Node } from "estree";
import { createSimpleRule, getRuleName } from "../utils.js";
import { getRuleName } from "../utils.js";

export const noDeclares = createSimpleRule({
name: getRuleName(import.meta.url),
message: "Disallow using `declare` statement.",
schema: [
{
type: "object",
properties: {
allowClassProperty: { type: "boolean" },
},
additionalProperties: false,
const name = getRuleName(import.meta.url);
const rule: Rule.RuleModule = {
meta: {
messages: {
default: "Disallow using `declare` statement.",
},
],
schema: [
{
type: "object",
properties: {
allowClassProperty: { type: "boolean" },
},
additionalProperties: false,
},
],
},
create: (context) => ({
"[declare=true]": (node: Node & { parent: Node }) => {
const { allowClassProperty = false } = context.options[0] ?? {};
Expand All @@ -23,7 +28,8 @@ export const noDeclares = createSimpleRule({
) {
return;
}
context.reportNode(node);
context.report({ node, messageId: "default" });
},
}),
});
};
export const noDeclares = { name, rule };
19 changes: 13 additions & 6 deletions packages/eslint-plugin-ts/src/rules/no-export-assignment.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import type { Rule } from "eslint";
import type { Node } from "estree";
import { createSimpleRule, getRuleName } from "../utils.js";
import { getRuleName } from "../utils.js";

export const noExportAssignment = createSimpleRule({
name: getRuleName(import.meta.url),
message: "Disallow using `export =` statement.",
const name = getRuleName(import.meta.url);
const rule: Rule.RuleModule = {
meta: {
messages: {
default: "Disallow using `export =` statement.",
},
},
create: (context) => ({
TSExportAssignment: (node: Node) => {
context.reportNode(node);
context.report({ node, messageId: "default" });
},
}),
});
};

export const noExportAssignment = { name, rule };
36 changes: 21 additions & 15 deletions packages/eslint-plugin-ts/src/rules/no-property-decorator.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import type { Rule } from "eslint";
import type { Node } from "estree";
import { createSimpleRule, getRuleName } from "../utils.js";
import { getRuleName } from "../utils.js";

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.",
schema: [
{
type: "object",
properties: {
ignoreDeclaration: { type: "boolean" },
},
additionalProperties: false,
const name = getRuleName(import.meta.url);
const rule: Rule.RuleModule = {
meta: {
messages: {
default:
"Disallow using property decorator. Consider adding `declare` keyword in front of the property to fix it.",
},
],
schema: [
{
type: "object",
properties: {
ignoreDeclaration: { type: "boolean" },
},
additionalProperties: false,
},
],
},
create: (context) => ({
"ClassBody > PropertyDefinition[decorators.length>0]": (node: Node) => {
if (
Expand All @@ -23,7 +28,8 @@ export const noPropertyDecorator = createSimpleRule({
) {
return;
}
context.reportNode(node);
context.report({ node, messageId: "default" });
},
}),
});
};
export const noPropertyDecorator = { name, rule };
20 changes: 13 additions & 7 deletions packages/eslint-plugin-ts/src/rules/no-untyped-empty-array.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import type { Rule } from "eslint";
import type { Node } from "estree";
import { createSimpleRule, getRuleName } from "../utils.js";
import { getRuleName } from "../utils.js";

export const noUntypedEmptyArray = createSimpleRule({
name: getRuleName(import.meta.url),
message:
"Defining a variable with an empty array should annotate the array type",
const name = getRuleName(import.meta.url);
const rule: Rule.RuleModule = {
meta: {
messages: {
default:
"Defining a variable with an empty array should annotate the array type",
},
},
create: (context) => ({
"VariableDeclarator:not([id.typeAnnotation]) > ArrayExpression.init[elements.length=0]":
(node: Node) => {
context.reportNode(node);
context.report({ node, messageId: "default" });
},
}),
});
};
export const noUntypedEmptyArray = { name, rule };
29 changes: 0 additions & 29 deletions packages/eslint-plugin-ts/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,5 @@
import path from "node:path";
import { fileURLToPath } from "node:url";
import type { Rule } from "eslint";
import type { Node } from "estree";
import type { JSONSchema4 } from "json-schema";

export interface Context extends Omit<Rule.RuleContext, "report"> {
reportNode: (node: Node) => void;
}

export function createSimpleRule(options: {
name: string;
message: string;
schema?: JSONSchema4[];
create: (context: Context) => Rule.RuleListener;
}): { name: string; rule: Rule.RuleModule } {
const { name, message, schema, create } = options;
const rule: Rule.RuleModule = {
meta: {
...(schema && { schema }),
},
create: (context: Rule.RuleContext) => {
const ctx = Object.assign({}, context, {
reportNode: (node: Node) => context.report({ node, message }),
});
Object.setPrototypeOf(ctx, Object.getPrototypeOf(context));
return create(ctx);
},
};
return { name, rule };
}

export function getRuleName(importMetaUrl: string) {
// remove '.js' extension
Expand Down

0 comments on commit b82d9ee

Please sign in to comment.