@typescript-eslint/eslint-plugin 8.51.1-alpha.8 → 8.52.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.
@@ -213,30 +213,19 @@ exports.default = (0, util_1.createRule)({
213
213
  if (checker.isArrayType(type)) {
214
214
  return collectArrayCertainty(type, new Set([...visited, type]));
215
215
  }
216
- const toString = checker.getPropertyOfType(type, 'toString') ??
217
- checker.getPropertyOfType(type, 'toLocaleString');
218
- if (!toString) {
219
- // unknown
220
- if (option.checkUnknown && type.flags === ts.TypeFlags.Unknown) {
221
- return Usefulness.Sometimes;
222
- }
223
- // e.g. any
224
- return Usefulness.Always;
225
- }
226
- const declarations = toString.getDeclarations();
227
- // eslint-disable-next-line @typescript-eslint/prefer-optional-chain
228
- if (declarations == null || declarations.length !== 1) {
229
- // If there are multiple declarations, at least one of them must not be
230
- // the default object toString.
231
- //
232
- // This may only matter for older versions of TS
233
- // see https://github.com/typescript-eslint/typescript-eslint/issues/8585
234
- return Usefulness.Always;
216
+ switch (isToStringLikeFromObject(type)) {
217
+ case undefined:
218
+ // unknown
219
+ if (option.checkUnknown && type.flags === ts.TypeFlags.Unknown) {
220
+ return Usefulness.Sometimes;
221
+ }
222
+ // e.g. any
223
+ return Usefulness.Always;
224
+ case true:
225
+ return Usefulness.Never;
226
+ case false:
227
+ return Usefulness.Always;
235
228
  }
236
- const declaration = declarations[0];
237
- const isBaseToString = ts.isInterfaceDeclaration(declaration.parent) &&
238
- declaration.parent.name.text === 'Object';
239
- return isBaseToString ? Usefulness.Never : Usefulness.Always;
240
229
  }
241
230
  function isBuiltInStringCall(node) {
242
231
  if (node.callee.type === utils_1.AST_NODE_TYPES.Identifier &&
@@ -250,6 +239,53 @@ exports.default = (0, util_1.createRule)({
250
239
  }
251
240
  return false;
252
241
  }
242
+ function isSymbolToPrimitiveMethod(node) {
243
+ return (ts.isMethodSignature(node) &&
244
+ ts.isComputedPropertyName(node.name) &&
245
+ ts.isPropertyAccessExpression(node.name.expression) &&
246
+ ts.isIdentifier(node.name.expression.expression) &&
247
+ node.name.expression.expression.text === 'Symbol' &&
248
+ ts.isIdentifier(node.name.expression.name) &&
249
+ node.name.expression.name.text === 'toPrimitive' &&
250
+ checker
251
+ .getSymbolAtLocation(node.name.expression.expression)
252
+ ?.valueDeclaration?.getSourceFile().hasNoDefaultLib);
253
+ }
254
+ function isToStringLikeFromObject(type) {
255
+ // An explicit [Symbol.toPrimitive] declaration is always user-defined
256
+ if (type
257
+ .getProperties()
258
+ .some(property => property.valueDeclaration &&
259
+ isSymbolToPrimitiveMethod(property.valueDeclaration))) {
260
+ return false;
261
+ }
262
+ // Otherwise, we check for known methods used in type coercion.
263
+ // We'll try to find one that's not declared on Object itself.
264
+ // Failing that, we'll fall back to one that is.
265
+ let foundFallbackOnObject = false;
266
+ for (const propertyName of ['toLocaleString', 'toString', 'valueOf']) {
267
+ const candidate = checker.getPropertyOfType(type, propertyName);
268
+ if (!candidate) {
269
+ continue;
270
+ }
271
+ const declarations = candidate.getDeclarations();
272
+ // If there are multiple declarations, at least one of them must not be
273
+ // the default object toString.
274
+ //
275
+ // This may only matter for older versions of TS
276
+ // see https://github.com/typescript-eslint/typescript-eslint/issues/8585
277
+ if (declarations?.length !== 1) {
278
+ continue;
279
+ }
280
+ // Not being the Object interface means this is user-defined.
281
+ if (!ts.isInterfaceDeclaration(declarations[0].parent) ||
282
+ declarations[0].parent.name.text !== 'Object') {
283
+ return false;
284
+ }
285
+ foundFallbackOnObject = true;
286
+ }
287
+ return foundFallbackOnObject ? true : undefined;
288
+ }
253
289
  return {
254
290
  'AssignmentExpression[operator = "+="], BinaryExpression[operator = "+"]'(node) {
255
291
  const leftType = services.getTypeAtLocation(node.left);
@@ -66,13 +66,6 @@ exports.default = (0, util_1.createRule)({
66
66
  .unionConstituents(type)
67
67
  .some(part => (0, util_1.isTypeFlagSet)(part, ts.TypeFlags.Undefined));
68
68
  }
69
- function getPropertyType(objectType, propertyName) {
70
- const symbol = objectType.getProperty(propertyName);
71
- if (!symbol) {
72
- return null;
73
- }
74
- return checker.getTypeOfSymbol(symbol);
75
- }
76
69
  function getArrayElementType(arrayType, elementIndex) {
77
70
  if (checker.isTupleType(arrayType)) {
78
71
  const tupleArgs = checker.getTypeArguments(arrayType);
@@ -167,7 +160,26 @@ exports.default = (0, util_1.createRule)({
167
160
  if (!propertyName) {
168
161
  return null;
169
162
  }
170
- return getPropertyType(sourceType, propertyName);
163
+ const symbol = sourceType.getProperty(propertyName);
164
+ if (!symbol) {
165
+ return null;
166
+ }
167
+ if (symbol.flags & ts.SymbolFlags.Optional &&
168
+ hasConditionalInitializer(objectPattern)) {
169
+ return null;
170
+ }
171
+ return checker.getTypeOfSymbol(symbol);
172
+ }
173
+ function hasConditionalInitializer(node) {
174
+ const parent = node.parent;
175
+ if (!parent) {
176
+ return false;
177
+ }
178
+ if (parent.type === utils_1.AST_NODE_TYPES.VariableDeclarator && parent.init) {
179
+ return (parent.init.type === utils_1.AST_NODE_TYPES.ConditionalExpression ||
180
+ parent.init.type === utils_1.AST_NODE_TYPES.LogicalExpression);
181
+ }
182
+ return hasConditionalInitializer(parent);
171
183
  }
172
184
  function getSourceTypeForPattern(pattern) {
173
185
  const parent = (0, util_1.nullThrows)(pattern.parent, util_1.NullThrowsReasons.MissingParent);
@@ -189,12 +201,11 @@ exports.default = (0, util_1.createRule)({
189
201
  return getTypeOfProperty(parent);
190
202
  }
191
203
  if (parent.type === utils_1.AST_NODE_TYPES.ArrayPattern) {
192
- const arrayPattern = parent;
193
- const arrayType = getSourceTypeForPattern(arrayPattern);
204
+ const arrayType = getSourceTypeForPattern(parent);
194
205
  if (!arrayType) {
195
206
  return null;
196
207
  }
197
- const elementIndex = arrayPattern.elements.indexOf(pattern);
208
+ const elementIndex = parent.elements.indexOf(pattern);
198
209
  return getArrayElementType(arrayType, elementIndex);
199
210
  }
200
211
  return null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typescript-eslint/eslint-plugin",
3
- "version": "8.51.1-alpha.8",
3
+ "version": "8.52.0",
4
4
  "description": "TypeScript plugin for ESLint",
5
5
  "files": [
6
6
  "dist",
@@ -59,10 +59,10 @@
59
59
  },
60
60
  "dependencies": {
61
61
  "@eslint-community/regexpp": "^4.12.2",
62
- "@typescript-eslint/scope-manager": "8.51.1-alpha.8",
63
- "@typescript-eslint/type-utils": "8.51.1-alpha.8",
64
- "@typescript-eslint/utils": "8.51.1-alpha.8",
65
- "@typescript-eslint/visitor-keys": "8.51.1-alpha.8",
62
+ "@typescript-eslint/scope-manager": "8.52.0",
63
+ "@typescript-eslint/type-utils": "8.52.0",
64
+ "@typescript-eslint/utils": "8.52.0",
65
+ "@typescript-eslint/visitor-keys": "8.52.0",
66
66
  "ignore": "^7.0.5",
67
67
  "natural-compare": "^1.4.0",
68
68
  "ts-api-utils": "^2.4.0"
@@ -70,8 +70,8 @@
70
70
  "devDependencies": {
71
71
  "@types/mdast": "^4.0.4",
72
72
  "@types/natural-compare": "*",
73
- "@typescript-eslint/rule-schema-to-typescript-types": "8.51.1-alpha.8",
74
- "@typescript-eslint/rule-tester": "8.51.1-alpha.8",
73
+ "@typescript-eslint/rule-schema-to-typescript-types": "8.52.0",
74
+ "@typescript-eslint/rule-tester": "8.52.0",
75
75
  "@vitest/coverage-v8": "^3.2.4",
76
76
  "ajv": "^6.12.6",
77
77
  "eslint": "*",
@@ -90,7 +90,7 @@
90
90
  "vitest": "^3.2.4"
91
91
  },
92
92
  "peerDependencies": {
93
- "@typescript-eslint/parser": "^8.51.1-alpha.8",
93
+ "@typescript-eslint/parser": "^8.52.0",
94
94
  "eslint": "^8.57.0 || ^9.0.0",
95
95
  "typescript": ">=4.8.4 <6.0.0"
96
96
  },