eslint-plugin-class-validator-type-match 3.1.2 → 3.1.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.
|
@@ -6,6 +6,15 @@ const type_helpers_util_1 = require("../utils/type-helpers.util");
|
|
|
6
6
|
* Creates an ESLint rule with proper documentation URL
|
|
7
7
|
*/
|
|
8
8
|
const createRule = utils_1.ESLintUtils.RuleCreator((name) => `https://github.com/robertlinde/eslint-plugin-class-validator-type-match#${name}`);
|
|
9
|
+
/**
|
|
10
|
+
* Maps JavaScript wrapper class names to their corresponding primitive type names.
|
|
11
|
+
* Used to validate @Type(() => Number) with `number` type, etc.
|
|
12
|
+
*/
|
|
13
|
+
const PRIMITIVE_WRAPPER_MAP = {
|
|
14
|
+
Number: 'number',
|
|
15
|
+
String: 'string',
|
|
16
|
+
Boolean: 'boolean',
|
|
17
|
+
};
|
|
9
18
|
/**
|
|
10
19
|
* ESLint rule to ensure @Type(() => ClassName) decorator matches TypeScript type annotations.
|
|
11
20
|
*
|
|
@@ -129,15 +138,18 @@ exports.default = createRule({
|
|
|
129
138
|
}
|
|
130
139
|
}
|
|
131
140
|
else {
|
|
132
|
-
// @Type is used with a primitive type
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
+
// @Type is used with a primitive type - check if it's a valid wrapper class match
|
|
142
|
+
const expectedPrimitive = PRIMITIVE_WRAPPER_MAP[typeClassName];
|
|
143
|
+
if (!expectedPrimitive || expectedPrimitive !== actualType) {
|
|
144
|
+
context.report({
|
|
145
|
+
node,
|
|
146
|
+
messageId: 'typeMismatch',
|
|
147
|
+
data: {
|
|
148
|
+
typeDecoratorClass: typeClassName,
|
|
149
|
+
actualType,
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
}
|
|
141
153
|
}
|
|
142
154
|
}
|
|
143
155
|
// For array types, check if @Type matches the array element type
|
|
@@ -158,17 +170,20 @@ exports.default = createRule({
|
|
|
158
170
|
}
|
|
159
171
|
}
|
|
160
172
|
else {
|
|
161
|
-
// @Type is used with an array of primitives
|
|
173
|
+
// @Type is used with an array of primitives - check if it's a valid wrapper class match
|
|
162
174
|
const elementType = (0, type_helpers_util_1.getTypeString)(elementTypeNode, checker, esTreeNodeMap);
|
|
163
175
|
if (elementType) {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
176
|
+
const expectedPrimitive = PRIMITIVE_WRAPPER_MAP[typeClassName];
|
|
177
|
+
if (!expectedPrimitive || expectedPrimitive !== elementType) {
|
|
178
|
+
context.report({
|
|
179
|
+
node,
|
|
180
|
+
messageId: 'typeMismatch',
|
|
181
|
+
data: {
|
|
182
|
+
typeDecoratorClass: typeClassName,
|
|
183
|
+
actualType: `${elementType}[]`,
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
}
|
|
172
187
|
}
|
|
173
188
|
}
|
|
174
189
|
}
|