diff --git a/lib/rules/no-tracked-properties-from-args.js b/lib/rules/no-tracked-properties-from-args.js index 86354f4078..45b9f22471 100644 --- a/lib/rules/no-tracked-properties-from-args.js +++ b/lib/rules/no-tracked-properties-from-args.js @@ -1,5 +1,7 @@ 'use strict'; +const { startsWithThisExpression } = require('../utils/utils'); +const { nodeToDependentKey } = require('../utils/property-getter'); const { getImportIdentifier } = require('../utils/import'); const { isClassPropertyOrPropertyDefinitionWithDecorator } = require('../utils/decorators'); @@ -12,9 +14,9 @@ function visitClassPropertyOrPropertyDefinition(node, context, trackedImportName isClassPropertyOrPropertyDefinitionWithDecorator(node, trackedImportName); const hasThisArgsValue = - node.value.object?.type === 'MemberExpression' && - node.value.object?.object.type === 'ThisExpression' && - node.value.object?.property.name === 'args'; + node.value && + startsWithThisExpression(node.value) && + nodeToDependentKey(node.value, context).split('.')[0] === 'args'; if (hasTrackedDecorator && hasThisArgsValue) { context.report({ node, messageId: 'main' }); diff --git a/tests/lib/rules/no-tracked-properties-from-args.js b/tests/lib/rules/no-tracked-properties-from-args.js index 9d79379358..0309875468 100644 --- a/tests/lib/rules/no-tracked-properties-from-args.js +++ b/tests/lib/rules/no-tracked-properties-from-args.js @@ -62,8 +62,35 @@ ruleTester.run('no-tracked-properties-from-args', rule, { @fooTracked test = this.notArgs.args.test } `, + ` + import { tracked } from '@glimmer/tracking'; + + class Test { + @tracked test = this.args2.test + } + `, + ` + class Test{ + notInitializedProperty; + } + `, ], invalid: [ + { + code: ` + import { tracked } from '@glimmer/tracking' + + class Test { + @tracked test = this.args; + }`, + output: null, + errors: [ + { + messageId: 'main', + // type could be ClassProperty (ESLint v7) or PropertyDefinition (ESLint v8) + }, + ], + }, { code: ` import { tracked } from '@glimmer/tracking'