@dloizides/frontend-devtools 1.0.1 → 1.0.3
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/dist/eslint/index.d.mts +77 -0
- package/dist/eslint/index.d.ts +77 -0
- package/dist/eslint/index.js +2000 -0
- package/dist/eslint/index.js.map +1 -0
- package/dist/eslint/index.mjs +1990 -0
- package/dist/eslint/index.mjs.map +1 -0
- package/dist/index.d.mts +69 -0
- package/dist/index.d.ts +69 -0
- package/dist/index.js +37 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +31 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +6 -1
- package/.eslintrc.js +0 -34
- package/jest.config.js +0 -24
- package/publish.ps1 +0 -107
- package/src/eslint/index.ts +0 -16
- package/src/eslint/legacy.ts +0 -44
- package/src/eslint/plugin.test.ts +0 -60
- package/src/eslint/plugin.ts +0 -87
- package/src/eslint/rules.behaviour.test.ts +0 -86
- package/src/eslint/rules.d.ts +0 -16
- package/src/eslint/types.ts +0 -27
- package/src/i18n/initI18n.test.ts +0 -101
- package/src/i18n/initI18n.ts +0 -109
- package/src/index.ts +0 -31
- package/src/rules/enforce-function-style.js +0 -278
- package/src/rules/enforce-module-structure.js +0 -323
- package/src/rules/enforce-route-preload.js +0 -288
- package/src/rules/enforce-test-colocation.js +0 -82
- package/src/rules/enum-file-isolation.js +0 -149
- package/src/rules/i18n-interpolation.js +0 -185
- package/src/rules/i18n-param-names.js +0 -119
- package/src/rules/no-barrel-companion-file.js +0 -142
- package/src/rules/no-duplicate-nav-prefix.js +0 -107
- package/src/rules/no-duplicate-shared-patterns.js +0 -166
- package/src/rules/no-generated-models-barrel-value-import.js +0 -70
- package/src/rules/no-null-check.js +0 -171
- package/src/rules/no-optional-undefined.js +0 -99
- package/src/rules/no-product-imports-in-shared.js +0 -164
- package/src/rules/prefer-const-enum.js +0 -127
- package/src/rules/require-stable-hook-args.js +0 -372
- package/src/rules/smart-max-lines.js +0 -344
- package/tsconfig.json +0 -25
- package/tsconfig.test.json +0 -11
- package/tsup.config.ts +0 -14
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import type { InitOptions } from 'i18next';
|
|
2
|
-
|
|
3
|
-
// Capture the fluent chain. The factory calls i18n.use(initReactI18next).init(opts).catch().
|
|
4
|
-
const useMock = jest.fn();
|
|
5
|
-
const initMock = jest.fn();
|
|
6
|
-
const catchMock = jest.fn();
|
|
7
|
-
|
|
8
|
-
jest.mock('react-i18next', () => ({
|
|
9
|
-
initReactI18next: { type: '3rdParty', init: jest.fn() },
|
|
10
|
-
}));
|
|
11
|
-
|
|
12
|
-
jest.mock('i18next', () => {
|
|
13
|
-
const singleton = {
|
|
14
|
-
use: (...args: unknown[]): unknown => {
|
|
15
|
-
useMock(...args);
|
|
16
|
-
return singleton;
|
|
17
|
-
},
|
|
18
|
-
init: (opts: InitOptions): { catch: (fn: () => void) => void } => {
|
|
19
|
-
initMock(opts);
|
|
20
|
-
return { catch: catchMock };
|
|
21
|
-
},
|
|
22
|
-
};
|
|
23
|
-
return { __esModule: true, default: singleton };
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
import { initI18n } from './initI18n';
|
|
27
|
-
import { initReactI18next } from 'react-i18next';
|
|
28
|
-
|
|
29
|
-
describe('initI18n', () => {
|
|
30
|
-
beforeEach(() => {
|
|
31
|
-
useMock.mockClear();
|
|
32
|
-
initMock.mockClear();
|
|
33
|
-
catchMock.mockClear();
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('wires react-i18next into the singleton', () => {
|
|
37
|
-
initI18n({ resources: { en: { translation: {} } } });
|
|
38
|
-
expect(useMock).toHaveBeenCalledWith(initReactI18next);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('passes resources straight through', () => {
|
|
42
|
-
const resources = { en: { translation: { hello: 'Hi' } } };
|
|
43
|
-
initI18n({ resources });
|
|
44
|
-
expect(initMock.mock.calls[0][0].resources).toBe(resources);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('defaults lng to the fallback language when lng omitted', () => {
|
|
48
|
-
initI18n({ resources: {}, fallbackLng: 'el' });
|
|
49
|
-
const opts = initMock.mock.calls[0][0];
|
|
50
|
-
expect(opts.lng).toBe('el');
|
|
51
|
-
expect(opts.fallbackLng).toBe('el');
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it('defaults fallbackLng to "en"', () => {
|
|
55
|
-
initI18n({ resources: {} });
|
|
56
|
-
expect(initMock.mock.calls[0][0].fallbackLng).toBe('en');
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it('uses the supplied lng over the fallback', () => {
|
|
60
|
-
initI18n({ resources: {}, lng: 'fr', fallbackLng: 'en' });
|
|
61
|
-
expect(initMock.mock.calls[0][0].lng).toBe('fr');
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it('defaults escapeValue to false (React already escapes)', () => {
|
|
65
|
-
initI18n({ resources: {} });
|
|
66
|
-
expect(initMock.mock.calls[0][0].interpolation).toEqual({ escapeValue: false });
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it('honours an explicit escapeValue', () => {
|
|
70
|
-
initI18n({ resources: {}, escapeValue: true });
|
|
71
|
-
expect(initMock.mock.calls[0][0].interpolation).toEqual({ escapeValue: true });
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it('omits defaultNS / ns when not provided', () => {
|
|
75
|
-
initI18n({ resources: {} });
|
|
76
|
-
const opts = initMock.mock.calls[0][0];
|
|
77
|
-
expect('defaultNS' in opts).toBe(false);
|
|
78
|
-
expect('ns' in opts).toBe(false);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it('forwards defaultNS and ns when provided', () => {
|
|
82
|
-
initI18n({ resources: {}, defaultNS: 'common', ns: ['common', 'forms'] });
|
|
83
|
-
const opts = initMock.mock.calls[0][0];
|
|
84
|
-
expect(opts.defaultNS).toBe('common');
|
|
85
|
-
expect(opts.ns).toEqual(['common', 'forms']);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it('swallows the init rejection via .catch()', () => {
|
|
89
|
-
initI18n({ resources: {} });
|
|
90
|
-
expect(catchMock).toHaveBeenCalledTimes(1);
|
|
91
|
-
// The handler is a no-op; calling it must not throw.
|
|
92
|
-
const handler = catchMock.mock.calls[0][0] as () => void;
|
|
93
|
-
expect(() => handler()).not.toThrow();
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
it('returns the i18next singleton', () => {
|
|
97
|
-
const result = initI18n({ resources: {} });
|
|
98
|
-
expect(result).toBeDefined();
|
|
99
|
-
expect(typeof result.init).toBe('function');
|
|
100
|
-
});
|
|
101
|
-
});
|
package/src/i18n/initI18n.ts
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
// i18next's default export is the configurable singleton; `.use()` / `.init()`
|
|
2
|
-
// are members on it — this is the documented i18next initialization pattern,
|
|
3
|
-
// which trips `import/no-named-as-default-member` for the whole fluent chain.
|
|
4
|
-
// The disable is owned by the package so the consuming apps don't repeat it.
|
|
5
|
-
/* eslint-disable import/no-named-as-default-member */
|
|
6
|
-
import i18n, { type i18n as I18nInstance, type Resource } from 'i18next';
|
|
7
|
-
import { initReactI18next } from 'react-i18next';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Options for {@link initI18n}.
|
|
11
|
-
*
|
|
12
|
-
* Everything app-specific (the strings, the locales, the fallback language and
|
|
13
|
-
* the active language) is passed in — the package owns only the init *chain*.
|
|
14
|
-
*/
|
|
15
|
-
export interface InitI18nOptions {
|
|
16
|
-
/**
|
|
17
|
-
* The i18next resource bundle, e.g. `{ en: { translation: en } }`.
|
|
18
|
-
* The app owns its own `locales/*.json` — only the init logic is shared.
|
|
19
|
-
*/
|
|
20
|
-
resources: Resource;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* The active language. Apps typically derive this from device locale,
|
|
24
|
-
* e.g. `expo-localization`'s `getLocales()[0]?.languageCode`.
|
|
25
|
-
*
|
|
26
|
-
* When omitted (or undefined), defaults to {@link fallbackLng}.
|
|
27
|
-
*/
|
|
28
|
-
lng?: string;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* The fallback language used when a key is missing in {@link lng}.
|
|
32
|
-
* Defaults to `'en'`.
|
|
33
|
-
*/
|
|
34
|
-
fallbackLng?: string;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Whether to escape interpolated values. React already escapes output, so
|
|
38
|
-
* the SaaS apps run with `escapeValue: false`. Defaults to `false`.
|
|
39
|
-
*/
|
|
40
|
-
escapeValue?: boolean;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Default namespace(s). i18next's default is `'translation'`; the SaaS apps
|
|
44
|
-
* keep that single-namespace convention, so this is optional.
|
|
45
|
-
*/
|
|
46
|
-
defaultNS?: string | readonly string[];
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Namespace list. Optional — only needed when the app splits strings across
|
|
50
|
-
* multiple namespaces. Single-namespace apps can omit it.
|
|
51
|
-
*/
|
|
52
|
-
ns?: string | readonly string[];
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const DEFAULT_FALLBACK_LNG = 'en';
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Initialize the shared i18next + react-i18next singleton.
|
|
59
|
-
*
|
|
60
|
-
* This is the parameterized factory that replaces the per-app `i18n.ts`. It
|
|
61
|
-
* wires `react-i18next` into the i18next singleton and runs `.init()` with the
|
|
62
|
-
* app-supplied resources/language, swallowing the async init rejection the same
|
|
63
|
-
* way every app did (i18next resolves synchronously for in-memory resources).
|
|
64
|
-
*
|
|
65
|
-
* The returned value is the i18next singleton — apps `export default` it and
|
|
66
|
-
* call `.t()` through their own `FM()` helper.
|
|
67
|
-
*
|
|
68
|
-
* @example
|
|
69
|
-
* ```ts
|
|
70
|
-
* import { initI18n } from '@dloizides/frontend-devtools';
|
|
71
|
-
* import * as Localization from 'expo-localization';
|
|
72
|
-
* import en from './locales/en.json';
|
|
73
|
-
*
|
|
74
|
-
* const i18n = initI18n({
|
|
75
|
-
* resources: { en: { translation: en } },
|
|
76
|
-
* lng: Localization.getLocales()[0]?.languageCode,
|
|
77
|
-
* });
|
|
78
|
-
* export default i18n;
|
|
79
|
-
* ```
|
|
80
|
-
*/
|
|
81
|
-
export function initI18n(options: InitI18nOptions): I18nInstance {
|
|
82
|
-
const {
|
|
83
|
-
resources,
|
|
84
|
-
lng,
|
|
85
|
-
fallbackLng = DEFAULT_FALLBACK_LNG,
|
|
86
|
-
escapeValue = false,
|
|
87
|
-
defaultNS,
|
|
88
|
-
ns,
|
|
89
|
-
} = options;
|
|
90
|
-
|
|
91
|
-
i18n
|
|
92
|
-
.use(initReactI18next)
|
|
93
|
-
.init({
|
|
94
|
-
lng: lng ?? fallbackLng,
|
|
95
|
-
fallbackLng,
|
|
96
|
-
resources,
|
|
97
|
-
...(defaultNS !== undefined ? { defaultNS } : {}),
|
|
98
|
-
...(ns !== undefined ? { ns } : {}),
|
|
99
|
-
interpolation: {
|
|
100
|
-
escapeValue,
|
|
101
|
-
},
|
|
102
|
-
})
|
|
103
|
-
.catch(() => {
|
|
104
|
-
// i18next resolves synchronously for in-memory resources; the apps have
|
|
105
|
-
// always swallowed this rejection. Kept identical to avoid behaviour drift.
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
return i18n;
|
|
109
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `@dloizides/frontend-devtools` — shared RN-web dev tooling for the
|
|
3
|
-
* dloizides.com SaaS frontends.
|
|
4
|
-
*
|
|
5
|
-
* Two product-agnostic, byte-for-byte-duplicated concerns extracted from
|
|
6
|
-
* erevna-web / katalogos-web / kefi-web:
|
|
7
|
-
*
|
|
8
|
-
* 1. The custom ESLint rule pack (17 rules) — also available under the
|
|
9
|
-
* `@dloizides/frontend-devtools/eslint` subpath. The app decides which
|
|
10
|
-
* rules to enable + their options; the package owns the rule logic.
|
|
11
|
-
*
|
|
12
|
-
* 2. `initI18n(options)` — the parameterized i18next + react-i18next init
|
|
13
|
-
* factory. The app owns its `locales/*.json` strings and the active
|
|
14
|
-
* language; the package owns the `.use().init()` chain (incl. the
|
|
15
|
-
* `import/no-named-as-default-member` disable that every app repeated).
|
|
16
|
-
*
|
|
17
|
-
* Never imports a product.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
// i18n init factory
|
|
21
|
-
export { initI18n } from './i18n/initI18n';
|
|
22
|
-
export type { InitI18nOptions } from './i18n/initI18n';
|
|
23
|
-
|
|
24
|
-
// ESLint custom rule pack (also at the `/eslint` subpath)
|
|
25
|
-
export { plugin, rules, ruleNames, legacyPlugins } from './eslint';
|
|
26
|
-
export type {
|
|
27
|
-
CustomRule,
|
|
28
|
-
FlatConfigPlugin,
|
|
29
|
-
FlatConfig,
|
|
30
|
-
SingleRulePlugin,
|
|
31
|
-
} from './eslint';
|
|
@@ -1,278 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Custom ESLint Plugin: Enforce Function Style & Ordering
|
|
3
|
-
*
|
|
4
|
-
* Two independent checks controlled by options:
|
|
5
|
-
*
|
|
6
|
-
* 1. **checkStyle** — Multi-statement functions must use `function` declarations,
|
|
7
|
-
* not `const`-assigned arrow/function expressions. Single-expression arrows
|
|
8
|
-
* are fine (concise one-liners). React components are exempt (enforced by
|
|
9
|
-
* `react/function-component-definition`). Closures/callbacks are exempt.
|
|
10
|
-
*
|
|
11
|
-
* 2. **checkOrdering** — Module-level constants must appear before functions and
|
|
12
|
-
* exports. Functions and exports may be freely interleaved.
|
|
13
|
-
* Imports, type declarations, and re-exports are exempt.
|
|
14
|
-
* This order is compatible with @typescript-eslint/no-use-before-define.
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
// ---------------------------------------------------------------------------
|
|
18
|
-
// Helpers – JSX detection (borrowed from smart-max-lines for consistency)
|
|
19
|
-
// ---------------------------------------------------------------------------
|
|
20
|
-
|
|
21
|
-
function isJSX(node) {
|
|
22
|
-
return node && (node.type === 'JSXElement' || node.type === 'JSXFragment');
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function expressionReturnsJSX(node) {
|
|
26
|
-
if (!node) return false;
|
|
27
|
-
if (isJSX(node)) return true;
|
|
28
|
-
if (node.type === 'ConditionalExpression')
|
|
29
|
-
return expressionReturnsJSX(node.consequent) || expressionReturnsJSX(node.alternate);
|
|
30
|
-
if (node.type === 'LogicalExpression')
|
|
31
|
-
return expressionReturnsJSX(node.left) || expressionReturnsJSX(node.right);
|
|
32
|
-
if (node.type === 'ParenthesizedExpression')
|
|
33
|
-
return expressionReturnsJSX(node.expression);
|
|
34
|
-
return false;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function blockReturnsJSX(body) {
|
|
38
|
-
if (!Array.isArray(body)) return false;
|
|
39
|
-
for (const stmt of body) {
|
|
40
|
-
if (stmt.type === 'ReturnStatement' && expressionReturnsJSX(stmt.argument))
|
|
41
|
-
return true;
|
|
42
|
-
if (stmt.type === 'IfStatement') {
|
|
43
|
-
const check = (branch) => {
|
|
44
|
-
if (!branch) return false;
|
|
45
|
-
if (branch.type === 'BlockStatement') return blockReturnsJSX(branch.body);
|
|
46
|
-
return branch.type === 'ReturnStatement' && expressionReturnsJSX(branch.argument);
|
|
47
|
-
};
|
|
48
|
-
if (check(stmt.consequent) || check(stmt.alternate)) return true;
|
|
49
|
-
}
|
|
50
|
-
if (stmt.type === 'SwitchStatement') {
|
|
51
|
-
for (const c of stmt.cases)
|
|
52
|
-
if (blockReturnsJSX(c.consequent)) return true;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return false;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function functionReturnsJSX(fnNode) {
|
|
59
|
-
if (fnNode.type === 'ArrowFunctionExpression' && fnNode.body.type !== 'BlockStatement')
|
|
60
|
-
return expressionReturnsJSX(fnNode.body);
|
|
61
|
-
if (fnNode.body && fnNode.body.type === 'BlockStatement')
|
|
62
|
-
return blockReturnsJSX(fnNode.body.body);
|
|
63
|
-
return false;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// ---------------------------------------------------------------------------
|
|
67
|
-
// Helpers – Statement classification for ordering
|
|
68
|
-
// ---------------------------------------------------------------------------
|
|
69
|
-
|
|
70
|
-
/** Exempt statement types that don't participate in ordering zones. */
|
|
71
|
-
function isExemptStatement(stmt) {
|
|
72
|
-
if (stmt.type === 'ImportDeclaration') return true;
|
|
73
|
-
if (stmt.type === 'TSTypeAliasDeclaration') return true;
|
|
74
|
-
if (stmt.type === 'TSInterfaceDeclaration') return true;
|
|
75
|
-
if (stmt.type === 'TSEnumDeclaration') return true;
|
|
76
|
-
if (stmt.type === 'ExportAllDeclaration') return true;
|
|
77
|
-
// Type-only exports
|
|
78
|
-
if (stmt.type === 'ExportNamedDeclaration' && stmt.exportKind === 'type') return true;
|
|
79
|
-
// Re-exports (export { ... } from '...')
|
|
80
|
-
if (stmt.type === 'ExportNamedDeclaration' && stmt.source) return true;
|
|
81
|
-
// Empty export declarations (export {};)
|
|
82
|
-
if (stmt.type === 'ExportNamedDeclaration' && !stmt.declaration && stmt.specifiers.length === 0)
|
|
83
|
-
return true;
|
|
84
|
-
return false;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/** Check if a VariableDeclaration's init is a function (arrow or function expression). */
|
|
88
|
-
function hasFunctionInit(varDecl) {
|
|
89
|
-
if (!varDecl.declarations || varDecl.declarations.length === 0) return false;
|
|
90
|
-
const init = varDecl.declarations[0].init;
|
|
91
|
-
if (!init) return false;
|
|
92
|
-
return init.type === 'ArrowFunctionExpression' || init.type === 'FunctionExpression';
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Classify a top-level statement into a zone for ordering:
|
|
97
|
-
* 1 = constant, 2 = function/export, 0 = exempt
|
|
98
|
-
*
|
|
99
|
-
* Expected file layout (compatible with no-use-before-define):
|
|
100
|
-
* imports/types → constants → functions & exports (freely interleaved)
|
|
101
|
-
*/
|
|
102
|
-
function classifyZone(stmt) {
|
|
103
|
-
if (isExemptStatement(stmt)) return 0;
|
|
104
|
-
|
|
105
|
-
// Exports → zone 2
|
|
106
|
-
if (stmt.type === 'ExportNamedDeclaration' || stmt.type === 'ExportDefaultDeclaration')
|
|
107
|
-
return 2;
|
|
108
|
-
|
|
109
|
-
// Private function declarations → zone 2
|
|
110
|
-
if (stmt.type === 'FunctionDeclaration') return 2;
|
|
111
|
-
|
|
112
|
-
// Private variable declarations
|
|
113
|
-
if (stmt.type === 'VariableDeclaration') {
|
|
114
|
-
if (hasFunctionInit(stmt)) return 2; // function-valued → zone 2
|
|
115
|
-
return 1; // plain constant → zone 1
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// Everything else (expression statements, etc.) → exempt
|
|
119
|
-
return 0;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/** Get a readable name from a statement for error messages. */
|
|
123
|
-
function getStatementName(stmt) {
|
|
124
|
-
if (stmt.type === 'ExportNamedDeclaration' && stmt.declaration) {
|
|
125
|
-
const decl = stmt.declaration;
|
|
126
|
-
if (decl.type === 'FunctionDeclaration' && decl.id) return decl.id.name;
|
|
127
|
-
if (decl.type === 'VariableDeclaration' && decl.declarations[0]?.id)
|
|
128
|
-
return decl.declarations[0].id.name;
|
|
129
|
-
}
|
|
130
|
-
if (stmt.type === 'ExportDefaultDeclaration') return 'default export';
|
|
131
|
-
if (stmt.type === 'FunctionDeclaration' && stmt.id) return stmt.id.name;
|
|
132
|
-
if (stmt.type === 'VariableDeclaration' && stmt.declarations[0]?.id)
|
|
133
|
-
return stmt.declarations[0].id.name;
|
|
134
|
-
return '(anonymous)';
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// ---------------------------------------------------------------------------
|
|
138
|
-
// Helpers – Top-level detection
|
|
139
|
-
// ---------------------------------------------------------------------------
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Check if a VariableDeclarator is at the module top level.
|
|
143
|
-
* A declarator is top-level if its VariableDeclaration parent is directly
|
|
144
|
-
* under Program, or under an ExportNamedDeclaration that is under Program.
|
|
145
|
-
*/
|
|
146
|
-
function isTopLevel(node) {
|
|
147
|
-
const varDecl = node.parent; // VariableDeclaration
|
|
148
|
-
if (!varDecl) return false;
|
|
149
|
-
const container = varDecl.parent;
|
|
150
|
-
if (!container) return false;
|
|
151
|
-
if (container.type === 'Program') return true;
|
|
152
|
-
if (container.type === 'ExportNamedDeclaration' && container.parent?.type === 'Program')
|
|
153
|
-
return true;
|
|
154
|
-
return false;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// ---------------------------------------------------------------------------
|
|
158
|
-
// Rule definition
|
|
159
|
-
// ---------------------------------------------------------------------------
|
|
160
|
-
|
|
161
|
-
const enforceFunctionStyleRule = {
|
|
162
|
-
meta: {
|
|
163
|
-
type: 'suggestion',
|
|
164
|
-
docs: {
|
|
165
|
-
description:
|
|
166
|
-
'Enforce function declarations for multi-statement functions and ' +
|
|
167
|
-
'correct ordering of exports, private functions, and constants.',
|
|
168
|
-
category: 'Best Practices',
|
|
169
|
-
recommended: true,
|
|
170
|
-
},
|
|
171
|
-
schema: [
|
|
172
|
-
{
|
|
173
|
-
type: 'object',
|
|
174
|
-
properties: {
|
|
175
|
-
checkStyle: {
|
|
176
|
-
type: 'boolean',
|
|
177
|
-
description: 'Flag const-assigned multi-statement functions (default true)',
|
|
178
|
-
},
|
|
179
|
-
checkOrdering: {
|
|
180
|
-
type: 'boolean',
|
|
181
|
-
description: 'Flag out-of-order module statements (default true)',
|
|
182
|
-
},
|
|
183
|
-
},
|
|
184
|
-
additionalProperties: false,
|
|
185
|
-
},
|
|
186
|
-
],
|
|
187
|
-
messages: {
|
|
188
|
-
useFunction:
|
|
189
|
-
'Use a function declaration (`function {{name}}`) instead of a ' +
|
|
190
|
-
'const-assigned arrow/function expression for multi-statement functions.',
|
|
191
|
-
constantAfterCode:
|
|
192
|
-
'Module-level constant `{{name}}` should appear before functions and exports.',
|
|
193
|
-
},
|
|
194
|
-
},
|
|
195
|
-
|
|
196
|
-
create(context) {
|
|
197
|
-
const options = context.options[0] || {};
|
|
198
|
-
const checkStyle = options.checkStyle !== false;
|
|
199
|
-
const checkOrdering = options.checkOrdering !== false;
|
|
200
|
-
|
|
201
|
-
return {
|
|
202
|
-
// -----------------------------------------------------------------
|
|
203
|
-
// Check 1: Declaration style
|
|
204
|
-
// -----------------------------------------------------------------
|
|
205
|
-
...(checkStyle
|
|
206
|
-
? {
|
|
207
|
-
VariableDeclarator(node) {
|
|
208
|
-
// Only care about top-level const-assigned functions
|
|
209
|
-
if (!isTopLevel(node)) return;
|
|
210
|
-
|
|
211
|
-
const init = node.init;
|
|
212
|
-
if (!init) return;
|
|
213
|
-
if (init.type !== 'ArrowFunctionExpression' && init.type !== 'FunctionExpression')
|
|
214
|
-
return;
|
|
215
|
-
|
|
216
|
-
// Allow single-expression arrows (concise body, no block)
|
|
217
|
-
if (init.type === 'ArrowFunctionExpression' && init.body.type !== 'BlockStatement')
|
|
218
|
-
return;
|
|
219
|
-
|
|
220
|
-
// Allow React components (JSX return) — enforced separately
|
|
221
|
-
if (functionReturnsJSX(init)) return;
|
|
222
|
-
|
|
223
|
-
// Allow PascalCase names — likely React components (enforced
|
|
224
|
-
// as arrow functions by react/function-component-definition).
|
|
225
|
-
// Components returning null won't be caught by JSX check above.
|
|
226
|
-
const name = node.id?.name ?? '(anonymous)';
|
|
227
|
-
if (/^[A-Z]/.test(name)) return;
|
|
228
|
-
|
|
229
|
-
// IIFEs like `const x = (() => {...})()` are implicitly exempt
|
|
230
|
-
// because their init is a CallExpression, filtered by the type
|
|
231
|
-
// check above.
|
|
232
|
-
|
|
233
|
-
context.report({
|
|
234
|
-
node,
|
|
235
|
-
messageId: 'useFunction',
|
|
236
|
-
data: { name },
|
|
237
|
-
});
|
|
238
|
-
},
|
|
239
|
-
}
|
|
240
|
-
: {}),
|
|
241
|
-
|
|
242
|
-
// -----------------------------------------------------------------
|
|
243
|
-
// Check 2: Ordering
|
|
244
|
-
// -----------------------------------------------------------------
|
|
245
|
-
...(checkOrdering
|
|
246
|
-
? {
|
|
247
|
-
'Program:exit'(programNode) {
|
|
248
|
-
let maxZoneSeen = 0;
|
|
249
|
-
|
|
250
|
-
for (const stmt of programNode.body) {
|
|
251
|
-
const zone = classifyZone(stmt);
|
|
252
|
-
if (zone === 0) continue; // exempt — ignore
|
|
253
|
-
|
|
254
|
-
if (zone < maxZoneSeen) {
|
|
255
|
-
const name = getStatementName(stmt);
|
|
256
|
-
const messageId = 'constantAfterCode';
|
|
257
|
-
|
|
258
|
-
context.report({
|
|
259
|
-
node: stmt,
|
|
260
|
-
messageId,
|
|
261
|
-
data: { name },
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
if (zone > maxZoneSeen) maxZoneSeen = zone;
|
|
266
|
-
}
|
|
267
|
-
},
|
|
268
|
-
}
|
|
269
|
-
: {}),
|
|
270
|
-
};
|
|
271
|
-
},
|
|
272
|
-
};
|
|
273
|
-
|
|
274
|
-
export default {
|
|
275
|
-
rules: {
|
|
276
|
-
'enforce-function-style': enforceFunctionStyleRule,
|
|
277
|
-
},
|
|
278
|
-
};
|