Skip to content
Draft
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
2 changes: 2 additions & 0 deletions eslint-local-rules/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
TVariablesShouldExtendOperationVariables,
TDataTVariablesOrder,
} from "./generics.ts";
import { enforceDocumentationTypes } from "./namespace-documentationTypes.ts";

export default {
"require-using-disposable": requireUsingDisposable,
Expand All @@ -31,4 +32,5 @@ export default {
"variables-should-extend-operation-variables":
TVariablesShouldExtendOperationVariables,
"tdata-tvariables-order": TDataTVariablesOrder,
"enforce-documentation-types": enforceDocumentationTypes,
};
72 changes: 72 additions & 0 deletions eslint-local-rules/namespace-documentationTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import type { TSESTree as AST } from "@typescript-eslint/types";
import { ESLintUtils } from "@typescript-eslint/utils";

export const enforceDocumentationTypes = ESLintUtils.RuleCreator.withoutDocs({
create(context) {
const namespaces = [];
const shouldBeDocumented: Record<
string,
{
namespaces: string[];
node: AST.TSTypeAliasDeclaration;
}
> = {};
return {
TSModuleDeclaration(node) {
if (node.kind !== "namespace") {
return;
}
namespaces.push(
node.id.type === "Identifier" ? node.id.name : "<unknown>"
);
},
"TSModuleDeclaration:exit"(node) {
if (node.kind !== "namespace") {
return;
}
namespaces.pop();
for (const [name, entry] of Object.entries(shouldBeDocumented)) {
if (entry.namespaces.length > namespaces.length) {
delete shouldBeDocumented[name];
context.report({
node: entry.node.id,
messageId: "shouldBeDocumented",
});
}
}
},
ExportNamedDeclaration(node) {
if (!node.declaration) {
return;
}
if (node.declaration.type === "TSTypeAliasDeclaration") {
const name = node.declaration.id.name;
if (name.endsWith("Result") || name.endsWith("Options")) {
shouldBeDocumented[name] = {
node: node.declaration,
namespaces: [...namespaces],
};
}
} else if (node.declaration.type === "TSInterfaceDeclaration") {
const name = node.declaration.id.name;
if (
name in shouldBeDocumented &&
namespaces.at(-1) === "DocumentationTypes"
) {
delete shouldBeDocumented[name];
}
}
},
};
},
meta: {
messages: {
shouldBeDocumented:
"This type should have an interface with the same name in a nested `DocumentationTypes` namespace.",
},
type: "problem",
schema: [],
fixable: "code",
},
defaultOptions: [],
});
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ export default [
"local-rules/valid-inherit-doc": "error",
"local-rules/variables-should-extend-operation-variables": "error",
"local-rules/tdata-tvariables-order": "error",
"local-rules/enforce-documentation-types": "error",
},
},
...compat.extends("plugin:testing-library/react").map((config) => ({
Expand Down
Loading