From c2202cc8b382eb092d10f6a80838c89d59c244e8 Mon Sep 17 00:00:00 2001 From: jjohnnystwsss <33243683+jjohnnystwsss@users.noreply.github.com> Date: Fri, 20 Nov 2026 03:58:12 +0800 Subject: [PATCH] Fix `no-get` autofixes for `firstObject`/`lastObject`-like names, leading-zero segments and template tags `firstObject` and `lastObject` were replaced as substrings, so names like `firstObjectId`, `lastObjectId` or `x_lastObject` were rewritten into invalid code (`this.items[0]Id`) or a method call (`this.x_at(-1)`). They are now only replaced as whole path segments. A numeric segment with a leading zero (`a.01`) became a legacy octal literal (`this.a[01]`), which is a syntax error in strict mode code. It now uses a string key (`this.a['01']`). When the `get` call is the tag of a tagged template, a fix that uses optional chaining is wrapped in parentheses (`(this.a?.b)`tpl``), since optional chaining is not allowed in a template tag. Part of #2889 (item 1). Co-Authored-By: Claude Opus 5.5 --- lib/rules/no-get.js | 24 ++++++++++++++++------ tests/lib/rules/no-get.js | 42 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/lib/rules/no-get.js b/lib/rules/no-get.js index 78b9d1c0e0..7b420f241c 100644 --- a/lib/rules/no-get.js +++ b/lib/rules/no-get.js @@ -12,7 +12,8 @@ const ERROR_MESSAGE_GET_PROPERTIES = "Use `{ prop1: this.prop1, prop2: this.prop2, ... }` instead of Ember's `getProperties` function"; const VALID_JS_VARIABLE_NAME_REGEXP = new RegExp('^[a-zA-Z_$][0-9a-zA-Z_$]*$'); -const VALID_JS_ARRAY_INDEX_REGEXP = new RegExp(/^\d+$/); +// No leading zeros: `01` would be an octal literal, so it needs bracket access (`['01']`). +const VALID_JS_ARRAY_INDEX_REGEXP = new RegExp(/^(?:0|[1-9]\d*)$/); function isValidJSVariableName(str) { return VALID_JS_VARIABLE_NAME_REGEXP.test(str); } @@ -23,6 +24,9 @@ function isValidJSPath(str) { return str.split('.').every((part) => isValidJSVariableName(part) || isValidJSArrayIndex(part)); } +// `lastObject` as a whole path segment, not part of a name like `lastObjectId` or `x_lastObject`. +const LAST_OBJECT_SEGMENT_REGEXP = /(? 1 && !useAt) { + if (pathWithPlaceholders.match(LAST_OBJECT_SEGMENT_REGEXP)?.length > 1 && !useAt) { // Do not autofix when multiple `lastObject` are chained, and use `at(-1)` is not allowed. return null; } @@ -545,17 +557,17 @@ function convertLiteralTypePath({ // Replace any array element access using `firstObject` and `lastObject` (foo.firstObject => foo[0] or foo?.[0]). replacementPath = replacementPath - .replaceAll('.firstObject', shouldIgnoreOptionalChaining ? '[0]' : '.[0]') // When `firstObject` is used in the middle of the path. e.g. foo.firstObject + .replaceAll(/\.firstObject(?![\w$])/g, shouldIgnoreOptionalChaining ? '[0]' : '.[0]') // When `firstObject` is used in the middle of the path. e.g. foo.firstObject .replace(/^firstObject\??\./, shouldIgnoreOptionalChaining ? '[0].' : '[0]?.') // When `firstObject` is used at the beginning of the path. e.g. firstObject.bar .replace(/^firstObject$/, '[0]'); // When `firstObject` is used as the entire path. // eslint-disable-next-line unicorn/prefer-ternary if (useAt) { - replacementPath = replacementPath.replaceAll('lastObject', 'at(-1)'); + replacementPath = replacementPath.replaceAll(LAST_OBJECT_SEGMENT_REGEXP, 'at(-1)'); } else { replacementPath = replacementPath .replace( - /\??\.lastObject/, // When `lastObject` is used in the middle of the path. e.g. foo.lastObject + /\??\.lastObject(?![\w$])/, // When `lastObject` is used in the middle of the path. e.g. foo.lastObject (_, offset) => `${shouldIgnoreOptionalChaining ? '' : '?.'}[${objectText}.${replacementPath.slice( 0, diff --git a/tests/lib/rules/no-get.js b/tests/lib/rules/no-get.js index 83b356740b..0d7075de6d 100644 --- a/tests/lib/rules/no-get.js +++ b/tests/lib/rules/no-get.js @@ -997,6 +997,48 @@ ruleTester.run('no-get', rule, { }, ], }, + { + // `firstObject` / `lastObject` are only replaced as whole path segments. + code: "this.get('items.firstObjectId').x;", + output: 'this.items.firstObjectId.x;', + errors: [{ message: ERROR_MESSAGE_GET, type: 'CallExpression' }], + }, + { + code: "this.get('items.lastObjectId').x;", + output: 'this.items.lastObjectId.x;', + errors: [{ message: ERROR_MESSAGE_GET, type: 'CallExpression' }], + }, + { + code: "this.get('items.lastObjectId');", + output: 'this.items?.lastObjectId;', + options: [{ useOptionalChaining: true }], + errors: [{ message: ERROR_MESSAGE_GET, type: 'CallExpression' }], + }, + { + code: "this.get('x_lastObject');", + output: 'this.x_lastObject;', + options: [{ useOptionalChaining: true }], + errors: [{ message: ERROR_MESSAGE_GET, type: 'CallExpression' }], + }, + { + code: "this.get('lastObjectId.lastObject');", + output: 'this.lastObjectId?.[this.lastObjectId.length - 1];', + options: [{ useOptionalChaining: true, useAt: false }], + errors: [{ message: ERROR_MESSAGE_GET, type: 'CallExpression' }], + }, + { + // Numeric segments with a leading zero are string keys, not octal literals. + code: "this.get('a.01').x;", + output: "this.a['01'].x;", + errors: [{ message: ERROR_MESSAGE_GET, type: 'CallExpression' }], + }, + { + // Optional chaining is not allowed in the tag of a tagged template. + code: "this.get('a.b')`tpl`;", + output: '(this.a?.b)`tpl`;', + options: [{ useOptionalChaining: true }], + errors: [{ message: ERROR_MESSAGE_GET, type: 'CallExpression' }], + }, { // Reports violation after (classic) proxy class. code: `