@dloizides/frontend-devtools 1.0.1
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/.eslintrc.js +34 -0
- package/CHANGELOG.md +26 -0
- package/LICENSE +21 -0
- package/README.md +131 -0
- package/jest.config.js +24 -0
- package/package.json +104 -0
- package/publish.ps1 +107 -0
- package/src/eslint/index.ts +16 -0
- package/src/eslint/legacy.ts +44 -0
- package/src/eslint/plugin.test.ts +60 -0
- package/src/eslint/plugin.ts +87 -0
- package/src/eslint/rules.behaviour.test.ts +86 -0
- package/src/eslint/rules.d.ts +16 -0
- package/src/eslint/types.ts +27 -0
- package/src/i18n/initI18n.test.ts +101 -0
- package/src/i18n/initI18n.ts +109 -0
- package/src/index.ts +31 -0
- package/src/rules/enforce-function-style.js +278 -0
- package/src/rules/enforce-module-structure.js +323 -0
- package/src/rules/enforce-route-preload.js +288 -0
- package/src/rules/enforce-test-colocation.js +82 -0
- package/src/rules/enum-file-isolation.js +149 -0
- package/src/rules/i18n-interpolation.js +185 -0
- package/src/rules/i18n-param-names.js +119 -0
- package/src/rules/no-barrel-companion-file.js +142 -0
- package/src/rules/no-duplicate-nav-prefix.js +107 -0
- package/src/rules/no-duplicate-shared-patterns.js +166 -0
- package/src/rules/no-generated-models-barrel-value-import.js +70 -0
- package/src/rules/no-null-check.js +171 -0
- package/src/rules/no-optional-undefined.js +99 -0
- package/src/rules/no-product-imports-in-shared.js +164 -0
- package/src/rules/prefer-const-enum.js +127 -0
- package/src/rules/require-stable-hook-args.js +372 -0
- package/src/rules/smart-max-lines.js +344 -0
- package/tsconfig.json +25 -0
- package/tsconfig.test.json +11 -0
- package/tsup.config.ts +14 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom ESLint Plugin: No Duplicate Shared Patterns
|
|
3
|
+
*
|
|
4
|
+
* Warns when product module directories create components with names
|
|
5
|
+
* that match existing shared components in src/components/. This
|
|
6
|
+
* helps prevent accidental duplication of shared UI patterns.
|
|
7
|
+
*
|
|
8
|
+
* Scanned directories (product-specific code):
|
|
9
|
+
* - packages/* /src/ (product module packages)
|
|
10
|
+
* - src/features/ (feature directories)
|
|
11
|
+
*
|
|
12
|
+
* Reference directory (shared components):
|
|
13
|
+
* - src/components/ (recursively scanned for .tsx basenames)
|
|
14
|
+
*
|
|
15
|
+
* The rule collects all .tsx file basenames from src/components/ at
|
|
16
|
+
* startup and warns when a .tsx file in a product directory has the
|
|
17
|
+
* same basename.
|
|
18
|
+
*
|
|
19
|
+
* Examples:
|
|
20
|
+
* WARN: packages/onlinemenu-module/src/components/EmptyState.tsx
|
|
21
|
+
* (when src/components/Shared/EmptyState.tsx exists)
|
|
22
|
+
*
|
|
23
|
+
* OK: packages/onlinemenu-module/src/components/MenuCard.tsx
|
|
24
|
+
* (no matching shared component)
|
|
25
|
+
*
|
|
26
|
+
* OK: packages/onlinemenu-module/src/index.ts
|
|
27
|
+
* (not a .tsx component file)
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import { readdirSync, existsSync } from 'fs';
|
|
31
|
+
import { posix, sep, basename, join } from 'path';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Normalize a file path to use forward slashes.
|
|
35
|
+
*/
|
|
36
|
+
function normalizePath(filePath) {
|
|
37
|
+
return filePath.split(sep).join(posix.sep);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Recursively collect all .tsx basenames from a directory.
|
|
42
|
+
* Skips test files and index files.
|
|
43
|
+
*/
|
|
44
|
+
function collectComponentNames(dirPath) {
|
|
45
|
+
const names = new Set();
|
|
46
|
+
if (!existsSync(dirPath)) return names;
|
|
47
|
+
|
|
48
|
+
function walk(dir) {
|
|
49
|
+
try {
|
|
50
|
+
const entries = readdirSync(dir, { withFileTypes: true });
|
|
51
|
+
for (const entry of entries) {
|
|
52
|
+
if (entry.isDirectory()) {
|
|
53
|
+
walk(join(dir, entry.name));
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
const name = entry.name;
|
|
57
|
+
if (!name.endsWith('.tsx')) continue;
|
|
58
|
+
if (name.endsWith('.test.tsx')) continue;
|
|
59
|
+
if (name === 'index.tsx') continue;
|
|
60
|
+
names.add(name);
|
|
61
|
+
}
|
|
62
|
+
} catch {
|
|
63
|
+
// Directory not readable, skip
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
walk(dirPath);
|
|
68
|
+
return names;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Check whether a file is inside a product-specific directory.
|
|
73
|
+
*/
|
|
74
|
+
function isInsideProductDirectory(filePath) {
|
|
75
|
+
const normalized = normalizePath(filePath);
|
|
76
|
+
return (
|
|
77
|
+
normalized.includes('/packages/') &&
|
|
78
|
+
normalized.includes('-module/src/')
|
|
79
|
+
) || normalized.includes('/src/features/');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Find the BaseClient src/components/ directory from a file path.
|
|
84
|
+
* Walks up looking for BaseClient/src/components/.
|
|
85
|
+
*/
|
|
86
|
+
function findSharedComponentsDir(filePath) {
|
|
87
|
+
const normalized = normalizePath(filePath);
|
|
88
|
+
const baseClientIdx = normalized.indexOf('/BaseClient/');
|
|
89
|
+
if (baseClientIdx === -1) return null;
|
|
90
|
+
const baseClientRoot = filePath
|
|
91
|
+
.split(sep)
|
|
92
|
+
.join(posix.sep)
|
|
93
|
+
.slice(0, baseClientIdx + '/BaseClient'.length);
|
|
94
|
+
// Convert back to OS path
|
|
95
|
+
const osPath = baseClientRoot.split(posix.sep).join(sep);
|
|
96
|
+
return join(osPath, 'src', 'components');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Cache shared component names per components dir. */
|
|
100
|
+
const sharedNamesCache = new Map();
|
|
101
|
+
|
|
102
|
+
const noDuplicateSharedPatternsRule = {
|
|
103
|
+
meta: {
|
|
104
|
+
type: 'suggestion',
|
|
105
|
+
docs: {
|
|
106
|
+
description:
|
|
107
|
+
'Warn when product modules create components that share names ' +
|
|
108
|
+
'with existing shared components in src/components/.',
|
|
109
|
+
category: 'Best Practices',
|
|
110
|
+
recommended: false,
|
|
111
|
+
},
|
|
112
|
+
schema: [],
|
|
113
|
+
messages: {
|
|
114
|
+
duplicateSharedPattern:
|
|
115
|
+
'"{{fileName}}" already exists as a shared component in ' +
|
|
116
|
+
'src/components/. Consider reusing the shared version or ' +
|
|
117
|
+
'renaming this component to clarify its purpose.',
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
create(context) {
|
|
122
|
+
const filePath = context.filename || context.getFilename();
|
|
123
|
+
if (!filePath) return {};
|
|
124
|
+
|
|
125
|
+
// Only check .tsx files (components)
|
|
126
|
+
if (!filePath.endsWith('.tsx')) return {};
|
|
127
|
+
|
|
128
|
+
const fileName = basename(filePath);
|
|
129
|
+
|
|
130
|
+
// Skip test files and index files
|
|
131
|
+
if (fileName.endsWith('.test.tsx')) return {};
|
|
132
|
+
if (fileName === 'index.tsx') return {};
|
|
133
|
+
|
|
134
|
+
// Only check files in product-specific directories
|
|
135
|
+
if (!isInsideProductDirectory(filePath)) return {};
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
Program() {
|
|
139
|
+
const componentsDir = findSharedComponentsDir(filePath);
|
|
140
|
+
if (!componentsDir) return;
|
|
141
|
+
|
|
142
|
+
// Cache the shared component names
|
|
143
|
+
if (!sharedNamesCache.has(componentsDir))
|
|
144
|
+
sharedNamesCache.set(
|
|
145
|
+
componentsDir,
|
|
146
|
+
collectComponentNames(componentsDir),
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
const sharedNames = sharedNamesCache.get(componentsDir);
|
|
150
|
+
|
|
151
|
+
if (sharedNames.has(fileName))
|
|
152
|
+
context.report({
|
|
153
|
+
loc: { line: 1, column: 0 },
|
|
154
|
+
messageId: 'duplicateSharedPattern',
|
|
155
|
+
data: { fileName },
|
|
156
|
+
});
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
export default {
|
|
163
|
+
rules: {
|
|
164
|
+
'no-duplicate-shared-patterns': noDuplicateSharedPatternsRule,
|
|
165
|
+
},
|
|
166
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom ESLint Plugin: No Generated Models Barrel Value Import
|
|
3
|
+
*
|
|
4
|
+
* Disallows runtime (value) imports from the auto-generated model barrels:
|
|
5
|
+
* server/autoGeneratedHooks/<service>/models
|
|
6
|
+
* server/autoGeneratedHooks/<service>/models/index
|
|
7
|
+
*
|
|
8
|
+
* These barrels re-export all generated types for a service. Importing runtime
|
|
9
|
+
* values from them pulls the entire barrel into the module graph, preventing
|
|
10
|
+
* tree-shaking and inflating bundle size.
|
|
11
|
+
*
|
|
12
|
+
* Type-only imports are always allowed:
|
|
13
|
+
* import type { Foo } from '../../server/autoGeneratedHooks/questioner/models'; // OK
|
|
14
|
+
* import { type Foo } from '../../server/autoGeneratedHooks/questioner/models'; // OK
|
|
15
|
+
*
|
|
16
|
+
* Runtime value imports must target the specific leaf module instead:
|
|
17
|
+
* import { QuestionType } from '../../server/autoGeneratedHooks/questioner/models/questionType'; // OK
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** Matches any path ending in autoGeneratedHooks/<service>/models or …/models/index */
|
|
21
|
+
const BARREL_PATTERN = /autoGeneratedHooks\/[^/]+\/models(?:\/index)?$/;
|
|
22
|
+
|
|
23
|
+
function isTypeOnlyImport(node) {
|
|
24
|
+
if (node.importKind === 'type') return true;
|
|
25
|
+
|
|
26
|
+
const specifiers = node.specifiers ?? [];
|
|
27
|
+
if (specifiers.length === 0) return false;
|
|
28
|
+
|
|
29
|
+
return specifiers.every((s) => s.importKind === 'type');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const noGeneratedModelsBarrelValueImportRule = {
|
|
33
|
+
meta: {
|
|
34
|
+
type: 'problem',
|
|
35
|
+
docs: {
|
|
36
|
+
description:
|
|
37
|
+
'Disallow runtime (value) imports from generated model barrels; use specific leaf modules or type-only imports.',
|
|
38
|
+
category: 'Best Practices',
|
|
39
|
+
recommended: true,
|
|
40
|
+
},
|
|
41
|
+
schema: [],
|
|
42
|
+
messages: {
|
|
43
|
+
noValueImport:
|
|
44
|
+
'Do not import runtime values from "{{module}}". Import from a specific leaf model module instead (e.g. …/models/questionType). Type-only imports are allowed.',
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
create(context) {
|
|
49
|
+
return {
|
|
50
|
+
ImportDeclaration(node) {
|
|
51
|
+
const moduleName = node.source?.value;
|
|
52
|
+
if (typeof moduleName !== 'string') return;
|
|
53
|
+
if (!BARREL_PATTERN.test(moduleName)) return;
|
|
54
|
+
if (isTypeOnlyImport(node)) return;
|
|
55
|
+
|
|
56
|
+
context.report({
|
|
57
|
+
node,
|
|
58
|
+
messageId: 'noValueImport',
|
|
59
|
+
data: { module: moduleName },
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export default {
|
|
67
|
+
rules: {
|
|
68
|
+
'no-generated-models-barrel-value-import': noGeneratedModelsBarrelValueImportRule,
|
|
69
|
+
},
|
|
70
|
+
};
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom ESLint Plugin: No Null/Undefined Check
|
|
3
|
+
*
|
|
4
|
+
* Prohibits direct null/undefined checks (!== null, === null, !== undefined, === undefined)
|
|
5
|
+
* and requires using isValueDefined() or isNullOrUndefined() from '@dloizides/utils' instead.
|
|
6
|
+
*
|
|
7
|
+
* These utility functions handle both null and undefined, providing
|
|
8
|
+
* consistent and safer null checking across the codebase.
|
|
9
|
+
*
|
|
10
|
+
* Examples:
|
|
11
|
+
* BAD: if (item !== null) { ... }
|
|
12
|
+
* BAD: if (item !== undefined) { ... }
|
|
13
|
+
* GOOD: if (isValueDefined(item)) { ... }
|
|
14
|
+
*
|
|
15
|
+
* BAD: if (item === null) { ... }
|
|
16
|
+
* BAD: if (item === undefined) { ... }
|
|
17
|
+
* GOOD: if (!isValueDefined(item)) { ... }
|
|
18
|
+
* GOOD: if (isNullOrUndefined(item)) { ... }
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const noNullCheckRule = {
|
|
22
|
+
meta: {
|
|
23
|
+
type: 'suggestion',
|
|
24
|
+
docs: {
|
|
25
|
+
description:
|
|
26
|
+
'Disallow direct null/undefined checks. Use isValueDefined() or isNullOrUndefined() instead.',
|
|
27
|
+
category: 'Best Practices',
|
|
28
|
+
recommended: true,
|
|
29
|
+
},
|
|
30
|
+
fixable: 'code',
|
|
31
|
+
schema: [
|
|
32
|
+
{
|
|
33
|
+
type: 'object',
|
|
34
|
+
properties: {
|
|
35
|
+
utilImportPath: {
|
|
36
|
+
type: 'string',
|
|
37
|
+
description: 'Import path for the utility functions',
|
|
38
|
+
default: '@dloizides/utils',
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
additionalProperties: false,
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
messages: {
|
|
45
|
+
noNotEqualsNull:
|
|
46
|
+
'Avoid "!== null" checks. Use isValueDefined({{identifier}}) instead, which handles both null and undefined.',
|
|
47
|
+
noEqualsNull:
|
|
48
|
+
'Avoid "=== null" checks. Use !isValueDefined({{identifier}}) or isNullOrUndefined({{identifier}}) instead.',
|
|
49
|
+
noNotEqualsNullComplex:
|
|
50
|
+
'Avoid "!== null" checks. Use isValueDefined() instead, which handles both null and undefined.',
|
|
51
|
+
noEqualsNullComplex:
|
|
52
|
+
'Avoid "=== null" checks. Use !isValueDefined() or isNullOrUndefined() instead.',
|
|
53
|
+
noNotEqualsUndefined:
|
|
54
|
+
'Avoid "!== undefined" checks. Use isValueDefined({{identifier}}) instead, which handles both null and undefined.',
|
|
55
|
+
noEqualsUndefined:
|
|
56
|
+
'Avoid "=== undefined" checks. Use !isValueDefined({{identifier}}) or isNullOrUndefined({{identifier}}) instead.',
|
|
57
|
+
noNotEqualsUndefinedComplex:
|
|
58
|
+
'Avoid "!== undefined" checks. Use isValueDefined() instead, which handles both null and undefined.',
|
|
59
|
+
noEqualsUndefinedComplex:
|
|
60
|
+
'Avoid "=== undefined" checks. Use !isValueDefined() or isNullOrUndefined() instead.',
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
|
|
64
|
+
create(context) {
|
|
65
|
+
const sourceCode = context.sourceCode || context.getSourceCode();
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Check if a node is a null literal
|
|
69
|
+
*/
|
|
70
|
+
function isNullLiteral(node) {
|
|
71
|
+
return node && node.type === 'Literal' && node.value === null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Check if a node is an undefined identifier
|
|
76
|
+
*/
|
|
77
|
+
function isUndefinedIdentifier(node) {
|
|
78
|
+
return node && node.type === 'Identifier' && node.name === 'undefined';
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Get the source text for a node
|
|
83
|
+
*/
|
|
84
|
+
function getNodeText(node) {
|
|
85
|
+
return sourceCode.getText(node);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Check if the identifier is simple enough for a clean message
|
|
90
|
+
*/
|
|
91
|
+
function isSimpleIdentifier(node) {
|
|
92
|
+
return (
|
|
93
|
+
node.type === 'Identifier' ||
|
|
94
|
+
(node.type === 'MemberExpression' && !node.computed)
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Handle binary expressions (===, !==)
|
|
100
|
+
*/
|
|
101
|
+
function checkBinaryExpression(node) {
|
|
102
|
+
const { operator, left, right } = node;
|
|
103
|
+
|
|
104
|
+
// Only handle === and !==
|
|
105
|
+
if (operator !== '===' && operator !== '!==') {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Check if either side is null or undefined
|
|
110
|
+
const leftIsNull = isNullLiteral(left);
|
|
111
|
+
const rightIsNull = isNullLiteral(right);
|
|
112
|
+
const leftIsUndefined = isUndefinedIdentifier(left);
|
|
113
|
+
const rightIsUndefined = isUndefinedIdentifier(right);
|
|
114
|
+
|
|
115
|
+
const isNullCheck = leftIsNull || rightIsNull;
|
|
116
|
+
const isUndefinedCheck = leftIsUndefined || rightIsUndefined;
|
|
117
|
+
|
|
118
|
+
if (!isNullCheck && !isUndefinedCheck) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Get the non-null/undefined operand
|
|
123
|
+
const valueNode = (leftIsNull || leftIsUndefined) ? right : left;
|
|
124
|
+
const valueText = getNodeText(valueNode);
|
|
125
|
+
const isSimple = isSimpleIdentifier(valueNode);
|
|
126
|
+
|
|
127
|
+
if (operator === '!==') {
|
|
128
|
+
// item !== null/undefined -> isValueDefined(item)
|
|
129
|
+
const messageId = isNullCheck
|
|
130
|
+
? (isSimple ? 'noNotEqualsNull' : 'noNotEqualsNullComplex')
|
|
131
|
+
: (isSimple ? 'noNotEqualsUndefined' : 'noNotEqualsUndefinedComplex');
|
|
132
|
+
|
|
133
|
+
context.report({
|
|
134
|
+
node,
|
|
135
|
+
messageId,
|
|
136
|
+
data: isSimple ? { identifier: valueText } : {},
|
|
137
|
+
fix(fixer) {
|
|
138
|
+
return fixer.replaceText(node, `isValueDefined(${valueText})`);
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
} else {
|
|
142
|
+
// item === null/undefined -> !isValueDefined(item)
|
|
143
|
+
const messageId = isNullCheck
|
|
144
|
+
? (isSimple ? 'noEqualsNull' : 'noEqualsNullComplex')
|
|
145
|
+
: (isSimple ? 'noEqualsUndefined' : 'noEqualsUndefinedComplex');
|
|
146
|
+
|
|
147
|
+
context.report({
|
|
148
|
+
node,
|
|
149
|
+
messageId,
|
|
150
|
+
data: isSimple ? { identifier: valueText } : {},
|
|
151
|
+
fix(fixer) {
|
|
152
|
+
return fixer.replaceText(node, `!isValueDefined(${valueText})`);
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
BinaryExpression: checkBinaryExpression,
|
|
160
|
+
};
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Export the plugin
|
|
166
|
+
*/
|
|
167
|
+
export default {
|
|
168
|
+
rules: {
|
|
169
|
+
'no-null-check': noNullCheckRule,
|
|
170
|
+
},
|
|
171
|
+
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ESLint rule: no-optional-undefined
|
|
3
|
+
*
|
|
4
|
+
* Prevents declaring optional properties/parameters with explicit `| undefined`
|
|
5
|
+
* in the type annotation. With `exactOptionalPropertyTypes: true`, `?: T` and
|
|
6
|
+
* `?: T | undefined` are semantically different, but the correct practice is to
|
|
7
|
+
* use `?: T` and fix the assignment side (omit the key) rather than widening the
|
|
8
|
+
* type with `| undefined`.
|
|
9
|
+
*
|
|
10
|
+
* Bad: pageCount?: number | undefined;
|
|
11
|
+
* Good: pageCount?: number;
|
|
12
|
+
*
|
|
13
|
+
* Bad: function foo(bar?: string | undefined) {}
|
|
14
|
+
* Good: function foo(bar?: string) {}
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** @type {import('eslint').ESLint.Plugin} */
|
|
18
|
+
export default {
|
|
19
|
+
rules: {
|
|
20
|
+
'no-optional-undefined': {
|
|
21
|
+
meta: {
|
|
22
|
+
type: 'suggestion',
|
|
23
|
+
docs: {
|
|
24
|
+
description:
|
|
25
|
+
'Disallow combining optional marker (?) with `| undefined` in type annotations',
|
|
26
|
+
},
|
|
27
|
+
messages: {
|
|
28
|
+
noOptionalUndefined:
|
|
29
|
+
'Do not combine optional (?) with `| undefined`. Use `?: {{type}}` and omit the key when the value is undefined.',
|
|
30
|
+
},
|
|
31
|
+
schema: [],
|
|
32
|
+
},
|
|
33
|
+
create(context) {
|
|
34
|
+
/**
|
|
35
|
+
* Check if a type annotation is a union containing `undefined`.
|
|
36
|
+
* Returns the cleaned type string (without undefined) if found,
|
|
37
|
+
* otherwise returns null.
|
|
38
|
+
*/
|
|
39
|
+
function getTypeWithoutUndefined(typeAnnotation) {
|
|
40
|
+
if (!typeAnnotation || typeAnnotation.type !== 'TSUnionType') return null;
|
|
41
|
+
|
|
42
|
+
const hasUndefined = typeAnnotation.types.some(
|
|
43
|
+
(t) => t.type === 'TSUndefinedKeyword',
|
|
44
|
+
);
|
|
45
|
+
if (!hasUndefined) return null;
|
|
46
|
+
|
|
47
|
+
const nonUndefinedTypes = typeAnnotation.types.filter(
|
|
48
|
+
(t) => t.type !== 'TSUndefinedKeyword',
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
const sourceCode = context.sourceCode ?? context.getSourceCode();
|
|
52
|
+
if (nonUndefinedTypes.length === 1)
|
|
53
|
+
return sourceCode.getText(nonUndefinedTypes[0]);
|
|
54
|
+
|
|
55
|
+
return nonUndefinedTypes.map((t) => sourceCode.getText(t)).join(' | ');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function checkNode(node, typeAnnotationNode) {
|
|
59
|
+
if (!node.optional) return;
|
|
60
|
+
if (!typeAnnotationNode) return;
|
|
61
|
+
|
|
62
|
+
const annotation =
|
|
63
|
+
typeAnnotationNode.typeAnnotation ?? typeAnnotationNode;
|
|
64
|
+
const cleanType = getTypeWithoutUndefined(annotation);
|
|
65
|
+
if (cleanType === null) return;
|
|
66
|
+
|
|
67
|
+
context.report({
|
|
68
|
+
node: typeAnnotationNode,
|
|
69
|
+
messageId: 'noOptionalUndefined',
|
|
70
|
+
data: { type: cleanType },
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
// Interface / type alias properties: foo?: string | undefined
|
|
76
|
+
TSPropertySignature(node) {
|
|
77
|
+
checkNode(node, node.typeAnnotation?.typeAnnotation);
|
|
78
|
+
},
|
|
79
|
+
// Function parameters: function foo(bar?: string | undefined)
|
|
80
|
+
// Arrow functions, methods, etc.
|
|
81
|
+
Identifier(node) {
|
|
82
|
+
if (!node.optional) return;
|
|
83
|
+
// Only check function parameters
|
|
84
|
+
const parent = node.parent;
|
|
85
|
+
if (!parent) return;
|
|
86
|
+
const isParam =
|
|
87
|
+
(parent.type === 'FunctionDeclaration' ||
|
|
88
|
+
parent.type === 'FunctionExpression' ||
|
|
89
|
+
parent.type === 'ArrowFunctionExpression') &&
|
|
90
|
+
parent.params.includes(node);
|
|
91
|
+
if (!isParam) return;
|
|
92
|
+
|
|
93
|
+
checkNode(node, node.typeAnnotation?.typeAnnotation);
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
};
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom ESLint Plugin: No Product Imports in Shared
|
|
3
|
+
*
|
|
4
|
+
* Prevents product-specific imports inside `src/components/`.
|
|
5
|
+
* Shared components must not depend on product modules, feature
|
|
6
|
+
* directories, or product-specific auto-generated API hooks.
|
|
7
|
+
* This ensures shared components remain reusable across all products.
|
|
8
|
+
*
|
|
9
|
+
* Banned import sources (when the importing file is under src/components/):
|
|
10
|
+
* - @baseclient/*-module (except identity-module, which is shared infra)
|
|
11
|
+
* - server/autoGeneratedHooks/{onlinemenu,questioner,content,notification}/
|
|
12
|
+
* - src/features/ or src/modules/
|
|
13
|
+
*
|
|
14
|
+
* Allowed:
|
|
15
|
+
* - @baseclient/core, @baseclient/identity-client (shared infra)
|
|
16
|
+
* - src/shared/, src/lib/, src/localization/, src/components/
|
|
17
|
+
* - External packages (react, react-native, etc.)
|
|
18
|
+
* - Relative imports within src/components/
|
|
19
|
+
*
|
|
20
|
+
* Examples:
|
|
21
|
+
* BAD: import { X } from '@baseclient/onlinemenu-module';
|
|
22
|
+
* BAD: import { Y } from '../../server/autoGeneratedHooks/questioner/...';
|
|
23
|
+
* BAD: import type { Z } from '@baseclient/questioner-module';
|
|
24
|
+
* GOOD: import { A } from '@baseclient/core';
|
|
25
|
+
* GOOD: import { B } from '../../shared/utils';
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { posix, sep } from 'path';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Scoped @baseclient packages that are shared infrastructure
|
|
32
|
+
* and allowed in src/components/.
|
|
33
|
+
*/
|
|
34
|
+
const ALLOWED_BASECLIENT_PACKAGES = new Set([
|
|
35
|
+
'@baseclient/core',
|
|
36
|
+
'@baseclient/identity-client',
|
|
37
|
+
'@baseclient/identity-module',
|
|
38
|
+
]);
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Product-specific auto-generated hook directory names under
|
|
42
|
+
* server/autoGeneratedHooks/ that are banned.
|
|
43
|
+
*/
|
|
44
|
+
const PRODUCT_HOOK_DIRS = new Set([
|
|
45
|
+
'onlinemenu',
|
|
46
|
+
'questioner',
|
|
47
|
+
'content',
|
|
48
|
+
'notification',
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Normalize a file path to use forward slashes for consistent matching.
|
|
53
|
+
*/
|
|
54
|
+
function normalizePath(filePath) {
|
|
55
|
+
return filePath.split(sep).join(posix.sep);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Check whether a file path is inside src/components/.
|
|
60
|
+
*/
|
|
61
|
+
function isInsideSharedComponents(filePath) {
|
|
62
|
+
const normalized = normalizePath(filePath);
|
|
63
|
+
return normalized.includes('/src/components/');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Check whether an import source is a banned @baseclient product module.
|
|
68
|
+
* Bans any @baseclient/*-module package except those in the allow list.
|
|
69
|
+
*/
|
|
70
|
+
function isBannedBaseclientModule(source) {
|
|
71
|
+
if (!source.startsWith('@baseclient/')) return false;
|
|
72
|
+
if (ALLOWED_BASECLIENT_PACKAGES.has(source)) return false;
|
|
73
|
+
return source.endsWith('-module');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Check whether an import source resolves to a product-specific
|
|
78
|
+
* auto-generated hooks directory.
|
|
79
|
+
*/
|
|
80
|
+
function isBannedAutoGeneratedHook(source) {
|
|
81
|
+
const normalized = normalizePath(source);
|
|
82
|
+
for (const dir of PRODUCT_HOOK_DIRS) {
|
|
83
|
+
if (normalized.includes(`autoGeneratedHooks/${dir}`)) return true;
|
|
84
|
+
if (normalized.includes(`autoGeneratedHooks/${dir}/`)) return true;
|
|
85
|
+
}
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Check whether an import source points to src/features/ or src/modules/.
|
|
91
|
+
*/
|
|
92
|
+
function isBannedFeatureOrModulePath(source) {
|
|
93
|
+
const normalized = normalizePath(source);
|
|
94
|
+
return (
|
|
95
|
+
normalized.includes('/src/features/') ||
|
|
96
|
+
normalized.includes('/src/modules/') ||
|
|
97
|
+
normalized.includes('/features/') ||
|
|
98
|
+
normalized.includes('/modules/')
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const noProductImportsInSharedRule = {
|
|
103
|
+
meta: {
|
|
104
|
+
type: 'problem',
|
|
105
|
+
docs: {
|
|
106
|
+
description:
|
|
107
|
+
'Disallow product-specific imports in src/components/. ' +
|
|
108
|
+
'Shared components must not depend on product modules.',
|
|
109
|
+
category: 'Best Practices',
|
|
110
|
+
recommended: true,
|
|
111
|
+
},
|
|
112
|
+
schema: [],
|
|
113
|
+
messages: {
|
|
114
|
+
productImportInShared:
|
|
115
|
+
'Shared components in src/components/ must not import from ' +
|
|
116
|
+
'product-specific modules ({{source}}). Move product-specific ' +
|
|
117
|
+
'logic to the feature module, or extract shared types to ' +
|
|
118
|
+
'@baseclient/core.',
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
|
|
122
|
+
create(context) {
|
|
123
|
+
const filePath = context.filename || context.getFilename();
|
|
124
|
+
if (!filePath) return {};
|
|
125
|
+
|
|
126
|
+
// Only apply to files inside src/components/
|
|
127
|
+
if (!isInsideSharedComponents(filePath)) return {};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Check a single import/export declaration for banned sources.
|
|
131
|
+
*/
|
|
132
|
+
function checkImportSource(node) {
|
|
133
|
+
const source = node.source;
|
|
134
|
+
if (!source) return;
|
|
135
|
+
|
|
136
|
+
const importPath = source.value;
|
|
137
|
+
if (typeof importPath !== 'string') return;
|
|
138
|
+
|
|
139
|
+
const isBanned =
|
|
140
|
+
isBannedBaseclientModule(importPath) ||
|
|
141
|
+
isBannedAutoGeneratedHook(importPath) ||
|
|
142
|
+
isBannedFeatureOrModulePath(importPath);
|
|
143
|
+
|
|
144
|
+
if (isBanned)
|
|
145
|
+
context.report({
|
|
146
|
+
node,
|
|
147
|
+
messageId: 'productImportInShared',
|
|
148
|
+
data: { source: importPath },
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return {
|
|
153
|
+
ImportDeclaration: checkImportSource,
|
|
154
|
+
ExportNamedDeclaration: checkImportSource,
|
|
155
|
+
ExportAllDeclaration: checkImportSource,
|
|
156
|
+
};
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
export default {
|
|
161
|
+
rules: {
|
|
162
|
+
'no-product-imports-in-shared': noProductImportsInSharedRule,
|
|
163
|
+
},
|
|
164
|
+
};
|