@dloizides/frontend-devtools 1.0.1 → 1.0.2
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 +71 -0
- package/dist/index.d.ts +71 -0
- package/dist/index.js +2029 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2017 -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,171 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,99 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,164 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Custom ESLint Plugin: Prefer Const Enum
|
|
3
|
-
*
|
|
4
|
-
* Flags type aliases where ALL union members are string literals (2+ members)
|
|
5
|
-
* and suggests converting to const enum.
|
|
6
|
-
*
|
|
7
|
-
* Examples:
|
|
8
|
-
* FLAGGED: type Mode = 'light' | 'dark';
|
|
9
|
-
* FLAGGED: export type Status = 'active' | 'inactive' | 'pending';
|
|
10
|
-
*
|
|
11
|
-
* NOT FLAGGED:
|
|
12
|
-
* type Result = Success | Error; // Union of type references
|
|
13
|
-
* type Nullable<T> = T | null; // Contains null keyword
|
|
14
|
-
* type Mixed = 'a' | number; // Contains non-string member
|
|
15
|
-
* type Single = 'only'; // Single value, not a union
|
|
16
|
-
* type Callback = () => void; // Function type
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Convert a string literal value to a PascalCase enum member name.
|
|
21
|
-
*
|
|
22
|
-
* 'light' -> 'Light'
|
|
23
|
-
* 'modern-grid' -> 'ModernGrid'
|
|
24
|
-
* 'UPPER_CASE' -> 'UpperCase'
|
|
25
|
-
* '2xl' -> '_2xl'
|
|
26
|
-
*/
|
|
27
|
-
function toPascalCase(value) {
|
|
28
|
-
// Split on hyphens, underscores, spaces, and camelCase boundaries
|
|
29
|
-
const words = value
|
|
30
|
-
.replace(/([a-z])([A-Z])/g, '$1 $2')
|
|
31
|
-
.split(/[-_\s]+/)
|
|
32
|
-
.filter((w) => w.length > 0);
|
|
33
|
-
|
|
34
|
-
const result = words
|
|
35
|
-
.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
|
|
36
|
-
.join('');
|
|
37
|
-
|
|
38
|
-
// If starts with a digit, prefix with underscore
|
|
39
|
-
if (/^\d/.test(result)) return `_${result}`;
|
|
40
|
-
|
|
41
|
-
return result;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const preferConstEnumRule = {
|
|
45
|
-
meta: {
|
|
46
|
-
type: 'suggestion',
|
|
47
|
-
docs: {
|
|
48
|
-
description:
|
|
49
|
-
'Prefer const enum over string literal union types for better autocomplete, refactoring, and centralized definitions.',
|
|
50
|
-
category: 'Best Practices',
|
|
51
|
-
recommended: true,
|
|
52
|
-
},
|
|
53
|
-
hasSuggestions: true,
|
|
54
|
-
schema: [],
|
|
55
|
-
messages: {
|
|
56
|
-
preferConstEnum:
|
|
57
|
-
'String literal union type "{{name}}" should be a const enum. Const enums provide autocomplete, refactoring support, and centralized definitions.',
|
|
58
|
-
convertToConstEnum: 'Convert "{{name}}" to const enum',
|
|
59
|
-
},
|
|
60
|
-
},
|
|
61
|
-
|
|
62
|
-
create(context) {
|
|
63
|
-
return {
|
|
64
|
-
TSTypeAliasDeclaration(node) {
|
|
65
|
-
const { typeAnnotation } = node;
|
|
66
|
-
|
|
67
|
-
// Must be a union type
|
|
68
|
-
if (typeAnnotation.type !== 'TSUnionType') return;
|
|
69
|
-
|
|
70
|
-
const { types } = typeAnnotation;
|
|
71
|
-
|
|
72
|
-
// Must have 2+ members
|
|
73
|
-
if (types.length < 2) return;
|
|
74
|
-
|
|
75
|
-
// ALL members must be string literals
|
|
76
|
-
const allStringLiterals = types.every(
|
|
77
|
-
(member) =>
|
|
78
|
-
member.type === 'TSLiteralType' &&
|
|
79
|
-
member.literal.type === 'Literal' &&
|
|
80
|
-
typeof member.literal.value === 'string',
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
if (!allStringLiterals) return;
|
|
84
|
-
|
|
85
|
-
const typeName = node.id.name;
|
|
86
|
-
const isExported =
|
|
87
|
-
node.parent.type === 'ExportNamedDeclaration';
|
|
88
|
-
const exportPrefix = isExported ? 'export ' : '';
|
|
89
|
-
|
|
90
|
-
// Build the const enum suggestion
|
|
91
|
-
const enumMembers = types
|
|
92
|
-
.map((member) => {
|
|
93
|
-
const value = member.literal.value;
|
|
94
|
-
const memberName = toPascalCase(value);
|
|
95
|
-
return ` ${memberName} = '${value}',`;
|
|
96
|
-
})
|
|
97
|
-
.join('\n');
|
|
98
|
-
|
|
99
|
-
const replacement = `${exportPrefix}const enum ${typeName} {\n${enumMembers}\n}`;
|
|
100
|
-
|
|
101
|
-
// The node to replace depends on whether it's exported
|
|
102
|
-
const targetNode = isExported ? node.parent : node;
|
|
103
|
-
|
|
104
|
-
context.report({
|
|
105
|
-
node: node.id,
|
|
106
|
-
messageId: 'preferConstEnum',
|
|
107
|
-
data: { name: typeName },
|
|
108
|
-
suggest: [
|
|
109
|
-
{
|
|
110
|
-
messageId: 'convertToConstEnum',
|
|
111
|
-
data: { name: typeName },
|
|
112
|
-
fix(fixer) {
|
|
113
|
-
return fixer.replaceText(targetNode, replacement);
|
|
114
|
-
},
|
|
115
|
-
},
|
|
116
|
-
],
|
|
117
|
-
});
|
|
118
|
-
},
|
|
119
|
-
};
|
|
120
|
-
},
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
export default {
|
|
124
|
-
rules: {
|
|
125
|
-
'prefer-const-enum': preferConstEnumRule,
|
|
126
|
-
},
|
|
127
|
-
};
|