eslint-plugin-one-function-per-file 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,21 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented here. The format follows
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project follows
5
+ [Semantic Versioning](https://semver.org/).
6
+
7
+ ## [0.1.0] - 2026-11-24
8
+
9
+ ### Added
10
+
11
+ - `one-function-per-file` rule: at most one module-level function per file. It counts function
12
+ declarations, `const` / `let` / `var` bindings initialised with an arrow or function expression
13
+ (also through `as`, `satisfies`, `<T>x` and `!`), and default exports of an arrow or function.
14
+ - `recommended` flat config: the rule plus `const`-arrow-only style (`func-style`,
15
+ `prefer-arrow-callback`, a `no-restricted-syntax` selector) and required TSDoc comments
16
+ (`jsdoc/*` and `tsdoc/syntax`), with TSDoc tags such as `@remarks` and `@typeParam` accepted.
17
+ - `minimal` flat config: only the rule.
18
+ - Type declarations and an ESM-only build. CommonJS configs can load it with `require()` on the
19
+ supported Node versions and read the plugin from `.default`.
20
+
21
+ [0.1.0]: https://github.com/Vaporjawn/eslint-plugin-one-function-per-file/releases/tag/v0.1.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Victor Williams
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,164 @@
1
- # Temporary Holding Version
1
+ # eslint-plugin-one-function-per-file
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![npm](https://img.shields.io/npm/v/eslint-plugin-one-function-per-file.svg)](https://www.npmjs.com/package/eslint-plugin-one-function-per-file)
4
+ [![CI](https://github.com/Vaporjawn/eslint-plugin-one-function-per-file/actions/workflows/ci.yml/badge.svg)](https://github.com/Vaporjawn/eslint-plugin-one-function-per-file/actions/workflows/ci.yml)
5
+ [![license](https://img.shields.io/npm/l/eslint-plugin-one-function-per-file.svg)](LICENSE)
6
+
7
+ An ESLint flat-config plugin and preset for a strict, easy-to-navigate code style:
8
+
9
+ 1. **One function per file.** A module defines at most one function at module level.
10
+ 2. **`const` arrow functions only.** `const name = () => {}`, never `function name() {}`.
11
+ 3. **A TSDoc comment on every named function**, with a description, no `{types}` in the comment.
12
+
13
+ Small single-purpose files are easier to find, review, test and delete. The preset makes that a rule
14
+ the linter enforces, instead of a habit people forget.
15
+
16
+ > Requires ESLint 9 or 10 (flat config) and Node.js `^20.19.0 || ^22.13.0 || >=24`.
17
+
18
+ ## Install
19
+
20
+ ```sh
21
+ npm install --save-dev eslint eslint-plugin-one-function-per-file
22
+ ```
23
+
24
+ `eslint-plugin-jsdoc` and `eslint-plugin-tsdoc` come with it, so there is nothing else to install.
25
+
26
+ ## Usage
27
+
28
+ ### JavaScript
29
+
30
+ ```js
31
+ // eslint.config.js
32
+ import oneFunctionPerFile from "eslint-plugin-one-function-per-file";
33
+
34
+ export default [oneFunctionPerFile.configs.recommended];
35
+ ```
36
+
37
+ ### TypeScript
38
+
39
+ The preset does not choose a parser. Add yours as usual:
40
+
41
+ ```js
42
+ // eslint.config.js
43
+ import tseslint from "typescript-eslint";
44
+ import oneFunctionPerFile from "eslint-plugin-one-function-per-file";
45
+
46
+ export default tseslint.config(
47
+ ...tseslint.configs.recommended,
48
+ oneFunctionPerFile.configs.recommended,
49
+ );
50
+ ```
51
+
52
+ ### CommonJS config
53
+
54
+ The package is ESM-only. A CommonJS `eslint.config.cjs` can still load it on the supported Node
55
+ versions, through `require()` of an ES module. Read the plugin from `.default`:
56
+
57
+ ```js
58
+ // eslint.config.cjs
59
+ const oneFunctionPerFile = require("eslint-plugin-one-function-per-file").default;
60
+
61
+ module.exports = [oneFunctionPerFile.configs.recommended];
62
+ ```
63
+
64
+ ### Only some folders
65
+
66
+ Both presets are plain config objects, so scope them with `files`:
67
+
68
+ ```js
69
+ export default [{ ...oneFunctionPerFile.configs.recommended, files: ["src/**/*.ts"] }];
70
+ ```
71
+
72
+ ### Just the rule
73
+
74
+ If you only want the file-structure rule and none of the other plugins:
75
+
76
+ ```js
77
+ import oneFunctionPerFile from "eslint-plugin-one-function-per-file";
78
+
79
+ export default [oneFunctionPerFile.configs.minimal];
80
+ // or, by hand:
81
+ // { plugins: { "one-function-per-file": oneFunctionPerFile },
82
+ // rules: { "one-function-per-file/one-function-per-file": "error" } }
83
+ ```
84
+
85
+ ## Presets
86
+
87
+ | Preset | What it enforces |
88
+ | ------------- | ------------------------------------------------------------------------------ |
89
+ | `recommended` | One function per file, `const` arrow functions, TSDoc on every named function. |
90
+ | `minimal` | Only `one-function-per-file/one-function-per-file`. |
91
+
92
+ Both apply to `**/*.{js,mjs,cjs,jsx,ts,mts,cts,tsx}` and skip `**/*.d.ts`.
93
+
94
+ ### What `recommended` turns on
95
+
96
+ | Rule | Setting | Why |
97
+ | --------------------------------------------- | ------------------------------------------------- | ----------------------------------------------------- |
98
+ | `one-function-per-file/one-function-per-file` | `error` | One function per file. |
99
+ | `func-style` | `["error", "expression"]` | No `function name() {}` declarations. |
100
+ | `prefer-arrow-callback` | `error` | Callbacks are arrows too. |
101
+ | `no-restricted-syntax` | `const f = function () {}` selector | Function expressions bound to a name are not allowed. |
102
+ | `jsdoc/require-jsdoc` | declarations, `const` functions, methods, classes | Every named function is documented. |
103
+ | `jsdoc/require-description` | `error` | The comment must say what the function does. |
104
+ | `jsdoc/no-types` | `error` | TSDoc reads types from the signature. |
105
+ | `jsdoc/check-tag-names` | `error`, TSDoc tags allowed | Typos in tags are caught; `@remarks` etc. are fine. |
106
+ | `jsdoc/check-param-names` | `error` | `@param` names match the signature. |
107
+ | `tsdoc/syntax` | `error` | Comments are valid TSDoc. |
108
+
109
+ Override any of them after the preset, as with any ESLint config:
110
+
111
+ ```js
112
+ export default [
113
+ oneFunctionPerFile.configs.recommended,
114
+ { rules: { "jsdoc/require-description": "off" } },
115
+ ];
116
+ ```
117
+
118
+ ## The rule: `one-function-per-file`
119
+
120
+ See [docs/rules/one-function-per-file.md](docs/rules/one-function-per-file.md) for every case.
121
+
122
+ ```ts
123
+ // Reported: the second function.
124
+ export const double = (n: number) => n * 2;
125
+ export const triple = (n: number) => n * 3; // 'triple' is a second function in this file (first: 'double').
126
+ ```
127
+
128
+ **Counted:** function declarations; `const` / `let` / `var` bindings initialised with an arrow or
129
+ function expression (also through `as`, `satisfies`, `!`); default exports of an arrow or function.
130
+
131
+ **Not counted:** functions nested inside the one function, class and object methods, test callbacks
132
+ passed to `describe` / `it`, calls such as `memo(() => ...)`, types, interfaces, constants, and
133
+ `declare`d functions or overload signatures.
134
+
135
+ ## FAQ
136
+
137
+ **My React component file has a component and a helper.** Move the helper to its own file. That is the
138
+ point of the rule. If you would rather not apply it to a folder, scope the preset with `files`.
139
+
140
+ **Does it apply to test files?** Yes by default. Callbacks inside `describe` / `it` are not
141
+ module-level functions, so ordinary tests pass. Scope it away from `tests/` with `files` or `ignores`
142
+ if you write many helpers there.
143
+
144
+ **Can I use `function` declarations if I turn off the other rules?** Yes. Use the `minimal` preset or
145
+ turn off `func-style` and `no-restricted-syntax`; the file rule counts declarations too.
146
+
147
+ **Why not `max-classes-per-file` or `max-lines-per-function`?** They count something else.
148
+ `max-classes-per-file` only counts classes, and `max-lines-per-function` measures one function's length.
149
+
150
+ ## Known advisory
151
+
152
+ `npm audit` reports a moderate `minimatch` denial-of-service advisory
153
+ ([GHSA-g3hv-75wg-m249](https://github.com/advisories/GHSA-g3hv-75wg-m249)) through
154
+ `eslint-plugin-tsdoc`, which pins an older `@typescript-eslint/utils`. It is a lint-time dependency
155
+ that only matches glob patterns from your own config, and `0.5.4` is the latest release of that plugin.
156
+ This package will pick up a fixed release as soon as one exists.
157
+
158
+ ## Contributing
159
+
160
+ See [CONTRIBUTING.md](CONTRIBUTING.md). Changes are listed in [CHANGELOG.md](CHANGELOG.md).
161
+
162
+ ## License
163
+
164
+ [MIT](LICENSE) © Victor Williams
@@ -0,0 +1,8 @@
1
+ import type { ESLint, Linter } from "eslint";
2
+ /**
3
+ * Builds the `minimal` flat config: only the `one-function-per-file` rule, with no other plugin.
4
+ *
5
+ * @param plugin - The plugin object to register under the `one-function-per-file` namespace.
6
+ * @returns A flat config object ready to spread into `eslint.config.js`.
7
+ */
8
+ export declare const createMinimalConfig: (plugin: ESLint.Plugin) => Linter.Config;
@@ -0,0 +1,15 @@
1
+ import { PLUGIN_NAME } from "../meta.js";
2
+ import { IGNORED_FILES, MINIMAL_RULES, SOURCE_FILES } from "./rules.js";
3
+ /**
4
+ * Builds the `minimal` flat config: only the `one-function-per-file` rule, with no other plugin.
5
+ *
6
+ * @param plugin - The plugin object to register under the `one-function-per-file` namespace.
7
+ * @returns A flat config object ready to spread into `eslint.config.js`.
8
+ */
9
+ export const createMinimalConfig = (plugin) => ({
10
+ name: `${PLUGIN_NAME}/minimal`,
11
+ files: SOURCE_FILES,
12
+ ignores: IGNORED_FILES,
13
+ plugins: { [PLUGIN_NAME]: plugin },
14
+ rules: MINIMAL_RULES,
15
+ });
@@ -0,0 +1,10 @@
1
+ import type { ESLint, Linter } from "eslint";
2
+ /**
3
+ * Builds the `recommended` flat config: one function per file, `const` arrow functions, and a
4
+ * TSDoc comment on every named function. It registers `jsdoc` and `tsdoc` itself, so the consumer
5
+ * installs nothing extra.
6
+ *
7
+ * @param plugin - The plugin object to register under the `one-function-per-file` namespace.
8
+ * @returns A flat config object ready to spread into `eslint.config.js`.
9
+ */
10
+ export declare const createRecommendedConfig: (plugin: ESLint.Plugin) => Linter.Config;
@@ -0,0 +1,21 @@
1
+ import jsdoc from "eslint-plugin-jsdoc";
2
+ import tsdoc from "eslint-plugin-tsdoc";
3
+ import { PLUGIN_NAME } from "../meta.js";
4
+ import { IGNORED_FILES, RECOMMENDED_RULES, SOURCE_FILES } from "./rules.js";
5
+ /**
6
+ * Builds the `recommended` flat config: one function per file, `const` arrow functions, and a
7
+ * TSDoc comment on every named function. It registers `jsdoc` and `tsdoc` itself, so the consumer
8
+ * installs nothing extra.
9
+ *
10
+ * @param plugin - The plugin object to register under the `one-function-per-file` namespace.
11
+ * @returns A flat config object ready to spread into `eslint.config.js`.
12
+ */
13
+ export const createRecommendedConfig = (plugin) => ({
14
+ name: `${PLUGIN_NAME}/recommended`,
15
+ files: SOURCE_FILES,
16
+ ignores: IGNORED_FILES,
17
+ plugins: { [PLUGIN_NAME]: plugin, jsdoc, tsdoc },
18
+ // TSDoc spells generics `@typeParam`; `eslint-plugin-jsdoc` would otherwise insist on `@template`.
19
+ settings: { jsdoc: { mode: "typescript", tagNamePreference: { template: "typeParam" } } },
20
+ rules: RECOMMENDED_RULES,
21
+ });
@@ -0,0 +1,15 @@
1
+ import type { Linter } from "eslint";
2
+ /** Source files the preset applies to. TypeScript files also need a TypeScript parser configured. */
3
+ export declare const SOURCE_FILES: string[];
4
+ /** Declaration files only declare shapes, so the preset leaves them alone. */
5
+ export declare const IGNORED_FILES: string[];
6
+ /** Just the file-structure rule. */
7
+ export declare const MINIMAL_RULES: Linter.RulesRecord;
8
+ /**
9
+ * Tags TSDoc defines that `eslint-plugin-jsdoc` does not know in `typescript` mode. Without this
10
+ * list `jsdoc/check-tag-names` would reject a valid TSDoc comment. (`@typeParam` is handled by the
11
+ * `tagNamePreference` setting in `createRecommendedConfig`, because jsdoc already knows it as an alias.)
12
+ */
13
+ export declare const TSDOC_TAGS: string[];
14
+ /** The whole preset: one function per file, `const` arrow functions, and a TSDoc comment on each. */
15
+ export declare const RECOMMENDED_RULES: Linter.RulesRecord;
@@ -0,0 +1,68 @@
1
+ import { PLUGIN_NAME } from "../meta.js";
2
+ /** Source files the preset applies to. TypeScript files also need a TypeScript parser configured. */
3
+ export const SOURCE_FILES = ["**/*.{js,mjs,cjs,jsx,ts,mts,cts,tsx}"];
4
+ /** Declaration files only declare shapes, so the preset leaves them alone. */
5
+ export const IGNORED_FILES = ["**/*.d.ts"];
6
+ /** Just the file-structure rule. */
7
+ export const MINIMAL_RULES = {
8
+ [`${PLUGIN_NAME}/${PLUGIN_NAME}`]: "error",
9
+ };
10
+ /**
11
+ * Tags TSDoc defines that `eslint-plugin-jsdoc` does not know in `typescript` mode. Without this
12
+ * list `jsdoc/check-tag-names` would reject a valid TSDoc comment. (`@typeParam` is handled by the
13
+ * `tagNamePreference` setting in `createRecommendedConfig`, because jsdoc already knows it as an alias.)
14
+ */
15
+ export const TSDOC_TAGS = [
16
+ "alpha",
17
+ "beta",
18
+ "decorator",
19
+ "defaultValue",
20
+ "eventProperty",
21
+ "experimental",
22
+ "inheritDoc",
23
+ "label",
24
+ "packageDocumentation",
25
+ "privateRemarks",
26
+ "remarks",
27
+ "sealed",
28
+ "virtual",
29
+ ];
30
+ /** The whole preset: one function per file, `const` arrow functions, and a TSDoc comment on each. */
31
+ export const RECOMMENDED_RULES = {
32
+ ...MINIMAL_RULES,
33
+ // Always `const name = () => {}`, never `function name() {}` or `const name = function () {}`.
34
+ "func-style": ["error", "expression"],
35
+ "prefer-arrow-callback": "error",
36
+ "no-restricted-syntax": [
37
+ "error",
38
+ {
39
+ selector: "VariableDeclarator > FunctionExpression",
40
+ message: "Use an arrow function: `const name = () => {}`.",
41
+ },
42
+ ],
43
+ "jsdoc/require-jsdoc": [
44
+ "error",
45
+ {
46
+ publicOnly: false,
47
+ // Named functions: declarations, `const f = () => {}`, methods, classes. Anonymous callbacks
48
+ // and lambdas inside object literals are not "functions" for this rule.
49
+ require: {
50
+ FunctionDeclaration: true,
51
+ ArrowFunctionExpression: false,
52
+ FunctionExpression: false,
53
+ MethodDefinition: true,
54
+ ClassDeclaration: true,
55
+ },
56
+ contexts: [
57
+ "VariableDeclarator > ArrowFunctionExpression",
58
+ "VariableDeclarator > FunctionExpression",
59
+ "ExportDefaultDeclaration > ArrowFunctionExpression",
60
+ ],
61
+ },
62
+ ],
63
+ "jsdoc/require-description": ["error", { descriptionStyle: "body" }],
64
+ "jsdoc/no-types": "error", // TSDoc takes types from the signature, never from `{type}`
65
+ "jsdoc/check-tag-names": ["error", { definedTags: TSDOC_TAGS }],
66
+ "jsdoc/check-param-names": "error",
67
+ "tsdoc/syntax": "error",
68
+ };
@@ -0,0 +1,5 @@
1
+ import { plugin } from "./plugin.js";
2
+ export { plugin };
3
+ export { oneFunctionPerFile } from "./rules/oneFunctionPerFile/index.js";
4
+ export { PLUGIN_NAME } from "./meta.js";
5
+ export default plugin;
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ import { plugin } from "./plugin.js";
2
+ export { plugin };
3
+ export { oneFunctionPerFile } from "./rules/oneFunctionPerFile/index.js";
4
+ export { PLUGIN_NAME } from "./meta.js";
5
+ export default plugin;
package/dist/meta.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ /** The namespace the plugin's rules are registered under: `one-function-per-file/<rule>`. */
2
+ export declare const PLUGIN_NAME = "one-function-per-file";
3
+ /** Plugin identity, shown by ESLint's config inspector and used for cache invalidation. */
4
+ export declare const PLUGIN_META: {
5
+ readonly name: "eslint-plugin-one-function-per-file";
6
+ readonly version: string;
7
+ };
package/dist/meta.js ADDED
@@ -0,0 +1,6 @@
1
+ import { createRequire } from "node:module";
2
+ /** The namespace the plugin's rules are registered under: `one-function-per-file/<rule>`. */
3
+ export const PLUGIN_NAME = "one-function-per-file";
4
+ const { version } = createRequire(import.meta.url)("../package.json");
5
+ /** Plugin identity, shown by ESLint's config inspector and used for cache invalidation. */
6
+ export const PLUGIN_META = { name: "eslint-plugin-one-function-per-file", version };
@@ -0,0 +1,13 @@
1
+ import type { ESLint, Linter } from "eslint";
2
+ /**
3
+ * The two flat configs the plugin ships: `recommended` (one function per file, `const` arrow
4
+ * functions, and a TSDoc comment on every named function) and `minimal` (only the rule).
5
+ */
6
+ export type PluginConfigs = Record<"recommended" | "minimal", Linter.Config>;
7
+ /**
8
+ * The ESLint plugin: the `one-function-per-file` rule plus two flat configs, `recommended` and
9
+ * `minimal`. The configs reference the plugin itself, so they are filled in after it exists.
10
+ */
11
+ export declare const plugin: ESLint.Plugin & {
12
+ configs: PluginConfigs;
13
+ };
package/dist/plugin.js ADDED
@@ -0,0 +1,16 @@
1
+ import { createMinimalConfig } from "./configs/createMinimalConfig.js";
2
+ import { createRecommendedConfig } from "./configs/createRecommendedConfig.js";
3
+ import { PLUGIN_META, PLUGIN_NAME } from "./meta.js";
4
+ import { oneFunctionPerFile } from "./rules/oneFunctionPerFile/index.js";
5
+ const configs = {};
6
+ /**
7
+ * The ESLint plugin: the `one-function-per-file` rule plus two flat configs, `recommended` and
8
+ * `minimal`. The configs reference the plugin itself, so they are filled in after it exists.
9
+ */
10
+ export const plugin = {
11
+ meta: PLUGIN_META,
12
+ rules: { [PLUGIN_NAME]: oneFunctionPerFile },
13
+ configs,
14
+ };
15
+ configs.recommended = createRecommendedConfig(plugin);
16
+ configs.minimal = createMinimalConfig(plugin);
@@ -0,0 +1,15 @@
1
+ import type { Node } from "estree";
2
+ import type { FunctionDefinition } from "./types.js";
3
+ /**
4
+ * The module-level functions one top-level statement defines.
5
+ *
6
+ * Counted: a function declaration; `const` / `let` / `var` bindings initialised with an arrow or
7
+ * function expression; and a default export of an arrow or function expression. Exported
8
+ * statements are unwrapped first. Not counted: anything nested inside a function, class methods,
9
+ * object-literal methods, calls such as `memo(() => ...)`, overload signatures and `declare`d
10
+ * functions.
11
+ *
12
+ * @param statement - A statement from `Program.body`.
13
+ * @returns One entry per function, with the node to report on and its name.
14
+ */
15
+ export declare const functionsDefinedBy: (statement: Node) => readonly FunctionDefinition[];
@@ -0,0 +1,37 @@
1
+ import { isFunctionValue } from "./isFunctionValue.js";
2
+ /**
3
+ * The module-level functions one top-level statement defines.
4
+ *
5
+ * Counted: a function declaration; `const` / `let` / `var` bindings initialised with an arrow or
6
+ * function expression; and a default export of an arrow or function expression. Exported
7
+ * statements are unwrapped first. Not counted: anything nested inside a function, class methods,
8
+ * object-literal methods, calls such as `memo(() => ...)`, overload signatures and `declare`d
9
+ * functions.
10
+ *
11
+ * @param statement - A statement from `Program.body`.
12
+ * @returns One entry per function, with the node to report on and its name.
13
+ */
14
+ export const functionsDefinedBy = (statement) => {
15
+ // `export default function () {}` has no `id` at runtime, which the ESTree types do not model.
16
+ const declaration = (statement.type === "ExportNamedDeclaration" || statement.type === "ExportDefaultDeclaration"
17
+ ? statement.declaration
18
+ : statement);
19
+ if (!declaration)
20
+ return [];
21
+ if (declaration.type === "FunctionDeclaration") {
22
+ const { id } = declaration;
23
+ return [{ node: declaration, name: id?.name ?? "default export" }];
24
+ }
25
+ if (declaration.type === "VariableDeclaration") {
26
+ return declaration.declarations
27
+ .filter((declarator) => isFunctionValue(declarator.init))
28
+ .map((declarator) => ({
29
+ node: declarator,
30
+ name: declarator.id.type === "Identifier" ? declarator.id.name : "anonymous",
31
+ }));
32
+ }
33
+ if (statement.type === "ExportDefaultDeclaration" && isFunctionValue(declaration)) {
34
+ return [{ node: declaration, name: "default export" }];
35
+ }
36
+ return [];
37
+ };
@@ -0,0 +1,10 @@
1
+ import type { Rule } from "eslint";
2
+ /**
3
+ * Rule `one-function-per-file`: a file defines at most ONE function at module level.
4
+ *
5
+ * Counted: function declarations, `const` / `let` / `var` bindings initialised with an arrow or
6
+ * function expression (exported or not), and a default export of an arrow or function expression.
7
+ * Not counted: functions nested inside that function (callbacks, closures), class methods, test
8
+ * callbacks passed to `describe` / `it`, calls such as `memo(() => ...)`, types, and constants.
9
+ */
10
+ export declare const oneFunctionPerFile: Rule.RuleModule;
@@ -0,0 +1,37 @@
1
+ import { functionsDefinedBy } from "./functionsDefinedBy.js";
2
+ /**
3
+ * Rule `one-function-per-file`: a file defines at most ONE function at module level.
4
+ *
5
+ * Counted: function declarations, `const` / `let` / `var` bindings initialised with an arrow or
6
+ * function expression (exported or not), and a default export of an arrow or function expression.
7
+ * Not counted: functions nested inside that function (callbacks, closures), class methods, test
8
+ * callbacks passed to `describe` / `it`, calls such as `memo(() => ...)`, types, and constants.
9
+ */
10
+ export const oneFunctionPerFile = {
11
+ meta: {
12
+ type: "suggestion",
13
+ docs: {
14
+ description: "Allow at most one module-level function per file.",
15
+ recommended: false,
16
+ url: "https://github.com/Vaporjawn/eslint-plugin-one-function-per-file/blob/main/docs/rules/one-function-per-file.md",
17
+ },
18
+ schema: [],
19
+ messages: {
20
+ tooMany: "'{{name}}' is a second function in this file (first: '{{first}}'). Put each function in its own file.",
21
+ },
22
+ },
23
+ create: (context) => ({
24
+ Program: (program) => {
25
+ const [first, ...extras] = program.body.flatMap(functionsDefinedBy);
26
+ if (!first)
27
+ return;
28
+ for (const extra of extras) {
29
+ context.report({
30
+ node: extra.node,
31
+ messageId: "tooMany",
32
+ data: { name: extra.name, first: first.name },
33
+ });
34
+ }
35
+ },
36
+ }),
37
+ };
@@ -0,0 +1,9 @@
1
+ import type { Node } from "estree";
2
+ /**
3
+ * Whether an expression is an arrow function or a function expression, ignoring any TypeScript
4
+ * `as` / `satisfies` / `!` wrapper around it.
5
+ *
6
+ * @param node - Any ESTree expression node, or a missing initialiser.
7
+ * @returns `true` for `() => {}` and `function () {}` values.
8
+ */
9
+ export declare const isFunctionValue: (node: Node | null | undefined) => boolean;
@@ -0,0 +1,12 @@
1
+ import { unwrapTypeWrappers } from "./unwrapTypeWrappers.js";
2
+ /**
3
+ * Whether an expression is an arrow function or a function expression, ignoring any TypeScript
4
+ * `as` / `satisfies` / `!` wrapper around it.
5
+ *
6
+ * @param node - Any ESTree expression node, or a missing initialiser.
7
+ * @returns `true` for `() => {}` and `function () {}` values.
8
+ */
9
+ export const isFunctionValue = (node) => {
10
+ const value = unwrapTypeWrappers(node);
11
+ return value?.type === "ArrowFunctionExpression" || value?.type === "FunctionExpression";
12
+ };
@@ -0,0 +1,8 @@
1
+ import type { Node } from "estree";
2
+ /** One module-level function found in a file: the node to report on, and a name for the message. */
3
+ export interface FunctionDefinition {
4
+ /** The AST node the rule reports on when this function is a second (or later) one. */
5
+ readonly node: Node;
6
+ /** The binding name, `"default export"` for an unnamed default export, or `"anonymous"`. */
7
+ readonly name: string;
8
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,11 @@
1
+ import type { Node } from "estree";
2
+ /**
3
+ * Strips TypeScript type wrappers (`as T`, `satisfies T`, `<T>x`, `x!`) from an expression.
4
+ *
5
+ * `const f = (() => {}) as Handler` defines a function exactly as `const f = () => {}` does, so the
6
+ * rule has to see through the wrapper. Plain ESTree nodes pass through unchanged.
7
+ *
8
+ * @param node - Any expression node, or a missing initialiser.
9
+ * @returns The innermost expression, or the input when it is not wrapped.
10
+ */
11
+ export declare const unwrapTypeWrappers: (node: Node | null | undefined) => Node | null | undefined;
@@ -0,0 +1,23 @@
1
+ /** TypeScript-only expression wrappers that do not change which value an expression produces. */
2
+ const TYPE_WRAPPERS = new Set([
3
+ "TSAsExpression",
4
+ "TSSatisfiesExpression",
5
+ "TSTypeAssertion",
6
+ "TSNonNullExpression",
7
+ ]);
8
+ /**
9
+ * Strips TypeScript type wrappers (`as T`, `satisfies T`, `<T>x`, `x!`) from an expression.
10
+ *
11
+ * `const f = (() => {}) as Handler` defines a function exactly as `const f = () => {}` does, so the
12
+ * rule has to see through the wrapper. Plain ESTree nodes pass through unchanged.
13
+ *
14
+ * @param node - Any expression node, or a missing initialiser.
15
+ * @returns The innermost expression, or the input when it is not wrapped.
16
+ */
17
+ export const unwrapTypeWrappers = (node) => {
18
+ let current = node;
19
+ while (current && TYPE_WRAPPERS.has(current.type)) {
20
+ current = current.expression;
21
+ }
22
+ return current;
23
+ };
@@ -0,0 +1,102 @@
1
+ # one-function-per-file
2
+
3
+ Allow at most one module-level function per file.
4
+
5
+ Each function gets its own file, so a file name tells you what is inside, a diff touches one thing,
6
+ and a test file maps to one source file.
7
+
8
+ ## Rule details
9
+
10
+ The rule counts the functions a file defines at module level and reports every one after the first.
11
+
12
+ Examples of **incorrect** code:
13
+
14
+ ```ts
15
+ function a() {}
16
+ function b() {} // 'b' is a second function in this file (first: 'a').
17
+ ```
18
+
19
+ ```ts
20
+ export const double = (n: number) => n * 2;
21
+ export const triple = (n: number) => n * 3;
22
+ ```
23
+
24
+ ```ts
25
+ const a = () => 1,
26
+ b = () => 2; // two declarators count as two functions
27
+ ```
28
+
29
+ ```ts
30
+ const a = () => 1;
31
+ export default () => 2; // a default-exported arrow is a function too
32
+ ```
33
+
34
+ ```ts
35
+ const a = () => 1;
36
+ const b = (() => 2) as () => number; // `as`, `satisfies` and `!` do not hide a function
37
+ ```
38
+
39
+ Examples of **correct** code:
40
+
41
+ ```ts
42
+ export const double = (n: number) => n * 2;
43
+ ```
44
+
45
+ ```ts
46
+ export const total = (items: number[]) => {
47
+ const add = (sum: number, n: number) => sum + n; // nested helper: not counted
48
+ return items.reduce(add, 0);
49
+ };
50
+ ```
51
+
52
+ ```ts
53
+ export const A = 1; // constants, types and classes are not functions
54
+ export type F = () => void;
55
+ export class Box {
56
+ open() {} // methods are not counted
57
+ }
58
+ ```
59
+
60
+ ```ts
61
+ describe("x", () => {
62
+ it("y", () => {}); // test callbacks are not module-level functions
63
+ });
64
+ ```
65
+
66
+ ```ts
67
+ function parse(a: string): number; // overload signatures plus one implementation: one function
68
+ function parse(a: number): number;
69
+ function parse(a: string | number) {
70
+ return Number(a);
71
+ }
72
+ ```
73
+
74
+ ## What is counted
75
+
76
+ | Counted | Not counted |
77
+ | ------------------------------------------------------------- | ----------------------------------------------------- |
78
+ | `function name() {}` (exported or not) | Functions nested inside a function |
79
+ | `const` / `let` / `var` = arrow or function expression | Class methods, object-literal methods |
80
+ | Several declarators in one statement, one count each | Calls that wrap a function, e.g. `memo(() => ...)` |
81
+ | `export default () => {}` and `export default function () {}` | `export { a }`, `export * from`, imports |
82
+ | The same through `as T`, `satisfies T`, `<T>x`, `x!` | Types, interfaces, enums, constants |
83
+ | | `declare function`, overload signatures |
84
+ | | Immediately invoked functions, `module.exports = ...` |
85
+
86
+ The first function in the file is the one that is allowed. Every later one is reported, so three
87
+ functions give two errors, which tells you how many to move.
88
+
89
+ ## Options
90
+
91
+ None.
92
+
93
+ ## When not to use it
94
+
95
+ Leave it off for folders where several small functions naturally belong together, such as a file of
96
+ sort comparators or a generated file. Scope the config with `files` / `ignores`, or turn the rule off
97
+ for that block.
98
+
99
+ ## Related rules
100
+
101
+ - [`max-classes-per-file`](https://eslint.org/docs/latest/rules/max-classes-per-file): the same idea for classes.
102
+ - [`func-style`](https://eslint.org/docs/latest/rules/func-style): forces arrow or declaration style. The `recommended` preset uses it.
package/package.json CHANGED
@@ -1,6 +1,86 @@
1
1
  {
2
2
  "name": "eslint-plugin-one-function-per-file",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "ESLint plugin and preset that enforce one function per file, const arrow functions only, and a TSDoc comment on every named function.",
5
+ "keywords": [
6
+ "eslint",
7
+ "eslintplugin",
8
+ "eslint-plugin",
9
+ "eslint-config",
10
+ "flat-config",
11
+ "one-function-per-file",
12
+ "single-responsibility",
13
+ "arrow-functions",
14
+ "tsdoc",
15
+ "jsdoc",
16
+ "code-style"
17
+ ],
18
+ "homepage": "https://github.com/Vaporjawn/eslint-plugin-one-function-per-file#readme",
19
+ "bugs": {
20
+ "url": "https://github.com/Vaporjawn/eslint-plugin-one-function-per-file/issues"
21
+ },
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/Vaporjawn/eslint-plugin-one-function-per-file.git"
25
+ },
26
+ "license": "MIT",
27
+ "author": "Victor Williams <victor.williams.dev@gmail.com>",
28
+ "type": "module",
29
+ "sideEffects": false,
30
+ "exports": {
31
+ ".": {
32
+ "types": "./dist/index.d.ts",
33
+ "default": "./dist/index.js"
34
+ },
35
+ "./package.json": "./package.json"
36
+ },
37
+ "main": "./dist/index.js",
38
+ "types": "./dist/index.d.ts",
39
+ "files": [
40
+ "dist",
41
+ "docs",
42
+ "CHANGELOG.md"
43
+ ],
44
+ "scripts": {
45
+ "build": "tsc -p tsconfig.build.json",
46
+ "check": "npm run typecheck && npm run lint && npm run format:check && npm test && npm run build && npm run verify:package",
47
+ "clean": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\"",
48
+ "format": "prettier --write .",
49
+ "format:check": "prettier --check .",
50
+ "lint": "npm run build && eslint .",
51
+ "prebuild": "npm run clean",
52
+ "prepare": "npm run build",
53
+ "prepublishOnly": "npm run check",
54
+ "test": "vitest run",
55
+ "test:watch": "vitest",
56
+ "typecheck": "tsc --noEmit",
57
+ "verify:package": "publint --strict && attw --pack . --profile esm-only && node scripts/smoke.mjs"
58
+ },
59
+ "dependencies": {
60
+ "eslint-plugin-jsdoc": "^65.5.4",
61
+ "eslint-plugin-tsdoc": "^0.5.4"
62
+ },
63
+ "devDependencies": {
64
+ "@arethetypeswrong/cli": "^0.18.5",
65
+ "@eslint/js": "^10.0.1",
66
+ "@types/estree": "^1.0.10",
67
+ "@types/node": "^26.6.7",
68
+ "eslint": "^10.15.0",
69
+ "eslint-config-prettier": "^10.1.8",
70
+ "globals": "^17.14.0",
71
+ "prettier": "^3.10.1",
72
+ "publint": "^0.3.25",
73
+ "typescript": "^6.0.3",
74
+ "typescript-eslint": "^8.74.0",
75
+ "vitest": "^5.1.1"
76
+ },
77
+ "peerDependencies": {
78
+ "eslint": "^9.0.0 || ^10.0.0"
79
+ },
80
+ "engines": {
81
+ "node": "^20.19.0 || ^22.13.0 || >=24"
82
+ },
83
+ "publishConfig": {
84
+ "access": "public"
85
+ }
86
+ }