@typescript-eslint/eslint-plugin 8.51.1-alpha.12 → 8.51.1-alpha.13

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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typescript-eslint/eslint-plugin",
3
- "version": "8.51.1-alpha.12",
3
+ "version": "8.51.1-alpha.13",
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.12",
63
- "@typescript-eslint/type-utils": "8.51.1-alpha.12",
64
- "@typescript-eslint/utils": "8.51.1-alpha.12",
65
- "@typescript-eslint/visitor-keys": "8.51.1-alpha.12",
62
+ "@typescript-eslint/scope-manager": "8.51.1-alpha.13",
63
+ "@typescript-eslint/type-utils": "8.51.1-alpha.13",
64
+ "@typescript-eslint/utils": "8.51.1-alpha.13",
65
+ "@typescript-eslint/visitor-keys": "8.51.1-alpha.13",
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.12",
74
- "@typescript-eslint/rule-tester": "8.51.1-alpha.12",
73
+ "@typescript-eslint/rule-schema-to-typescript-types": "8.51.1-alpha.13",
74
+ "@typescript-eslint/rule-tester": "8.51.1-alpha.13",
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.12",
93
+ "@typescript-eslint/parser": "^8.51.1-alpha.13",
94
94
  "eslint": "^8.57.0 || ^9.0.0",
95
95
  "typescript": ">=4.8.4 <6.0.0"
96
96
  },